2 * drivers/spi/spi_imx.c
4 * Copyright (C) 2006 SWAPP
5 * Andrea Paterniani <a.paterniani@swapp-eng.it>
7 * Initial version inspired by:
8 * linux-2.6.17-rc3-mm1/drivers/spi/pxa2xx_spi.c
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/device.h>
24 #include <linux/ioport.h>
25 #include <linux/errno.h>
26 #include <linux/interrupt.h>
27 #include <linux/platform_device.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/spi/spi.h>
30 #include <linux/workqueue.h>
31 #include <linux/delay.h>
32 #include <linux/clk.h>
36 #include <asm/delay.h>
38 #include <mach/hardware.h>
39 #include <mach/imx-dma.h>
40 #include <mach/spi_imx.h>
42 /*-------------------------------------------------------------------------*/
43 /* SPI Registers offsets from peripheral base address */
44 #define SPI_RXDATA (0x00)
45 #define SPI_TXDATA (0x04)
46 #define SPI_CONTROL (0x08)
47 #define SPI_INT_STATUS (0x0C)
48 #define SPI_TEST (0x10)
49 #define SPI_PERIOD (0x14)
50 #define SPI_DMA (0x18)
51 #define SPI_RESET (0x1C)
53 /* SPI Control Register Bit Fields & Masks */
54 #define SPI_CONTROL_BITCOUNT_MASK (0xF) /* Bit Count Mask */
55 #define SPI_CONTROL_BITCOUNT(n) (((n) - 1) & SPI_CONTROL_BITCOUNT_MASK)
56 #define SPI_CONTROL_POL (0x1 << 4) /* Clock Polarity Mask */
57 #define SPI_CONTROL_POL_ACT_HIGH (0x0 << 4) /* Active high pol. (0=idle) */
58 #define SPI_CONTROL_POL_ACT_LOW (0x1 << 4) /* Active low pol. (1=idle) */
59 #define SPI_CONTROL_PHA (0x1 << 5) /* Clock Phase Mask */
60 #define SPI_CONTROL_PHA_0 (0x0 << 5) /* Clock Phase 0 */
61 #define SPI_CONTROL_PHA_1 (0x1 << 5) /* Clock Phase 1 */
62 #define SPI_CONTROL_SSCTL (0x1 << 6) /* /SS Waveform Select Mask */
63 #define SPI_CONTROL_SSCTL_0 (0x0 << 6) /* Master: /SS stays low between SPI burst
64 Slave: RXFIFO advanced by BIT_COUNT */
65 #define SPI_CONTROL_SSCTL_1 (0x1 << 6) /* Master: /SS insert pulse between SPI burst
66 Slave: RXFIFO advanced by /SS rising edge */
67 #define SPI_CONTROL_SSPOL (0x1 << 7) /* /SS Polarity Select Mask */
68 #define SPI_CONTROL_SSPOL_ACT_LOW (0x0 << 7) /* /SS Active low */
69 #define SPI_CONTROL_SSPOL_ACT_HIGH (0x1 << 7) /* /SS Active high */
70 #define SPI_CONTROL_XCH (0x1 << 8) /* Exchange */
71 #define SPI_CONTROL_SPIEN (0x1 << 9) /* SPI Module Enable */
72 #define SPI_CONTROL_MODE (0x1 << 10) /* SPI Mode Select Mask */
73 #define SPI_CONTROL_MODE_SLAVE (0x0 << 10) /* SPI Mode Slave */
74 #define SPI_CONTROL_MODE_MASTER (0x1 << 10) /* SPI Mode Master */
75 #define SPI_CONTROL_DRCTL (0x3 << 11) /* /SPI_RDY Control Mask */
76 #define SPI_CONTROL_DRCTL_0 (0x0 << 11) /* Ignore /SPI_RDY */
77 #define SPI_CONTROL_DRCTL_1 (0x1 << 11) /* /SPI_RDY falling edge triggers input */
78 #define SPI_CONTROL_DRCTL_2 (0x2 << 11) /* /SPI_RDY active low level triggers input */
79 #define SPI_CONTROL_DATARATE (0x7 << 13) /* Data Rate Mask */
80 #define SPI_PERCLK2_DIV_MIN (0) /* PERCLK2:4 */
81 #define SPI_PERCLK2_DIV_MAX (7) /* PERCLK2:512 */
82 #define SPI_CONTROL_DATARATE_MIN (SPI_PERCLK2_DIV_MAX << 13)
83 #define SPI_CONTROL_DATARATE_MAX (SPI_PERCLK2_DIV_MIN << 13)
84 #define SPI_CONTROL_DATARATE_BAD (SPI_CONTROL_DATARATE_MIN + 1)
86 /* SPI Interrupt/Status Register Bit Fields & Masks */
87 #define SPI_STATUS_TE (0x1 << 0) /* TXFIFO Empty Status */
88 #define SPI_STATUS_TH (0x1 << 1) /* TXFIFO Half Status */
89 #define SPI_STATUS_TF (0x1 << 2) /* TXFIFO Full Status */
90 #define SPI_STATUS_RR (0x1 << 3) /* RXFIFO Data Ready Status */
91 #define SPI_STATUS_RH (0x1 << 4) /* RXFIFO Half Status */
92 #define SPI_STATUS_RF (0x1 << 5) /* RXFIFO Full Status */
93 #define SPI_STATUS_RO (0x1 << 6) /* RXFIFO Overflow */
94 #define SPI_STATUS_BO (0x1 << 7) /* Bit Count Overflow */
95 #define SPI_STATUS (0xFF) /* SPI Status Mask */
96 #define SPI_INTEN_TE (0x1 << 8) /* TXFIFO Empty Interrupt Enable */
97 #define SPI_INTEN_TH (0x1 << 9) /* TXFIFO Half Interrupt Enable */
98 #define SPI_INTEN_TF (0x1 << 10) /* TXFIFO Full Interrupt Enable */
99 #define SPI_INTEN_RE (0x1 << 11) /* RXFIFO Data Ready Interrupt Enable */
100 #define SPI_INTEN_RH (0x1 << 12) /* RXFIFO Half Interrupt Enable */
101 #define SPI_INTEN_RF (0x1 << 13) /* RXFIFO Full Interrupt Enable */
102 #define SPI_INTEN_RO (0x1 << 14) /* RXFIFO Overflow Interrupt Enable */
103 #define SPI_INTEN_BO (0x1 << 15) /* Bit Count Overflow Interrupt Enable */
104 #define SPI_INTEN (0xFF << 8) /* SPI Interrupt Enable Mask */
106 /* SPI Test Register Bit Fields & Masks */
107 #define SPI_TEST_TXCNT (0xF << 0) /* TXFIFO Counter */
108 #define SPI_TEST_RXCNT_LSB (4) /* RXFIFO Counter LSB */
109 #define SPI_TEST_RXCNT (0xF << 4) /* RXFIFO Counter */
110 #define SPI_TEST_SSTATUS (0xF << 8) /* State Machine Status */
111 #define SPI_TEST_LBC (0x1 << 14) /* Loop Back Control */
113 /* SPI Period Register Bit Fields & Masks */
114 #define SPI_PERIOD_WAIT (0x7FFF << 0) /* Wait Between Transactions */
115 #define SPI_PERIOD_MAX_WAIT (0x7FFF) /* Max Wait Between
117 #define SPI_PERIOD_CSRC (0x1 << 15) /* Period Clock Source Mask */
118 #define SPI_PERIOD_CSRC_BCLK (0x0 << 15) /* Period Clock Source is
120 #define SPI_PERIOD_CSRC_32768 (0x1 << 15) /* Period Clock Source is
123 /* SPI DMA Register Bit Fields & Masks */
124 #define SPI_DMA_RHDMA (0x1 << 4) /* RXFIFO Half Status */
125 #define SPI_DMA_RFDMA (0x1 << 5) /* RXFIFO Full Status */
126 #define SPI_DMA_TEDMA (0x1 << 6) /* TXFIFO Empty Status */
127 #define SPI_DMA_THDMA (0x1 << 7) /* TXFIFO Half Status */
128 #define SPI_DMA_RHDEN (0x1 << 12) /* RXFIFO Half DMA Request Enable */
129 #define SPI_DMA_RFDEN (0x1 << 13) /* RXFIFO Full DMA Request Enable */
130 #define SPI_DMA_TEDEN (0x1 << 14) /* TXFIFO Empty DMA Request Enable */
131 #define SPI_DMA_THDEN (0x1 << 15) /* TXFIFO Half DMA Request Enable */
133 /* SPI Soft Reset Register Bit Fields & Masks */
134 #define SPI_RESET_START (0x1) /* Start */
136 /* Default SPI configuration values */
137 #define SPI_DEFAULT_CONTROL \
139 SPI_CONTROL_BITCOUNT(16) | \
140 SPI_CONTROL_POL_ACT_HIGH | \
141 SPI_CONTROL_PHA_0 | \
142 SPI_CONTROL_SPIEN | \
143 SPI_CONTROL_SSCTL_1 | \
144 SPI_CONTROL_MODE_MASTER | \
145 SPI_CONTROL_DRCTL_0 | \
146 SPI_CONTROL_DATARATE_MIN \
148 #define SPI_DEFAULT_ENABLE_LOOPBACK (0)
149 #define SPI_DEFAULT_ENABLE_DMA (0)
150 #define SPI_DEFAULT_PERIOD_WAIT (8)
151 /*-------------------------------------------------------------------------*/
154 /*-------------------------------------------------------------------------*/
155 /* TX/RX SPI FIFO size */
156 #define SPI_FIFO_DEPTH (8)
157 #define SPI_FIFO_BYTE_WIDTH (2)
158 #define SPI_FIFO_OVERFLOW_MARGIN (2)
160 /* DMA burst length for half full/empty request trigger */
161 #define SPI_DMA_BLR (SPI_FIFO_DEPTH * SPI_FIFO_BYTE_WIDTH / 2)
163 /* Dummy char output to achieve reads.
164 Choosing something different from all zeroes may help pattern recogition
165 for oscilloscope analysis, but may break some drivers. */
166 #define SPI_DUMMY_u8 0
167 #define SPI_DUMMY_u16 ((SPI_DUMMY_u8 << 8) | SPI_DUMMY_u8)
168 #define SPI_DUMMY_u32 ((SPI_DUMMY_u16 << 16) | SPI_DUMMY_u16)
171 * Macro to change a u32 field:
172 * @r : register to edit
174 * @v : new value for the field correctly bit-alligned
176 #define u32_EDIT(r, m, v) r = (r & ~(m)) | (v)
179 #define START_STATE ((void*)0)
180 #define RUNNING_STATE ((void*)1)
181 #define DONE_STATE ((void*)2)
182 #define ERROR_STATE ((void*)-1)
185 #define QUEUE_RUNNING (0)
186 #define QUEUE_STOPPED (1)
188 #define IS_DMA_ALIGNED(x) (((u32)(x) & 0x03) == 0)
189 /*-------------------------------------------------------------------------*/
192 /*-------------------------------------------------------------------------*/
193 /* Driver data structs */
197 /* Driver model hookup */
198 struct platform_device
*pdev
;
200 /* SPI framework hookup */
201 struct spi_master
*master
;
204 struct spi_imx_master
*master_info
;
206 /* Memory resources and SPI regs virtual address */
207 struct resource
*ioarea
;
210 /* SPI RX_DATA physical address */
211 dma_addr_t rd_data_phys
;
213 /* Driver message queue */
214 struct workqueue_struct
*workqueue
;
215 struct work_struct work
;
217 struct list_head queue
;
221 /* Message Transfer pump */
222 struct tasklet_struct pump_transfers
;
224 /* Current message, transfer and state */
225 struct spi_message
*cur_msg
;
226 struct spi_transfer
*cur_transfer
;
227 struct chip_data
*cur_chip
;
229 /* Rd / Wr buffers pointers */
240 /* Function pointers */
241 irqreturn_t (*transfer_handler
)(struct driver_data
*drv_data
);
242 void (*cs_control
)(u32 command
);
249 int rx_dma_needs_unmap
;
250 int tx_dma_needs_unmap
;
252 u32 dummy_dma_buf ____cacheline_aligned
;
268 void (*cs_control
)(u32 command
);
270 /*-------------------------------------------------------------------------*/
273 static void pump_messages(struct work_struct
*work
);
275 static void flush(struct driver_data
*drv_data
)
277 void __iomem
*regs
= drv_data
->regs
;
280 dev_dbg(&drv_data
->pdev
->dev
, "flush\n");
282 /* Wait for end of transaction */
284 control
= readl(regs
+ SPI_CONTROL
);
285 } while (control
& SPI_CONTROL_XCH
);
287 /* Release chip select if requested, transfer delays are
288 handled in pump_transfers */
289 if (drv_data
->cs_change
)
290 drv_data
->cs_control(SPI_CS_DEASSERT
);
292 /* Disable SPI to flush FIFOs */
293 writel(control
& ~SPI_CONTROL_SPIEN
, regs
+ SPI_CONTROL
);
294 writel(control
, regs
+ SPI_CONTROL
);
297 static void restore_state(struct driver_data
*drv_data
)
299 void __iomem
*regs
= drv_data
->regs
;
300 struct chip_data
*chip
= drv_data
->cur_chip
;
302 /* Load chip registers */
303 dev_dbg(&drv_data
->pdev
->dev
,
306 " control = 0x%08X\n",
309 writel(chip
->test
, regs
+ SPI_TEST
);
310 writel(chip
->period
, regs
+ SPI_PERIOD
);
311 writel(0, regs
+ SPI_INT_STATUS
);
312 writel(chip
->control
, regs
+ SPI_CONTROL
);
315 static void null_cs_control(u32 command
)
319 static inline u32
data_to_write(struct driver_data
*drv_data
)
321 return ((u32
)(drv_data
->tx_end
- drv_data
->tx
)) / drv_data
->n_bytes
;
324 static inline u32
data_to_read(struct driver_data
*drv_data
)
326 return ((u32
)(drv_data
->rx_end
- drv_data
->rx
)) / drv_data
->n_bytes
;
329 static int write(struct driver_data
*drv_data
)
331 void __iomem
*regs
= drv_data
->regs
;
332 void *tx
= drv_data
->tx
;
333 void *tx_end
= drv_data
->tx_end
;
334 u8 n_bytes
= drv_data
->n_bytes
;
335 u32 remaining_writes
;
336 u32 fifo_avail_space
;
340 /* Compute how many fifo writes to do */
341 remaining_writes
= (u32
)(tx_end
- tx
) / n_bytes
;
342 fifo_avail_space
= SPI_FIFO_DEPTH
-
343 (readl(regs
+ SPI_TEST
) & SPI_TEST_TXCNT
);
344 if (drv_data
->rx
&& (fifo_avail_space
> SPI_FIFO_OVERFLOW_MARGIN
))
345 /* Fix misunderstood receive overflow */
346 fifo_avail_space
-= SPI_FIFO_OVERFLOW_MARGIN
;
347 n
= min(remaining_writes
, fifo_avail_space
);
349 dev_dbg(&drv_data
->pdev
->dev
,
351 " remaining writes = %d\n"
352 " fifo avail space = %d\n"
353 " fifo writes = %d\n",
354 (n_bytes
== 1) ? "u8" : "u16",
360 /* Fill SPI TXFIFO */
361 if (drv_data
->rd_only
) {
364 writel(SPI_DUMMY_u16
, regs
+ SPI_TXDATA
);
369 writel(d
, regs
+ SPI_TXDATA
);
375 writel(d
, regs
+ SPI_TXDATA
);
381 /* Trigger transfer */
382 writel(readl(regs
+ SPI_CONTROL
) | SPI_CONTROL_XCH
,
385 /* Update tx pointer */
389 return (tx
>= tx_end
);
392 static int read(struct driver_data
*drv_data
)
394 void __iomem
*regs
= drv_data
->regs
;
395 void *rx
= drv_data
->rx
;
396 void *rx_end
= drv_data
->rx_end
;
397 u8 n_bytes
= drv_data
->n_bytes
;
403 /* Compute how many fifo reads to do */
404 remaining_reads
= (u32
)(rx_end
- rx
) / n_bytes
;
405 fifo_rxcnt
= (readl(regs
+ SPI_TEST
) & SPI_TEST_RXCNT
) >>
407 n
= min(remaining_reads
, fifo_rxcnt
);
409 dev_dbg(&drv_data
->pdev
->dev
,
411 " remaining reads = %d\n"
412 " fifo rx count = %d\n"
413 " fifo reads = %d\n",
414 (n_bytes
== 1) ? "u8" : "u16",
420 /* Read SPI RXFIFO */
423 d
= readl(regs
+ SPI_RXDATA
);
429 d
= readl(regs
+ SPI_RXDATA
);
435 /* Update rx pointer */
439 return (rx
>= rx_end
);
442 static void *next_transfer(struct driver_data
*drv_data
)
444 struct spi_message
*msg
= drv_data
->cur_msg
;
445 struct spi_transfer
*trans
= drv_data
->cur_transfer
;
447 /* Move to next transfer */
448 if (trans
->transfer_list
.next
!= &msg
->transfers
) {
449 drv_data
->cur_transfer
=
450 list_entry(trans
->transfer_list
.next
,
453 return RUNNING_STATE
;
459 static int map_dma_buffers(struct driver_data
*drv_data
)
461 struct spi_message
*msg
;
465 drv_data
->rx_dma_needs_unmap
= 0;
466 drv_data
->tx_dma_needs_unmap
= 0;
468 if (!drv_data
->master_info
->enable_dma
||
469 !drv_data
->cur_chip
->enable_dma
)
472 msg
= drv_data
->cur_msg
;
473 dev
= &msg
->spi
->dev
;
474 if (msg
->is_dma_mapped
) {
475 if (drv_data
->tx_dma
)
476 /* The caller provided at least dma and cpu virtual
477 address for write; pump_transfers() will consider the
478 transfer as write only if cpu rx virtual address is
482 if (drv_data
->rx_dma
) {
483 /* The caller provided dma and cpu virtual address to
484 performe read only transfer -->
485 use drv_data->dummy_dma_buf for dummy writes to
487 buf
= &drv_data
->dummy_dma_buf
;
488 drv_data
->tx_map_len
= sizeof(drv_data
->dummy_dma_buf
);
489 drv_data
->tx_dma
= dma_map_single(dev
,
491 drv_data
->tx_map_len
,
493 if (dma_mapping_error(dev
, drv_data
->tx_dma
))
496 drv_data
->tx_dma_needs_unmap
= 1;
498 /* Flags transfer as rd_only for pump_transfers() DMA
499 regs programming (should be redundant) */
506 if (!IS_DMA_ALIGNED(drv_data
->rx
) || !IS_DMA_ALIGNED(drv_data
->tx
))
509 if (drv_data
->tx
== NULL
) {
510 /* Read only message --> use drv_data->dummy_dma_buf for dummy
511 writes to achive reads */
512 buf
= &drv_data
->dummy_dma_buf
;
513 drv_data
->tx_map_len
= sizeof(drv_data
->dummy_dma_buf
);
516 drv_data
->tx_map_len
= drv_data
->len
;
518 drv_data
->tx_dma
= dma_map_single(dev
,
520 drv_data
->tx_map_len
,
522 if (dma_mapping_error(dev
, drv_data
->tx_dma
))
524 drv_data
->tx_dma_needs_unmap
= 1;
526 /* NULL rx means write-only transfer and no map needed
527 * since rx DMA will not be used */
530 drv_data
->rx_dma
= dma_map_single(dev
,
534 if (dma_mapping_error(dev
, drv_data
->rx_dma
)) {
535 if (drv_data
->tx_dma
) {
536 dma_unmap_single(dev
,
538 drv_data
->tx_map_len
,
540 drv_data
->tx_dma_needs_unmap
= 0;
544 drv_data
->rx_dma_needs_unmap
= 1;
550 static void unmap_dma_buffers(struct driver_data
*drv_data
)
552 struct spi_message
*msg
= drv_data
->cur_msg
;
553 struct device
*dev
= &msg
->spi
->dev
;
555 if (drv_data
->rx_dma_needs_unmap
) {
556 dma_unmap_single(dev
,
560 drv_data
->rx_dma_needs_unmap
= 0;
562 if (drv_data
->tx_dma_needs_unmap
) {
563 dma_unmap_single(dev
,
565 drv_data
->tx_map_len
,
567 drv_data
->tx_dma_needs_unmap
= 0;
571 /* Caller already set message->status (dma is already blocked) */
572 static void giveback(struct spi_message
*message
, struct driver_data
*drv_data
)
574 void __iomem
*regs
= drv_data
->regs
;
576 /* Bring SPI to sleep; restore_state() and pump_transfer()
578 writel(0, regs
+ SPI_INT_STATUS
);
579 writel(0, regs
+ SPI_DMA
);
581 /* Unconditioned deselct */
582 drv_data
->cs_control(SPI_CS_DEASSERT
);
584 message
->state
= NULL
;
585 if (message
->complete
)
586 message
->complete(message
->context
);
588 drv_data
->cur_msg
= NULL
;
589 drv_data
->cur_transfer
= NULL
;
590 drv_data
->cur_chip
= NULL
;
591 queue_work(drv_data
->workqueue
, &drv_data
->work
);
594 static void dma_err_handler(int channel
, void *data
, int errcode
)
596 struct driver_data
*drv_data
= data
;
597 struct spi_message
*msg
= drv_data
->cur_msg
;
599 dev_dbg(&drv_data
->pdev
->dev
, "dma_err_handler\n");
601 /* Disable both rx and tx dma channels */
602 imx_dma_disable(drv_data
->rx_channel
);
603 imx_dma_disable(drv_data
->tx_channel
);
604 unmap_dma_buffers(drv_data
);
608 msg
->state
= ERROR_STATE
;
609 tasklet_schedule(&drv_data
->pump_transfers
);
612 static void dma_tx_handler(int channel
, void *data
)
614 struct driver_data
*drv_data
= data
;
616 dev_dbg(&drv_data
->pdev
->dev
, "dma_tx_handler\n");
618 imx_dma_disable(channel
);
620 /* Now waits for TX FIFO empty */
621 writel(SPI_INTEN_TE
, drv_data
->regs
+ SPI_INT_STATUS
);
624 static irqreturn_t
dma_transfer(struct driver_data
*drv_data
)
627 struct spi_message
*msg
= drv_data
->cur_msg
;
628 void __iomem
*regs
= drv_data
->regs
;
630 status
= readl(regs
+ SPI_INT_STATUS
);
632 if ((status
& (SPI_INTEN_RO
| SPI_STATUS_RO
))
633 == (SPI_INTEN_RO
| SPI_STATUS_RO
)) {
634 writel(status
& ~SPI_INTEN
, regs
+ SPI_INT_STATUS
);
636 imx_dma_disable(drv_data
->tx_channel
);
637 imx_dma_disable(drv_data
->rx_channel
);
638 unmap_dma_buffers(drv_data
);
642 dev_warn(&drv_data
->pdev
->dev
,
643 "dma_transfer - fifo overun\n");
645 msg
->state
= ERROR_STATE
;
646 tasklet_schedule(&drv_data
->pump_transfers
);
651 if (status
& SPI_STATUS_TE
) {
652 writel(status
& ~SPI_INTEN_TE
, regs
+ SPI_INT_STATUS
);
655 /* Wait end of transfer before read trailing data */
656 while (readl(regs
+ SPI_CONTROL
) & SPI_CONTROL_XCH
)
659 imx_dma_disable(drv_data
->rx_channel
);
660 unmap_dma_buffers(drv_data
);
662 /* Release chip select if requested, transfer delays are
663 handled in pump_transfers() */
664 if (drv_data
->cs_change
)
665 drv_data
->cs_control(SPI_CS_DEASSERT
);
667 /* Calculate number of trailing data and read them */
668 dev_dbg(&drv_data
->pdev
->dev
,
669 "dma_transfer - test = 0x%08X\n",
670 readl(regs
+ SPI_TEST
));
671 drv_data
->rx
= drv_data
->rx_end
-
672 ((readl(regs
+ SPI_TEST
) &
674 SPI_TEST_RXCNT_LSB
)*drv_data
->n_bytes
;
677 /* Write only transfer */
678 unmap_dma_buffers(drv_data
);
683 /* End of transfer, update total byte transfered */
684 msg
->actual_length
+= drv_data
->len
;
686 /* Move to next transfer */
687 msg
->state
= next_transfer(drv_data
);
689 /* Schedule transfer tasklet */
690 tasklet_schedule(&drv_data
->pump_transfers
);
695 /* Opps problem detected */
699 static irqreturn_t
interrupt_wronly_transfer(struct driver_data
*drv_data
)
701 struct spi_message
*msg
= drv_data
->cur_msg
;
702 void __iomem
*regs
= drv_data
->regs
;
704 irqreturn_t handled
= IRQ_NONE
;
706 status
= readl(regs
+ SPI_INT_STATUS
);
708 if (status
& SPI_INTEN_TE
) {
709 /* TXFIFO Empty Interrupt on the last transfered word */
710 writel(status
& ~SPI_INTEN
, regs
+ SPI_INT_STATUS
);
711 dev_dbg(&drv_data
->pdev
->dev
,
712 "interrupt_wronly_transfer - end of tx\n");
716 /* Update total byte transfered */
717 msg
->actual_length
+= drv_data
->len
;
719 /* Move to next transfer */
720 msg
->state
= next_transfer(drv_data
);
722 /* Schedule transfer tasklet */
723 tasklet_schedule(&drv_data
->pump_transfers
);
727 while (status
& SPI_STATUS_TH
) {
728 dev_dbg(&drv_data
->pdev
->dev
,
729 "interrupt_wronly_transfer - status = 0x%08X\n",
733 if (write(drv_data
)) {
734 /* End of TXFIFO writes,
735 now wait until TXFIFO is empty */
736 writel(SPI_INTEN_TE
, regs
+ SPI_INT_STATUS
);
740 status
= readl(regs
+ SPI_INT_STATUS
);
742 /* We did something */
743 handled
= IRQ_HANDLED
;
750 static irqreturn_t
interrupt_transfer(struct driver_data
*drv_data
)
752 struct spi_message
*msg
= drv_data
->cur_msg
;
753 void __iomem
*regs
= drv_data
->regs
;
755 irqreturn_t handled
= IRQ_NONE
;
758 status
= readl(regs
+ SPI_INT_STATUS
);
760 if (status
& SPI_INTEN_TE
) {
761 /* TXFIFO Empty Interrupt on the last transfered word */
762 writel(status
& ~SPI_INTEN
, regs
+ SPI_INT_STATUS
);
763 dev_dbg(&drv_data
->pdev
->dev
,
764 "interrupt_transfer - end of tx\n");
766 if (msg
->state
== ERROR_STATE
) {
767 /* RXFIFO overrun was detected and message aborted */
770 /* Wait for end of transaction */
772 control
= readl(regs
+ SPI_CONTROL
);
773 } while (control
& SPI_CONTROL_XCH
);
775 /* Release chip select if requested, transfer delays are
776 handled in pump_transfers */
777 if (drv_data
->cs_change
)
778 drv_data
->cs_control(SPI_CS_DEASSERT
);
780 /* Read trailing bytes */
781 limit
= loops_per_jiffy
<< 1;
782 while ((read(drv_data
) == 0) && limit
--);
785 dev_err(&drv_data
->pdev
->dev
,
786 "interrupt_transfer - "
787 "trailing byte read failed\n");
789 dev_dbg(&drv_data
->pdev
->dev
,
790 "interrupt_transfer - end of rx\n");
792 /* Update total byte transfered */
793 msg
->actual_length
+= drv_data
->len
;
795 /* Move to next transfer */
796 msg
->state
= next_transfer(drv_data
);
799 /* Schedule transfer tasklet */
800 tasklet_schedule(&drv_data
->pump_transfers
);
804 while (status
& (SPI_STATUS_TH
| SPI_STATUS_RO
)) {
805 dev_dbg(&drv_data
->pdev
->dev
,
806 "interrupt_transfer - status = 0x%08X\n",
809 if (status
& SPI_STATUS_RO
) {
810 /* RXFIFO overrun, abort message end wait
811 until TXFIFO is empty */
812 writel(SPI_INTEN_TE
, regs
+ SPI_INT_STATUS
);
814 dev_warn(&drv_data
->pdev
->dev
,
815 "interrupt_transfer - fifo overun\n"
816 " data not yet written = %d\n"
817 " data not yet read = %d\n",
818 data_to_write(drv_data
),
819 data_to_read(drv_data
));
821 msg
->state
= ERROR_STATE
;
828 if (write(drv_data
)) {
829 /* End of TXFIFO writes,
830 now wait until TXFIFO is empty */
831 writel(SPI_INTEN_TE
, regs
+ SPI_INT_STATUS
);
835 status
= readl(regs
+ SPI_INT_STATUS
);
837 /* We did something */
838 handled
= IRQ_HANDLED
;
845 static irqreturn_t
spi_int(int irq
, void *dev_id
)
847 struct driver_data
*drv_data
= (struct driver_data
*)dev_id
;
849 if (!drv_data
->cur_msg
) {
850 dev_err(&drv_data
->pdev
->dev
,
851 "spi_int - bad message state\n");
856 return drv_data
->transfer_handler(drv_data
);
859 static inline u32
spi_speed_hz(struct driver_data
*drv_data
, u32 data_rate
)
861 return clk_get_rate(drv_data
->clk
) / (4 << ((data_rate
) >> 13));
864 static u32
spi_data_rate(struct driver_data
*drv_data
, u32 speed_hz
)
867 u32 quantized_hz
= clk_get_rate(drv_data
->clk
) >> 2;
869 for (div
= SPI_PERCLK2_DIV_MIN
;
870 div
<= SPI_PERCLK2_DIV_MAX
;
871 div
++, quantized_hz
>>= 1) {
872 if (quantized_hz
<= speed_hz
)
873 /* Max available speed LEQ required speed */
876 return SPI_CONTROL_DATARATE_BAD
;
879 static void pump_transfers(unsigned long data
)
881 struct driver_data
*drv_data
= (struct driver_data
*)data
;
882 struct spi_message
*message
;
883 struct spi_transfer
*transfer
, *previous
;
884 struct chip_data
*chip
;
888 dev_dbg(&drv_data
->pdev
->dev
, "pump_transfer\n");
890 message
= drv_data
->cur_msg
;
892 /* Handle for abort */
893 if (message
->state
== ERROR_STATE
) {
894 message
->status
= -EIO
;
895 giveback(message
, drv_data
);
899 /* Handle end of message */
900 if (message
->state
== DONE_STATE
) {
902 giveback(message
, drv_data
);
906 chip
= drv_data
->cur_chip
;
908 /* Delay if requested at end of transfer*/
909 transfer
= drv_data
->cur_transfer
;
910 if (message
->state
== RUNNING_STATE
) {
911 previous
= list_entry(transfer
->transfer_list
.prev
,
914 if (previous
->delay_usecs
)
915 udelay(previous
->delay_usecs
);
918 message
->state
= RUNNING_STATE
;
919 drv_data
->cs_control
= chip
->cs_control
;
922 transfer
= drv_data
->cur_transfer
;
923 drv_data
->tx
= (void *)transfer
->tx_buf
;
924 drv_data
->tx_end
= drv_data
->tx
+ transfer
->len
;
925 drv_data
->rx
= transfer
->rx_buf
;
926 drv_data
->rx_end
= drv_data
->rx
+ transfer
->len
;
927 drv_data
->rx_dma
= transfer
->rx_dma
;
928 drv_data
->tx_dma
= transfer
->tx_dma
;
929 drv_data
->len
= transfer
->len
;
930 drv_data
->cs_change
= transfer
->cs_change
;
931 drv_data
->rd_only
= (drv_data
->tx
== NULL
);
933 regs
= drv_data
->regs
;
934 control
= readl(regs
+ SPI_CONTROL
);
936 /* Bits per word setup */
937 tmp
= transfer
->bits_per_word
;
939 /* Use device setup */
940 tmp
= chip
->bits_per_word
;
941 drv_data
->n_bytes
= chip
->n_bytes
;
943 /* Use per-transfer setup */
944 drv_data
->n_bytes
= (tmp
<= 8) ? 1 : 2;
945 u32_EDIT(control
, SPI_CONTROL_BITCOUNT_MASK
, tmp
- 1);
947 /* Speed setup (surely valid because already checked) */
948 tmp
= transfer
->speed_hz
;
950 tmp
= chip
->max_speed_hz
;
951 tmp
= spi_data_rate(drv_data
, tmp
);
952 u32_EDIT(control
, SPI_CONTROL_DATARATE
, tmp
);
954 writel(control
, regs
+ SPI_CONTROL
);
956 /* Assert device chip-select */
957 drv_data
->cs_control(SPI_CS_ASSERT
);
959 /* DMA cannot read/write SPI FIFOs other than 16 bits at a time; hence
960 if bits_per_word is less or equal 8 PIO transfers are performed.
961 Moreover DMA is convinient for transfer length bigger than FIFOs
963 if ((drv_data
->n_bytes
== 2) &&
964 (drv_data
->len
> SPI_FIFO_DEPTH
*SPI_FIFO_BYTE_WIDTH
) &&
965 (map_dma_buffers(drv_data
) == 0)) {
966 dev_dbg(&drv_data
->pdev
->dev
,
967 "pump dma transfer\n"
974 (unsigned int)drv_data
->tx_dma
,
976 (unsigned int)drv_data
->rx_dma
,
979 /* Ensure we have the correct interrupt handler */
980 drv_data
->transfer_handler
= dma_transfer
;
982 /* Trigger transfer */
983 writel(readl(regs
+ SPI_CONTROL
) | SPI_CONTROL_XCH
,
988 /* Linear source address */
989 CCR(drv_data
->tx_channel
) =
992 CCR_SSIZ_32
| CCR_DSIZ_16
|
995 /* Read only transfer -> fixed source address for
996 dummy write to achive read */
997 CCR(drv_data
->tx_channel
) =
1000 CCR_SSIZ_32
| CCR_DSIZ_16
|
1003 imx_dma_setup_single(
1004 drv_data
->tx_channel
,
1007 drv_data
->rd_data_phys
+ 4,
1011 /* Setup rx DMA for linear destination address */
1012 CCR(drv_data
->rx_channel
) =
1015 CCR_DSIZ_32
| CCR_SSIZ_16
|
1017 imx_dma_setup_single(
1018 drv_data
->rx_channel
,
1021 drv_data
->rd_data_phys
,
1023 imx_dma_enable(drv_data
->rx_channel
);
1025 /* Enable SPI interrupt */
1026 writel(SPI_INTEN_RO
, regs
+ SPI_INT_STATUS
);
1028 /* Set SPI to request DMA service on both
1029 Rx and Tx half fifo watermark */
1030 writel(SPI_DMA_RHDEN
| SPI_DMA_THDEN
, regs
+ SPI_DMA
);
1032 /* Write only access -> set SPI to request DMA
1033 service on Tx half fifo watermark */
1034 writel(SPI_DMA_THDEN
, regs
+ SPI_DMA
);
1036 imx_dma_enable(drv_data
->tx_channel
);
1038 dev_dbg(&drv_data
->pdev
->dev
,
1039 "pump pio transfer\n"
1047 /* Ensure we have the correct interrupt handler */
1049 drv_data
->transfer_handler
= interrupt_transfer
;
1051 drv_data
->transfer_handler
= interrupt_wronly_transfer
;
1053 /* Enable SPI interrupt */
1055 writel(SPI_INTEN_TH
| SPI_INTEN_RO
,
1056 regs
+ SPI_INT_STATUS
);
1058 writel(SPI_INTEN_TH
, regs
+ SPI_INT_STATUS
);
1062 static void pump_messages(struct work_struct
*work
)
1064 struct driver_data
*drv_data
=
1065 container_of(work
, struct driver_data
, work
);
1066 unsigned long flags
;
1068 /* Lock queue and check for queue work */
1069 spin_lock_irqsave(&drv_data
->lock
, flags
);
1070 if (list_empty(&drv_data
->queue
) || drv_data
->run
== QUEUE_STOPPED
) {
1072 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1076 /* Make sure we are not already running a message */
1077 if (drv_data
->cur_msg
) {
1078 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1082 /* Extract head of queue */
1083 drv_data
->cur_msg
= list_entry(drv_data
->queue
.next
,
1084 struct spi_message
, queue
);
1085 list_del_init(&drv_data
->cur_msg
->queue
);
1087 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1089 /* Initial message state */
1090 drv_data
->cur_msg
->state
= START_STATE
;
1091 drv_data
->cur_transfer
= list_entry(drv_data
->cur_msg
->transfers
.next
,
1092 struct spi_transfer
,
1095 /* Setup the SPI using the per chip configuration */
1096 drv_data
->cur_chip
= spi_get_ctldata(drv_data
->cur_msg
->spi
);
1097 restore_state(drv_data
);
1099 /* Mark as busy and launch transfers */
1100 tasklet_schedule(&drv_data
->pump_transfers
);
1103 static int transfer(struct spi_device
*spi
, struct spi_message
*msg
)
1105 struct driver_data
*drv_data
= spi_master_get_devdata(spi
->master
);
1106 u32 min_speed_hz
, max_speed_hz
, tmp
;
1107 struct spi_transfer
*trans
;
1108 unsigned long flags
;
1110 msg
->actual_length
= 0;
1112 /* Per transfer setup check */
1113 min_speed_hz
= spi_speed_hz(drv_data
, SPI_CONTROL_DATARATE_MIN
);
1114 max_speed_hz
= spi
->max_speed_hz
;
1115 list_for_each_entry(trans
, &msg
->transfers
, transfer_list
) {
1116 tmp
= trans
->bits_per_word
;
1118 dev_err(&drv_data
->pdev
->dev
,
1119 "message rejected : "
1120 "invalid transfer bits_per_word (%d bits)\n",
1124 tmp
= trans
->speed_hz
;
1126 if (tmp
< min_speed_hz
) {
1127 dev_err(&drv_data
->pdev
->dev
,
1128 "message rejected : "
1129 "device min speed (%d Hz) exceeds "
1130 "required transfer speed (%d Hz)\n",
1134 } else if (tmp
> max_speed_hz
) {
1135 dev_err(&drv_data
->pdev
->dev
,
1136 "message rejected : "
1137 "transfer speed (%d Hz) exceeds "
1138 "device max speed (%d Hz)\n",
1146 /* Message accepted */
1147 msg
->status
= -EINPROGRESS
;
1148 msg
->state
= START_STATE
;
1150 spin_lock_irqsave(&drv_data
->lock
, flags
);
1151 if (drv_data
->run
== QUEUE_STOPPED
) {
1152 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1156 list_add_tail(&msg
->queue
, &drv_data
->queue
);
1157 if (drv_data
->run
== QUEUE_RUNNING
&& !drv_data
->busy
)
1158 queue_work(drv_data
->workqueue
, &drv_data
->work
);
1160 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1164 /* Message rejected and not queued */
1165 msg
->status
= -EINVAL
;
1166 msg
->state
= ERROR_STATE
;
1168 msg
->complete(msg
->context
);
1172 /* the spi->mode bits understood by this driver: */
1173 #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH)
1175 /* On first setup bad values must free chip_data memory since will cause
1176 spi_new_device to fail. Bad value setup from protocol driver are simply not
1177 applied and notified to the calling driver. */
1178 static int setup(struct spi_device
*spi
)
1180 struct driver_data
*drv_data
= spi_master_get_devdata(spi
->master
);
1181 struct spi_imx_chip
*chip_info
;
1182 struct chip_data
*chip
;
1183 int first_setup
= 0;
1187 if (spi
->mode
& ~MODEBITS
) {
1188 dev_dbg(&spi
->dev
, "setup: unsupported mode bits %x\n",
1189 spi
->mode
& ~MODEBITS
);
1193 /* Get controller data */
1194 chip_info
= spi
->controller_data
;
1196 /* Get controller_state */
1197 chip
= spi_get_ctldata(spi
);
1201 chip
= kzalloc(sizeof(struct chip_data
), GFP_KERNEL
);
1204 "setup - cannot allocate controller state\n");
1207 chip
->control
= SPI_DEFAULT_CONTROL
;
1209 if (chip_info
== NULL
) {
1210 /* spi_board_info.controller_data not is supplied */
1211 chip_info
= kzalloc(sizeof(struct spi_imx_chip
),
1216 "cannot allocate controller data\n");
1218 goto err_first_setup
;
1220 /* Set controller data default value */
1221 chip_info
->enable_loopback
=
1222 SPI_DEFAULT_ENABLE_LOOPBACK
;
1223 chip_info
->enable_dma
= SPI_DEFAULT_ENABLE_DMA
;
1224 chip_info
->ins_ss_pulse
= 1;
1225 chip_info
->bclk_wait
= SPI_DEFAULT_PERIOD_WAIT
;
1226 chip_info
->cs_control
= null_cs_control
;
1230 /* Now set controller state based on controller data */
1234 if (chip_info
->enable_loopback
)
1235 chip
->test
= SPI_TEST_LBC
;
1239 /* SPI dma driven */
1240 chip
->enable_dma
= chip_info
->enable_dma
;
1242 /* SPI /SS pulse between spi burst */
1243 if (chip_info
->ins_ss_pulse
)
1244 u32_EDIT(chip
->control
,
1245 SPI_CONTROL_SSCTL
, SPI_CONTROL_SSCTL_1
);
1247 u32_EDIT(chip
->control
,
1248 SPI_CONTROL_SSCTL
, SPI_CONTROL_SSCTL_0
);
1250 /* SPI bclk waits between each bits_per_word spi burst */
1251 if (chip_info
->bclk_wait
> SPI_PERIOD_MAX_WAIT
) {
1254 "bclk_wait exceeds max allowed (%d)\n",
1255 SPI_PERIOD_MAX_WAIT
);
1256 goto err_first_setup
;
1258 chip
->period
= SPI_PERIOD_CSRC_BCLK
|
1259 (chip_info
->bclk_wait
& SPI_PERIOD_WAIT
);
1264 if (tmp
& SPI_CS_HIGH
) {
1265 u32_EDIT(chip
->control
,
1266 SPI_CONTROL_SSPOL
, SPI_CONTROL_SSPOL_ACT_HIGH
);
1268 switch (tmp
& SPI_MODE_3
) {
1273 tmp
= SPI_CONTROL_PHA_1
;
1276 tmp
= SPI_CONTROL_POL_ACT_LOW
;
1280 tmp
= SPI_CONTROL_PHA_1
| SPI_CONTROL_POL_ACT_LOW
;
1283 u32_EDIT(chip
->control
, SPI_CONTROL_POL
| SPI_CONTROL_PHA
, tmp
);
1285 /* SPI word width */
1286 tmp
= spi
->bits_per_word
;
1289 spi
->bits_per_word
= 8;
1290 } else if (tmp
> 16) {
1294 "invalid bits_per_word (%d)\n",
1297 goto err_first_setup
;
1299 /* Undo setup using chip as backup copy */
1300 tmp
= chip
->bits_per_word
;
1301 spi
->bits_per_word
= tmp
;
1304 chip
->bits_per_word
= tmp
;
1305 u32_EDIT(chip
->control
, SPI_CONTROL_BITCOUNT_MASK
, tmp
- 1);
1306 chip
->n_bytes
= (tmp
<= 8) ? 1 : 2;
1309 tmp
= spi_data_rate(drv_data
, spi
->max_speed_hz
);
1310 if (tmp
== SPI_CONTROL_DATARATE_BAD
) {
1314 "HW min speed (%d Hz) exceeds required "
1315 "max speed (%d Hz)\n",
1316 spi_speed_hz(drv_data
, SPI_CONTROL_DATARATE_MIN
),
1319 goto err_first_setup
;
1321 /* Undo setup using chip as backup copy */
1322 spi
->max_speed_hz
= chip
->max_speed_hz
;
1324 u32_EDIT(chip
->control
, SPI_CONTROL_DATARATE
, tmp
);
1325 /* Actual rounded max_speed_hz */
1326 tmp
= spi_speed_hz(drv_data
, tmp
);
1327 spi
->max_speed_hz
= tmp
;
1328 chip
->max_speed_hz
= tmp
;
1331 /* SPI chip-select management */
1332 if (chip_info
->cs_control
)
1333 chip
->cs_control
= chip_info
->cs_control
;
1335 chip
->cs_control
= null_cs_control
;
1337 /* Save controller_state */
1338 spi_set_ctldata(spi
, chip
);
1343 " loopback enable = %s\n"
1344 " dma enable = %s\n"
1345 " insert /ss pulse = %s\n"
1346 " period wait = %d\n"
1348 " bits per word = %d\n"
1349 " min speed = %d Hz\n"
1350 " rounded max speed = %d Hz\n",
1351 chip
->test
& SPI_TEST_LBC
? "Yes" : "No",
1352 chip
->enable_dma
? "Yes" : "No",
1353 chip
->control
& SPI_CONTROL_SSCTL
? "Yes" : "No",
1354 chip
->period
& SPI_PERIOD_WAIT
,
1357 spi_speed_hz(drv_data
, SPI_CONTROL_DATARATE_MIN
),
1366 static void cleanup(struct spi_device
*spi
)
1368 kfree(spi_get_ctldata(spi
));
1371 static int __init
init_queue(struct driver_data
*drv_data
)
1373 INIT_LIST_HEAD(&drv_data
->queue
);
1374 spin_lock_init(&drv_data
->lock
);
1376 drv_data
->run
= QUEUE_STOPPED
;
1379 tasklet_init(&drv_data
->pump_transfers
,
1380 pump_transfers
, (unsigned long)drv_data
);
1382 INIT_WORK(&drv_data
->work
, pump_messages
);
1383 drv_data
->workqueue
= create_singlethread_workqueue(
1384 drv_data
->master
->dev
.parent
->bus_id
);
1385 if (drv_data
->workqueue
== NULL
)
1391 static int start_queue(struct driver_data
*drv_data
)
1393 unsigned long flags
;
1395 spin_lock_irqsave(&drv_data
->lock
, flags
);
1397 if (drv_data
->run
== QUEUE_RUNNING
|| drv_data
->busy
) {
1398 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1402 drv_data
->run
= QUEUE_RUNNING
;
1403 drv_data
->cur_msg
= NULL
;
1404 drv_data
->cur_transfer
= NULL
;
1405 drv_data
->cur_chip
= NULL
;
1406 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1408 queue_work(drv_data
->workqueue
, &drv_data
->work
);
1413 static int stop_queue(struct driver_data
*drv_data
)
1415 unsigned long flags
;
1416 unsigned limit
= 500;
1419 spin_lock_irqsave(&drv_data
->lock
, flags
);
1421 /* This is a bit lame, but is optimized for the common execution path.
1422 * A wait_queue on the drv_data->busy could be used, but then the common
1423 * execution path (pump_messages) would be required to call wake_up or
1424 * friends on every SPI message. Do this instead */
1425 drv_data
->run
= QUEUE_STOPPED
;
1426 while (!list_empty(&drv_data
->queue
) && drv_data
->busy
&& limit
--) {
1427 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1429 spin_lock_irqsave(&drv_data
->lock
, flags
);
1432 if (!list_empty(&drv_data
->queue
) || drv_data
->busy
)
1435 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1440 static int destroy_queue(struct driver_data
*drv_data
)
1444 status
= stop_queue(drv_data
);
1448 if (drv_data
->workqueue
)
1449 destroy_workqueue(drv_data
->workqueue
);
1454 static int __init
spi_imx_probe(struct platform_device
*pdev
)
1456 struct device
*dev
= &pdev
->dev
;
1457 struct spi_imx_master
*platform_info
;
1458 struct spi_master
*master
;
1459 struct driver_data
*drv_data
;
1460 struct resource
*res
;
1461 int irq
, status
= 0;
1463 platform_info
= dev
->platform_data
;
1464 if (platform_info
== NULL
) {
1465 dev_err(&pdev
->dev
, "probe - no platform data supplied\n");
1470 /* Allocate master with space for drv_data */
1471 master
= spi_alloc_master(dev
, sizeof(struct driver_data
));
1473 dev_err(&pdev
->dev
, "probe - cannot alloc spi_master\n");
1477 drv_data
= spi_master_get_devdata(master
);
1478 drv_data
->master
= master
;
1479 drv_data
->master_info
= platform_info
;
1480 drv_data
->pdev
= pdev
;
1482 master
->bus_num
= pdev
->id
;
1483 master
->num_chipselect
= platform_info
->num_chipselect
;
1484 master
->cleanup
= cleanup
;
1485 master
->setup
= setup
;
1486 master
->transfer
= transfer
;
1488 drv_data
->dummy_dma_buf
= SPI_DUMMY_u32
;
1490 drv_data
->clk
= clk_get(&pdev
->dev
, "perclk2");
1491 if (IS_ERR(drv_data
->clk
)) {
1492 dev_err(&pdev
->dev
, "probe - cannot get clock\n");
1493 status
= PTR_ERR(drv_data
->clk
);
1496 clk_enable(drv_data
->clk
);
1498 /* Find and map resources */
1499 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1501 dev_err(&pdev
->dev
, "probe - MEM resources not defined\n");
1505 drv_data
->ioarea
= request_mem_region(res
->start
,
1506 res
->end
- res
->start
+ 1,
1508 if (drv_data
->ioarea
== NULL
) {
1509 dev_err(&pdev
->dev
, "probe - cannot reserve region\n");
1513 drv_data
->regs
= ioremap(res
->start
, res
->end
- res
->start
+ 1);
1514 if (drv_data
->regs
== NULL
) {
1515 dev_err(&pdev
->dev
, "probe - cannot map IO\n");
1519 drv_data
->rd_data_phys
= (dma_addr_t
)res
->start
;
1522 irq
= platform_get_irq(pdev
, 0);
1524 dev_err(&pdev
->dev
, "probe - IRQ resource not defined\n");
1528 status
= request_irq(irq
, spi_int
, IRQF_DISABLED
, dev
->bus_id
, drv_data
);
1530 dev_err(&pdev
->dev
, "probe - cannot get IRQ (%d)\n", status
);
1534 /* Setup DMA if requested */
1535 drv_data
->tx_channel
= -1;
1536 drv_data
->rx_channel
= -1;
1537 if (platform_info
->enable_dma
) {
1538 /* Get rx DMA channel */
1539 drv_data
->rx_channel
= imx_dma_request_by_prio("spi_imx_rx",
1541 if (drv_data
->rx_channel
< 0) {
1543 "probe - problem (%d) requesting rx channel\n",
1544 drv_data
->rx_channel
);
1547 imx_dma_setup_handlers(drv_data
->rx_channel
, NULL
,
1548 dma_err_handler
, drv_data
);
1550 /* Get tx DMA channel */
1551 drv_data
->tx_channel
= imx_dma_request_by_prio("spi_imx_tx",
1553 if (drv_data
->tx_channel
< 0) {
1555 "probe - problem (%d) requesting tx channel\n",
1556 drv_data
->tx_channel
);
1557 imx_dma_free(drv_data
->rx_channel
);
1560 imx_dma_setup_handlers(drv_data
->tx_channel
,
1561 dma_tx_handler
, dma_err_handler
,
1564 /* Set request source and burst length for allocated channels */
1565 switch (drv_data
->pdev
->id
) {
1568 RSSR(drv_data
->rx_channel
) = DMA_REQ_SPI1_R
;
1569 RSSR(drv_data
->tx_channel
) = DMA_REQ_SPI1_T
;
1573 RSSR(drv_data
->rx_channel
) = DMA_REQ_SPI2_R
;
1574 RSSR(drv_data
->tx_channel
) = DMA_REQ_SPI2_T
;
1577 dev_err(dev
, "probe - bad SPI Id\n");
1578 imx_dma_free(drv_data
->rx_channel
);
1579 imx_dma_free(drv_data
->tx_channel
);
1583 BLR(drv_data
->rx_channel
) = SPI_DMA_BLR
;
1584 BLR(drv_data
->tx_channel
) = SPI_DMA_BLR
;
1587 /* Load default SPI configuration */
1588 writel(SPI_RESET_START
, drv_data
->regs
+ SPI_RESET
);
1589 writel(0, drv_data
->regs
+ SPI_RESET
);
1590 writel(SPI_DEFAULT_CONTROL
, drv_data
->regs
+ SPI_CONTROL
);
1592 /* Initial and start queue */
1593 status
= init_queue(drv_data
);
1595 dev_err(&pdev
->dev
, "probe - problem initializing queue\n");
1596 goto err_init_queue
;
1598 status
= start_queue(drv_data
);
1600 dev_err(&pdev
->dev
, "probe - problem starting queue\n");
1601 goto err_start_queue
;
1604 /* Register with the SPI framework */
1605 platform_set_drvdata(pdev
, drv_data
);
1606 status
= spi_register_master(master
);
1608 dev_err(&pdev
->dev
, "probe - problem registering spi master\n");
1609 goto err_spi_register
;
1612 dev_dbg(dev
, "probe succeded\n");
1618 destroy_queue(drv_data
);
1623 free_irq(irq
, drv_data
);
1626 iounmap(drv_data
->regs
);
1629 release_resource(drv_data
->ioarea
);
1630 kfree(drv_data
->ioarea
);
1633 clk_disable(drv_data
->clk
);
1634 clk_put(drv_data
->clk
);
1637 spi_master_put(master
);
1644 static int __exit
spi_imx_remove(struct platform_device
*pdev
)
1646 struct driver_data
*drv_data
= platform_get_drvdata(pdev
);
1653 tasklet_kill(&drv_data
->pump_transfers
);
1655 /* Remove the queue */
1656 status
= destroy_queue(drv_data
);
1658 dev_err(&pdev
->dev
, "queue remove failed (%d)\n", status
);
1663 writel(SPI_RESET_START
, drv_data
->regs
+ SPI_RESET
);
1664 writel(0, drv_data
->regs
+ SPI_RESET
);
1667 if (drv_data
->master_info
->enable_dma
) {
1668 RSSR(drv_data
->rx_channel
) = 0;
1669 RSSR(drv_data
->tx_channel
) = 0;
1670 imx_dma_free(drv_data
->tx_channel
);
1671 imx_dma_free(drv_data
->rx_channel
);
1675 irq
= platform_get_irq(pdev
, 0);
1677 free_irq(irq
, drv_data
);
1679 clk_disable(drv_data
->clk
);
1680 clk_put(drv_data
->clk
);
1682 /* Release map resources */
1683 iounmap(drv_data
->regs
);
1684 release_resource(drv_data
->ioarea
);
1685 kfree(drv_data
->ioarea
);
1687 /* Disconnect from the SPI framework */
1688 spi_unregister_master(drv_data
->master
);
1689 spi_master_put(drv_data
->master
);
1691 /* Prevent double remove */
1692 platform_set_drvdata(pdev
, NULL
);
1694 dev_dbg(&pdev
->dev
, "remove succeded\n");
1699 static void spi_imx_shutdown(struct platform_device
*pdev
)
1701 struct driver_data
*drv_data
= platform_get_drvdata(pdev
);
1704 writel(SPI_RESET_START
, drv_data
->regs
+ SPI_RESET
);
1705 writel(0, drv_data
->regs
+ SPI_RESET
);
1707 dev_dbg(&pdev
->dev
, "shutdown succeded\n");
1712 static int spi_imx_suspend(struct platform_device
*pdev
, pm_message_t state
)
1714 struct driver_data
*drv_data
= platform_get_drvdata(pdev
);
1717 status
= stop_queue(drv_data
);
1719 dev_warn(&pdev
->dev
, "suspend cannot stop queue\n");
1723 dev_dbg(&pdev
->dev
, "suspended\n");
1728 static int spi_imx_resume(struct platform_device
*pdev
)
1730 struct driver_data
*drv_data
= platform_get_drvdata(pdev
);
1733 /* Start the queue running */
1734 status
= start_queue(drv_data
);
1736 dev_err(&pdev
->dev
, "problem starting queue (%d)\n", status
);
1738 dev_dbg(&pdev
->dev
, "resumed\n");
1743 #define spi_imx_suspend NULL
1744 #define spi_imx_resume NULL
1745 #endif /* CONFIG_PM */
1747 /* work with hotplug and coldplug */
1748 MODULE_ALIAS("platform:spi_imx");
1750 static struct platform_driver driver
= {
1753 .owner
= THIS_MODULE
,
1755 .remove
= __exit_p(spi_imx_remove
),
1756 .shutdown
= spi_imx_shutdown
,
1757 .suspend
= spi_imx_suspend
,
1758 .resume
= spi_imx_resume
,
1761 static int __init
spi_imx_init(void)
1763 return platform_driver_probe(&driver
, spi_imx_probe
);
1765 module_init(spi_imx_init
);
1767 static void __exit
spi_imx_exit(void)
1769 platform_driver_unregister(&driver
);
1771 module_exit(spi_imx_exit
);
1773 MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
1774 MODULE_DESCRIPTION("iMX SPI Controller Driver");
1775 MODULE_LICENSE("GPL");