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