blob: 5a143b9f63611278da3e00ea51894f22df558a61 [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>
17#include <linux/platform_device.h>
John Linnff82c582009-01-09 16:01:53 -070018
19#include <linux/of_platform.h>
20#include <linux/of_device.h>
21#include <linux/of_spi.h>
22
Andrei Konovalovae918c02007-07-17 04:04:11 -070023#include <linux/spi/spi.h>
24#include <linux/spi/spi_bitbang.h>
25#include <linux/io.h>
26
David Brownellfc3ba952007-08-30 23:56:24 -070027#define XILINX_SPI_NAME "xilinx_spi"
Andrei Konovalovae918c02007-07-17 04:04:11 -070028
29/* Register definitions as per "OPB Serial Peripheral Interface (SPI) (v1.00e)
30 * Product Specification", DS464
31 */
32#define XSPI_CR_OFFSET 0x62 /* 16-bit Control Register */
33
34#define XSPI_CR_ENABLE 0x02
35#define XSPI_CR_MASTER_MODE 0x04
36#define XSPI_CR_CPOL 0x08
37#define XSPI_CR_CPHA 0x10
38#define XSPI_CR_MODE_MASK (XSPI_CR_CPHA | XSPI_CR_CPOL)
39#define XSPI_CR_TXFIFO_RESET 0x20
40#define XSPI_CR_RXFIFO_RESET 0x40
41#define XSPI_CR_MANUAL_SSELECT 0x80
42#define XSPI_CR_TRANS_INHIBIT 0x100
43
44#define XSPI_SR_OFFSET 0x67 /* 8-bit Status Register */
45
46#define XSPI_SR_RX_EMPTY_MASK 0x01 /* Receive FIFO is empty */
47#define XSPI_SR_RX_FULL_MASK 0x02 /* Receive FIFO is full */
48#define XSPI_SR_TX_EMPTY_MASK 0x04 /* Transmit FIFO is empty */
49#define XSPI_SR_TX_FULL_MASK 0x08 /* Transmit FIFO is full */
50#define XSPI_SR_MODE_FAULT_MASK 0x10 /* Mode fault error */
51
52#define XSPI_TXD_OFFSET 0x6b /* 8-bit Data Transmit Register */
53#define XSPI_RXD_OFFSET 0x6f /* 8-bit Data Receive Register */
54
55#define XSPI_SSR_OFFSET 0x70 /* 32-bit Slave Select Register */
56
57/* Register definitions as per "OPB IPIF (v3.01c) Product Specification", DS414
58 * IPIF registers are 32 bit
59 */
60#define XIPIF_V123B_DGIER_OFFSET 0x1c /* IPIF global int enable reg */
61#define XIPIF_V123B_GINTR_ENABLE 0x80000000
62
63#define XIPIF_V123B_IISR_OFFSET 0x20 /* IPIF interrupt status reg */
64#define XIPIF_V123B_IIER_OFFSET 0x28 /* IPIF interrupt enable reg */
65
66#define XSPI_INTR_MODE_FAULT 0x01 /* Mode fault error */
67#define XSPI_INTR_SLAVE_MODE_FAULT 0x02 /* Selected as slave while
68 * disabled */
69#define XSPI_INTR_TX_EMPTY 0x04 /* TxFIFO is empty */
70#define XSPI_INTR_TX_UNDERRUN 0x08 /* TxFIFO was underrun */
71#define XSPI_INTR_RX_FULL 0x10 /* RxFIFO is full */
72#define XSPI_INTR_RX_OVERRUN 0x20 /* RxFIFO was overrun */
73
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;
81
82 void __iomem *regs; /* virt. address of the control registers */
83
84 u32 irq;
85
86 u32 speed_hz; /* SCK has a fixed frequency of speed_hz Hz */
87
88 u8 *rx_ptr; /* pointer in the Tx buffer */
89 const u8 *tx_ptr; /* pointer in the Rx buffer */
90 int remaining_bytes; /* the number of bytes left to transfer */
91};
92
93static void xspi_init_hw(void __iomem *regs_base)
94{
95 /* Reset the SPI device */
96 out_be32(regs_base + XIPIF_V123B_RESETR_OFFSET,
97 XIPIF_V123B_RESET_MASK);
98 /* Disable all the interrupts just in case */
99 out_be32(regs_base + XIPIF_V123B_IIER_OFFSET, 0);
100 /* Enable the global IPIF interrupt */
101 out_be32(regs_base + XIPIF_V123B_DGIER_OFFSET,
102 XIPIF_V123B_GINTR_ENABLE);
103 /* Deselect the slave on the SPI bus */
104 out_be32(regs_base + XSPI_SSR_OFFSET, 0xffff);
105 /* Disable the transmitter, enable Manual Slave Select Assertion,
106 * put SPI controller into master mode, and enable it */
107 out_be16(regs_base + XSPI_CR_OFFSET,
108 XSPI_CR_TRANS_INHIBIT | XSPI_CR_MANUAL_SSELECT
109 | XSPI_CR_MASTER_MODE | XSPI_CR_ENABLE);
110}
111
112static void xilinx_spi_chipselect(struct spi_device *spi, int is_on)
113{
114 struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
115
116 if (is_on == BITBANG_CS_INACTIVE) {
117 /* Deselect the slave on the SPI bus */
118 out_be32(xspi->regs + XSPI_SSR_OFFSET, 0xffff);
119 } else if (is_on == BITBANG_CS_ACTIVE) {
120 /* Set the SPI clock phase and polarity */
121 u16 cr = in_be16(xspi->regs + XSPI_CR_OFFSET)
122 & ~XSPI_CR_MODE_MASK;
123 if (spi->mode & SPI_CPHA)
124 cr |= XSPI_CR_CPHA;
125 if (spi->mode & SPI_CPOL)
126 cr |= XSPI_CR_CPOL;
127 out_be16(xspi->regs + XSPI_CR_OFFSET, cr);
128
129 /* We do not check spi->max_speed_hz here as the SPI clock
130 * frequency is not software programmable (the IP block design
131 * parameter)
132 */
133
134 /* Activate the chip select */
135 out_be32(xspi->regs + XSPI_SSR_OFFSET,
136 ~(0x0001 << spi->chip_select));
137 }
138}
139
140/* 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
142 * supports just 8 bits per word, and SPI clock can't be changed in software.
143 * Check for 8 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.
145 */
146static int xilinx_spi_setup_transfer(struct spi_device *spi,
147 struct spi_transfer *t)
148{
149 u8 bits_per_word;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700150
John Linn1a8d3b72009-09-14 08:17:05 +0000151 bits_per_word = (t && t->bits_per_word)
152 ? t->bits_per_word : spi->bits_per_word;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700153 if (bits_per_word != 8) {
154 dev_err(&spi->dev, "%s, unsupported bits_per_word=%d\n",
Harvey Harrisonb687d2a2008-04-28 02:14:19 -0700155 __func__, bits_per_word);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700156 return -EINVAL;
157 }
158
Andrei Konovalovae918c02007-07-17 04:04:11 -0700159 return 0;
160}
161
Andrei Konovalovae918c02007-07-17 04:04:11 -0700162static int xilinx_spi_setup(struct spi_device *spi)
163{
164 struct spi_bitbang *bitbang;
165 struct xilinx_spi *xspi;
166 int retval;
167
168 xspi = spi_master_get_devdata(spi->master);
169 bitbang = &xspi->bitbang;
170
Andrei Konovalovae918c02007-07-17 04:04:11 -0700171 retval = xilinx_spi_setup_transfer(spi, NULL);
172 if (retval < 0)
173 return retval;
174
Andrei Konovalovae918c02007-07-17 04:04:11 -0700175 return 0;
176}
177
178static void xilinx_spi_fill_tx_fifo(struct xilinx_spi *xspi)
179{
180 u8 sr;
181
182 /* Fill the Tx FIFO with as many bytes as possible */
183 sr = in_8(xspi->regs + XSPI_SR_OFFSET);
184 while ((sr & XSPI_SR_TX_FULL_MASK) == 0 && xspi->remaining_bytes > 0) {
185 if (xspi->tx_ptr) {
186 out_8(xspi->regs + XSPI_TXD_OFFSET, *xspi->tx_ptr++);
187 } else {
188 out_8(xspi->regs + XSPI_TXD_OFFSET, 0);
189 }
190 xspi->remaining_bytes--;
191 sr = in_8(xspi->regs + XSPI_SR_OFFSET);
192 }
193}
194
195static int xilinx_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
196{
197 struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
198 u32 ipif_ier;
199 u16 cr;
200
201 /* We get here with transmitter inhibited */
202
203 xspi->tx_ptr = t->tx_buf;
204 xspi->rx_ptr = t->rx_buf;
205 xspi->remaining_bytes = t->len;
206 INIT_COMPLETION(xspi->done);
207
208 xilinx_spi_fill_tx_fifo(xspi);
209
210 /* Enable the transmit empty interrupt, which we use to determine
211 * progress on the transmission.
212 */
213 ipif_ier = in_be32(xspi->regs + XIPIF_V123B_IIER_OFFSET);
214 out_be32(xspi->regs + XIPIF_V123B_IIER_OFFSET,
215 ipif_ier | XSPI_INTR_TX_EMPTY);
216
217 /* Start the transfer by not inhibiting the transmitter any longer */
218 cr = in_be16(xspi->regs + XSPI_CR_OFFSET) & ~XSPI_CR_TRANS_INHIBIT;
219 out_be16(xspi->regs + XSPI_CR_OFFSET, cr);
220
221 wait_for_completion(&xspi->done);
222
223 /* Disable the transmit empty interrupt */
224 out_be32(xspi->regs + XIPIF_V123B_IIER_OFFSET, ipif_ier);
225
226 return t->len - xspi->remaining_bytes;
227}
228
229
230/* This driver supports single master mode only. Hence Tx FIFO Empty
231 * is the only interrupt we care about.
232 * Receive FIFO Overrun, Transmit FIFO Underrun, Mode Fault, and Slave Mode
233 * Fault are not to happen.
234 */
235static irqreturn_t xilinx_spi_irq(int irq, void *dev_id)
236{
237 struct xilinx_spi *xspi = dev_id;
238 u32 ipif_isr;
239
240 /* Get the IPIF interrupts, and clear them immediately */
241 ipif_isr = in_be32(xspi->regs + XIPIF_V123B_IISR_OFFSET);
242 out_be32(xspi->regs + XIPIF_V123B_IISR_OFFSET, ipif_isr);
243
244 if (ipif_isr & XSPI_INTR_TX_EMPTY) { /* Transmission completed */
245 u16 cr;
246 u8 sr;
247
248 /* A transmit has just completed. Process received data and
249 * check for more data to transmit. Always inhibit the
250 * transmitter while the Isr refills the transmit register/FIFO,
251 * or make sure it is stopped if we're done.
252 */
253 cr = in_be16(xspi->regs + XSPI_CR_OFFSET);
254 out_be16(xspi->regs + XSPI_CR_OFFSET,
255 cr | XSPI_CR_TRANS_INHIBIT);
256
257 /* Read out all the data from the Rx FIFO */
258 sr = in_8(xspi->regs + XSPI_SR_OFFSET);
259 while ((sr & XSPI_SR_RX_EMPTY_MASK) == 0) {
260 u8 data;
261
262 data = in_8(xspi->regs + XSPI_RXD_OFFSET);
263 if (xspi->rx_ptr) {
264 *xspi->rx_ptr++ = data;
265 }
266 sr = in_8(xspi->regs + XSPI_SR_OFFSET);
267 }
268
269 /* See if there is more data to send */
270 if (xspi->remaining_bytes > 0) {
271 xilinx_spi_fill_tx_fifo(xspi);
272 /* Start the transfer by not inhibiting the
273 * transmitter any longer
274 */
275 out_be16(xspi->regs + XSPI_CR_OFFSET, cr);
276 } else {
277 /* No more data to send.
278 * Indicate the transfer is completed.
279 */
280 complete(&xspi->done);
281 }
282 }
283
284 return IRQ_HANDLED;
285}
286
John Linnff82c582009-01-09 16:01:53 -0700287static int __init xilinx_spi_of_probe(struct of_device *ofdev,
288 const struct of_device_id *match)
Andrei Konovalovae918c02007-07-17 04:04:11 -0700289{
Andrei Konovalovae918c02007-07-17 04:04:11 -0700290 struct spi_master *master;
291 struct xilinx_spi *xspi;
John Linnff82c582009-01-09 16:01:53 -0700292 struct resource r_irq_struct;
293 struct resource r_mem_struct;
294
295 struct resource *r_irq = &r_irq_struct;
296 struct resource *r_mem = &r_mem_struct;
297 int rc = 0;
298 const u32 *prop;
299 int len;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700300
301 /* Get resources(memory, IRQ) associated with the device */
John Linnff82c582009-01-09 16:01:53 -0700302 master = spi_alloc_master(&ofdev->dev, sizeof(struct xilinx_spi));
Andrei Konovalovae918c02007-07-17 04:04:11 -0700303
304 if (master == NULL) {
305 return -ENOMEM;
306 }
307
John Linnff82c582009-01-09 16:01:53 -0700308 dev_set_drvdata(&ofdev->dev, master);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700309
John Linnff82c582009-01-09 16:01:53 -0700310 rc = of_address_to_resource(ofdev->node, 0, r_mem);
311 if (rc) {
312 dev_warn(&ofdev->dev, "invalid address\n");
Andrei Konovalovae918c02007-07-17 04:04:11 -0700313 goto put_master;
314 }
315
John Linnff82c582009-01-09 16:01:53 -0700316 rc = of_irq_to_resource(ofdev->node, 0, r_irq);
317 if (rc == NO_IRQ) {
318 dev_warn(&ofdev->dev, "no IRQ found\n");
Andrei Konovalovae918c02007-07-17 04:04:11 -0700319 goto put_master;
320 }
321
David Brownelle7db06b2009-06-17 16:26:04 -0700322 /* the spi->mode bits understood by this driver: */
323 master->mode_bits = SPI_CPOL | SPI_CPHA;
324
Andrei Konovalovae918c02007-07-17 04:04:11 -0700325 xspi = spi_master_get_devdata(master);
326 xspi->bitbang.master = spi_master_get(master);
327 xspi->bitbang.chipselect = xilinx_spi_chipselect;
328 xspi->bitbang.setup_transfer = xilinx_spi_setup_transfer;
329 xspi->bitbang.txrx_bufs = xilinx_spi_txrx_bufs;
330 xspi->bitbang.master->setup = xilinx_spi_setup;
331 init_completion(&xspi->done);
332
John Linnff82c582009-01-09 16:01:53 -0700333 xspi->irq = r_irq->start;
334
335 if (!request_mem_region(r_mem->start,
336 r_mem->end - r_mem->start + 1, XILINX_SPI_NAME)) {
337 rc = -ENXIO;
338 dev_warn(&ofdev->dev, "memory request failure\n");
Andrei Konovalovae918c02007-07-17 04:04:11 -0700339 goto put_master;
340 }
341
John Linnff82c582009-01-09 16:01:53 -0700342 xspi->regs = ioremap(r_mem->start, r_mem->end - r_mem->start + 1);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700343 if (xspi->regs == NULL) {
John Linnff82c582009-01-09 16:01:53 -0700344 rc = -ENOMEM;
345 dev_warn(&ofdev->dev, "ioremap failure\n");
John Linn1df879e2009-03-11 09:36:20 -0600346 goto release_mem;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700347 }
John Linnff82c582009-01-09 16:01:53 -0700348 xspi->irq = r_irq->start;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700349
John Linnff82c582009-01-09 16:01:53 -0700350 /* dynamic bus assignment */
351 master->bus_num = -1;
352
353 /* number of slave select bits is required */
354 prop = of_get_property(ofdev->node, "xlnx,num-ss-bits", &len);
355 if (!prop || len < sizeof(*prop)) {
356 dev_warn(&ofdev->dev, "no 'xlnx,num-ss-bits' property\n");
John Linn1df879e2009-03-11 09:36:20 -0600357 goto unmap_io;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700358 }
John Linnff82c582009-01-09 16:01:53 -0700359 master->num_chipselect = *prop;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700360
361 /* SPI controller initializations */
362 xspi_init_hw(xspi->regs);
363
364 /* Register for SPI Interrupt */
John Linnff82c582009-01-09 16:01:53 -0700365 rc = request_irq(xspi->irq, xilinx_spi_irq, 0, XILINX_SPI_NAME, xspi);
366 if (rc != 0) {
367 dev_warn(&ofdev->dev, "irq request failure: %d\n", xspi->irq);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700368 goto unmap_io;
John Linnff82c582009-01-09 16:01:53 -0700369 }
Andrei Konovalovae918c02007-07-17 04:04:11 -0700370
John Linnff82c582009-01-09 16:01:53 -0700371 rc = spi_bitbang_start(&xspi->bitbang);
372 if (rc != 0) {
373 dev_err(&ofdev->dev, "spi_bitbang_start FAILED\n");
Andrei Konovalovae918c02007-07-17 04:04:11 -0700374 goto free_irq;
375 }
376
John Linnff82c582009-01-09 16:01:53 -0700377 dev_info(&ofdev->dev, "at 0x%08X mapped to 0x%08X, irq=%d\n",
378 (unsigned int)r_mem->start, (u32)xspi->regs, xspi->irq);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700379
John Linnff82c582009-01-09 16:01:53 -0700380 /* Add any subnodes on the SPI bus */
381 of_register_spi_devices(master, ofdev->node);
382
383 return rc;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700384
385free_irq:
386 free_irq(xspi->irq, xspi);
387unmap_io:
388 iounmap(xspi->regs);
John Linn1df879e2009-03-11 09:36:20 -0600389release_mem:
390 release_mem_region(r_mem->start, resource_size(r_mem));
Andrei Konovalovae918c02007-07-17 04:04:11 -0700391put_master:
392 spi_master_put(master);
John Linnff82c582009-01-09 16:01:53 -0700393 return rc;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700394}
395
John Linnff82c582009-01-09 16:01:53 -0700396static int __devexit xilinx_spi_remove(struct of_device *ofdev)
Andrei Konovalovae918c02007-07-17 04:04:11 -0700397{
398 struct xilinx_spi *xspi;
399 struct spi_master *master;
John Linn1df879e2009-03-11 09:36:20 -0600400 struct resource r_mem;
Andrei Konovalovae918c02007-07-17 04:04:11 -0700401
John Linnff82c582009-01-09 16:01:53 -0700402 master = platform_get_drvdata(ofdev);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700403 xspi = spi_master_get_devdata(master);
404
405 spi_bitbang_stop(&xspi->bitbang);
406 free_irq(xspi->irq, xspi);
407 iounmap(xspi->regs);
John Linn1df879e2009-03-11 09:36:20 -0600408 if (!of_address_to_resource(ofdev->node, 0, &r_mem))
409 release_mem_region(r_mem.start, resource_size(&r_mem));
John Linnff82c582009-01-09 16:01:53 -0700410 dev_set_drvdata(&ofdev->dev, 0);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700411 spi_master_put(xspi->bitbang.master);
412
413 return 0;
414}
415
Kay Sievers7e38c3c2008-04-10 21:29:20 -0700416/* work with hotplug and coldplug */
417MODULE_ALIAS("platform:" XILINX_SPI_NAME);
418
John Linnff82c582009-01-09 16:01:53 -0700419static int __exit xilinx_spi_of_remove(struct of_device *op)
420{
421 return xilinx_spi_remove(op);
422}
423
424static struct of_device_id xilinx_spi_of_match[] = {
425 { .compatible = "xlnx,xps-spi-2.00.a", },
426 { .compatible = "xlnx,xps-spi-2.00.b", },
427 {}
428};
429
430MODULE_DEVICE_TABLE(of, xilinx_spi_of_match);
431
432static struct of_platform_driver xilinx_spi_of_driver = {
433 .owner = THIS_MODULE,
434 .name = "xilinx-xps-spi",
435 .match_table = xilinx_spi_of_match,
436 .probe = xilinx_spi_of_probe,
437 .remove = __exit_p(xilinx_spi_of_remove),
Andrei Konovalovae918c02007-07-17 04:04:11 -0700438 .driver = {
John Linnff82c582009-01-09 16:01:53 -0700439 .name = "xilinx-xps-spi",
Andrei Konovalovae918c02007-07-17 04:04:11 -0700440 .owner = THIS_MODULE,
441 },
442};
443
444static int __init xilinx_spi_init(void)
445{
John Linnff82c582009-01-09 16:01:53 -0700446 return of_register_platform_driver(&xilinx_spi_of_driver);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700447}
448module_init(xilinx_spi_init);
449
450static void __exit xilinx_spi_exit(void)
451{
John Linnff82c582009-01-09 16:01:53 -0700452 of_unregister_platform_driver(&xilinx_spi_of_driver);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700453}
454module_exit(xilinx_spi_exit);
Andrei Konovalovae918c02007-07-17 04:04:11 -0700455MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
456MODULE_DESCRIPTION("Xilinx SPI driver");
457MODULE_LICENSE("GPL");