Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 1 | /* |
| 2 | * xilinx_spi.c |
| 3 | * |
| 4 | * Xilinx SPI controller driver (master mode only) |
| 5 | * |
| 6 | * Author: MontaVista Software, Inc. |
| 7 | * source@mvista.com |
| 8 | * |
| 9 | * 2002-2007 (c) MontaVista Software, Inc. This file is licensed under the |
| 10 | * terms of the GNU General Public License version 2. This program is licensed |
| 11 | * "as is" without any warranty of any kind, whether express or implied. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/interrupt.h> |
John Linn | ff82c58 | 2009-01-09 16:01:53 -0700 | [diff] [blame] | 17 | |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 18 | #include <linux/spi/spi.h> |
| 19 | #include <linux/spi/spi_bitbang.h> |
| 20 | #include <linux/io.h> |
| 21 | |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 22 | #include "xilinx_spi.h" |
| 23 | #include <linux/spi/xilinx_spi.h> |
| 24 | |
David Brownell | fc3ba95 | 2007-08-30 23:56:24 -0700 | [diff] [blame] | 25 | #define XILINX_SPI_NAME "xilinx_spi" |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 26 | |
| 27 | /* Register definitions as per "OPB Serial Peripheral Interface (SPI) (v1.00e) |
| 28 | * Product Specification", DS464 |
| 29 | */ |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 30 | #define XSPI_CR_OFFSET 0x60 /* Control Register */ |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 31 | |
| 32 | #define XSPI_CR_ENABLE 0x02 |
| 33 | #define XSPI_CR_MASTER_MODE 0x04 |
| 34 | #define XSPI_CR_CPOL 0x08 |
| 35 | #define XSPI_CR_CPHA 0x10 |
| 36 | #define XSPI_CR_MODE_MASK (XSPI_CR_CPHA | XSPI_CR_CPOL) |
| 37 | #define XSPI_CR_TXFIFO_RESET 0x20 |
| 38 | #define XSPI_CR_RXFIFO_RESET 0x40 |
| 39 | #define XSPI_CR_MANUAL_SSELECT 0x80 |
| 40 | #define XSPI_CR_TRANS_INHIBIT 0x100 |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 41 | #define XSPI_CR_LSB_FIRST 0x200 |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 42 | |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 43 | #define XSPI_SR_OFFSET 0x64 /* Status Register */ |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 44 | |
| 45 | #define XSPI_SR_RX_EMPTY_MASK 0x01 /* Receive FIFO is empty */ |
| 46 | #define XSPI_SR_RX_FULL_MASK 0x02 /* Receive FIFO is full */ |
| 47 | #define XSPI_SR_TX_EMPTY_MASK 0x04 /* Transmit FIFO is empty */ |
| 48 | #define XSPI_SR_TX_FULL_MASK 0x08 /* Transmit FIFO is full */ |
| 49 | #define XSPI_SR_MODE_FAULT_MASK 0x10 /* Mode fault error */ |
| 50 | |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 51 | #define XSPI_TXD_OFFSET 0x68 /* Data Transmit Register */ |
| 52 | #define XSPI_RXD_OFFSET 0x6c /* Data Receive Register */ |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 53 | |
| 54 | #define XSPI_SSR_OFFSET 0x70 /* 32-bit Slave Select Register */ |
| 55 | |
| 56 | /* Register definitions as per "OPB IPIF (v3.01c) Product Specification", DS414 |
| 57 | * IPIF registers are 32 bit |
| 58 | */ |
| 59 | #define XIPIF_V123B_DGIER_OFFSET 0x1c /* IPIF global int enable reg */ |
| 60 | #define XIPIF_V123B_GINTR_ENABLE 0x80000000 |
| 61 | |
| 62 | #define XIPIF_V123B_IISR_OFFSET 0x20 /* IPIF interrupt status reg */ |
| 63 | #define XIPIF_V123B_IIER_OFFSET 0x28 /* IPIF interrupt enable reg */ |
| 64 | |
| 65 | #define XSPI_INTR_MODE_FAULT 0x01 /* Mode fault error */ |
| 66 | #define XSPI_INTR_SLAVE_MODE_FAULT 0x02 /* Selected as slave while |
| 67 | * disabled */ |
| 68 | #define XSPI_INTR_TX_EMPTY 0x04 /* TxFIFO is empty */ |
| 69 | #define XSPI_INTR_TX_UNDERRUN 0x08 /* TxFIFO was underrun */ |
| 70 | #define XSPI_INTR_RX_FULL 0x10 /* RxFIFO is full */ |
| 71 | #define XSPI_INTR_RX_OVERRUN 0x20 /* RxFIFO was overrun */ |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 72 | #define XSPI_INTR_TX_HALF_EMPTY 0x40 /* TxFIFO is half empty */ |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 73 | |
| 74 | #define XIPIF_V123B_RESETR_OFFSET 0x40 /* IPIF reset register */ |
| 75 | #define XIPIF_V123B_RESET_MASK 0x0a /* the value to write */ |
| 76 | |
| 77 | struct xilinx_spi { |
| 78 | /* bitbang has to be first */ |
| 79 | struct spi_bitbang bitbang; |
| 80 | struct completion done; |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 81 | struct resource mem; /* phys mem */ |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 82 | void __iomem *regs; /* virt. address of the control registers */ |
| 83 | |
| 84 | u32 irq; |
| 85 | |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 86 | u8 *rx_ptr; /* pointer in the Tx buffer */ |
| 87 | const u8 *tx_ptr; /* pointer in the Rx buffer */ |
| 88 | int remaining_bytes; /* the number of bytes left to transfer */ |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 89 | u8 bits_per_word; |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 90 | unsigned int (*read_fn) (void __iomem *); |
| 91 | void (*write_fn) (u32, void __iomem *); |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 92 | void (*tx_fn) (struct xilinx_spi *); |
| 93 | void (*rx_fn) (struct xilinx_spi *); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 96 | static void xspi_tx8(struct xilinx_spi *xspi) |
| 97 | { |
| 98 | xspi->write_fn(*xspi->tx_ptr, xspi->regs + XSPI_TXD_OFFSET); |
| 99 | xspi->tx_ptr++; |
| 100 | } |
| 101 | |
| 102 | static void xspi_tx16(struct xilinx_spi *xspi) |
| 103 | { |
| 104 | xspi->write_fn(*(u16 *)(xspi->tx_ptr), xspi->regs + XSPI_TXD_OFFSET); |
| 105 | xspi->tx_ptr += 2; |
| 106 | } |
| 107 | |
| 108 | static void xspi_tx32(struct xilinx_spi *xspi) |
| 109 | { |
| 110 | xspi->write_fn(*(u32 *)(xspi->tx_ptr), xspi->regs + XSPI_TXD_OFFSET); |
| 111 | xspi->tx_ptr += 4; |
| 112 | } |
| 113 | |
| 114 | static void xspi_rx8(struct xilinx_spi *xspi) |
| 115 | { |
| 116 | u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET); |
| 117 | if (xspi->rx_ptr) { |
| 118 | *xspi->rx_ptr = data & 0xff; |
| 119 | xspi->rx_ptr++; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | static void xspi_rx16(struct xilinx_spi *xspi) |
| 124 | { |
| 125 | u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET); |
| 126 | if (xspi->rx_ptr) { |
| 127 | *(u16 *)(xspi->rx_ptr) = data & 0xffff; |
| 128 | xspi->rx_ptr += 2; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static void xspi_rx32(struct xilinx_spi *xspi) |
| 133 | { |
| 134 | u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET); |
| 135 | if (xspi->rx_ptr) { |
| 136 | *(u32 *)(xspi->rx_ptr) = data; |
| 137 | xspi->rx_ptr += 4; |
| 138 | } |
| 139 | } |
| 140 | |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 141 | static void xspi_init_hw(struct xilinx_spi *xspi) |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 142 | { |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 143 | void __iomem *regs_base = xspi->regs; |
| 144 | |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 145 | /* Reset the SPI device */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 146 | xspi->write_fn(XIPIF_V123B_RESET_MASK, |
| 147 | regs_base + XIPIF_V123B_RESETR_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 148 | /* Disable all the interrupts just in case */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 149 | xspi->write_fn(0, regs_base + XIPIF_V123B_IIER_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 150 | /* Enable the global IPIF interrupt */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 151 | xspi->write_fn(XIPIF_V123B_GINTR_ENABLE, |
| 152 | regs_base + XIPIF_V123B_DGIER_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 153 | /* Deselect the slave on the SPI bus */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 154 | xspi->write_fn(0xffff, regs_base + XSPI_SSR_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 155 | /* Disable the transmitter, enable Manual Slave Select Assertion, |
| 156 | * put SPI controller into master mode, and enable it */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 157 | xspi->write_fn(XSPI_CR_TRANS_INHIBIT | XSPI_CR_MANUAL_SSELECT | |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 158 | XSPI_CR_MASTER_MODE | XSPI_CR_ENABLE | XSPI_CR_TXFIFO_RESET | |
| 159 | XSPI_CR_RXFIFO_RESET, regs_base + XSPI_CR_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static void xilinx_spi_chipselect(struct spi_device *spi, int is_on) |
| 163 | { |
| 164 | struct xilinx_spi *xspi = spi_master_get_devdata(spi->master); |
| 165 | |
| 166 | if (is_on == BITBANG_CS_INACTIVE) { |
| 167 | /* Deselect the slave on the SPI bus */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 168 | xspi->write_fn(0xffff, xspi->regs + XSPI_SSR_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 169 | } else if (is_on == BITBANG_CS_ACTIVE) { |
| 170 | /* Set the SPI clock phase and polarity */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 171 | u16 cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET) |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 172 | & ~XSPI_CR_MODE_MASK; |
| 173 | if (spi->mode & SPI_CPHA) |
| 174 | cr |= XSPI_CR_CPHA; |
| 175 | if (spi->mode & SPI_CPOL) |
| 176 | cr |= XSPI_CR_CPOL; |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 177 | xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 178 | |
| 179 | /* We do not check spi->max_speed_hz here as the SPI clock |
| 180 | * frequency is not software programmable (the IP block design |
| 181 | * parameter) |
| 182 | */ |
| 183 | |
| 184 | /* Activate the chip select */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 185 | xspi->write_fn(~(0x0001 << spi->chip_select), |
| 186 | xspi->regs + XSPI_SSR_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | /* spi_bitbang requires custom setup_transfer() to be defined if there is a |
| 191 | * custom txrx_bufs(). We have nothing to setup here as the SPI IP block |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 192 | * supports 8 or 16 bits per word which cannot be changed in software. |
| 193 | * SPI clock can't be changed in software either. |
| 194 | * Check for correct bits per word. Chip select delay calculations could be |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 195 | * added here as soon as bitbang_work() can be made aware of the delay value. |
| 196 | */ |
| 197 | static int xilinx_spi_setup_transfer(struct spi_device *spi, |
| 198 | struct spi_transfer *t) |
| 199 | { |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 200 | struct xilinx_spi *xspi = spi_master_get_devdata(spi->master); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 201 | u8 bits_per_word; |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 202 | |
John Linn | 1a8d3b7 | 2009-09-14 08:17:05 +0000 | [diff] [blame] | 203 | bits_per_word = (t && t->bits_per_word) |
| 204 | ? t->bits_per_word : spi->bits_per_word; |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 205 | if (bits_per_word != xspi->bits_per_word) { |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 206 | dev_err(&spi->dev, "%s, unsupported bits_per_word=%d\n", |
Harvey Harrison | b687d2a | 2008-04-28 02:14:19 -0700 | [diff] [blame] | 207 | __func__, bits_per_word); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 208 | return -EINVAL; |
| 209 | } |
| 210 | |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 211 | return 0; |
| 212 | } |
| 213 | |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 214 | static int xilinx_spi_setup(struct spi_device *spi) |
| 215 | { |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 216 | /* always return 0, we can not check the number of bits. |
| 217 | * There are cases when SPI setup is called before any driver is |
| 218 | * there, in that case the SPI core defaults to 8 bits, which we |
| 219 | * do not support in some cases. But if we return an error, the |
| 220 | * SPI device would not be registered and no driver can get hold of it |
| 221 | * When the driver is there, it will call SPI setup again with the |
| 222 | * correct number of bits per transfer. |
| 223 | * If a driver setups with the wrong bit number, it will fail when |
| 224 | * it tries to do a transfer |
| 225 | */ |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | static void xilinx_spi_fill_tx_fifo(struct xilinx_spi *xspi) |
| 230 | { |
| 231 | u8 sr; |
| 232 | |
| 233 | /* Fill the Tx FIFO with as many bytes as possible */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 234 | sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 235 | while ((sr & XSPI_SR_TX_FULL_MASK) == 0 && xspi->remaining_bytes > 0) { |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 236 | if (xspi->tx_ptr) |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 237 | xspi->tx_fn(xspi); |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 238 | else |
| 239 | xspi->write_fn(0, xspi->regs + XSPI_TXD_OFFSET); |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 240 | xspi->remaining_bytes -= xspi->bits_per_word / 8; |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 241 | sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | static int xilinx_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t) |
| 246 | { |
| 247 | struct xilinx_spi *xspi = spi_master_get_devdata(spi->master); |
| 248 | u32 ipif_ier; |
| 249 | u16 cr; |
| 250 | |
| 251 | /* We get here with transmitter inhibited */ |
| 252 | |
| 253 | xspi->tx_ptr = t->tx_buf; |
| 254 | xspi->rx_ptr = t->rx_buf; |
| 255 | xspi->remaining_bytes = t->len; |
| 256 | INIT_COMPLETION(xspi->done); |
| 257 | |
| 258 | xilinx_spi_fill_tx_fifo(xspi); |
| 259 | |
| 260 | /* Enable the transmit empty interrupt, which we use to determine |
| 261 | * progress on the transmission. |
| 262 | */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 263 | ipif_ier = xspi->read_fn(xspi->regs + XIPIF_V123B_IIER_OFFSET); |
| 264 | xspi->write_fn(ipif_ier | XSPI_INTR_TX_EMPTY, |
| 265 | xspi->regs + XIPIF_V123B_IIER_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 266 | |
| 267 | /* Start the transfer by not inhibiting the transmitter any longer */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 268 | cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET) & |
| 269 | ~XSPI_CR_TRANS_INHIBIT; |
| 270 | xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 271 | |
| 272 | wait_for_completion(&xspi->done); |
| 273 | |
| 274 | /* Disable the transmit empty interrupt */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 275 | xspi->write_fn(ipif_ier, xspi->regs + XIPIF_V123B_IIER_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 276 | |
| 277 | return t->len - xspi->remaining_bytes; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | /* This driver supports single master mode only. Hence Tx FIFO Empty |
| 282 | * is the only interrupt we care about. |
| 283 | * Receive FIFO Overrun, Transmit FIFO Underrun, Mode Fault, and Slave Mode |
| 284 | * Fault are not to happen. |
| 285 | */ |
| 286 | static irqreturn_t xilinx_spi_irq(int irq, void *dev_id) |
| 287 | { |
| 288 | struct xilinx_spi *xspi = dev_id; |
| 289 | u32 ipif_isr; |
| 290 | |
| 291 | /* Get the IPIF interrupts, and clear them immediately */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 292 | ipif_isr = xspi->read_fn(xspi->regs + XIPIF_V123B_IISR_OFFSET); |
| 293 | xspi->write_fn(ipif_isr, xspi->regs + XIPIF_V123B_IISR_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 294 | |
| 295 | if (ipif_isr & XSPI_INTR_TX_EMPTY) { /* Transmission completed */ |
| 296 | u16 cr; |
| 297 | u8 sr; |
| 298 | |
| 299 | /* A transmit has just completed. Process received data and |
| 300 | * check for more data to transmit. Always inhibit the |
| 301 | * transmitter while the Isr refills the transmit register/FIFO, |
| 302 | * or make sure it is stopped if we're done. |
| 303 | */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 304 | cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET); |
| 305 | xspi->write_fn(cr | XSPI_CR_TRANS_INHIBIT, |
| 306 | xspi->regs + XSPI_CR_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 307 | |
| 308 | /* Read out all the data from the Rx FIFO */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 309 | sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 310 | while ((sr & XSPI_SR_RX_EMPTY_MASK) == 0) { |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 311 | xspi->rx_fn(xspi); |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 312 | sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /* See if there is more data to send */ |
| 316 | if (xspi->remaining_bytes > 0) { |
| 317 | xilinx_spi_fill_tx_fifo(xspi); |
| 318 | /* Start the transfer by not inhibiting the |
| 319 | * transmitter any longer |
| 320 | */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 321 | xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 322 | } else { |
| 323 | /* No more data to send. |
| 324 | * Indicate the transfer is completed. |
| 325 | */ |
| 326 | complete(&xspi->done); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | return IRQ_HANDLED; |
| 331 | } |
| 332 | |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 333 | struct spi_master *xilinx_spi_init(struct device *dev, struct resource *mem, |
| 334 | u32 irq, s16 bus_num) |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 335 | { |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 336 | struct spi_master *master; |
| 337 | struct xilinx_spi *xspi; |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 338 | struct xspi_platform_data *pdata = dev->platform_data; |
| 339 | int ret; |
John Linn | ff82c58 | 2009-01-09 16:01:53 -0700 | [diff] [blame] | 340 | |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 341 | if (!pdata) { |
| 342 | dev_err(dev, "No platform data attached\n"); |
| 343 | return NULL; |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 346 | master = spi_alloc_master(dev, sizeof(struct xilinx_spi)); |
| 347 | if (!master) |
| 348 | return NULL; |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 349 | |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 350 | /* the spi->mode bits understood by this driver: */ |
| 351 | master->mode_bits = SPI_CPOL | SPI_CPHA; |
| 352 | |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 353 | xspi = spi_master_get_devdata(master); |
| 354 | xspi->bitbang.master = spi_master_get(master); |
| 355 | xspi->bitbang.chipselect = xilinx_spi_chipselect; |
| 356 | xspi->bitbang.setup_transfer = xilinx_spi_setup_transfer; |
| 357 | xspi->bitbang.txrx_bufs = xilinx_spi_txrx_bufs; |
| 358 | xspi->bitbang.master->setup = xilinx_spi_setup; |
| 359 | init_completion(&xspi->done); |
| 360 | |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 361 | if (!request_mem_region(mem->start, resource_size(mem), |
| 362 | XILINX_SPI_NAME)) |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 363 | goto put_master; |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 364 | |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 365 | xspi->regs = ioremap(mem->start, resource_size(mem)); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 366 | if (xspi->regs == NULL) { |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 367 | dev_warn(dev, "ioremap failure\n"); |
| 368 | goto map_failed; |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 369 | } |
| 370 | |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 371 | master->bus_num = bus_num; |
| 372 | master->num_chipselect = pdata->num_chipselect; |
John Linn | ff82c58 | 2009-01-09 16:01:53 -0700 | [diff] [blame] | 373 | |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 374 | xspi->mem = *mem; |
| 375 | xspi->irq = irq; |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 376 | if (pdata->little_endian) { |
| 377 | xspi->read_fn = ioread32; |
| 378 | xspi->write_fn = iowrite32; |
| 379 | } else { |
| 380 | xspi->read_fn = ioread32be; |
| 381 | xspi->write_fn = iowrite32be; |
| 382 | } |
Richard Röjfors | c9da2e1 | 2009-11-13 12:28:55 +0100 | [diff] [blame] | 383 | xspi->bits_per_word = pdata->bits_per_word; |
| 384 | if (xspi->bits_per_word == 8) { |
| 385 | xspi->tx_fn = xspi_tx8; |
| 386 | xspi->rx_fn = xspi_rx8; |
| 387 | } else if (xspi->bits_per_word == 16) { |
| 388 | xspi->tx_fn = xspi_tx16; |
| 389 | xspi->rx_fn = xspi_rx16; |
| 390 | } else if (xspi->bits_per_word == 32) { |
| 391 | xspi->tx_fn = xspi_tx32; |
| 392 | xspi->rx_fn = xspi_rx32; |
| 393 | } else |
| 394 | goto unmap_io; |
| 395 | |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 396 | |
| 397 | /* SPI controller initializations */ |
Richard Röjfors | 86fc593 | 2009-11-13 12:28:49 +0100 | [diff] [blame] | 398 | xspi_init_hw(xspi); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 399 | |
| 400 | /* Register for SPI Interrupt */ |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 401 | ret = request_irq(xspi->irq, xilinx_spi_irq, 0, XILINX_SPI_NAME, xspi); |
| 402 | if (ret) |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 403 | goto unmap_io; |
| 404 | |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 405 | ret = spi_bitbang_start(&xspi->bitbang); |
| 406 | if (ret) { |
| 407 | dev_err(dev, "spi_bitbang_start FAILED\n"); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 408 | goto free_irq; |
| 409 | } |
| 410 | |
Grant Likely | 920712a | 2009-11-25 07:23:35 -0700 | [diff] [blame] | 411 | dev_info(dev, "at 0x%08llX mapped to 0x%p, irq=%d\n", |
| 412 | (unsigned long long)mem->start, xspi->regs, xspi->irq); |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 413 | return master; |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 414 | |
| 415 | free_irq: |
| 416 | free_irq(xspi->irq, xspi); |
| 417 | unmap_io: |
| 418 | iounmap(xspi->regs); |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 419 | map_failed: |
| 420 | release_mem_region(mem->start, resource_size(mem)); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 421 | put_master: |
| 422 | spi_master_put(master); |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 423 | return NULL; |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 424 | } |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 425 | EXPORT_SYMBOL(xilinx_spi_init); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 426 | |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 427 | void xilinx_spi_deinit(struct spi_master *master) |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 428 | { |
| 429 | struct xilinx_spi *xspi; |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 430 | |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 431 | xspi = spi_master_get_devdata(master); |
| 432 | |
| 433 | spi_bitbang_stop(&xspi->bitbang); |
| 434 | free_irq(xspi->irq, xspi); |
| 435 | iounmap(xspi->regs); |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 436 | |
| 437 | release_mem_region(xspi->mem.start, resource_size(&xspi->mem)); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 438 | spi_master_put(xspi->bitbang.master); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 439 | } |
Richard Röjfors | d5af91a | 2009-11-13 12:28:39 +0100 | [diff] [blame] | 440 | EXPORT_SYMBOL(xilinx_spi_deinit); |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 441 | |
Andrei Konovalov | ae918c0 | 2007-07-17 04:04:11 -0700 | [diff] [blame] | 442 | MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>"); |
| 443 | MODULE_DESCRIPTION("Xilinx SPI driver"); |
| 444 | MODULE_LICENSE("GPL"); |