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