2 * SPI bus driver for CSR SiRFprimaII
4 * Copyright (c) 2011 Cambridge Silicon Radio Limited, a CSR plc group company.
6 * Licensed under GPLv2 or later.
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/clk.h>
13 #include <linux/interrupt.h>
16 #include <linux/bitops.h>
17 #include <linux/err.h>
18 #include <linux/platform_device.h>
19 #include <linux/of_gpio.h>
20 #include <linux/spi/spi.h>
21 #include <linux/spi/spi_bitbang.h>
22 #include <linux/dmaengine.h>
23 #include <linux/dma-direction.h>
24 #include <linux/dma-mapping.h>
26 #define DRIVER_NAME "sirfsoc_spi"
28 #define SIRFSOC_SPI_CTRL 0x0000
29 #define SIRFSOC_SPI_CMD 0x0004
30 #define SIRFSOC_SPI_TX_RX_EN 0x0008
31 #define SIRFSOC_SPI_INT_EN 0x000C
32 #define SIRFSOC_SPI_INT_STATUS 0x0010
33 #define SIRFSOC_SPI_TX_DMA_IO_CTRL 0x0100
34 #define SIRFSOC_SPI_TX_DMA_IO_LEN 0x0104
35 #define SIRFSOC_SPI_TXFIFO_CTRL 0x0108
36 #define SIRFSOC_SPI_TXFIFO_LEVEL_CHK 0x010C
37 #define SIRFSOC_SPI_TXFIFO_OP 0x0110
38 #define SIRFSOC_SPI_TXFIFO_STATUS 0x0114
39 #define SIRFSOC_SPI_TXFIFO_DATA 0x0118
40 #define SIRFSOC_SPI_RX_DMA_IO_CTRL 0x0120
41 #define SIRFSOC_SPI_RX_DMA_IO_LEN 0x0124
42 #define SIRFSOC_SPI_RXFIFO_CTRL 0x0128
43 #define SIRFSOC_SPI_RXFIFO_LEVEL_CHK 0x012C
44 #define SIRFSOC_SPI_RXFIFO_OP 0x0130
45 #define SIRFSOC_SPI_RXFIFO_STATUS 0x0134
46 #define SIRFSOC_SPI_RXFIFO_DATA 0x0138
47 #define SIRFSOC_SPI_DUMMY_DELAY_CTL 0x0144
49 /* SPI CTRL register defines */
50 #define SIRFSOC_SPI_SLV_MODE BIT(16)
51 #define SIRFSOC_SPI_CMD_MODE BIT(17)
52 #define SIRFSOC_SPI_CS_IO_OUT BIT(18)
53 #define SIRFSOC_SPI_CS_IO_MODE BIT(19)
54 #define SIRFSOC_SPI_CLK_IDLE_STAT BIT(20)
55 #define SIRFSOC_SPI_CS_IDLE_STAT BIT(21)
56 #define SIRFSOC_SPI_TRAN_MSB BIT(22)
57 #define SIRFSOC_SPI_DRV_POS_EDGE BIT(23)
58 #define SIRFSOC_SPI_CS_HOLD_TIME BIT(24)
59 #define SIRFSOC_SPI_CLK_SAMPLE_MODE BIT(25)
60 #define SIRFSOC_SPI_TRAN_DAT_FORMAT_8 (0 << 26)
61 #define SIRFSOC_SPI_TRAN_DAT_FORMAT_12 (1 << 26)
62 #define SIRFSOC_SPI_TRAN_DAT_FORMAT_16 (2 << 26)
63 #define SIRFSOC_SPI_TRAN_DAT_FORMAT_32 (3 << 26)
64 #define SIRFSOC_SPI_CMD_BYTE_NUM(x) ((x & 3) << 28)
65 #define SIRFSOC_SPI_ENA_AUTO_CLR BIT(30)
66 #define SIRFSOC_SPI_MUL_DAT_MODE BIT(31)
68 /* Interrupt Enable */
69 #define SIRFSOC_SPI_RX_DONE_INT_EN BIT(0)
70 #define SIRFSOC_SPI_TX_DONE_INT_EN BIT(1)
71 #define SIRFSOC_SPI_RX_OFLOW_INT_EN BIT(2)
72 #define SIRFSOC_SPI_TX_UFLOW_INT_EN BIT(3)
73 #define SIRFSOC_SPI_RX_IO_DMA_INT_EN BIT(4)
74 #define SIRFSOC_SPI_TX_IO_DMA_INT_EN BIT(5)
75 #define SIRFSOC_SPI_RXFIFO_FULL_INT_EN BIT(6)
76 #define SIRFSOC_SPI_TXFIFO_EMPTY_INT_EN BIT(7)
77 #define SIRFSOC_SPI_RXFIFO_THD_INT_EN BIT(8)
78 #define SIRFSOC_SPI_TXFIFO_THD_INT_EN BIT(9)
79 #define SIRFSOC_SPI_FRM_END_INT_EN BIT(10)
81 #define SIRFSOC_SPI_INT_MASK_ALL 0x1FFF
83 /* Interrupt status */
84 #define SIRFSOC_SPI_RX_DONE BIT(0)
85 #define SIRFSOC_SPI_TX_DONE BIT(1)
86 #define SIRFSOC_SPI_RX_OFLOW BIT(2)
87 #define SIRFSOC_SPI_TX_UFLOW BIT(3)
88 #define SIRFSOC_SPI_RX_FIFO_FULL BIT(6)
89 #define SIRFSOC_SPI_TXFIFO_EMPTY BIT(7)
90 #define SIRFSOC_SPI_RXFIFO_THD_REACH BIT(8)
91 #define SIRFSOC_SPI_TXFIFO_THD_REACH BIT(9)
92 #define SIRFSOC_SPI_FRM_END BIT(10)
95 #define SIRFSOC_SPI_RX_EN BIT(0)
96 #define SIRFSOC_SPI_TX_EN BIT(1)
97 #define SIRFSOC_SPI_CMD_TX_EN BIT(2)
99 #define SIRFSOC_SPI_IO_MODE_SEL BIT(0)
100 #define SIRFSOC_SPI_RX_DMA_FLUSH BIT(2)
103 #define SIRFSOC_SPI_FIFO_RESET BIT(0)
104 #define SIRFSOC_SPI_FIFO_START BIT(1)
107 #define SIRFSOC_SPI_FIFO_WIDTH_BYTE (0 << 0)
108 #define SIRFSOC_SPI_FIFO_WIDTH_WORD (1 << 0)
109 #define SIRFSOC_SPI_FIFO_WIDTH_DWORD (2 << 0)
112 #define SIRFSOC_SPI_FIFO_LEVEL_MASK 0xFF
113 #define SIRFSOC_SPI_FIFO_FULL BIT(8)
114 #define SIRFSOC_SPI_FIFO_EMPTY BIT(9)
116 /* 256 bytes rx/tx FIFO */
117 #define SIRFSOC_SPI_FIFO_SIZE 256
118 #define SIRFSOC_SPI_DAT_FRM_LEN_MAX (64 * 1024)
120 #define SIRFSOC_SPI_FIFO_SC(x) ((x) & 0x3F)
121 #define SIRFSOC_SPI_FIFO_LC(x) (((x) & 0x3F) << 10)
122 #define SIRFSOC_SPI_FIFO_HC(x) (((x) & 0x3F) << 20)
123 #define SIRFSOC_SPI_FIFO_THD(x) (((x) & 0xFF) << 2)
126 * only if the rx/tx buffer and transfer size are 4-bytes aligned, we use dma
127 * due to the limitation of dma controller
130 #define ALIGNED(x) (!((u32)x & 0x3))
131 #define IS_DMA_VALID(x) (x && ALIGNED(x->tx_buf) && ALIGNED(x->rx_buf) && \
132 ALIGNED(x->len) && (x->len < 2 * PAGE_SIZE))
134 #define SIRFSOC_MAX_CMD_BYTES 4
137 struct spi_bitbang bitbang
;
138 struct completion rx_done
;
139 struct completion tx_done
;
142 u32 ctrl_freq
; /* SPI controller clock speed */
145 /* rx & tx bufs from the spi_transfer */
149 /* place received word into rx buffer */
150 void (*rx_word
) (struct sirfsoc_spi
*);
151 /* get word from tx buffer for sending */
152 void (*tx_word
) (struct sirfsoc_spi
*);
154 /* number of words left to be tranmitted/received */
155 unsigned int left_tx_word
;
156 unsigned int left_rx_word
;
158 /* rx & tx DMA channels */
159 struct dma_chan
*rx_chan
;
160 struct dma_chan
*tx_chan
;
161 dma_addr_t src_start
;
162 dma_addr_t dst_start
;
164 int word_width
; /* in bytes */
167 * if tx size is not more than 4 and rx size is NULL, use
175 static void spi_sirfsoc_rx_word_u8(struct sirfsoc_spi
*sspi
)
180 data
= readl(sspi
->base
+ SIRFSOC_SPI_RXFIFO_DATA
);
187 sspi
->left_rx_word
--;
190 static void spi_sirfsoc_tx_word_u8(struct sirfsoc_spi
*sspi
)
193 const u8
*tx
= sspi
->tx
;
200 writel(data
, sspi
->base
+ SIRFSOC_SPI_TXFIFO_DATA
);
201 sspi
->left_tx_word
--;
204 static void spi_sirfsoc_rx_word_u16(struct sirfsoc_spi
*sspi
)
209 data
= readl(sspi
->base
+ SIRFSOC_SPI_RXFIFO_DATA
);
216 sspi
->left_rx_word
--;
219 static void spi_sirfsoc_tx_word_u16(struct sirfsoc_spi
*sspi
)
222 const u16
*tx
= sspi
->tx
;
229 writel(data
, sspi
->base
+ SIRFSOC_SPI_TXFIFO_DATA
);
230 sspi
->left_tx_word
--;
233 static void spi_sirfsoc_rx_word_u32(struct sirfsoc_spi
*sspi
)
238 data
= readl(sspi
->base
+ SIRFSOC_SPI_RXFIFO_DATA
);
245 sspi
->left_rx_word
--;
249 static void spi_sirfsoc_tx_word_u32(struct sirfsoc_spi
*sspi
)
252 const u32
*tx
= sspi
->tx
;
259 writel(data
, sspi
->base
+ SIRFSOC_SPI_TXFIFO_DATA
);
260 sspi
->left_tx_word
--;
263 static irqreturn_t
spi_sirfsoc_irq(int irq
, void *dev_id
)
265 struct sirfsoc_spi
*sspi
= dev_id
;
266 u32 spi_stat
= readl(sspi
->base
+ SIRFSOC_SPI_INT_STATUS
);
268 writel(spi_stat
, sspi
->base
+ SIRFSOC_SPI_INT_STATUS
);
270 if (sspi
->tx_by_cmd
&& (spi_stat
& SIRFSOC_SPI_FRM_END
)) {
271 complete(&sspi
->tx_done
);
272 writel(0x0, sspi
->base
+ SIRFSOC_SPI_INT_EN
);
276 /* Error Conditions */
277 if (spi_stat
& SIRFSOC_SPI_RX_OFLOW
||
278 spi_stat
& SIRFSOC_SPI_TX_UFLOW
) {
279 complete(&sspi
->rx_done
);
280 writel(0x0, sspi
->base
+ SIRFSOC_SPI_INT_EN
);
283 if (spi_stat
& (SIRFSOC_SPI_FRM_END
284 | SIRFSOC_SPI_RXFIFO_THD_REACH
))
285 while (!((readl(sspi
->base
+ SIRFSOC_SPI_RXFIFO_STATUS
)
286 & SIRFSOC_SPI_FIFO_EMPTY
)) &&
290 if (spi_stat
& (SIRFSOC_SPI_FIFO_EMPTY
291 | SIRFSOC_SPI_TXFIFO_THD_REACH
))
292 while (!((readl(sspi
->base
+ SIRFSOC_SPI_TXFIFO_STATUS
)
293 & SIRFSOC_SPI_FIFO_FULL
)) &&
297 /* Received all words */
298 if ((sspi
->left_rx_word
== 0) && (sspi
->left_tx_word
== 0)) {
299 complete(&sspi
->rx_done
);
300 writel(0x0, sspi
->base
+ SIRFSOC_SPI_INT_EN
);
305 static void spi_sirfsoc_dma_fini_callback(void *data
)
307 struct completion
*dma_complete
= data
;
309 complete(dma_complete
);
312 static int spi_sirfsoc_transfer(struct spi_device
*spi
, struct spi_transfer
*t
)
314 struct sirfsoc_spi
*sspi
;
315 int timeout
= t
->len
* 10;
316 sspi
= spi_master_get_devdata(spi
->master
);
318 sspi
->tx
= t
->tx_buf
? t
->tx_buf
: sspi
->dummypage
;
319 sspi
->rx
= t
->rx_buf
? t
->rx_buf
: sspi
->dummypage
;
320 sspi
->left_tx_word
= sspi
->left_rx_word
= t
->len
/ sspi
->word_width
;
321 reinit_completion(&sspi
->rx_done
);
322 reinit_completion(&sspi
->tx_done
);
324 writel(SIRFSOC_SPI_INT_MASK_ALL
, sspi
->base
+ SIRFSOC_SPI_INT_STATUS
);
327 * fill tx_buf into command register and wait for its completion
329 if (sspi
->tx_by_cmd
) {
331 memcpy(&cmd
, sspi
->tx
, t
->len
);
333 if (sspi
->word_width
== 1 && !(spi
->mode
& SPI_LSB_FIRST
))
334 cmd
= cpu_to_be32(cmd
) >>
335 ((SIRFSOC_MAX_CMD_BYTES
- t
->len
) * 8);
336 if (sspi
->word_width
== 2 && t
->len
== 4 &&
337 (!(spi
->mode
& SPI_LSB_FIRST
)))
338 cmd
= ((cmd
& 0xffff) << 16) | (cmd
>> 16);
340 writel(cmd
, sspi
->base
+ SIRFSOC_SPI_CMD
);
341 writel(SIRFSOC_SPI_FRM_END_INT_EN
,
342 sspi
->base
+ SIRFSOC_SPI_INT_EN
);
343 writel(SIRFSOC_SPI_CMD_TX_EN
,
344 sspi
->base
+ SIRFSOC_SPI_TX_RX_EN
);
346 if (wait_for_completion_timeout(&sspi
->tx_done
, timeout
) == 0) {
347 dev_err(&spi
->dev
, "transfer timeout\n");
354 if (sspi
->left_tx_word
== 1) {
355 writel(readl(sspi
->base
+ SIRFSOC_SPI_CTRL
) |
356 SIRFSOC_SPI_ENA_AUTO_CLR
,
357 sspi
->base
+ SIRFSOC_SPI_CTRL
);
358 writel(0, sspi
->base
+ SIRFSOC_SPI_TX_DMA_IO_LEN
);
359 writel(0, sspi
->base
+ SIRFSOC_SPI_RX_DMA_IO_LEN
);
360 } else if ((sspi
->left_tx_word
> 1) && (sspi
->left_tx_word
<
361 SIRFSOC_SPI_DAT_FRM_LEN_MAX
)) {
362 writel(readl(sspi
->base
+ SIRFSOC_SPI_CTRL
) |
363 SIRFSOC_SPI_MUL_DAT_MODE
|
364 SIRFSOC_SPI_ENA_AUTO_CLR
,
365 sspi
->base
+ SIRFSOC_SPI_CTRL
);
366 writel(sspi
->left_tx_word
- 1,
367 sspi
->base
+ SIRFSOC_SPI_TX_DMA_IO_LEN
);
368 writel(sspi
->left_tx_word
- 1,
369 sspi
->base
+ SIRFSOC_SPI_RX_DMA_IO_LEN
);
371 writel(readl(sspi
->base
+ SIRFSOC_SPI_CTRL
),
372 sspi
->base
+ SIRFSOC_SPI_CTRL
);
373 writel(0, sspi
->base
+ SIRFSOC_SPI_TX_DMA_IO_LEN
);
374 writel(0, sspi
->base
+ SIRFSOC_SPI_RX_DMA_IO_LEN
);
377 writel(SIRFSOC_SPI_FIFO_RESET
, sspi
->base
+ SIRFSOC_SPI_RXFIFO_OP
);
378 writel(SIRFSOC_SPI_FIFO_RESET
, sspi
->base
+ SIRFSOC_SPI_TXFIFO_OP
);
379 writel(SIRFSOC_SPI_FIFO_START
, sspi
->base
+ SIRFSOC_SPI_RXFIFO_OP
);
380 writel(SIRFSOC_SPI_FIFO_START
, sspi
->base
+ SIRFSOC_SPI_TXFIFO_OP
);
382 if (IS_DMA_VALID(t
)) {
383 struct dma_async_tx_descriptor
*rx_desc
, *tx_desc
;
385 sspi
->dst_start
= dma_map_single(&spi
->dev
, sspi
->rx
, t
->len
, DMA_FROM_DEVICE
);
386 rx_desc
= dmaengine_prep_slave_single(sspi
->rx_chan
,
387 sspi
->dst_start
, t
->len
, DMA_DEV_TO_MEM
,
388 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
389 rx_desc
->callback
= spi_sirfsoc_dma_fini_callback
;
390 rx_desc
->callback_param
= &sspi
->rx_done
;
392 sspi
->src_start
= dma_map_single(&spi
->dev
, (void *)sspi
->tx
, t
->len
, DMA_TO_DEVICE
);
393 tx_desc
= dmaengine_prep_slave_single(sspi
->tx_chan
,
394 sspi
->src_start
, t
->len
, DMA_MEM_TO_DEV
,
395 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
396 tx_desc
->callback
= spi_sirfsoc_dma_fini_callback
;
397 tx_desc
->callback_param
= &sspi
->tx_done
;
399 dmaengine_submit(tx_desc
);
400 dmaengine_submit(rx_desc
);
401 dma_async_issue_pending(sspi
->tx_chan
);
402 dma_async_issue_pending(sspi
->rx_chan
);
404 /* Send the first word to trigger the whole tx/rx process */
407 writel(SIRFSOC_SPI_RX_OFLOW_INT_EN
| SIRFSOC_SPI_TX_UFLOW_INT_EN
|
408 SIRFSOC_SPI_RXFIFO_THD_INT_EN
| SIRFSOC_SPI_TXFIFO_THD_INT_EN
|
409 SIRFSOC_SPI_FRM_END_INT_EN
| SIRFSOC_SPI_RXFIFO_FULL_INT_EN
|
410 SIRFSOC_SPI_TXFIFO_EMPTY_INT_EN
, sspi
->base
+ SIRFSOC_SPI_INT_EN
);
413 writel(SIRFSOC_SPI_RX_EN
| SIRFSOC_SPI_TX_EN
, sspi
->base
+ SIRFSOC_SPI_TX_RX_EN
);
415 if (!IS_DMA_VALID(t
)) { /* for PIO */
416 if (wait_for_completion_timeout(&sspi
->rx_done
, timeout
) == 0)
417 dev_err(&spi
->dev
, "transfer timeout\n");
418 } else if (wait_for_completion_timeout(&sspi
->rx_done
, timeout
) == 0) {
419 dev_err(&spi
->dev
, "transfer timeout\n");
420 dmaengine_terminate_all(sspi
->rx_chan
);
422 sspi
->left_rx_word
= 0;
425 * we only wait tx-done event if transferring by DMA. for PIO,
426 * we get rx data by writing tx data, so if rx is done, tx has
429 if (IS_DMA_VALID(t
)) {
430 if (wait_for_completion_timeout(&sspi
->tx_done
, timeout
) == 0) {
431 dev_err(&spi
->dev
, "transfer timeout\n");
432 dmaengine_terminate_all(sspi
->tx_chan
);
436 if (IS_DMA_VALID(t
)) {
437 dma_unmap_single(&spi
->dev
, sspi
->src_start
, t
->len
, DMA_TO_DEVICE
);
438 dma_unmap_single(&spi
->dev
, sspi
->dst_start
, t
->len
, DMA_FROM_DEVICE
);
441 /* TX, RX FIFO stop */
442 writel(0, sspi
->base
+ SIRFSOC_SPI_RXFIFO_OP
);
443 writel(0, sspi
->base
+ SIRFSOC_SPI_TXFIFO_OP
);
444 writel(0, sspi
->base
+ SIRFSOC_SPI_TX_RX_EN
);
445 writel(0, sspi
->base
+ SIRFSOC_SPI_INT_EN
);
447 return t
->len
- sspi
->left_rx_word
* sspi
->word_width
;
450 static void spi_sirfsoc_chipselect(struct spi_device
*spi
, int value
)
452 struct sirfsoc_spi
*sspi
= spi_master_get_devdata(spi
->master
);
454 if (sspi
->chipselect
[spi
->chip_select
] == 0) {
455 u32 regval
= readl(sspi
->base
+ SIRFSOC_SPI_CTRL
);
457 case BITBANG_CS_ACTIVE
:
458 if (spi
->mode
& SPI_CS_HIGH
)
459 regval
|= SIRFSOC_SPI_CS_IO_OUT
;
461 regval
&= ~SIRFSOC_SPI_CS_IO_OUT
;
463 case BITBANG_CS_INACTIVE
:
464 if (spi
->mode
& SPI_CS_HIGH
)
465 regval
&= ~SIRFSOC_SPI_CS_IO_OUT
;
467 regval
|= SIRFSOC_SPI_CS_IO_OUT
;
470 writel(regval
, sspi
->base
+ SIRFSOC_SPI_CTRL
);
472 int gpio
= sspi
->chipselect
[spi
->chip_select
];
473 gpio_direction_output(gpio
, spi
->mode
& SPI_CS_HIGH
? 0 : 1);
478 spi_sirfsoc_setup_transfer(struct spi_device
*spi
, struct spi_transfer
*t
)
480 struct sirfsoc_spi
*sspi
;
481 u8 bits_per_word
= 0;
484 u32 txfifo_ctrl
, rxfifo_ctrl
;
485 u32 fifo_size
= SIRFSOC_SPI_FIFO_SIZE
/ 4;
487 sspi
= spi_master_get_devdata(spi
->master
);
489 bits_per_word
= (t
) ? t
->bits_per_word
: spi
->bits_per_word
;
490 hz
= t
&& t
->speed_hz
? t
->speed_hz
: spi
->max_speed_hz
;
492 regval
= (sspi
->ctrl_freq
/ (2 * hz
)) - 1;
493 if (regval
> 0xFFFF || regval
< 0) {
494 dev_err(&spi
->dev
, "Speed %d not supported\n", hz
);
498 switch (bits_per_word
) {
500 regval
|= SIRFSOC_SPI_TRAN_DAT_FORMAT_8
;
501 sspi
->rx_word
= spi_sirfsoc_rx_word_u8
;
502 sspi
->tx_word
= spi_sirfsoc_tx_word_u8
;
506 regval
|= (bits_per_word
== 12) ? SIRFSOC_SPI_TRAN_DAT_FORMAT_12
:
507 SIRFSOC_SPI_TRAN_DAT_FORMAT_16
;
508 sspi
->rx_word
= spi_sirfsoc_rx_word_u16
;
509 sspi
->tx_word
= spi_sirfsoc_tx_word_u16
;
512 regval
|= SIRFSOC_SPI_TRAN_DAT_FORMAT_32
;
513 sspi
->rx_word
= spi_sirfsoc_rx_word_u32
;
514 sspi
->tx_word
= spi_sirfsoc_tx_word_u32
;
520 sspi
->word_width
= DIV_ROUND_UP(bits_per_word
, 8);
521 txfifo_ctrl
= SIRFSOC_SPI_FIFO_THD(SIRFSOC_SPI_FIFO_SIZE
/ 2) |
523 rxfifo_ctrl
= SIRFSOC_SPI_FIFO_THD(SIRFSOC_SPI_FIFO_SIZE
/ 2) |
526 if (!(spi
->mode
& SPI_CS_HIGH
))
527 regval
|= SIRFSOC_SPI_CS_IDLE_STAT
;
528 if (!(spi
->mode
& SPI_LSB_FIRST
))
529 regval
|= SIRFSOC_SPI_TRAN_MSB
;
530 if (spi
->mode
& SPI_CPOL
)
531 regval
|= SIRFSOC_SPI_CLK_IDLE_STAT
;
534 * Data should be driven at least 1/2 cycle before the fetch edge to make
535 * sure that data gets stable at the fetch edge.
537 if (((spi
->mode
& SPI_CPOL
) && (spi
->mode
& SPI_CPHA
)) ||
538 (!(spi
->mode
& SPI_CPOL
) && !(spi
->mode
& SPI_CPHA
)))
539 regval
&= ~SIRFSOC_SPI_DRV_POS_EDGE
;
541 regval
|= SIRFSOC_SPI_DRV_POS_EDGE
;
543 writel(SIRFSOC_SPI_FIFO_SC(fifo_size
- 2) |
544 SIRFSOC_SPI_FIFO_LC(fifo_size
/ 2) |
545 SIRFSOC_SPI_FIFO_HC(2),
546 sspi
->base
+ SIRFSOC_SPI_TXFIFO_LEVEL_CHK
);
547 writel(SIRFSOC_SPI_FIFO_SC(2) |
548 SIRFSOC_SPI_FIFO_LC(fifo_size
/ 2) |
549 SIRFSOC_SPI_FIFO_HC(fifo_size
- 2),
550 sspi
->base
+ SIRFSOC_SPI_RXFIFO_LEVEL_CHK
);
551 writel(txfifo_ctrl
, sspi
->base
+ SIRFSOC_SPI_TXFIFO_CTRL
);
552 writel(rxfifo_ctrl
, sspi
->base
+ SIRFSOC_SPI_RXFIFO_CTRL
);
554 if (t
&& t
->tx_buf
&& !t
->rx_buf
&& (t
->len
<= SIRFSOC_MAX_CMD_BYTES
)) {
555 regval
|= (SIRFSOC_SPI_CMD_BYTE_NUM((t
->len
- 1)) |
556 SIRFSOC_SPI_CMD_MODE
);
557 sspi
->tx_by_cmd
= true;
559 regval
&= ~SIRFSOC_SPI_CMD_MODE
;
560 sspi
->tx_by_cmd
= false;
562 writel(regval
, sspi
->base
+ SIRFSOC_SPI_CTRL
);
564 if (IS_DMA_VALID(t
)) {
565 /* Enable DMA mode for RX, TX */
566 writel(0, sspi
->base
+ SIRFSOC_SPI_TX_DMA_IO_CTRL
);
567 writel(SIRFSOC_SPI_RX_DMA_FLUSH
, sspi
->base
+ SIRFSOC_SPI_RX_DMA_IO_CTRL
);
569 /* Enable IO mode for RX, TX */
570 writel(SIRFSOC_SPI_IO_MODE_SEL
, sspi
->base
+ SIRFSOC_SPI_TX_DMA_IO_CTRL
);
571 writel(SIRFSOC_SPI_IO_MODE_SEL
, sspi
->base
+ SIRFSOC_SPI_RX_DMA_IO_CTRL
);
577 static int spi_sirfsoc_setup(struct spi_device
*spi
)
579 if (!spi
->max_speed_hz
)
582 return spi_sirfsoc_setup_transfer(spi
, NULL
);
585 static int spi_sirfsoc_probe(struct platform_device
*pdev
)
587 struct sirfsoc_spi
*sspi
;
588 struct spi_master
*master
;
589 struct resource
*mem_res
;
590 int num_cs
, cs_gpio
, irq
;
594 ret
= of_property_read_u32(pdev
->dev
.of_node
,
595 "sirf,spi-num-chipselects", &num_cs
);
597 dev_err(&pdev
->dev
, "Unable to get chip select number\n");
601 master
= spi_alloc_master(&pdev
->dev
, sizeof(*sspi
) + sizeof(int) * num_cs
);
603 dev_err(&pdev
->dev
, "Unable to allocate SPI master\n");
606 platform_set_drvdata(pdev
, master
);
607 sspi
= spi_master_get_devdata(master
);
609 master
->num_chipselect
= num_cs
;
611 for (i
= 0; i
< master
->num_chipselect
; i
++) {
612 cs_gpio
= of_get_named_gpio(pdev
->dev
.of_node
, "cs-gpios", i
);
614 dev_err(&pdev
->dev
, "can't get cs gpio from DT\n");
619 sspi
->chipselect
[i
] = cs_gpio
;
621 continue; /* use cs from spi controller */
623 ret
= gpio_request(cs_gpio
, DRIVER_NAME
);
627 if (sspi
->chipselect
[i
] > 0)
628 gpio_free(sspi
->chipselect
[i
]);
630 dev_err(&pdev
->dev
, "fail to request cs gpios\n");
635 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
636 sspi
->base
= devm_ioremap_resource(&pdev
->dev
, mem_res
);
637 if (IS_ERR(sspi
->base
)) {
638 ret
= PTR_ERR(sspi
->base
);
642 irq
= platform_get_irq(pdev
, 0);
647 ret
= devm_request_irq(&pdev
->dev
, irq
, spi_sirfsoc_irq
, 0,
652 sspi
->bitbang
.master
= master
;
653 sspi
->bitbang
.chipselect
= spi_sirfsoc_chipselect
;
654 sspi
->bitbang
.setup_transfer
= spi_sirfsoc_setup_transfer
;
655 sspi
->bitbang
.txrx_bufs
= spi_sirfsoc_transfer
;
656 sspi
->bitbang
.master
->setup
= spi_sirfsoc_setup
;
657 master
->bus_num
= pdev
->id
;
658 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_LSB_FIRST
| SPI_CS_HIGH
;
659 master
->bits_per_word_mask
= SPI_BPW_MASK(8) | SPI_BPW_MASK(12) |
660 SPI_BPW_MASK(16) | SPI_BPW_MASK(32);
661 sspi
->bitbang
.master
->dev
.of_node
= pdev
->dev
.of_node
;
663 /* request DMA channels */
664 sspi
->rx_chan
= dma_request_slave_channel(&pdev
->dev
, "rx");
665 if (!sspi
->rx_chan
) {
666 dev_err(&pdev
->dev
, "can not allocate rx dma channel\n");
670 sspi
->tx_chan
= dma_request_slave_channel(&pdev
->dev
, "tx");
671 if (!sspi
->tx_chan
) {
672 dev_err(&pdev
->dev
, "can not allocate tx dma channel\n");
677 sspi
->clk
= clk_get(&pdev
->dev
, NULL
);
678 if (IS_ERR(sspi
->clk
)) {
679 ret
= PTR_ERR(sspi
->clk
);
682 clk_prepare_enable(sspi
->clk
);
683 sspi
->ctrl_freq
= clk_get_rate(sspi
->clk
);
685 init_completion(&sspi
->rx_done
);
686 init_completion(&sspi
->tx_done
);
688 writel(SIRFSOC_SPI_FIFO_RESET
, sspi
->base
+ SIRFSOC_SPI_RXFIFO_OP
);
689 writel(SIRFSOC_SPI_FIFO_RESET
, sspi
->base
+ SIRFSOC_SPI_TXFIFO_OP
);
690 writel(SIRFSOC_SPI_FIFO_START
, sspi
->base
+ SIRFSOC_SPI_RXFIFO_OP
);
691 writel(SIRFSOC_SPI_FIFO_START
, sspi
->base
+ SIRFSOC_SPI_TXFIFO_OP
);
692 /* We are not using dummy delay between command and data */
693 writel(0, sspi
->base
+ SIRFSOC_SPI_DUMMY_DELAY_CTL
);
695 sspi
->dummypage
= kmalloc(2 * PAGE_SIZE
, GFP_KERNEL
);
696 if (!sspi
->dummypage
) {
701 ret
= spi_bitbang_start(&sspi
->bitbang
);
705 dev_info(&pdev
->dev
, "registerred, bus number = %d\n", master
->bus_num
);
709 kfree(sspi
->dummypage
);
711 clk_disable_unprepare(sspi
->clk
);
714 dma_release_channel(sspi
->tx_chan
);
716 dma_release_channel(sspi
->rx_chan
);
718 spi_master_put(master
);
723 static int spi_sirfsoc_remove(struct platform_device
*pdev
)
725 struct spi_master
*master
;
726 struct sirfsoc_spi
*sspi
;
729 master
= platform_get_drvdata(pdev
);
730 sspi
= spi_master_get_devdata(master
);
732 spi_bitbang_stop(&sspi
->bitbang
);
733 for (i
= 0; i
< master
->num_chipselect
; i
++) {
734 if (sspi
->chipselect
[i
] > 0)
735 gpio_free(sspi
->chipselect
[i
]);
737 kfree(sspi
->dummypage
);
738 clk_disable_unprepare(sspi
->clk
);
740 dma_release_channel(sspi
->rx_chan
);
741 dma_release_channel(sspi
->tx_chan
);
742 spi_master_put(master
);
746 #ifdef CONFIG_PM_SLEEP
747 static int spi_sirfsoc_suspend(struct device
*dev
)
749 struct spi_master
*master
= dev_get_drvdata(dev
);
750 struct sirfsoc_spi
*sspi
= spi_master_get_devdata(master
);
753 ret
= spi_master_suspend(master
);
757 clk_disable(sspi
->clk
);
761 static int spi_sirfsoc_resume(struct device
*dev
)
763 struct spi_master
*master
= dev_get_drvdata(dev
);
764 struct sirfsoc_spi
*sspi
= spi_master_get_devdata(master
);
766 clk_enable(sspi
->clk
);
767 writel(SIRFSOC_SPI_FIFO_RESET
, sspi
->base
+ SIRFSOC_SPI_RXFIFO_OP
);
768 writel(SIRFSOC_SPI_FIFO_RESET
, sspi
->base
+ SIRFSOC_SPI_TXFIFO_OP
);
769 writel(SIRFSOC_SPI_FIFO_START
, sspi
->base
+ SIRFSOC_SPI_RXFIFO_OP
);
770 writel(SIRFSOC_SPI_FIFO_START
, sspi
->base
+ SIRFSOC_SPI_TXFIFO_OP
);
772 return spi_master_resume(master
);
776 static SIMPLE_DEV_PM_OPS(spi_sirfsoc_pm_ops
, spi_sirfsoc_suspend
,
779 static const struct of_device_id spi_sirfsoc_of_match
[] = {
780 { .compatible
= "sirf,prima2-spi", },
781 { .compatible
= "sirf,marco-spi", },
784 MODULE_DEVICE_TABLE(of
, spi_sirfsoc_of_match
);
786 static struct platform_driver spi_sirfsoc_driver
= {
789 .owner
= THIS_MODULE
,
790 .pm
= &spi_sirfsoc_pm_ops
,
791 .of_match_table
= spi_sirfsoc_of_match
,
793 .probe
= spi_sirfsoc_probe
,
794 .remove
= spi_sirfsoc_remove
,
796 module_platform_driver(spi_sirfsoc_driver
);
798 MODULE_DESCRIPTION("SiRF SoC SPI master driver");
799 MODULE_AUTHOR("Zhiwu Song <Zhiwu.Song@csr.com>, "
800 "Barry Song <Baohua.Song@csr.com>");
801 MODULE_LICENSE("GPL v2");