spi/xilinx: Remove iowrite/ioread wrappers
[deliverable/linux.git] / drivers / spi / spi-xilinx.c
CommitLineData
ae918c02 1/*
ae918c02
AK
2 * Xilinx SPI controller driver (master mode only)
3 *
4 * Author: MontaVista Software, Inc.
5 * source@mvista.com
6 *
8fd8821b
GL
7 * Copyright (c) 2010 Secret Lab Technologies, Ltd.
8 * Copyright (c) 2009 Intel Corporation
9 * 2002-2007 (c) MontaVista Software, Inc.
10
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
ae918c02
AK
14 */
15
16#include <linux/module.h>
ae918c02 17#include <linux/interrupt.h>
eae6cb31 18#include <linux/of.h>
8fd8821b 19#include <linux/platform_device.h>
ae918c02
AK
20#include <linux/spi/spi.h>
21#include <linux/spi/spi_bitbang.h>
d5af91a1 22#include <linux/spi/xilinx_spi.h>
eae6cb31 23#include <linux/io.h>
d5af91a1 24
fc3ba952 25#define XILINX_SPI_NAME "xilinx_spi"
ae918c02
AK
26
27/* Register definitions as per "OPB Serial Peripheral Interface (SPI) (v1.00e)
28 * Product Specification", DS464
29 */
c9da2e12 30#define XSPI_CR_OFFSET 0x60 /* Control Register */
ae918c02 31
082339bc 32#define XSPI_CR_LOOP 0x01
ae918c02
AK
33#define XSPI_CR_ENABLE 0x02
34#define XSPI_CR_MASTER_MODE 0x04
35#define XSPI_CR_CPOL 0x08
36#define XSPI_CR_CPHA 0x10
bca690db 37#define XSPI_CR_MODE_MASK (XSPI_CR_CPHA | XSPI_CR_CPOL | \
0240f945 38 XSPI_CR_LSB_FIRST | XSPI_CR_LOOP)
ae918c02
AK
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
c9da2e12 43#define XSPI_CR_LSB_FIRST 0x200
ae918c02 44
c9da2e12 45#define XSPI_SR_OFFSET 0x64 /* Status Register */
ae918c02
AK
46
47#define XSPI_SR_RX_EMPTY_MASK 0x01 /* Receive FIFO is empty */
48#define XSPI_SR_RX_FULL_MASK 0x02 /* Receive FIFO is full */
49#define XSPI_SR_TX_EMPTY_MASK 0x04 /* Transmit FIFO is empty */
50#define XSPI_SR_TX_FULL_MASK 0x08 /* Transmit FIFO is full */
51#define XSPI_SR_MODE_FAULT_MASK 0x10 /* Mode fault error */
52
c9da2e12
RR
53#define XSPI_TXD_OFFSET 0x68 /* Data Transmit Register */
54#define XSPI_RXD_OFFSET 0x6c /* Data Receive Register */
ae918c02
AK
55
56#define XSPI_SSR_OFFSET 0x70 /* 32-bit Slave Select Register */
57
58/* Register definitions as per "OPB IPIF (v3.01c) Product Specification", DS414
59 * IPIF registers are 32 bit
60 */
61#define XIPIF_V123B_DGIER_OFFSET 0x1c /* IPIF global int enable reg */
62#define XIPIF_V123B_GINTR_ENABLE 0x80000000
63
64#define XIPIF_V123B_IISR_OFFSET 0x20 /* IPIF interrupt status reg */
65#define XIPIF_V123B_IIER_OFFSET 0x28 /* IPIF interrupt enable reg */
66
67#define XSPI_INTR_MODE_FAULT 0x01 /* Mode fault error */
68#define XSPI_INTR_SLAVE_MODE_FAULT 0x02 /* Selected as slave while
69 * disabled */
70#define XSPI_INTR_TX_EMPTY 0x04 /* TxFIFO is empty */
71#define XSPI_INTR_TX_UNDERRUN 0x08 /* TxFIFO was underrun */
72#define XSPI_INTR_RX_FULL 0x10 /* RxFIFO is full */
73#define XSPI_INTR_RX_OVERRUN 0x20 /* RxFIFO was overrun */
c9da2e12 74#define XSPI_INTR_TX_HALF_EMPTY 0x40 /* TxFIFO is half empty */
ae918c02
AK
75
76#define XIPIF_V123B_RESETR_OFFSET 0x40 /* IPIF reset register */
77#define XIPIF_V123B_RESET_MASK 0x0a /* the value to write */
78
79struct xilinx_spi {
80 /* bitbang has to be first */
81 struct spi_bitbang bitbang;
82 struct completion done;
ae918c02
AK
83 void __iomem *regs; /* virt. address of the control registers */
84
9ca1273b 85 int irq;
ae918c02 86
ae918c02
AK
87 u8 *rx_ptr; /* pointer in the Tx buffer */
88 const u8 *tx_ptr; /* pointer in the Rx buffer */
d79b2d07 89 int remaining_words; /* the number of words left to transfer */
17aaaa80 90 u8 bytes_per_word;
4c9a7614 91 int buffer_size; /* buffer size in words */
f9c6ef6c 92 u32 cs_inactive; /* Level of the CS pins when inactive*/
6ff8672a
JH
93 unsigned int (*read_fn)(void __iomem *);
94 void (*write_fn)(u32, void __iomem *);
ae918c02
AK
95};
96
24ba5e59 97static void xilinx_spi_tx(struct xilinx_spi *xspi)
c9da2e12 98{
c3092941
RRD
99 if (!xspi->tx_ptr) {
100 xspi->write_fn(0, xspi->regs + XSPI_TXD_OFFSET);
101 return;
102 }
c9da2e12 103 xspi->write_fn(*(u32 *)(xspi->tx_ptr), xspi->regs + XSPI_TXD_OFFSET);
17aaaa80 104 xspi->tx_ptr += xspi->bytes_per_word;
c9da2e12
RR
105}
106
24ba5e59 107static void xilinx_spi_rx(struct xilinx_spi *xspi)
c9da2e12
RR
108{
109 u32 data = xspi->read_fn(xspi->regs + XSPI_RXD_OFFSET);
c9da2e12 110
24ba5e59
RRD
111 if (!xspi->rx_ptr)
112 return;
c9da2e12 113
17aaaa80
RRD
114 switch (xspi->bytes_per_word) {
115 case 1:
24ba5e59
RRD
116 *(u8 *)(xspi->rx_ptr) = data;
117 break;
17aaaa80 118 case 2:
24ba5e59
RRD
119 *(u16 *)(xspi->rx_ptr) = data;
120 break;
17aaaa80 121 case 4:
c9da2e12 122 *(u32 *)(xspi->rx_ptr) = data;
24ba5e59 123 break;
c9da2e12 124 }
24ba5e59 125
17aaaa80 126 xspi->rx_ptr += xspi->bytes_per_word;
c9da2e12
RR
127}
128
86fc5935 129static void xspi_init_hw(struct xilinx_spi *xspi)
ae918c02 130{
86fc5935 131 void __iomem *regs_base = xspi->regs;
d9f58812 132 u32 inhibit;
86fc5935 133
ae918c02 134 /* Reset the SPI device */
86fc5935
RR
135 xspi->write_fn(XIPIF_V123B_RESET_MASK,
136 regs_base + XIPIF_V123B_RESETR_OFFSET);
899929ba
RRD
137 /* Enable the transmit empty interrupt, which we use to determine
138 * progress on the transmission.
139 */
140 xspi->write_fn(XSPI_INTR_TX_EMPTY,
141 regs_base + XIPIF_V123B_IIER_OFFSET);
ae918c02 142 /* Enable the global IPIF interrupt */
d9f58812 143 if (xspi->irq >= 0) {
5fe11cc0
RRD
144 xspi->write_fn(XIPIF_V123B_GINTR_ENABLE,
145 regs_base + XIPIF_V123B_DGIER_OFFSET);
d9f58812
RRD
146 inhibit = XSPI_CR_TRANS_INHIBIT;
147 } else {
5fe11cc0 148 xspi->write_fn(0, regs_base + XIPIF_V123B_DGIER_OFFSET);
d9f58812
RRD
149 inhibit = 0;
150 }
ae918c02 151 /* Deselect the slave on the SPI bus */
86fc5935 152 xspi->write_fn(0xffff, regs_base + XSPI_SSR_OFFSET);
ae918c02
AK
153 /* Disable the transmitter, enable Manual Slave Select Assertion,
154 * put SPI controller into master mode, and enable it */
d9f58812 155 xspi->write_fn(inhibit | XSPI_CR_MANUAL_SSELECT |
c9da2e12
RR
156 XSPI_CR_MASTER_MODE | XSPI_CR_ENABLE | XSPI_CR_TXFIFO_RESET |
157 XSPI_CR_RXFIFO_RESET, regs_base + XSPI_CR_OFFSET);
ae918c02
AK
158}
159
160static void xilinx_spi_chipselect(struct spi_device *spi, int is_on)
161{
162 struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
f9c6ef6c
RRD
163 u16 cr;
164 u32 cs;
ae918c02
AK
165
166 if (is_on == BITBANG_CS_INACTIVE) {
167 /* Deselect the slave on the SPI bus */
f9c6ef6c
RRD
168 xspi->write_fn(xspi->cs_inactive, xspi->regs + XSPI_SSR_OFFSET);
169 return;
ae918c02 170 }
f9c6ef6c
RRD
171
172 /* Set the SPI clock phase and polarity */
173 cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET) & ~XSPI_CR_MODE_MASK;
174 if (spi->mode & SPI_CPHA)
175 cr |= XSPI_CR_CPHA;
176 if (spi->mode & SPI_CPOL)
177 cr |= XSPI_CR_CPOL;
178 if (spi->mode & SPI_LSB_FIRST)
179 cr |= XSPI_CR_LSB_FIRST;
180 if (spi->mode & SPI_LOOP)
181 cr |= XSPI_CR_LOOP;
182 xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
183
184 /* We do not check spi->max_speed_hz here as the SPI clock
185 * frequency is not software programmable (the IP block design
186 * parameter)
187 */
188
189 cs = xspi->cs_inactive;
190 cs ^= BIT(spi->chip_select);
191
192 /* Activate the chip select */
193 xspi->write_fn(cs, xspi->regs + XSPI_SSR_OFFSET);
ae918c02
AK
194}
195
196/* spi_bitbang requires custom setup_transfer() to be defined if there is a
9bf46f6d 197 * custom txrx_bufs().
ae918c02
AK
198 */
199static int xilinx_spi_setup_transfer(struct spi_device *spi,
200 struct spi_transfer *t)
201{
f9c6ef6c
RRD
202 struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
203
204 if (spi->mode & SPI_CS_HIGH)
205 xspi->cs_inactive &= ~BIT(spi->chip_select);
206 else
207 xspi->cs_inactive |= BIT(spi->chip_select);
208
ae918c02
AK
209 return 0;
210}
211
4c9a7614 212static void xilinx_spi_fill_tx_fifo(struct xilinx_spi *xspi, int n_words)
ae918c02 213{
d79b2d07 214 xspi->remaining_words -= n_words;
ae918c02 215
4c9a7614 216 while (n_words--)
c3092941 217 xilinx_spi_tx(xspi);
4c9a7614 218 return;
ae918c02
AK
219}
220
221static int xilinx_spi_txrx_bufs(struct spi_device *spi, struct spi_transfer *t)
222{
223 struct xilinx_spi *xspi = spi_master_get_devdata(spi->master);
ae918c02
AK
224
225 /* We get here with transmitter inhibited */
226
227 xspi->tx_ptr = t->tx_buf;
228 xspi->rx_ptr = t->rx_buf;
17aaaa80 229 xspi->remaining_words = t->len / xspi->bytes_per_word;
16735d02 230 reinit_completion(&xspi->done);
ae918c02 231
d79b2d07 232 while (xspi->remaining_words) {
d9f58812 233 u16 cr = 0;
c5d348df 234 int n_words;
68c315bb 235
d79b2d07 236 n_words = min(xspi->remaining_words, xspi->buffer_size);
4c9a7614
RRD
237
238 xilinx_spi_fill_tx_fifo(xspi, n_words);
68c315bb
PC
239
240 /* Start the transfer by not inhibiting the transmitter any
241 * longer
242 */
68c315bb 243
d9f58812
RRD
244 if (xspi->irq >= 0) {
245 cr = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET) &
246 ~XSPI_CR_TRANS_INHIBIT;
247 xspi->write_fn(cr, xspi->regs + XSPI_CR_OFFSET);
5fe11cc0 248 wait_for_completion(&xspi->done);
d9f58812 249 } else
5fe11cc0
RRD
250 while (!(xspi->read_fn(xspi->regs + XSPI_SR_OFFSET) &
251 XSPI_SR_TX_EMPTY_MASK))
252 ;
68c315bb
PC
253
254 /* A transmit has just completed. Process received data and
255 * check for more data to transmit. Always inhibit the
256 * transmitter while the Isr refills the transmit register/FIFO,
257 * or make sure it is stopped if we're done.
258 */
d9f58812
RRD
259 if (xspi->irq >= 0)
260 xspi->write_fn(cr | XSPI_CR_TRANS_INHIBIT,
68c315bb
PC
261 xspi->regs + XSPI_CR_OFFSET);
262
263 /* Read out all the data from the Rx FIFO */
c5d348df 264 while (n_words--)
24ba5e59 265 xilinx_spi_rx(xspi);
68c315bb 266 }
ae918c02 267
d79b2d07 268 return t->len;
ae918c02
AK
269}
270
271
272/* This driver supports single master mode only. Hence Tx FIFO Empty
273 * is the only interrupt we care about.
274 * Receive FIFO Overrun, Transmit FIFO Underrun, Mode Fault, and Slave Mode
275 * Fault are not to happen.
276 */
277static irqreturn_t xilinx_spi_irq(int irq, void *dev_id)
278{
279 struct xilinx_spi *xspi = dev_id;
280 u32 ipif_isr;
281
282 /* Get the IPIF interrupts, and clear them immediately */
86fc5935
RR
283 ipif_isr = xspi->read_fn(xspi->regs + XIPIF_V123B_IISR_OFFSET);
284 xspi->write_fn(ipif_isr, xspi->regs + XIPIF_V123B_IISR_OFFSET);
ae918c02
AK
285
286 if (ipif_isr & XSPI_INTR_TX_EMPTY) { /* Transmission completed */
68c315bb 287 complete(&xspi->done);
ae918c02
AK
288 }
289
290 return IRQ_HANDLED;
291}
292
4c9a7614
RRD
293static int xilinx_spi_find_buffer_size(struct xilinx_spi *xspi)
294{
295 u8 sr;
296 int n_words = 0;
297
298 /*
299 * Before the buffer_size detection we reset the core
300 * to make sure we start with a clean state.
301 */
302 xspi->write_fn(XIPIF_V123B_RESET_MASK,
303 xspi->regs + XIPIF_V123B_RESETR_OFFSET);
304
305 /* Fill the Tx FIFO with as many words as possible */
306 do {
307 xspi->write_fn(0, xspi->regs + XSPI_TXD_OFFSET);
308 sr = xspi->read_fn(xspi->regs + XSPI_SR_OFFSET);
309 n_words++;
310 } while (!(sr & XSPI_SR_TX_FULL_MASK));
311
312 return n_words;
313}
314
eae6cb31
GL
315static const struct of_device_id xilinx_spi_of_match[] = {
316 { .compatible = "xlnx,xps-spi-2.00.a", },
317 { .compatible = "xlnx,xps-spi-2.00.b", },
318 {}
319};
320MODULE_DEVICE_TABLE(of, xilinx_spi_of_match);
eae6cb31 321
7cb2abd0 322static int xilinx_spi_probe(struct platform_device *pdev)
ae918c02 323{
ae918c02 324 struct xilinx_spi *xspi;
d81c0bbb 325 struct xspi_platform_data *pdata;
ad3fdbca 326 struct resource *res;
7b3b7432 327 int ret, num_cs = 0, bits_per_word = 8;
d81c0bbb 328 struct spi_master *master;
082339bc 329 u32 tmp;
d81c0bbb
MB
330 u8 i;
331
8074cf06 332 pdata = dev_get_platdata(&pdev->dev);
d81c0bbb
MB
333 if (pdata) {
334 num_cs = pdata->num_chipselect;
335 bits_per_word = pdata->bits_per_word;
be3acdff
MS
336 } else {
337 of_property_read_u32(pdev->dev.of_node, "xlnx,num-ss-bits",
338 &num_cs);
d81c0bbb 339 }
ae918c02 340
d81c0bbb 341 if (!num_cs) {
7cb2abd0
MB
342 dev_err(&pdev->dev,
343 "Missing slave select configuration data\n");
d81c0bbb
MB
344 return -EINVAL;
345 }
346
7cb2abd0 347 master = spi_alloc_master(&pdev->dev, sizeof(struct xilinx_spi));
d5af91a1 348 if (!master)
d81c0bbb 349 return -ENODEV;
ae918c02 350
e7db06b5 351 /* the spi->mode bits understood by this driver: */
f9c6ef6c
RRD
352 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_LOOP |
353 SPI_CS_HIGH;
e7db06b5 354
ae918c02 355 xspi = spi_master_get_devdata(master);
f9c6ef6c 356 xspi->cs_inactive = 0xffffffff;
94c69f76 357 xspi->bitbang.master = master;
ae918c02
AK
358 xspi->bitbang.chipselect = xilinx_spi_chipselect;
359 xspi->bitbang.setup_transfer = xilinx_spi_setup_transfer;
360 xspi->bitbang.txrx_bufs = xilinx_spi_txrx_bufs;
ae918c02
AK
361 init_completion(&xspi->done);
362
ad3fdbca
MS
363 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
364 xspi->regs = devm_ioremap_resource(&pdev->dev, res);
c40537d0
MB
365 if (IS_ERR(xspi->regs)) {
366 ret = PTR_ERR(xspi->regs);
ae918c02 367 goto put_master;
ae918c02
AK
368 }
369
4b153a21 370 master->bus_num = pdev->id;
91565c40 371 master->num_chipselect = num_cs;
7cb2abd0 372 master->dev.of_node = pdev->dev.of_node;
082339bc
MS
373
374 /*
375 * Detect endianess on the IP via loop bit in CR. Detection
376 * must be done before reset is sent because incorrect reset
377 * value generates error interrupt.
378 * Setup little endian helper functions first and try to use them
379 * and check if bit was correctly setup or not.
380 */
99082eab
RRD
381 xspi->read_fn = ioread32;
382 xspi->write_fn = iowrite32;
082339bc
MS
383
384 xspi->write_fn(XSPI_CR_LOOP, xspi->regs + XSPI_CR_OFFSET);
385 tmp = xspi->read_fn(xspi->regs + XSPI_CR_OFFSET);
386 tmp &= XSPI_CR_LOOP;
387 if (tmp != XSPI_CR_LOOP) {
99082eab
RRD
388 xspi->read_fn = ioread32be;
389 xspi->write_fn = iowrite32be;
86fc5935 390 }
082339bc 391
9bf46f6d 392 master->bits_per_word_mask = SPI_BPW_MASK(bits_per_word);
17aaaa80 393 xspi->bytes_per_word = bits_per_word / 8;
4c9a7614
RRD
394 xspi->buffer_size = xilinx_spi_find_buffer_size(xspi);
395
7b3b7432 396 xspi->irq = platform_get_irq(pdev, 0);
5fe11cc0
RRD
397 if (xspi->irq >= 0) {
398 /* Register for SPI Interrupt */
399 ret = devm_request_irq(&pdev->dev, xspi->irq, xilinx_spi_irq, 0,
400 dev_name(&pdev->dev), xspi);
401 if (ret)
402 goto put_master;
7b3b7432
MS
403 }
404
5fe11cc0
RRD
405 /* SPI controller initializations */
406 xspi_init_hw(xspi);
ae918c02 407
d5af91a1
RR
408 ret = spi_bitbang_start(&xspi->bitbang);
409 if (ret) {
7cb2abd0 410 dev_err(&pdev->dev, "spi_bitbang_start FAILED\n");
7b3b7432 411 goto put_master;
eae6cb31
GL
412 }
413
7cb2abd0 414 dev_info(&pdev->dev, "at 0x%08llX mapped to 0x%p, irq=%d\n",
ad3fdbca 415 (unsigned long long)res->start, xspi->regs, xspi->irq);
8fd8821b 416
eae6cb31
GL
417 if (pdata) {
418 for (i = 0; i < pdata->num_devices; i++)
419 spi_new_device(master, pdata->devices + i);
420 }
8fd8821b 421
7cb2abd0 422 platform_set_drvdata(pdev, master);
8fd8821b 423 return 0;
ae918c02 424
ae918c02
AK
425put_master:
426 spi_master_put(master);
d81c0bbb
MB
427
428 return ret;
8fd8821b
GL
429}
430
7cb2abd0 431static int xilinx_spi_remove(struct platform_device *pdev)
8fd8821b 432{
7cb2abd0 433 struct spi_master *master = platform_get_drvdata(pdev);
d81c0bbb 434 struct xilinx_spi *xspi = spi_master_get_devdata(master);
7b3b7432 435 void __iomem *regs_base = xspi->regs;
ae918c02
AK
436
437 spi_bitbang_stop(&xspi->bitbang);
7b3b7432
MS
438
439 /* Disable all the interrupts just in case */
440 xspi->write_fn(0, regs_base + XIPIF_V123B_IIER_OFFSET);
441 /* Disable the global IPIF interrupt */
442 xspi->write_fn(0, regs_base + XIPIF_V123B_DGIER_OFFSET);
ff82c587 443
d5af91a1 444 spi_master_put(xspi->bitbang.master);
8fd8821b
GL
445
446 return 0;
447}
448
449/* work with hotplug and coldplug */
450MODULE_ALIAS("platform:" XILINX_SPI_NAME);
451
452static struct platform_driver xilinx_spi_driver = {
453 .probe = xilinx_spi_probe,
fd4a319b 454 .remove = xilinx_spi_remove,
8fd8821b
GL
455 .driver = {
456 .name = XILINX_SPI_NAME,
eae6cb31 457 .of_match_table = xilinx_spi_of_match,
8fd8821b
GL
458 },
459};
940ab889 460module_platform_driver(xilinx_spi_driver);
8fd8821b 461
ae918c02
AK
462MODULE_AUTHOR("MontaVista Software, Inc. <source@mvista.com>");
463MODULE_DESCRIPTION("Xilinx SPI driver");
464MODULE_LICENSE("GPL");
This page took 0.520894 seconds and 5 git commands to generate.