aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/spi/xilinx_spi.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/spi/xilinx_spi.c')
-rw-r--r--drivers/spi/xilinx_spi.c378
1 files changed, 193 insertions, 185 deletions
diff --git a/drivers/spi/xilinx_spi.c b/drivers/spi/xilinx_spi.c
index 46b8c5c2f45e..1b47363cb73f 100644
--- a/drivers/spi/xilinx_spi.c
+++ b/drivers/spi/xilinx_spi.c
@@ -14,22 +14,20 @@
14#include <linux/module.h> 14#include <linux/module.h>
15#include <linux/init.h> 15#include <linux/init.h>
16#include <linux/interrupt.h> 16#include <linux/interrupt.h>
17#include <linux/platform_device.h>
18
19#include <linux/of_platform.h>
20#include <linux/of_device.h>
21#include <linux/of_spi.h>
22 17
23#include <linux/spi/spi.h> 18#include <linux/spi/spi.h>
24#include <linux/spi/spi_bitbang.h> 19#include <linux/spi/spi_bitbang.h>
25#include <linux/io.h> 20#include <linux/io.h>
26 21
22#include "xilinx_spi.h"
23#include <linux/spi/xilinx_spi.h>
24
27#define XILINX_SPI_NAME "xilinx_spi" 25#define XILINX_SPI_NAME "xilinx_spi"
28 26
29/* Register definitions as per "OPB Serial Peripheral Interface (SPI) (v1.00e) 27/* Register definitions as per "OPB Serial Peripheral Interface (SPI) (v1.00e)
30 * Product Specification", DS464 28 * Product Specification", DS464
31 */ 29 */
32#define XSPI_CR_OFFSET 0x62 /* 16-bit Control Register */ 30#define XSPI_CR_OFFSET 0x60 /* Control Register */
33 31
34#define XSPI_CR_ENABLE 0x02 32#define XSPI_CR_ENABLE 0x02
35#define XSPI_CR_MASTER_MODE 0x04 33#define XSPI_CR_MASTER_MODE 0x04
@@ -40,8 +38,9 @@
40#define XSPI_CR_RXFIFO_RESET 0x40 38#define XSPI_CR_RXFIFO_RESET 0x40
41#define XSPI_CR_MANUAL_SSELECT 0x80 39#define XSPI_CR_MANUAL_SSELECT 0x80
42#define XSPI_CR_TRANS_INHIBIT 0x100 40#define XSPI_CR_TRANS_INHIBIT 0x100
41#define XSPI_CR_LSB_FIRST 0x200
43 42
44#define XSPI_SR_OFFSET 0x67 /* 8-bit Status Register */ 43#define XSPI_SR_OFFSET 0x64 /* Status Register */
45 44
46#define XSPI_SR_RX_EMPTY_MASK 0x01 /* Receive FIFO is empty */ 45#define XSPI_SR_RX_EMPTY_MASK 0x01 /* Receive FIFO is empty */
47#define XSPI_SR_RX_FULL_MASK 0x02 /* Receive FIFO is full */ 46#define XSPI_SR_RX_FULL_MASK 0x02 /* Receive FIFO is full */
@@ -49,8 +48,8 @@
49#define XSPI_SR_TX_FULL_MASK 0x08 /* Transmit FIFO is full */ 48#define XSPI_SR_TX_FULL_MASK 0x08 /* Transmit FIFO is full */
50#define XSPI_SR_MODE_FAULT_MASK 0x10 /* Mode fault error */ 49#define XSPI_SR_MODE_FAULT_MASK 0x10 /* Mode fault error */
51 50
52#define XSPI_TXD_OFFSET 0x6b /* 8-bit Data Transmit Register */ 51#define XSPI_TXD_OFFSET 0x68 /* Data Transmit Register */
53#define XSPI_RXD_OFFSET 0x6f /* 8-bit Data Receive Register */ 52#define XSPI_RXD_OFFSET 0x6c /* Data Receive Register */
54 53
55#define XSPI_SSR_OFFSET 0x70 /* 32-bit Slave Select Register */ 54#define XSPI_SSR_OFFSET 0x70 /* 32-bit Slave Select Register */
56 55
@@ -70,6 +69,7 @@
70#define XSPI_INTR_TX_UNDERRUN 0x08 /* TxFIFO was underrun */ 69#define XSPI_INTR_TX_UNDERRUN 0x08 /* TxFIFO was underrun */
71#define XSPI_INTR_RX_FULL 0x10 /* RxFIFO is full */ 70#define XSPI_INTR_RX_FULL 0x10 /* RxFIFO is full */
72#define XSPI_INTR_RX_OVERRUN 0x20 /* RxFIFO was overrun */ 71#define XSPI_INTR_RX_OVERRUN 0x20 /* RxFIFO was overrun */
72#define XSPI_INTR_TX_HALF_EMPTY 0x40 /* TxFIFO is half empty */
73 73
74#define XIPIF_V123B_RESETR_OFFSET 0x40 /* IPIF reset register */ 74#define XIPIF_V123B_RESETR_OFFSET 0x40 /* IPIF reset register */
75#define XIPIF_V123B_RESET_MASK 0x0a /* the value to write */ 75#define XIPIF_V123B_RESET_MASK 0x0a /* the value to write */
@@ -78,35 +78,105 @@ struct xilinx_spi {
78 /* bitbang has to be first */ 78 /* bitbang has to be first */
79 struct spi_bitbang bitbang; 79 struct spi_bitbang bitbang;
80 struct completion done; 80 struct completion done;
81 81 struct resource mem; /* phys mem */
82 void __iomem *regs; /* virt. address of the control registers */ 82 void __iomem *regs; /* virt. address of the control registers */
83 83
84 u32 irq; 84 u32 irq;
85 85
86 u32 speed_hz; /* SCK has a fixed frequency of speed_hz Hz */
87
88 u8 *rx_ptr; /* pointer in the Tx buffer */ 86 u8 *rx_ptr; /* pointer in the Tx buffer */
89 const u8 *tx_ptr; /* pointer in the Rx buffer */ 87 const u8 *tx_ptr; /* pointer in the Rx buffer */
90 int remaining_bytes; /* the number of bytes left to transfer */ 88 int remaining_bytes; /* the number of bytes left to transfer */
89 u8 bits_per_word;
90 unsigned int (*read_fn) (void __iomem *);
91 void (*write_fn) (u32, void __iomem *);
92 void (*tx_fn) (struct xilinx_spi *);
93 void (*rx_fn) (struct xilinx_spi *);
91}; 94};
92 95
93static void xspi_init_hw(void __iomem *regs_base) 96static void xspi_write32(u32 val, void __iomem *addr)
97{
98 iowrite32(val, addr);
99}
100
101static unsigned int xspi_read32(void __iomem *addr)
102{
103 return ioread32(addr);
104}
105
106static void xspi_write32_be(u32 val, void __iomem *addr)
94{ 107{
108 iowrite32be(val, addr);
109}
110
111static unsigned int xspi_read32_be(void __iomem *addr)
112{
113 return ioread32be(addr);
114}
115
116static void xspi_tx8(struct xilinx_spi *xspi)
117{
118 xspi->write_fn(*xspi->tx_ptr, xspi->regs + XSPI_TXD_OFFSET);
119 xspi->tx_ptr++;
120}
121
122static void xspi_tx16(struct xilinx_spi *xspi)
123{
124 xspi->write_fn(*(u16 *)(xspi->tx_ptr), xspi->regs + XSPI_TXD_OFFSET);
125 xspi->tx_ptr += 2;
126}
127
128static void xspi_tx32(struct xilinx_spi *xspi)
129{
130 xspi->write_fn(*(u32 *)(xspi->tx_ptr), xspi->regs + XSPI_TXD_OFFSET);
131 xspi->tx_ptr += 4;
132}
133
134static void xspi_rx8(struct xilinx_spi *xspi)
135{
136 u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET);
137 if (xspi->rx_ptr) {
138 *xspi->rx_ptr = data & 0xff;
139 xspi->rx_ptr++;
140 }
141}
142
143static void xspi_rx16(struct xilinx_spi *xspi)
144{
145 u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET);
146 if (xspi->rx_ptr) {
147 *(u16 *)(xspi->rx_ptr) = data & 0xffff;
148 xspi->rx_ptr += 2;
149 }
150}
151
152static void xspi_rx32(struct xilinx_spi *xspi)
153{
154 u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET);
155 if (xspi->rx_ptr) {
156 *(u32 *)(xspi->rx_ptr) = data;
157 xspi->rx_ptr += 4;
158 }
159}
160
161static void xspi_init_hw(struct xilinx_spi *xspi)
162{
163 void __iomem *regs_base = xspi->regs;
164
95 /* Reset the SPI device */ 165 /* Reset the SPI device */
96 out_be32(regs_base + XIPIF_V123B_RESETR_OFFSET, 166 xspi->write_fn(XIPIF_V123B_RESET_MASK,
97 XIPIF_V123B_RESET_MASK); 167 regs_base + XIPIF_V123B_RESETR_OFFSET);
98 /* Disable all the interrupts just in case */ 168 /* Disable all the interrupts just in case */
99 out_be32(regs_base + XIPIF_V123B_IIER_OFFSET, 0); 169 xspi->write_fn(0, regs_base + XIPIF_V123B_IIER_OFFSET);
100 /* Enable the global IPIF interrupt */ 170 /* Enable the global IPIF interrupt */
101 out_be32(regs_base + XIPIF_V123B_DGIER_OFFSET, 171 xspi->write_fn(XIPIF_V123B_GINTR_ENABLE,
102 XIPIF_V123B_GINTR_ENABLE); 172 regs_base + XIPIF_V123B_DGIER_OFFSET);
103 /* Deselect the slave on the SPI bus */ 173 /* Deselect the slave on the SPI bus */
104 out_be32(regs_base + XSPI_SSR_OFFSET, 0xffff); 174 xspi->write_fn(0xffff, regs_base + XSPI_SSR_OFFSET);
105 /* Disable the transmitter, enable Manual Slave Select Assertion, 175 /* Disable the transmitter, enable Manual Slave Select Assertion,
106 * put SPI controller into master mode, and enable it */ 176 * put SPI controller into master mode, and enable it */
107 out_be16(regs_base + XSPI_CR_OFFSET, 177 xspi->write_fn(XSPI_CR_TRANS_INHIBIT | XSPI_CR_MANUAL_SSELECT |
108 XSPI_CR_TRANS_INHIBIT | XSPI_CR_MANUAL_SSELECT 178 XSPI_CR_MASTER_MODE | XSPI_CR_ENABLE | XSPI_CR_TXFIFO_RESET |
109 | XSPI_CR_MASTER_MODE | XSPI_CR_ENABLE); 179 XSPI_CR_RXFIFO_RESET, regs_base + XSPI_CR_OFFSET);
110} 180}
111 181
112static void xilinx_spi_chipselect(struct spi_device *spi, int is_on) 182static void xilinx_spi_chipselect(struct spi_device *spi, int is_on)
@@ -115,16 +185,16 @@ static void xilinx_spi_chipselect(struct spi_device *spi, int is_on)
115 185
116 if (is_on == BITBANG_CS_INACTIVE) { 186 if (is_on == BITBANG_CS_INACTIVE) {
117 /* Deselect the slave on the SPI bus */ 187 /* Deselect the slave on the SPI bus */
118 out_be32(xspi->regs + XSPI_SSR_OFFSET, 0xffff); 188 xspi->write_fn(0xffff, xspi->regs + XSPI_SSR_OFFSET);
119 } else if (is_on == BITBANG_CS_ACTIVE) { 189 } else if (is_on == BITBANG_CS_ACTIVE) {
120 /* Set the SPI clock phase and polarity */ 190 /* Set the SPI clock phase and polarity */
121 u16 cr = in_be16(xspi->regs + XSPI_CR_OFFSET) 191 u16 cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET)
122 & ~XSPI_CR_MODE_MASK; 192 & ~XSPI_CR_MODE_MASK;
123 if (spi->mode & SPI_CPHA) 193 if (spi->mode & SPI_CPHA)
124 cr |= XSPI_CR_CPHA; 194 cr |= XSPI_CR_CPHA;
125 if (spi->mode & SPI_CPOL) 195 if (spi->mode & SPI_CPOL)
126 cr |= XSPI_CR_CPOL; 196 cr |= XSPI_CR_CPOL;
127 out_be16(xspi->regs + XSPI_CR_OFFSET, cr); 197 xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
128 198
129 /* We do not check spi->max_speed_hz here as the SPI clock 199 /* We do not check spi->max_speed_hz here as the SPI clock
130 * frequency is not software programmable (the IP block design 200 * frequency is not software programmable (the IP block design
@@ -132,24 +202,27 @@ static void xilinx_spi_chipselect(struct spi_device *spi, int is_on)
132 */ 202 */
133 203
134 /* Activate the chip select */ 204 /* Activate the chip select */
135 out_be32(xspi->regs + XSPI_SSR_OFFSET, 205 xspi->write_fn(~(0x0001 << spi->chip_select),
136 ~(0x0001 << spi->chip_select)); 206 xspi->regs + XSPI_SSR_OFFSET);
137 } 207 }
138} 208}
139 209
140/* spi_bitbang requires custom setup_transfer() to be defined if there is a 210/* spi_bitbang requires custom setup_transfer() to be defined if there is a
141 * custom txrx_bufs(). We have nothing to setup here as the SPI IP block 211 * custom txrx_bufs(). We have nothing to setup here as the SPI IP block
142 * supports just 8 bits per word, and SPI clock can't be changed in software. 212 * supports 8 or 16 bits per word which cannot be changed in software.
143 * Check for 8 bits per word. Chip select delay calculations could be 213 * SPI clock can't be changed in software either.
214 * Check for correct bits per word. Chip select delay calculations could be
144 * added here as soon as bitbang_work() can be made aware of the delay value. 215 * added here as soon as bitbang_work() can be made aware of the delay value.
145 */ 216 */
146static int xilinx_spi_setup_transfer(struct spi_device *spi, 217static int xilinx_spi_setup_transfer(struct spi_device *spi,
147 struct spi_transfer *t) 218 struct spi_transfer *t)
148{ 219{
220 struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
149 u8 bits_per_word; 221 u8 bits_per_word;
150 222
151 bits_per_word = (t) ? t->bits_per_word : spi->bits_per_word; 223 bits_per_word = (t && t->bits_per_word)
152 if (bits_per_word != 8) { 224 ? t->bits_per_word : spi->bits_per_word;
225 if (bits_per_word != xspi->bits_per_word) {
153 dev_err(&spi->dev, "%s, unsupported bits_per_word=%d\n", 226 dev_err(&spi->dev, "%s, unsupported bits_per_word=%d\n",
154 __func__, bits_per_word); 227 __func__, bits_per_word);
155 return -EINVAL; 228 return -EINVAL;
@@ -160,17 +233,16 @@ static int xilinx_spi_setup_transfer(struct spi_device *spi,
160 233
161static int xilinx_spi_setup(struct spi_device *spi) 234static int xilinx_spi_setup(struct spi_device *spi)
162{ 235{
163 struct spi_bitbang *bitbang; 236 /* always return 0, we can not check the number of bits.
164 struct xilinx_spi *xspi; 237 * There are cases when SPI setup is called before any driver is
165 int retval; 238 * there, in that case the SPI core defaults to 8 bits, which we
166 239 * do not support in some cases. But if we return an error, the
167 xspi = spi_master_get_devdata(spi->master); 240 * SPI device would not be registered and no driver can get hold of it
168 bitbang = &xspi->bitbang; 241 * When the driver is there, it will call SPI setup again with the
169 242 * correct number of bits per transfer.
170 retval = xilinx_spi_setup_transfer(spi, NULL); 243 * If a driver setups with the wrong bit number, it will fail when
171 if (retval < 0) 244 * it tries to do a transfer
172 return retval; 245 */
173
174 return 0; 246 return 0;
175} 247}
176 248
@@ -179,15 +251,14 @@ static void xilinx_spi_fill_tx_fifo(struct xilinx_spi *xspi)
179 u8 sr; 251 u8 sr;
180 252
181 /* Fill the Tx FIFO with as many bytes as possible */ 253 /* Fill the Tx FIFO with as many bytes as possible */
182 sr = in_8(xspi->regs + XSPI_SR_OFFSET); 254 sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
183 while ((sr & XSPI_SR_TX_FULL_MASK) == 0 && xspi->remaining_bytes > 0) { 255 while ((sr & XSPI_SR_TX_FULL_MASK) == 0 && xspi->remaining_bytes > 0) {
184 if (xspi->tx_ptr) { 256 if (xspi->tx_ptr)
185 out_8(xspi->regs + XSPI_TXD_OFFSET, *xspi->tx_ptr++); 257 xspi->tx_fn(xspi);
186 } else { 258 else
187 out_8(xspi->regs + XSPI_TXD_OFFSET, 0); 259 xspi->write_fn(0, xspi->regs + XSPI_TXD_OFFSET);
188 } 260 xspi->remaining_bytes -= xspi->bits_per_word / 8;
189 xspi->remaining_bytes--; 261 sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
190 sr = in_8(xspi->regs + XSPI_SR_OFFSET);
191 } 262 }
192} 263}
193 264
@@ -209,18 +280,19 @@ static int xilinx_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
209 /* Enable the transmit empty interrupt, which we use to determine 280 /* Enable the transmit empty interrupt, which we use to determine
210 * progress on the transmission. 281 * progress on the transmission.
211 */ 282 */
212 ipif_ier = in_be32(xspi->regs + XIPIF_V123B_IIER_OFFSET); 283 ipif_ier = xspi->read_fn(xspi->regs + XIPIF_V123B_IIER_OFFSET);
213 out_be32(xspi->regs + XIPIF_V123B_IIER_OFFSET, 284 xspi->write_fn(ipif_ier | XSPI_INTR_TX_EMPTY,
214 ipif_ier | XSPI_INTR_TX_EMPTY); 285 xspi->regs + XIPIF_V123B_IIER_OFFSET);
215 286
216 /* Start the transfer by not inhibiting the transmitter any longer */ 287 /* Start the transfer by not inhibiting the transmitter any longer */
217 cr = in_be16(xspi->regs + XSPI_CR_OFFSET) & ~XSPI_CR_TRANS_INHIBIT; 288 cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET) &
218 out_be16(xspi->regs + XSPI_CR_OFFSET, cr); 289 ~XSPI_CR_TRANS_INHIBIT;
290 xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
219 291
220 wait_for_completion(&xspi->done); 292 wait_for_completion(&xspi->done);
221 293
222 /* Disable the transmit empty interrupt */ 294 /* Disable the transmit empty interrupt */
223 out_be32(xspi->regs + XIPIF_V123B_IIER_OFFSET, ipif_ier); 295 xspi->write_fn(ipif_ier, xspi->regs + XIPIF_V123B_IIER_OFFSET);
224 296
225 return t->len - xspi->remaining_bytes; 297 return t->len - xspi->remaining_bytes;
226} 298}
@@ -237,8 +309,8 @@ static irqreturn_t xilinx_spi_irq(int irq, void *dev_id)
237 u32 ipif_isr; 309 u32 ipif_isr;
238 310
239 /* Get the IPIF interrupts, and clear them immediately */ 311 /* Get the IPIF interrupts, and clear them immediately */
240 ipif_isr = in_be32(xspi->regs + XIPIF_V123B_IISR_OFFSET); 312 ipif_isr = xspi->read_fn(xspi->regs + XIPIF_V123B_IISR_OFFSET);
241 out_be32(xspi->regs + XIPIF_V123B_IISR_OFFSET, ipif_isr); 313 xspi->write_fn(ipif_isr, xspi->regs + XIPIF_V123B_IISR_OFFSET);
242 314
243 if (ipif_isr & XSPI_INTR_TX_EMPTY) { /* Transmission completed */ 315 if (ipif_isr & XSPI_INTR_TX_EMPTY) { /* Transmission completed */
244 u16 cr; 316 u16 cr;
@@ -249,20 +321,15 @@ static irqreturn_t xilinx_spi_irq(int irq, void *dev_id)
249 * transmitter while the Isr refills the transmit register/FIFO, 321 * transmitter while the Isr refills the transmit register/FIFO,
250 * or make sure it is stopped if we're done. 322 * or make sure it is stopped if we're done.
251 */ 323 */
252 cr = in_be16(xspi->regs + XSPI_CR_OFFSET); 324 cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET);
253 out_be16(xspi->regs + XSPI_CR_OFFSET, 325 xspi->write_fn(cr | XSPI_CR_TRANS_INHIBIT,
254 cr | XSPI_CR_TRANS_INHIBIT); 326 xspi->regs + XSPI_CR_OFFSET);
255 327
256 /* Read out all the data from the Rx FIFO */ 328 /* Read out all the data from the Rx FIFO */
257 sr = in_8(xspi->regs + XSPI_SR_OFFSET); 329 sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
258 while ((sr & XSPI_SR_RX_EMPTY_MASK) == 0) { 330 while ((sr & XSPI_SR_RX_EMPTY_MASK) == 0) {
259 u8 data; 331 xspi->rx_fn(xspi);
260 332 sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
261 data = in_8(xspi->regs + XSPI_RXD_OFFSET);
262 if (xspi->rx_ptr) {
263 *xspi->rx_ptr++ = data;
264 }
265 sr = in_8(xspi->regs + XSPI_SR_OFFSET);
266 } 333 }
267 334
268 /* See if there is more data to send */ 335 /* See if there is more data to send */
@@ -271,7 +338,7 @@ static irqreturn_t xilinx_spi_irq(int irq, void *dev_id)
271 /* Start the transfer by not inhibiting the 338 /* Start the transfer by not inhibiting the
272 * transmitter any longer 339 * transmitter any longer
273 */ 340 */
274 out_be16(xspi->regs + XSPI_CR_OFFSET, cr); 341 xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
275 } else { 342 } else {
276 /* No more data to send. 343 /* No more data to send.
277 * Indicate the transfer is completed. 344 * Indicate the transfer is completed.
@@ -283,40 +350,22 @@ static irqreturn_t xilinx_spi_irq(int irq, void *dev_id)
283 return IRQ_HANDLED; 350 return IRQ_HANDLED;
284} 351}
285 352
286static int __init xilinx_spi_of_probe(struct of_device *ofdev, 353struct spi_master *xilinx_spi_init(struct device *dev, struct resource *mem,
287 const struct of_device_id *match) 354 u32 irq, s16 bus_num)
288{ 355{
289 struct spi_master *master; 356 struct spi_master *master;
290 struct xilinx_spi *xspi; 357 struct xilinx_spi *xspi;
291 struct resource r_irq_struct; 358 struct xspi_platform_data *pdata = dev->platform_data;
292 struct resource r_mem_struct; 359 int ret;
293
294 struct resource *r_irq = &r_irq_struct;
295 struct resource *r_mem = &r_mem_struct;
296 int rc = 0;
297 const u32 *prop;
298 int len;
299
300 /* Get resources(memory, IRQ) associated with the device */
301 master = spi_alloc_master(&ofdev->dev, sizeof(struct xilinx_spi));
302 360
303 if (master == NULL) { 361 if (!pdata) {
304 return -ENOMEM; 362 dev_err(dev, "No platform data attached\n");
363 return NULL;
305 } 364 }
306 365
307 dev_set_drvdata(&ofdev->dev, master); 366 master = spi_alloc_master(dev, sizeof(struct xilinx_spi));
308 367 if (!master)
309 rc = of_address_to_resource(ofdev->node, 0, r_mem); 368 return NULL;
310 if (rc) {
311 dev_warn(&ofdev->dev, "invalid address\n");
312 goto put_master;
313 }
314
315 rc = of_irq_to_resource(ofdev->node, 0, r_irq);
316 if (rc == NO_IRQ) {
317 dev_warn(&ofdev->dev, "no IRQ found\n");
318 goto put_master;
319 }
320 369
321 /* the spi->mode bits understood by this driver: */ 370 /* the spi->mode bits understood by this driver: */
322 master->mode_bits = SPI_CPOL | SPI_CPHA; 371 master->mode_bits = SPI_CPOL | SPI_CPHA;
@@ -329,128 +378,87 @@ static int __init xilinx_spi_of_probe(struct of_device *ofdev,
329 xspi->bitbang.master->setup = xilinx_spi_setup; 378 xspi->bitbang.master->setup = xilinx_spi_setup;
330 init_completion(&xspi->done); 379 init_completion(&xspi->done);
331 380
332 xspi->irq = r_irq->start; 381 if (!request_mem_region(mem->start, resource_size(mem),
333 382 XILINX_SPI_NAME))
334 if (!request_mem_region(r_mem->start,
335 r_mem->end - r_mem->start + 1, XILINX_SPI_NAME)) {
336 rc = -ENXIO;
337 dev_warn(&ofdev->dev, "memory request failure\n");
338 goto put_master; 383 goto put_master;
339 }
340 384
341 xspi->regs = ioremap(r_mem->start, r_mem->end - r_mem->start + 1); 385 xspi->regs = ioremap(mem->start, resource_size(mem));
342 if (xspi->regs == NULL) { 386 if (xspi->regs == NULL) {
343 rc = -ENOMEM; 387 dev_warn(dev, "ioremap failure\n");
344 dev_warn(&ofdev->dev, "ioremap failure\n"); 388 goto map_failed;
345 goto release_mem;
346 } 389 }
347 xspi->irq = r_irq->start;
348
349 /* dynamic bus assignment */
350 master->bus_num = -1;
351 390
352 /* number of slave select bits is required */ 391 master->bus_num = bus_num;
353 prop = of_get_property(ofdev->node, "xlnx,num-ss-bits", &len); 392 master->num_chipselect = pdata->num_chipselect;
354 if (!prop || len < sizeof(*prop)) { 393
355 dev_warn(&ofdev->dev, "no 'xlnx,num-ss-bits' property\n"); 394 xspi->mem = *mem;
356 goto unmap_io; 395 xspi->irq = irq;
396 if (pdata->little_endian) {
397 xspi->read_fn = xspi_read32;
398 xspi->write_fn = xspi_write32;
399 } else {
400 xspi->read_fn = xspi_read32_be;
401 xspi->write_fn = xspi_write32_be;
357 } 402 }
358 master->num_chipselect = *prop; 403 xspi->bits_per_word = pdata->bits_per_word;
404 if (xspi->bits_per_word == 8) {
405 xspi->tx_fn = xspi_tx8;
406 xspi->rx_fn = xspi_rx8;
407 } else if (xspi->bits_per_word == 16) {
408 xspi->tx_fn = xspi_tx16;
409 xspi->rx_fn = xspi_rx16;
410 } else if (xspi->bits_per_word == 32) {
411 xspi->tx_fn = xspi_tx32;
412 xspi->rx_fn = xspi_rx32;
413 } else
414 goto unmap_io;
415
359 416
360 /* SPI controller initializations */ 417 /* SPI controller initializations */
361 xspi_init_hw(xspi->regs); 418 xspi_init_hw(xspi);
362 419
363 /* Register for SPI Interrupt */ 420 /* Register for SPI Interrupt */
364 rc = request_irq(xspi->irq, xilinx_spi_irq, 0, XILINX_SPI_NAME, xspi); 421 ret = request_irq(xspi->irq, xilinx_spi_irq, 0, XILINX_SPI_NAME, xspi);
365 if (rc != 0) { 422 if (ret)
366 dev_warn(&ofdev->dev, "irq request failure: %d\n", xspi->irq);
367 goto unmap_io; 423 goto unmap_io;
368 }
369 424
370 rc = spi_bitbang_start(&xspi->bitbang); 425 ret = spi_bitbang_start(&xspi->bitbang);
371 if (rc != 0) { 426 if (ret) {
372 dev_err(&ofdev->dev, "spi_bitbang_start FAILED\n"); 427 dev_err(dev, "spi_bitbang_start FAILED\n");
373 goto free_irq; 428 goto free_irq;
374 } 429 }
375 430
376 dev_info(&ofdev->dev, "at 0x%08X mapped to 0x%08X, irq=%d\n", 431 dev_info(dev, "at 0x%08llX mapped to 0x%p, irq=%d\n",
377 (unsigned int)r_mem->start, (u32)xspi->regs, xspi->irq); 432 (unsigned long long)mem->start, xspi->regs, xspi->irq);
378 433 return master;
379 /* Add any subnodes on the SPI bus */
380 of_register_spi_devices(master, ofdev->node);
381
382 return rc;
383 434
384free_irq: 435free_irq:
385 free_irq(xspi->irq, xspi); 436 free_irq(xspi->irq, xspi);
386unmap_io: 437unmap_io:
387 iounmap(xspi->regs); 438 iounmap(xspi->regs);
388release_mem: 439map_failed:
389 release_mem_region(r_mem->start, resource_size(r_mem)); 440 release_mem_region(mem->start, resource_size(mem));
390put_master: 441put_master:
391 spi_master_put(master); 442 spi_master_put(master);
392 return rc; 443 return NULL;
393} 444}
445EXPORT_SYMBOL(xilinx_spi_init);
394 446
395static int __devexit xilinx_spi_remove(struct of_device *ofdev) 447void xilinx_spi_deinit(struct spi_master *master)
396{ 448{
397 struct xilinx_spi *xspi; 449 struct xilinx_spi *xspi;
398 struct spi_master *master;
399 struct resource r_mem;
400 450
401 master = platform_get_drvdata(ofdev);
402 xspi = spi_master_get_devdata(master); 451 xspi = spi_master_get_devdata(master);
403 452
404 spi_bitbang_stop(&xspi->bitbang); 453 spi_bitbang_stop(&xspi->bitbang);
405 free_irq(xspi->irq, xspi); 454 free_irq(xspi->irq, xspi);
406 iounmap(xspi->regs); 455 iounmap(xspi->regs);
407 if (!of_address_to_resource(ofdev->node, 0, &r_mem))
408 release_mem_region(r_mem.start, resource_size(&r_mem));
409 dev_set_drvdata(&ofdev->dev, 0);
410 spi_master_put(xspi->bitbang.master);
411
412 return 0;
413}
414
415/* work with hotplug and coldplug */
416MODULE_ALIAS("platform:" XILINX_SPI_NAME);
417
418static int __exit xilinx_spi_of_remove(struct of_device *op)
419{
420 return xilinx_spi_remove(op);
421}
422
423static struct of_device_id xilinx_spi_of_match[] = {
424 { .compatible = "xlnx,xps-spi-2.00.a", },
425 { .compatible = "xlnx,xps-spi-2.00.b", },
426 {}
427};
428
429MODULE_DEVICE_TABLE(of, xilinx_spi_of_match);
430
431static struct of_platform_driver xilinx_spi_of_driver = {
432 .owner = THIS_MODULE,
433 .name = "xilinx-xps-spi",
434 .match_table = xilinx_spi_of_match,
435 .probe = xilinx_spi_of_probe,
436 .remove = __exit_p(xilinx_spi_of_remove),
437 .driver = {
438 .name = "xilinx-xps-spi",
439 .owner = THIS_MODULE,
440 },
441};
442 456
443static int __init xilinx_spi_init(void) 457 release_mem_region(xspi->mem.start, resource_size(&xspi->mem));
444{ 458 spi_master_put(xspi->bitbang.master);
445 return of_register_platform_driver(&xilinx_spi_of_driver);
446} 459}
447module_init(xilinx_spi_init); 460EXPORT_SYMBOL(xilinx_spi_deinit);
448 461
449static void __exit xilinx_spi_exit(void)
450{
451 of_unregister_platform_driver(&xilinx_spi_of_driver);
452}
453module_exit(xilinx_spi_exit);
454MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>"); 462MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
455MODULE_DESCRIPTION("Xilinx SPI driver"); 463MODULE_DESCRIPTION("Xilinx SPI driver");
456MODULE_LICENSE("GPL"); 464MODULE_LICENSE("GPL");