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