blob: 9f386379c16974285c48c48932075d4025471b90 [file] [log] [blame]
Andrei Konovalovae918c02007-07-17 04:04:11 -07001/*
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 Linnff82c582009-01-09 16:01:53 -070017
Andrei Konovalovae918c02007-07-17 04:04:11 -070018#include <linux/spi/spi.h>
19#include <linux/spi/spi_bitbang.h>
20#include <linux/io.h>
21
Richard Röjforsd5af91a2009-11-13 12:28:39 +010022#include "xilinx_spi.h"
23#include <linux/spi/xilinx_spi.h>
24
David Brownellfc3ba952007-08-30 23:56:24 -070025#define XILINX_SPI_NAME "xilinx_spi"
Andrei Konovalovae918c02007-07-17 04:04:11 -070026
27/* Register definitions as per "OPB Serial Peripheral Interface (SPI) (v1.00e)
28 * Product Specification", DS464
29 */
Richard Röjforsc9da2e12009-11-13 12:28:55 +010030#define XSPI_CR_OFFSET 0x60 /* Control Register */
Andrei Konovalovae918c02007-07-17 04:04:11 -070031
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öjforsc9da2e12009-11-13 12:28:55 +010041#define XSPI_CR_LSB_FIRST 0x200
Andrei Konovalovae918c02007-07-17 04:04:11 -070042
Richard Röjforsc9da2e12009-11-13 12:28:55 +010043#define XSPI_SR_OFFSET 0x64 /* Status Register */
Andrei Konovalovae918c02007-07-17 04:04:11 -070044
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öjforsc9da2e12009-11-13 12:28:55 +010051#define XSPI_TXD_OFFSET 0x68 /* Data Transmit Register */
52#define XSPI_RXD_OFFSET 0x6c /* Data Receive Register */
Andrei Konovalovae918c02007-07-17 04:04:11 -070053
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öjforsc9da2e12009-11-13 12:28:55 +010072#define XSPI_INTR_TX_HALF_EMPTY 0x40 /* TxFIFO is half empty */
Andrei Konovalovae918c02007-07-17 04:04:11 -070073
74#define XIPIF_V123B_RESETR_OFFSET 0x40 /* IPIF reset register */
75#define XIPIF_V123B_RESET_MASK 0x0a /* the value to write */
76
77struct xilinx_spi {
78 /* bitbang has to be first */
79 struct spi_bitbang bitbang;
80 struct completion done;
Richard Röjforsd5af91a2009-11-13 12:28:39 +010081 struct resource mem; /* phys mem */
Andrei Konovalovae918c02007-07-17 04:04:11 -070082 void __iomem *regs; /* virt. address of the control registers */
83
84 u32 irq;
85
Andrei Konovalovae918c02007-07-17 04:04:11 -070086 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öjforsc9da2e12009-11-13 12:28:55 +010089 u8 bits_per_word;
Richard Röjfors86fc5932009-11-13 12:28:49 +010090 unsigned int (*read_fn) (void __iomem *);
91 void (*write_fn) (u32, void __iomem *);
Richard Röjforsc9da2e12009-11-13 12:28:55 +010092 void (*tx_fn) (struct xilinx_spi *);
93 void (*rx_fn) (struct xilinx_spi *);
Andrei Konovalovae918c02007-07-17 04:04:11 -070094};
95
Richard Röjforsc9da2e12009-11-13 12:28:55 +010096static 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
102static 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
108static 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
114static 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
123static 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
132static 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öjfors86fc5932009-11-13 12:28:49 +0100141static void xspi_init_hw(struct xilinx_spi *xspi)
Andrei Konovalovae918c02007-07-17 04:04:11 -0700142{
Richard Röjfors86fc5932009-11-13 12:28:49 +0100143 void __iomem *regs_base = xspi->regs;
144
Andrei Konovalovae918c02007-07-17 04:04:11 -0700145 /* Reset the SPI device */
Richard Röjfors86fc5932009-11-13 12:28:49 +0100146 xspi->write_fn(XIPIF_V123B_RESET_MASK,
147 regs_base + XIPIF_V123B_RESETR_OFFSET);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700148 /* Disable all the interrupts just in case */
Richard Röjfors86fc5932009-11-13 12:28:49 +0100149 xspi->write_fn(0, regs_base + XIPIF_V123B_IIER_OFFSET);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700150 /* Enable the global IPIF interrupt */
Richard Röjfors86fc5932009-11-13 12:28:49 +0100151 xspi->write_fn(XIPIF_V123B_GINTR_ENABLE,
152 regs_base + XIPIF_V123B_DGIER_OFFSET);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700153 /* Deselect the slave on the SPI bus */
Richard Röjfors86fc5932009-11-13 12:28:49 +0100154 xspi->write_fn(0xffff, regs_base + XSPI_SSR_OFFSET);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700155 /* Disable the transmitter, enable Manual Slave Select Assertion,
156 * put SPI controller into master mode, and enable it */
Richard Röjfors86fc5932009-11-13 12:28:49 +0100157 xspi->write_fn(XSPI_CR_TRANS_INHIBIT | XSPI_CR_MANUAL_SSELECT |
Richard Röjforsc9da2e12009-11-13 12:28:55 +0100158 XSPI_CR_MASTER_MODE | XSPI_CR_ENABLE | XSPI_CR_TXFIFO_RESET |
159 XSPI_CR_RXFIFO_RESET, regs_base + XSPI_CR_OFFSET);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700160}
161
162static 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öjfors86fc5932009-11-13 12:28:49 +0100168 xspi->write_fn(0xffff, xspi->regs + XSPI_SSR_OFFSET);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700169 } else if (is_on == BITBANG_CS_ACTIVE) {
170 /* Set the SPI clock phase and polarity */
Richard Röjfors86fc5932009-11-13 12:28:49 +0100171 u16 cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET)
Andrei Konovalovae918c02007-07-17 04:04:11 -0700172 & ~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öjfors86fc5932009-11-13 12:28:49 +0100177 xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700178
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öjfors86fc5932009-11-13 12:28:49 +0100185 xspi->write_fn(~(0x0001 << spi->chip_select),
186 xspi->regs + XSPI_SSR_OFFSET);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700187 }
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öjforsc9da2e12009-11-13 12:28:55 +0100192 * 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 Konovalovae918c02007-07-17 04:04:11 -0700195 * added here as soon as bitbang_work() can be made aware of the delay value.
196 */
197static int xilinx_spi_setup_transfer(struct spi_device *spi,
198 struct spi_transfer *t)
199{
Richard Röjforsc9da2e12009-11-13 12:28:55 +0100200 struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700201 u8 bits_per_word;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700202
John Linn1a8d3b72009-09-14 08:17:05 +0000203 bits_per_word = (t && t->bits_per_word)
204 ? t->bits_per_word : spi->bits_per_word;
Richard Röjforsc9da2e12009-11-13 12:28:55 +0100205 if (bits_per_word != xspi->bits_per_word) {
Andrei Konovalovae918c02007-07-17 04:04:11 -0700206 dev_err(&spi->dev, "%s, unsupported bits_per_word=%d\n",
Harvey Harrisonb687d2a2008-04-28 02:14:19 -0700207 __func__, bits_per_word);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700208 return -EINVAL;
209 }
210
Andrei Konovalovae918c02007-07-17 04:04:11 -0700211 return 0;
212}
213
Andrei Konovalovae918c02007-07-17 04:04:11 -0700214static int xilinx_spi_setup(struct spi_device *spi)
215{
Richard Röjforsc9da2e12009-11-13 12:28:55 +0100216 /* 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 Konovalovae918c02007-07-17 04:04:11 -0700226 return 0;
227}
228
229static 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öjfors86fc5932009-11-13 12:28:49 +0100234 sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700235 while ((sr & XSPI_SR_TX_FULL_MASK) == 0 && xspi->remaining_bytes > 0) {
Richard Röjfors86fc5932009-11-13 12:28:49 +0100236 if (xspi->tx_ptr)
Richard Röjforsc9da2e12009-11-13 12:28:55 +0100237 xspi->tx_fn(xspi);
Richard Röjfors86fc5932009-11-13 12:28:49 +0100238 else
239 xspi->write_fn(0, xspi->regs + XSPI_TXD_OFFSET);
Richard Röjforsc9da2e12009-11-13 12:28:55 +0100240 xspi->remaining_bytes -= xspi->bits_per_word / 8;
Richard Röjfors86fc5932009-11-13 12:28:49 +0100241 sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700242 }
243}
244
245static 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öjfors86fc5932009-11-13 12:28:49 +0100263 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 Konovalovae918c02007-07-17 04:04:11 -0700266
267 /* Start the transfer by not inhibiting the transmitter any longer */
Richard Röjfors86fc5932009-11-13 12:28:49 +0100268 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 Konovalovae918c02007-07-17 04:04:11 -0700271
272 wait_for_completion(&xspi->done);
273
274 /* Disable the transmit empty interrupt */
Richard Röjfors86fc5932009-11-13 12:28:49 +0100275 xspi->write_fn(ipif_ier, xspi->regs + XIPIF_V123B_IIER_OFFSET);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700276
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 */
286static 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öjfors86fc5932009-11-13 12:28:49 +0100292 ipif_isr = xspi->read_fn(xspi->regs + XIPIF_V123B_IISR_OFFSET);
293 xspi->write_fn(ipif_isr, xspi->regs + XIPIF_V123B_IISR_OFFSET);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700294
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öjfors86fc5932009-11-13 12:28:49 +0100304 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 Konovalovae918c02007-07-17 04:04:11 -0700307
308 /* Read out all the data from the Rx FIFO */
Richard Röjfors86fc5932009-11-13 12:28:49 +0100309 sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700310 while ((sr & XSPI_SR_RX_EMPTY_MASK) == 0) {
Richard Röjforsc9da2e12009-11-13 12:28:55 +0100311 xspi->rx_fn(xspi);
Richard Röjfors86fc5932009-11-13 12:28:49 +0100312 sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700313 }
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öjfors86fc5932009-11-13 12:28:49 +0100321 xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700322 } 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öjforsd5af91a2009-11-13 12:28:39 +0100333struct spi_master *xilinx_spi_init(struct device *dev, struct resource *mem,
334 u32 irq, s16 bus_num)
Andrei Konovalovae918c02007-07-17 04:04:11 -0700335{
Andrei Konovalovae918c02007-07-17 04:04:11 -0700336 struct spi_master *master;
337 struct xilinx_spi *xspi;
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100338 struct xspi_platform_data *pdata = dev->platform_data;
339 int ret;
John Linnff82c582009-01-09 16:01:53 -0700340
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100341 if (!pdata) {
342 dev_err(dev, "No platform data attached\n");
343 return NULL;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700344 }
345
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100346 master = spi_alloc_master(dev, sizeof(struct xilinx_spi));
347 if (!master)
348 return NULL;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700349
David Brownelle7db06b2009-06-17 16:26:04 -0700350 /* the spi->mode bits understood by this driver: */
351 master->mode_bits = SPI_CPOL | SPI_CPHA;
352
Andrei Konovalovae918c02007-07-17 04:04:11 -0700353 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öjforsd5af91a2009-11-13 12:28:39 +0100361 if (!request_mem_region(mem->start, resource_size(mem),
362 XILINX_SPI_NAME))
Andrei Konovalovae918c02007-07-17 04:04:11 -0700363 goto put_master;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700364
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100365 xspi->regs = ioremap(mem->start, resource_size(mem));
Andrei Konovalovae918c02007-07-17 04:04:11 -0700366 if (xspi->regs == NULL) {
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100367 dev_warn(dev, "ioremap failure\n");
368 goto map_failed;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700369 }
370
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100371 master->bus_num = bus_num;
372 master->num_chipselect = pdata->num_chipselect;
John Linnff82c582009-01-09 16:01:53 -0700373
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100374 xspi->mem = *mem;
375 xspi->irq = irq;
Richard Röjfors86fc5932009-11-13 12:28:49 +0100376 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öjforsc9da2e12009-11-13 12:28:55 +0100383 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 Konovalovae918c02007-07-17 04:04:11 -0700396
397 /* SPI controller initializations */
Richard Röjfors86fc5932009-11-13 12:28:49 +0100398 xspi_init_hw(xspi);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700399
400 /* Register for SPI Interrupt */
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100401 ret = request_irq(xspi->irq, xilinx_spi_irq, 0, XILINX_SPI_NAME, xspi);
402 if (ret)
Andrei Konovalovae918c02007-07-17 04:04:11 -0700403 goto unmap_io;
404
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100405 ret = spi_bitbang_start(&xspi->bitbang);
406 if (ret) {
407 dev_err(dev, "spi_bitbang_start FAILED\n");
Andrei Konovalovae918c02007-07-17 04:04:11 -0700408 goto free_irq;
409 }
410
Grant Likely920712a2009-11-25 07:23:35 -0700411 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öjforsd5af91a2009-11-13 12:28:39 +0100413 return master;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700414
415free_irq:
416 free_irq(xspi->irq, xspi);
417unmap_io:
418 iounmap(xspi->regs);
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100419map_failed:
420 release_mem_region(mem->start, resource_size(mem));
Andrei Konovalovae918c02007-07-17 04:04:11 -0700421put_master:
422 spi_master_put(master);
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100423 return NULL;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700424}
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100425EXPORT_SYMBOL(xilinx_spi_init);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700426
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100427void xilinx_spi_deinit(struct spi_master *master)
Andrei Konovalovae918c02007-07-17 04:04:11 -0700428{
429 struct xilinx_spi *xspi;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700430
Andrei Konovalovae918c02007-07-17 04:04:11 -0700431 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öjforsd5af91a2009-11-13 12:28:39 +0100436
437 release_mem_region(xspi->mem.start, resource_size(&xspi->mem));
Andrei Konovalovae918c02007-07-17 04:04:11 -0700438 spi_master_put(xspi->bitbang.master);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700439}
Richard Röjforsd5af91a2009-11-13 12:28:39 +0100440EXPORT_SYMBOL(xilinx_spi_deinit);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700441
Andrei Konovalovae918c02007-07-17 04:04:11 -0700442MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
443MODULE_DESCRIPTION("Xilinx SPI driver");
444MODULE_LICENSE("GPL");