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_TIMEOUT 10
32 /* SPI Register Offest */
33 #define A3700_SPI_IF_CTRL_REG 0x00
34 #define A3700_SPI_IF_CFG_REG 0x04
35 #define A3700_SPI_DATA_OUT_REG 0x08
36 #define A3700_SPI_DATA_IN_REG 0x0C
37 #define A3700_SPI_IF_INST_REG 0x10
38 #define A3700_SPI_IF_ADDR_REG 0x14
39 #define A3700_SPI_IF_RMODE_REG 0x18
40 #define A3700_SPI_IF_HDR_CNT_REG 0x1C
41 #define A3700_SPI_IF_DIN_CNT_REG 0x20
42 #define A3700_SPI_IF_TIME_REG 0x24
43 #define A3700_SPI_INT_STAT_REG 0x28
44 #define A3700_SPI_INT_MASK_REG 0x2C
46 /* A3700_SPI_IF_CTRL_REG */
47 #define A3700_SPI_EN BIT(16)
48 #define A3700_SPI_ADDR_NOT_CONFIG BIT(12)
49 #define A3700_SPI_WFIFO_OVERFLOW BIT(11)
50 #define A3700_SPI_WFIFO_UNDERFLOW BIT(10)
51 #define A3700_SPI_RFIFO_OVERFLOW BIT(9)
52 #define A3700_SPI_RFIFO_UNDERFLOW BIT(8)
53 #define A3700_SPI_WFIFO_FULL BIT(7)
54 #define A3700_SPI_WFIFO_EMPTY BIT(6)
55 #define A3700_SPI_RFIFO_FULL BIT(5)
56 #define A3700_SPI_RFIFO_EMPTY BIT(4)
57 #define A3700_SPI_WFIFO_RDY BIT(3)
58 #define A3700_SPI_RFIFO_RDY BIT(2)
59 #define A3700_SPI_XFER_RDY BIT(1)
60 #define A3700_SPI_XFER_DONE BIT(0)
62 /* A3700_SPI_IF_CFG_REG */
63 #define A3700_SPI_WFIFO_THRS BIT(28)
64 #define A3700_SPI_RFIFO_THRS BIT(24)
65 #define A3700_SPI_AUTO_CS BIT(20)
66 #define A3700_SPI_DMA_RD_EN BIT(18)
67 #define A3700_SPI_FIFO_MODE BIT(17)
68 #define A3700_SPI_SRST BIT(16)
69 #define A3700_SPI_XFER_START BIT(15)
70 #define A3700_SPI_XFER_STOP BIT(14)
71 #define A3700_SPI_INST_PIN BIT(13)
72 #define A3700_SPI_ADDR_PIN BIT(12)
73 #define A3700_SPI_DATA_PIN1 BIT(11)
74 #define A3700_SPI_DATA_PIN0 BIT(10)
75 #define A3700_SPI_FIFO_FLUSH BIT(9)
76 #define A3700_SPI_RW_EN BIT(8)
77 #define A3700_SPI_CLK_POL BIT(7)
78 #define A3700_SPI_CLK_PHA BIT(6)
79 #define A3700_SPI_BYTE_LEN BIT(5)
80 #define A3700_SPI_CLK_PRESCALE BIT(0)
81 #define A3700_SPI_CLK_PRESCALE_MASK (0x1f)
83 #define A3700_SPI_WFIFO_THRS_BIT 28
84 #define A3700_SPI_RFIFO_THRS_BIT 24
85 #define A3700_SPI_FIFO_THRS_MASK 0x7
87 #define A3700_SPI_DATA_PIN_MASK 0x3
89 /* A3700_SPI_IF_HDR_CNT_REG */
90 #define A3700_SPI_DUMMY_CNT_BIT 12
91 #define A3700_SPI_DUMMY_CNT_MASK 0x7
92 #define A3700_SPI_RMODE_CNT_BIT 8
93 #define A3700_SPI_RMODE_CNT_MASK 0x3
94 #define A3700_SPI_ADDR_CNT_BIT 4
95 #define A3700_SPI_ADDR_CNT_MASK 0x7
96 #define A3700_SPI_INSTR_CNT_BIT 0
97 #define A3700_SPI_INSTR_CNT_MASK 0x3
99 /* A3700_SPI_IF_TIME_REG */
100 #define A3700_SPI_CLK_CAPT_EDGE BIT(7)
103 struct spi_master *master;
114 struct completion done;
117 static u32 spireg_read(struct a3700_spi *a3700_spi, u32 offset)
119 return readl(a3700_spi->base + offset);
122 static void spireg_write(struct a3700_spi *a3700_spi, u32 offset, u32 data)
124 writel(data, a3700_spi->base + offset);
127 static void a3700_spi_auto_cs_unset(struct a3700_spi *a3700_spi)
131 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
132 val &= ~A3700_SPI_AUTO_CS;
133 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
136 static void a3700_spi_activate_cs(struct a3700_spi *a3700_spi, unsigned int cs)
140 val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
141 val |= (A3700_SPI_EN << cs);
142 spireg_write(a3700_spi, A3700_SPI_IF_CTRL_REG, val);
145 static void a3700_spi_deactivate_cs(struct a3700_spi *a3700_spi,
150 val = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
151 val &= ~(A3700_SPI_EN << cs);
152 spireg_write(a3700_spi, A3700_SPI_IF_CTRL_REG, val);
155 static int a3700_spi_pin_mode_set(struct a3700_spi *a3700_spi,
156 unsigned int pin_mode, bool receiving)
160 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
161 val &= ~(A3700_SPI_INST_PIN | A3700_SPI_ADDR_PIN);
162 val &= ~(A3700_SPI_DATA_PIN0 | A3700_SPI_DATA_PIN1);
165 case SPI_NBITS_SINGLE:
168 val |= A3700_SPI_DATA_PIN0;
171 val |= A3700_SPI_DATA_PIN1;
172 /* RX during address reception uses 4-pin */
174 val |= A3700_SPI_ADDR_PIN;
177 dev_err(&a3700_spi->master->dev, "wrong pin mode %u", pin_mode);
181 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
186 static void a3700_spi_fifo_mode_set(struct a3700_spi *a3700_spi)
190 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
191 val |= A3700_SPI_FIFO_MODE;
192 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
195 static void a3700_spi_mode_set(struct a3700_spi *a3700_spi,
196 unsigned int mode_bits)
200 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
202 if (mode_bits & SPI_CPOL)
203 val |= A3700_SPI_CLK_POL;
205 val &= ~A3700_SPI_CLK_POL;
207 if (mode_bits & SPI_CPHA)
208 val |= A3700_SPI_CLK_PHA;
210 val &= ~A3700_SPI_CLK_PHA;
212 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
215 static void a3700_spi_clock_set(struct a3700_spi *a3700_spi,
216 unsigned int speed_hz, u16 mode)
221 prescale = DIV_ROUND_UP(clk_get_rate(a3700_spi->clk), speed_hz);
223 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
224 val = val & ~A3700_SPI_CLK_PRESCALE_MASK;
226 val = val | (prescale & A3700_SPI_CLK_PRESCALE_MASK);
227 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
230 val = spireg_read(a3700_spi, A3700_SPI_IF_TIME_REG);
231 val |= A3700_SPI_CLK_CAPT_EDGE;
232 spireg_write(a3700_spi, A3700_SPI_IF_TIME_REG, val);
235 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
236 val &= ~(A3700_SPI_CLK_POL | A3700_SPI_CLK_PHA);
239 val |= A3700_SPI_CLK_POL;
242 val |= A3700_SPI_CLK_PHA;
244 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
247 static void a3700_spi_bytelen_set(struct a3700_spi *a3700_spi, unsigned int len)
251 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
253 val |= A3700_SPI_BYTE_LEN;
255 val &= ~A3700_SPI_BYTE_LEN;
256 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
258 a3700_spi->byte_len = len;
261 static int a3700_spi_fifo_flush(struct a3700_spi *a3700_spi)
263 int timeout = A3700_SPI_TIMEOUT;
266 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
267 val |= A3700_SPI_FIFO_FLUSH;
268 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
271 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
272 if (!(val & A3700_SPI_FIFO_FLUSH))
280 static int a3700_spi_init(struct a3700_spi *a3700_spi)
282 struct spi_master *master = a3700_spi->master;
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 udelay(A3700_SPI_TIMEOUT);
293 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
294 val &= ~A3700_SPI_SRST;
295 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
297 /* Disable AUTO_CS and deactivate all chip-selects */
298 a3700_spi_auto_cs_unset(a3700_spi);
299 for (i = 0; i < master->num_chipselect; i++)
300 a3700_spi_deactivate_cs(a3700_spi, i);
302 /* Enable FIFO mode */
303 a3700_spi_fifo_mode_set(a3700_spi);
306 a3700_spi_mode_set(a3700_spi, master->mode_bits);
309 spireg_write(a3700_spi, A3700_SPI_IF_HDR_CNT_REG, 0);
310 spireg_write(a3700_spi, A3700_SPI_IF_DIN_CNT_REG, 0);
312 /* Mask the interrupts and clear cause bits */
313 spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
314 spireg_write(a3700_spi, A3700_SPI_INT_STAT_REG, ~0U);
319 static irqreturn_t a3700_spi_interrupt(int irq, void *dev_id)
321 struct spi_master *master = dev_id;
322 struct a3700_spi *a3700_spi;
325 a3700_spi = spi_master_get_devdata(master);
327 /* Get interrupt causes */
328 cause = spireg_read(a3700_spi, A3700_SPI_INT_STAT_REG);
330 if (!cause || !(a3700_spi->wait_mask & cause))
333 /* mask and acknowledge the SPI interrupts */
334 spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
335 spireg_write(a3700_spi, A3700_SPI_INT_STAT_REG, cause);
337 /* Wake up the transfer */
338 complete(&a3700_spi->done);
343 static bool a3700_spi_wait_completion(struct spi_device *spi)
345 struct a3700_spi *a3700_spi;
346 unsigned int timeout;
347 unsigned int ctrl_reg;
348 unsigned long timeout_jiffies;
350 a3700_spi = spi_master_get_devdata(spi->master);
352 /* SPI interrupt is edge-triggered, which means an interrupt will
353 * be generated only when detecting a specific status bit changed
354 * from '0' to '1'. So when we start waiting for a interrupt, we
355 * need to check status bit in control reg first, if it is already 1,
356 * then we do not need to wait for interrupt
358 ctrl_reg = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
359 if (a3700_spi->wait_mask & ctrl_reg)
362 reinit_completion(&a3700_spi->done);
364 spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG,
365 a3700_spi->wait_mask);
367 timeout_jiffies = msecs_to_jiffies(A3700_SPI_TIMEOUT);
368 timeout = wait_for_completion_timeout(&a3700_spi->done,
371 a3700_spi->wait_mask = 0;
376 /* there might be the case that right after we checked the
377 * status bits in this routine and before start to wait for
378 * interrupt by wait_for_completion_timeout, the interrupt
379 * happens, to avoid missing it we need to double check
380 * status bits in control reg, if it is already 1, then
381 * consider that we have the interrupt successfully and
384 ctrl_reg = spireg_read(a3700_spi, A3700_SPI_IF_CTRL_REG);
385 if (a3700_spi->wait_mask & ctrl_reg)
388 spireg_write(a3700_spi, A3700_SPI_INT_MASK_REG, 0);
393 static bool a3700_spi_transfer_wait(struct spi_device *spi,
394 unsigned int bit_mask)
396 struct a3700_spi *a3700_spi;
398 a3700_spi = spi_master_get_devdata(spi->master);
399 a3700_spi->wait_mask = bit_mask;
401 return a3700_spi_wait_completion(spi);
404 static void a3700_spi_fifo_thres_set(struct a3700_spi *a3700_spi,
409 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
410 val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_RFIFO_THRS_BIT);
411 val |= (bytes - 1) << A3700_SPI_RFIFO_THRS_BIT;
412 val &= ~(A3700_SPI_FIFO_THRS_MASK << A3700_SPI_WFIFO_THRS_BIT);
413 val |= (7 - bytes) << A3700_SPI_WFIFO_THRS_BIT;
414 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
417 static void a3700_spi_transfer_setup(struct spi_device *spi,
418 struct spi_transfer *xfer)
420 struct a3700_spi *a3700_spi;
421 unsigned int byte_len;
423 a3700_spi = spi_master_get_devdata(spi->master);
425 a3700_spi_clock_set(a3700_spi, xfer->speed_hz, spi->mode);
427 byte_len = xfer->bits_per_word >> 3;
429 a3700_spi_fifo_thres_set(a3700_spi, byte_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 = cpu_to_le32(*(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) {
519 u32 data = le32_to_cpu(val);
521 memcpy(a3700_spi->rx_buf, &data, 4);
523 a3700_spi->buf_len -= 4;
524 a3700_spi->rx_buf += 4;
527 * When remain bytes is not larger than 4, we should
528 * avoid memory overwriting and just write the left rx
531 while (a3700_spi->buf_len) {
532 *a3700_spi->rx_buf = val & 0xff;
535 a3700_spi->buf_len--;
544 static void a3700_spi_transfer_abort_fifo(struct a3700_spi *a3700_spi)
546 int timeout = A3700_SPI_TIMEOUT;
549 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
550 val |= A3700_SPI_XFER_STOP;
551 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
554 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
555 if (!(val & A3700_SPI_XFER_START))
560 a3700_spi_fifo_flush(a3700_spi);
562 val &= ~A3700_SPI_XFER_STOP;
563 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
566 static int a3700_spi_prepare_message(struct spi_master *master,
567 struct spi_message *message)
569 struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
570 struct spi_device *spi = message->spi;
573 ret = clk_enable(a3700_spi->clk);
575 dev_err(&spi->dev, "failed to enable clk with error %d\n", ret);
579 /* Flush the FIFOs */
580 ret = a3700_spi_fifo_flush(a3700_spi);
584 a3700_spi_bytelen_set(a3700_spi, 4);
589 static int a3700_spi_transfer_one(struct spi_master *master,
590 struct spi_device *spi,
591 struct spi_transfer *xfer)
593 struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
594 int ret = 0, timeout = A3700_SPI_TIMEOUT;
595 unsigned int nbits = 0;
598 a3700_spi_transfer_setup(spi, xfer);
600 a3700_spi->tx_buf = xfer->tx_buf;
601 a3700_spi->rx_buf = xfer->rx_buf;
602 a3700_spi->buf_len = xfer->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 /* Set read data length */
619 spireg_write(a3700_spi, A3700_SPI_IF_DIN_CNT_REG,
621 /* Start READ transfer */
622 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
623 val &= ~A3700_SPI_RW_EN;
624 val |= A3700_SPI_XFER_START;
625 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
626 } else if (xfer->tx_buf) {
627 /* Start Write transfer */
628 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
629 val |= (A3700_SPI_XFER_START | A3700_SPI_RW_EN);
630 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
633 * If there are data to be written to the SPI device, xmit_data
634 * flag is set true; otherwise the instruction in SPI_INSTR does
635 * not require data to be written to the SPI device, then
636 * xmit_data flag is set false.
638 a3700_spi->xmit_data = (a3700_spi->buf_len != 0);
641 while (a3700_spi->buf_len) {
642 if (a3700_spi->tx_buf) {
643 /* Wait wfifo ready */
644 if (!a3700_spi_transfer_wait(spi,
645 A3700_SPI_WFIFO_RDY)) {
647 "wait wfifo ready timed out\n");
651 /* Fill up the wfifo */
652 ret = a3700_spi_fifo_write(a3700_spi);
655 } else if (a3700_spi->rx_buf) {
656 /* Wait rfifo ready */
657 if (!a3700_spi_transfer_wait(spi,
658 A3700_SPI_RFIFO_RDY)) {
660 "wait rfifo ready timed out\n");
664 /* Drain out the rfifo */
665 ret = a3700_spi_fifo_read(a3700_spi);
672 * Stop a write transfer in fifo mode:
673 * - wait all the bytes in wfifo to be shifted out
674 * - set XFER_STOP bit
675 * - wait XFER_START bit clear
676 * - clear XFER_STOP bit
677 * Stop a read transfer in fifo mode:
678 * - the hardware is to reset the XFER_START bit
679 * after the number of bytes indicated in DIN_CNT
681 * - just wait XFER_START bit clear
683 if (a3700_spi->tx_buf) {
684 if (a3700_spi->xmit_data) {
686 * If there are data written to the SPI device, wait
687 * until SPI_WFIFO_EMPTY is 1 to wait for all data to
688 * transfer out of write FIFO.
690 if (!a3700_spi_transfer_wait(spi,
691 A3700_SPI_WFIFO_EMPTY)) {
692 dev_err(&spi->dev, "wait wfifo empty timed out\n");
697 if (!a3700_spi_transfer_wait(spi, A3700_SPI_XFER_RDY)) {
698 dev_err(&spi->dev, "wait xfer ready timed out\n");
702 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
703 val |= A3700_SPI_XFER_STOP;
704 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
708 val = spireg_read(a3700_spi, A3700_SPI_IF_CFG_REG);
709 if (!(val & A3700_SPI_XFER_START))
715 dev_err(&spi->dev, "wait transfer start clear timed out\n");
720 val &= ~A3700_SPI_XFER_STOP;
721 spireg_write(a3700_spi, A3700_SPI_IF_CFG_REG, val);
725 a3700_spi_transfer_abort_fifo(a3700_spi);
727 spi_finalize_current_transfer(master);
732 static int a3700_spi_unprepare_message(struct spi_master *master,
733 struct spi_message *message)
735 struct a3700_spi *a3700_spi = spi_master_get_devdata(master);
737 clk_disable(a3700_spi->clk);
742 static const struct of_device_id a3700_spi_dt_ids[] = {
743 { .compatible = "marvell,armada-3700-spi", .data = NULL },
747 MODULE_DEVICE_TABLE(of, a3700_spi_dt_ids);
749 static int a3700_spi_probe(struct platform_device *pdev)
751 struct device *dev = &pdev->dev;
752 struct device_node *of_node = dev->of_node;
753 struct resource *res;
754 struct spi_master *master;
755 struct a3700_spi *spi;
759 master = spi_alloc_master(dev, sizeof(*spi));
761 dev_err(dev, "master allocation failed\n");
766 if (of_property_read_u32(of_node, "num-cs", &num_cs)) {
767 dev_err(dev, "could not find num-cs\n");
772 master->bus_num = pdev->id;
773 master->dev.of_node = of_node;
774 master->mode_bits = SPI_MODE_3;
775 master->num_chipselect = num_cs;
776 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(32);
777 master->prepare_message = a3700_spi_prepare_message;
778 master->transfer_one = a3700_spi_transfer_one;
779 master->unprepare_message = a3700_spi_unprepare_message;
780 master->set_cs = a3700_spi_set_cs;
781 master->flags = SPI_MASTER_HALF_DUPLEX;
782 master->mode_bits |= (SPI_RX_DUAL | SPI_TX_DUAL |
783 SPI_RX_QUAD | SPI_TX_QUAD);
785 platform_set_drvdata(pdev, master);
787 spi = spi_master_get_devdata(master);
788 memset(spi, 0, sizeof(struct a3700_spi));
790 spi->master = master;
792 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
793 spi->base = devm_ioremap_resource(dev, res);
794 if (IS_ERR(spi->base)) {
795 ret = PTR_ERR(spi->base);
799 irq = platform_get_irq(pdev, 0);
801 dev_err(dev, "could not get irq: %d\n", irq);
807 init_completion(&spi->done);
809 spi->clk = devm_clk_get(dev, NULL);
810 if (IS_ERR(spi->clk)) {
811 dev_err(dev, "could not find clk: %ld\n", PTR_ERR(spi->clk));
815 ret = clk_prepare(spi->clk);
817 dev_err(dev, "could not prepare clk: %d\n", ret);
821 ret = a3700_spi_init(spi);
825 ret = devm_request_irq(dev, spi->irq, a3700_spi_interrupt, 0,
826 dev_name(dev), master);
828 dev_err(dev, "could not request IRQ: %d\n", ret);
832 ret = devm_spi_register_master(dev, master);
834 dev_err(dev, "Failed to register master\n");
841 clk_disable_unprepare(spi->clk);
843 spi_master_put(master);
848 static int a3700_spi_remove(struct platform_device *pdev)
850 struct spi_master *master = platform_get_drvdata(pdev);
851 struct a3700_spi *spi = spi_master_get_devdata(master);
853 clk_unprepare(spi->clk);
858 static struct platform_driver a3700_spi_driver = {
861 .of_match_table = of_match_ptr(a3700_spi_dt_ids),
863 .probe = a3700_spi_probe,
864 .remove = a3700_spi_remove,
867 module_platform_driver(a3700_spi_driver);
869 MODULE_DESCRIPTION("Armada-3700 SPI driver");
870 MODULE_AUTHOR("Wilson Ding <dingwei@marvell.com>");
871 MODULE_LICENSE("GPL");
872 MODULE_ALIAS("platform:" DRIVER_NAME);