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/completion.h>
14 #include <linux/interrupt.h>
17 #include <linux/bitops.h>
18 #include <linux/err.h>
19 #include <linux/platform_device.h>
20 #include <linux/of_gpio.h>
21 #include <linux/spi/spi.h>
22 #include <linux/spi/spi_bitbang.h>
23 #include <linux/dmaengine.h>
24 #include <linux/dma-direction.h>
25 #include <linux/dma-mapping.h>
27 #define DRIVER_NAME "sirfsoc_spi"
29 #define SIRFSOC_SPI_CTRL 0x0000
30 #define SIRFSOC_SPI_CMD 0x0004
31 #define SIRFSOC_SPI_TX_RX_EN 0x0008
32 #define SIRFSOC_SPI_INT_EN 0x000C
33 #define SIRFSOC_SPI_INT_STATUS 0x0010
34 #define SIRFSOC_SPI_TX_DMA_IO_CTRL 0x0100
35 #define SIRFSOC_SPI_TX_DMA_IO_LEN 0x0104
36 #define SIRFSOC_SPI_TXFIFO_CTRL 0x0108
37 #define SIRFSOC_SPI_TXFIFO_LEVEL_CHK 0x010C
38 #define SIRFSOC_SPI_TXFIFO_OP 0x0110
39 #define SIRFSOC_SPI_TXFIFO_STATUS 0x0114
40 #define SIRFSOC_SPI_TXFIFO_DATA 0x0118
41 #define SIRFSOC_SPI_RX_DMA_IO_CTRL 0x0120
42 #define SIRFSOC_SPI_RX_DMA_IO_LEN 0x0124
43 #define SIRFSOC_SPI_RXFIFO_CTRL 0x0128
44 #define SIRFSOC_SPI_RXFIFO_LEVEL_CHK 0x012C
45 #define SIRFSOC_SPI_RXFIFO_OP 0x0130
46 #define SIRFSOC_SPI_RXFIFO_STATUS 0x0134
47 #define SIRFSOC_SPI_RXFIFO_DATA 0x0138
48 #define SIRFSOC_SPI_DUMMY_DELAY_CTL 0x0144
50 /* SPI CTRL register defines */
51 #define SIRFSOC_SPI_SLV_MODE BIT(16)
52 #define SIRFSOC_SPI_CMD_MODE BIT(17)
53 #define SIRFSOC_SPI_CS_IO_OUT BIT(18)
54 #define SIRFSOC_SPI_CS_IO_MODE BIT(19)
55 #define SIRFSOC_SPI_CLK_IDLE_STAT BIT(20)
56 #define SIRFSOC_SPI_CS_IDLE_STAT BIT(21)
57 #define SIRFSOC_SPI_TRAN_MSB BIT(22)
58 #define SIRFSOC_SPI_DRV_POS_EDGE BIT(23)
59 #define SIRFSOC_SPI_CS_HOLD_TIME BIT(24)
60 #define SIRFSOC_SPI_CLK_SAMPLE_MODE BIT(25)
61 #define SIRFSOC_SPI_TRAN_DAT_FORMAT_8 (0 << 26)
62 #define SIRFSOC_SPI_TRAN_DAT_FORMAT_12 (1 << 26)
63 #define SIRFSOC_SPI_TRAN_DAT_FORMAT_16 (2 << 26)
64 #define SIRFSOC_SPI_TRAN_DAT_FORMAT_32 (3 << 26)
65 #define SIRFSOC_SPI_CMD_BYTE_NUM(x) ((x & 3) << 28)
66 #define SIRFSOC_SPI_ENA_AUTO_CLR BIT(30)
67 #define SIRFSOC_SPI_MUL_DAT_MODE BIT(31)
69 /* Interrupt Enable */
70 #define SIRFSOC_SPI_RX_DONE_INT_EN BIT(0)
71 #define SIRFSOC_SPI_TX_DONE_INT_EN BIT(1)
72 #define SIRFSOC_SPI_RX_OFLOW_INT_EN BIT(2)
73 #define SIRFSOC_SPI_TX_UFLOW_INT_EN BIT(3)
74 #define SIRFSOC_SPI_RX_IO_DMA_INT_EN BIT(4)
75 #define SIRFSOC_SPI_TX_IO_DMA_INT_EN BIT(5)
76 #define SIRFSOC_SPI_RXFIFO_FULL_INT_EN BIT(6)
77 #define SIRFSOC_SPI_TXFIFO_EMPTY_INT_EN BIT(7)
78 #define SIRFSOC_SPI_RXFIFO_THD_INT_EN BIT(8)
79 #define SIRFSOC_SPI_TXFIFO_THD_INT_EN BIT(9)
80 #define SIRFSOC_SPI_FRM_END_INT_EN BIT(10)
82 #define SIRFSOC_SPI_INT_MASK_ALL 0x1FFF
84 /* Interrupt status */
85 #define SIRFSOC_SPI_RX_DONE BIT(0)
86 #define SIRFSOC_SPI_TX_DONE BIT(1)
87 #define SIRFSOC_SPI_RX_OFLOW BIT(2)
88 #define SIRFSOC_SPI_TX_UFLOW BIT(3)
89 #define SIRFSOC_SPI_RX_IO_DMA BIT(4)
90 #define SIRFSOC_SPI_RX_FIFO_FULL BIT(6)
91 #define SIRFSOC_SPI_TXFIFO_EMPTY BIT(7)
92 #define SIRFSOC_SPI_RXFIFO_THD_REACH BIT(8)
93 #define SIRFSOC_SPI_TXFIFO_THD_REACH BIT(9)
94 #define SIRFSOC_SPI_FRM_END BIT(10)
97 #define SIRFSOC_SPI_RX_EN BIT(0)
98 #define SIRFSOC_SPI_TX_EN BIT(1)
99 #define SIRFSOC_SPI_CMD_TX_EN BIT(2)
101 #define SIRFSOC_SPI_IO_MODE_SEL BIT(0)
102 #define SIRFSOC_SPI_RX_DMA_FLUSH BIT(2)
105 #define SIRFSOC_SPI_FIFO_RESET BIT(0)
106 #define SIRFSOC_SPI_FIFO_START BIT(1)
109 #define SIRFSOC_SPI_FIFO_WIDTH_BYTE (0 << 0)
110 #define SIRFSOC_SPI_FIFO_WIDTH_WORD (1 << 0)
111 #define SIRFSOC_SPI_FIFO_WIDTH_DWORD (2 << 0)
114 #define SIRFSOC_SPI_FIFO_LEVEL_MASK 0xFF
115 #define SIRFSOC_SPI_FIFO_FULL BIT(8)
116 #define SIRFSOC_SPI_FIFO_EMPTY BIT(9)
118 /* 256 bytes rx/tx FIFO */
119 #define SIRFSOC_SPI_FIFO_SIZE 256
120 #define SIRFSOC_SPI_DAT_FRM_LEN_MAX (64 * 1024)
122 #define SIRFSOC_SPI_FIFO_SC(x) ((x) & 0x3F)
123 #define SIRFSOC_SPI_FIFO_LC(x) (((x) & 0x3F) << 10)
124 #define SIRFSOC_SPI_FIFO_HC(x) (((x) & 0x3F) << 20)
125 #define SIRFSOC_SPI_FIFO_THD(x) (((x) & 0xFF) << 2)
128 * only if the rx/tx buffer and transfer size are 4-bytes aligned, we use dma
129 * due to the limitation of dma controller
132 #define ALIGNED(x) (!((u32)x & 0x3))
133 #define IS_DMA_VALID(x) (x && ALIGNED(x->tx_buf) && ALIGNED(x->rx_buf) && \
134 ALIGNED(x->len) && (x->len < 2 * PAGE_SIZE))
136 #define SIRFSOC_MAX_CMD_BYTES 4
139 struct spi_bitbang bitbang;
140 struct completion rx_done;
141 struct completion tx_done;
144 u32 ctrl_freq; /* SPI controller clock speed */
147 /* rx & tx bufs from the spi_transfer */
151 /* place received word into rx buffer */
152 void (*rx_word) (struct sirfsoc_spi *);
153 /* get word from tx buffer for sending */
154 void (*tx_word) (struct sirfsoc_spi *);
156 /* number of words left to be tranmitted/received */
157 unsigned int left_tx_word;
158 unsigned int left_rx_word;
160 /* rx & tx DMA channels */
161 struct dma_chan *rx_chan;
162 struct dma_chan *tx_chan;
163 dma_addr_t src_start;
164 dma_addr_t dst_start;
166 int word_width; /* in bytes */
169 * if tx size is not more than 4 and rx size is NULL, use
176 static void spi_sirfsoc_rx_word_u8(struct sirfsoc_spi *sspi)
181 data = readl(sspi->base + SIRFSOC_SPI_RXFIFO_DATA);
188 sspi->left_rx_word--;
191 static void spi_sirfsoc_tx_word_u8(struct sirfsoc_spi *sspi)
194 const u8 *tx = sspi->tx;
201 writel(data, sspi->base + SIRFSOC_SPI_TXFIFO_DATA);
202 sspi->left_tx_word--;
205 static void spi_sirfsoc_rx_word_u16(struct sirfsoc_spi *sspi)
210 data = readl(sspi->base + SIRFSOC_SPI_RXFIFO_DATA);
217 sspi->left_rx_word--;
220 static void spi_sirfsoc_tx_word_u16(struct sirfsoc_spi *sspi)
223 const u16 *tx = sspi->tx;
230 writel(data, sspi->base + SIRFSOC_SPI_TXFIFO_DATA);
231 sspi->left_tx_word--;
234 static void spi_sirfsoc_rx_word_u32(struct sirfsoc_spi *sspi)
239 data = readl(sspi->base + SIRFSOC_SPI_RXFIFO_DATA);
246 sspi->left_rx_word--;
250 static void spi_sirfsoc_tx_word_u32(struct sirfsoc_spi *sspi)
253 const u32 *tx = sspi->tx;
260 writel(data, sspi->base + SIRFSOC_SPI_TXFIFO_DATA);
261 sspi->left_tx_word--;
264 static irqreturn_t spi_sirfsoc_irq(int irq, void *dev_id)
266 struct sirfsoc_spi *sspi = dev_id;
267 u32 spi_stat = readl(sspi->base + SIRFSOC_SPI_INT_STATUS);
268 if (sspi->tx_by_cmd && (spi_stat & SIRFSOC_SPI_FRM_END)) {
269 complete(&sspi->tx_done);
270 writel(0x0, sspi->base + SIRFSOC_SPI_INT_EN);
271 writel(SIRFSOC_SPI_INT_MASK_ALL,
272 sspi->base + SIRFSOC_SPI_INT_STATUS);
276 /* Error Conditions */
277 if (spi_stat & SIRFSOC_SPI_RX_OFLOW ||
278 spi_stat & SIRFSOC_SPI_TX_UFLOW) {
279 complete(&sspi->tx_done);
280 complete(&sspi->rx_done);
281 writel(0x0, sspi->base + SIRFSOC_SPI_INT_EN);
282 writel(SIRFSOC_SPI_INT_MASK_ALL,
283 sspi->base + SIRFSOC_SPI_INT_STATUS);
286 if (spi_stat & SIRFSOC_SPI_TXFIFO_EMPTY)
287 complete(&sspi->tx_done);
288 while (!(readl(sspi->base + SIRFSOC_SPI_INT_STATUS) &
289 SIRFSOC_SPI_RX_IO_DMA))
291 complete(&sspi->rx_done);
292 writel(0x0, sspi->base + SIRFSOC_SPI_INT_EN);
293 writel(SIRFSOC_SPI_INT_MASK_ALL,
294 sspi->base + SIRFSOC_SPI_INT_STATUS);
299 static void spi_sirfsoc_dma_fini_callback(void *data)
301 struct completion *dma_complete = data;
303 complete(dma_complete);
306 static void spi_sirfsoc_cmd_transfer(struct spi_device *spi,
307 struct spi_transfer *t)
309 struct sirfsoc_spi *sspi;
310 int timeout = t->len * 10;
313 sspi = spi_master_get_devdata(spi->master);
314 memcpy(&cmd, sspi->tx, t->len);
315 if (sspi->word_width == 1 && !(spi->mode & SPI_LSB_FIRST))
316 cmd = cpu_to_be32(cmd) >>
317 ((SIRFSOC_MAX_CMD_BYTES - t->len) * 8);
318 if (sspi->word_width == 2 && t->len == 4 &&
319 (!(spi->mode & SPI_LSB_FIRST)))
320 cmd = ((cmd & 0xffff) << 16) | (cmd >> 16);
321 writel(cmd, sspi->base + SIRFSOC_SPI_CMD);
322 writel(SIRFSOC_SPI_FRM_END_INT_EN,
323 sspi->base + SIRFSOC_SPI_INT_EN);
324 writel(SIRFSOC_SPI_CMD_TX_EN,
325 sspi->base + SIRFSOC_SPI_TX_RX_EN);
326 if (wait_for_completion_timeout(&sspi->tx_done, timeout) == 0) {
327 dev_err(&spi->dev, "cmd transfer timeout\n");
330 sspi->left_rx_word -= t->len;
333 static void spi_sirfsoc_dma_transfer(struct spi_device *spi,
334 struct spi_transfer *t)
336 struct sirfsoc_spi *sspi;
337 struct dma_async_tx_descriptor *rx_desc, *tx_desc;
338 int timeout = t->len * 10;
340 sspi = spi_master_get_devdata(spi->master);
341 writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
342 writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
343 writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
344 writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
345 writel(0, sspi->base + SIRFSOC_SPI_INT_EN);
346 writel(SIRFSOC_SPI_INT_MASK_ALL, sspi->base + SIRFSOC_SPI_INT_STATUS);
347 if (sspi->left_tx_word < SIRFSOC_SPI_DAT_FRM_LEN_MAX) {
348 writel(readl(sspi->base + SIRFSOC_SPI_CTRL) |
349 SIRFSOC_SPI_ENA_AUTO_CLR | SIRFSOC_SPI_MUL_DAT_MODE,
350 sspi->base + SIRFSOC_SPI_CTRL);
351 writel(sspi->left_tx_word - 1,
352 sspi->base + SIRFSOC_SPI_TX_DMA_IO_LEN);
353 writel(sspi->left_tx_word - 1,
354 sspi->base + SIRFSOC_SPI_RX_DMA_IO_LEN);
356 writel(readl(sspi->base + SIRFSOC_SPI_CTRL),
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);
361 sspi->dst_start = dma_map_single(&spi->dev, sspi->rx, t->len,
362 (t->tx_buf != t->rx_buf) ?
363 DMA_FROM_DEVICE : DMA_BIDIRECTIONAL);
364 rx_desc = dmaengine_prep_slave_single(sspi->rx_chan,
365 sspi->dst_start, t->len, DMA_DEV_TO_MEM,
366 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
367 rx_desc->callback = spi_sirfsoc_dma_fini_callback;
368 rx_desc->callback_param = &sspi->rx_done;
370 sspi->src_start = dma_map_single(&spi->dev, (void *)sspi->tx, t->len,
371 (t->tx_buf != t->rx_buf) ?
372 DMA_TO_DEVICE : DMA_BIDIRECTIONAL);
373 tx_desc = dmaengine_prep_slave_single(sspi->tx_chan,
374 sspi->src_start, t->len, DMA_MEM_TO_DEV,
375 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
376 tx_desc->callback = spi_sirfsoc_dma_fini_callback;
377 tx_desc->callback_param = &sspi->tx_done;
379 dmaengine_submit(tx_desc);
380 dmaengine_submit(rx_desc);
381 dma_async_issue_pending(sspi->tx_chan);
382 dma_async_issue_pending(sspi->rx_chan);
383 writel(SIRFSOC_SPI_RX_EN | SIRFSOC_SPI_TX_EN,
384 sspi->base + SIRFSOC_SPI_TX_RX_EN);
385 if (wait_for_completion_timeout(&sspi->rx_done, timeout) == 0) {
386 dev_err(&spi->dev, "transfer timeout\n");
387 dmaengine_terminate_all(sspi->rx_chan);
389 sspi->left_rx_word = 0;
391 * we only wait tx-done event if transferring by DMA. for PIO,
392 * we get rx data by writing tx data, so if rx is done, tx has
395 if (wait_for_completion_timeout(&sspi->tx_done, timeout) == 0) {
396 dev_err(&spi->dev, "transfer timeout\n");
397 dmaengine_terminate_all(sspi->tx_chan);
399 dma_unmap_single(&spi->dev, sspi->src_start, t->len, DMA_TO_DEVICE);
400 dma_unmap_single(&spi->dev, sspi->dst_start, t->len, DMA_FROM_DEVICE);
401 /* TX, RX FIFO stop */
402 writel(0, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
403 writel(0, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
404 if (sspi->left_tx_word >= SIRFSOC_SPI_DAT_FRM_LEN_MAX)
405 writel(0, sspi->base + SIRFSOC_SPI_TX_RX_EN);
408 static void spi_sirfsoc_pio_transfer(struct spi_device *spi,
409 struct spi_transfer *t)
411 struct sirfsoc_spi *sspi;
412 int timeout = t->len * 10;
414 sspi = spi_master_get_devdata(spi->master);
416 writel(SIRFSOC_SPI_FIFO_RESET,
417 sspi->base + SIRFSOC_SPI_RXFIFO_OP);
418 writel(SIRFSOC_SPI_FIFO_RESET,
419 sspi->base + SIRFSOC_SPI_TXFIFO_OP);
420 writel(SIRFSOC_SPI_FIFO_START,
421 sspi->base + SIRFSOC_SPI_RXFIFO_OP);
422 writel(SIRFSOC_SPI_FIFO_START,
423 sspi->base + SIRFSOC_SPI_TXFIFO_OP);
424 writel(0, sspi->base + SIRFSOC_SPI_INT_EN);
425 writel(SIRFSOC_SPI_INT_MASK_ALL,
426 sspi->base + SIRFSOC_SPI_INT_STATUS);
427 writel(readl(sspi->base + SIRFSOC_SPI_CTRL) |
428 SIRFSOC_SPI_MUL_DAT_MODE | SIRFSOC_SPI_ENA_AUTO_CLR,
429 sspi->base + SIRFSOC_SPI_CTRL);
430 writel(min(sspi->left_tx_word, (u32)(256 / sspi->word_width))
431 - 1, sspi->base + SIRFSOC_SPI_TX_DMA_IO_LEN);
432 writel(min(sspi->left_rx_word, (u32)(256 / sspi->word_width))
433 - 1, sspi->base + SIRFSOC_SPI_RX_DMA_IO_LEN);
434 while (!((readl(sspi->base + SIRFSOC_SPI_TXFIFO_STATUS)
435 & SIRFSOC_SPI_FIFO_FULL)) && sspi->left_tx_word)
437 writel(SIRFSOC_SPI_TXFIFO_EMPTY_INT_EN |
438 SIRFSOC_SPI_TX_UFLOW_INT_EN |
439 SIRFSOC_SPI_RX_OFLOW_INT_EN,
440 sspi->base + SIRFSOC_SPI_INT_EN);
441 writel(SIRFSOC_SPI_RX_EN | SIRFSOC_SPI_TX_EN,
442 sspi->base + SIRFSOC_SPI_TX_RX_EN);
443 if (!wait_for_completion_timeout(&sspi->tx_done, timeout) ||
444 !wait_for_completion_timeout(&sspi->rx_done, timeout)) {
445 dev_err(&spi->dev, "transfer timeout\n");
448 while (!((readl(sspi->base + SIRFSOC_SPI_RXFIFO_STATUS)
449 & SIRFSOC_SPI_FIFO_EMPTY)) && sspi->left_rx_word)
451 writel(0, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
452 writel(0, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
453 } while (sspi->left_tx_word != 0 || sspi->left_rx_word != 0);
456 static int spi_sirfsoc_transfer(struct spi_device *spi, struct spi_transfer *t)
458 struct sirfsoc_spi *sspi;
459 sspi = spi_master_get_devdata(spi->master);
461 sspi->tx = t->tx_buf ? t->tx_buf : sspi->dummypage;
462 sspi->rx = t->rx_buf ? t->rx_buf : sspi->dummypage;
463 sspi->left_tx_word = sspi->left_rx_word = t->len / sspi->word_width;
464 reinit_completion(&sspi->rx_done);
465 reinit_completion(&sspi->tx_done);
467 * in the transfer, if transfer data using command register with rx_buf
468 * null, just fill command data into command register and wait for its
472 spi_sirfsoc_cmd_transfer(spi, t);
473 else if (IS_DMA_VALID(t))
474 spi_sirfsoc_dma_transfer(spi, t);
476 spi_sirfsoc_pio_transfer(spi, t);
478 return t->len - sspi->left_rx_word * sspi->word_width;
481 static void spi_sirfsoc_chipselect(struct spi_device *spi, int value)
483 struct sirfsoc_spi *sspi = spi_master_get_devdata(spi->master);
486 u32 regval = readl(sspi->base + SIRFSOC_SPI_CTRL);
488 case BITBANG_CS_ACTIVE:
489 if (spi->mode & SPI_CS_HIGH)
490 regval |= SIRFSOC_SPI_CS_IO_OUT;
492 regval &= ~SIRFSOC_SPI_CS_IO_OUT;
494 case BITBANG_CS_INACTIVE:
495 if (spi->mode & SPI_CS_HIGH)
496 regval &= ~SIRFSOC_SPI_CS_IO_OUT;
498 regval |= SIRFSOC_SPI_CS_IO_OUT;
501 writel(regval, sspi->base + SIRFSOC_SPI_CTRL);
504 case BITBANG_CS_ACTIVE:
505 gpio_direction_output(spi->cs_gpio,
506 spi->mode & SPI_CS_HIGH ? 1 : 0);
508 case BITBANG_CS_INACTIVE:
509 gpio_direction_output(spi->cs_gpio,
510 spi->mode & SPI_CS_HIGH ? 0 : 1);
517 spi_sirfsoc_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
519 struct sirfsoc_spi *sspi;
520 u8 bits_per_word = 0;
523 u32 txfifo_ctrl, rxfifo_ctrl;
524 u32 fifo_size = SIRFSOC_SPI_FIFO_SIZE / 4;
526 sspi = spi_master_get_devdata(spi->master);
528 bits_per_word = (t) ? t->bits_per_word : spi->bits_per_word;
529 hz = t && t->speed_hz ? t->speed_hz : spi->max_speed_hz;
531 regval = (sspi->ctrl_freq / (2 * hz)) - 1;
532 if (regval > 0xFFFF || regval < 0) {
533 dev_err(&spi->dev, "Speed %d not supported\n", hz);
537 switch (bits_per_word) {
539 regval |= SIRFSOC_SPI_TRAN_DAT_FORMAT_8;
540 sspi->rx_word = spi_sirfsoc_rx_word_u8;
541 sspi->tx_word = spi_sirfsoc_tx_word_u8;
545 regval |= (bits_per_word == 12) ?
546 SIRFSOC_SPI_TRAN_DAT_FORMAT_12 :
547 SIRFSOC_SPI_TRAN_DAT_FORMAT_16;
548 sspi->rx_word = spi_sirfsoc_rx_word_u16;
549 sspi->tx_word = spi_sirfsoc_tx_word_u16;
552 regval |= SIRFSOC_SPI_TRAN_DAT_FORMAT_32;
553 sspi->rx_word = spi_sirfsoc_rx_word_u32;
554 sspi->tx_word = spi_sirfsoc_tx_word_u32;
560 sspi->word_width = DIV_ROUND_UP(bits_per_word, 8);
561 txfifo_ctrl = SIRFSOC_SPI_FIFO_THD(SIRFSOC_SPI_FIFO_SIZE / 2) |
563 rxfifo_ctrl = SIRFSOC_SPI_FIFO_THD(SIRFSOC_SPI_FIFO_SIZE / 2) |
566 if (!(spi->mode & SPI_CS_HIGH))
567 regval |= SIRFSOC_SPI_CS_IDLE_STAT;
568 if (!(spi->mode & SPI_LSB_FIRST))
569 regval |= SIRFSOC_SPI_TRAN_MSB;
570 if (spi->mode & SPI_CPOL)
571 regval |= SIRFSOC_SPI_CLK_IDLE_STAT;
574 * Data should be driven at least 1/2 cycle before the fetch edge
575 * to make sure that data gets stable at the fetch edge.
577 if (((spi->mode & SPI_CPOL) && (spi->mode & SPI_CPHA)) ||
578 (!(spi->mode & SPI_CPOL) && !(spi->mode & SPI_CPHA)))
579 regval &= ~SIRFSOC_SPI_DRV_POS_EDGE;
581 regval |= SIRFSOC_SPI_DRV_POS_EDGE;
583 writel(SIRFSOC_SPI_FIFO_SC(fifo_size - 2) |
584 SIRFSOC_SPI_FIFO_LC(fifo_size / 2) |
585 SIRFSOC_SPI_FIFO_HC(2),
586 sspi->base + SIRFSOC_SPI_TXFIFO_LEVEL_CHK);
587 writel(SIRFSOC_SPI_FIFO_SC(2) |
588 SIRFSOC_SPI_FIFO_LC(fifo_size / 2) |
589 SIRFSOC_SPI_FIFO_HC(fifo_size - 2),
590 sspi->base + SIRFSOC_SPI_RXFIFO_LEVEL_CHK);
591 writel(txfifo_ctrl, sspi->base + SIRFSOC_SPI_TXFIFO_CTRL);
592 writel(rxfifo_ctrl, sspi->base + SIRFSOC_SPI_RXFIFO_CTRL);
594 if (t && t->tx_buf && !t->rx_buf && (t->len <= SIRFSOC_MAX_CMD_BYTES)) {
595 regval |= (SIRFSOC_SPI_CMD_BYTE_NUM((t->len - 1)) |
596 SIRFSOC_SPI_CMD_MODE);
597 sspi->tx_by_cmd = true;
599 regval &= ~SIRFSOC_SPI_CMD_MODE;
600 sspi->tx_by_cmd = false;
603 * it should never set to hardware cs mode because in hardware cs mode,
604 * cs signal can't controlled by driver.
606 regval |= SIRFSOC_SPI_CS_IO_MODE;
607 writel(regval, sspi->base + SIRFSOC_SPI_CTRL);
609 if (IS_DMA_VALID(t)) {
610 /* Enable DMA mode for RX, TX */
611 writel(0, sspi->base + SIRFSOC_SPI_TX_DMA_IO_CTRL);
612 writel(SIRFSOC_SPI_RX_DMA_FLUSH,
613 sspi->base + SIRFSOC_SPI_RX_DMA_IO_CTRL);
615 /* Enable IO mode for RX, TX */
616 writel(SIRFSOC_SPI_IO_MODE_SEL,
617 sspi->base + SIRFSOC_SPI_TX_DMA_IO_CTRL);
618 writel(SIRFSOC_SPI_IO_MODE_SEL,
619 sspi->base + SIRFSOC_SPI_RX_DMA_IO_CTRL);
625 static int spi_sirfsoc_setup(struct spi_device *spi)
627 struct sirfsoc_spi *sspi;
629 if (!spi->max_speed_hz)
632 sspi = spi_master_get_devdata(spi->master);
634 if (spi->cs_gpio == -ENOENT)
638 return spi_sirfsoc_setup_transfer(spi, NULL);
641 static int spi_sirfsoc_probe(struct platform_device *pdev)
643 struct sirfsoc_spi *sspi;
644 struct spi_master *master;
645 struct resource *mem_res;
649 master = spi_alloc_master(&pdev->dev, sizeof(*sspi));
651 dev_err(&pdev->dev, "Unable to allocate SPI master\n");
654 platform_set_drvdata(pdev, master);
655 sspi = spi_master_get_devdata(master);
657 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
658 sspi->base = devm_ioremap_resource(&pdev->dev, mem_res);
659 if (IS_ERR(sspi->base)) {
660 ret = PTR_ERR(sspi->base);
664 irq = platform_get_irq(pdev, 0);
669 ret = devm_request_irq(&pdev->dev, irq, spi_sirfsoc_irq, 0,
674 sspi->bitbang.master = master;
675 sspi->bitbang.chipselect = spi_sirfsoc_chipselect;
676 sspi->bitbang.setup_transfer = spi_sirfsoc_setup_transfer;
677 sspi->bitbang.txrx_bufs = spi_sirfsoc_transfer;
678 sspi->bitbang.master->setup = spi_sirfsoc_setup;
679 master->bus_num = pdev->id;
680 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST | SPI_CS_HIGH;
681 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(12) |
682 SPI_BPW_MASK(16) | SPI_BPW_MASK(32);
683 sspi->bitbang.master->dev.of_node = pdev->dev.of_node;
685 /* request DMA channels */
686 sspi->rx_chan = dma_request_slave_channel(&pdev->dev, "rx");
687 if (!sspi->rx_chan) {
688 dev_err(&pdev->dev, "can not allocate rx dma channel\n");
692 sspi->tx_chan = dma_request_slave_channel(&pdev->dev, "tx");
693 if (!sspi->tx_chan) {
694 dev_err(&pdev->dev, "can not allocate tx dma channel\n");
699 sspi->clk = clk_get(&pdev->dev, NULL);
700 if (IS_ERR(sspi->clk)) {
701 ret = PTR_ERR(sspi->clk);
704 clk_prepare_enable(sspi->clk);
705 sspi->ctrl_freq = clk_get_rate(sspi->clk);
707 init_completion(&sspi->rx_done);
708 init_completion(&sspi->tx_done);
710 writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
711 writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
712 writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
713 writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
714 /* We are not using dummy delay between command and data */
715 writel(0, sspi->base + SIRFSOC_SPI_DUMMY_DELAY_CTL);
717 sspi->dummypage = kmalloc(2 * PAGE_SIZE, GFP_KERNEL);
718 if (!sspi->dummypage) {
723 ret = spi_bitbang_start(&sspi->bitbang);
726 for (i = 0; master->cs_gpios && i < master->num_chipselect; i++) {
727 if (master->cs_gpios[i] == -ENOENT)
729 if (!gpio_is_valid(master->cs_gpios[i])) {
730 dev_err(&pdev->dev, "no valid gpio\n");
734 ret = devm_gpio_request(&pdev->dev,
735 master->cs_gpios[i], DRIVER_NAME);
737 dev_err(&pdev->dev, "failed to request gpio\n");
741 dev_info(&pdev->dev, "registerred, bus number = %d\n", master->bus_num);
745 kfree(sspi->dummypage);
747 clk_disable_unprepare(sspi->clk);
750 dma_release_channel(sspi->tx_chan);
752 dma_release_channel(sspi->rx_chan);
754 spi_master_put(master);
759 static int spi_sirfsoc_remove(struct platform_device *pdev)
761 struct spi_master *master;
762 struct sirfsoc_spi *sspi;
764 master = platform_get_drvdata(pdev);
765 sspi = spi_master_get_devdata(master);
767 spi_bitbang_stop(&sspi->bitbang);
768 kfree(sspi->dummypage);
769 clk_disable_unprepare(sspi->clk);
771 dma_release_channel(sspi->rx_chan);
772 dma_release_channel(sspi->tx_chan);
773 spi_master_put(master);
777 #ifdef CONFIG_PM_SLEEP
778 static int spi_sirfsoc_suspend(struct device *dev)
780 struct spi_master *master = dev_get_drvdata(dev);
781 struct sirfsoc_spi *sspi = spi_master_get_devdata(master);
784 ret = spi_master_suspend(master);
788 clk_disable(sspi->clk);
792 static int spi_sirfsoc_resume(struct device *dev)
794 struct spi_master *master = dev_get_drvdata(dev);
795 struct sirfsoc_spi *sspi = spi_master_get_devdata(master);
797 clk_enable(sspi->clk);
798 writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
799 writel(SIRFSOC_SPI_FIFO_RESET, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
800 writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_RXFIFO_OP);
801 writel(SIRFSOC_SPI_FIFO_START, sspi->base + SIRFSOC_SPI_TXFIFO_OP);
803 return spi_master_resume(master);
807 static SIMPLE_DEV_PM_OPS(spi_sirfsoc_pm_ops, spi_sirfsoc_suspend,
810 static const struct of_device_id spi_sirfsoc_of_match[] = {
811 { .compatible = "sirf,prima2-spi", },
812 { .compatible = "sirf,marco-spi", },
815 MODULE_DEVICE_TABLE(of, spi_sirfsoc_of_match);
817 static struct platform_driver spi_sirfsoc_driver = {
820 .owner = THIS_MODULE,
821 .pm = &spi_sirfsoc_pm_ops,
822 .of_match_table = spi_sirfsoc_of_match,
824 .probe = spi_sirfsoc_probe,
825 .remove = spi_sirfsoc_remove,
827 module_platform_driver(spi_sirfsoc_driver);
828 MODULE_DESCRIPTION("SiRF SoC SPI master driver");
829 MODULE_AUTHOR("Zhiwu Song <Zhiwu.Song@csr.com>");
830 MODULE_AUTHOR("Barry Song <Baohua.Song@csr.com>");
831 MODULE_LICENSE("GPL v2");