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> |
| 21 | |
| 22 | #include <asm/io.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 23 | #include <mach/board.h> |
| 24 | #include <mach/gpio.h> |
| 25 | #include <mach/cpu.h> |
David Brownell | bb2d1c3 | 2007-02-20 13:58:19 -0800 | [diff] [blame] | 26 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 27 | #include "atmel_spi.h" |
| 28 | |
| 29 | /* |
| 30 | * The core SPI transfer engine just talks to a register bank to set up |
| 31 | * DMA transfers; transfer queue progress is driven by IRQs. The clock |
| 32 | * framework provides the base clock, subdivided for each spi_device. |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 33 | */ |
| 34 | struct atmel_spi { |
| 35 | spinlock_t lock; |
| 36 | |
| 37 | void __iomem *regs; |
| 38 | int irq; |
| 39 | struct clk *clk; |
| 40 | struct platform_device *pdev; |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 41 | struct spi_device *stay; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 42 | |
| 43 | u8 stopping; |
| 44 | struct list_head queue; |
| 45 | struct spi_transfer *current_transfer; |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 46 | unsigned long current_remaining_bytes; |
| 47 | struct spi_transfer *next_transfer; |
| 48 | unsigned long next_remaining_bytes; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 49 | |
| 50 | void *buffer; |
| 51 | dma_addr_t buffer_dma; |
| 52 | }; |
| 53 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 54 | /* Controller-specific per-slave state */ |
| 55 | struct atmel_spi_device { |
| 56 | unsigned int npcs_pin; |
| 57 | u32 csr; |
| 58 | }; |
| 59 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 60 | #define BUFFER_SIZE PAGE_SIZE |
| 61 | #define INVALID_DMA_ADDRESS 0xffffffff |
| 62 | |
| 63 | /* |
Haavard Skinnemoen | 5bfa26c | 2009-01-06 14:41:42 -0800 | [diff] [blame] | 64 | * Version 2 of the SPI controller has |
| 65 | * - CR.LASTXFER |
| 66 | * - SPI_MR.DIV32 may become FDIV or must-be-zero (here: always zero) |
| 67 | * - SPI_SR.TXEMPTY, SPI_SR.NSSR (and corresponding irqs) |
| 68 | * - SPI_CSRx.CSAAT |
| 69 | * - SPI_CSRx.SBCR allows faster clocking |
| 70 | * |
| 71 | * We can determine the controller version by reading the VERSION |
| 72 | * register, but I haven't checked that it exists on all chips, and |
| 73 | * this is cheaper anyway. |
| 74 | */ |
| 75 | static bool atmel_spi_is_v2(void) |
| 76 | { |
| 77 | return !cpu_is_at91rm9200(); |
| 78 | } |
| 79 | |
| 80 | /* |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 81 | * Earlier SPI controllers (e.g. on at91rm9200) have a design bug whereby |
| 82 | * 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] | 83 | * that automagic deselection is OK. ("NPCSx rises if no data is to be |
| 84 | * transmitted") Not so! Workaround uses nCSx pins as GPIOs; or newer |
| 85 | * controllers have CSAAT and friends. |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 86 | * |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 87 | * Since the CSAAT functionality is a bit weird on newer controllers as |
| 88 | * well, we use GPIO to control nCSx pins on all controllers, updating |
| 89 | * MR.PCS to avoid confusing the controller. Using GPIOs also lets us |
| 90 | * support active-high chipselects despite the controller's belief that |
| 91 | * only active-low devices/systems exists. |
| 92 | * |
| 93 | * However, at91rm9200 has a second erratum whereby nCS0 doesn't work |
| 94 | * right when driven with GPIO. ("Mode Fault does not allow more than one |
| 95 | * Master on Chip Select 0.") No workaround exists for that ... so for |
| 96 | * nCS0 on that chip, we (a) don't use the GPIO, (b) can't support CS_HIGH, |
| 97 | * and (c) will trigger that first erratum in some cases. |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 98 | * |
| 99 | * TODO: Test if the atmel_spi_is_v2() branch below works on |
| 100 | * AT91RM9200 if we use some other register than CSR0. However, don't |
| 101 | * do this unconditionally since AP7000 has an errata where the BITS |
| 102 | * field in CSR0 overrides all other CSRs. |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 103 | */ |
| 104 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 105 | static void cs_activate(struct atmel_spi *as, struct spi_device *spi) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 106 | { |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 107 | struct atmel_spi_device *asd = spi->controller_state; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 108 | unsigned active = spi->mode & SPI_CS_HIGH; |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 109 | u32 mr; |
Atsushi Nemoto | f6febcc | 2008-02-23 15:23:39 -0800 | [diff] [blame] | 110 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 111 | if (atmel_spi_is_v2()) { |
| 112 | /* |
| 113 | * Always use CSR0. This ensures that the clock |
| 114 | * switches to the correct idle polarity before we |
| 115 | * toggle the CS. |
| 116 | */ |
| 117 | spi_writel(as, CSR0, asd->csr); |
| 118 | spi_writel(as, MR, SPI_BF(PCS, 0x0e) | SPI_BIT(MODFDIS) |
| 119 | | SPI_BIT(MSTR)); |
| 120 | mr = spi_readl(as, MR); |
| 121 | gpio_set_value(asd->npcs_pin, active); |
| 122 | } else { |
| 123 | u32 cpol = (spi->mode & SPI_CPOL) ? SPI_BIT(CPOL) : 0; |
| 124 | int i; |
| 125 | u32 csr; |
| 126 | |
| 127 | /* Make sure clock polarity is correct */ |
| 128 | for (i = 0; i < spi->master->num_chipselect; i++) { |
| 129 | csr = spi_readl(as, CSR0 + 4 * i); |
| 130 | if ((csr ^ cpol) & SPI_BIT(CPOL)) |
| 131 | spi_writel(as, CSR0 + 4 * i, |
| 132 | csr ^ SPI_BIT(CPOL)); |
| 133 | } |
| 134 | |
| 135 | mr = spi_readl(as, MR); |
| 136 | mr = SPI_BFINS(PCS, ~(1 << spi->chip_select), mr); |
| 137 | if (spi->chip_select != 0) |
| 138 | gpio_set_value(asd->npcs_pin, active); |
| 139 | spi_writel(as, MR, mr); |
Atsushi Nemoto | f6febcc | 2008-02-23 15:23:39 -0800 | [diff] [blame] | 140 | } |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 141 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 142 | dev_dbg(&spi->dev, "activate %u%s, mr %08x\n", |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 143 | asd->npcs_pin, active ? " (high)" : "", |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 144 | mr); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 145 | } |
| 146 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 147 | static void cs_deactivate(struct atmel_spi *as, struct spi_device *spi) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 148 | { |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 149 | struct atmel_spi_device *asd = spi->controller_state; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 150 | unsigned active = spi->mode & SPI_CS_HIGH; |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 151 | u32 mr; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 152 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 153 | /* only deactivate *this* device; sometimes transfers to |
| 154 | * another device may be active when this routine is called. |
| 155 | */ |
| 156 | mr = spi_readl(as, MR); |
| 157 | if (~SPI_BFEXT(PCS, mr) & (1 << spi->chip_select)) { |
| 158 | mr = SPI_BFINS(PCS, 0xf, mr); |
| 159 | spi_writel(as, MR, mr); |
| 160 | } |
| 161 | |
| 162 | dev_dbg(&spi->dev, "DEactivate %u%s, mr %08x\n", |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 163 | asd->npcs_pin, active ? " (low)" : "", |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 164 | mr); |
| 165 | |
Haavard Skinnemoen | 5bfa26c | 2009-01-06 14:41:42 -0800 | [diff] [blame] | 166 | if (atmel_spi_is_v2() || spi->chip_select != 0) |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 167 | gpio_set_value(asd->npcs_pin, !active); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 170 | static inline int atmel_spi_xfer_is_last(struct spi_message *msg, |
| 171 | struct spi_transfer *xfer) |
| 172 | { |
| 173 | return msg->transfers.prev == &xfer->transfer_list; |
| 174 | } |
| 175 | |
| 176 | static inline int atmel_spi_xfer_can_be_chained(struct spi_transfer *xfer) |
| 177 | { |
| 178 | return xfer->delay_usecs == 0 && !xfer->cs_change; |
| 179 | } |
| 180 | |
| 181 | static void atmel_spi_next_xfer_data(struct spi_master *master, |
| 182 | struct spi_transfer *xfer, |
| 183 | dma_addr_t *tx_dma, |
| 184 | dma_addr_t *rx_dma, |
| 185 | u32 *plen) |
| 186 | { |
| 187 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 188 | u32 len = *plen; |
| 189 | |
| 190 | /* use scratch buffer only when rx or tx data is unspecified */ |
| 191 | if (xfer->rx_buf) |
Ben Nizette | 6aed4ee | 2009-12-14 22:20:20 -0800 | [diff] [blame] | 192 | *rx_dma = xfer->rx_dma + xfer->len - *plen; |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 193 | else { |
| 194 | *rx_dma = as->buffer_dma; |
| 195 | if (len > BUFFER_SIZE) |
| 196 | len = BUFFER_SIZE; |
| 197 | } |
| 198 | if (xfer->tx_buf) |
Ben Nizette | 6aed4ee | 2009-12-14 22:20:20 -0800 | [diff] [blame] | 199 | *tx_dma = xfer->tx_dma + xfer->len - *plen; |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 200 | else { |
| 201 | *tx_dma = as->buffer_dma; |
| 202 | if (len > BUFFER_SIZE) |
| 203 | len = BUFFER_SIZE; |
| 204 | memset(as->buffer, 0, len); |
| 205 | dma_sync_single_for_device(&as->pdev->dev, |
| 206 | as->buffer_dma, len, DMA_TO_DEVICE); |
| 207 | } |
| 208 | |
| 209 | *plen = len; |
| 210 | } |
| 211 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 212 | /* |
| 213 | * Submit next transfer for DMA. |
| 214 | * lock is held, spi irq is blocked |
| 215 | */ |
| 216 | static void atmel_spi_next_xfer(struct spi_master *master, |
| 217 | struct spi_message *msg) |
| 218 | { |
| 219 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 220 | struct spi_transfer *xfer; |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 221 | u32 len, remaining; |
| 222 | u32 ieval; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 223 | dma_addr_t tx_dma, rx_dma; |
| 224 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 225 | if (!as->current_transfer) |
| 226 | xfer = list_entry(msg->transfers.next, |
| 227 | struct spi_transfer, transfer_list); |
| 228 | else if (!as->next_transfer) |
| 229 | xfer = list_entry(as->current_transfer->transfer_list.next, |
| 230 | struct spi_transfer, transfer_list); |
| 231 | else |
| 232 | xfer = NULL; |
| 233 | |
| 234 | if (xfer) { |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 235 | spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS)); |
| 236 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 237 | len = xfer->len; |
| 238 | atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len); |
| 239 | remaining = xfer->len - len; |
| 240 | |
| 241 | spi_writel(as, RPR, rx_dma); |
| 242 | spi_writel(as, TPR, tx_dma); |
| 243 | |
| 244 | if (msg->spi->bits_per_word > 8) |
| 245 | len >>= 1; |
| 246 | spi_writel(as, RCR, len); |
| 247 | spi_writel(as, TCR, len); |
Haavard Skinnemoen | 8bacb21 | 2008-02-06 01:38:13 -0800 | [diff] [blame] | 248 | |
| 249 | dev_dbg(&msg->spi->dev, |
| 250 | " start xfer %p: len %u tx %p/%08x rx %p/%08x\n", |
| 251 | xfer, xfer->len, xfer->tx_buf, xfer->tx_dma, |
| 252 | xfer->rx_buf, xfer->rx_dma); |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 253 | } else { |
| 254 | xfer = as->next_transfer; |
| 255 | remaining = as->next_remaining_bytes; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 258 | as->current_transfer = xfer; |
| 259 | as->current_remaining_bytes = remaining; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 260 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 261 | if (remaining > 0) |
| 262 | len = remaining; |
Haavard Skinnemoen | 8bacb21 | 2008-02-06 01:38:13 -0800 | [diff] [blame] | 263 | else if (!atmel_spi_xfer_is_last(msg, xfer) |
| 264 | && atmel_spi_xfer_can_be_chained(xfer)) { |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 265 | xfer = list_entry(xfer->transfer_list.next, |
| 266 | struct spi_transfer, transfer_list); |
| 267 | len = xfer->len; |
| 268 | } else |
| 269 | xfer = NULL; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 270 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 271 | as->next_transfer = xfer; |
| 272 | |
| 273 | if (xfer) { |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 274 | u32 total; |
| 275 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 276 | total = len; |
| 277 | atmel_spi_next_xfer_data(master, xfer, &tx_dma, &rx_dma, &len); |
| 278 | as->next_remaining_bytes = total - len; |
| 279 | |
| 280 | spi_writel(as, RNPR, rx_dma); |
| 281 | spi_writel(as, TNPR, tx_dma); |
| 282 | |
| 283 | if (msg->spi->bits_per_word > 8) |
| 284 | len >>= 1; |
| 285 | spi_writel(as, RNCR, len); |
| 286 | spi_writel(as, TNCR, len); |
Haavard Skinnemoen | 8bacb21 | 2008-02-06 01:38:13 -0800 | [diff] [blame] | 287 | |
| 288 | dev_dbg(&msg->spi->dev, |
| 289 | " next xfer %p: len %u tx %p/%08x rx %p/%08x\n", |
| 290 | xfer, xfer->len, xfer->tx_buf, xfer->tx_dma, |
| 291 | xfer->rx_buf, xfer->rx_dma); |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 292 | ieval = SPI_BIT(ENDRX) | SPI_BIT(OVRES); |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 293 | } else { |
| 294 | spi_writel(as, RNCR, 0); |
| 295 | spi_writel(as, TNCR, 0); |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 296 | ieval = SPI_BIT(RXBUFF) | SPI_BIT(ENDRX) | SPI_BIT(OVRES); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 297 | } |
| 298 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 299 | /* REVISIT: We're waiting for ENDRX before we start the next |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 300 | * transfer because we need to handle some difficult timing |
| 301 | * issues otherwise. If we wait for ENDTX in one transfer and |
| 302 | * then starts waiting for ENDRX in the next, it's difficult |
| 303 | * to tell the difference between the ENDRX interrupt we're |
| 304 | * actually waiting for and the ENDRX interrupt of the |
| 305 | * previous transfer. |
| 306 | * |
| 307 | * It should be doable, though. Just not now... |
| 308 | */ |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 309 | spi_writel(as, IER, ieval); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 310 | spi_writel(as, PTCR, SPI_BIT(TXTEN) | SPI_BIT(RXTEN)); |
| 311 | } |
| 312 | |
| 313 | static void atmel_spi_next_message(struct spi_master *master) |
| 314 | { |
| 315 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 316 | struct spi_message *msg; |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 317 | struct spi_device *spi; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 318 | |
| 319 | BUG_ON(as->current_transfer); |
| 320 | |
| 321 | msg = list_entry(as->queue.next, struct spi_message, queue); |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 322 | spi = msg->spi; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 323 | |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 324 | dev_dbg(master->dev.parent, "start message %p for %s\n", |
Kay Sievers | 6c7377a | 2009-03-24 16:38:21 -0700 | [diff] [blame] | 325 | msg, dev_name(&spi->dev)); |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 326 | |
| 327 | /* select chip if it's not still active */ |
| 328 | if (as->stay) { |
| 329 | if (as->stay != spi) { |
| 330 | cs_deactivate(as, as->stay); |
| 331 | cs_activate(as, spi); |
| 332 | } |
| 333 | as->stay = NULL; |
| 334 | } else |
| 335 | cs_activate(as, spi); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 336 | |
| 337 | atmel_spi_next_xfer(master, msg); |
| 338 | } |
| 339 | |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 340 | /* |
| 341 | * For DMA, tx_buf/tx_dma have the same relationship as rx_buf/rx_dma: |
| 342 | * - The buffer is either valid for CPU access, else NULL |
| 343 | * - If the buffer is valid, so is its DMA addresss |
| 344 | * |
| 345 | * This driver manages the dma addresss unless message->is_dma_mapped. |
| 346 | */ |
| 347 | static int |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 348 | atmel_spi_dma_map_xfer(struct atmel_spi *as, struct spi_transfer *xfer) |
| 349 | { |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 350 | struct device *dev = &as->pdev->dev; |
| 351 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 352 | xfer->tx_dma = xfer->rx_dma = INVALID_DMA_ADDRESS; |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 353 | if (xfer->tx_buf) { |
| 354 | xfer->tx_dma = dma_map_single(dev, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 355 | (void *) xfer->tx_buf, xfer->len, |
| 356 | DMA_TO_DEVICE); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 357 | if (dma_mapping_error(dev, xfer->tx_dma)) |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 358 | return -ENOMEM; |
| 359 | } |
| 360 | if (xfer->rx_buf) { |
| 361 | xfer->rx_dma = dma_map_single(dev, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 362 | xfer->rx_buf, xfer->len, |
| 363 | DMA_FROM_DEVICE); |
FUJITA Tomonori | 8d8bb39 | 2008-07-25 19:44:49 -0700 | [diff] [blame] | 364 | if (dma_mapping_error(dev, xfer->rx_dma)) { |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 365 | if (xfer->tx_buf) |
| 366 | dma_unmap_single(dev, |
| 367 | xfer->tx_dma, xfer->len, |
| 368 | DMA_TO_DEVICE); |
| 369 | return -ENOMEM; |
| 370 | } |
| 371 | } |
| 372 | return 0; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | static void atmel_spi_dma_unmap_xfer(struct spi_master *master, |
| 376 | struct spi_transfer *xfer) |
| 377 | { |
| 378 | if (xfer->tx_dma != INVALID_DMA_ADDRESS) |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 379 | dma_unmap_single(master->dev.parent, xfer->tx_dma, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 380 | xfer->len, DMA_TO_DEVICE); |
| 381 | if (xfer->rx_dma != INVALID_DMA_ADDRESS) |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 382 | dma_unmap_single(master->dev.parent, xfer->rx_dma, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 383 | xfer->len, DMA_FROM_DEVICE); |
| 384 | } |
| 385 | |
| 386 | static void |
| 387 | atmel_spi_msg_done(struct spi_master *master, struct atmel_spi *as, |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 388 | struct spi_message *msg, int status, int stay) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 389 | { |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 390 | if (!stay || status < 0) |
| 391 | cs_deactivate(as, msg->spi); |
| 392 | else |
| 393 | as->stay = msg->spi; |
| 394 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 395 | list_del(&msg->queue); |
| 396 | msg->status = status; |
| 397 | |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 398 | dev_dbg(master->dev.parent, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 399 | "xfer complete: %u bytes transferred\n", |
| 400 | msg->actual_length); |
| 401 | |
| 402 | spin_unlock(&as->lock); |
| 403 | msg->complete(msg->context); |
| 404 | spin_lock(&as->lock); |
| 405 | |
| 406 | as->current_transfer = NULL; |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 407 | as->next_transfer = NULL; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 408 | |
| 409 | /* continue if needed */ |
| 410 | if (list_empty(&as->queue) || as->stopping) |
| 411 | spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS)); |
| 412 | else |
| 413 | atmel_spi_next_message(master); |
| 414 | } |
| 415 | |
| 416 | static irqreturn_t |
| 417 | atmel_spi_interrupt(int irq, void *dev_id) |
| 418 | { |
| 419 | struct spi_master *master = dev_id; |
| 420 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 421 | struct spi_message *msg; |
| 422 | struct spi_transfer *xfer; |
| 423 | u32 status, pending, imr; |
| 424 | int ret = IRQ_NONE; |
| 425 | |
| 426 | spin_lock(&as->lock); |
| 427 | |
| 428 | xfer = as->current_transfer; |
| 429 | msg = list_entry(as->queue.next, struct spi_message, queue); |
| 430 | |
| 431 | imr = spi_readl(as, IMR); |
| 432 | status = spi_readl(as, SR); |
| 433 | pending = status & imr; |
| 434 | |
| 435 | if (pending & SPI_BIT(OVRES)) { |
| 436 | int timeout; |
| 437 | |
| 438 | ret = IRQ_HANDLED; |
| 439 | |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 440 | spi_writel(as, IDR, (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 441 | | SPI_BIT(OVRES))); |
| 442 | |
| 443 | /* |
| 444 | * When we get an overrun, we disregard the current |
| 445 | * transfer. Data will not be copied back from any |
| 446 | * bounce buffer and msg->actual_len will not be |
| 447 | * updated with the last xfer. |
| 448 | * |
| 449 | * We will also not process any remaning transfers in |
| 450 | * the message. |
| 451 | * |
| 452 | * First, stop the transfer and unmap the DMA buffers. |
| 453 | */ |
| 454 | spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS)); |
| 455 | if (!msg->is_dma_mapped) |
| 456 | atmel_spi_dma_unmap_xfer(master, xfer); |
| 457 | |
| 458 | /* REVISIT: udelay in irq is unfriendly */ |
| 459 | if (xfer->delay_usecs) |
| 460 | udelay(xfer->delay_usecs); |
| 461 | |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 462 | dev_warn(master->dev.parent, "overrun (%u/%u remaining)\n", |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 463 | spi_readl(as, TCR), spi_readl(as, RCR)); |
| 464 | |
| 465 | /* |
| 466 | * Clean up DMA registers and make sure the data |
| 467 | * registers are empty. |
| 468 | */ |
| 469 | spi_writel(as, RNCR, 0); |
| 470 | spi_writel(as, TNCR, 0); |
| 471 | spi_writel(as, RCR, 0); |
| 472 | spi_writel(as, TCR, 0); |
| 473 | for (timeout = 1000; timeout; timeout--) |
| 474 | if (spi_readl(as, SR) & SPI_BIT(TXEMPTY)) |
| 475 | break; |
| 476 | if (!timeout) |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 477 | dev_warn(master->dev.parent, |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 478 | "timeout waiting for TXEMPTY"); |
| 479 | while (spi_readl(as, SR) & SPI_BIT(RDRF)) |
| 480 | spi_readl(as, RDR); |
| 481 | |
| 482 | /* Clear any overrun happening while cleaning up */ |
| 483 | spi_readl(as, SR); |
| 484 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 485 | atmel_spi_msg_done(master, as, msg, -EIO, 0); |
Gerard Kam | dc32944 | 2008-08-04 13:41:12 -0700 | [diff] [blame] | 486 | } else if (pending & (SPI_BIT(RXBUFF) | SPI_BIT(ENDRX))) { |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 487 | ret = IRQ_HANDLED; |
| 488 | |
| 489 | spi_writel(as, IDR, pending); |
| 490 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 491 | if (as->current_remaining_bytes == 0) { |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 492 | msg->actual_length += xfer->len; |
| 493 | |
| 494 | if (!msg->is_dma_mapped) |
| 495 | atmel_spi_dma_unmap_xfer(master, xfer); |
| 496 | |
| 497 | /* REVISIT: udelay in irq is unfriendly */ |
| 498 | if (xfer->delay_usecs) |
| 499 | udelay(xfer->delay_usecs); |
| 500 | |
Silvester Erdeg | 154443c | 2008-02-06 01:38:12 -0800 | [diff] [blame] | 501 | if (atmel_spi_xfer_is_last(msg, xfer)) { |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 502 | /* report completed message */ |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 503 | atmel_spi_msg_done(master, as, msg, 0, |
| 504 | xfer->cs_change); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 505 | } else { |
| 506 | if (xfer->cs_change) { |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 507 | cs_deactivate(as, msg->spi); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 508 | udelay(1); |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 509 | cs_activate(as, msg->spi); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /* |
| 513 | * Not done yet. Submit the next transfer. |
| 514 | * |
| 515 | * FIXME handle protocol options for xfer |
| 516 | */ |
| 517 | atmel_spi_next_xfer(master, msg); |
| 518 | } |
| 519 | } else { |
| 520 | /* |
| 521 | * Keep going, we still have data to send in |
| 522 | * the current transfer. |
| 523 | */ |
| 524 | atmel_spi_next_xfer(master, msg); |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | spin_unlock(&as->lock); |
| 529 | |
| 530 | return ret; |
| 531 | } |
| 532 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 533 | static int atmel_spi_setup(struct spi_device *spi) |
| 534 | { |
| 535 | struct atmel_spi *as; |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 536 | struct atmel_spi_device *asd; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 537 | u32 scbr, csr; |
| 538 | unsigned int bits = spi->bits_per_word; |
Haavard Skinnemoen | 592e7bf | 2008-04-30 00:52:17 -0700 | [diff] [blame] | 539 | unsigned long bus_hz; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 540 | unsigned int npcs_pin; |
| 541 | int ret; |
| 542 | |
| 543 | as = spi_master_get_devdata(spi->master); |
| 544 | |
| 545 | if (as->stopping) |
| 546 | return -ESHUTDOWN; |
| 547 | |
| 548 | if (spi->chip_select > spi->master->num_chipselect) { |
| 549 | dev_dbg(&spi->dev, |
| 550 | "setup: invalid chipselect %u (%u defined)\n", |
| 551 | spi->chip_select, spi->master->num_chipselect); |
| 552 | return -EINVAL; |
| 553 | } |
| 554 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 555 | if (bits < 8 || bits > 16) { |
| 556 | dev_dbg(&spi->dev, |
| 557 | "setup: invalid bits_per_word %u (8 to 16)\n", |
| 558 | bits); |
| 559 | return -EINVAL; |
| 560 | } |
| 561 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 562 | /* see notes above re chipselect */ |
Haavard Skinnemoen | 5bfa26c | 2009-01-06 14:41:42 -0800 | [diff] [blame] | 563 | if (!atmel_spi_is_v2() |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 564 | && spi->chip_select == 0 |
| 565 | && (spi->mode & SPI_CS_HIGH)) { |
| 566 | dev_dbg(&spi->dev, "setup: can't be active-high\n"); |
| 567 | return -EINVAL; |
| 568 | } |
| 569 | |
Haavard Skinnemoen | 5bfa26c | 2009-01-06 14:41:42 -0800 | [diff] [blame] | 570 | /* v1 chips start out at half the peripheral bus speed. */ |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 571 | bus_hz = clk_get_rate(as->clk); |
Haavard Skinnemoen | 5bfa26c | 2009-01-06 14:41:42 -0800 | [diff] [blame] | 572 | if (!atmel_spi_is_v2()) |
Haavard Skinnemoen | 592e7bf | 2008-04-30 00:52:17 -0700 | [diff] [blame] | 573 | bus_hz /= 2; |
| 574 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 575 | if (spi->max_speed_hz) { |
Haavard Skinnemoen | 592e7bf | 2008-04-30 00:52:17 -0700 | [diff] [blame] | 576 | /* |
| 577 | * Calculate the lowest divider that satisfies the |
| 578 | * constraint, assuming div32/fdiv/mbz == 0. |
| 579 | */ |
| 580 | scbr = DIV_ROUND_UP(bus_hz, spi->max_speed_hz); |
| 581 | |
| 582 | /* |
| 583 | * If the resulting divider doesn't fit into the |
| 584 | * register bitfield, we can't satisfy the constraint. |
| 585 | */ |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 586 | if (scbr >= (1 << SPI_SCBR_SIZE)) { |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 587 | dev_dbg(&spi->dev, |
| 588 | "setup: %d Hz too slow, scbr %u; min %ld Hz\n", |
| 589 | spi->max_speed_hz, scbr, bus_hz/255); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 590 | return -EINVAL; |
| 591 | } |
| 592 | } else |
Haavard Skinnemoen | 592e7bf | 2008-04-30 00:52:17 -0700 | [diff] [blame] | 593 | /* speed zero means "as slow as possible" */ |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 594 | scbr = 0xff; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 595 | |
| 596 | csr = SPI_BF(SCBR, scbr) | SPI_BF(BITS, bits - 8); |
| 597 | if (spi->mode & SPI_CPOL) |
| 598 | csr |= SPI_BIT(CPOL); |
| 599 | if (!(spi->mode & SPI_CPHA)) |
| 600 | csr |= SPI_BIT(NCPHA); |
| 601 | |
Haavard Skinnemoen | 1eed29d | 2008-02-06 01:38:11 -0800 | [diff] [blame] | 602 | /* DLYBS is mostly irrelevant since we manage chipselect using GPIOs. |
| 603 | * |
| 604 | * DLYBCT would add delays between words, slowing down transfers. |
| 605 | * It could potentially be useful to cope with DMA bottlenecks, but |
| 606 | * in those cases it's probably best to just use a lower bitrate. |
| 607 | */ |
| 608 | csr |= SPI_BF(DLYBS, 0); |
| 609 | csr |= SPI_BF(DLYBCT, 0); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 610 | |
| 611 | /* chipselect must have been muxed as GPIO (e.g. in board setup) */ |
| 612 | npcs_pin = (unsigned int)spi->controller_data; |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 613 | asd = spi->controller_state; |
| 614 | if (!asd) { |
| 615 | asd = kzalloc(sizeof(struct atmel_spi_device), GFP_KERNEL); |
| 616 | if (!asd) |
| 617 | return -ENOMEM; |
| 618 | |
Kay Sievers | 6c7377a | 2009-03-24 16:38:21 -0700 | [diff] [blame] | 619 | ret = gpio_request(npcs_pin, dev_name(&spi->dev)); |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 620 | if (ret) { |
| 621 | kfree(asd); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 622 | return ret; |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | asd->npcs_pin = npcs_pin; |
| 626 | spi->controller_state = asd; |
David Brownell | 28735a7 | 2007-03-16 13:38:14 -0800 | [diff] [blame] | 627 | gpio_direction_output(npcs_pin, !(spi->mode & SPI_CS_HIGH)); |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 628 | } else { |
| 629 | unsigned long flags; |
| 630 | |
| 631 | spin_lock_irqsave(&as->lock, flags); |
| 632 | if (as->stay == spi) |
| 633 | as->stay = NULL; |
| 634 | cs_deactivate(as, spi); |
| 635 | spin_unlock_irqrestore(&as->lock, flags); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 636 | } |
| 637 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 638 | asd->csr = csr; |
| 639 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 640 | dev_dbg(&spi->dev, |
| 641 | "setup: %lu Hz bpw %u mode 0x%x -> csr%d %08x\n", |
Haavard Skinnemoen | 592e7bf | 2008-04-30 00:52:17 -0700 | [diff] [blame] | 642 | bus_hz / scbr, bits, spi->mode, spi->chip_select, csr); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 643 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 644 | if (!atmel_spi_is_v2()) |
| 645 | spi_writel(as, CSR0 + 4 * spi->chip_select, csr); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 646 | |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | static int atmel_spi_transfer(struct spi_device *spi, struct spi_message *msg) |
| 651 | { |
| 652 | struct atmel_spi *as; |
| 653 | struct spi_transfer *xfer; |
| 654 | unsigned long flags; |
Tony Jones | 49dce68 | 2007-10-16 01:27:48 -0700 | [diff] [blame] | 655 | struct device *controller = spi->master->dev.parent; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 656 | |
| 657 | as = spi_master_get_devdata(spi->master); |
| 658 | |
| 659 | dev_dbg(controller, "new message %p submitted for %s\n", |
Kay Sievers | 6c7377a | 2009-03-24 16:38:21 -0700 | [diff] [blame] | 660 | msg, dev_name(&spi->dev)); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 661 | |
Stanislaw Gruszka | 5b96f17 | 2009-01-15 13:50:44 -0800 | [diff] [blame] | 662 | if (unlikely(list_empty(&msg->transfers))) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 663 | return -EINVAL; |
| 664 | |
| 665 | if (as->stopping) |
| 666 | return -ESHUTDOWN; |
| 667 | |
| 668 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
Atsushi Nemoto | 0671981 | 2008-04-28 02:14:19 -0700 | [diff] [blame] | 669 | if (!(xfer->tx_buf || xfer->rx_buf) && xfer->len) { |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 670 | dev_dbg(&spi->dev, "missing rx or tx buf\n"); |
| 671 | return -EINVAL; |
| 672 | } |
| 673 | |
| 674 | /* FIXME implement these protocol options!! */ |
| 675 | if (xfer->bits_per_word || xfer->speed_hz) { |
| 676 | dev_dbg(&spi->dev, "no protocol options yet\n"); |
| 677 | return -ENOPROTOOPT; |
| 678 | } |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 679 | |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 680 | /* |
| 681 | * DMA map early, for performance (empties dcache ASAP) and |
| 682 | * better fault reporting. This is a DMA-only driver. |
| 683 | * |
| 684 | * NOTE that if dma_unmap_single() ever starts to do work on |
| 685 | * platforms supported by this driver, we would need to clean |
| 686 | * up mappings for previously-mapped transfers. |
| 687 | */ |
| 688 | if (!msg->is_dma_mapped) { |
| 689 | if (atmel_spi_dma_map_xfer(as, xfer) < 0) |
| 690 | return -ENOMEM; |
| 691 | } |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 692 | } |
| 693 | |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 694 | #ifdef VERBOSE |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 695 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
| 696 | dev_dbg(controller, |
| 697 | " xfer %p: len %u tx %p/%08x rx %p/%08x\n", |
| 698 | xfer, xfer->len, |
| 699 | xfer->tx_buf, xfer->tx_dma, |
| 700 | xfer->rx_buf, xfer->rx_dma); |
| 701 | } |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 702 | #endif |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 703 | |
| 704 | msg->status = -EINPROGRESS; |
| 705 | msg->actual_length = 0; |
| 706 | |
| 707 | spin_lock_irqsave(&as->lock, flags); |
| 708 | list_add_tail(&msg->queue, &as->queue); |
| 709 | if (!as->current_transfer) |
| 710 | atmel_spi_next_message(spi->master); |
| 711 | spin_unlock_irqrestore(&as->lock, flags); |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
David Brownell | bb2d1c3 | 2007-02-20 13:58:19 -0800 | [diff] [blame] | 716 | static void atmel_spi_cleanup(struct spi_device *spi) |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 717 | { |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 718 | struct atmel_spi *as = spi_master_get_devdata(spi->master); |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 719 | struct atmel_spi_device *asd = spi->controller_state; |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 720 | unsigned gpio = (unsigned) spi->controller_data; |
| 721 | unsigned long flags; |
| 722 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 723 | if (!asd) |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 724 | return; |
| 725 | |
| 726 | spin_lock_irqsave(&as->lock, flags); |
| 727 | if (as->stay == spi) { |
| 728 | as->stay = NULL; |
| 729 | cs_deactivate(as, spi); |
| 730 | } |
| 731 | spin_unlock_irqrestore(&as->lock, flags); |
| 732 | |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 733 | spi->controller_state = NULL; |
David Brownell | defbd3b | 2007-07-17 04:04:08 -0700 | [diff] [blame] | 734 | gpio_free(gpio); |
Haavard Skinnemoen | 5ee36c9 | 2009-01-06 14:41:43 -0800 | [diff] [blame] | 735 | kfree(asd); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /*-------------------------------------------------------------------------*/ |
| 739 | |
| 740 | static int __init atmel_spi_probe(struct platform_device *pdev) |
| 741 | { |
| 742 | struct resource *regs; |
| 743 | int irq; |
| 744 | struct clk *clk; |
| 745 | int ret; |
| 746 | struct spi_master *master; |
| 747 | struct atmel_spi *as; |
| 748 | |
| 749 | regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 750 | if (!regs) |
| 751 | return -ENXIO; |
| 752 | |
| 753 | irq = platform_get_irq(pdev, 0); |
| 754 | if (irq < 0) |
| 755 | return irq; |
| 756 | |
| 757 | clk = clk_get(&pdev->dev, "spi_clk"); |
| 758 | if (IS_ERR(clk)) |
| 759 | return PTR_ERR(clk); |
| 760 | |
| 761 | /* setup spi core then atmel-specific driver state */ |
| 762 | ret = -ENOMEM; |
| 763 | master = spi_alloc_master(&pdev->dev, sizeof *as); |
| 764 | if (!master) |
| 765 | goto out_free; |
| 766 | |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 767 | /* the spi->mode bits understood by this driver: */ |
| 768 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; |
| 769 | |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 770 | master->bus_num = pdev->id; |
| 771 | master->num_chipselect = 4; |
| 772 | master->setup = atmel_spi_setup; |
| 773 | master->transfer = atmel_spi_transfer; |
| 774 | master->cleanup = atmel_spi_cleanup; |
| 775 | platform_set_drvdata(pdev, master); |
| 776 | |
| 777 | as = spi_master_get_devdata(master); |
| 778 | |
David Brownell | 8da0859 | 2007-07-17 04:04:07 -0700 | [diff] [blame] | 779 | /* |
| 780 | * Scratch buffer is used for throwaway rx and tx data. |
| 781 | * It's coherent to minimize dcache pollution. |
| 782 | */ |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 783 | as->buffer = dma_alloc_coherent(&pdev->dev, BUFFER_SIZE, |
| 784 | &as->buffer_dma, GFP_KERNEL); |
| 785 | if (!as->buffer) |
| 786 | goto out_free; |
| 787 | |
| 788 | spin_lock_init(&as->lock); |
| 789 | INIT_LIST_HEAD(&as->queue); |
| 790 | as->pdev = pdev; |
hartleys | 905aa0a | 2009-12-14 22:22:25 +0000 | [diff] [blame] | 791 | as->regs = ioremap(regs->start, resource_size(regs)); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 792 | if (!as->regs) |
| 793 | goto out_free_buffer; |
| 794 | as->irq = irq; |
| 795 | as->clk = clk; |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 796 | |
| 797 | ret = request_irq(irq, atmel_spi_interrupt, 0, |
Kay Sievers | 6c7377a | 2009-03-24 16:38:21 -0700 | [diff] [blame] | 798 | dev_name(&pdev->dev), master); |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 799 | if (ret) |
| 800 | goto out_unmap_regs; |
| 801 | |
| 802 | /* Initialize the hardware */ |
| 803 | clk_enable(clk); |
| 804 | spi_writel(as, CR, SPI_BIT(SWRST)); |
Jean-Christophe Lallemand | 50d7d5b | 2008-11-12 13:27:00 -0800 | [diff] [blame] | 805 | spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */ |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 806 | spi_writel(as, MR, SPI_BIT(MSTR) | SPI_BIT(MODFDIS)); |
| 807 | spi_writel(as, PTCR, SPI_BIT(RXTDIS) | SPI_BIT(TXTDIS)); |
| 808 | spi_writel(as, CR, SPI_BIT(SPIEN)); |
| 809 | |
| 810 | /* go! */ |
| 811 | dev_info(&pdev->dev, "Atmel SPI Controller at 0x%08lx (irq %d)\n", |
| 812 | (unsigned long)regs->start, irq); |
| 813 | |
| 814 | ret = spi_register_master(master); |
| 815 | if (ret) |
| 816 | goto out_reset_hw; |
| 817 | |
| 818 | return 0; |
| 819 | |
| 820 | out_reset_hw: |
| 821 | spi_writel(as, CR, SPI_BIT(SWRST)); |
Jean-Christophe Lallemand | 50d7d5b | 2008-11-12 13:27:00 -0800 | [diff] [blame] | 822 | spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */ |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 823 | clk_disable(clk); |
| 824 | free_irq(irq, master); |
| 825 | out_unmap_regs: |
| 826 | iounmap(as->regs); |
| 827 | out_free_buffer: |
| 828 | dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer, |
| 829 | as->buffer_dma); |
| 830 | out_free: |
| 831 | clk_put(clk); |
| 832 | spi_master_put(master); |
| 833 | return ret; |
| 834 | } |
| 835 | |
| 836 | static int __exit atmel_spi_remove(struct platform_device *pdev) |
| 837 | { |
| 838 | struct spi_master *master = platform_get_drvdata(pdev); |
| 839 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 840 | struct spi_message *msg; |
| 841 | |
| 842 | /* reset the hardware and block queue progress */ |
| 843 | spin_lock_irq(&as->lock); |
| 844 | as->stopping = 1; |
| 845 | spi_writel(as, CR, SPI_BIT(SWRST)); |
Jean-Christophe Lallemand | 50d7d5b | 2008-11-12 13:27:00 -0800 | [diff] [blame] | 846 | spi_writel(as, CR, SPI_BIT(SWRST)); /* AT91SAM9263 Rev B workaround */ |
Haavard Skinnemoen | 754ce4f | 2007-02-14 00:33:09 -0800 | [diff] [blame] | 847 | spi_readl(as, SR); |
| 848 | spin_unlock_irq(&as->lock); |
| 849 | |
| 850 | /* Terminate remaining queued transfers */ |
| 851 | list_for_each_entry(msg, &as->queue, queue) { |
| 852 | /* REVISIT unmapping the dma is a NOP on ARM and AVR32 |
| 853 | * but we shouldn't depend on that... |
| 854 | */ |
| 855 | msg->status = -ESHUTDOWN; |
| 856 | msg->complete(msg->context); |
| 857 | } |
| 858 | |
| 859 | dma_free_coherent(&pdev->dev, BUFFER_SIZE, as->buffer, |
| 860 | as->buffer_dma); |
| 861 | |
| 862 | clk_disable(as->clk); |
| 863 | clk_put(as->clk); |
| 864 | free_irq(as->irq, master); |
| 865 | iounmap(as->regs); |
| 866 | |
| 867 | spi_unregister_master(master); |
| 868 | |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | #ifdef CONFIG_PM |
| 873 | |
| 874 | static int atmel_spi_suspend(struct platform_device *pdev, pm_message_t mesg) |
| 875 | { |
| 876 | struct spi_master *master = platform_get_drvdata(pdev); |
| 877 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 878 | |
| 879 | clk_disable(as->clk); |
| 880 | return 0; |
| 881 | } |
| 882 | |
| 883 | static int atmel_spi_resume(struct platform_device *pdev) |
| 884 | { |
| 885 | struct spi_master *master = platform_get_drvdata(pdev); |
| 886 | struct atmel_spi *as = spi_master_get_devdata(master); |
| 887 | |
| 888 | clk_enable(as->clk); |
| 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | #else |
| 893 | #define atmel_spi_suspend NULL |
| 894 | #define atmel_spi_resume NULL |
| 895 | #endif |
| 896 | |
| 897 | |
| 898 | static struct platform_driver atmel_spi_driver = { |
| 899 | .driver = { |
| 900 | .name = "atmel_spi", |
| 901 | .owner = THIS_MODULE, |
| 902 | }, |
| 903 | .suspend = atmel_spi_suspend, |
| 904 | .resume = atmel_spi_resume, |
| 905 | .remove = __exit_p(atmel_spi_remove), |
| 906 | }; |
| 907 | |
| 908 | static int __init atmel_spi_init(void) |
| 909 | { |
| 910 | return platform_driver_probe(&atmel_spi_driver, atmel_spi_probe); |
| 911 | } |
| 912 | module_init(atmel_spi_init); |
| 913 | |
| 914 | static void __exit atmel_spi_exit(void) |
| 915 | { |
| 916 | platform_driver_unregister(&atmel_spi_driver); |
| 917 | } |
| 918 | module_exit(atmel_spi_exit); |
| 919 | |
| 920 | MODULE_DESCRIPTION("Atmel AT32/AT91 SPI Controller driver"); |
| 921 | MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>"); |
| 922 | MODULE_LICENSE("GPL"); |
Kay Sievers | 7e38c3c | 2008-04-10 21:29:20 -0700 | [diff] [blame] | 923 | MODULE_ALIAS("platform:atmel_spi"); |