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> |
| 18 | #include <linux/err.h> |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/spi/spi.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Jean-Christophe PLAGNIOL-VILLARD | bcd2360 | 2012-10-30 05:12:23 +0800 | [diff] [blame] | 22 | #include <linux/platform_data/atmel.h> |
Jean-Christophe PLAGNIOL-VILLARD | 850a5b6 | 2012-11-23 13:44:39 +0100 | [diff] [blame] | 23 | #include <linux/of.h> |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 24 | |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 25 | #include <linux/io.h> |
| 26 | #include <linux/gpio.h> |
David Brownell | bb2d1c3 | 2007-02-20 13:58:19 -0800 | [diff] [blame] | 27 | |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 28 | /* SPI register offsets */ |
| 29 | #define SPI_CR 0x0000 |
| 30 | #define SPI_MR 0x0004 |
| 31 | #define SPI_RDR 0x0008 |
| 32 | #define SPI_TDR 0x000c |
| 33 | #define SPI_SR 0x0010 |
| 34 | #define SPI_IER 0x0014 |
| 35 | #define SPI_IDR 0x0018 |
| 36 | #define SPI_IMR 0x001c |
| 37 | #define SPI_CSR0 0x0030 |
| 38 | #define SPI_CSR1 0x0034 |
| 39 | #define SPI_CSR2 0x0038 |
| 40 | #define SPI_CSR3 0x003c |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 41 | #define SPI_VERSION 0x00fc |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 42 | #define SPI_RPR 0x0100 |
| 43 | #define SPI_RCR 0x0104 |
| 44 | #define SPI_TPR 0x0108 |
| 45 | #define SPI_TCR 0x010c |
| 46 | #define SPI_RNPR 0x0110 |
| 47 | #define SPI_RNCR 0x0114 |
| 48 | #define SPI_TNPR 0x0118 |
| 49 | #define SPI_TNCR 0x011c |
| 50 | #define SPI_PTCR 0x0120 |
| 51 | #define SPI_PTSR 0x0124 |
| 52 | |
| 53 | /* Bitfields in CR */ |
| 54 | #define SPI_SPIEN_OFFSET 0 |
| 55 | #define SPI_SPIEN_SIZE 1 |
| 56 | #define SPI_SPIDIS_OFFSET 1 |
| 57 | #define SPI_SPIDIS_SIZE 1 |
| 58 | #define SPI_SWRST_OFFSET 7 |
| 59 | #define SPI_SWRST_SIZE 1 |
| 60 | #define SPI_LASTXFER_OFFSET 24 |
| 61 | #define SPI_LASTXFER_SIZE 1 |
| 62 | |
| 63 | /* Bitfields in MR */ |
| 64 | #define SPI_MSTR_OFFSET 0 |
| 65 | #define SPI_MSTR_SIZE 1 |
| 66 | #define SPI_PS_OFFSET 1 |
| 67 | #define SPI_PS_SIZE 1 |
| 68 | #define SPI_PCSDEC_OFFSET 2 |
| 69 | #define SPI_PCSDEC_SIZE 1 |
| 70 | #define SPI_FDIV_OFFSET 3 |
| 71 | #define SPI_FDIV_SIZE 1 |
| 72 | #define SPI_MODFDIS_OFFSET 4 |
| 73 | #define SPI_MODFDIS_SIZE 1 |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 74 | #define SPI_WDRBT_OFFSET 5 |
| 75 | #define SPI_WDRBT_SIZE 1 |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 76 | #define SPI_LLB_OFFSET 7 |
| 77 | #define SPI_LLB_SIZE 1 |
| 78 | #define SPI_PCS_OFFSET 16 |
| 79 | #define SPI_PCS_SIZE 4 |
| 80 | #define SPI_DLYBCS_OFFSET 24 |
| 81 | #define SPI_DLYBCS_SIZE 8 |
| 82 | |
| 83 | /* Bitfields in RDR */ |
| 84 | #define SPI_RD_OFFSET 0 |
| 85 | #define SPI_RD_SIZE 16 |
| 86 | |
| 87 | /* Bitfields in TDR */ |
| 88 | #define SPI_TD_OFFSET 0 |
| 89 | #define SPI_TD_SIZE 16 |
| 90 | |
| 91 | /* Bitfields in SR */ |
| 92 | #define SPI_RDRF_OFFSET 0 |
| 93 | #define SPI_RDRF_SIZE 1 |
| 94 | #define SPI_TDRE_OFFSET 1 |
| 95 | #define SPI_TDRE_SIZE 1 |
| 96 | #define SPI_MODF_OFFSET 2 |
| 97 | #define SPI_MODF_SIZE 1 |
| 98 | #define SPI_OVRES_OFFSET 3 |
| 99 | #define SPI_OVRES_SIZE 1 |
| 100 | #define SPI_ENDRX_OFFSET 4 |
| 101 | #define SPI_ENDRX_SIZE 1 |
| 102 | #define SPI_ENDTX_OFFSET 5 |
| 103 | #define SPI_ENDTX_SIZE 1 |
| 104 | #define SPI_RXBUFF_OFFSET 6 |
| 105 | #define SPI_RXBUFF_SIZE 1 |
| 106 | #define SPI_TXBUFE_OFFSET 7 |
| 107 | #define SPI_TXBUFE_SIZE 1 |
| 108 | #define SPI_NSSR_OFFSET 8 |
| 109 | #define SPI_NSSR_SIZE 1 |
| 110 | #define SPI_TXEMPTY_OFFSET 9 |
| 111 | #define SPI_TXEMPTY_SIZE 1 |
| 112 | #define SPI_SPIENS_OFFSET 16 |
| 113 | #define SPI_SPIENS_SIZE 1 |
| 114 | |
| 115 | /* Bitfields in CSR0 */ |
| 116 | #define SPI_CPOL_OFFSET 0 |
| 117 | #define SPI_CPOL_SIZE 1 |
| 118 | #define SPI_NCPHA_OFFSET 1 |
| 119 | #define SPI_NCPHA_SIZE 1 |
| 120 | #define SPI_CSAAT_OFFSET 3 |
| 121 | #define SPI_CSAAT_SIZE 1 |
| 122 | #define SPI_BITS_OFFSET 4 |
| 123 | #define SPI_BITS_SIZE 4 |
| 124 | #define SPI_SCBR_OFFSET 8 |
| 125 | #define SPI_SCBR_SIZE 8 |
| 126 | #define SPI_DLYBS_OFFSET 16 |
| 127 | #define SPI_DLYBS_SIZE 8 |
| 128 | #define SPI_DLYBCT_OFFSET 24 |
| 129 | #define SPI_DLYBCT_SIZE 8 |
| 130 | |
| 131 | /* Bitfields in RCR */ |
| 132 | #define SPI_RXCTR_OFFSET 0 |
| 133 | #define SPI_RXCTR_SIZE 16 |
| 134 | |
| 135 | /* Bitfields in TCR */ |
| 136 | #define SPI_TXCTR_OFFSET 0 |
| 137 | #define SPI_TXCTR_SIZE 16 |
| 138 | |
| 139 | /* Bitfields in RNCR */ |
| 140 | #define SPI_RXNCR_OFFSET 0 |
| 141 | #define SPI_RXNCR_SIZE 16 |
| 142 | |
| 143 | /* Bitfields in TNCR */ |
| 144 | #define SPI_TXNCR_OFFSET 0 |
| 145 | #define SPI_TXNCR_SIZE 16 |
| 146 | |
| 147 | /* Bitfields in PTCR */ |
| 148 | #define SPI_RXTEN_OFFSET 0 |
| 149 | #define SPI_RXTEN_SIZE 1 |
| 150 | #define SPI_RXTDIS_OFFSET 1 |
| 151 | #define SPI_RXTDIS_SIZE 1 |
| 152 | #define SPI_TXTEN_OFFSET 8 |
| 153 | #define SPI_TXTEN_SIZE 1 |
| 154 | #define SPI_TXTDIS_OFFSET 9 |
| 155 | #define SPI_TXTDIS_SIZE 1 |
| 156 | |
| 157 | /* Constants for BITS */ |
| 158 | #define SPI_BITS_8_BPT 0 |
| 159 | #define SPI_BITS_9_BPT 1 |
| 160 | #define SPI_BITS_10_BPT 2 |
| 161 | #define SPI_BITS_11_BPT 3 |
| 162 | #define SPI_BITS_12_BPT 4 |
| 163 | #define SPI_BITS_13_BPT 5 |
| 164 | #define SPI_BITS_14_BPT 6 |
| 165 | #define SPI_BITS_15_BPT 7 |
| 166 | #define SPI_BITS_16_BPT 8 |
| 167 | |
| 168 | /* Bit manipulation macros */ |
| 169 | #define SPI_BIT(name) \ |
| 170 | (1 << SPI_##name##_OFFSET) |
| 171 | #define SPI_BF(name,value) \ |
| 172 | (((value) & ((1 << SPI_##name##_SIZE) - 1)) << SPI_##name##_OFFSET) |
| 173 | #define SPI_BFEXT(name,value) \ |
| 174 | (((value) >> SPI_##name##_OFFSET) & ((1 << SPI_##name##_SIZE) - 1)) |
| 175 | #define SPI_BFINS(name,value,old) \ |
| 176 | ( ((old) & ~(((1 << SPI_##name##_SIZE) - 1) << SPI_##name##_OFFSET)) \ |
| 177 | | SPI_BF(name,value)) |
| 178 | |
| 179 | /* Register access macros */ |
| 180 | #define spi_readl(port,reg) \ |
| 181 | __raw_readl((port)->regs + SPI_##reg) |
| 182 | #define spi_writel(port,reg,value) \ |
| 183 | __raw_writel((value), (port)->regs + SPI_##reg) |
| 184 | |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 185 | struct atmel_spi_caps { |
| 186 | bool is_spi2; |
| 187 | bool has_wdrbt; |
| 188 | bool has_dma_support; |
| 189 | }; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 190 | |
| 191 | /* |
| 192 | * The core SPI transfer engine just talks to a register bank to set up |
| 193 | * DMA transfers; transfer queue progress is driven by IRQs. The clock |
| 194 | * framework provides the base clock, subdivided for each spi_device. |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 195 | */ |
| 196 | struct atmel_spi { |
| 197 | spinlock_t lock; |
| 198 | |
| 199 | void __iomem *regs; |
| 200 | int irq; |
| 201 | struct clk *clk; |
| 202 | struct platform_device *pdev; |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 203 | struct spi_device *stay; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 204 | |
| 205 | u8 stopping; |
| 206 | struct list_head queue; |
| 207 | struct spi_transfer *current_transfer; |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 208 | unsigned long current_remaining_bytes; |
| 209 | struct spi_transfer *next_transfer; |
| 210 | unsigned long next_remaining_bytes; |
Nicolas Ferre | 823cd04 | 2013-03-19 15:45:01 +0800 | [diff] [blame] | 211 | int done_status; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 212 | |
| 213 | void *buffer; |
| 214 | dma_addr_t buffer_dma; |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 215 | |
| 216 | struct atmel_spi_caps caps; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 217 | }; |
| 218 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 219 | /* Controller-specific per-slave state */ |
| 220 | struct atmel_spi_device { |
| 221 | unsigned int npcs_pin; |
| 222 | u32 csr; |
| 223 | }; |
| 224 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 225 | #define BUFFER_SIZE PAGE_SIZE |
| 226 | #define INVALID_DMA_ADDRESS 0xffffffff |
| 227 | |
| 228 | /* |
Haavard Skinnemoen | 5bfa26c | 2009-01-06 14:41:42 -0800 | [diff] [blame] | 229 | * Version 2 of the SPI controller has |
| 230 | * - CR.LASTXFER |
| 231 | * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero) |
| 232 | * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs) |
| 233 | * - SPI_CSRx.CSAAT |
| 234 | * - SPI_CSRx.SBCR allows faster clocking |
Haavard Skinnemoen | 5bfa26c | 2009-01-06 14:41:42 -0800 | [diff] [blame] | 235 | */ |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 236 | static bool atmel_spi_is_v2(struct atmel_spi *as) |
Haavard Skinnemoen | 5bfa26c | 2009-01-06 14:41:42 -0800 | [diff] [blame] | 237 | { |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 238 | return as->caps.is_spi2; |
Haavard Skinnemoen | 5bfa26c | 2009-01-06 14:41:42 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 242 | * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby |
| 243 | * 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] | 244 | * that automagic deselection is OK. ("NPCSx rises if no data is to be |
| 245 | * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer |
| 246 | * controllers have CSAAT and friends. |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 247 | * |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 248 | * Since the CSAAT functionality is a bit weird on newer controllers as |
| 249 | * well, we use GPIO to control nCSx pins on all controllers, updating |
| 250 | * MR.PCS to avoid confusing the controller. Using GPIOs also lets us |
| 251 | * support active-high chipselects despite the controller's belief that |
| 252 | * only active-low devices/systems exists. |
| 253 | * |
| 254 | * However, at91rm9200 has a second erratum whereby nCS0 doesn't work |
| 255 | * right when driven with GPIO. ("Mode Fault does not allow more than one |
| 256 | * Master on Chip Select 0.") No workaround exists for that ... so for |
| 257 | * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH, |
| 258 | * and (c) will trigger that first erratum in some cases. |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 259 | */ |
| 260 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 261 | static void cs_activate(struct atmel_spi *as, struct spi_device *spi) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 262 | { |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 263 | struct atmel_spi_device *asd = spi->controller_state; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 264 | unsigned active = spi->mode & SPI_CS_HIGH; |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 265 | u32 mr; |
Atsushi Nemoto | f6febcc | 2008-02-23 15:23:39 -0800 | [diff] [blame] | 266 | |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 267 | if (atmel_spi_is_v2(as)) { |
Wenyou Yang | 97ed465 | 2013-03-19 15:43:01 +0800 | [diff] [blame] | 268 | spi_writel(as, CSR0 + 4 * spi->chip_select, asd->csr); |
| 269 | /* For the low SPI version, there is a issue that PDC transfer |
| 270 | * 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] | 271 | */ |
| 272 | spi_writel(as, CSR0, asd->csr); |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 273 | if (as->caps.has_wdrbt) { |
Wenyou Yang | 97ed465 | 2013-03-19 15:43:01 +0800 | [diff] [blame] | 274 | spi_writel(as, MR, |
| 275 | SPI_BF(PCS, ~(0x01 << spi->chip_select)) |
| 276 | | SPI_BIT(WDRBT) |
| 277 | | SPI_BIT(MODFDIS) |
| 278 | | SPI_BIT(MSTR)); |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 279 | } else { |
Wenyou Yang | 97ed465 | 2013-03-19 15:43:01 +0800 | [diff] [blame] | 280 | spi_writel(as, MR, |
| 281 | SPI_BF(PCS, ~(0x01 << spi->chip_select)) |
| 282 | | SPI_BIT(MODFDIS) |
| 283 | | SPI_BIT(MSTR)); |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 284 | } |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 285 | mr = spi_readl(as, MR); |
| 286 | gpio_set_value(asd->npcs_pin, active); |
| 287 | } else { |
| 288 | u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0; |
| 289 | int i; |
| 290 | u32 csr; |
| 291 | |
| 292 | /* Make sure clock polarity is correct */ |
| 293 | for (i = 0; i < spi->master->num_chipselect; i++) { |
| 294 | csr = spi_readl(as, CSR0 + 4 * i); |
| 295 | if ((csr ^ cpol) & SPI_BIT(CPOL)) |
| 296 | spi_writel(as, CSR0 + 4 * i, |
| 297 | csr ^ SPI_BIT(CPOL)); |
| 298 | } |
| 299 | |
| 300 | mr = spi_readl(as, MR); |
| 301 | mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr); |
| 302 | if (spi->chip_select != 0) |
| 303 | gpio_set_value(asd->npcs_pin, active); |
| 304 | spi_writel(as, MR, mr); |
Atsushi Nemoto | f6febcc | 2008-02-23 15:23:39 -0800 | [diff] [blame] | 305 | } |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 306 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 307 | dev_dbg(&spi->dev, "activate %u%s, mr %08x\n", |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 308 | asd->npcs_pin, active ? " (high)" : "", |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 309 | mr); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 310 | } |
| 311 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 312 | static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 313 | { |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 314 | struct atmel_spi_device *asd = spi->controller_state; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 315 | unsigned active = spi->mode & SPI_CS_HIGH; |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 316 | u32 mr; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 317 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 318 | /* only deactivate *this* device; sometimes transfers to |
| 319 | * another device may be active when this routine is called. |
| 320 | */ |
| 321 | mr = spi_readl(as, MR); |
| 322 | if (~SPI_BFEXT(PCS, mr) & (1 << spi->chip_select)) { |
| 323 | mr = SPI_BFINS(PCS, 0xf, mr); |
| 324 | spi_writel(as, MR, mr); |
| 325 | } |
| 326 | |
| 327 | dev_dbg(&spi->dev, "DEactivate %u%s, mr %08x\n", |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 328 | asd->npcs_pin, active ? " (low)" : "", |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 329 | mr); |
| 330 | |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 331 | if (atmel_spi_is_v2(as) || spi->chip_select != 0) |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 332 | gpio_set_value(asd->npcs_pin, !active); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 335 | static inline int atmel_spi_xfer_is_last(struct spi_message *msg, |
| 336 | struct spi_transfer *xfer) |
| 337 | { |
| 338 | return msg->transfers.prev == &xfer->transfer_list; |
| 339 | } |
| 340 | |
| 341 | static inline int atmel_spi_xfer_can_be_chained(struct spi_transfer *xfer) |
| 342 | { |
| 343 | return xfer->delay_usecs == 0 && !xfer->cs_change; |
| 344 | } |
| 345 | |
| 346 | static void atmel_spi_next_xfer_data(struct spi_master *master, |
| 347 | struct spi_transfer *xfer, |
| 348 | dma_addr_t *tx_dma, |
| 349 | dma_addr_t *rx_dma, |
| 350 | u32 *plen) |
| 351 | { |
| 352 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 353 | u32 len = *plen; |
| 354 | |
| 355 | /* use scratch buffer only when rx or tx data is unspecified */ |
| 356 | if (xfer->rx_buf) |
Ben Nizette | 6aed4ee | 2009-12-14 22:20:20 -0800 | [diff] [blame] | 357 | *rx_dma = xfer->rx_dma + xfer->len - *plen; |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 358 | else { |
| 359 | *rx_dma = as->buffer_dma; |
| 360 | if (len > BUFFER_SIZE) |
| 361 | len = BUFFER_SIZE; |
| 362 | } |
| 363 | if (xfer->tx_buf) |
Ben Nizette | 6aed4ee | 2009-12-14 22:20:20 -0800 | [diff] [blame] | 364 | *tx_dma = xfer->tx_dma + xfer->len - *plen; |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 365 | else { |
| 366 | *tx_dma = as->buffer_dma; |
| 367 | if (len > BUFFER_SIZE) |
| 368 | len = BUFFER_SIZE; |
| 369 | memset(as->buffer, 0, len); |
| 370 | dma_sync_single_for_device(&as->pdev->dev, |
| 371 | as->buffer_dma, len, DMA_TO_DEVICE); |
| 372 | } |
| 373 | |
| 374 | *plen = len; |
| 375 | } |
| 376 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 377 | /* |
| 378 | * Submit next transfer for DMA. |
| 379 | * lock is held, spi irq is blocked |
| 380 | */ |
| 381 | static void atmel_spi_next_xfer(struct spi_master *master, |
| 382 | struct spi_message *msg) |
| 383 | { |
| 384 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 385 | struct spi_transfer *xfer; |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 386 | u32 len, remaining; |
| 387 | u32 ieval; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 388 | dma_addr_t tx_dma, rx_dma; |
| 389 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 390 | if (!as->current_transfer) |
| 391 | xfer = list_entry(msg->transfers.next, |
| 392 | struct spi_transfer, transfer_list); |
| 393 | else if (!as->next_transfer) |
| 394 | xfer = list_entry(as->current_transfer->transfer_list.next, |
| 395 | struct spi_transfer, transfer_list); |
| 396 | else |
| 397 | xfer = NULL; |
| 398 | |
| 399 | if (xfer) { |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 400 | spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS)); |
| 401 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 402 | len = xfer->len; |
| 403 | atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len); |
| 404 | remaining = xfer->len - len; |
| 405 | |
| 406 | spi_writel(as, RPR, rx_dma); |
| 407 | spi_writel(as, TPR, tx_dma); |
| 408 | |
| 409 | if (msg->spi->bits_per_word > 8) |
| 410 | len >>= 1; |
| 411 | spi_writel(as, RCR, len); |
| 412 | spi_writel(as, TCR, len); |
Haavard Skinnemoen | 8bacb21 | 2008-02-06 01:38:13 -0800 | [diff] [blame] | 413 | |
| 414 | dev_dbg(&msg->spi->dev, |
| 415 | " start xfer %p: len %u tx %p/%08x rx %p/%08x\n", |
| 416 | xfer, xfer->len, xfer->tx_buf, xfer->tx_dma, |
| 417 | xfer->rx_buf, xfer->rx_dma); |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 418 | } else { |
| 419 | xfer = as->next_transfer; |
| 420 | remaining = as->next_remaining_bytes; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 421 | } |
| 422 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 423 | as->current_transfer = xfer; |
| 424 | as->current_remaining_bytes = remaining; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 425 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 426 | if (remaining > 0) |
| 427 | len = remaining; |
Haavard Skinnemoen | 8bacb21 | 2008-02-06 01:38:13 -0800 | [diff] [blame] | 428 | else if (!atmel_spi_xfer_is_last(msg, xfer) |
| 429 | && atmel_spi_xfer_can_be_chained(xfer)) { |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 430 | xfer = list_entry(xfer->transfer_list.next, |
| 431 | struct spi_transfer, transfer_list); |
| 432 | len = xfer->len; |
| 433 | } else |
| 434 | xfer = NULL; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 435 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 436 | as->next_transfer = xfer; |
| 437 | |
| 438 | if (xfer) { |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 439 | u32 total; |
| 440 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 441 | total = len; |
| 442 | atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len); |
| 443 | as->next_remaining_bytes = total - len; |
| 444 | |
| 445 | spi_writel(as, RNPR, rx_dma); |
| 446 | spi_writel(as, TNPR, tx_dma); |
| 447 | |
| 448 | if (msg->spi->bits_per_word > 8) |
| 449 | len >>= 1; |
| 450 | spi_writel(as, RNCR, len); |
| 451 | spi_writel(as, TNCR, len); |
Haavard Skinnemoen | 8bacb21 | 2008-02-06 01:38:13 -0800 | [diff] [blame] | 452 | |
| 453 | dev_dbg(&msg->spi->dev, |
| 454 | " next xfer %p: len %u tx %p/%08x rx %p/%08x\n", |
| 455 | xfer, xfer->len, xfer->tx_buf, xfer->tx_dma, |
| 456 | xfer->rx_buf, xfer->rx_dma); |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 457 | ieval = SPI_BIT(ENDRX) | SPI_BIT(OVRES); |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 458 | } else { |
| 459 | spi_writel(as, RNCR, 0); |
| 460 | spi_writel(as, TNCR, 0); |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 461 | ieval = SPI_BIT(RXBUFF) | SPI_BIT(ENDRX) | SPI_BIT(OVRES); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 462 | } |
| 463 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 464 | /* REVISIT: We're waiting for ENDRX before we start the next |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 465 | * transfer because we need to handle some difficult timing |
| 466 | * issues otherwise. If we wait for ENDTX in one transfer and |
| 467 | * then starts waiting for ENDRX in the next, it's difficult |
| 468 | * to tell the difference between the ENDRX interrupt we're |
| 469 | * actually waiting for and the ENDRX interrupt of the |
| 470 | * previous transfer. |
| 471 | * |
| 472 | * It should be doable, though. Just not now... |
| 473 | */ |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 474 | spi_writel(as, IER, ieval); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 475 | spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN)); |
| 476 | } |
| 477 | |
| 478 | static void atmel_spi_next_message(struct spi_master *master) |
| 479 | { |
| 480 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 481 | struct spi_message *msg; |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 482 | struct spi_device *spi; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 483 | |
| 484 | BUG_ON(as->current_transfer); |
| 485 | |
| 486 | msg = list_entry(as->queue.next, struct spi_message, queue); |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 487 | spi = msg->spi; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 488 | |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 489 | dev_dbg(master->dev.parent, "start message %p for %s\n", |
Kay Sievers | 6c7377a | 2009-03-24 16:38:21 -0700 | [diff] [blame] | 490 | msg, dev_name(&spi->dev)); |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 491 | |
| 492 | /* select chip if it's not still active */ |
| 493 | if (as->stay) { |
| 494 | if (as->stay != spi) { |
| 495 | cs_deactivate(as, as->stay); |
| 496 | cs_activate(as, spi); |
| 497 | } |
| 498 | as->stay = NULL; |
| 499 | } else |
| 500 | cs_activate(as, spi); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 501 | |
| 502 | atmel_spi_next_xfer(master, msg); |
| 503 | } |
| 504 | |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 505 | /* |
| 506 | * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma: |
| 507 | * - The buffer is either valid for CPU access, else NULL |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 508 | * - If the buffer is valid, so is its DMA address |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 509 | * |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 510 | * This driver manages the dma address unless message->is_dma_mapped. |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 511 | */ |
| 512 | static int |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 513 | atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer) |
| 514 | { |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 515 | struct device *dev = &as->pdev->dev; |
| 516 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 517 | xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS; |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 518 | if (xfer->tx_buf) { |
Jean-Christophe PLAGNIOL-VILLARD | 214b574 | 2010-11-20 14:52:53 +0800 | [diff] [blame] | 519 | /* tx_buf is a const void* where we need a void * for the dma |
| 520 | * mapping */ |
| 521 | void *nonconst_tx = (void *)xfer->tx_buf; |
| 522 | |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 523 | xfer->tx_dma = dma_map_single(dev, |
Jean-Christophe PLAGNIOL-VILLARD | 214b574 | 2010-11-20 14:52:53 +0800 | [diff] [blame] | 524 | nonconst_tx, xfer->len, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 525 | DMA_TO_DEVICE); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 526 | if (dma_mapping_error(dev, xfer->tx_dma)) |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 527 | return -ENOMEM; |
| 528 | } |
| 529 | if (xfer->rx_buf) { |
| 530 | xfer->rx_dma = dma_map_single(dev, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 531 | xfer->rx_buf, xfer->len, |
| 532 | DMA_FROM_DEVICE); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 533 | if (dma_mapping_error(dev, xfer->rx_dma)) { |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 534 | if (xfer->tx_buf) |
| 535 | dma_unmap_single(dev, |
| 536 | xfer->tx_dma, xfer->len, |
| 537 | DMA_TO_DEVICE); |
| 538 | return -ENOMEM; |
| 539 | } |
| 540 | } |
| 541 | return 0; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | static void atmel_spi_dma_unmap_xfer(struct spi_master *master, |
| 545 | struct spi_transfer *xfer) |
| 546 | { |
| 547 | if (xfer->tx_dma != INVALID_DMA_ADDRESS) |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 548 | dma_unmap_single(master->dev.parent, xfer->tx_dma, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 549 | xfer->len, DMA_TO_DEVICE); |
| 550 | if (xfer->rx_dma != INVALID_DMA_ADDRESS) |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 551 | dma_unmap_single(master->dev.parent, xfer->rx_dma, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 552 | xfer->len, DMA_FROM_DEVICE); |
| 553 | } |
| 554 | |
| 555 | static void |
| 556 | atmel_spi_msg_done(struct spi_master *master, struct atmel_spi *as, |
Nicolas Ferre | 823cd04 | 2013-03-19 15:45:01 +0800 | [diff] [blame] | 557 | struct spi_message *msg, int stay) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 558 | { |
Nicolas Ferre | 823cd04 | 2013-03-19 15:45:01 +0800 | [diff] [blame] | 559 | if (!stay || as->done_status < 0) |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 560 | cs_deactivate(as, msg->spi); |
| 561 | else |
| 562 | as->stay = msg->spi; |
| 563 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 564 | list_del(&msg->queue); |
Nicolas Ferre | 823cd04 | 2013-03-19 15:45:01 +0800 | [diff] [blame] | 565 | msg->status = as->done_status; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 566 | |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 567 | dev_dbg(master->dev.parent, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 568 | "xfer complete: %u bytes transferred\n", |
| 569 | msg->actual_length); |
| 570 | |
| 571 | spin_unlock(&as->lock); |
| 572 | msg->complete(msg->context); |
| 573 | spin_lock(&as->lock); |
| 574 | |
| 575 | as->current_transfer = NULL; |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 576 | as->next_transfer = NULL; |
Nicolas Ferre | 823cd04 | 2013-03-19 15:45:01 +0800 | [diff] [blame] | 577 | as->done_status = 0; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 578 | |
| 579 | /* continue if needed */ |
| 580 | if (list_empty(&as->queue) || as->stopping) |
| 581 | spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS)); |
| 582 | else |
| 583 | atmel_spi_next_message(master); |
| 584 | } |
| 585 | |
| 586 | static irqreturn_t |
| 587 | atmel_spi_interrupt(int irq, void *dev_id) |
| 588 | { |
| 589 | struct spi_master *master = dev_id; |
| 590 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 591 | struct spi_message *msg; |
| 592 | struct spi_transfer *xfer; |
| 593 | u32 status, pending, imr; |
| 594 | int ret = IRQ_NONE; |
| 595 | |
| 596 | spin_lock(&as->lock); |
| 597 | |
| 598 | xfer = as->current_transfer; |
| 599 | msg = list_entry(as->queue.next, struct spi_message, queue); |
| 600 | |
| 601 | imr = spi_readl(as, IMR); |
| 602 | status = spi_readl(as, SR); |
| 603 | pending = status & imr; |
| 604 | |
| 605 | if (pending & SPI_BIT(OVRES)) { |
| 606 | int timeout; |
| 607 | |
| 608 | ret = IRQ_HANDLED; |
| 609 | |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 610 | spi_writel(as, IDR, (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 611 | | SPI_BIT(OVRES))); |
| 612 | |
| 613 | /* |
| 614 | * When we get an overrun, we disregard the current |
| 615 | * transfer. Data will not be copied back from any |
| 616 | * bounce buffer and msg->actual_len will not be |
| 617 | * updated with the last xfer. |
| 618 | * |
| 619 | * We will also not process any remaning transfers in |
| 620 | * the message. |
| 621 | * |
| 622 | * First, stop the transfer and unmap the DMA buffers. |
| 623 | */ |
| 624 | spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS)); |
| 625 | if (!msg->is_dma_mapped) |
| 626 | atmel_spi_dma_unmap_xfer(master, xfer); |
| 627 | |
| 628 | /* REVISIT: udelay in irq is unfriendly */ |
| 629 | if (xfer->delay_usecs) |
| 630 | udelay(xfer->delay_usecs); |
| 631 | |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 632 | dev_warn(master->dev.parent, "overrun (%u/%u remaining)\n", |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 633 | spi_readl(as, TCR), spi_readl(as, RCR)); |
| 634 | |
| 635 | /* |
| 636 | * Clean up DMA registers and make sure the data |
| 637 | * registers are empty. |
| 638 | */ |
| 639 | spi_writel(as, RNCR, 0); |
| 640 | spi_writel(as, TNCR, 0); |
| 641 | spi_writel(as, RCR, 0); |
| 642 | spi_writel(as, TCR, 0); |
| 643 | for (timeout = 1000; timeout; timeout--) |
| 644 | if (spi_readl(as, SR) & SPI_BIT(TXEMPTY)) |
| 645 | break; |
| 646 | if (!timeout) |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 647 | dev_warn(master->dev.parent, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 648 | "timeout waiting for TXEMPTY"); |
| 649 | while (spi_readl(as, SR) & SPI_BIT(RDRF)) |
| 650 | spi_readl(as, RDR); |
| 651 | |
| 652 | /* Clear any overrun happening while cleaning up */ |
| 653 | spi_readl(as, SR); |
| 654 | |
Nicolas Ferre | 823cd04 | 2013-03-19 15:45:01 +0800 | [diff] [blame] | 655 | as->done_status = -EIO; |
| 656 | atmel_spi_msg_done(master, as, msg, 0); |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 657 | } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) { |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 658 | ret = IRQ_HANDLED; |
| 659 | |
| 660 | spi_writel(as, IDR, pending); |
| 661 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 662 | if (as->current_remaining_bytes == 0) { |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 663 | msg->actual_length += xfer->len; |
| 664 | |
| 665 | if (!msg->is_dma_mapped) |
| 666 | atmel_spi_dma_unmap_xfer(master, xfer); |
| 667 | |
| 668 | /* REVISIT: udelay in irq is unfriendly */ |
| 669 | if (xfer->delay_usecs) |
| 670 | udelay(xfer->delay_usecs); |
| 671 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 672 | if (atmel_spi_xfer_is_last(msg, xfer)) { |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 673 | /* report completed message */ |
Nicolas Ferre | 823cd04 | 2013-03-19 15:45:01 +0800 | [diff] [blame] | 674 | atmel_spi_msg_done(master, as, msg, |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 675 | xfer->cs_change); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 676 | } else { |
| 677 | if (xfer->cs_change) { |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 678 | cs_deactivate(as, msg->spi); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 679 | udelay(1); |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 680 | cs_activate(as, msg->spi); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* |
| 684 | * Not done yet. Submit the next transfer. |
| 685 | * |
| 686 | * FIXME handle protocol options for xfer |
| 687 | */ |
| 688 | atmel_spi_next_xfer(master, msg); |
| 689 | } |
| 690 | } else { |
| 691 | /* |
| 692 | * Keep going, we still have data to send in |
| 693 | * the current transfer. |
| 694 | */ |
| 695 | atmel_spi_next_xfer(master, msg); |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | spin_unlock(&as->lock); |
| 700 | |
| 701 | return ret; |
| 702 | } |
| 703 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 704 | static int atmel_spi_setup(struct spi_device *spi) |
| 705 | { |
| 706 | struct atmel_spi *as; |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 707 | struct atmel_spi_device *asd; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 708 | u32 scbr, csr; |
| 709 | unsigned int bits = spi->bits_per_word; |
Haavard Skinnemoen | 592e7bf | 2008-04-30 00:52:17 -0700 | [diff] [blame] | 710 | unsigned long bus_hz; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 711 | unsigned int npcs_pin; |
| 712 | int ret; |
| 713 | |
| 714 | as = spi_master_get_devdata(spi->master); |
| 715 | |
| 716 | if (as->stopping) |
| 717 | return -ESHUTDOWN; |
| 718 | |
| 719 | if (spi->chip_select > spi->master->num_chipselect) { |
| 720 | dev_dbg(&spi->dev, |
| 721 | "setup: invalid chipselect %u (%u defined)\n", |
| 722 | spi->chip_select, spi->master->num_chipselect); |
| 723 | return -EINVAL; |
| 724 | } |
| 725 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 726 | if (bits < 8 || bits > 16) { |
| 727 | dev_dbg(&spi->dev, |
| 728 | "setup: invalid bits_per_word %u (8 to 16)\n", |
| 729 | bits); |
| 730 | return -EINVAL; |
| 731 | } |
| 732 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 733 | /* see notes above re chipselect */ |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 734 | if (!atmel_spi_is_v2(as) |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 735 | && spi->chip_select == 0 |
| 736 | && (spi->mode & SPI_CS_HIGH)) { |
| 737 | dev_dbg(&spi->dev, "setup: can't be active-high\n"); |
| 738 | return -EINVAL; |
| 739 | } |
| 740 | |
Haavard Skinnemoen | 5bfa26c | 2009-01-06 14:41:42 -0800 | [diff] [blame] | 741 | /* v1 chips start out at half the peripheral bus speed. */ |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 742 | bus_hz = clk_get_rate(as->clk); |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 743 | if (!atmel_spi_is_v2(as)) |
Haavard Skinnemoen | 592e7bf | 2008-04-30 00:52:17 -0700 | [diff] [blame] | 744 | bus_hz /= 2; |
| 745 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 746 | if (spi->max_speed_hz) { |
Haavard Skinnemoen | 592e7bf | 2008-04-30 00:52:17 -0700 | [diff] [blame] | 747 | /* |
| 748 | * Calculate the lowest divider that satisfies the |
| 749 | * constraint, assuming div32/fdiv/mbz == 0. |
| 750 | */ |
| 751 | scbr = DIV_ROUND_UP(bus_hz, spi->max_speed_hz); |
| 752 | |
| 753 | /* |
| 754 | * If the resulting divider doesn't fit into the |
| 755 | * register bitfield, we can't satisfy the constraint. |
| 756 | */ |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 757 | if (scbr >= (1 << SPI_SCBR_SIZE)) { |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 758 | dev_dbg(&spi->dev, |
| 759 | "setup: %d Hz too slow, scbr %u; min %ld Hz\n", |
| 760 | spi->max_speed_hz, scbr, bus_hz/255); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 761 | return -EINVAL; |
| 762 | } |
| 763 | } else |
Haavard Skinnemoen | 592e7bf | 2008-04-30 00:52:17 -0700 | [diff] [blame] | 764 | /* speed zero means "as slow as possible" */ |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 765 | scbr = 0xff; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 766 | |
| 767 | csr = SPI_BF(SCBR, scbr) | SPI_BF(BITS, bits - 8); |
| 768 | if (spi->mode & SPI_CPOL) |
| 769 | csr |= SPI_BIT(CPOL); |
| 770 | if (!(spi->mode & SPI_CPHA)) |
| 771 | csr |= SPI_BIT(NCPHA); |
| 772 | |
Haavard Skinnemoen | 1eed29d | 2008-02-06 01:38:11 -0800 | [diff] [blame] | 773 | /* DLYBS is mostly irrelevant since we manage chipselect using GPIOs. |
| 774 | * |
| 775 | * DLYBCT would add delays between words, slowing down transfers. |
| 776 | * It could potentially be useful to cope with DMA bottlenecks, but |
| 777 | * in those cases it's probably best to just use a lower bitrate. |
| 778 | */ |
| 779 | csr |= SPI_BF(DLYBS, 0); |
| 780 | csr |= SPI_BF(DLYBCT, 0); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 781 | |
| 782 | /* chipselect must have been muxed as GPIO (e.g. in board setup) */ |
| 783 | npcs_pin = (unsigned int)spi->controller_data; |
Jean-Christophe PLAGNIOL-VILLARD | 850a5b6 | 2012-11-23 13:44:39 +0100 | [diff] [blame] | 784 | |
| 785 | if (gpio_is_valid(spi->cs_gpio)) |
| 786 | npcs_pin = spi->cs_gpio; |
| 787 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 788 | asd = spi->controller_state; |
| 789 | if (!asd) { |
| 790 | asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL); |
| 791 | if (!asd) |
| 792 | return -ENOMEM; |
| 793 | |
Kay Sievers | 6c7377a | 2009-03-24 16:38:21 -0700 | [diff] [blame] | 794 | ret = gpio_request(npcs_pin, dev_name(&spi->dev)); |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 795 | if (ret) { |
| 796 | kfree(asd); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 797 | return ret; |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | asd->npcs_pin = npcs_pin; |
| 801 | spi->controller_state = asd; |
David Brownell | 28735a7 | 2007-03-16 13:38:14 -0800 | [diff] [blame] | 802 | gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH)); |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 803 | } else { |
| 804 | unsigned long flags; |
| 805 | |
| 806 | spin_lock_irqsave(&as->lock, flags); |
| 807 | if (as->stay == spi) |
| 808 | as->stay = NULL; |
| 809 | cs_deactivate(as, spi); |
| 810 | spin_unlock_irqrestore(&as->lock, flags); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 811 | } |
| 812 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 813 | asd->csr = csr; |
| 814 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 815 | dev_dbg(&spi->dev, |
| 816 | "setup: %lu Hz bpw %u mode 0x%x -> csr%d %08x\n", |
Haavard Skinnemoen | 592e7bf | 2008-04-30 00:52:17 -0700 | [diff] [blame] | 817 | bus_hz / scbr, bits, spi->mode, spi->chip_select, csr); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 818 | |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 819 | if (!atmel_spi_is_v2(as)) |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 820 | spi_writel(as, CSR0 + 4 * spi->chip_select, csr); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 821 | |
| 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | static int atmel_spi_transfer(struct spi_device *spi, struct spi_message *msg) |
| 826 | { |
| 827 | struct atmel_spi *as; |
| 828 | struct spi_transfer *xfer; |
| 829 | unsigned long flags; |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 830 | struct device *controller = spi->master->dev.parent; |
Matthias Brugger | b9d228f | 2010-10-13 17:51:02 +0200 | [diff] [blame] | 831 | u8 bits; |
| 832 | struct atmel_spi_device *asd; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 833 | |
| 834 | as = spi_master_get_devdata(spi->master); |
| 835 | |
| 836 | dev_dbg(controller, "new message %p submitted for %s\n", |
Kay Sievers | 6c7377a | 2009-03-24 16:38:21 -0700 | [diff] [blame] | 837 | msg, dev_name(&spi->dev)); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 838 | |
Stanislaw Gruszka | 5b96f17 | 2009-01-15 13:50:44 -0800 | [diff] [blame] | 839 | if (unlikely(list_empty(&msg->transfers))) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 840 | return -EINVAL; |
| 841 | |
| 842 | if (as->stopping) |
| 843 | return -ESHUTDOWN; |
| 844 | |
| 845 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
Atsushi Nemoto | 0671981 | 2008-04-28 02:14:19 -0700 | [diff] [blame] | 846 | if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) { |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 847 | dev_dbg(&spi->dev, "missing rx or tx buf\n"); |
| 848 | return -EINVAL; |
| 849 | } |
| 850 | |
Matthias Brugger | b9d228f | 2010-10-13 17:51:02 +0200 | [diff] [blame] | 851 | if (xfer->bits_per_word) { |
| 852 | asd = spi->controller_state; |
| 853 | bits = (asd->csr >> 4) & 0xf; |
| 854 | if (bits != xfer->bits_per_word - 8) { |
| 855 | dev_dbg(&spi->dev, "you can't yet change " |
Matthias Brugger | ee2007d | 2010-10-16 01:39:49 +0200 | [diff] [blame] | 856 | "bits_per_word in transfers\n"); |
Matthias Brugger | b9d228f | 2010-10-13 17:51:02 +0200 | [diff] [blame] | 857 | return -ENOPROTOOPT; |
| 858 | } |
| 859 | } |
| 860 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 861 | /* FIXME implement these protocol options!! */ |
Matthias Brugger | b9d228f | 2010-10-13 17:51:02 +0200 | [diff] [blame] | 862 | if (xfer->speed_hz) { |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 863 | dev_dbg(&spi->dev, "no protocol options yet\n"); |
| 864 | return -ENOPROTOOPT; |
| 865 | } |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 866 | |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 867 | /* |
| 868 | * DMA map early, for performance (empties dcache ASAP) and |
| 869 | * better fault reporting. This is a DMA-only driver. |
| 870 | * |
| 871 | * NOTE that if dma_unmap_single() ever starts to do work on |
| 872 | * platforms supported by this driver, we would need to clean |
| 873 | * up mappings for previously-mapped transfers. |
| 874 | */ |
| 875 | if (!msg->is_dma_mapped) { |
| 876 | if (atmel_spi_dma_map_xfer(as, xfer) < 0) |
| 877 | return -ENOMEM; |
| 878 | } |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 879 | } |
| 880 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 881 | #ifdef VERBOSE |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 882 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 883 | dev_dbg(controller, |
| 884 | " xfer %p: len %u tx %p/%08x rx %p/%08x\n", |
| 885 | xfer, xfer->len, |
| 886 | xfer->tx_buf, xfer->tx_dma, |
| 887 | xfer->rx_buf, xfer->rx_dma); |
| 888 | } |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 889 | #endif |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 890 | |
| 891 | msg->status = -EINPROGRESS; |
| 892 | msg->actual_length = 0; |
| 893 | |
| 894 | spin_lock_irqsave(&as->lock, flags); |
| 895 | list_add_tail(&msg->queue, &as->queue); |
| 896 | if (!as->current_transfer) |
| 897 | atmel_spi_next_message(spi->master); |
| 898 | spin_unlock_irqrestore(&as->lock, flags); |
| 899 | |
| 900 | return 0; |
| 901 | } |
| 902 | |
David Brownell | bb2d1c3 | 2007-02-20 13:58:19 -0800 | [diff] [blame] | 903 | static void atmel_spi_cleanup(struct spi_device *spi) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 904 | { |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 905 | struct atmel_spi *as = spi_master_get_devdata(spi->master); |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 906 | struct atmel_spi_device *asd = spi->controller_state; |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 907 | unsigned gpio = (unsigned) spi->controller_data; |
| 908 | unsigned long flags; |
| 909 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 910 | if (!asd) |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 911 | return; |
| 912 | |
| 913 | spin_lock_irqsave(&as->lock, flags); |
| 914 | if (as->stay == spi) { |
| 915 | as->stay = NULL; |
| 916 | cs_deactivate(as, spi); |
| 917 | } |
| 918 | spin_unlock_irqrestore(&as->lock, flags); |
| 919 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 920 | spi->controller_state = NULL; |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 921 | gpio_free(gpio); |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 922 | kfree(asd); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 923 | } |
| 924 | |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 925 | static inline unsigned int atmel_get_version(struct atmel_spi *as) |
| 926 | { |
| 927 | return spi_readl(as, VERSION) & 0x00000fff; |
| 928 | } |
| 929 | |
| 930 | static void atmel_get_caps(struct atmel_spi *as) |
| 931 | { |
| 932 | unsigned int version; |
| 933 | |
| 934 | version = atmel_get_version(as); |
| 935 | dev_info(&as->pdev->dev, "version: 0x%x\n", version); |
| 936 | |
| 937 | as->caps.is_spi2 = version > 0x121; |
| 938 | as->caps.has_wdrbt = version >= 0x210; |
| 939 | as->caps.has_dma_support = version >= 0x212; |
| 940 | } |
| 941 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 942 | /*-------------------------------------------------------------------------*/ |
| 943 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 944 | static int atmel_spi_probe(struct platform_device *pdev) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 945 | { |
| 946 | struct resource *regs; |
| 947 | int irq; |
| 948 | struct clk *clk; |
| 949 | int ret; |
| 950 | struct spi_master *master; |
| 951 | struct atmel_spi *as; |
| 952 | |
| 953 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 954 | if (!regs) |
| 955 | return -ENXIO; |
| 956 | |
| 957 | irq = platform_get_irq(pdev, 0); |
| 958 | if (irq < 0) |
| 959 | return irq; |
| 960 | |
| 961 | clk = clk_get(&pdev->dev, "spi_clk"); |
| 962 | if (IS_ERR(clk)) |
| 963 | return PTR_ERR(clk); |
| 964 | |
| 965 | /* setup spi core then atmel-specific driver state */ |
| 966 | ret = -ENOMEM; |
| 967 | master = spi_alloc_master(&pdev->dev, sizeof *as); |
| 968 | if (!master) |
| 969 | goto out_free; |
| 970 | |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 971 | /* the spi->mode bits understood by this driver: */ |
| 972 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; |
| 973 | |
Jean-Christophe PLAGNIOL-VILLARD | 850a5b6 | 2012-11-23 13:44:39 +0100 | [diff] [blame] | 974 | master->dev.of_node = pdev->dev.of_node; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 975 | master->bus_num = pdev->id; |
Jean-Christophe PLAGNIOL-VILLARD | 850a5b6 | 2012-11-23 13:44:39 +0100 | [diff] [blame] | 976 | master->num_chipselect = master->dev.of_node ? 0 : 4; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 977 | master->setup = atmel_spi_setup; |
| 978 | master->transfer = atmel_spi_transfer; |
| 979 | master->cleanup = atmel_spi_cleanup; |
| 980 | platform_set_drvdata(pdev, master); |
| 981 | |
| 982 | as = spi_master_get_devdata(master); |
| 983 | |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 984 | /* |
| 985 | * Scratch buffer is used for throwaway rx and tx data. |
| 986 | * It's coherent to minimize dcache pollution. |
| 987 | */ |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 988 | as->buffer = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE, |
| 989 | &as->buffer_dma, GFP_KERNEL); |
| 990 | if (!as->buffer) |
| 991 | goto out_free; |
| 992 | |
| 993 | spin_lock_init(&as->lock); |
| 994 | INIT_LIST_HEAD(&as->queue); |
| 995 | as->pdev = pdev; |
hartleys | 905aa0a | 2009-12-14 22:22:25 +0000 | [diff] [blame] | 996 | as->regs = ioremap(regs->start, resource_size(regs)); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 997 | if (!as->regs) |
| 998 | goto out_free_buffer; |
| 999 | as->irq = irq; |
| 1000 | as->clk = clk; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1001 | |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 1002 | atmel_get_caps(as); |
| 1003 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1004 | ret = request_irq(irq, atmel_spi_interrupt, 0, |
Kay Sievers | 6c7377a | 2009-03-24 16:38:21 -0700 | [diff] [blame] | 1005 | dev_name(&pdev->dev), master); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1006 | if (ret) |
| 1007 | goto out_unmap_regs; |
| 1008 | |
| 1009 | /* Initialize the hardware */ |
| 1010 | clk_enable(clk); |
| 1011 | spi_writel(as, CR, SPI_BIT(SWRST)); |
Jean-Christophe Lallemand | 50d7d5b | 2008-11-12 13:27:00 -0800 | [diff] [blame] | 1012 | spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */ |
Wenyou Yang | d4820b7 | 2013-03-19 15:42:15 +0800 | [diff] [blame] | 1013 | if (as->caps.has_wdrbt) { |
| 1014 | spi_writel(as, MR, SPI_BIT(WDRBT) | SPI_BIT(MODFDIS) |
| 1015 | | SPI_BIT(MSTR)); |
| 1016 | } else { |
| 1017 | spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS)); |
| 1018 | } |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1019 | spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS)); |
| 1020 | spi_writel(as, CR, SPI_BIT(SPIEN)); |
| 1021 | |
| 1022 | /* go! */ |
| 1023 | dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n", |
| 1024 | (unsigned long)regs->start, irq); |
| 1025 | |
| 1026 | ret = spi_register_master(master); |
| 1027 | if (ret) |
| 1028 | goto out_reset_hw; |
| 1029 | |
| 1030 | return 0; |
| 1031 | |
| 1032 | out_reset_hw: |
| 1033 | spi_writel(as, CR, SPI_BIT(SWRST)); |
Jean-Christophe Lallemand | 50d7d5b | 2008-11-12 13:27:00 -0800 | [diff] [blame] | 1034 | spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */ |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1035 | clk_disable(clk); |
| 1036 | free_irq(irq, master); |
| 1037 | out_unmap_regs: |
| 1038 | iounmap(as->regs); |
| 1039 | out_free_buffer: |
| 1040 | dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer, |
| 1041 | as->buffer_dma); |
| 1042 | out_free: |
| 1043 | clk_put(clk); |
| 1044 | spi_master_put(master); |
| 1045 | return ret; |
| 1046 | } |
| 1047 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 1048 | static int atmel_spi_remove(struct platform_device *pdev) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1049 | { |
| 1050 | struct spi_master *master = platform_get_drvdata(pdev); |
| 1051 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 1052 | struct spi_message *msg; |
Nicolas Ferre | 1888e8f | 2013-03-19 15:44:22 +0800 | [diff] [blame] | 1053 | struct spi_transfer *xfer; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1054 | |
| 1055 | /* reset the hardware and block queue progress */ |
| 1056 | spin_lock_irq(&as->lock); |
| 1057 | as->stopping = 1; |
| 1058 | spi_writel(as, CR, SPI_BIT(SWRST)); |
Jean-Christophe Lallemand | 50d7d5b | 2008-11-12 13:27:00 -0800 | [diff] [blame] | 1059 | spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */ |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1060 | spi_readl(as, SR); |
| 1061 | spin_unlock_irq(&as->lock); |
| 1062 | |
| 1063 | /* Terminate remaining queued transfers */ |
| 1064 | list_for_each_entry(msg, &as->queue, queue) { |
Nicolas Ferre | 1888e8f | 2013-03-19 15:44:22 +0800 | [diff] [blame] | 1065 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 1066 | if (!msg->is_dma_mapped) |
| 1067 | atmel_spi_dma_unmap_xfer(master, xfer); |
| 1068 | } |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1069 | msg->status = -ESHUTDOWN; |
| 1070 | msg->complete(msg->context); |
| 1071 | } |
| 1072 | |
| 1073 | dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer, |
| 1074 | as->buffer_dma); |
| 1075 | |
| 1076 | clk_disable(as->clk); |
| 1077 | clk_put(as->clk); |
| 1078 | free_irq(as->irq, master); |
| 1079 | iounmap(as->regs); |
| 1080 | |
| 1081 | spi_unregister_master(master); |
| 1082 | |
| 1083 | return 0; |
| 1084 | } |
| 1085 | |
| 1086 | #ifdef CONFIG_PM |
| 1087 | |
| 1088 | static int atmel_spi_suspend(struct platform_device *pdev, pm_message_t mesg) |
| 1089 | { |
| 1090 | struct spi_master *master = platform_get_drvdata(pdev); |
| 1091 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 1092 | |
| 1093 | clk_disable(as->clk); |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | static int atmel_spi_resume(struct platform_device *pdev) |
| 1098 | { |
| 1099 | struct spi_master *master = platform_get_drvdata(pdev); |
| 1100 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 1101 | |
| 1102 | clk_enable(as->clk); |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | #else |
| 1107 | #define atmel_spi_suspend NULL |
| 1108 | #define atmel_spi_resume NULL |
| 1109 | #endif |
| 1110 | |
Jean-Christophe PLAGNIOL-VILLARD | 850a5b6 | 2012-11-23 13:44:39 +0100 | [diff] [blame] | 1111 | #if defined(CONFIG_OF) |
| 1112 | static const struct of_device_id atmel_spi_dt_ids[] = { |
| 1113 | { .compatible = "atmel,at91rm9200-spi" }, |
| 1114 | { /* sentinel */ } |
| 1115 | }; |
| 1116 | |
| 1117 | MODULE_DEVICE_TABLE(of, atmel_spi_dt_ids); |
| 1118 | #endif |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1119 | |
| 1120 | static struct platform_driver atmel_spi_driver = { |
| 1121 | .driver = { |
| 1122 | .name = "atmel_spi", |
| 1123 | .owner = THIS_MODULE, |
Jean-Christophe PLAGNIOL-VILLARD | 850a5b6 | 2012-11-23 13:44:39 +0100 | [diff] [blame] | 1124 | .of_match_table = of_match_ptr(atmel_spi_dt_ids), |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1125 | }, |
| 1126 | .suspend = atmel_spi_suspend, |
| 1127 | .resume = atmel_spi_resume, |
Jean-Christophe PLAGNIOL-VILLARD | 1cb201a | 2011-11-04 01:20:21 +0800 | [diff] [blame] | 1128 | .probe = atmel_spi_probe, |
Grant Likely | 2deff8d | 2013-02-05 13:27:35 +0000 | [diff] [blame] | 1129 | .remove = atmel_spi_remove, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1130 | }; |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 1131 | module_platform_driver(atmel_spi_driver); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1132 | |
| 1133 | MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver"); |
Jean Delvare | e05503e | 2011-05-18 16:49:24 +0200 | [diff] [blame] | 1134 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 1135 | MODULE_LICENSE("GPL"); |
Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 1136 | MODULE_ALIAS("platform:atmel_spi"); |