2 * Marvell Armada-3700 SPI controller driver
4 * Copyright (C) 2016 Marvell Ltd.
6 * Author: Wilson Ding <dingwei@marvell.com>
7 * Author: Romain Perier <romain.perier@free-electrons.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/clk.h>
15 #include <linux/completion.h>
16 #include <linux/delay.h>
17 #include <linux/err.h>
18 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
23 #include <linux/of_irq.h>
24 #include <linux/of_device.h>
25 #include <linux/pinctrl/consumer.h>
26 #include <linux/spi/spi.h>
28 #define DRIVER_NAME "armada_3700_spi"
30 #define A3700_SPI_MAX_SPEED_HZ 100000000
31 #define A3700_SPI_MAX_PRESCALE 30
32 #define A3700_SPI_TIMEOUT 10
34 /* SPI Register Offest */
35 #define A3700_SPI_IF_CTRL_REG 0x00
36 #define A3700_SPI_IF_CFG_REG 0x04
37 #define A3700_SPI_DATA_OUT_REG 0x08
38 #define A3700_SPI_DATA_IN_REG 0x0C
39 #define A3700_SPI_IF_INST_REG 0x10
40 #define A3700_SPI_IF_ADDR_REG 0x14
41 #define A3700_SPI_IF_RMODE_REG 0x18
42 #define A3700_SPI_IF_HDR_CNT_REG 0x1C
43 #define A3700_SPI_IF_DIN_CNT_REG 0x20
44 #define A3700_SPI_IF_TIME_REG 0x24
45 #define A3700_SPI_INT_STAT_REG 0x28
46 #define A3700_SPI_INT_MASK_REG 0x2C
48 /* A3700_SPI_IF_CTRL_REG */
49 #define A3700_SPI_EN BIT(16)
50 #define A3700_SPI_ADDR_NOT_CONFIG BIT(12)
51 #define A3700_SPI_WFIFO_OVERFLOW BIT(11)
52 #define A3700_SPI_WFIFO_UNDERFLOW BIT(10)
53 #define A3700_SPI_RFIFO_OVERFLOW BIT(9)
54 #define A3700_SPI_RFIFO_UNDERFLOW BIT(8)
55 #define A3700_SPI_WFIFO_FULL BIT(7)
56 #define A3700_SPI_WFIFO_EMPTY BIT(6)
57 #define A3700_SPI_RFIFO_FULL BIT(5)
58 #define A3700_SPI_RFIFO_EMPTY BIT(4)
59 #define A3700_SPI_WFIFO_RDY BIT(3)
60 #define A3700_SPI_RFIFO_RDY BIT(2)
61 #define A3700_SPI_XFER_RDY BIT(1)
62 #define A3700_SPI_XFER_DONE BIT(0)
64 /* A3700_SPI_IF_CFG_REG */
65 #define A3700_SPI_WFIFO_THRS BIT(28)
66 #define A3700_SPI_RFIFO_THRS BIT(24)
67 #define A3700_SPI_AUTO_CS BIT(20)
68 #define A3700_SPI_DMA_RD_EN BIT(18)
69 #define A3700_SPI_FIFO_MODE BIT(17)
70 #define A3700_SPI_SRST BIT(16)
71 #define A3700_SPI_XFER_START BIT(15)
72 #define A3700_SPI_XFER_STOP BIT(14)
73 #define A3700_SPI_INST_PIN BIT(13)
74 #define A3700_SPI_ADDR_PIN BIT(12)
75 #define A3700_SPI_DATA_PIN1 BIT(11)
76 #define A3700_SPI_DATA_PIN0 BIT(10)
77 #define A3700_SPI_FIFO_FLUSH BIT(9)
78 #define A3700_SPI_RW_EN BIT(8)
79 #define A3700_SPI_CLK_POL BIT(7)
80 #define A3700_SPI_CLK_PHA BIT(6)
81 #define A3700_SPI_BYTE_LEN BIT(5)
82 #define A3700_SPI_CLK_PRESCALE BIT(0)
83 #define A3700_SPI_CLK_PRESCALE_MASK (0x1f)
85 #define A3700_SPI_WFIFO_THRS_BIT 28
86 #define A3700_SPI_RFIFO_THRS_BIT 24
87 #define A3700_SPI_FIFO_THRS_MASK 0x7
89 #define A3700_SPI_DATA_PIN_MASK 0x3
91 /* A3700_SPI_IF_HDR_CNT_REG */
92 #define A3700_SPI_DUMMY_CNT_BIT 12
93 #define A3700_SPI_DUMMY_CNT_MASK 0x7
94 #define A3700_SPI_RMODE_CNT_BIT 8
95 #define A3700_SPI_RMODE_CNT_MASK 0x3
96 #define A3700_SPI_ADDR_CNT_BIT 4
97 #define A3700_SPI_ADDR_CNT_MASK 0x7
98 #define A3700_SPI_INSTR_CNT_BIT 0
99 #define A3700_SPI_INSTR_CNT_MASK 0x3
101 /* A3700_SPI_IF_TIME_REG */
102 #define A3700_SPI_CLK_CAPT_EDGE BIT(7)
105 struct spi_master *master;
116 struct completion done;
119 static u32 spireg_read(struct a3700_spi *a3700_spi, u32 offset)
121 return readl(a3700_spi->base + offset);
124 static void spireg_write(struct a3700_spi *a3700_spi, u32 offset, u32 data)
126 writel(data, a3700_spi->base + offset);
129 static void a3700_spi_auto_cs_unset(struct a3700_spi *a3700_spi)
133 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
134 val &= ~A3700_SPI_AUTO_CS;
135 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
138 static void a3700_spi_activate_cs(struct a3700_spi *a3700_spi, unsigned int cs)
142 val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
143 val |= (A3700_SPI_EN << cs);
144 spireg_write(a3700_spi, A3700_SPI_IF_CTRL_REG, val);
147 static void a3700_spi_deactivate_cs(struct a3700_spi *a3700_spi,
152 val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
153 val &= ~(A3700_SPI_EN << cs);
154 spireg_write(a3700_spi, A3700_SPI_IF_CTRL_REG, val);
157 static int a3700_spi_pin_mode_set(struct a3700_spi *a3700_spi,
158 unsigned int pin_mode, bool receiving)
162 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
163 val &= ~(A3700_SPI_INST_PIN | A3700_SPI_ADDR_PIN);
164 val &= ~(A3700_SPI_DATA_PIN0 | A3700_SPI_DATA_PIN1);
167 case SPI_NBITS_SINGLE:
170 val |= A3700_SPI_DATA_PIN0;
173 val |= A3700_SPI_DATA_PIN1;
174 /* RX during address reception uses 4-pin */
176 val |= A3700_SPI_ADDR_PIN;
179 dev_err(&a3700_spi->master->dev, "wrong pin mode %u", pin_mode);
183 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
188 static void a3700_spi_fifo_mode_set(struct a3700_spi *a3700_spi, bool enable)
192 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
194 val |= A3700_SPI_FIFO_MODE;
196 val &= ~A3700_SPI_FIFO_MODE;
197 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
200 static void a3700_spi_mode_set(struct a3700_spi *a3700_spi,
201 unsigned int mode_bits)
205 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
207 if (mode_bits & SPI_CPOL)
208 val |= A3700_SPI_CLK_POL;
210 val &= ~A3700_SPI_CLK_POL;
212 if (mode_bits & SPI_CPHA)
213 val |= A3700_SPI_CLK_PHA;
215 val &= ~A3700_SPI_CLK_PHA;
217 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
220 static void a3700_spi_clock_set(struct a3700_spi *a3700_spi,
221 unsigned int speed_hz)
226 prescale = DIV_ROUND_UP(clk_get_rate(a3700_spi->clk), speed_hz);
228 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
229 val = val & ~A3700_SPI_CLK_PRESCALE_MASK;
231 val = val | (prescale & A3700_SPI_CLK_PRESCALE_MASK);
232 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
235 val = spireg_read(a3700_spi, A3700_SPI_IF_TIME_REG);
236 val |= A3700_SPI_CLK_CAPT_EDGE;
237 spireg_write(a3700_spi, A3700_SPI_IF_TIME_REG, val);
241 static void a3700_spi_bytelen_set(struct a3700_spi *a3700_spi, unsigned int len)
245 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
247 val |= A3700_SPI_BYTE_LEN;
249 val &= ~A3700_SPI_BYTE_LEN;
250 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
252 a3700_spi->byte_len = len;
255 static int a3700_spi_fifo_flush(struct a3700_spi *a3700_spi)
257 int timeout = A3700_SPI_TIMEOUT;
260 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
261 val |= A3700_SPI_FIFO_FLUSH;
262 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
265 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
266 if (!(val & A3700_SPI_FIFO_FLUSH))
274 static int a3700_spi_init(struct a3700_spi *a3700_spi)
276 struct spi_master *master = a3700_spi->master;
281 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
282 val |= A3700_SPI_SRST;
283 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
285 udelay(A3700_SPI_TIMEOUT);
287 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
288 val &= ~A3700_SPI_SRST;
289 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
291 /* Disable AUTO_CS and deactivate all chip-selects */
292 a3700_spi_auto_cs_unset(a3700_spi);
293 for (i = 0; i < master->num_chipselect; i++)
294 a3700_spi_deactivate_cs(a3700_spi, i);
296 /* Enable FIFO mode */
297 a3700_spi_fifo_mode_set(a3700_spi, true);
300 a3700_spi_mode_set(a3700_spi, master->mode_bits);
303 spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, 0);
304 spireg_write(a3700_spi, A3700_SPI_IF_DIN_CNT_REG, 0);
306 /* Mask the interrupts and clear cause bits */
307 spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
308 spireg_write(a3700_spi, A3700_SPI_INT_STAT_REG, ~0U);
313 static irqreturn_t a3700_spi_interrupt(int irq, void *dev_id)
315 struct spi_master *master = dev_id;
316 struct a3700_spi *a3700_spi;
319 a3700_spi = spi_master_get_devdata(master);
321 /* Get interrupt causes */
322 cause = spireg_read(a3700_spi, A3700_SPI_INT_STAT_REG);
324 if (!cause || !(a3700_spi->wait_mask & cause))
327 /* mask and acknowledge the SPI interrupts */
328 spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
329 spireg_write(a3700_spi, A3700_SPI_INT_STAT_REG, cause);
331 /* Wake up the transfer */
332 complete(&a3700_spi->done);
337 static bool a3700_spi_wait_completion(struct spi_device *spi)
339 struct a3700_spi *a3700_spi;
340 unsigned int timeout;
341 unsigned int ctrl_reg;
342 unsigned long timeout_jiffies;
344 a3700_spi = spi_master_get_devdata(spi->master);
346 /* SPI interrupt is edge-triggered, which means an interrupt will
347 * be generated only when detecting a specific status bit changed
348 * from '0' to '1'. So when we start waiting for a interrupt, we
349 * need to check status bit in control reg first, if it is already 1,
350 * then we do not need to wait for interrupt
352 ctrl_reg = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
353 if (a3700_spi->wait_mask & ctrl_reg)
356 reinit_completion(&a3700_spi->done);
358 spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG,
359 a3700_spi->wait_mask);
361 timeout_jiffies = msecs_to_jiffies(A3700_SPI_TIMEOUT);
362 timeout = wait_for_completion_timeout(&a3700_spi->done,
365 a3700_spi->wait_mask = 0;
370 /* there might be the case that right after we checked the
371 * status bits in this routine and before start to wait for
372 * interrupt by wait_for_completion_timeout, the interrupt
373 * happens, to avoid missing it we need to double check
374 * status bits in control reg, if it is already 1, then
375 * consider that we have the interrupt successfully and
378 ctrl_reg = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
379 if (a3700_spi->wait_mask & ctrl_reg)
382 spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
384 /* Timeout was reached */
388 static bool a3700_spi_transfer_wait(struct spi_device *spi,
389 unsigned int bit_mask)
391 struct a3700_spi *a3700_spi;
393 a3700_spi = spi_master_get_devdata(spi->master);
394 a3700_spi->wait_mask = bit_mask;
396 return a3700_spi_wait_completion(spi);
399 static void a3700_spi_fifo_thres_set(struct a3700_spi *a3700_spi,
404 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
405 val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_RFIFO_THRS_BIT);
406 val |= (bytes - 1) << A3700_SPI_RFIFO_THRS_BIT;
407 val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_WFIFO_THRS_BIT);
408 val |= (7 - bytes) << A3700_SPI_WFIFO_THRS_BIT;
409 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
412 static void a3700_spi_transfer_setup(struct spi_device *spi,
413 struct spi_transfer *xfer)
415 struct a3700_spi *a3700_spi;
417 a3700_spi = spi_master_get_devdata(spi->master);
419 a3700_spi_clock_set(a3700_spi, xfer->speed_hz);
421 /* Use 4 bytes long transfers. Each transfer method has its way to deal
422 * with the remaining bytes for non 4-bytes aligned transfers.
424 a3700_spi_bytelen_set(a3700_spi, 4);
426 /* Initialize the working buffers */
427 a3700_spi->tx_buf = xfer->tx_buf;
428 a3700_spi->rx_buf = xfer->rx_buf;
429 a3700_spi->buf_len = xfer->len;
432 static void a3700_spi_set_cs(struct spi_device *spi, bool enable)
434 struct a3700_spi *a3700_spi = spi_master_get_devdata(spi->master);
437 a3700_spi_activate_cs(a3700_spi, spi->chip_select);
439 a3700_spi_deactivate_cs(a3700_spi, spi->chip_select);
442 static void a3700_spi_header_set(struct a3700_spi *a3700_spi)
444 unsigned int addr_cnt;
447 /* Clear the header registers */
448 spireg_write(a3700_spi, A3700_SPI_IF_INST_REG, 0);
449 spireg_write(a3700_spi, A3700_SPI_IF_ADDR_REG, 0);
450 spireg_write(a3700_spi, A3700_SPI_IF_RMODE_REG, 0);
451 spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, 0);
453 /* Set header counters */
454 if (a3700_spi->tx_buf) {
456 * when tx data is not 4 bytes aligned, there will be unexpected
457 * bytes out of SPI output register, since it always shifts out
458 * as whole 4 bytes. This might cause incorrect transaction with
459 * some devices. To avoid that, use SPI header count feature to
460 * transfer up to 3 bytes of data first, and then make the rest
461 * of data 4-byte aligned.
463 addr_cnt = a3700_spi->buf_len % 4;
465 val = (addr_cnt & A3700_SPI_ADDR_CNT_MASK)
466 << A3700_SPI_ADDR_CNT_BIT;
467 spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, val);
469 /* Update the buffer length to be transferred */
470 a3700_spi->buf_len -= addr_cnt;
472 /* transfer 1~3 bytes through address count */
475 val = (val << 8) | a3700_spi->tx_buf[0];
478 spireg_write(a3700_spi, A3700_SPI_IF_ADDR_REG, val);
483 static int a3700_is_wfifo_full(struct a3700_spi *a3700_spi)
487 val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
488 return (val & A3700_SPI_WFIFO_FULL);
491 static int a3700_spi_fifo_write(struct a3700_spi *a3700_spi)
495 while (!a3700_is_wfifo_full(a3700_spi) && a3700_spi->buf_len) {
496 val = *(u32 *)a3700_spi->tx_buf;
497 spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG, val);
498 a3700_spi->buf_len -= 4;
499 a3700_spi->tx_buf += 4;
505 static int a3700_is_rfifo_empty(struct a3700_spi *a3700_spi)
507 u32 val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
509 return (val & A3700_SPI_RFIFO_EMPTY);
512 static int a3700_spi_fifo_read(struct a3700_spi *a3700_spi)
516 while (!a3700_is_rfifo_empty(a3700_spi) && a3700_spi->buf_len) {
517 val = spireg_read(a3700_spi, A3700_SPI_DATA_IN_REG);
518 if (a3700_spi->buf_len >= 4) {
520 memcpy(a3700_spi->rx_buf, &val, 4);
522 a3700_spi->buf_len -= 4;
523 a3700_spi->rx_buf += 4;
526 * When remain bytes is not larger than 4, we should
527 * avoid memory overwriting and just write the left rx
530 while (a3700_spi->buf_len) {
531 *a3700_spi->rx_buf = val & 0xff;
534 a3700_spi->buf_len--;
543 static void a3700_spi_transfer_abort_fifo(struct a3700_spi *a3700_spi)
545 int timeout = A3700_SPI_TIMEOUT;
548 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
549 val |= A3700_SPI_XFER_STOP;
550 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
553 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
554 if (!(val & A3700_SPI_XFER_START))
559 a3700_spi_fifo_flush(a3700_spi);
561 val &= ~A3700_SPI_XFER_STOP;
562 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
565 static int a3700_spi_prepare_message(struct spi_master *master,
566 struct spi_message *message)
568 struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
569 struct spi_device *spi = message->spi;
572 ret = clk_enable(a3700_spi->clk);
574 dev_err(&spi->dev, "failed to enable clk with error %d\n", ret);
578 /* Flush the FIFOs */
579 ret = a3700_spi_fifo_flush(a3700_spi);
583 a3700_spi_mode_set(a3700_spi, spi->mode);
588 static int a3700_spi_transfer_one_fifo(struct spi_master *master,
589 struct spi_device *spi,
590 struct spi_transfer *xfer)
592 struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
593 int ret = 0, timeout = A3700_SPI_TIMEOUT;
594 unsigned int nbits = 0, byte_len;
597 /* Make sure we use FIFO mode */
598 a3700_spi_fifo_mode_set(a3700_spi, true);
600 /* Configure FIFO thresholds */
601 byte_len = xfer->bits_per_word >> 3;
602 a3700_spi_fifo_thres_set(a3700_spi, byte_len);
605 nbits = xfer->tx_nbits;
606 else if (xfer->rx_buf)
607 nbits = xfer->rx_nbits;
609 a3700_spi_pin_mode_set(a3700_spi, nbits, xfer->rx_buf ? true : false);
611 /* Flush the FIFOs */
612 a3700_spi_fifo_flush(a3700_spi);
614 /* Transfer first bytes of data when buffer is not 4-byte aligned */
615 a3700_spi_header_set(a3700_spi);
618 /* Clear WFIFO, since it's last 2 bytes are shifted out during
621 spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG, 0);
623 /* Set read data length */
624 spireg_write(a3700_spi, A3700_SPI_IF_DIN_CNT_REG,
626 /* Start READ transfer */
627 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
628 val &= ~A3700_SPI_RW_EN;
629 val |= A3700_SPI_XFER_START;
630 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
631 } else if (xfer->tx_buf) {
632 /* Start Write transfer */
633 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
634 val |= (A3700_SPI_XFER_START | A3700_SPI_RW_EN);
635 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
638 * If there are data to be written to the SPI device, xmit_data
639 * flag is set true; otherwise the instruction in SPI_INSTR does
640 * not require data to be written to the SPI device, then
641 * xmit_data flag is set false.
643 a3700_spi->xmit_data = (a3700_spi->buf_len != 0);
646 while (a3700_spi->buf_len) {
647 if (a3700_spi->tx_buf) {
648 /* Wait wfifo ready */
649 if (!a3700_spi_transfer_wait(spi,
650 A3700_SPI_WFIFO_RDY)) {
652 "wait wfifo ready timed out\n");
656 /* Fill up the wfifo */
657 ret = a3700_spi_fifo_write(a3700_spi);
660 } else if (a3700_spi->rx_buf) {
661 /* Wait rfifo ready */
662 if (!a3700_spi_transfer_wait(spi,
663 A3700_SPI_RFIFO_RDY)) {
665 "wait rfifo ready timed out\n");
669 /* Drain out the rfifo */
670 ret = a3700_spi_fifo_read(a3700_spi);
677 * Stop a write transfer in fifo mode:
678 * - wait all the bytes in wfifo to be shifted out
679 * - set XFER_STOP bit
680 * - wait XFER_START bit clear
681 * - clear XFER_STOP bit
682 * Stop a read transfer in fifo mode:
683 * - the hardware is to reset the XFER_START bit
684 * after the number of bytes indicated in DIN_CNT
686 * - just wait XFER_START bit clear
688 if (a3700_spi->tx_buf) {
689 if (a3700_spi->xmit_data) {
691 * If there are data written to the SPI device, wait
692 * until SPI_WFIFO_EMPTY is 1 to wait for all data to
693 * transfer out of write FIFO.
695 if (!a3700_spi_transfer_wait(spi,
696 A3700_SPI_WFIFO_EMPTY)) {
697 dev_err(&spi->dev, "wait wfifo empty timed out\n");
702 if (!a3700_spi_transfer_wait(spi, A3700_SPI_XFER_RDY)) {
703 dev_err(&spi->dev, "wait xfer ready timed out\n");
707 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
708 val |= A3700_SPI_XFER_STOP;
709 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
713 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
714 if (!(val & A3700_SPI_XFER_START))
720 dev_err(&spi->dev, "wait transfer start clear timed out\n");
725 val &= ~A3700_SPI_XFER_STOP;
726 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
730 a3700_spi_transfer_abort_fifo(a3700_spi);
732 spi_finalize_current_transfer(master);
737 static int a3700_spi_transfer_one_full_duplex(struct spi_master *master,
738 struct spi_device *spi,
739 struct spi_transfer *xfer)
741 struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
744 /* Disable FIFO mode */
745 a3700_spi_fifo_mode_set(a3700_spi, false);
747 while (a3700_spi->buf_len) {
749 /* When we have less than 4 bytes to transfer, switch to 1 byte
750 * mode. This is reset after each transfer
752 if (a3700_spi->buf_len < 4)
753 a3700_spi_bytelen_set(a3700_spi, 1);
755 if (a3700_spi->byte_len == 1)
756 val_out = *a3700_spi->tx_buf;
758 val_out = cpu_to_le32(*(u32 *)a3700_spi->tx_buf);
760 spireg_write(a3700_spi, A3700_SPI_DATA_OUT_REG, val_out);
762 /* Wait for all the data to be shifted in / out */
763 while (!(spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG) &
764 A3700_SPI_XFER_DONE))
767 val_in = le32_to_cpu(spireg_read(a3700_spi,
768 A3700_SPI_DATA_IN_REG));
770 memcpy(a3700_spi->rx_buf, &val_in, a3700_spi->byte_len);
772 a3700_spi->buf_len -= a3700_spi->byte_len;
773 a3700_spi->tx_buf += a3700_spi->byte_len;
774 a3700_spi->rx_buf += a3700_spi->byte_len;
778 spi_finalize_current_transfer(master);
783 static int a3700_spi_transfer_one(struct spi_master *master,
784 struct spi_device *spi,
785 struct spi_transfer *xfer)
787 a3700_spi_transfer_setup(spi, xfer);
789 if (xfer->tx_buf && xfer->rx_buf)
790 return a3700_spi_transfer_one_full_duplex(master, spi, xfer);
792 return a3700_spi_transfer_one_fifo(master, spi, xfer);
795 static int a3700_spi_unprepare_message(struct spi_master *master,
796 struct spi_message *message)
798 struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
800 clk_disable(a3700_spi->clk);
805 static const struct of_device_id a3700_spi_dt_ids[] = {
806 { .compatible = "marvell,armada-3700-spi", .data = NULL },
810 MODULE_DEVICE_TABLE(of, a3700_spi_dt_ids);
812 static int a3700_spi_probe(struct platform_device *pdev)
814 struct device *dev = &pdev->dev;
815 struct device_node *of_node = dev->of_node;
816 struct resource *res;
817 struct spi_master *master;
818 struct a3700_spi *spi;
822 master = spi_alloc_master(dev, sizeof(*spi));
824 dev_err(dev, "master allocation failed\n");
829 if (of_property_read_u32(of_node, "num-cs", &num_cs)) {
830 dev_err(dev, "could not find num-cs\n");
835 master->bus_num = pdev->id;
836 master->dev.of_node = of_node;
837 master->mode_bits = SPI_MODE_3;
838 master->num_chipselect = num_cs;
839 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(32);
840 master->prepare_message = a3700_spi_prepare_message;
841 master->transfer_one = a3700_spi_transfer_one;
842 master->unprepare_message = a3700_spi_unprepare_message;
843 master->set_cs = a3700_spi_set_cs;
844 master->mode_bits |= (SPI_RX_DUAL | SPI_TX_DUAL |
845 SPI_RX_QUAD | SPI_TX_QUAD);
847 platform_set_drvdata(pdev, master);
849 spi = spi_master_get_devdata(master);
850 memset(spi, 0, sizeof(struct a3700_spi));
852 spi->master = master;
854 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
855 spi->base = devm_ioremap_resource(dev, res);
856 if (IS_ERR(spi->base)) {
857 ret = PTR_ERR(spi->base);
861 irq = platform_get_irq(pdev, 0);
863 dev_err(dev, "could not get irq: %d\n", irq);
869 init_completion(&spi->done);
871 spi->clk = devm_clk_get(dev, NULL);
872 if (IS_ERR(spi->clk)) {
873 dev_err(dev, "could not find clk: %ld\n", PTR_ERR(spi->clk));
877 ret = clk_prepare(spi->clk);
879 dev_err(dev, "could not prepare clk: %d\n", ret);
883 master->max_speed_hz = min_t(unsigned long, A3700_SPI_MAX_SPEED_HZ,
884 clk_get_rate(spi->clk));
885 master->min_speed_hz = DIV_ROUND_UP(clk_get_rate(spi->clk),
886 A3700_SPI_MAX_PRESCALE);
888 ret = a3700_spi_init(spi);
892 ret = devm_request_irq(dev, spi->irq, a3700_spi_interrupt, 0,
893 dev_name(dev), master);
895 dev_err(dev, "could not request IRQ: %d\n", ret);
899 ret = devm_spi_register_master(dev, master);
901 dev_err(dev, "Failed to register master\n");
908 clk_disable_unprepare(spi->clk);
910 spi_master_put(master);
915 static int a3700_spi_remove(struct platform_device *pdev)
917 struct spi_master *master = platform_get_drvdata(pdev);
918 struct a3700_spi *spi = spi_master_get_devdata(master);
920 clk_unprepare(spi->clk);
925 static struct platform_driver a3700_spi_driver = {
928 .of_match_table = of_match_ptr(a3700_spi_dt_ids),
930 .probe = a3700_spi_probe,
931 .remove = a3700_spi_remove,
934 module_platform_driver(a3700_spi_driver);
936 MODULE_DESCRIPTION("Armada-3700 SPI driver");
937 MODULE_AUTHOR("Wilson Ding <dingwei@marvell.com>");
938 MODULE_LICENSE("GPL");
939 MODULE_ALIAS("platform:" DRIVER_NAME);