Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Driver for Atmel AT32 and AT91 SPI Controllers |
| 4 | * |
| 5 | * Copyright (C) 2006 Atmel Corporation |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 9 | #include <linux/clk.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/platform_device.h> |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/dma-mapping.h> |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 14 | #include <linux/dmaengine.h> |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 15 | #include <linux/err.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/spi/spi.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 19 | #include <linux/platform_data/dma-atmel.h> |
Jean-Christophe PLAGNIOL-VILLARD | 850a5b6 | 2012-11-23 13:44:39 +0100 | [diff] [blame] | 20 | #include <linux/of.h> |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 21 | |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 22 | #include <linux/io.h> |
Linus Walleij | efc92fb | 2019-01-07 16:51:52 +0100 | [diff] [blame] | 23 | #include <linux/gpio/consumer.h> |
Wenyou Yang | 5bdfd49 | 2014-03-05 09:58:49 +0800 | [diff] [blame] | 24 | #include <linux/pinctrl/consumer.h> |
Wenyou Yang | ce0c4ca | 2014-10-16 17:23:10 +0800 | [diff] [blame] | 25 | #include <linux/pm_runtime.h> |
Uwe Kleine-König | 3c0448d | 2019-08-01 22:47:10 +0200 | [diff] [blame] | 26 | #include <trace/events/spi.h> |
David Brownell | bb2d1c3 | 2007-02-20 13:58:19 -0800 | [diff] [blame] | 27 | |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 28 | /* SPI register offsets */ |
| 29 | #define SPI_CR 0x0000 |
| 30 | #define SPI_MR 0x0004 |
| 31 | #define SPI_RDR 0x0008 |
| 32 | #define SPI_TDR 0x000c |
| 33 | #define SPI_SR 0x0010 |
| 34 | #define SPI_IER 0x0014 |
| 35 | #define SPI_IDR 0x0018 |
| 36 | #define SPI_IMR 0x001c |
| 37 | #define SPI_CSR0 0x0030 |
| 38 | #define SPI_CSR1 0x0034 |
| 39 | #define SPI_CSR2 0x0038 |
| 40 | #define SPI_CSR3 0x003c |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 41 | #define SPI_FMR 0x0040 |
| 42 | #define SPI_FLR 0x0044 |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 43 | #define SPI_VERSION 0x00fc |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 44 | #define SPI_RPR 0x0100 |
| 45 | #define SPI_RCR 0x0104 |
| 46 | #define SPI_TPR 0x0108 |
| 47 | #define SPI_TCR 0x010c |
| 48 | #define SPI_RNPR 0x0110 |
| 49 | #define SPI_RNCR 0x0114 |
| 50 | #define SPI_TNPR 0x0118 |
| 51 | #define SPI_TNCR 0x011c |
| 52 | #define SPI_PTCR 0x0120 |
| 53 | #define SPI_PTSR 0x0124 |
| 54 | |
| 55 | /* Bitfields in CR */ |
| 56 | #define SPI_SPIEN_OFFSET 0 |
| 57 | #define SPI_SPIEN_SIZE 1 |
| 58 | #define SPI_SPIDIS_OFFSET 1 |
| 59 | #define SPI_SPIDIS_SIZE 1 |
| 60 | #define SPI_SWRST_OFFSET 7 |
| 61 | #define SPI_SWRST_SIZE 1 |
| 62 | #define SPI_LASTXFER_OFFSET 24 |
| 63 | #define SPI_LASTXFER_SIZE 1 |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 64 | #define SPI_TXFCLR_OFFSET 16 |
| 65 | #define SPI_TXFCLR_SIZE 1 |
| 66 | #define SPI_RXFCLR_OFFSET 17 |
| 67 | #define SPI_RXFCLR_SIZE 1 |
| 68 | #define SPI_FIFOEN_OFFSET 30 |
| 69 | #define SPI_FIFOEN_SIZE 1 |
| 70 | #define SPI_FIFODIS_OFFSET 31 |
| 71 | #define SPI_FIFODIS_SIZE 1 |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 72 | |
| 73 | /* Bitfields in MR */ |
| 74 | #define SPI_MSTR_OFFSET 0 |
| 75 | #define SPI_MSTR_SIZE 1 |
| 76 | #define SPI_PS_OFFSET 1 |
| 77 | #define SPI_PS_SIZE 1 |
| 78 | #define SPI_PCSDEC_OFFSET 2 |
| 79 | #define SPI_PCSDEC_SIZE 1 |
| 80 | #define SPI_FDIV_OFFSET 3 |
| 81 | #define SPI_FDIV_SIZE 1 |
| 82 | #define SPI_MODFDIS_OFFSET 4 |
| 83 | #define SPI_MODFDIS_SIZE 1 |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 84 | #define SPI_WDRBT_OFFSET 5 |
| 85 | #define SPI_WDRBT_SIZE 1 |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 86 | #define SPI_LLB_OFFSET 7 |
| 87 | #define SPI_LLB_SIZE 1 |
| 88 | #define SPI_PCS_OFFSET 16 |
| 89 | #define SPI_PCS_SIZE 4 |
| 90 | #define SPI_DLYBCS_OFFSET 24 |
| 91 | #define SPI_DLYBCS_SIZE 8 |
| 92 | |
| 93 | /* Bitfields in RDR */ |
| 94 | #define SPI_RD_OFFSET 0 |
| 95 | #define SPI_RD_SIZE 16 |
| 96 | |
| 97 | /* Bitfields in TDR */ |
| 98 | #define SPI_TD_OFFSET 0 |
| 99 | #define SPI_TD_SIZE 16 |
| 100 | |
| 101 | /* Bitfields in SR */ |
| 102 | #define SPI_RDRF_OFFSET 0 |
| 103 | #define SPI_RDRF_SIZE 1 |
| 104 | #define SPI_TDRE_OFFSET 1 |
| 105 | #define SPI_TDRE_SIZE 1 |
| 106 | #define SPI_MODF_OFFSET 2 |
| 107 | #define SPI_MODF_SIZE 1 |
| 108 | #define SPI_OVRES_OFFSET 3 |
| 109 | #define SPI_OVRES_SIZE 1 |
| 110 | #define SPI_ENDRX_OFFSET 4 |
| 111 | #define SPI_ENDRX_SIZE 1 |
| 112 | #define SPI_ENDTX_OFFSET 5 |
| 113 | #define SPI_ENDTX_SIZE 1 |
| 114 | #define SPI_RXBUFF_OFFSET 6 |
| 115 | #define SPI_RXBUFF_SIZE 1 |
| 116 | #define SPI_TXBUFE_OFFSET 7 |
| 117 | #define SPI_TXBUFE_SIZE 1 |
| 118 | #define SPI_NSSR_OFFSET 8 |
| 119 | #define SPI_NSSR_SIZE 1 |
| 120 | #define SPI_TXEMPTY_OFFSET 9 |
| 121 | #define SPI_TXEMPTY_SIZE 1 |
| 122 | #define SPI_SPIENS_OFFSET 16 |
| 123 | #define SPI_SPIENS_SIZE 1 |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 124 | #define SPI_TXFEF_OFFSET 24 |
| 125 | #define SPI_TXFEF_SIZE 1 |
| 126 | #define SPI_TXFFF_OFFSET 25 |
| 127 | #define SPI_TXFFF_SIZE 1 |
| 128 | #define SPI_TXFTHF_OFFSET 26 |
| 129 | #define SPI_TXFTHF_SIZE 1 |
| 130 | #define SPI_RXFEF_OFFSET 27 |
| 131 | #define SPI_RXFEF_SIZE 1 |
| 132 | #define SPI_RXFFF_OFFSET 28 |
| 133 | #define SPI_RXFFF_SIZE 1 |
| 134 | #define SPI_RXFTHF_OFFSET 29 |
| 135 | #define SPI_RXFTHF_SIZE 1 |
| 136 | #define SPI_TXFPTEF_OFFSET 30 |
| 137 | #define SPI_TXFPTEF_SIZE 1 |
| 138 | #define SPI_RXFPTEF_OFFSET 31 |
| 139 | #define SPI_RXFPTEF_SIZE 1 |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 140 | |
| 141 | /* Bitfields in CSR0 */ |
| 142 | #define SPI_CPOL_OFFSET 0 |
| 143 | #define SPI_CPOL_SIZE 1 |
| 144 | #define SPI_NCPHA_OFFSET 1 |
| 145 | #define SPI_NCPHA_SIZE 1 |
| 146 | #define SPI_CSAAT_OFFSET 3 |
| 147 | #define SPI_CSAAT_SIZE 1 |
| 148 | #define SPI_BITS_OFFSET 4 |
| 149 | #define SPI_BITS_SIZE 4 |
| 150 | #define SPI_SCBR_OFFSET 8 |
| 151 | #define SPI_SCBR_SIZE 8 |
| 152 | #define SPI_DLYBS_OFFSET 16 |
| 153 | #define SPI_DLYBS_SIZE 8 |
| 154 | #define SPI_DLYBCT_OFFSET 24 |
| 155 | #define SPI_DLYBCT_SIZE 8 |
| 156 | |
| 157 | /* Bitfields in RCR */ |
| 158 | #define SPI_RXCTR_OFFSET 0 |
| 159 | #define SPI_RXCTR_SIZE 16 |
| 160 | |
| 161 | /* Bitfields in TCR */ |
| 162 | #define SPI_TXCTR_OFFSET 0 |
| 163 | #define SPI_TXCTR_SIZE 16 |
| 164 | |
| 165 | /* Bitfields in RNCR */ |
| 166 | #define SPI_RXNCR_OFFSET 0 |
| 167 | #define SPI_RXNCR_SIZE 16 |
| 168 | |
| 169 | /* Bitfields in TNCR */ |
| 170 | #define SPI_TXNCR_OFFSET 0 |
| 171 | #define SPI_TXNCR_SIZE 16 |
| 172 | |
| 173 | /* Bitfields in PTCR */ |
| 174 | #define SPI_RXTEN_OFFSET 0 |
| 175 | #define SPI_RXTEN_SIZE 1 |
| 176 | #define SPI_RXTDIS_OFFSET 1 |
| 177 | #define SPI_RXTDIS_SIZE 1 |
| 178 | #define SPI_TXTEN_OFFSET 8 |
| 179 | #define SPI_TXTEN_SIZE 1 |
| 180 | #define SPI_TXTDIS_OFFSET 9 |
| 181 | #define SPI_TXTDIS_SIZE 1 |
| 182 | |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 183 | /* Bitfields in FMR */ |
| 184 | #define SPI_TXRDYM_OFFSET 0 |
| 185 | #define SPI_TXRDYM_SIZE 2 |
| 186 | #define SPI_RXRDYM_OFFSET 4 |
| 187 | #define SPI_RXRDYM_SIZE 2 |
| 188 | #define SPI_TXFTHRES_OFFSET 16 |
| 189 | #define SPI_TXFTHRES_SIZE 6 |
| 190 | #define SPI_RXFTHRES_OFFSET 24 |
| 191 | #define SPI_RXFTHRES_SIZE 6 |
| 192 | |
| 193 | /* Bitfields in FLR */ |
| 194 | #define SPI_TXFL_OFFSET 0 |
| 195 | #define SPI_TXFL_SIZE 6 |
| 196 | #define SPI_RXFL_OFFSET 16 |
| 197 | #define SPI_RXFL_SIZE 6 |
| 198 | |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 199 | /* Constants for BITS */ |
| 200 | #define SPI_BITS_8_BPT 0 |
| 201 | #define SPI_BITS_9_BPT 1 |
| 202 | #define SPI_BITS_10_BPT 2 |
| 203 | #define SPI_BITS_11_BPT 3 |
| 204 | #define SPI_BITS_12_BPT 4 |
| 205 | #define SPI_BITS_13_BPT 5 |
| 206 | #define SPI_BITS_14_BPT 6 |
| 207 | #define SPI_BITS_15_BPT 7 |
| 208 | #define SPI_BITS_16_BPT 8 |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 209 | #define SPI_ONE_DATA 0 |
| 210 | #define SPI_TWO_DATA 1 |
| 211 | #define SPI_FOUR_DATA 2 |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 212 | |
| 213 | /* Bit manipulation macros */ |
| 214 | #define SPI_BIT(name) \ |
| 215 | (1 << SPI_##name##_OFFSET) |
Sachin Kamat | a536d76 | 2013-09-10 17:06:27 +0530 | [diff] [blame] | 216 | #define SPI_BF(name, value) \ |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 217 | (((value) & ((1 << SPI_##name##_SIZE) - 1)) << SPI_##name##_OFFSET) |
Sachin Kamat | a536d76 | 2013-09-10 17:06:27 +0530 | [diff] [blame] | 218 | #define SPI_BFEXT(name, value) \ |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 219 | (((value) >> SPI_##name##_OFFSET) & ((1 << SPI_##name##_SIZE) - 1)) |
Sachin Kamat | a536d76 | 2013-09-10 17:06:27 +0530 | [diff] [blame] | 220 | #define SPI_BFINS(name, value, old) \ |
| 221 | (((old) & ~(((1 << SPI_##name##_SIZE) - 1) << SPI_##name##_OFFSET)) \ |
| 222 | | SPI_BF(name, value)) |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 223 | |
| 224 | /* Register access macros */ |
Ben Dooks | ea46732 | 2015-03-18 15:53:08 +0000 | [diff] [blame] | 225 | #define spi_readl(port, reg) \ |
| 226 | readl_relaxed((port)->regs + SPI_##reg) |
| 227 | #define spi_writel(port, reg, value) \ |
| 228 | writel_relaxed((value), (port)->regs + SPI_##reg) |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 229 | #define spi_writew(port, reg, value) \ |
| 230 | writew_relaxed((value), (port)->regs + SPI_##reg) |
| 231 | |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 232 | /* use PIO for small transfers, avoiding DMA setup/teardown overhead and |
| 233 | * cache operations; better heuristics consider wordsize and bitrate. |
| 234 | */ |
| 235 | #define DMA_MIN_BYTES 16 |
| 236 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 237 | #define SPI_DMA_TIMEOUT (msecs_to_jiffies(1000)) |
| 238 | |
Wenyou Yang | ce0c4ca | 2014-10-16 17:23:10 +0800 | [diff] [blame] | 239 | #define AUTOSUSPEND_TIMEOUT 2000 |
| 240 | |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 241 | struct atmel_spi_caps { |
| 242 | bool is_spi2; |
| 243 | bool has_wdrbt; |
| 244 | bool has_dma_support; |
Cyrille Pitchen | 7094576 | 2017-06-23 17:39:16 +0200 | [diff] [blame] | 245 | bool has_pdc_support; |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 246 | }; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 247 | |
| 248 | /* |
| 249 | * The core SPI transfer engine just talks to a register bank to set up |
| 250 | * DMA transfers; transfer queue progress is driven by IRQs. The clock |
| 251 | * framework provides the base clock, subdivided for each spi_device. |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 252 | */ |
| 253 | struct atmel_spi { |
| 254 | spinlock_t lock; |
Nicolas Ferre | 8aad792 | 2013-04-03 13:58:36 +0800 | [diff] [blame] | 255 | unsigned long flags; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 256 | |
Nicolas Ferre | dfab30e | 2013-04-03 13:57:42 +0800 | [diff] [blame] | 257 | phys_addr_t phybase; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 258 | void __iomem *regs; |
| 259 | int irq; |
| 260 | struct clk *clk; |
| 261 | struct platform_device *pdev; |
Ben Whitten | 39fe33f | 2016-11-14 15:13:20 +0000 | [diff] [blame] | 262 | unsigned long spi_clk; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 263 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 264 | struct spi_transfer *current_transfer; |
Axel Lin | 0c3b974 | 2014-03-27 09:26:38 +0800 | [diff] [blame] | 265 | int current_remaining_bytes; |
Nicolas Ferre | 823cd04 | 2013-03-19 15:45:01 +0800 | [diff] [blame] | 266 | int done_status; |
Radu Pirea | a9889ed | 2017-12-19 17:17:59 +0200 | [diff] [blame] | 267 | dma_addr_t dma_addr_rx_bbuf; |
| 268 | dma_addr_t dma_addr_tx_bbuf; |
| 269 | void *addr_rx_bbuf; |
| 270 | void *addr_tx_bbuf; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 271 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 272 | struct completion xfer_completion; |
| 273 | |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 274 | struct atmel_spi_caps caps; |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 275 | |
| 276 | bool use_dma; |
| 277 | bool use_pdc; |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 278 | |
| 279 | bool keep_cs; |
| 280 | bool cs_active; |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 281 | |
| 282 | u32 fifo_size; |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 283 | u8 native_cs_free; |
| 284 | u8 native_cs_for_gpio; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 285 | }; |
| 286 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 287 | /* Controller-specific per-slave state */ |
| 288 | struct atmel_spi_device { |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 289 | u32 csr; |
| 290 | }; |
| 291 | |
Nicolas Ferre | 7910d9a | 2016-11-24 12:24:58 +0100 | [diff] [blame] | 292 | #define SPI_MAX_DMA_XFER 65535 /* true for both PDC and DMA */ |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 293 | #define INVALID_DMA_ADDRESS 0xffffffff |
| 294 | |
| 295 | /* |
Haavard Skinnemoen | 5bfa26c | 2009-01-06 14:41:42 -0800 | [diff] [blame] | 296 | * Version 2 of the SPI controller has |
| 297 | * - CR.LASTXFER |
| 298 | * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero) |
| 299 | * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs) |
| 300 | * - SPI_CSRx.CSAAT |
| 301 | * - SPI_CSRx.SBCR allows faster clocking |
Haavard Skinnemoen | 5bfa26c | 2009-01-06 14:41:42 -0800 | [diff] [blame] | 302 | */ |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 303 | static bool atmel_spi_is_v2(struct atmel_spi *as) |
Haavard Skinnemoen | 5bfa26c | 2009-01-06 14:41:42 -0800 | [diff] [blame] | 304 | { |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 305 | return as->caps.is_spi2; |
Haavard Skinnemoen | 5bfa26c | 2009-01-06 14:41:42 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /* |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 309 | * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby |
| 310 | * they assume that spi slave device state will not change on deselect, so |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 311 | * that automagic deselection is OK. ("NPCSx rises if no data is to be |
| 312 | * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer |
| 313 | * controllers have CSAAT and friends. |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 314 | * |
Gregory CLEMENT | 4d8672d | 2019-10-17 16:18:40 +0200 | [diff] [blame] | 315 | * Even controller newer than ar91rm9200, using GPIOs can make sens as |
| 316 | * it lets us support active-high chipselects despite the controller's |
| 317 | * belief that only active-low devices/systems exists. |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 318 | * |
| 319 | * However, at91rm9200 has a second erratum whereby nCS0 doesn't work |
| 320 | * right when driven with GPIO. ("Mode Fault does not allow more than one |
| 321 | * Master on Chip Select 0.") No workaround exists for that ... so for |
| 322 | * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH, |
| 323 | * and (c) will trigger that first erratum in some cases. |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 324 | */ |
| 325 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 326 | static void cs_activate(struct atmel_spi *as, struct spi_device *spi) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 327 | { |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 328 | struct atmel_spi_device *asd = spi->controller_state; |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 329 | int chip_select; |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 330 | u32 mr; |
Atsushi Nemoto | f6febcc | 2008-02-23 15:23:39 -0800 | [diff] [blame] | 331 | |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 332 | if (spi->cs_gpiod) |
| 333 | chip_select = as->native_cs_for_gpio; |
| 334 | else |
| 335 | chip_select = spi->chip_select; |
| 336 | |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 337 | if (atmel_spi_is_v2(as)) { |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 338 | spi_writel(as, CSR0 + 4 * chip_select, asd->csr); |
Wenyou Yang | 97ed465 | 2013-03-19 15:43:01 +0800 | [diff] [blame] | 339 | /* For the low SPI version, there is a issue that PDC transfer |
| 340 | * on CS1,2,3 needs SPI_CSR0.BITS config as SPI_CSR1,2,3.BITS |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 341 | */ |
| 342 | spi_writel(as, CSR0, asd->csr); |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 343 | if (as->caps.has_wdrbt) { |
Wenyou Yang | 97ed465 | 2013-03-19 15:43:01 +0800 | [diff] [blame] | 344 | spi_writel(as, MR, |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 345 | SPI_BF(PCS, ~(0x01 << chip_select)) |
Wenyou Yang | 97ed465 | 2013-03-19 15:43:01 +0800 | [diff] [blame] | 346 | | SPI_BIT(WDRBT) |
| 347 | | SPI_BIT(MODFDIS) |
| 348 | | SPI_BIT(MSTR)); |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 349 | } else { |
Wenyou Yang | 97ed465 | 2013-03-19 15:43:01 +0800 | [diff] [blame] | 350 | spi_writel(as, MR, |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 351 | SPI_BF(PCS, ~(0x01 << chip_select)) |
Wenyou Yang | 97ed465 | 2013-03-19 15:43:01 +0800 | [diff] [blame] | 352 | | SPI_BIT(MODFDIS) |
| 353 | | SPI_BIT(MSTR)); |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 354 | } |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 355 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 356 | mr = spi_readl(as, MR); |
Gregory CLEMENT | 60086e2 | 2019-10-17 16:18:43 +0200 | [diff] [blame] | 357 | if (spi->cs_gpiod) |
| 358 | gpiod_set_value(spi->cs_gpiod, 1); |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 359 | } else { |
| 360 | u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0; |
| 361 | int i; |
| 362 | u32 csr; |
| 363 | |
| 364 | /* Make sure clock polarity is correct */ |
| 365 | for (i = 0; i < spi->master->num_chipselect; i++) { |
| 366 | csr = spi_readl(as, CSR0 + 4 * i); |
| 367 | if ((csr ^ cpol) & SPI_BIT(CPOL)) |
| 368 | spi_writel(as, CSR0 + 4 * i, |
| 369 | csr ^ SPI_BIT(CPOL)); |
| 370 | } |
| 371 | |
| 372 | mr = spi_readl(as, MR); |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 373 | mr = SPI_BFINS(PCS, ~(1 << chip_select), mr); |
Gregory CLEMENT | 9c86f12 | 2019-10-17 16:18:46 +0200 | [diff] [blame] | 374 | if (spi->cs_gpiod) |
Gregory CLEMENT | 60086e2 | 2019-10-17 16:18:43 +0200 | [diff] [blame] | 375 | gpiod_set_value(spi->cs_gpiod, 1); |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 376 | spi_writel(as, MR, mr); |
Atsushi Nemoto | f6febcc | 2008-02-23 15:23:39 -0800 | [diff] [blame] | 377 | } |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 378 | |
Linus Walleij | efc92fb | 2019-01-07 16:51:52 +0100 | [diff] [blame] | 379 | dev_dbg(&spi->dev, "activate NPCS, mr %08x\n", mr); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 380 | } |
| 381 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 382 | static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 383 | { |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 384 | int chip_select; |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 385 | u32 mr; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 386 | |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 387 | if (spi->cs_gpiod) |
| 388 | chip_select = as->native_cs_for_gpio; |
| 389 | else |
| 390 | chip_select = spi->chip_select; |
| 391 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 392 | /* only deactivate *this* device; sometimes transfers to |
| 393 | * another device may be active when this routine is called. |
| 394 | */ |
| 395 | mr = spi_readl(as, MR); |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 396 | if (~SPI_BFEXT(PCS, mr) & (1 << chip_select)) { |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 397 | mr = SPI_BFINS(PCS, 0xf, mr); |
| 398 | spi_writel(as, MR, mr); |
| 399 | } |
| 400 | |
Linus Walleij | efc92fb | 2019-01-07 16:51:52 +0100 | [diff] [blame] | 401 | dev_dbg(&spi->dev, "DEactivate NPCS, mr %08x\n", mr); |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 402 | |
Gregory CLEMENT | 60086e2 | 2019-10-17 16:18:43 +0200 | [diff] [blame] | 403 | if (!spi->cs_gpiod) |
Cyrille Pitchen | 4820303 | 2015-06-09 13:53:52 +0200 | [diff] [blame] | 404 | spi_writel(as, CR, SPI_BIT(LASTXFER)); |
Gregory CLEMENT | 9c86f12 | 2019-10-17 16:18:46 +0200 | [diff] [blame] | 405 | else |
Gregory CLEMENT | 60086e2 | 2019-10-17 16:18:43 +0200 | [diff] [blame] | 406 | gpiod_set_value(spi->cs_gpiod, 0); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 407 | } |
| 408 | |
Mark Brown | 6c07ef2 | 2013-07-28 14:32:27 +0100 | [diff] [blame] | 409 | static void atmel_spi_lock(struct atmel_spi *as) __acquires(&as->lock) |
Nicolas Ferre | 8aad792 | 2013-04-03 13:58:36 +0800 | [diff] [blame] | 410 | { |
| 411 | spin_lock_irqsave(&as->lock, as->flags); |
| 412 | } |
| 413 | |
Mark Brown | 6c07ef2 | 2013-07-28 14:32:27 +0100 | [diff] [blame] | 414 | static void atmel_spi_unlock(struct atmel_spi *as) __releases(&as->lock) |
Nicolas Ferre | 8aad792 | 2013-04-03 13:58:36 +0800 | [diff] [blame] | 415 | { |
| 416 | spin_unlock_irqrestore(&as->lock, as->flags); |
| 417 | } |
| 418 | |
Radu Pirea | a9889ed | 2017-12-19 17:17:59 +0200 | [diff] [blame] | 419 | static inline bool atmel_spi_is_vmalloc_xfer(struct spi_transfer *xfer) |
| 420 | { |
| 421 | return is_vmalloc_addr(xfer->tx_buf) || is_vmalloc_addr(xfer->rx_buf); |
| 422 | } |
| 423 | |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 424 | static inline bool atmel_spi_use_dma(struct atmel_spi *as, |
| 425 | struct spi_transfer *xfer) |
| 426 | { |
| 427 | return as->use_dma && xfer->len >= DMA_MIN_BYTES; |
| 428 | } |
| 429 | |
Cyrille Pitchen | 04242ca | 2016-11-24 12:24:59 +0100 | [diff] [blame] | 430 | static bool atmel_spi_can_dma(struct spi_master *master, |
| 431 | struct spi_device *spi, |
| 432 | struct spi_transfer *xfer) |
| 433 | { |
| 434 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 435 | |
Radu Pirea | a9889ed | 2017-12-19 17:17:59 +0200 | [diff] [blame] | 436 | if (IS_ENABLED(CONFIG_SOC_SAM_V4_V5)) |
| 437 | return atmel_spi_use_dma(as, xfer) && |
| 438 | !atmel_spi_is_vmalloc_xfer(xfer); |
| 439 | else |
| 440 | return atmel_spi_use_dma(as, xfer); |
| 441 | |
Cyrille Pitchen | 04242ca | 2016-11-24 12:24:59 +0100 | [diff] [blame] | 442 | } |
| 443 | |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 444 | static int atmel_spi_dma_slave_config(struct atmel_spi *as, |
| 445 | struct dma_slave_config *slave_config, |
| 446 | u8 bits_per_word) |
| 447 | { |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 448 | struct spi_master *master = platform_get_drvdata(as->pdev); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 449 | int err = 0; |
| 450 | |
| 451 | if (bits_per_word > 8) { |
| 452 | slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 453 | slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_2_BYTES; |
| 454 | } else { |
| 455 | slave_config->dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 456 | slave_config->src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 457 | } |
| 458 | |
| 459 | slave_config->dst_addr = (dma_addr_t)as->phybase + SPI_TDR; |
| 460 | slave_config->src_addr = (dma_addr_t)as->phybase + SPI_RDR; |
| 461 | slave_config->src_maxburst = 1; |
| 462 | slave_config->dst_maxburst = 1; |
| 463 | slave_config->device_fc = false; |
| 464 | |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 465 | /* |
| 466 | * This driver uses fixed peripheral select mode (PS bit set to '0' in |
| 467 | * the Mode Register). |
| 468 | * So according to the datasheet, when FIFOs are available (and |
| 469 | * enabled), the Transmit FIFO operates in Multiple Data Mode. |
| 470 | * In this mode, up to 2 data, not 4, can be written into the Transmit |
| 471 | * Data Register in a single access. |
| 472 | * However, the first data has to be written into the lowest 16 bits and |
| 473 | * the second data into the highest 16 bits of the Transmit |
| 474 | * Data Register. For 8bit data (the most frequent case), it would |
| 475 | * require to rework tx_buf so each data would actualy fit 16 bits. |
| 476 | * So we'd rather write only one data at the time. Hence the transmit |
| 477 | * path works the same whether FIFOs are available (and enabled) or not. |
| 478 | */ |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 479 | slave_config->direction = DMA_MEM_TO_DEV; |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 480 | if (dmaengine_slave_config(master->dma_tx, slave_config)) { |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 481 | dev_err(&as->pdev->dev, |
| 482 | "failed to configure tx dma channel\n"); |
| 483 | err = -EINVAL; |
| 484 | } |
| 485 | |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 486 | /* |
| 487 | * This driver configures the spi controller for master mode (MSTR bit |
| 488 | * set to '1' in the Mode Register). |
| 489 | * So according to the datasheet, when FIFOs are available (and |
| 490 | * enabled), the Receive FIFO operates in Single Data Mode. |
| 491 | * So the receive path works the same whether FIFOs are available (and |
| 492 | * enabled) or not. |
| 493 | */ |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 494 | slave_config->direction = DMA_DEV_TO_MEM; |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 495 | if (dmaengine_slave_config(master->dma_rx, slave_config)) { |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 496 | dev_err(&as->pdev->dev, |
| 497 | "failed to configure rx dma channel\n"); |
| 498 | err = -EINVAL; |
| 499 | } |
| 500 | |
| 501 | return err; |
| 502 | } |
| 503 | |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 504 | static int atmel_spi_configure_dma(struct spi_master *master, |
| 505 | struct atmel_spi *as) |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 506 | { |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 507 | struct dma_slave_config slave_config; |
Richard Genoud | 2f767a9 | 2013-05-31 17:01:59 +0200 | [diff] [blame] | 508 | struct device *dev = &as->pdev->dev; |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 509 | int err; |
| 510 | |
Richard Genoud | 2f767a9 | 2013-05-31 17:01:59 +0200 | [diff] [blame] | 511 | dma_cap_mask_t mask; |
| 512 | dma_cap_zero(mask); |
| 513 | dma_cap_set(DMA_SLAVE, mask); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 514 | |
Peter Ujfalusi | bef1e0c | 2019-11-13 11:42:49 +0200 | [diff] [blame^] | 515 | master->dma_tx = dma_request_chan(dev, "tx"); |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 516 | if (IS_ERR(master->dma_tx)) { |
| 517 | err = PTR_ERR(master->dma_tx); |
Ludovic Desroches | 5e9af37 | 2014-11-14 17:12:54 +0100 | [diff] [blame] | 518 | if (err == -EPROBE_DEFER) { |
| 519 | dev_warn(dev, "no DMA channel available at the moment\n"); |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 520 | goto error_clear; |
Ludovic Desroches | 5e9af37 | 2014-11-14 17:12:54 +0100 | [diff] [blame] | 521 | } |
Richard Genoud | 2f767a9 | 2013-05-31 17:01:59 +0200 | [diff] [blame] | 522 | dev_err(dev, |
| 523 | "DMA TX channel not available, SPI unable to use DMA\n"); |
| 524 | err = -EBUSY; |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 525 | goto error_clear; |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 526 | } |
Richard Genoud | 2f767a9 | 2013-05-31 17:01:59 +0200 | [diff] [blame] | 527 | |
Ludovic Desroches | 5e9af37 | 2014-11-14 17:12:54 +0100 | [diff] [blame] | 528 | /* |
| 529 | * No reason to check EPROBE_DEFER here since we have already requested |
| 530 | * tx channel. If it fails here, it's for another reason. |
| 531 | */ |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 532 | master->dma_rx = dma_request_slave_channel(dev, "rx"); |
Richard Genoud | 2f767a9 | 2013-05-31 17:01:59 +0200 | [diff] [blame] | 533 | |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 534 | if (!master->dma_rx) { |
Richard Genoud | 2f767a9 | 2013-05-31 17:01:59 +0200 | [diff] [blame] | 535 | dev_err(dev, |
| 536 | "DMA RX channel not available, SPI unable to use DMA\n"); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 537 | err = -EBUSY; |
| 538 | goto error; |
| 539 | } |
| 540 | |
| 541 | err = atmel_spi_dma_slave_config(as, &slave_config, 8); |
| 542 | if (err) |
| 543 | goto error; |
| 544 | |
| 545 | dev_info(&as->pdev->dev, |
| 546 | "Using %s (tx) and %s (rx) for DMA transfers\n", |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 547 | dma_chan_name(master->dma_tx), |
| 548 | dma_chan_name(master->dma_rx)); |
| 549 | |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 550 | return 0; |
| 551 | error: |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 552 | if (master->dma_rx) |
| 553 | dma_release_channel(master->dma_rx); |
| 554 | if (!IS_ERR(master->dma_tx)) |
| 555 | dma_release_channel(master->dma_tx); |
| 556 | error_clear: |
| 557 | master->dma_tx = master->dma_rx = NULL; |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 558 | return err; |
| 559 | } |
| 560 | |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 561 | static void atmel_spi_stop_dma(struct spi_master *master) |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 562 | { |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 563 | if (master->dma_rx) |
| 564 | dmaengine_terminate_all(master->dma_rx); |
| 565 | if (master->dma_tx) |
| 566 | dmaengine_terminate_all(master->dma_tx); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 567 | } |
| 568 | |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 569 | static void atmel_spi_release_dma(struct spi_master *master) |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 570 | { |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 571 | if (master->dma_rx) { |
| 572 | dma_release_channel(master->dma_rx); |
| 573 | master->dma_rx = NULL; |
| 574 | } |
| 575 | if (master->dma_tx) { |
| 576 | dma_release_channel(master->dma_tx); |
| 577 | master->dma_tx = NULL; |
| 578 | } |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | /* This function is called by the DMA driver from tasklet context */ |
| 582 | static void dma_callback(void *data) |
| 583 | { |
| 584 | struct spi_master *master = data; |
| 585 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 586 | |
Radu Pirea | a9889ed | 2017-12-19 17:17:59 +0200 | [diff] [blame] | 587 | if (is_vmalloc_addr(as->current_transfer->rx_buf) && |
| 588 | IS_ENABLED(CONFIG_SOC_SAM_V4_V5)) { |
| 589 | memcpy(as->current_transfer->rx_buf, as->addr_rx_bbuf, |
| 590 | as->current_transfer->len); |
| 591 | } |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 592 | complete(&as->xfer_completion); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | /* |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 596 | * Next transfer using PIO without FIFO. |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 597 | */ |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 598 | static void atmel_spi_next_xfer_single(struct spi_master *master, |
| 599 | struct spi_transfer *xfer) |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 600 | { |
| 601 | struct atmel_spi *as = spi_master_get_devdata(master); |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 602 | unsigned long xfer_pos = xfer->len - as->current_remaining_bytes; |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 603 | |
| 604 | dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_pio\n"); |
| 605 | |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 606 | /* Make sure data is not remaining in RDR */ |
| 607 | spi_readl(as, RDR); |
| 608 | while (spi_readl(as, SR) & SPI_BIT(RDRF)) { |
| 609 | spi_readl(as, RDR); |
| 610 | cpu_relax(); |
| 611 | } |
| 612 | |
Nicolas Ferre | 7910d9a | 2016-11-24 12:24:58 +0100 | [diff] [blame] | 613 | if (xfer->bits_per_word > 8) |
| 614 | spi_writel(as, TDR, *(u16 *)(xfer->tx_buf + xfer_pos)); |
| 615 | else |
| 616 | spi_writel(as, TDR, *(u8 *)(xfer->tx_buf + xfer_pos)); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 617 | |
| 618 | dev_dbg(master->dev.parent, |
Richard Genoud | f557c98 | 2013-05-02 19:25:11 +0800 | [diff] [blame] | 619 | " start pio xfer %p: len %u tx %p rx %p bitpw %d\n", |
| 620 | xfer, xfer->len, xfer->tx_buf, xfer->rx_buf, |
| 621 | xfer->bits_per_word); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 622 | |
| 623 | /* Enable relevant interrupts */ |
| 624 | spi_writel(as, IER, SPI_BIT(RDRF) | SPI_BIT(OVRES)); |
| 625 | } |
| 626 | |
| 627 | /* |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 628 | * Next transfer using PIO with FIFO. |
| 629 | */ |
| 630 | static void atmel_spi_next_xfer_fifo(struct spi_master *master, |
| 631 | struct spi_transfer *xfer) |
| 632 | { |
| 633 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 634 | u32 current_remaining_data, num_data; |
| 635 | u32 offset = xfer->len - as->current_remaining_bytes; |
| 636 | const u16 *words = (const u16 *)((u8 *)xfer->tx_buf + offset); |
| 637 | const u8 *bytes = (const u8 *)((u8 *)xfer->tx_buf + offset); |
| 638 | u16 td0, td1; |
| 639 | u32 fifomr; |
| 640 | |
| 641 | dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_fifo\n"); |
| 642 | |
| 643 | /* Compute the number of data to transfer in the current iteration */ |
| 644 | current_remaining_data = ((xfer->bits_per_word > 8) ? |
| 645 | ((u32)as->current_remaining_bytes >> 1) : |
| 646 | (u32)as->current_remaining_bytes); |
| 647 | num_data = min(current_remaining_data, as->fifo_size); |
| 648 | |
| 649 | /* Flush RX and TX FIFOs */ |
| 650 | spi_writel(as, CR, SPI_BIT(RXFCLR) | SPI_BIT(TXFCLR)); |
| 651 | while (spi_readl(as, FLR)) |
| 652 | cpu_relax(); |
| 653 | |
| 654 | /* Set RX FIFO Threshold to the number of data to transfer */ |
| 655 | fifomr = spi_readl(as, FMR); |
| 656 | spi_writel(as, FMR, SPI_BFINS(RXFTHRES, num_data, fifomr)); |
| 657 | |
| 658 | /* Clear FIFO flags in the Status Register, especially RXFTHF */ |
| 659 | (void)spi_readl(as, SR); |
| 660 | |
| 661 | /* Fill TX FIFO */ |
| 662 | while (num_data >= 2) { |
Nicolas Ferre | 7910d9a | 2016-11-24 12:24:58 +0100 | [diff] [blame] | 663 | if (xfer->bits_per_word > 8) { |
| 664 | td0 = *words++; |
| 665 | td1 = *words++; |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 666 | } else { |
Nicolas Ferre | 7910d9a | 2016-11-24 12:24:58 +0100 | [diff] [blame] | 667 | td0 = *bytes++; |
| 668 | td1 = *bytes++; |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | spi_writel(as, TDR, (td1 << 16) | td0); |
| 672 | num_data -= 2; |
| 673 | } |
| 674 | |
| 675 | if (num_data) { |
Nicolas Ferre | 7910d9a | 2016-11-24 12:24:58 +0100 | [diff] [blame] | 676 | if (xfer->bits_per_word > 8) |
| 677 | td0 = *words++; |
| 678 | else |
| 679 | td0 = *bytes++; |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 680 | |
| 681 | spi_writew(as, TDR, td0); |
| 682 | num_data--; |
| 683 | } |
| 684 | |
| 685 | dev_dbg(master->dev.parent, |
| 686 | " start fifo xfer %p: len %u tx %p rx %p bitpw %d\n", |
| 687 | xfer, xfer->len, xfer->tx_buf, xfer->rx_buf, |
| 688 | xfer->bits_per_word); |
| 689 | |
| 690 | /* |
| 691 | * Enable RX FIFO Threshold Flag interrupt to be notified about |
| 692 | * transfer completion. |
| 693 | */ |
| 694 | spi_writel(as, IER, SPI_BIT(RXFTHF) | SPI_BIT(OVRES)); |
| 695 | } |
| 696 | |
| 697 | /* |
| 698 | * Next transfer using PIO. |
| 699 | */ |
| 700 | static void atmel_spi_next_xfer_pio(struct spi_master *master, |
| 701 | struct spi_transfer *xfer) |
| 702 | { |
| 703 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 704 | |
| 705 | if (as->fifo_size) |
| 706 | atmel_spi_next_xfer_fifo(master, xfer); |
| 707 | else |
| 708 | atmel_spi_next_xfer_single(master, xfer); |
| 709 | } |
| 710 | |
| 711 | /* |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 712 | * Submit next transfer for DMA. |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 713 | */ |
| 714 | static int atmel_spi_next_xfer_dma_submit(struct spi_master *master, |
| 715 | struct spi_transfer *xfer, |
| 716 | u32 *plen) |
| 717 | { |
| 718 | struct atmel_spi *as = spi_master_get_devdata(master); |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 719 | struct dma_chan *rxchan = master->dma_rx; |
| 720 | struct dma_chan *txchan = master->dma_tx; |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 721 | struct dma_async_tx_descriptor *rxdesc; |
| 722 | struct dma_async_tx_descriptor *txdesc; |
| 723 | struct dma_slave_config slave_config; |
| 724 | dma_cookie_t cookie; |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 725 | |
| 726 | dev_vdbg(master->dev.parent, "atmel_spi_next_xfer_dma_submit\n"); |
| 727 | |
| 728 | /* Check that the channels are available */ |
| 729 | if (!rxchan || !txchan) |
| 730 | return -ENODEV; |
| 731 | |
| 732 | /* release lock for DMA operations */ |
| 733 | atmel_spi_unlock(as); |
| 734 | |
Cyrille Pitchen | 04242ca | 2016-11-24 12:24:59 +0100 | [diff] [blame] | 735 | *plen = xfer->len; |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 736 | |
David Mosberger-Tang | 06515f8 | 2015-10-20 14:26:47 +0200 | [diff] [blame] | 737 | if (atmel_spi_dma_slave_config(as, &slave_config, |
| 738 | xfer->bits_per_word)) |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 739 | goto err_exit; |
| 740 | |
| 741 | /* Send both scatterlists */ |
Radu Pirea | a9889ed | 2017-12-19 17:17:59 +0200 | [diff] [blame] | 742 | if (atmel_spi_is_vmalloc_xfer(xfer) && |
| 743 | IS_ENABLED(CONFIG_SOC_SAM_V4_V5)) { |
| 744 | rxdesc = dmaengine_prep_slave_single(rxchan, |
| 745 | as->dma_addr_rx_bbuf, |
| 746 | xfer->len, |
Stefan Agner | 3573257 | 2018-03-24 11:48:00 +0100 | [diff] [blame] | 747 | DMA_DEV_TO_MEM, |
Radu Pirea | a9889ed | 2017-12-19 17:17:59 +0200 | [diff] [blame] | 748 | DMA_PREP_INTERRUPT | |
| 749 | DMA_CTRL_ACK); |
| 750 | } else { |
| 751 | rxdesc = dmaengine_prep_slave_sg(rxchan, |
| 752 | xfer->rx_sg.sgl, |
| 753 | xfer->rx_sg.nents, |
Stefan Agner | 3573257 | 2018-03-24 11:48:00 +0100 | [diff] [blame] | 754 | DMA_DEV_TO_MEM, |
Radu Pirea | a9889ed | 2017-12-19 17:17:59 +0200 | [diff] [blame] | 755 | DMA_PREP_INTERRUPT | |
| 756 | DMA_CTRL_ACK); |
| 757 | } |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 758 | if (!rxdesc) |
| 759 | goto err_dma; |
| 760 | |
Radu Pirea | a9889ed | 2017-12-19 17:17:59 +0200 | [diff] [blame] | 761 | if (atmel_spi_is_vmalloc_xfer(xfer) && |
| 762 | IS_ENABLED(CONFIG_SOC_SAM_V4_V5)) { |
| 763 | memcpy(as->addr_tx_bbuf, xfer->tx_buf, xfer->len); |
| 764 | txdesc = dmaengine_prep_slave_single(txchan, |
| 765 | as->dma_addr_tx_bbuf, |
Stefan Agner | 3573257 | 2018-03-24 11:48:00 +0100 | [diff] [blame] | 766 | xfer->len, DMA_MEM_TO_DEV, |
Radu Pirea | a9889ed | 2017-12-19 17:17:59 +0200 | [diff] [blame] | 767 | DMA_PREP_INTERRUPT | |
| 768 | DMA_CTRL_ACK); |
| 769 | } else { |
| 770 | txdesc = dmaengine_prep_slave_sg(txchan, |
| 771 | xfer->tx_sg.sgl, |
| 772 | xfer->tx_sg.nents, |
Stefan Agner | 3573257 | 2018-03-24 11:48:00 +0100 | [diff] [blame] | 773 | DMA_MEM_TO_DEV, |
Radu Pirea | a9889ed | 2017-12-19 17:17:59 +0200 | [diff] [blame] | 774 | DMA_PREP_INTERRUPT | |
| 775 | DMA_CTRL_ACK); |
| 776 | } |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 777 | if (!txdesc) |
| 778 | goto err_dma; |
| 779 | |
| 780 | dev_dbg(master->dev.parent, |
Emil Goode | 2de024b | 2013-07-30 19:35:35 +0200 | [diff] [blame] | 781 | " start dma xfer %p: len %u tx %p/%08llx rx %p/%08llx\n", |
| 782 | xfer, xfer->len, xfer->tx_buf, (unsigned long long)xfer->tx_dma, |
| 783 | xfer->rx_buf, (unsigned long long)xfer->rx_dma); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 784 | |
| 785 | /* Enable relevant interrupts */ |
| 786 | spi_writel(as, IER, SPI_BIT(OVRES)); |
| 787 | |
| 788 | /* Put the callback on the RX transfer only, that should finish last */ |
| 789 | rxdesc->callback = dma_callback; |
| 790 | rxdesc->callback_param = master; |
| 791 | |
| 792 | /* Submit and fire RX and TX with TX last so we're ready to read! */ |
| 793 | cookie = rxdesc->tx_submit(rxdesc); |
| 794 | if (dma_submit_error(cookie)) |
| 795 | goto err_dma; |
| 796 | cookie = txdesc->tx_submit(txdesc); |
| 797 | if (dma_submit_error(cookie)) |
| 798 | goto err_dma; |
| 799 | rxchan->device->device_issue_pending(rxchan); |
| 800 | txchan->device->device_issue_pending(txchan); |
| 801 | |
| 802 | /* take back lock */ |
| 803 | atmel_spi_lock(as); |
| 804 | return 0; |
| 805 | |
| 806 | err_dma: |
| 807 | spi_writel(as, IDR, SPI_BIT(OVRES)); |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 808 | atmel_spi_stop_dma(master); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 809 | err_exit: |
| 810 | atmel_spi_lock(as); |
| 811 | return -ENOMEM; |
| 812 | } |
| 813 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 814 | static void atmel_spi_next_xfer_data(struct spi_master *master, |
| 815 | struct spi_transfer *xfer, |
| 816 | dma_addr_t *tx_dma, |
| 817 | dma_addr_t *rx_dma, |
| 818 | u32 *plen) |
| 819 | { |
Nicolas Ferre | 7910d9a | 2016-11-24 12:24:58 +0100 | [diff] [blame] | 820 | *rx_dma = xfer->rx_dma + xfer->len - *plen; |
| 821 | *tx_dma = xfer->tx_dma + xfer->len - *plen; |
Cyrille Pitchen | 04242ca | 2016-11-24 12:24:59 +0100 | [diff] [blame] | 822 | if (*plen > master->max_dma_len) |
| 823 | *plen = master->max_dma_len; |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 824 | } |
| 825 | |
Richard Genoud | d3b72c7 | 2013-11-07 10:34:06 +0100 | [diff] [blame] | 826 | static int atmel_spi_set_xfer_speed(struct atmel_spi *as, |
| 827 | struct spi_device *spi, |
| 828 | struct spi_transfer *xfer) |
| 829 | { |
| 830 | u32 scbr, csr; |
| 831 | unsigned long bus_hz; |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 832 | int chip_select; |
| 833 | |
| 834 | if (spi->cs_gpiod) |
| 835 | chip_select = as->native_cs_for_gpio; |
| 836 | else |
| 837 | chip_select = spi->chip_select; |
Richard Genoud | d3b72c7 | 2013-11-07 10:34:06 +0100 | [diff] [blame] | 838 | |
| 839 | /* v1 chips start out at half the peripheral bus speed. */ |
Ben Whitten | 39fe33f | 2016-11-14 15:13:20 +0000 | [diff] [blame] | 840 | bus_hz = as->spi_clk; |
Richard Genoud | d3b72c7 | 2013-11-07 10:34:06 +0100 | [diff] [blame] | 841 | if (!atmel_spi_is_v2(as)) |
| 842 | bus_hz /= 2; |
| 843 | |
| 844 | /* |
| 845 | * Calculate the lowest divider that satisfies the |
| 846 | * constraint, assuming div32/fdiv/mbz == 0. |
| 847 | */ |
Jarkko Nikula | e864658 | 2015-09-25 09:03:01 +0300 | [diff] [blame] | 848 | scbr = DIV_ROUND_UP(bus_hz, xfer->speed_hz); |
Richard Genoud | d3b72c7 | 2013-11-07 10:34:06 +0100 | [diff] [blame] | 849 | |
| 850 | /* |
| 851 | * If the resulting divider doesn't fit into the |
| 852 | * register bitfield, we can't satisfy the constraint. |
| 853 | */ |
| 854 | if (scbr >= (1 << SPI_SCBR_SIZE)) { |
| 855 | dev_err(&spi->dev, |
| 856 | "setup: %d Hz too slow, scbr %u; min %ld Hz\n", |
| 857 | xfer->speed_hz, scbr, bus_hz/255); |
| 858 | return -EINVAL; |
| 859 | } |
| 860 | if (scbr == 0) { |
| 861 | dev_err(&spi->dev, |
| 862 | "setup: %d Hz too high, scbr %u; max %ld Hz\n", |
| 863 | xfer->speed_hz, scbr, bus_hz); |
| 864 | return -EINVAL; |
| 865 | } |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 866 | csr = spi_readl(as, CSR0 + 4 * chip_select); |
Richard Genoud | d3b72c7 | 2013-11-07 10:34:06 +0100 | [diff] [blame] | 867 | csr = SPI_BFINS(SCBR, scbr, csr); |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 868 | spi_writel(as, CSR0 + 4 * chip_select, csr); |
Richard Genoud | d3b72c7 | 2013-11-07 10:34:06 +0100 | [diff] [blame] | 869 | |
| 870 | return 0; |
| 871 | } |
| 872 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 873 | /* |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 874 | * Submit next transfer for PDC. |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 875 | * lock is held, spi irq is blocked |
| 876 | */ |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 877 | static void atmel_spi_pdc_next_xfer(struct spi_master *master, |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 878 | struct spi_message *msg, |
| 879 | struct spi_transfer *xfer) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 880 | { |
| 881 | struct atmel_spi *as = spi_master_get_devdata(master); |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 882 | u32 len; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 883 | dma_addr_t tx_dma, rx_dma; |
| 884 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 885 | spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS)); |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 886 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 887 | len = as->current_remaining_bytes; |
| 888 | atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len); |
| 889 | as->current_remaining_bytes -= len; |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 890 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 891 | spi_writel(as, RPR, rx_dma); |
| 892 | spi_writel(as, TPR, tx_dma); |
| 893 | |
| 894 | if (msg->spi->bits_per_word > 8) |
| 895 | len >>= 1; |
| 896 | spi_writel(as, RCR, len); |
| 897 | spi_writel(as, TCR, len); |
| 898 | |
| 899 | dev_dbg(&msg->spi->dev, |
| 900 | " start xfer %p: len %u tx %p/%08llx rx %p/%08llx\n", |
| 901 | xfer, xfer->len, xfer->tx_buf, |
| 902 | (unsigned long long)xfer->tx_dma, xfer->rx_buf, |
| 903 | (unsigned long long)xfer->rx_dma); |
| 904 | |
| 905 | if (as->current_remaining_bytes) { |
| 906 | len = as->current_remaining_bytes; |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 907 | atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len); |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 908 | as->current_remaining_bytes -= len; |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 909 | |
| 910 | spi_writel(as, RNPR, rx_dma); |
| 911 | spi_writel(as, TNPR, tx_dma); |
| 912 | |
| 913 | if (msg->spi->bits_per_word > 8) |
| 914 | len >>= 1; |
| 915 | spi_writel(as, RNCR, len); |
| 916 | spi_writel(as, TNCR, len); |
Haavard Skinnemoen | 8bacb21 | 2008-02-06 01:38:13 -0800 | [diff] [blame] | 917 | |
| 918 | dev_dbg(&msg->spi->dev, |
Emil Goode | 2de024b | 2013-07-30 19:35:35 +0200 | [diff] [blame] | 919 | " next xfer %p: len %u tx %p/%08llx rx %p/%08llx\n", |
| 920 | xfer, xfer->len, xfer->tx_buf, |
| 921 | (unsigned long long)xfer->tx_dma, xfer->rx_buf, |
| 922 | (unsigned long long)xfer->rx_dma); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 923 | } |
| 924 | |
Torsten Fleischer | 76e1d14 | 2015-02-24 16:32:57 +0100 | [diff] [blame] | 925 | /* REVISIT: We're waiting for RXBUFF before we start the next |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 926 | * transfer because we need to handle some difficult timing |
Torsten Fleischer | 76e1d14 | 2015-02-24 16:32:57 +0100 | [diff] [blame] | 927 | * issues otherwise. If we wait for TXBUFE in one transfer and |
| 928 | * then starts waiting for RXBUFF in the next, it's difficult |
| 929 | * to tell the difference between the RXBUFF interrupt we're |
| 930 | * actually waiting for and the RXBUFF interrupt of the |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 931 | * previous transfer. |
| 932 | * |
| 933 | * It should be doable, though. Just not now... |
| 934 | */ |
Torsten Fleischer | 76e1d14 | 2015-02-24 16:32:57 +0100 | [diff] [blame] | 935 | spi_writel(as, IER, SPI_BIT(RXBUFF) | SPI_BIT(OVRES)); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 936 | spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN)); |
| 937 | } |
| 938 | |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 939 | /* |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 940 | * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma: |
| 941 | * - The buffer is either valid for CPU access, else NULL |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 942 | * - If the buffer is valid, so is its DMA address |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 943 | * |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 944 | * This driver manages the dma address unless message->is_dma_mapped. |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 945 | */ |
| 946 | static int |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 947 | atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer) |
| 948 | { |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 949 | struct device *dev = &as->pdev->dev; |
| 950 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 951 | xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS; |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 952 | if (xfer->tx_buf) { |
Jean-Christophe PLAGNIOL-VILLARD | 214b574 | 2010-11-20 14:52:53 +0800 | [diff] [blame] | 953 | /* tx_buf is a const void* where we need a void * for the dma |
| 954 | * mapping */ |
| 955 | void *nonconst_tx = (void *)xfer->tx_buf; |
| 956 | |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 957 | xfer->tx_dma = dma_map_single(dev, |
Jean-Christophe PLAGNIOL-VILLARD | 214b574 | 2010-11-20 14:52:53 +0800 | [diff] [blame] | 958 | nonconst_tx, xfer->len, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 959 | DMA_TO_DEVICE); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 960 | if (dma_mapping_error(dev, xfer->tx_dma)) |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 961 | return -ENOMEM; |
| 962 | } |
| 963 | if (xfer->rx_buf) { |
| 964 | xfer->rx_dma = dma_map_single(dev, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 965 | xfer->rx_buf, xfer->len, |
| 966 | DMA_FROM_DEVICE); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 967 | if (dma_mapping_error(dev, xfer->rx_dma)) { |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 968 | if (xfer->tx_buf) |
| 969 | dma_unmap_single(dev, |
| 970 | xfer->tx_dma, xfer->len, |
| 971 | DMA_TO_DEVICE); |
| 972 | return -ENOMEM; |
| 973 | } |
| 974 | } |
| 975 | return 0; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 976 | } |
| 977 | |
| 978 | static void atmel_spi_dma_unmap_xfer(struct spi_master *master, |
| 979 | struct spi_transfer *xfer) |
| 980 | { |
| 981 | if (xfer->tx_dma != INVALID_DMA_ADDRESS) |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 982 | dma_unmap_single(master->dev.parent, xfer->tx_dma, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 983 | xfer->len, DMA_TO_DEVICE); |
| 984 | if (xfer->rx_dma != INVALID_DMA_ADDRESS) |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 985 | dma_unmap_single(master->dev.parent, xfer->rx_dma, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 986 | xfer->len, DMA_FROM_DEVICE); |
| 987 | } |
| 988 | |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 989 | static void atmel_spi_disable_pdc_transfer(struct atmel_spi *as) |
| 990 | { |
| 991 | spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS)); |
| 992 | } |
| 993 | |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 994 | static void |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 995 | atmel_spi_pump_single_data(struct atmel_spi *as, struct spi_transfer *xfer) |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 996 | { |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 997 | u8 *rxp; |
Richard Genoud | f557c98 | 2013-05-02 19:25:11 +0800 | [diff] [blame] | 998 | u16 *rxp16; |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 999 | unsigned long xfer_pos = xfer->len - as->current_remaining_bytes; |
| 1000 | |
Nicolas Ferre | 7910d9a | 2016-11-24 12:24:58 +0100 | [diff] [blame] | 1001 | if (xfer->bits_per_word > 8) { |
| 1002 | rxp16 = (u16 *)(((u8 *)xfer->rx_buf) + xfer_pos); |
| 1003 | *rxp16 = spi_readl(as, RDR); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1004 | } else { |
Nicolas Ferre | 7910d9a | 2016-11-24 12:24:58 +0100 | [diff] [blame] | 1005 | rxp = ((u8 *)xfer->rx_buf) + xfer_pos; |
| 1006 | *rxp = spi_readl(as, RDR); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1007 | } |
Richard Genoud | f557c98 | 2013-05-02 19:25:11 +0800 | [diff] [blame] | 1008 | if (xfer->bits_per_word > 8) { |
Alexandre Belloni | b112f05 | 2014-05-06 17:44:41 +0200 | [diff] [blame] | 1009 | if (as->current_remaining_bytes > 2) |
| 1010 | as->current_remaining_bytes -= 2; |
| 1011 | else |
Richard Genoud | f557c98 | 2013-05-02 19:25:11 +0800 | [diff] [blame] | 1012 | as->current_remaining_bytes = 0; |
| 1013 | } else { |
| 1014 | as->current_remaining_bytes--; |
| 1015 | } |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1016 | } |
| 1017 | |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 1018 | static void |
| 1019 | atmel_spi_pump_fifo_data(struct atmel_spi *as, struct spi_transfer *xfer) |
| 1020 | { |
| 1021 | u32 fifolr = spi_readl(as, FLR); |
| 1022 | u32 num_bytes, num_data = SPI_BFEXT(RXFL, fifolr); |
| 1023 | u32 offset = xfer->len - as->current_remaining_bytes; |
| 1024 | u16 *words = (u16 *)((u8 *)xfer->rx_buf + offset); |
| 1025 | u8 *bytes = (u8 *)((u8 *)xfer->rx_buf + offset); |
| 1026 | u16 rd; /* RD field is the lowest 16 bits of RDR */ |
| 1027 | |
| 1028 | /* Update the number of remaining bytes to transfer */ |
| 1029 | num_bytes = ((xfer->bits_per_word > 8) ? |
| 1030 | (num_data << 1) : |
| 1031 | num_data); |
| 1032 | |
| 1033 | if (as->current_remaining_bytes > num_bytes) |
| 1034 | as->current_remaining_bytes -= num_bytes; |
| 1035 | else |
| 1036 | as->current_remaining_bytes = 0; |
| 1037 | |
| 1038 | /* Handle odd number of bytes when data are more than 8bit width */ |
| 1039 | if (xfer->bits_per_word > 8) |
| 1040 | as->current_remaining_bytes &= ~0x1; |
| 1041 | |
| 1042 | /* Read data */ |
| 1043 | while (num_data) { |
| 1044 | rd = spi_readl(as, RDR); |
Nicolas Ferre | 7910d9a | 2016-11-24 12:24:58 +0100 | [diff] [blame] | 1045 | if (xfer->bits_per_word > 8) |
| 1046 | *words++ = rd; |
| 1047 | else |
| 1048 | *bytes++ = rd; |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 1049 | num_data--; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | /* Called from IRQ |
| 1054 | * |
| 1055 | * Must update "current_remaining_bytes" to keep track of data |
| 1056 | * to transfer. |
| 1057 | */ |
| 1058 | static void |
| 1059 | atmel_spi_pump_pio_data(struct atmel_spi *as, struct spi_transfer *xfer) |
| 1060 | { |
| 1061 | if (as->fifo_size) |
| 1062 | atmel_spi_pump_fifo_data(as, xfer); |
| 1063 | else |
| 1064 | atmel_spi_pump_single_data(as, xfer); |
| 1065 | } |
| 1066 | |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1067 | /* Interrupt |
| 1068 | * |
| 1069 | * No need for locking in this Interrupt handler: done_status is the |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1070 | * only information modified. |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1071 | */ |
| 1072 | static irqreturn_t |
| 1073 | atmel_spi_pio_interrupt(int irq, void *dev_id) |
| 1074 | { |
| 1075 | struct spi_master *master = dev_id; |
| 1076 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 1077 | u32 status, pending, imr; |
| 1078 | struct spi_transfer *xfer; |
| 1079 | int ret = IRQ_NONE; |
| 1080 | |
| 1081 | imr = spi_readl(as, IMR); |
| 1082 | status = spi_readl(as, SR); |
| 1083 | pending = status & imr; |
| 1084 | |
| 1085 | if (pending & SPI_BIT(OVRES)) { |
| 1086 | ret = IRQ_HANDLED; |
| 1087 | spi_writel(as, IDR, SPI_BIT(OVRES)); |
| 1088 | dev_warn(master->dev.parent, "overrun\n"); |
| 1089 | |
| 1090 | /* |
| 1091 | * When we get an overrun, we disregard the current |
| 1092 | * transfer. Data will not be copied back from any |
| 1093 | * bounce buffer and msg->actual_len will not be |
| 1094 | * updated with the last xfer. |
| 1095 | * |
| 1096 | * We will also not process any remaning transfers in |
| 1097 | * the message. |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1098 | */ |
| 1099 | as->done_status = -EIO; |
| 1100 | smp_wmb(); |
| 1101 | |
| 1102 | /* Clear any overrun happening while cleaning up */ |
| 1103 | spi_readl(as, SR); |
| 1104 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1105 | complete(&as->xfer_completion); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1106 | |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 1107 | } else if (pending & (SPI_BIT(RDRF) | SPI_BIT(RXFTHF))) { |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1108 | atmel_spi_lock(as); |
| 1109 | |
| 1110 | if (as->current_remaining_bytes) { |
| 1111 | ret = IRQ_HANDLED; |
| 1112 | xfer = as->current_transfer; |
| 1113 | atmel_spi_pump_pio_data(as, xfer); |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1114 | if (!as->current_remaining_bytes) |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1115 | spi_writel(as, IDR, pending); |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1116 | |
| 1117 | complete(&as->xfer_completion); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1118 | } |
| 1119 | |
| 1120 | atmel_spi_unlock(as); |
| 1121 | } else { |
| 1122 | WARN_ONCE(pending, "IRQ not handled, pending = %x\n", pending); |
| 1123 | ret = IRQ_HANDLED; |
| 1124 | spi_writel(as, IDR, pending); |
| 1125 | } |
| 1126 | |
| 1127 | return ret; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | static irqreturn_t |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1131 | atmel_spi_pdc_interrupt(int irq, void *dev_id) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1132 | { |
| 1133 | struct spi_master *master = dev_id; |
| 1134 | struct atmel_spi *as = spi_master_get_devdata(master); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1135 | u32 status, pending, imr; |
| 1136 | int ret = IRQ_NONE; |
| 1137 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1138 | imr = spi_readl(as, IMR); |
| 1139 | status = spi_readl(as, SR); |
| 1140 | pending = status & imr; |
| 1141 | |
| 1142 | if (pending & SPI_BIT(OVRES)) { |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1143 | |
| 1144 | ret = IRQ_HANDLED; |
| 1145 | |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 1146 | spi_writel(as, IDR, (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1147 | | SPI_BIT(OVRES))); |
| 1148 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1149 | /* Clear any overrun happening while cleaning up */ |
| 1150 | spi_readl(as, SR); |
| 1151 | |
Nicolas Ferre | 823cd04 | 2013-03-19 15:45:01 +0800 | [diff] [blame] | 1152 | as->done_status = -EIO; |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1153 | |
| 1154 | complete(&as->xfer_completion); |
| 1155 | |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 1156 | } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) { |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1157 | ret = IRQ_HANDLED; |
| 1158 | |
| 1159 | spi_writel(as, IDR, pending); |
| 1160 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1161 | complete(&as->xfer_completion); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1162 | } |
| 1163 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1164 | return ret; |
| 1165 | } |
| 1166 | |
Alexandru Ardelean | 6c613f6 | 2019-09-26 13:51:35 +0300 | [diff] [blame] | 1167 | static int atmel_word_delay_csr(struct spi_device *spi, struct atmel_spi *as) |
| 1168 | { |
| 1169 | struct spi_delay *delay = &spi->word_delay; |
| 1170 | u32 value = delay->value; |
| 1171 | |
| 1172 | switch (delay->unit) { |
| 1173 | case SPI_DELAY_UNIT_NSECS: |
| 1174 | value /= 1000; |
| 1175 | break; |
| 1176 | case SPI_DELAY_UNIT_USECS: |
| 1177 | break; |
| 1178 | default: |
| 1179 | return -EINVAL; |
| 1180 | } |
| 1181 | |
| 1182 | return (as->spi_clk / 1000000 * value) >> 5; |
| 1183 | } |
| 1184 | |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 1185 | static void initialize_native_cs_for_gpio(struct atmel_spi *as) |
| 1186 | { |
| 1187 | int i; |
| 1188 | struct spi_master *master = platform_get_drvdata(as->pdev); |
| 1189 | |
| 1190 | if (!as->native_cs_free) |
| 1191 | return; /* already initialized */ |
| 1192 | |
| 1193 | if (!master->cs_gpiods) |
| 1194 | return; /* No CS GPIO */ |
| 1195 | |
Gregory CLEMENT | 9c86f12 | 2019-10-17 16:18:46 +0200 | [diff] [blame] | 1196 | /* |
| 1197 | * On the first version of the controller (AT91RM9200), CS0 |
| 1198 | * can't be used associated with GPIO |
| 1199 | */ |
| 1200 | if (atmel_spi_is_v2(as)) |
| 1201 | i = 0; |
| 1202 | else |
| 1203 | i = 1; |
| 1204 | |
| 1205 | for (; i < 4; i++) |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 1206 | if (master->cs_gpiods[i]) |
| 1207 | as->native_cs_free |= BIT(i); |
| 1208 | |
| 1209 | if (as->native_cs_free) |
| 1210 | as->native_cs_for_gpio = ffs(as->native_cs_free); |
| 1211 | } |
| 1212 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1213 | static int atmel_spi_setup(struct spi_device *spi) |
| 1214 | { |
| 1215 | struct atmel_spi *as; |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 1216 | struct atmel_spi_device *asd; |
Richard Genoud | d3b72c7 | 2013-11-07 10:34:06 +0100 | [diff] [blame] | 1217 | u32 csr; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1218 | unsigned int bits = spi->bits_per_word; |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 1219 | int chip_select; |
Alexandru Ardelean | 6c613f6 | 2019-09-26 13:51:35 +0300 | [diff] [blame] | 1220 | int word_delay_csr; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1221 | |
| 1222 | as = spi_master_get_devdata(spi->master); |
| 1223 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 1224 | /* see notes above re chipselect */ |
Gregory CLEMENT | 585d18f | 2019-10-17 16:18:42 +0200 | [diff] [blame] | 1225 | if (!spi->cs_gpiod && (spi->mode & SPI_CS_HIGH)) { |
Gregory CLEMENT | 7cbb16b | 2019-10-17 16:18:41 +0200 | [diff] [blame] | 1226 | dev_warn(&spi->dev, "setup: non GPIO CS can't be active-high\n"); |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 1227 | return -EINVAL; |
| 1228 | } |
| 1229 | |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 1230 | /* Setup() is called during spi_register_controller(aka |
| 1231 | * spi_register_master) but after all membmers of the cs_gpiod |
| 1232 | * array have been filled, so we can looked for which native |
| 1233 | * CS will be free for using with GPIO |
| 1234 | */ |
| 1235 | initialize_native_cs_for_gpio(as); |
| 1236 | |
| 1237 | if (spi->cs_gpiod && as->native_cs_free) { |
| 1238 | dev_err(&spi->dev, |
| 1239 | "No native CS available to support this GPIO CS\n"); |
| 1240 | return -EBUSY; |
| 1241 | } |
| 1242 | |
| 1243 | if (spi->cs_gpiod) |
| 1244 | chip_select = as->native_cs_for_gpio; |
| 1245 | else |
| 1246 | chip_select = spi->chip_select; |
| 1247 | |
Richard Genoud | d3b72c7 | 2013-11-07 10:34:06 +0100 | [diff] [blame] | 1248 | csr = SPI_BF(BITS, bits - 8); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1249 | if (spi->mode & SPI_CPOL) |
| 1250 | csr |= SPI_BIT(CPOL); |
| 1251 | if (!(spi->mode & SPI_CPHA)) |
| 1252 | csr |= SPI_BIT(NCPHA); |
| 1253 | |
Gregory CLEMENT | 585d18f | 2019-10-17 16:18:42 +0200 | [diff] [blame] | 1254 | if (!spi->cs_gpiod) |
| 1255 | csr |= SPI_BIT(CSAAT); |
Haavard Skinnemoen | 1eed29d | 2008-02-06 01:38:11 -0800 | [diff] [blame] | 1256 | csr |= SPI_BF(DLYBS, 0); |
Jonas Bonn | 473a78a | 2019-01-30 09:40:05 +0100 | [diff] [blame] | 1257 | |
Alexandru Ardelean | 6c613f6 | 2019-09-26 13:51:35 +0300 | [diff] [blame] | 1258 | word_delay_csr = atmel_word_delay_csr(spi, as); |
| 1259 | if (word_delay_csr < 0) |
| 1260 | return word_delay_csr; |
| 1261 | |
Jonas Bonn | 473a78a | 2019-01-30 09:40:05 +0100 | [diff] [blame] | 1262 | /* DLYBCT adds delays between words. This is useful for slow devices |
| 1263 | * that need a bit of time to setup the next transfer. |
| 1264 | */ |
Alexandru Ardelean | 6c613f6 | 2019-09-26 13:51:35 +0300 | [diff] [blame] | 1265 | csr |= SPI_BF(DLYBCT, word_delay_csr); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1266 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 1267 | asd = spi->controller_state; |
| 1268 | if (!asd) { |
| 1269 | asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL); |
| 1270 | if (!asd) |
| 1271 | return -ENOMEM; |
| 1272 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 1273 | spi->controller_state = asd; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1274 | } |
| 1275 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 1276 | asd->csr = csr; |
| 1277 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1278 | dev_dbg(&spi->dev, |
Richard Genoud | d3b72c7 | 2013-11-07 10:34:06 +0100 | [diff] [blame] | 1279 | "setup: bpw %u mode 0x%x -> csr%d %08x\n", |
| 1280 | bits, spi->mode, spi->chip_select, csr); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1281 | |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 1282 | if (!atmel_spi_is_v2(as)) |
Gregory CLEMENT | 57e3137 | 2019-10-17 16:18:45 +0200 | [diff] [blame] | 1283 | spi_writel(as, CSR0 + 4 * chip_select, csr); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1284 | |
| 1285 | return 0; |
| 1286 | } |
| 1287 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1288 | static int atmel_spi_one_transfer(struct spi_master *master, |
| 1289 | struct spi_message *msg, |
| 1290 | struct spi_transfer *xfer) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1291 | { |
| 1292 | struct atmel_spi *as; |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1293 | struct spi_device *spi = msg->spi; |
Matthias Brugger | b9d228f | 2010-10-13 17:51:02 +0200 | [diff] [blame] | 1294 | u8 bits; |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1295 | u32 len; |
Matthias Brugger | b9d228f | 2010-10-13 17:51:02 +0200 | [diff] [blame] | 1296 | struct atmel_spi_device *asd; |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1297 | int timeout; |
| 1298 | int ret; |
Nicholas Mc Guire | 1369dea | 2015-02-02 10:43:31 -0500 | [diff] [blame] | 1299 | unsigned long dma_timeout; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1300 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1301 | as = spi_master_get_devdata(master); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1302 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1303 | if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) { |
| 1304 | dev_dbg(&spi->dev, "missing rx or tx buf\n"); |
| 1305 | return -EINVAL; |
| 1306 | } |
| 1307 | |
Jarkko Nikula | e864658 | 2015-09-25 09:03:01 +0300 | [diff] [blame] | 1308 | asd = spi->controller_state; |
| 1309 | bits = (asd->csr >> 4) & 0xf; |
| 1310 | if (bits != xfer->bits_per_word - 8) { |
| 1311 | dev_dbg(&spi->dev, |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1312 | "you can't yet change bits_per_word in transfers\n"); |
Jarkko Nikula | e864658 | 2015-09-25 09:03:01 +0300 | [diff] [blame] | 1313 | return -ENOPROTOOPT; |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1314 | } |
| 1315 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1316 | /* |
| 1317 | * DMA map early, for performance (empties dcache ASAP) and |
| 1318 | * better fault reporting. |
| 1319 | */ |
| 1320 | if ((!msg->is_dma_mapped) |
Cyrille Pitchen | 04242ca | 2016-11-24 12:24:59 +0100 | [diff] [blame] | 1321 | && as->use_pdc) { |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1322 | if (atmel_spi_dma_map_xfer(as, xfer) < 0) |
| 1323 | return -ENOMEM; |
| 1324 | } |
| 1325 | |
| 1326 | atmel_spi_set_xfer_speed(as, msg->spi, xfer); |
| 1327 | |
| 1328 | as->done_status = 0; |
| 1329 | as->current_transfer = xfer; |
| 1330 | as->current_remaining_bytes = xfer->len; |
| 1331 | while (as->current_remaining_bytes) { |
| 1332 | reinit_completion(&as->xfer_completion); |
| 1333 | |
| 1334 | if (as->use_pdc) { |
| 1335 | atmel_spi_pdc_next_xfer(master, msg, xfer); |
| 1336 | } else if (atmel_spi_use_dma(as, xfer)) { |
| 1337 | len = as->current_remaining_bytes; |
| 1338 | ret = atmel_spi_next_xfer_dma_submit(master, |
| 1339 | xfer, &len); |
| 1340 | if (ret) { |
| 1341 | dev_err(&spi->dev, |
| 1342 | "unable to use DMA, fallback to PIO\n"); |
| 1343 | atmel_spi_next_xfer_pio(master, xfer); |
| 1344 | } else { |
| 1345 | as->current_remaining_bytes -= len; |
Axel Lin | 0c3b974 | 2014-03-27 09:26:38 +0800 | [diff] [blame] | 1346 | if (as->current_remaining_bytes < 0) |
| 1347 | as->current_remaining_bytes = 0; |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1348 | } |
| 1349 | } else { |
| 1350 | atmel_spi_next_xfer_pio(master, xfer); |
| 1351 | } |
| 1352 | |
Alexander Stein | 1676014 | 2014-04-13 12:45:10 +0200 | [diff] [blame] | 1353 | /* interrupts are disabled, so free the lock for schedule */ |
| 1354 | atmel_spi_unlock(as); |
Nicholas Mc Guire | 1369dea | 2015-02-02 10:43:31 -0500 | [diff] [blame] | 1355 | dma_timeout = wait_for_completion_timeout(&as->xfer_completion, |
| 1356 | SPI_DMA_TIMEOUT); |
Alexander Stein | 1676014 | 2014-04-13 12:45:10 +0200 | [diff] [blame] | 1357 | atmel_spi_lock(as); |
Nicholas Mc Guire | 1369dea | 2015-02-02 10:43:31 -0500 | [diff] [blame] | 1358 | if (WARN_ON(dma_timeout == 0)) { |
| 1359 | dev_err(&spi->dev, "spi transfer timeout\n"); |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1360 | as->done_status = -EIO; |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | if (as->done_status) |
| 1364 | break; |
| 1365 | } |
| 1366 | |
| 1367 | if (as->done_status) { |
| 1368 | if (as->use_pdc) { |
| 1369 | dev_warn(master->dev.parent, |
| 1370 | "overrun (%u/%u remaining)\n", |
| 1371 | spi_readl(as, TCR), spi_readl(as, RCR)); |
| 1372 | |
| 1373 | /* |
| 1374 | * Clean up DMA registers and make sure the data |
| 1375 | * registers are empty. |
| 1376 | */ |
| 1377 | spi_writel(as, RNCR, 0); |
| 1378 | spi_writel(as, TNCR, 0); |
| 1379 | spi_writel(as, RCR, 0); |
| 1380 | spi_writel(as, TCR, 0); |
| 1381 | for (timeout = 1000; timeout; timeout--) |
| 1382 | if (spi_readl(as, SR) & SPI_BIT(TXEMPTY)) |
| 1383 | break; |
| 1384 | if (!timeout) |
| 1385 | dev_warn(master->dev.parent, |
| 1386 | "timeout waiting for TXEMPTY"); |
| 1387 | while (spi_readl(as, SR) & SPI_BIT(RDRF)) |
| 1388 | spi_readl(as, RDR); |
| 1389 | |
| 1390 | /* Clear any overrun happening while cleaning up */ |
| 1391 | spi_readl(as, SR); |
| 1392 | |
| 1393 | } else if (atmel_spi_use_dma(as, xfer)) { |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 1394 | atmel_spi_stop_dma(master); |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1395 | } |
| 1396 | |
| 1397 | if (!msg->is_dma_mapped |
Cyrille Pitchen | 04242ca | 2016-11-24 12:24:59 +0100 | [diff] [blame] | 1398 | && as->use_pdc) |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1399 | atmel_spi_dma_unmap_xfer(master, xfer); |
| 1400 | |
| 1401 | return 0; |
| 1402 | |
| 1403 | } else { |
| 1404 | /* only update length if no error */ |
| 1405 | msg->actual_length += xfer->len; |
| 1406 | } |
| 1407 | |
| 1408 | if (!msg->is_dma_mapped |
Cyrille Pitchen | 04242ca | 2016-11-24 12:24:59 +0100 | [diff] [blame] | 1409 | && as->use_pdc) |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1410 | atmel_spi_dma_unmap_xfer(master, xfer); |
| 1411 | |
Alexandru Ardelean | e74dc5c | 2019-09-26 13:51:37 +0300 | [diff] [blame] | 1412 | spi_transfer_delay_exec(xfer); |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1413 | |
| 1414 | if (xfer->cs_change) { |
| 1415 | if (list_is_last(&xfer->transfer_list, |
| 1416 | &msg->transfers)) { |
| 1417 | as->keep_cs = true; |
| 1418 | } else { |
| 1419 | as->cs_active = !as->cs_active; |
| 1420 | if (as->cs_active) |
| 1421 | cs_activate(as, msg->spi); |
| 1422 | else |
| 1423 | cs_deactivate(as, msg->spi); |
| 1424 | } |
| 1425 | } |
| 1426 | |
| 1427 | return 0; |
| 1428 | } |
| 1429 | |
| 1430 | static int atmel_spi_transfer_one_message(struct spi_master *master, |
| 1431 | struct spi_message *msg) |
| 1432 | { |
| 1433 | struct atmel_spi *as; |
| 1434 | struct spi_transfer *xfer; |
| 1435 | struct spi_device *spi = msg->spi; |
| 1436 | int ret = 0; |
| 1437 | |
| 1438 | as = spi_master_get_devdata(master); |
| 1439 | |
| 1440 | dev_dbg(&spi->dev, "new message %p submitted for %s\n", |
| 1441 | msg, dev_name(&spi->dev)); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1442 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1443 | atmel_spi_lock(as); |
| 1444 | cs_activate(as, spi); |
| 1445 | |
| 1446 | as->cs_active = true; |
| 1447 | as->keep_cs = false; |
| 1448 | |
| 1449 | msg->status = 0; |
| 1450 | msg->actual_length = 0; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1451 | |
| 1452 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
Uwe Kleine-König | 3c0448d | 2019-08-01 22:47:10 +0200 | [diff] [blame] | 1453 | trace_spi_transfer_start(msg, xfer); |
| 1454 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1455 | ret = atmel_spi_one_transfer(master, msg, xfer); |
| 1456 | if (ret) |
| 1457 | goto msg_done; |
Uwe Kleine-König | 3c0448d | 2019-08-01 22:47:10 +0200 | [diff] [blame] | 1458 | |
| 1459 | trace_spi_transfer_stop(msg, xfer); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1460 | } |
| 1461 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1462 | if (as->use_pdc) |
| 1463 | atmel_spi_disable_pdc_transfer(as); |
| 1464 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1465 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1466 | dev_dbg(&spi->dev, |
Randy Dunlap | 54f4c51 | 2014-03-21 08:53:41 -0700 | [diff] [blame] | 1467 | " xfer %p: len %u tx %p/%pad rx %p/%pad\n", |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1468 | xfer, xfer->len, |
Randy Dunlap | 54f4c51 | 2014-03-21 08:53:41 -0700 | [diff] [blame] | 1469 | xfer->tx_buf, &xfer->tx_dma, |
| 1470 | xfer->rx_buf, &xfer->rx_dma); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1471 | } |
| 1472 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1473 | msg_done: |
| 1474 | if (!as->keep_cs) |
| 1475 | cs_deactivate(as, msg->spi); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1476 | |
Nicolas Ferre | 8aad792 | 2013-04-03 13:58:36 +0800 | [diff] [blame] | 1477 | atmel_spi_unlock(as); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1478 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1479 | msg->status = as->done_status; |
| 1480 | spi_finalize_current_message(spi->master); |
| 1481 | |
| 1482 | return ret; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1483 | } |
| 1484 | |
David Brownell | bb2d1c3 | 2007-02-20 13:58:19 -0800 | [diff] [blame] | 1485 | static void atmel_spi_cleanup(struct spi_device *spi) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1486 | { |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 1487 | struct atmel_spi_device *asd = spi->controller_state; |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 1488 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 1489 | if (!asd) |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 1490 | return; |
| 1491 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 1492 | spi->controller_state = NULL; |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 1493 | kfree(asd); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1494 | } |
| 1495 | |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 1496 | static inline unsigned int atmel_get_version(struct atmel_spi *as) |
| 1497 | { |
| 1498 | return spi_readl(as, VERSION) & 0x00000fff; |
| 1499 | } |
| 1500 | |
| 1501 | static void atmel_get_caps(struct atmel_spi *as) |
| 1502 | { |
| 1503 | unsigned int version; |
| 1504 | |
| 1505 | version = atmel_get_version(as); |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 1506 | |
| 1507 | as->caps.is_spi2 = version > 0x121; |
| 1508 | as->caps.has_wdrbt = version >= 0x210; |
| 1509 | as->caps.has_dma_support = version >= 0x212; |
Cyrille Pitchen | 7094576 | 2017-06-23 17:39:16 +0200 | [diff] [blame] | 1510 | as->caps.has_pdc_support = version < 0x212; |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 1511 | } |
| 1512 | |
Quentin Schulz | 05514c8 | 2017-04-12 09:05:19 +0200 | [diff] [blame] | 1513 | static void atmel_spi_init(struct atmel_spi *as) |
| 1514 | { |
| 1515 | spi_writel(as, CR, SPI_BIT(SWRST)); |
| 1516 | spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */ |
Eugen Hristev | 9581329 | 2018-02-27 12:25:07 +0200 | [diff] [blame] | 1517 | |
| 1518 | /* It is recommended to enable FIFOs first thing after reset */ |
| 1519 | if (as->fifo_size) |
| 1520 | spi_writel(as, CR, SPI_BIT(FIFOEN)); |
| 1521 | |
Quentin Schulz | 05514c8 | 2017-04-12 09:05:19 +0200 | [diff] [blame] | 1522 | if (as->caps.has_wdrbt) { |
| 1523 | spi_writel(as, MR, SPI_BIT(WDRBT) | SPI_BIT(MODFDIS) |
| 1524 | | SPI_BIT(MSTR)); |
| 1525 | } else { |
| 1526 | spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS)); |
| 1527 | } |
| 1528 | |
| 1529 | if (as->use_pdc) |
| 1530 | spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS)); |
| 1531 | spi_writel(as, CR, SPI_BIT(SPIEN)); |
Quentin Schulz | 05514c8 | 2017-04-12 09:05:19 +0200 | [diff] [blame] | 1532 | } |
| 1533 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1534 | static int atmel_spi_probe(struct platform_device *pdev) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1535 | { |
| 1536 | struct resource *regs; |
| 1537 | int irq; |
| 1538 | struct clk *clk; |
| 1539 | int ret; |
| 1540 | struct spi_master *master; |
| 1541 | struct atmel_spi *as; |
| 1542 | |
Wenyou Yang | 5bdfd49 | 2014-03-05 09:58:49 +0800 | [diff] [blame] | 1543 | /* Select default pin state */ |
| 1544 | pinctrl_pm_select_default_state(&pdev->dev); |
| 1545 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1546 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1547 | if (!regs) |
| 1548 | return -ENXIO; |
| 1549 | |
| 1550 | irq = platform_get_irq(pdev, 0); |
| 1551 | if (irq < 0) |
| 1552 | return irq; |
| 1553 | |
Jingoo Han | 9f87d6f | 2013-12-04 14:07:51 +0900 | [diff] [blame] | 1554 | clk = devm_clk_get(&pdev->dev, "spi_clk"); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1555 | if (IS_ERR(clk)) |
| 1556 | return PTR_ERR(clk); |
| 1557 | |
| 1558 | /* setup spi core then atmel-specific driver state */ |
| 1559 | ret = -ENOMEM; |
Sachin Kamat | a536d76 | 2013-09-10 17:06:27 +0530 | [diff] [blame] | 1560 | master = spi_alloc_master(&pdev->dev, sizeof(*as)); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1561 | if (!master) |
| 1562 | goto out_free; |
| 1563 | |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 1564 | /* the spi->mode bits understood by this driver: */ |
Linus Walleij | efc92fb | 2019-01-07 16:51:52 +0100 | [diff] [blame] | 1565 | master->use_gpio_descriptors = true; |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 1566 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 1567 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 16); |
Jean-Christophe PLAGNIOL-VILLARD | 850a5b6 | 2012-11-23 13:44:39 +0100 | [diff] [blame] | 1568 | master->dev.of_node = pdev->dev.of_node; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1569 | master->bus_num = pdev->id; |
Gregory CLEMENT | 1cb84b0 | 2019-10-17 16:18:44 +0200 | [diff] [blame] | 1570 | master->num_chipselect = 4; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1571 | master->setup = atmel_spi_setup; |
Nicolas Ferre | 7910d9a | 2016-11-24 12:24:58 +0100 | [diff] [blame] | 1572 | master->flags = (SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX); |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1573 | master->transfer_one_message = atmel_spi_transfer_one_message; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1574 | master->cleanup = atmel_spi_cleanup; |
Wenyou Yang | ce0c4ca | 2014-10-16 17:23:10 +0800 | [diff] [blame] | 1575 | master->auto_runtime_pm = true; |
Nicolas Ferre | 7910d9a | 2016-11-24 12:24:58 +0100 | [diff] [blame] | 1576 | master->max_dma_len = SPI_MAX_DMA_XFER; |
Cyrille Pitchen | 04242ca | 2016-11-24 12:24:59 +0100 | [diff] [blame] | 1577 | master->can_dma = atmel_spi_can_dma; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1578 | platform_set_drvdata(pdev, master); |
| 1579 | |
| 1580 | as = spi_master_get_devdata(master); |
| 1581 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1582 | spin_lock_init(&as->lock); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1583 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1584 | as->pdev = pdev; |
Mark Brown | 3140747 | 2013-10-16 13:22:35 +0100 | [diff] [blame] | 1585 | as->regs = devm_ioremap_resource(&pdev->dev, regs); |
Wei Yongjun | 543c954 | 2013-10-21 11:12:02 +0800 | [diff] [blame] | 1586 | if (IS_ERR(as->regs)) { |
| 1587 | ret = PTR_ERR(as->regs); |
Nicolas Ferre | 7910d9a | 2016-11-24 12:24:58 +0100 | [diff] [blame] | 1588 | goto out_unmap_regs; |
Wei Yongjun | 543c954 | 2013-10-21 11:12:02 +0800 | [diff] [blame] | 1589 | } |
Nicolas Ferre | dfab30e | 2013-04-03 13:57:42 +0800 | [diff] [blame] | 1590 | as->phybase = regs->start; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1591 | as->irq = irq; |
| 1592 | as->clk = clk; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1593 | |
Wenyou Yang | 8090d6d | 2014-01-09 13:19:15 +0800 | [diff] [blame] | 1594 | init_completion(&as->xfer_completion); |
| 1595 | |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 1596 | atmel_get_caps(as); |
| 1597 | |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1598 | as->use_dma = false; |
| 1599 | as->use_pdc = false; |
| 1600 | if (as->caps.has_dma_support) { |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 1601 | ret = atmel_spi_configure_dma(master, as); |
Cyrille Pitchen | 04242ca | 2016-11-24 12:24:59 +0100 | [diff] [blame] | 1602 | if (ret == 0) { |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1603 | as->use_dma = true; |
Cyrille Pitchen | 04242ca | 2016-11-24 12:24:59 +0100 | [diff] [blame] | 1604 | } else if (ret == -EPROBE_DEFER) { |
Ludovic Desroches | 5e9af37 | 2014-11-14 17:12:54 +0100 | [diff] [blame] | 1605 | return ret; |
Cyrille Pitchen | 04242ca | 2016-11-24 12:24:59 +0100 | [diff] [blame] | 1606 | } |
Cyrille Pitchen | 7094576 | 2017-06-23 17:39:16 +0200 | [diff] [blame] | 1607 | } else if (as->caps.has_pdc_support) { |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1608 | as->use_pdc = true; |
| 1609 | } |
| 1610 | |
Radu Pirea | a9889ed | 2017-12-19 17:17:59 +0200 | [diff] [blame] | 1611 | if (IS_ENABLED(CONFIG_SOC_SAM_V4_V5)) { |
| 1612 | as->addr_rx_bbuf = dma_alloc_coherent(&pdev->dev, |
| 1613 | SPI_MAX_DMA_XFER, |
| 1614 | &as->dma_addr_rx_bbuf, |
| 1615 | GFP_KERNEL | GFP_DMA); |
| 1616 | if (!as->addr_rx_bbuf) { |
| 1617 | as->use_dma = false; |
| 1618 | } else { |
| 1619 | as->addr_tx_bbuf = dma_alloc_coherent(&pdev->dev, |
| 1620 | SPI_MAX_DMA_XFER, |
| 1621 | &as->dma_addr_tx_bbuf, |
| 1622 | GFP_KERNEL | GFP_DMA); |
| 1623 | if (!as->addr_tx_bbuf) { |
| 1624 | as->use_dma = false; |
| 1625 | dma_free_coherent(&pdev->dev, SPI_MAX_DMA_XFER, |
| 1626 | as->addr_rx_bbuf, |
| 1627 | as->dma_addr_rx_bbuf); |
| 1628 | } |
| 1629 | } |
| 1630 | if (!as->use_dma) |
| 1631 | dev_info(master->dev.parent, |
| 1632 | " can not allocate dma coherent memory\n"); |
| 1633 | } |
| 1634 | |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1635 | if (as->caps.has_dma_support && !as->use_dma) |
| 1636 | dev_info(&pdev->dev, "Atmel SPI Controller using PIO only\n"); |
| 1637 | |
| 1638 | if (as->use_pdc) { |
Jingoo Han | 9f87d6f | 2013-12-04 14:07:51 +0900 | [diff] [blame] | 1639 | ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pdc_interrupt, |
| 1640 | 0, dev_name(&pdev->dev), master); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1641 | } else { |
Jingoo Han | 9f87d6f | 2013-12-04 14:07:51 +0900 | [diff] [blame] | 1642 | ret = devm_request_irq(&pdev->dev, irq, atmel_spi_pio_interrupt, |
| 1643 | 0, dev_name(&pdev->dev), master); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1644 | } |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1645 | if (ret) |
| 1646 | goto out_unmap_regs; |
| 1647 | |
| 1648 | /* Initialize the hardware */ |
Boris BREZILLON | dfec4a6 | 2013-07-16 17:16:22 +0200 | [diff] [blame] | 1649 | ret = clk_prepare_enable(clk); |
| 1650 | if (ret) |
Sachin Kamat | de8cc23 | 2013-09-10 17:06:26 +0530 | [diff] [blame] | 1651 | goto out_free_irq; |
Ben Whitten | 39fe33f | 2016-11-14 15:13:20 +0000 | [diff] [blame] | 1652 | |
| 1653 | as->spi_clk = clk_get_rate(clk); |
| 1654 | |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 1655 | as->fifo_size = 0; |
| 1656 | if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size", |
| 1657 | &as->fifo_size)) { |
| 1658 | dev_info(&pdev->dev, "Using FIFO (%u data)\n", as->fifo_size); |
Cyrille Pitchen | 11f2764 | 2015-06-16 12:09:31 +0200 | [diff] [blame] | 1659 | } |
| 1660 | |
Quentin Schulz | 05514c8 | 2017-04-12 09:05:19 +0200 | [diff] [blame] | 1661 | atmel_spi_init(as); |
| 1662 | |
Wenyou Yang | ce0c4ca | 2014-10-16 17:23:10 +0800 | [diff] [blame] | 1663 | pm_runtime_set_autosuspend_delay(&pdev->dev, AUTOSUSPEND_TIMEOUT); |
| 1664 | pm_runtime_use_autosuspend(&pdev->dev); |
| 1665 | pm_runtime_set_active(&pdev->dev); |
| 1666 | pm_runtime_enable(&pdev->dev); |
| 1667 | |
Jingoo Han | 9f87d6f | 2013-12-04 14:07:51 +0900 | [diff] [blame] | 1668 | ret = devm_spi_register_master(&pdev->dev, master); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1669 | if (ret) |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1670 | goto out_free_dma; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1671 | |
Nicolas Ferre | ce24a51 | 2016-11-24 12:24:57 +0100 | [diff] [blame] | 1672 | /* go! */ |
Baruch Siach | 6aba9c6 | 2017-05-30 08:33:30 +0300 | [diff] [blame] | 1673 | dev_info(&pdev->dev, "Atmel SPI Controller version 0x%x at 0x%08lx (irq %d)\n", |
| 1674 | atmel_get_version(as), (unsigned long)regs->start, |
| 1675 | irq); |
Nicolas Ferre | ce24a51 | 2016-11-24 12:24:57 +0100 | [diff] [blame] | 1676 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1677 | return 0; |
| 1678 | |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1679 | out_free_dma: |
Wenyou Yang | ce0c4ca | 2014-10-16 17:23:10 +0800 | [diff] [blame] | 1680 | pm_runtime_disable(&pdev->dev); |
| 1681 | pm_runtime_set_suspended(&pdev->dev); |
| 1682 | |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1683 | if (as->use_dma) |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 1684 | atmel_spi_release_dma(master); |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1685 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1686 | spi_writel(as, CR, SPI_BIT(SWRST)); |
Jean-Christophe Lallemand | 50d7d5b | 2008-11-12 13:27:00 -0800 | [diff] [blame] | 1687 | spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */ |
Boris BREZILLON | dfec4a6 | 2013-07-16 17:16:22 +0200 | [diff] [blame] | 1688 | clk_disable_unprepare(clk); |
Sachin Kamat | de8cc23 | 2013-09-10 17:06:26 +0530 | [diff] [blame] | 1689 | out_free_irq: |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1690 | out_unmap_regs: |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1691 | out_free: |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1692 | spi_master_put(master); |
| 1693 | return ret; |
| 1694 | } |
| 1695 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1696 | static int atmel_spi_remove(struct platform_device *pdev) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1697 | { |
| 1698 | struct spi_master *master = platform_get_drvdata(pdev); |
| 1699 | struct atmel_spi *as = spi_master_get_devdata(master); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1700 | |
Wenyou Yang | ce0c4ca | 2014-10-16 17:23:10 +0800 | [diff] [blame] | 1701 | pm_runtime_get_sync(&pdev->dev); |
| 1702 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1703 | /* reset the hardware and block queue progress */ |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1704 | if (as->use_dma) { |
Nicolas Ferre | 768f3d9 | 2016-11-24 12:25:01 +0100 | [diff] [blame] | 1705 | atmel_spi_stop_dma(master); |
| 1706 | atmel_spi_release_dma(master); |
Radu Pirea | a9889ed | 2017-12-19 17:17:59 +0200 | [diff] [blame] | 1707 | if (IS_ENABLED(CONFIG_SOC_SAM_V4_V5)) { |
| 1708 | dma_free_coherent(&pdev->dev, SPI_MAX_DMA_XFER, |
| 1709 | as->addr_tx_bbuf, |
| 1710 | as->dma_addr_tx_bbuf); |
| 1711 | dma_free_coherent(&pdev->dev, SPI_MAX_DMA_XFER, |
| 1712 | as->addr_rx_bbuf, |
| 1713 | as->dma_addr_rx_bbuf); |
| 1714 | } |
Nicolas Ferre | 1ccc404 | 2013-04-03 13:59:19 +0800 | [diff] [blame] | 1715 | } |
| 1716 | |
Radu Pirea | 66e900a | 2017-12-15 17:40:17 +0200 | [diff] [blame] | 1717 | spin_lock_irq(&as->lock); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1718 | spi_writel(as, CR, SPI_BIT(SWRST)); |
Jean-Christophe Lallemand | 50d7d5b | 2008-11-12 13:27:00 -0800 | [diff] [blame] | 1719 | spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */ |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1720 | spi_readl(as, SR); |
| 1721 | spin_unlock_irq(&as->lock); |
| 1722 | |
Boris BREZILLON | dfec4a6 | 2013-07-16 17:16:22 +0200 | [diff] [blame] | 1723 | clk_disable_unprepare(as->clk); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1724 | |
Wenyou Yang | ce0c4ca | 2014-10-16 17:23:10 +0800 | [diff] [blame] | 1725 | pm_runtime_put_noidle(&pdev->dev); |
| 1726 | pm_runtime_disable(&pdev->dev); |
| 1727 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1728 | return 0; |
| 1729 | } |
| 1730 | |
Wenyou Yang | ce0c4ca | 2014-10-16 17:23:10 +0800 | [diff] [blame] | 1731 | #ifdef CONFIG_PM |
Wenyou Yang | ce0c4ca | 2014-10-16 17:23:10 +0800 | [diff] [blame] | 1732 | static int atmel_spi_runtime_suspend(struct device *dev) |
| 1733 | { |
| 1734 | struct spi_master *master = dev_get_drvdata(dev); |
| 1735 | struct atmel_spi *as = spi_master_get_devdata(master); |
Jingoo Han | ec60dd3 | 2013-09-09 17:54:12 +0900 | [diff] [blame] | 1736 | |
Wenyou Yang | ce0c4ca | 2014-10-16 17:23:10 +0800 | [diff] [blame] | 1737 | clk_disable_unprepare(as->clk); |
| 1738 | pinctrl_pm_select_sleep_state(dev); |
| 1739 | |
| 1740 | return 0; |
| 1741 | } |
| 1742 | |
| 1743 | static int atmel_spi_runtime_resume(struct device *dev) |
| 1744 | { |
| 1745 | struct spi_master *master = dev_get_drvdata(dev); |
| 1746 | struct atmel_spi *as = spi_master_get_devdata(master); |
Wenyou Yang | ce0c4ca | 2014-10-16 17:23:10 +0800 | [diff] [blame] | 1747 | |
| 1748 | pinctrl_pm_select_default_state(dev); |
| 1749 | |
Fengguang Wu | d0de6ff | 2014-10-17 00:18:56 +0800 | [diff] [blame] | 1750 | return clk_prepare_enable(as->clk); |
Wenyou Yang | ce0c4ca | 2014-10-16 17:23:10 +0800 | [diff] [blame] | 1751 | } |
Wenyou Yang | c1ee8f3 | 2014-10-21 11:43:34 +0800 | [diff] [blame] | 1752 | |
Alexandre Belloni | d630526 | 2015-09-10 10:19:52 +0200 | [diff] [blame] | 1753 | #ifdef CONFIG_PM_SLEEP |
Wenyou Yang | c1ee8f3 | 2014-10-21 11:43:34 +0800 | [diff] [blame] | 1754 | static int atmel_spi_suspend(struct device *dev) |
| 1755 | { |
| 1756 | struct spi_master *master = dev_get_drvdata(dev); |
| 1757 | int ret; |
| 1758 | |
| 1759 | /* Stop the queue running */ |
| 1760 | ret = spi_master_suspend(master); |
Geert Uytterhoeven | 7c5d8a2 | 2018-09-05 10:51:57 +0200 | [diff] [blame] | 1761 | if (ret) |
Wenyou Yang | c1ee8f3 | 2014-10-21 11:43:34 +0800 | [diff] [blame] | 1762 | return ret; |
Wenyou Yang | c1ee8f3 | 2014-10-21 11:43:34 +0800 | [diff] [blame] | 1763 | |
| 1764 | if (!pm_runtime_suspended(dev)) |
| 1765 | atmel_spi_runtime_suspend(dev); |
| 1766 | |
| 1767 | return 0; |
| 1768 | } |
| 1769 | |
| 1770 | static int atmel_spi_resume(struct device *dev) |
| 1771 | { |
| 1772 | struct spi_master *master = dev_get_drvdata(dev); |
Quentin Schulz | e538007 | 2017-04-14 10:22:43 +0200 | [diff] [blame] | 1773 | struct atmel_spi *as = spi_master_get_devdata(master); |
Wenyou Yang | c1ee8f3 | 2014-10-21 11:43:34 +0800 | [diff] [blame] | 1774 | int ret; |
| 1775 | |
Quentin Schulz | e538007 | 2017-04-14 10:22:43 +0200 | [diff] [blame] | 1776 | ret = clk_prepare_enable(as->clk); |
| 1777 | if (ret) |
| 1778 | return ret; |
| 1779 | |
| 1780 | atmel_spi_init(as); |
| 1781 | |
| 1782 | clk_disable_unprepare(as->clk); |
| 1783 | |
Wenyou Yang | c1ee8f3 | 2014-10-21 11:43:34 +0800 | [diff] [blame] | 1784 | if (!pm_runtime_suspended(dev)) { |
| 1785 | ret = atmel_spi_runtime_resume(dev); |
| 1786 | if (ret) |
| 1787 | return ret; |
| 1788 | } |
| 1789 | |
| 1790 | /* Start the queue running */ |
Geert Uytterhoeven | 7c5d8a2 | 2018-09-05 10:51:57 +0200 | [diff] [blame] | 1791 | return spi_master_resume(master); |
Wenyou Yang | c1ee8f3 | 2014-10-21 11:43:34 +0800 | [diff] [blame] | 1792 | } |
Alexandre Belloni | d630526 | 2015-09-10 10:19:52 +0200 | [diff] [blame] | 1793 | #endif |
Wenyou Yang | ce0c4ca | 2014-10-16 17:23:10 +0800 | [diff] [blame] | 1794 | |
| 1795 | static const struct dev_pm_ops atmel_spi_pm_ops = { |
| 1796 | SET_SYSTEM_SLEEP_PM_OPS(atmel_spi_suspend, atmel_spi_resume) |
| 1797 | SET_RUNTIME_PM_OPS(atmel_spi_runtime_suspend, |
| 1798 | atmel_spi_runtime_resume, NULL) |
| 1799 | }; |
Jingoo Han | ec60dd3 | 2013-09-09 17:54:12 +0900 | [diff] [blame] | 1800 | #define ATMEL_SPI_PM_OPS (&atmel_spi_pm_ops) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1801 | #else |
Jingoo Han | ec60dd3 | 2013-09-09 17:54:12 +0900 | [diff] [blame] | 1802 | #define ATMEL_SPI_PM_OPS NULL |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1803 | #endif |
| 1804 | |
Jean-Christophe PLAGNIOL-VILLARD | 850a5b6 | 2012-11-23 13:44:39 +0100 | [diff] [blame] | 1805 | static const struct of_device_id atmel_spi_dt_ids[] = { |
| 1806 | { .compatible = "atmel,at91rm9200-spi" }, |
| 1807 | { /* sentinel */ } |
| 1808 | }; |
| 1809 | |
| 1810 | MODULE_DEVICE_TABLE(of, atmel_spi_dt_ids); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1811 | |
| 1812 | static struct platform_driver atmel_spi_driver = { |
| 1813 | .driver = { |
| 1814 | .name = "atmel_spi", |
Jingoo Han | ec60dd3 | 2013-09-09 17:54:12 +0900 | [diff] [blame] | 1815 | .pm = ATMEL_SPI_PM_OPS, |
Gregory CLEMENT | 1cb84b0 | 2019-10-17 16:18:44 +0200 | [diff] [blame] | 1816 | .of_match_table = atmel_spi_dt_ids, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1817 | }, |
Jean-Christophe PLAGNIOL-VILLARD | 1cb201a | 2011-11-04 01:20:21 +0800 | [diff] [blame] | 1818 | .probe = atmel_spi_probe, |
Grant Likely | 2deff8d | 2013-02-05 13:27:35 +0000 | [diff] [blame] | 1819 | .remove = atmel_spi_remove, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1820 | }; |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 1821 | module_platform_driver(atmel_spi_driver); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1822 | |
| 1823 | MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver"); |
Jean Delvare | e05503e | 2011-05-18 16:49:24 +0200 | [diff] [blame] | 1824 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1825 | MODULE_LICENSE("GPL"); |
Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 1826 | MODULE_ALIAS("platform:atmel_spi"); |