1 /* linux/drivers/spi/spi_s3c64xx.c
3 * Copyright (C) 2009 Samsung Electronics Ltd.
4 * Jaswinder Singh <jassi.brar@samsung.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/workqueue.h>
24 #include <linux/delay.h>
25 #include <linux/clk.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/platform_device.h>
28 #include <linux/spi/spi.h>
31 #include <plat/s3c64xx-spi.h>
33 /* Registers and bit-fields */
35 #define S3C64XX_SPI_CH_CFG 0x00
36 #define S3C64XX_SPI_CLK_CFG 0x04
37 #define S3C64XX_SPI_MODE_CFG 0x08
38 #define S3C64XX_SPI_SLAVE_SEL 0x0C
39 #define S3C64XX_SPI_INT_EN 0x10
40 #define S3C64XX_SPI_STATUS 0x14
41 #define S3C64XX_SPI_TX_DATA 0x18
42 #define S3C64XX_SPI_RX_DATA 0x1C
43 #define S3C64XX_SPI_PACKET_CNT 0x20
44 #define S3C64XX_SPI_PENDING_CLR 0x24
45 #define S3C64XX_SPI_SWAP_CFG 0x28
46 #define S3C64XX_SPI_FB_CLK 0x2C
48 #define S3C64XX_SPI_CH_HS_EN (1<<6) /* High Speed Enable */
49 #define S3C64XX_SPI_CH_SW_RST (1<<5)
50 #define S3C64XX_SPI_CH_SLAVE (1<<4)
51 #define S3C64XX_SPI_CPOL_L (1<<3)
52 #define S3C64XX_SPI_CPHA_B (1<<2)
53 #define S3C64XX_SPI_CH_RXCH_ON (1<<1)
54 #define S3C64XX_SPI_CH_TXCH_ON (1<<0)
56 #define S3C64XX_SPI_CLKSEL_SRCMSK (3<<9)
57 #define S3C64XX_SPI_CLKSEL_SRCSHFT 9
58 #define S3C64XX_SPI_ENCLK_ENABLE (1<<8)
59 #define S3C64XX_SPI_PSR_MASK 0xff
61 #define S3C64XX_SPI_MODE_CH_TSZ_BYTE (0<<29)
62 #define S3C64XX_SPI_MODE_CH_TSZ_HALFWORD (1<<29)
63 #define S3C64XX_SPI_MODE_CH_TSZ_WORD (2<<29)
64 #define S3C64XX_SPI_MODE_CH_TSZ_MASK (3<<29)
65 #define S3C64XX_SPI_MODE_BUS_TSZ_BYTE (0<<17)
66 #define S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD (1<<17)
67 #define S3C64XX_SPI_MODE_BUS_TSZ_WORD (2<<17)
68 #define S3C64XX_SPI_MODE_BUS_TSZ_MASK (3<<17)
69 #define S3C64XX_SPI_MODE_RXDMA_ON (1<<2)
70 #define S3C64XX_SPI_MODE_TXDMA_ON (1<<1)
71 #define S3C64XX_SPI_MODE_4BURST (1<<0)
73 #define S3C64XX_SPI_SLAVE_AUTO (1<<1)
74 #define S3C64XX_SPI_SLAVE_SIG_INACT (1<<0)
76 #define S3C64XX_SPI_ACT(c) writel(0, (c)->regs + S3C64XX_SPI_SLAVE_SEL)
78 #define S3C64XX_SPI_DEACT(c) writel(S3C64XX_SPI_SLAVE_SIG_INACT, \
79 (c)->regs + S3C64XX_SPI_SLAVE_SEL)
81 #define S3C64XX_SPI_INT_TRAILING_EN (1<<6)
82 #define S3C64XX_SPI_INT_RX_OVERRUN_EN (1<<5)
83 #define S3C64XX_SPI_INT_RX_UNDERRUN_EN (1<<4)
84 #define S3C64XX_SPI_INT_TX_OVERRUN_EN (1<<3)
85 #define S3C64XX_SPI_INT_TX_UNDERRUN_EN (1<<2)
86 #define S3C64XX_SPI_INT_RX_FIFORDY_EN (1<<1)
87 #define S3C64XX_SPI_INT_TX_FIFORDY_EN (1<<0)
89 #define S3C64XX_SPI_ST_RX_OVERRUN_ERR (1<<5)
90 #define S3C64XX_SPI_ST_RX_UNDERRUN_ERR (1<<4)
91 #define S3C64XX_SPI_ST_TX_OVERRUN_ERR (1<<3)
92 #define S3C64XX_SPI_ST_TX_UNDERRUN_ERR (1<<2)
93 #define S3C64XX_SPI_ST_RX_FIFORDY (1<<1)
94 #define S3C64XX_SPI_ST_TX_FIFORDY (1<<0)
96 #define S3C64XX_SPI_PACKET_CNT_EN (1<<16)
98 #define S3C64XX_SPI_PND_TX_UNDERRUN_CLR (1<<4)
99 #define S3C64XX_SPI_PND_TX_OVERRUN_CLR (1<<3)
100 #define S3C64XX_SPI_PND_RX_UNDERRUN_CLR (1<<2)
101 #define S3C64XX_SPI_PND_RX_OVERRUN_CLR (1<<1)
102 #define S3C64XX_SPI_PND_TRAILING_CLR (1<<0)
104 #define S3C64XX_SPI_SWAP_RX_HALF_WORD (1<<7)
105 #define S3C64XX_SPI_SWAP_RX_BYTE (1<<6)
106 #define S3C64XX_SPI_SWAP_RX_BIT (1<<5)
107 #define S3C64XX_SPI_SWAP_RX_EN (1<<4)
108 #define S3C64XX_SPI_SWAP_TX_HALF_WORD (1<<3)
109 #define S3C64XX_SPI_SWAP_TX_BYTE (1<<2)
110 #define S3C64XX_SPI_SWAP_TX_BIT (1<<1)
111 #define S3C64XX_SPI_SWAP_TX_EN (1<<0)
113 #define S3C64XX_SPI_FBCLK_MSK (3<<0)
115 #define S3C64XX_SPI_ST_TRLCNTZ(v, i) ((((v) >> (i)->rx_lvl_offset) & \
116 (((i)->fifo_lvl_mask + 1))) \
119 #define S3C64XX_SPI_ST_TX_DONE(v, i) ((((v) >> (i)->rx_lvl_offset) & \
120 (((i)->fifo_lvl_mask + 1) << 1)) \
122 #define TX_FIFO_LVL(v, i) (((v) >> 6) & (i)->fifo_lvl_mask)
123 #define RX_FIFO_LVL(v, i) (((v) >> (i)->rx_lvl_offset) & (i)->fifo_lvl_mask)
125 #define S3C64XX_SPI_MAX_TRAILCNT 0x3ff
126 #define S3C64XX_SPI_TRAILCNT_OFF 19
128 #define S3C64XX_SPI_TRAILCNT S3C64XX_SPI_MAX_TRAILCNT
130 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
132 #define SUSPND (1<<0)
133 #define SPIBUSY (1<<1)
134 #define RXBUSY (1<<2)
135 #define TXBUSY (1<<3)
138 * struct s3c64xx_spi_driver_data - Runtime info holder for SPI driver.
139 * @clk: Pointer to the spi clock.
140 * @src_clk: Pointer to the clock used to generate SPI signals.
141 * @master: Pointer to the SPI Protocol master.
142 * @workqueue: Work queue for the SPI xfer requests.
143 * @cntrlr_info: Platform specific data for the controller this driver manages.
144 * @tgl_spi: Pointer to the last CS left untoggled by the cs_change hint.
146 * @queue: To log SPI xfer requests.
147 * @lock: Controller specific lock.
148 * @state: Set of FLAGS to indicate status.
149 * @rx_dmach: Controller's DMA channel for Rx.
150 * @tx_dmach: Controller's DMA channel for Tx.
151 * @sfr_start: BUS address of SPI controller regs.
152 * @regs: Pointer to ioremap'ed controller registers.
153 * @xfer_completion: To indicate completion of xfer task.
154 * @cur_mode: Stores the active configuration of the controller.
155 * @cur_bpw: Stores the active bits per word settings.
156 * @cur_speed: Stores the active xfer clock speed.
158 struct s3c64xx_spi_driver_data
{
162 struct platform_device
*pdev
;
163 struct spi_master
*master
;
164 struct workqueue_struct
*workqueue
;
165 struct s3c64xx_spi_info
*cntrlr_info
;
166 struct spi_device
*tgl_spi
;
167 struct work_struct work
;
168 struct list_head queue
;
170 enum dma_ch rx_dmach
;
171 enum dma_ch tx_dmach
;
172 unsigned long sfr_start
;
173 struct completion xfer_completion
;
175 unsigned cur_mode
, cur_bpw
;
179 static struct s3c2410_dma_client s3c64xx_spi_dma_client
= {
180 .name
= "samsung-spi-dma",
183 static void flush_fifo(struct s3c64xx_spi_driver_data
*sdd
)
185 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
186 void __iomem
*regs
= sdd
->regs
;
190 writel(0, regs
+ S3C64XX_SPI_PACKET_CNT
);
192 val
= readl(regs
+ S3C64XX_SPI_CH_CFG
);
193 val
|= S3C64XX_SPI_CH_SW_RST
;
194 val
&= ~S3C64XX_SPI_CH_HS_EN
;
195 writel(val
, regs
+ S3C64XX_SPI_CH_CFG
);
198 loops
= msecs_to_loops(1);
200 val
= readl(regs
+ S3C64XX_SPI_STATUS
);
201 } while (TX_FIFO_LVL(val
, sci
) && loops
--);
204 dev_warn(&sdd
->pdev
->dev
, "Timed out flushing TX FIFO\n");
207 loops
= msecs_to_loops(1);
209 val
= readl(regs
+ S3C64XX_SPI_STATUS
);
210 if (RX_FIFO_LVL(val
, sci
))
211 readl(regs
+ S3C64XX_SPI_RX_DATA
);
217 dev_warn(&sdd
->pdev
->dev
, "Timed out flushing RX FIFO\n");
219 val
= readl(regs
+ S3C64XX_SPI_CH_CFG
);
220 val
&= ~S3C64XX_SPI_CH_SW_RST
;
221 writel(val
, regs
+ S3C64XX_SPI_CH_CFG
);
223 val
= readl(regs
+ S3C64XX_SPI_MODE_CFG
);
224 val
&= ~(S3C64XX_SPI_MODE_TXDMA_ON
| S3C64XX_SPI_MODE_RXDMA_ON
);
225 writel(val
, regs
+ S3C64XX_SPI_MODE_CFG
);
227 val
= readl(regs
+ S3C64XX_SPI_CH_CFG
);
228 val
&= ~(S3C64XX_SPI_CH_RXCH_ON
| S3C64XX_SPI_CH_TXCH_ON
);
229 writel(val
, regs
+ S3C64XX_SPI_CH_CFG
);
232 static void enable_datapath(struct s3c64xx_spi_driver_data
*sdd
,
233 struct spi_device
*spi
,
234 struct spi_transfer
*xfer
, int dma_mode
)
236 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
237 void __iomem
*regs
= sdd
->regs
;
240 modecfg
= readl(regs
+ S3C64XX_SPI_MODE_CFG
);
241 modecfg
&= ~(S3C64XX_SPI_MODE_TXDMA_ON
| S3C64XX_SPI_MODE_RXDMA_ON
);
243 chcfg
= readl(regs
+ S3C64XX_SPI_CH_CFG
);
244 chcfg
&= ~S3C64XX_SPI_CH_TXCH_ON
;
247 chcfg
&= ~S3C64XX_SPI_CH_RXCH_ON
;
249 /* Always shift in data in FIFO, even if xfer is Tx only,
250 * this helps setting PCKT_CNT value for generating clocks
253 chcfg
|= S3C64XX_SPI_CH_RXCH_ON
;
254 writel(((xfer
->len
* 8 / sdd
->cur_bpw
) & 0xffff)
255 | S3C64XX_SPI_PACKET_CNT_EN
,
256 regs
+ S3C64XX_SPI_PACKET_CNT
);
259 if (xfer
->tx_buf
!= NULL
) {
260 sdd
->state
|= TXBUSY
;
261 chcfg
|= S3C64XX_SPI_CH_TXCH_ON
;
263 modecfg
|= S3C64XX_SPI_MODE_TXDMA_ON
;
264 s3c2410_dma_config(sdd
->tx_dmach
, sdd
->cur_bpw
/ 8);
265 s3c2410_dma_enqueue(sdd
->tx_dmach
, (void *)sdd
,
266 xfer
->tx_dma
, xfer
->len
);
267 s3c2410_dma_ctrl(sdd
->tx_dmach
, S3C2410_DMAOP_START
);
269 switch (sdd
->cur_bpw
) {
271 iowrite32_rep(regs
+ S3C64XX_SPI_TX_DATA
,
272 xfer
->tx_buf
, xfer
->len
/ 4);
275 iowrite16_rep(regs
+ S3C64XX_SPI_TX_DATA
,
276 xfer
->tx_buf
, xfer
->len
/ 2);
279 iowrite8_rep(regs
+ S3C64XX_SPI_TX_DATA
,
280 xfer
->tx_buf
, xfer
->len
);
286 if (xfer
->rx_buf
!= NULL
) {
287 sdd
->state
|= RXBUSY
;
289 if (sci
->high_speed
&& sdd
->cur_speed
>= 30000000UL
290 && !(sdd
->cur_mode
& SPI_CPHA
))
291 chcfg
|= S3C64XX_SPI_CH_HS_EN
;
294 modecfg
|= S3C64XX_SPI_MODE_RXDMA_ON
;
295 chcfg
|= S3C64XX_SPI_CH_RXCH_ON
;
296 writel(((xfer
->len
* 8 / sdd
->cur_bpw
) & 0xffff)
297 | S3C64XX_SPI_PACKET_CNT_EN
,
298 regs
+ S3C64XX_SPI_PACKET_CNT
);
299 s3c2410_dma_config(sdd
->rx_dmach
, sdd
->cur_bpw
/ 8);
300 s3c2410_dma_enqueue(sdd
->rx_dmach
, (void *)sdd
,
301 xfer
->rx_dma
, xfer
->len
);
302 s3c2410_dma_ctrl(sdd
->rx_dmach
, S3C2410_DMAOP_START
);
306 writel(modecfg
, regs
+ S3C64XX_SPI_MODE_CFG
);
307 writel(chcfg
, regs
+ S3C64XX_SPI_CH_CFG
);
310 static inline void enable_cs(struct s3c64xx_spi_driver_data
*sdd
,
311 struct spi_device
*spi
)
313 struct s3c64xx_spi_csinfo
*cs
;
315 if (sdd
->tgl_spi
!= NULL
) { /* If last device toggled after mssg */
316 if (sdd
->tgl_spi
!= spi
) { /* if last mssg on diff device */
317 /* Deselect the last toggled device */
318 cs
= sdd
->tgl_spi
->controller_data
;
319 cs
->set_level(cs
->line
,
320 spi
->mode
& SPI_CS_HIGH
? 0 : 1);
325 cs
= spi
->controller_data
;
326 cs
->set_level(cs
->line
, spi
->mode
& SPI_CS_HIGH
? 1 : 0);
329 static int wait_for_xfer(struct s3c64xx_spi_driver_data
*sdd
,
330 struct spi_transfer
*xfer
, int dma_mode
)
332 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
333 void __iomem
*regs
= sdd
->regs
;
337 /* millisecs to xfer 'len' bytes @ 'cur_speed' */
338 ms
= xfer
->len
* 8 * 1000 / sdd
->cur_speed
;
339 ms
+= 10; /* some tolerance */
342 val
= msecs_to_jiffies(ms
) + 10;
343 val
= wait_for_completion_timeout(&sdd
->xfer_completion
, val
);
346 val
= msecs_to_loops(ms
);
348 status
= readl(regs
+ S3C64XX_SPI_STATUS
);
349 } while (RX_FIFO_LVL(status
, sci
) < xfer
->len
&& --val
);
359 * DmaTx returns after simply writing data in the FIFO,
360 * w/o waiting for real transmission on the bus to finish.
361 * DmaRx returns only after Dma read data from FIFO which
362 * needs bus transmission to finish, so we don't worry if
363 * Xfer involved Rx(with or without Tx).
365 if (xfer
->rx_buf
== NULL
) {
366 val
= msecs_to_loops(10);
367 status
= readl(regs
+ S3C64XX_SPI_STATUS
);
368 while ((TX_FIFO_LVL(status
, sci
)
369 || !S3C64XX_SPI_ST_TX_DONE(status
, sci
))
372 status
= readl(regs
+ S3C64XX_SPI_STATUS
);
379 /* If it was only Tx */
380 if (xfer
->rx_buf
== NULL
) {
381 sdd
->state
&= ~TXBUSY
;
385 switch (sdd
->cur_bpw
) {
387 ioread32_rep(regs
+ S3C64XX_SPI_RX_DATA
,
388 xfer
->rx_buf
, xfer
->len
/ 4);
391 ioread16_rep(regs
+ S3C64XX_SPI_RX_DATA
,
392 xfer
->rx_buf
, xfer
->len
/ 2);
395 ioread8_rep(regs
+ S3C64XX_SPI_RX_DATA
,
396 xfer
->rx_buf
, xfer
->len
);
399 sdd
->state
&= ~RXBUSY
;
405 static inline void disable_cs(struct s3c64xx_spi_driver_data
*sdd
,
406 struct spi_device
*spi
)
408 struct s3c64xx_spi_csinfo
*cs
= spi
->controller_data
;
410 if (sdd
->tgl_spi
== spi
)
413 cs
->set_level(cs
->line
, spi
->mode
& SPI_CS_HIGH
? 0 : 1);
416 static void s3c64xx_spi_config(struct s3c64xx_spi_driver_data
*sdd
)
418 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
419 void __iomem
*regs
= sdd
->regs
;
423 if (sci
->clk_from_cmu
) {
424 clk_disable(sdd
->src_clk
);
426 val
= readl(regs
+ S3C64XX_SPI_CLK_CFG
);
427 val
&= ~S3C64XX_SPI_ENCLK_ENABLE
;
428 writel(val
, regs
+ S3C64XX_SPI_CLK_CFG
);
431 /* Set Polarity and Phase */
432 val
= readl(regs
+ S3C64XX_SPI_CH_CFG
);
433 val
&= ~(S3C64XX_SPI_CH_SLAVE
|
437 if (sdd
->cur_mode
& SPI_CPOL
)
438 val
|= S3C64XX_SPI_CPOL_L
;
440 if (sdd
->cur_mode
& SPI_CPHA
)
441 val
|= S3C64XX_SPI_CPHA_B
;
443 writel(val
, regs
+ S3C64XX_SPI_CH_CFG
);
445 /* Set Channel & DMA Mode */
446 val
= readl(regs
+ S3C64XX_SPI_MODE_CFG
);
447 val
&= ~(S3C64XX_SPI_MODE_BUS_TSZ_MASK
448 | S3C64XX_SPI_MODE_CH_TSZ_MASK
);
450 switch (sdd
->cur_bpw
) {
452 val
|= S3C64XX_SPI_MODE_BUS_TSZ_WORD
;
453 val
|= S3C64XX_SPI_MODE_CH_TSZ_WORD
;
456 val
|= S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD
;
457 val
|= S3C64XX_SPI_MODE_CH_TSZ_HALFWORD
;
460 val
|= S3C64XX_SPI_MODE_BUS_TSZ_BYTE
;
461 val
|= S3C64XX_SPI_MODE_CH_TSZ_BYTE
;
465 writel(val
, regs
+ S3C64XX_SPI_MODE_CFG
);
467 if (sci
->clk_from_cmu
) {
468 /* Configure Clock */
469 /* There is half-multiplier before the SPI */
470 clk_set_rate(sdd
->src_clk
, sdd
->cur_speed
* 2);
472 clk_enable(sdd
->src_clk
);
474 /* Configure Clock */
475 val
= readl(regs
+ S3C64XX_SPI_CLK_CFG
);
476 val
&= ~S3C64XX_SPI_PSR_MASK
;
477 val
|= ((clk_get_rate(sdd
->src_clk
) / sdd
->cur_speed
/ 2 - 1)
478 & S3C64XX_SPI_PSR_MASK
);
479 writel(val
, regs
+ S3C64XX_SPI_CLK_CFG
);
482 val
= readl(regs
+ S3C64XX_SPI_CLK_CFG
);
483 val
|= S3C64XX_SPI_ENCLK_ENABLE
;
484 writel(val
, regs
+ S3C64XX_SPI_CLK_CFG
);
488 static void s3c64xx_spi_dma_rxcb(struct s3c2410_dma_chan
*chan
, void *buf_id
,
489 int size
, enum s3c2410_dma_buffresult res
)
491 struct s3c64xx_spi_driver_data
*sdd
= buf_id
;
494 spin_lock_irqsave(&sdd
->lock
, flags
);
496 if (res
== S3C2410_RES_OK
)
497 sdd
->state
&= ~RXBUSY
;
499 dev_err(&sdd
->pdev
->dev
, "DmaAbrtRx-%d\n", size
);
501 /* If the other done */
502 if (!(sdd
->state
& TXBUSY
))
503 complete(&sdd
->xfer_completion
);
505 spin_unlock_irqrestore(&sdd
->lock
, flags
);
508 static void s3c64xx_spi_dma_txcb(struct s3c2410_dma_chan
*chan
, void *buf_id
,
509 int size
, enum s3c2410_dma_buffresult res
)
511 struct s3c64xx_spi_driver_data
*sdd
= buf_id
;
514 spin_lock_irqsave(&sdd
->lock
, flags
);
516 if (res
== S3C2410_RES_OK
)
517 sdd
->state
&= ~TXBUSY
;
519 dev_err(&sdd
->pdev
->dev
, "DmaAbrtTx-%d \n", size
);
521 /* If the other done */
522 if (!(sdd
->state
& RXBUSY
))
523 complete(&sdd
->xfer_completion
);
525 spin_unlock_irqrestore(&sdd
->lock
, flags
);
528 #define XFER_DMAADDR_INVALID DMA_BIT_MASK(32)
530 static int s3c64xx_spi_map_mssg(struct s3c64xx_spi_driver_data
*sdd
,
531 struct spi_message
*msg
)
533 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
534 struct device
*dev
= &sdd
->pdev
->dev
;
535 struct spi_transfer
*xfer
;
537 if (msg
->is_dma_mapped
)
540 /* First mark all xfer unmapped */
541 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
542 xfer
->rx_dma
= XFER_DMAADDR_INVALID
;
543 xfer
->tx_dma
= XFER_DMAADDR_INVALID
;
546 /* Map until end or first fail */
547 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
549 if (xfer
->len
<= ((sci
->fifo_lvl_mask
>> 1) + 1))
552 if (xfer
->tx_buf
!= NULL
) {
553 xfer
->tx_dma
= dma_map_single(dev
,
554 (void *)xfer
->tx_buf
, xfer
->len
,
556 if (dma_mapping_error(dev
, xfer
->tx_dma
)) {
557 dev_err(dev
, "dma_map_single Tx failed\n");
558 xfer
->tx_dma
= XFER_DMAADDR_INVALID
;
563 if (xfer
->rx_buf
!= NULL
) {
564 xfer
->rx_dma
= dma_map_single(dev
, xfer
->rx_buf
,
565 xfer
->len
, DMA_FROM_DEVICE
);
566 if (dma_mapping_error(dev
, xfer
->rx_dma
)) {
567 dev_err(dev
, "dma_map_single Rx failed\n");
568 dma_unmap_single(dev
, xfer
->tx_dma
,
569 xfer
->len
, DMA_TO_DEVICE
);
570 xfer
->tx_dma
= XFER_DMAADDR_INVALID
;
571 xfer
->rx_dma
= XFER_DMAADDR_INVALID
;
580 static void s3c64xx_spi_unmap_mssg(struct s3c64xx_spi_driver_data
*sdd
,
581 struct spi_message
*msg
)
583 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
584 struct device
*dev
= &sdd
->pdev
->dev
;
585 struct spi_transfer
*xfer
;
587 if (msg
->is_dma_mapped
)
590 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
592 if (xfer
->len
<= ((sci
->fifo_lvl_mask
>> 1) + 1))
595 if (xfer
->rx_buf
!= NULL
596 && xfer
->rx_dma
!= XFER_DMAADDR_INVALID
)
597 dma_unmap_single(dev
, xfer
->rx_dma
,
598 xfer
->len
, DMA_FROM_DEVICE
);
600 if (xfer
->tx_buf
!= NULL
601 && xfer
->tx_dma
!= XFER_DMAADDR_INVALID
)
602 dma_unmap_single(dev
, xfer
->tx_dma
,
603 xfer
->len
, DMA_TO_DEVICE
);
607 static void handle_msg(struct s3c64xx_spi_driver_data
*sdd
,
608 struct spi_message
*msg
)
610 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
611 struct spi_device
*spi
= msg
->spi
;
612 struct s3c64xx_spi_csinfo
*cs
= spi
->controller_data
;
613 struct spi_transfer
*xfer
;
614 int status
= 0, cs_toggle
= 0;
618 /* If Master's(controller) state differs from that needed by Slave */
619 if (sdd
->cur_speed
!= spi
->max_speed_hz
620 || sdd
->cur_mode
!= spi
->mode
621 || sdd
->cur_bpw
!= spi
->bits_per_word
) {
622 sdd
->cur_bpw
= spi
->bits_per_word
;
623 sdd
->cur_speed
= spi
->max_speed_hz
;
624 sdd
->cur_mode
= spi
->mode
;
625 s3c64xx_spi_config(sdd
);
628 /* Map all the transfers if needed */
629 if (s3c64xx_spi_map_mssg(sdd
, msg
)) {
631 "Xfer: Unable to map message buffers!\n");
636 /* Configure feedback delay */
637 writel(cs
->fb_delay
& 0x3, sdd
->regs
+ S3C64XX_SPI_FB_CLK
);
639 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
644 INIT_COMPLETION(sdd
->xfer_completion
);
646 /* Only BPW and Speed may change across transfers */
647 bpw
= xfer
->bits_per_word
? : spi
->bits_per_word
;
648 speed
= xfer
->speed_hz
? : spi
->max_speed_hz
;
650 if (xfer
->len
% (bpw
/ 8)) {
652 "Xfer length(%u) not a multiple of word size(%u)\n",
658 if (bpw
!= sdd
->cur_bpw
|| speed
!= sdd
->cur_speed
) {
660 sdd
->cur_speed
= speed
;
661 s3c64xx_spi_config(sdd
);
664 /* Polling method for xfers not bigger than FIFO capacity */
665 if (xfer
->len
<= ((sci
->fifo_lvl_mask
>> 1) + 1))
670 spin_lock_irqsave(&sdd
->lock
, flags
);
672 /* Pending only which is to be done */
673 sdd
->state
&= ~RXBUSY
;
674 sdd
->state
&= ~TXBUSY
;
676 enable_datapath(sdd
, spi
, xfer
, use_dma
);
681 /* Start the signals */
682 S3C64XX_SPI_ACT(sdd
);
684 spin_unlock_irqrestore(&sdd
->lock
, flags
);
686 status
= wait_for_xfer(sdd
, xfer
, use_dma
);
688 /* Quiese the signals */
689 S3C64XX_SPI_DEACT(sdd
);
692 dev_err(&spi
->dev
, "I/O Error: "
693 "rx-%d tx-%d res:rx-%c tx-%c len-%d\n",
694 xfer
->rx_buf
? 1 : 0, xfer
->tx_buf
? 1 : 0,
695 (sdd
->state
& RXBUSY
) ? 'f' : 'p',
696 (sdd
->state
& TXBUSY
) ? 'f' : 'p',
700 if (xfer
->tx_buf
!= NULL
701 && (sdd
->state
& TXBUSY
))
702 s3c2410_dma_ctrl(sdd
->tx_dmach
,
703 S3C2410_DMAOP_FLUSH
);
704 if (xfer
->rx_buf
!= NULL
705 && (sdd
->state
& RXBUSY
))
706 s3c2410_dma_ctrl(sdd
->rx_dmach
,
707 S3C2410_DMAOP_FLUSH
);
713 if (xfer
->delay_usecs
)
714 udelay(xfer
->delay_usecs
);
716 if (xfer
->cs_change
) {
717 /* Hint that the next mssg is gonna be
718 for the same device */
719 if (list_is_last(&xfer
->transfer_list
,
723 disable_cs(sdd
, spi
);
726 msg
->actual_length
+= xfer
->len
;
732 if (!cs_toggle
|| status
)
733 disable_cs(sdd
, spi
);
737 s3c64xx_spi_unmap_mssg(sdd
, msg
);
739 msg
->status
= status
;
742 msg
->complete(msg
->context
);
745 static int acquire_dma(struct s3c64xx_spi_driver_data
*sdd
)
747 if (s3c2410_dma_request(sdd
->rx_dmach
,
748 &s3c64xx_spi_dma_client
, NULL
) < 0) {
749 dev_err(&sdd
->pdev
->dev
, "cannot get RxDMA\n");
752 s3c2410_dma_set_buffdone_fn(sdd
->rx_dmach
, s3c64xx_spi_dma_rxcb
);
753 s3c2410_dma_devconfig(sdd
->rx_dmach
, S3C2410_DMASRC_HW
,
754 sdd
->sfr_start
+ S3C64XX_SPI_RX_DATA
);
756 if (s3c2410_dma_request(sdd
->tx_dmach
,
757 &s3c64xx_spi_dma_client
, NULL
) < 0) {
758 dev_err(&sdd
->pdev
->dev
, "cannot get TxDMA\n");
759 s3c2410_dma_free(sdd
->rx_dmach
, &s3c64xx_spi_dma_client
);
762 s3c2410_dma_set_buffdone_fn(sdd
->tx_dmach
, s3c64xx_spi_dma_txcb
);
763 s3c2410_dma_devconfig(sdd
->tx_dmach
, S3C2410_DMASRC_MEM
,
764 sdd
->sfr_start
+ S3C64XX_SPI_TX_DATA
);
769 static void s3c64xx_spi_work(struct work_struct
*work
)
771 struct s3c64xx_spi_driver_data
*sdd
= container_of(work
,
772 struct s3c64xx_spi_driver_data
, work
);
775 /* Acquire DMA channels */
776 while (!acquire_dma(sdd
))
779 spin_lock_irqsave(&sdd
->lock
, flags
);
781 while (!list_empty(&sdd
->queue
)
782 && !(sdd
->state
& SUSPND
)) {
784 struct spi_message
*msg
;
786 msg
= container_of(sdd
->queue
.next
, struct spi_message
, queue
);
788 list_del_init(&msg
->queue
);
790 /* Set Xfer busy flag */
791 sdd
->state
|= SPIBUSY
;
793 spin_unlock_irqrestore(&sdd
->lock
, flags
);
795 handle_msg(sdd
, msg
);
797 spin_lock_irqsave(&sdd
->lock
, flags
);
799 sdd
->state
&= ~SPIBUSY
;
802 spin_unlock_irqrestore(&sdd
->lock
, flags
);
804 /* Free DMA channels */
805 s3c2410_dma_free(sdd
->tx_dmach
, &s3c64xx_spi_dma_client
);
806 s3c2410_dma_free(sdd
->rx_dmach
, &s3c64xx_spi_dma_client
);
809 static int s3c64xx_spi_transfer(struct spi_device
*spi
,
810 struct spi_message
*msg
)
812 struct s3c64xx_spi_driver_data
*sdd
;
815 sdd
= spi_master_get_devdata(spi
->master
);
817 spin_lock_irqsave(&sdd
->lock
, flags
);
819 if (sdd
->state
& SUSPND
) {
820 spin_unlock_irqrestore(&sdd
->lock
, flags
);
824 msg
->status
= -EINPROGRESS
;
825 msg
->actual_length
= 0;
827 list_add_tail(&msg
->queue
, &sdd
->queue
);
829 queue_work(sdd
->workqueue
, &sdd
->work
);
831 spin_unlock_irqrestore(&sdd
->lock
, flags
);
837 * Here we only check the validity of requested configuration
838 * and save the configuration in a local data-structure.
839 * The controller is actually configured only just before we
840 * get a message to transfer.
842 static int s3c64xx_spi_setup(struct spi_device
*spi
)
844 struct s3c64xx_spi_csinfo
*cs
= spi
->controller_data
;
845 struct s3c64xx_spi_driver_data
*sdd
;
846 struct s3c64xx_spi_info
*sci
;
847 struct spi_message
*msg
;
851 if (cs
== NULL
|| cs
->set_level
== NULL
) {
852 dev_err(&spi
->dev
, "No CS for SPI(%d)\n", spi
->chip_select
);
856 sdd
= spi_master_get_devdata(spi
->master
);
857 sci
= sdd
->cntrlr_info
;
859 spin_lock_irqsave(&sdd
->lock
, flags
);
861 list_for_each_entry(msg
, &sdd
->queue
, queue
) {
862 /* Is some mssg is already queued for this device */
863 if (msg
->spi
== spi
) {
865 "setup: attempt while mssg in queue!\n");
866 spin_unlock_irqrestore(&sdd
->lock
, flags
);
871 if (sdd
->state
& SUSPND
) {
872 spin_unlock_irqrestore(&sdd
->lock
, flags
);
874 "setup: SPI-%d not active!\n", spi
->master
->bus_num
);
878 spin_unlock_irqrestore(&sdd
->lock
, flags
);
880 if (spi
->bits_per_word
!= 8
881 && spi
->bits_per_word
!= 16
882 && spi
->bits_per_word
!= 32) {
883 dev_err(&spi
->dev
, "setup: %dbits/wrd not supported!\n",
889 /* Check if we can provide the requested rate */
890 if (!sci
->clk_from_cmu
) {
894 speed
= clk_get_rate(sdd
->src_clk
) / 2 / (0 + 1);
896 if (spi
->max_speed_hz
> speed
)
897 spi
->max_speed_hz
= speed
;
899 psr
= clk_get_rate(sdd
->src_clk
) / 2 / spi
->max_speed_hz
- 1;
900 psr
&= S3C64XX_SPI_PSR_MASK
;
901 if (psr
== S3C64XX_SPI_PSR_MASK
)
904 speed
= clk_get_rate(sdd
->src_clk
) / 2 / (psr
+ 1);
905 if (spi
->max_speed_hz
< speed
) {
906 if (psr
+1 < S3C64XX_SPI_PSR_MASK
) {
914 speed
= clk_get_rate(sdd
->src_clk
) / 2 / (psr
+ 1);
915 if (spi
->max_speed_hz
>= speed
)
916 spi
->max_speed_hz
= speed
;
923 /* setup() returns with device de-selected */
924 disable_cs(sdd
, spi
);
929 static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data
*sdd
, int channel
)
931 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
932 void __iomem
*regs
= sdd
->regs
;
937 S3C64XX_SPI_DEACT(sdd
);
939 /* Disable Interrupts - we use Polling if not DMA mode */
940 writel(0, regs
+ S3C64XX_SPI_INT_EN
);
942 if (!sci
->clk_from_cmu
)
943 writel(sci
->src_clk_nr
<< S3C64XX_SPI_CLKSEL_SRCSHFT
,
944 regs
+ S3C64XX_SPI_CLK_CFG
);
945 writel(0, regs
+ S3C64XX_SPI_MODE_CFG
);
946 writel(0, regs
+ S3C64XX_SPI_PACKET_CNT
);
948 /* Clear any irq pending bits */
949 writel(readl(regs
+ S3C64XX_SPI_PENDING_CLR
),
950 regs
+ S3C64XX_SPI_PENDING_CLR
);
952 writel(0, regs
+ S3C64XX_SPI_SWAP_CFG
);
954 val
= readl(regs
+ S3C64XX_SPI_MODE_CFG
);
955 val
&= ~S3C64XX_SPI_MODE_4BURST
;
956 val
&= ~(S3C64XX_SPI_MAX_TRAILCNT
<< S3C64XX_SPI_TRAILCNT_OFF
);
957 val
|= (S3C64XX_SPI_TRAILCNT
<< S3C64XX_SPI_TRAILCNT_OFF
);
958 writel(val
, regs
+ S3C64XX_SPI_MODE_CFG
);
963 static int __init
s3c64xx_spi_probe(struct platform_device
*pdev
)
965 struct resource
*mem_res
, *dmatx_res
, *dmarx_res
;
966 struct s3c64xx_spi_driver_data
*sdd
;
967 struct s3c64xx_spi_info
*sci
;
968 struct spi_master
*master
;
973 "Invalid platform device id-%d\n", pdev
->id
);
977 if (pdev
->dev
.platform_data
== NULL
) {
978 dev_err(&pdev
->dev
, "platform_data missing!\n");
982 sci
= pdev
->dev
.platform_data
;
983 if (!sci
->src_clk_name
) {
985 "Board init must call s3c64xx_spi_set_info()\n");
989 /* Check for availability of necessary resource */
991 dmatx_res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
992 if (dmatx_res
== NULL
) {
993 dev_err(&pdev
->dev
, "Unable to get SPI-Tx dma resource\n");
997 dmarx_res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
998 if (dmarx_res
== NULL
) {
999 dev_err(&pdev
->dev
, "Unable to get SPI-Rx dma resource\n");
1003 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1004 if (mem_res
== NULL
) {
1005 dev_err(&pdev
->dev
, "Unable to get SPI MEM resource\n");
1009 master
= spi_alloc_master(&pdev
->dev
,
1010 sizeof(struct s3c64xx_spi_driver_data
));
1011 if (master
== NULL
) {
1012 dev_err(&pdev
->dev
, "Unable to allocate SPI Master\n");
1016 platform_set_drvdata(pdev
, master
);
1018 sdd
= spi_master_get_devdata(master
);
1019 sdd
->master
= master
;
1020 sdd
->cntrlr_info
= sci
;
1022 sdd
->sfr_start
= mem_res
->start
;
1023 sdd
->tx_dmach
= dmatx_res
->start
;
1024 sdd
->rx_dmach
= dmarx_res
->start
;
1028 master
->bus_num
= pdev
->id
;
1029 master
->setup
= s3c64xx_spi_setup
;
1030 master
->transfer
= s3c64xx_spi_transfer
;
1031 master
->num_chipselect
= sci
->num_cs
;
1032 master
->dma_alignment
= 8;
1033 /* the spi->mode bits understood by this driver: */
1034 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
1036 if (request_mem_region(mem_res
->start
,
1037 resource_size(mem_res
), pdev
->name
) == NULL
) {
1038 dev_err(&pdev
->dev
, "Req mem region failed\n");
1043 sdd
->regs
= ioremap(mem_res
->start
, resource_size(mem_res
));
1044 if (sdd
->regs
== NULL
) {
1045 dev_err(&pdev
->dev
, "Unable to remap IO\n");
1050 if (sci
->cfg_gpio
== NULL
|| sci
->cfg_gpio(pdev
)) {
1051 dev_err(&pdev
->dev
, "Unable to config gpio\n");
1057 sdd
->clk
= clk_get(&pdev
->dev
, "spi");
1058 if (IS_ERR(sdd
->clk
)) {
1059 dev_err(&pdev
->dev
, "Unable to acquire clock 'spi'\n");
1060 ret
= PTR_ERR(sdd
->clk
);
1064 if (clk_enable(sdd
->clk
)) {
1065 dev_err(&pdev
->dev
, "Couldn't enable clock 'spi'\n");
1070 sdd
->src_clk
= clk_get(&pdev
->dev
, sci
->src_clk_name
);
1071 if (IS_ERR(sdd
->src_clk
)) {
1073 "Unable to acquire clock '%s'\n", sci
->src_clk_name
);
1074 ret
= PTR_ERR(sdd
->src_clk
);
1078 if (clk_enable(sdd
->src_clk
)) {
1079 dev_err(&pdev
->dev
, "Couldn't enable clock '%s'\n",
1085 sdd
->workqueue
= create_singlethread_workqueue(
1086 dev_name(master
->dev
.parent
));
1087 if (sdd
->workqueue
== NULL
) {
1088 dev_err(&pdev
->dev
, "Unable to create workqueue\n");
1093 /* Setup Deufult Mode */
1094 s3c64xx_spi_hwinit(sdd
, pdev
->id
);
1096 spin_lock_init(&sdd
->lock
);
1097 init_completion(&sdd
->xfer_completion
);
1098 INIT_WORK(&sdd
->work
, s3c64xx_spi_work
);
1099 INIT_LIST_HEAD(&sdd
->queue
);
1101 if (spi_register_master(master
)) {
1102 dev_err(&pdev
->dev
, "cannot register SPI master\n");
1107 dev_dbg(&pdev
->dev
, "Samsung SoC SPI Driver loaded for Bus SPI-%d "
1108 "with %d Slaves attached\n",
1109 pdev
->id
, master
->num_chipselect
);
1110 dev_dbg(&pdev
->dev
, "\tIOmem=[0x%x-0x%x]\tDMA=[Rx-%d, Tx-%d]\n",
1111 mem_res
->end
, mem_res
->start
,
1112 sdd
->rx_dmach
, sdd
->tx_dmach
);
1117 destroy_workqueue(sdd
->workqueue
);
1119 clk_disable(sdd
->src_clk
);
1121 clk_put(sdd
->src_clk
);
1123 clk_disable(sdd
->clk
);
1128 iounmap((void *) sdd
->regs
);
1130 release_mem_region(mem_res
->start
, resource_size(mem_res
));
1132 platform_set_drvdata(pdev
, NULL
);
1133 spi_master_put(master
);
1138 static int s3c64xx_spi_remove(struct platform_device
*pdev
)
1140 struct spi_master
*master
= spi_master_get(platform_get_drvdata(pdev
));
1141 struct s3c64xx_spi_driver_data
*sdd
= spi_master_get_devdata(master
);
1142 struct resource
*mem_res
;
1143 unsigned long flags
;
1145 spin_lock_irqsave(&sdd
->lock
, flags
);
1146 sdd
->state
|= SUSPND
;
1147 spin_unlock_irqrestore(&sdd
->lock
, flags
);
1149 while (sdd
->state
& SPIBUSY
)
1152 spi_unregister_master(master
);
1154 destroy_workqueue(sdd
->workqueue
);
1156 clk_disable(sdd
->src_clk
);
1157 clk_put(sdd
->src_clk
);
1159 clk_disable(sdd
->clk
);
1162 iounmap((void *) sdd
->regs
);
1164 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1165 if (mem_res
!= NULL
)
1166 release_mem_region(mem_res
->start
, resource_size(mem_res
));
1168 platform_set_drvdata(pdev
, NULL
);
1169 spi_master_put(master
);
1175 static int s3c64xx_spi_suspend(struct platform_device
*pdev
, pm_message_t state
)
1177 struct spi_master
*master
= spi_master_get(platform_get_drvdata(pdev
));
1178 struct s3c64xx_spi_driver_data
*sdd
= spi_master_get_devdata(master
);
1179 unsigned long flags
;
1181 spin_lock_irqsave(&sdd
->lock
, flags
);
1182 sdd
->state
|= SUSPND
;
1183 spin_unlock_irqrestore(&sdd
->lock
, flags
);
1185 while (sdd
->state
& SPIBUSY
)
1188 /* Disable the clock */
1189 clk_disable(sdd
->src_clk
);
1190 clk_disable(sdd
->clk
);
1192 sdd
->cur_speed
= 0; /* Output Clock is stopped */
1197 static int s3c64xx_spi_resume(struct platform_device
*pdev
)
1199 struct spi_master
*master
= spi_master_get(platform_get_drvdata(pdev
));
1200 struct s3c64xx_spi_driver_data
*sdd
= spi_master_get_devdata(master
);
1201 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
1202 unsigned long flags
;
1204 sci
->cfg_gpio(pdev
);
1206 /* Enable the clock */
1207 clk_enable(sdd
->src_clk
);
1208 clk_enable(sdd
->clk
);
1210 s3c64xx_spi_hwinit(sdd
, pdev
->id
);
1212 spin_lock_irqsave(&sdd
->lock
, flags
);
1213 sdd
->state
&= ~SUSPND
;
1214 spin_unlock_irqrestore(&sdd
->lock
, flags
);
1219 #define s3c64xx_spi_suspend NULL
1220 #define s3c64xx_spi_resume NULL
1221 #endif /* CONFIG_PM */
1223 static struct platform_driver s3c64xx_spi_driver
= {
1225 .name
= "s3c64xx-spi",
1226 .owner
= THIS_MODULE
,
1228 .remove
= s3c64xx_spi_remove
,
1229 .suspend
= s3c64xx_spi_suspend
,
1230 .resume
= s3c64xx_spi_resume
,
1232 MODULE_ALIAS("platform:s3c64xx-spi");
1234 static int __init
s3c64xx_spi_init(void)
1236 return platform_driver_probe(&s3c64xx_spi_driver
, s3c64xx_spi_probe
);
1238 subsys_initcall(s3c64xx_spi_init
);
1240 static void __exit
s3c64xx_spi_exit(void)
1242 platform_driver_unregister(&s3c64xx_spi_driver
);
1244 module_exit(s3c64xx_spi_exit
);
1246 MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
1247 MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
1248 MODULE_LICENSE("GPL");