1 // SPDX-License-Identifier: GPL-2.0-only
3 * Marvell Armada-3700 SPI controller driver
5 * Copyright (C) 2016 Marvell Ltd.
7 * Author: Wilson Ding <dingwei@marvell.com>
8 * Author: Romain Perier <romain.perier@free-electrons.com>
11 #include <linux/clk.h>
12 #include <linux/completion.h>
13 #include <linux/delay.h>
14 #include <linux/err.h>
15 #include <linux/interrupt.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
20 #include <linux/of_irq.h>
21 #include <linux/of_device.h>
22 #include <linux/pinctrl/consumer.h>
23 #include <linux/spi/spi.h>
25 #define DRIVER_NAME "armada_3700_spi"
27 #define A3700_SPI_MAX_SPEED_HZ 100000000
28 #define A3700_SPI_MAX_PRESCALE 30
29 #define A3700_SPI_TIMEOUT 10
31 /* SPI Register Offest */
32 #define A3700_SPI_IF_CTRL_REG 0x00
33 #define A3700_SPI_IF_CFG_REG 0x04
34 #define A3700_SPI_DATA_OUT_REG 0x08
35 #define A3700_SPI_DATA_IN_REG 0x0C
36 #define A3700_SPI_IF_INST_REG 0x10
37 #define A3700_SPI_IF_ADDR_REG 0x14
38 #define A3700_SPI_IF_RMODE_REG 0x18
39 #define A3700_SPI_IF_HDR_CNT_REG 0x1C
40 #define A3700_SPI_IF_DIN_CNT_REG 0x20
41 #define A3700_SPI_IF_TIME_REG 0x24
42 #define A3700_SPI_INT_STAT_REG 0x28
43 #define A3700_SPI_INT_MASK_REG 0x2C
45 /* A3700_SPI_IF_CTRL_REG */
46 #define A3700_SPI_EN BIT(16)
47 #define A3700_SPI_ADDR_NOT_CONFIG BIT(12)
48 #define A3700_SPI_WFIFO_OVERFLOW BIT(11)
49 #define A3700_SPI_WFIFO_UNDERFLOW BIT(10)
50 #define A3700_SPI_RFIFO_OVERFLOW BIT(9)
51 #define A3700_SPI_RFIFO_UNDERFLOW BIT(8)
52 #define A3700_SPI_WFIFO_FULL BIT(7)
53 #define A3700_SPI_WFIFO_EMPTY BIT(6)
54 #define A3700_SPI_RFIFO_FULL BIT(5)
55 #define A3700_SPI_RFIFO_EMPTY BIT(4)
56 #define A3700_SPI_WFIFO_RDY BIT(3)
57 #define A3700_SPI_RFIFO_RDY BIT(2)
58 #define A3700_SPI_XFER_RDY BIT(1)
59 #define A3700_SPI_XFER_DONE BIT(0)
61 /* A3700_SPI_IF_CFG_REG */
62 #define A3700_SPI_WFIFO_THRS BIT(28)
63 #define A3700_SPI_RFIFO_THRS BIT(24)
64 #define A3700_SPI_AUTO_CS BIT(20)
65 #define A3700_SPI_DMA_RD_EN BIT(18)
66 #define A3700_SPI_FIFO_MODE BIT(17)
67 #define A3700_SPI_SRST BIT(16)
68 #define A3700_SPI_XFER_START BIT(15)
69 #define A3700_SPI_XFER_STOP BIT(14)
70 #define A3700_SPI_INST_PIN BIT(13)
71 #define A3700_SPI_ADDR_PIN BIT(12)
72 #define A3700_SPI_DATA_PIN1 BIT(11)
73 #define A3700_SPI_DATA_PIN0 BIT(10)
74 #define A3700_SPI_FIFO_FLUSH BIT(9)
75 #define A3700_SPI_RW_EN BIT(8)
76 #define A3700_SPI_CLK_POL BIT(7)
77 #define A3700_SPI_CLK_PHA BIT(6)
78 #define A3700_SPI_BYTE_LEN BIT(5)
79 #define A3700_SPI_CLK_PRESCALE BIT(0)
80 #define A3700_SPI_CLK_PRESCALE_MASK (0x1f)
81 #define A3700_SPI_CLK_EVEN_OFFS (0x10)
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
, bool enable
)
190 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CFG_REG
);
192 val
|= A3700_SPI_FIFO_MODE
;
194 val
&= ~A3700_SPI_FIFO_MODE
;
195 spireg_write(a3700_spi
, A3700_SPI_IF_CFG_REG
, val
);
198 static void a3700_spi_mode_set(struct a3700_spi
*a3700_spi
,
199 unsigned int mode_bits
)
203 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CFG_REG
);
205 if (mode_bits
& SPI_CPOL
)
206 val
|= A3700_SPI_CLK_POL
;
208 val
&= ~A3700_SPI_CLK_POL
;
210 if (mode_bits
& SPI_CPHA
)
211 val
|= A3700_SPI_CLK_PHA
;
213 val
&= ~A3700_SPI_CLK_PHA
;
215 spireg_write(a3700_spi
, A3700_SPI_IF_CFG_REG
, val
);
218 static void a3700_spi_clock_set(struct a3700_spi
*a3700_spi
,
219 unsigned int speed_hz
)
224 prescale
= DIV_ROUND_UP(clk_get_rate(a3700_spi
->clk
), speed_hz
);
226 /* For prescaler values over 15, we can only set it by steps of 2.
227 * Starting from A3700_SPI_CLK_EVEN_OFFS, we set values from 0 up to
228 * 30. We only use this range from 16 to 30.
231 prescale
= A3700_SPI_CLK_EVEN_OFFS
+ DIV_ROUND_UP(prescale
, 2);
233 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CFG_REG
);
234 val
= val
& ~A3700_SPI_CLK_PRESCALE_MASK
;
236 val
= val
| (prescale
& A3700_SPI_CLK_PRESCALE_MASK
);
237 spireg_write(a3700_spi
, A3700_SPI_IF_CFG_REG
, val
);
240 val
= spireg_read(a3700_spi
, A3700_SPI_IF_TIME_REG
);
241 val
|= A3700_SPI_CLK_CAPT_EDGE
;
242 spireg_write(a3700_spi
, A3700_SPI_IF_TIME_REG
, val
);
246 static void a3700_spi_bytelen_set(struct a3700_spi
*a3700_spi
, unsigned int len
)
250 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CFG_REG
);
252 val
|= A3700_SPI_BYTE_LEN
;
254 val
&= ~A3700_SPI_BYTE_LEN
;
255 spireg_write(a3700_spi
, A3700_SPI_IF_CFG_REG
, val
);
257 a3700_spi
->byte_len
= len
;
260 static int a3700_spi_fifo_flush(struct a3700_spi
*a3700_spi
)
262 int timeout
= A3700_SPI_TIMEOUT
;
265 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CFG_REG
);
266 val
|= A3700_SPI_FIFO_FLUSH
;
267 spireg_write(a3700_spi
, A3700_SPI_IF_CFG_REG
, val
);
270 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CFG_REG
);
271 if (!(val
& A3700_SPI_FIFO_FLUSH
))
279 static void a3700_spi_init(struct a3700_spi
*a3700_spi
)
281 struct spi_master
*master
= a3700_spi
->master
;
286 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CFG_REG
);
287 val
|= A3700_SPI_SRST
;
288 spireg_write(a3700_spi
, A3700_SPI_IF_CFG_REG
, val
);
290 udelay(A3700_SPI_TIMEOUT
);
292 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CFG_REG
);
293 val
&= ~A3700_SPI_SRST
;
294 spireg_write(a3700_spi
, A3700_SPI_IF_CFG_REG
, val
);
296 /* Disable AUTO_CS and deactivate all chip-selects */
297 a3700_spi_auto_cs_unset(a3700_spi
);
298 for (i
= 0; i
< master
->num_chipselect
; i
++)
299 a3700_spi_deactivate_cs(a3700_spi
, i
);
301 /* Enable FIFO mode */
302 a3700_spi_fifo_mode_set(a3700_spi
, true);
305 a3700_spi_mode_set(a3700_spi
, master
->mode_bits
);
308 spireg_write(a3700_spi
, A3700_SPI_IF_HDR_CNT_REG
, 0);
309 spireg_write(a3700_spi
, A3700_SPI_IF_DIN_CNT_REG
, 0);
311 /* Mask the interrupts and clear cause bits */
312 spireg_write(a3700_spi
, A3700_SPI_INT_MASK_REG
, 0);
313 spireg_write(a3700_spi
, A3700_SPI_INT_STAT_REG
, ~0U);
316 static irqreturn_t
a3700_spi_interrupt(int irq
, void *dev_id
)
318 struct spi_master
*master
= dev_id
;
319 struct a3700_spi
*a3700_spi
;
322 a3700_spi
= spi_master_get_devdata(master
);
324 /* Get interrupt causes */
325 cause
= spireg_read(a3700_spi
, A3700_SPI_INT_STAT_REG
);
327 if (!cause
|| !(a3700_spi
->wait_mask
& cause
))
330 /* mask and acknowledge the SPI interrupts */
331 spireg_write(a3700_spi
, A3700_SPI_INT_MASK_REG
, 0);
332 spireg_write(a3700_spi
, A3700_SPI_INT_STAT_REG
, cause
);
334 /* Wake up the transfer */
335 complete(&a3700_spi
->done
);
340 static bool a3700_spi_wait_completion(struct spi_device
*spi
)
342 struct a3700_spi
*a3700_spi
;
343 unsigned int timeout
;
344 unsigned int ctrl_reg
;
345 unsigned long timeout_jiffies
;
347 a3700_spi
= spi_master_get_devdata(spi
->master
);
349 /* SPI interrupt is edge-triggered, which means an interrupt will
350 * be generated only when detecting a specific status bit changed
351 * from '0' to '1'. So when we start waiting for a interrupt, we
352 * need to check status bit in control reg first, if it is already 1,
353 * then we do not need to wait for interrupt
355 ctrl_reg
= spireg_read(a3700_spi
, A3700_SPI_IF_CTRL_REG
);
356 if (a3700_spi
->wait_mask
& ctrl_reg
)
359 reinit_completion(&a3700_spi
->done
);
361 spireg_write(a3700_spi
, A3700_SPI_INT_MASK_REG
,
362 a3700_spi
->wait_mask
);
364 timeout_jiffies
= msecs_to_jiffies(A3700_SPI_TIMEOUT
);
365 timeout
= wait_for_completion_timeout(&a3700_spi
->done
,
368 a3700_spi
->wait_mask
= 0;
373 /* there might be the case that right after we checked the
374 * status bits in this routine and before start to wait for
375 * interrupt by wait_for_completion_timeout, the interrupt
376 * happens, to avoid missing it we need to double check
377 * status bits in control reg, if it is already 1, then
378 * consider that we have the interrupt successfully and
381 ctrl_reg
= spireg_read(a3700_spi
, A3700_SPI_IF_CTRL_REG
);
382 if (a3700_spi
->wait_mask
& ctrl_reg
)
385 spireg_write(a3700_spi
, A3700_SPI_INT_MASK_REG
, 0);
387 /* Timeout was reached */
391 static bool a3700_spi_transfer_wait(struct spi_device
*spi
,
392 unsigned int bit_mask
)
394 struct a3700_spi
*a3700_spi
;
396 a3700_spi
= spi_master_get_devdata(spi
->master
);
397 a3700_spi
->wait_mask
= bit_mask
;
399 return a3700_spi_wait_completion(spi
);
402 static void a3700_spi_fifo_thres_set(struct a3700_spi
*a3700_spi
,
407 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CFG_REG
);
408 val
&= ~(A3700_SPI_FIFO_THRS_MASK
<< A3700_SPI_RFIFO_THRS_BIT
);
409 val
|= (bytes
- 1) << A3700_SPI_RFIFO_THRS_BIT
;
410 val
&= ~(A3700_SPI_FIFO_THRS_MASK
<< A3700_SPI_WFIFO_THRS_BIT
);
411 val
|= (7 - bytes
) << A3700_SPI_WFIFO_THRS_BIT
;
412 spireg_write(a3700_spi
, A3700_SPI_IF_CFG_REG
, val
);
415 static void a3700_spi_transfer_setup(struct spi_device
*spi
,
416 struct spi_transfer
*xfer
)
418 struct a3700_spi
*a3700_spi
;
420 a3700_spi
= spi_master_get_devdata(spi
->master
);
422 a3700_spi_clock_set(a3700_spi
, xfer
->speed_hz
);
424 /* Use 4 bytes long transfers. Each transfer method has its way to deal
425 * with the remaining bytes for non 4-bytes aligned transfers.
427 a3700_spi_bytelen_set(a3700_spi
, 4);
429 /* Initialize the working buffers */
430 a3700_spi
->tx_buf
= xfer
->tx_buf
;
431 a3700_spi
->rx_buf
= xfer
->rx_buf
;
432 a3700_spi
->buf_len
= xfer
->len
;
435 static void a3700_spi_set_cs(struct spi_device
*spi
, bool enable
)
437 struct a3700_spi
*a3700_spi
= spi_master_get_devdata(spi
->master
);
440 a3700_spi_activate_cs(a3700_spi
, spi
->chip_select
);
442 a3700_spi_deactivate_cs(a3700_spi
, spi
->chip_select
);
445 static void a3700_spi_header_set(struct a3700_spi
*a3700_spi
)
447 unsigned int addr_cnt
;
450 /* Clear the header registers */
451 spireg_write(a3700_spi
, A3700_SPI_IF_INST_REG
, 0);
452 spireg_write(a3700_spi
, A3700_SPI_IF_ADDR_REG
, 0);
453 spireg_write(a3700_spi
, A3700_SPI_IF_RMODE_REG
, 0);
454 spireg_write(a3700_spi
, A3700_SPI_IF_HDR_CNT_REG
, 0);
456 /* Set header counters */
457 if (a3700_spi
->tx_buf
) {
459 * when tx data is not 4 bytes aligned, there will be unexpected
460 * bytes out of SPI output register, since it always shifts out
461 * as whole 4 bytes. This might cause incorrect transaction with
462 * some devices. To avoid that, use SPI header count feature to
463 * transfer up to 3 bytes of data first, and then make the rest
464 * of data 4-byte aligned.
466 addr_cnt
= a3700_spi
->buf_len
% 4;
468 val
= (addr_cnt
& A3700_SPI_ADDR_CNT_MASK
)
469 << A3700_SPI_ADDR_CNT_BIT
;
470 spireg_write(a3700_spi
, A3700_SPI_IF_HDR_CNT_REG
, val
);
472 /* Update the buffer length to be transferred */
473 a3700_spi
->buf_len
-= addr_cnt
;
475 /* transfer 1~3 bytes through address count */
478 val
= (val
<< 8) | a3700_spi
->tx_buf
[0];
481 spireg_write(a3700_spi
, A3700_SPI_IF_ADDR_REG
, val
);
486 static int a3700_is_wfifo_full(struct a3700_spi
*a3700_spi
)
490 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CTRL_REG
);
491 return (val
& A3700_SPI_WFIFO_FULL
);
494 static int a3700_spi_fifo_write(struct a3700_spi
*a3700_spi
)
498 while (!a3700_is_wfifo_full(a3700_spi
) && a3700_spi
->buf_len
) {
499 val
= *(u32
*)a3700_spi
->tx_buf
;
500 spireg_write(a3700_spi
, A3700_SPI_DATA_OUT_REG
, val
);
501 a3700_spi
->buf_len
-= 4;
502 a3700_spi
->tx_buf
+= 4;
508 static int a3700_is_rfifo_empty(struct a3700_spi
*a3700_spi
)
510 u32 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CTRL_REG
);
512 return (val
& A3700_SPI_RFIFO_EMPTY
);
515 static int a3700_spi_fifo_read(struct a3700_spi
*a3700_spi
)
519 while (!a3700_is_rfifo_empty(a3700_spi
) && a3700_spi
->buf_len
) {
520 val
= spireg_read(a3700_spi
, A3700_SPI_DATA_IN_REG
);
521 if (a3700_spi
->buf_len
>= 4) {
523 memcpy(a3700_spi
->rx_buf
, &val
, 4);
525 a3700_spi
->buf_len
-= 4;
526 a3700_spi
->rx_buf
+= 4;
529 * When remain bytes is not larger than 4, we should
530 * avoid memory overwriting and just write the left rx
533 while (a3700_spi
->buf_len
) {
534 *a3700_spi
->rx_buf
= val
& 0xff;
537 a3700_spi
->buf_len
--;
546 static void a3700_spi_transfer_abort_fifo(struct a3700_spi
*a3700_spi
)
548 int timeout
= A3700_SPI_TIMEOUT
;
551 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CFG_REG
);
552 val
|= A3700_SPI_XFER_STOP
;
553 spireg_write(a3700_spi
, A3700_SPI_IF_CFG_REG
, val
);
556 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CFG_REG
);
557 if (!(val
& A3700_SPI_XFER_START
))
562 a3700_spi_fifo_flush(a3700_spi
);
564 val
&= ~A3700_SPI_XFER_STOP
;
565 spireg_write(a3700_spi
, A3700_SPI_IF_CFG_REG
, val
);
568 static int a3700_spi_prepare_message(struct spi_master
*master
,
569 struct spi_message
*message
)
571 struct a3700_spi
*a3700_spi
= spi_master_get_devdata(master
);
572 struct spi_device
*spi
= message
->spi
;
575 ret
= clk_enable(a3700_spi
->clk
);
577 dev_err(&spi
->dev
, "failed to enable clk with error %d\n", ret
);
581 /* Flush the FIFOs */
582 ret
= a3700_spi_fifo_flush(a3700_spi
);
586 a3700_spi_mode_set(a3700_spi
, spi
->mode
);
591 static int a3700_spi_transfer_one_fifo(struct spi_master
*master
,
592 struct spi_device
*spi
,
593 struct spi_transfer
*xfer
)
595 struct a3700_spi
*a3700_spi
= spi_master_get_devdata(master
);
596 int ret
= 0, timeout
= A3700_SPI_TIMEOUT
;
597 unsigned int nbits
= 0, byte_len
;
600 /* Make sure we use FIFO mode */
601 a3700_spi_fifo_mode_set(a3700_spi
, true);
603 /* Configure FIFO thresholds */
604 byte_len
= xfer
->bits_per_word
>> 3;
605 a3700_spi_fifo_thres_set(a3700_spi
, byte_len
);
608 nbits
= xfer
->tx_nbits
;
609 else if (xfer
->rx_buf
)
610 nbits
= xfer
->rx_nbits
;
612 a3700_spi_pin_mode_set(a3700_spi
, nbits
, xfer
->rx_buf
? true : false);
614 /* Flush the FIFOs */
615 a3700_spi_fifo_flush(a3700_spi
);
617 /* Transfer first bytes of data when buffer is not 4-byte aligned */
618 a3700_spi_header_set(a3700_spi
);
621 /* Clear WFIFO, since it's last 2 bytes are shifted out during
624 spireg_write(a3700_spi
, A3700_SPI_DATA_OUT_REG
, 0);
626 /* Set read data length */
627 spireg_write(a3700_spi
, A3700_SPI_IF_DIN_CNT_REG
,
629 /* Start READ transfer */
630 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CFG_REG
);
631 val
&= ~A3700_SPI_RW_EN
;
632 val
|= A3700_SPI_XFER_START
;
633 spireg_write(a3700_spi
, A3700_SPI_IF_CFG_REG
, val
);
634 } else if (xfer
->tx_buf
) {
635 /* Start Write transfer */
636 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CFG_REG
);
637 val
|= (A3700_SPI_XFER_START
| A3700_SPI_RW_EN
);
638 spireg_write(a3700_spi
, A3700_SPI_IF_CFG_REG
, val
);
641 * If there are data to be written to the SPI device, xmit_data
642 * flag is set true; otherwise the instruction in SPI_INSTR does
643 * not require data to be written to the SPI device, then
644 * xmit_data flag is set false.
646 a3700_spi
->xmit_data
= (a3700_spi
->buf_len
!= 0);
649 while (a3700_spi
->buf_len
) {
650 if (a3700_spi
->tx_buf
) {
651 /* Wait wfifo ready */
652 if (!a3700_spi_transfer_wait(spi
,
653 A3700_SPI_WFIFO_RDY
)) {
655 "wait wfifo ready timed out\n");
659 /* Fill up the wfifo */
660 ret
= a3700_spi_fifo_write(a3700_spi
);
663 } else if (a3700_spi
->rx_buf
) {
664 /* Wait rfifo ready */
665 if (!a3700_spi_transfer_wait(spi
,
666 A3700_SPI_RFIFO_RDY
)) {
668 "wait rfifo ready timed out\n");
672 /* Drain out the rfifo */
673 ret
= a3700_spi_fifo_read(a3700_spi
);
680 * Stop a write transfer in fifo mode:
681 * - wait all the bytes in wfifo to be shifted out
682 * - set XFER_STOP bit
683 * - wait XFER_START bit clear
684 * - clear XFER_STOP bit
685 * Stop a read transfer in fifo mode:
686 * - the hardware is to reset the XFER_START bit
687 * after the number of bytes indicated in DIN_CNT
689 * - just wait XFER_START bit clear
691 if (a3700_spi
->tx_buf
) {
692 if (a3700_spi
->xmit_data
) {
694 * If there are data written to the SPI device, wait
695 * until SPI_WFIFO_EMPTY is 1 to wait for all data to
696 * transfer out of write FIFO.
698 if (!a3700_spi_transfer_wait(spi
,
699 A3700_SPI_WFIFO_EMPTY
)) {
700 dev_err(&spi
->dev
, "wait wfifo empty timed out\n");
705 if (!a3700_spi_transfer_wait(spi
, A3700_SPI_XFER_RDY
)) {
706 dev_err(&spi
->dev
, "wait xfer ready timed out\n");
710 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CFG_REG
);
711 val
|= A3700_SPI_XFER_STOP
;
712 spireg_write(a3700_spi
, A3700_SPI_IF_CFG_REG
, val
);
716 val
= spireg_read(a3700_spi
, A3700_SPI_IF_CFG_REG
);
717 if (!(val
& A3700_SPI_XFER_START
))
723 dev_err(&spi
->dev
, "wait transfer start clear timed out\n");
728 val
&= ~A3700_SPI_XFER_STOP
;
729 spireg_write(a3700_spi
, A3700_SPI_IF_CFG_REG
, val
);
733 a3700_spi_transfer_abort_fifo(a3700_spi
);
735 spi_finalize_current_transfer(master
);
740 static int a3700_spi_transfer_one_full_duplex(struct spi_master
*master
,
741 struct spi_device
*spi
,
742 struct spi_transfer
*xfer
)
744 struct a3700_spi
*a3700_spi
= spi_master_get_devdata(master
);
747 /* Disable FIFO mode */
748 a3700_spi_fifo_mode_set(a3700_spi
, false);
750 while (a3700_spi
->buf_len
) {
752 /* When we have less than 4 bytes to transfer, switch to 1 byte
753 * mode. This is reset after each transfer
755 if (a3700_spi
->buf_len
< 4)
756 a3700_spi_bytelen_set(a3700_spi
, 1);
758 if (a3700_spi
->byte_len
== 1)
759 val
= *a3700_spi
->tx_buf
;
761 val
= *(u32
*)a3700_spi
->tx_buf
;
763 spireg_write(a3700_spi
, A3700_SPI_DATA_OUT_REG
, val
);
765 /* Wait for all the data to be shifted in / out */
766 while (!(spireg_read(a3700_spi
, A3700_SPI_IF_CTRL_REG
) &
767 A3700_SPI_XFER_DONE
))
770 val
= spireg_read(a3700_spi
, A3700_SPI_DATA_IN_REG
);
772 memcpy(a3700_spi
->rx_buf
, &val
, a3700_spi
->byte_len
);
774 a3700_spi
->buf_len
-= a3700_spi
->byte_len
;
775 a3700_spi
->tx_buf
+= a3700_spi
->byte_len
;
776 a3700_spi
->rx_buf
+= a3700_spi
->byte_len
;
780 spi_finalize_current_transfer(master
);
785 static int a3700_spi_transfer_one(struct spi_master
*master
,
786 struct spi_device
*spi
,
787 struct spi_transfer
*xfer
)
789 a3700_spi_transfer_setup(spi
, xfer
);
791 if (xfer
->tx_buf
&& xfer
->rx_buf
)
792 return a3700_spi_transfer_one_full_duplex(master
, spi
, xfer
);
794 return a3700_spi_transfer_one_fifo(master
, spi
, xfer
);
797 static int a3700_spi_unprepare_message(struct spi_master
*master
,
798 struct spi_message
*message
)
800 struct a3700_spi
*a3700_spi
= spi_master_get_devdata(master
);
802 clk_disable(a3700_spi
->clk
);
807 static const struct of_device_id a3700_spi_dt_ids
[] = {
808 { .compatible
= "marvell,armada-3700-spi", .data
= NULL
},
812 MODULE_DEVICE_TABLE(of
, a3700_spi_dt_ids
);
814 static int a3700_spi_probe(struct platform_device
*pdev
)
816 struct device
*dev
= &pdev
->dev
;
817 struct device_node
*of_node
= dev
->of_node
;
818 struct spi_master
*master
;
819 struct a3700_spi
*spi
;
823 master
= spi_alloc_master(dev
, sizeof(*spi
));
825 dev_err(dev
, "master allocation failed\n");
830 if (of_property_read_u32(of_node
, "num-cs", &num_cs
)) {
831 dev_err(dev
, "could not find num-cs\n");
836 master
->bus_num
= pdev
->id
;
837 master
->dev
.of_node
= of_node
;
838 master
->mode_bits
= SPI_MODE_3
;
839 master
->num_chipselect
= num_cs
;
840 master
->bits_per_word_mask
= SPI_BPW_MASK(8) | SPI_BPW_MASK(32);
841 master
->prepare_message
= a3700_spi_prepare_message
;
842 master
->transfer_one
= a3700_spi_transfer_one
;
843 master
->unprepare_message
= a3700_spi_unprepare_message
;
844 master
->set_cs
= a3700_spi_set_cs
;
845 master
->mode_bits
|= (SPI_RX_DUAL
| SPI_TX_DUAL
|
846 SPI_RX_QUAD
| SPI_TX_QUAD
);
848 platform_set_drvdata(pdev
, master
);
850 spi
= spi_master_get_devdata(master
);
852 spi
->master
= master
;
854 spi
->base
= devm_platform_ioremap_resource(pdev
, 0);
855 if (IS_ERR(spi
->base
)) {
856 ret
= PTR_ERR(spi
->base
);
860 irq
= platform_get_irq(pdev
, 0);
867 init_completion(&spi
->done
);
869 spi
->clk
= devm_clk_get(dev
, NULL
);
870 if (IS_ERR(spi
->clk
)) {
871 dev_err(dev
, "could not find clk: %ld\n", PTR_ERR(spi
->clk
));
875 ret
= clk_prepare(spi
->clk
);
877 dev_err(dev
, "could not prepare clk: %d\n", ret
);
881 master
->max_speed_hz
= min_t(unsigned long, A3700_SPI_MAX_SPEED_HZ
,
882 clk_get_rate(spi
->clk
));
883 master
->min_speed_hz
= DIV_ROUND_UP(clk_get_rate(spi
->clk
),
884 A3700_SPI_MAX_PRESCALE
);
888 ret
= devm_request_irq(dev
, spi
->irq
, a3700_spi_interrupt
, 0,
889 dev_name(dev
), master
);
891 dev_err(dev
, "could not request IRQ: %d\n", ret
);
895 ret
= devm_spi_register_master(dev
, master
);
897 dev_err(dev
, "Failed to register master\n");
904 clk_disable_unprepare(spi
->clk
);
906 spi_master_put(master
);
911 static int a3700_spi_remove(struct platform_device
*pdev
)
913 struct spi_master
*master
= platform_get_drvdata(pdev
);
914 struct a3700_spi
*spi
= spi_master_get_devdata(master
);
916 clk_unprepare(spi
->clk
);
921 static struct platform_driver a3700_spi_driver
= {
924 .of_match_table
= of_match_ptr(a3700_spi_dt_ids
),
926 .probe
= a3700_spi_probe
,
927 .remove
= a3700_spi_remove
,
930 module_platform_driver(a3700_spi_driver
);
932 MODULE_DESCRIPTION("Armada-3700 SPI driver");
933 MODULE_AUTHOR("Wilson Ding <dingwei@marvell.com>");
934 MODULE_LICENSE("GPL");
935 MODULE_ALIAS("platform:" DRIVER_NAME
);