2 * Copyright (C) 2009 Samsung Electronics Ltd.
3 * Jaswinder Singh <jassi.brar@samsung.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/workqueue.h>
23 #include <linux/delay.h>
24 #include <linux/clk.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/platform_device.h>
27 #include <linux/spi/spi.h>
30 #include <plat/s3c64xx-spi.h>
32 /* Registers and bit-fields */
34 #define S3C64XX_SPI_CH_CFG 0x00
35 #define S3C64XX_SPI_CLK_CFG 0x04
36 #define S3C64XX_SPI_MODE_CFG 0x08
37 #define S3C64XX_SPI_SLAVE_SEL 0x0C
38 #define S3C64XX_SPI_INT_EN 0x10
39 #define S3C64XX_SPI_STATUS 0x14
40 #define S3C64XX_SPI_TX_DATA 0x18
41 #define S3C64XX_SPI_RX_DATA 0x1C
42 #define S3C64XX_SPI_PACKET_CNT 0x20
43 #define S3C64XX_SPI_PENDING_CLR 0x24
44 #define S3C64XX_SPI_SWAP_CFG 0x28
45 #define S3C64XX_SPI_FB_CLK 0x2C
47 #define S3C64XX_SPI_CH_HS_EN (1<<6) /* High Speed Enable */
48 #define S3C64XX_SPI_CH_SW_RST (1<<5)
49 #define S3C64XX_SPI_CH_SLAVE (1<<4)
50 #define S3C64XX_SPI_CPOL_L (1<<3)
51 #define S3C64XX_SPI_CPHA_B (1<<2)
52 #define S3C64XX_SPI_CH_RXCH_ON (1<<1)
53 #define S3C64XX_SPI_CH_TXCH_ON (1<<0)
55 #define S3C64XX_SPI_CLKSEL_SRCMSK (3<<9)
56 #define S3C64XX_SPI_CLKSEL_SRCSHFT 9
57 #define S3C64XX_SPI_ENCLK_ENABLE (1<<8)
58 #define S3C64XX_SPI_PSR_MASK 0xff
60 #define S3C64XX_SPI_MODE_CH_TSZ_BYTE (0<<29)
61 #define S3C64XX_SPI_MODE_CH_TSZ_HALFWORD (1<<29)
62 #define S3C64XX_SPI_MODE_CH_TSZ_WORD (2<<29)
63 #define S3C64XX_SPI_MODE_CH_TSZ_MASK (3<<29)
64 #define S3C64XX_SPI_MODE_BUS_TSZ_BYTE (0<<17)
65 #define S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD (1<<17)
66 #define S3C64XX_SPI_MODE_BUS_TSZ_WORD (2<<17)
67 #define S3C64XX_SPI_MODE_BUS_TSZ_MASK (3<<17)
68 #define S3C64XX_SPI_MODE_RXDMA_ON (1<<2)
69 #define S3C64XX_SPI_MODE_TXDMA_ON (1<<1)
70 #define S3C64XX_SPI_MODE_4BURST (1<<0)
72 #define S3C64XX_SPI_SLAVE_AUTO (1<<1)
73 #define S3C64XX_SPI_SLAVE_SIG_INACT (1<<0)
75 #define S3C64XX_SPI_ACT(c) writel(0, (c)->regs + S3C64XX_SPI_SLAVE_SEL)
77 #define S3C64XX_SPI_DEACT(c) writel(S3C64XX_SPI_SLAVE_SIG_INACT, \
78 (c)->regs + S3C64XX_SPI_SLAVE_SEL)
80 #define S3C64XX_SPI_INT_TRAILING_EN (1<<6)
81 #define S3C64XX_SPI_INT_RX_OVERRUN_EN (1<<5)
82 #define S3C64XX_SPI_INT_RX_UNDERRUN_EN (1<<4)
83 #define S3C64XX_SPI_INT_TX_OVERRUN_EN (1<<3)
84 #define S3C64XX_SPI_INT_TX_UNDERRUN_EN (1<<2)
85 #define S3C64XX_SPI_INT_RX_FIFORDY_EN (1<<1)
86 #define S3C64XX_SPI_INT_TX_FIFORDY_EN (1<<0)
88 #define S3C64XX_SPI_ST_RX_OVERRUN_ERR (1<<5)
89 #define S3C64XX_SPI_ST_RX_UNDERRUN_ERR (1<<4)
90 #define S3C64XX_SPI_ST_TX_OVERRUN_ERR (1<<3)
91 #define S3C64XX_SPI_ST_TX_UNDERRUN_ERR (1<<2)
92 #define S3C64XX_SPI_ST_RX_FIFORDY (1<<1)
93 #define S3C64XX_SPI_ST_TX_FIFORDY (1<<0)
95 #define S3C64XX_SPI_PACKET_CNT_EN (1<<16)
97 #define S3C64XX_SPI_PND_TX_UNDERRUN_CLR (1<<4)
98 #define S3C64XX_SPI_PND_TX_OVERRUN_CLR (1<<3)
99 #define S3C64XX_SPI_PND_RX_UNDERRUN_CLR (1<<2)
100 #define S3C64XX_SPI_PND_RX_OVERRUN_CLR (1<<1)
101 #define S3C64XX_SPI_PND_TRAILING_CLR (1<<0)
103 #define S3C64XX_SPI_SWAP_RX_HALF_WORD (1<<7)
104 #define S3C64XX_SPI_SWAP_RX_BYTE (1<<6)
105 #define S3C64XX_SPI_SWAP_RX_BIT (1<<5)
106 #define S3C64XX_SPI_SWAP_RX_EN (1<<4)
107 #define S3C64XX_SPI_SWAP_TX_HALF_WORD (1<<3)
108 #define S3C64XX_SPI_SWAP_TX_BYTE (1<<2)
109 #define S3C64XX_SPI_SWAP_TX_BIT (1<<1)
110 #define S3C64XX_SPI_SWAP_TX_EN (1<<0)
112 #define S3C64XX_SPI_FBCLK_MSK (3<<0)
114 #define S3C64XX_SPI_ST_TRLCNTZ(v, i) ((((v) >> (i)->rx_lvl_offset) & \
115 (((i)->fifo_lvl_mask + 1))) \
118 #define S3C64XX_SPI_ST_TX_DONE(v, i) (((v) & (1 << (i)->tx_st_done)) ? 1 : 0)
119 #define TX_FIFO_LVL(v, i) (((v) >> 6) & (i)->fifo_lvl_mask)
120 #define RX_FIFO_LVL(v, i) (((v) >> (i)->rx_lvl_offset) & (i)->fifo_lvl_mask)
122 #define S3C64XX_SPI_MAX_TRAILCNT 0x3ff
123 #define S3C64XX_SPI_TRAILCNT_OFF 19
125 #define S3C64XX_SPI_TRAILCNT S3C64XX_SPI_MAX_TRAILCNT
127 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
129 #define SUSPND (1<<0)
130 #define SPIBUSY (1<<1)
131 #define RXBUSY (1<<2)
132 #define TXBUSY (1<<3)
135 * struct s3c64xx_spi_driver_data - Runtime info holder for SPI driver.
136 * @clk: Pointer to the spi clock.
137 * @src_clk: Pointer to the clock used to generate SPI signals.
138 * @master: Pointer to the SPI Protocol master.
139 * @workqueue: Work queue for the SPI xfer requests.
140 * @cntrlr_info: Platform specific data for the controller this driver manages.
141 * @tgl_spi: Pointer to the last CS left untoggled by the cs_change hint.
143 * @queue: To log SPI xfer requests.
144 * @lock: Controller specific lock.
145 * @state: Set of FLAGS to indicate status.
146 * @rx_dmach: Controller's DMA channel for Rx.
147 * @tx_dmach: Controller's DMA channel for Tx.
148 * @sfr_start: BUS address of SPI controller regs.
149 * @regs: Pointer to ioremap'ed controller registers.
150 * @xfer_completion: To indicate completion of xfer task.
151 * @cur_mode: Stores the active configuration of the controller.
152 * @cur_bpw: Stores the active bits per word settings.
153 * @cur_speed: Stores the active xfer clock speed.
155 struct s3c64xx_spi_driver_data
{
159 struct platform_device
*pdev
;
160 struct spi_master
*master
;
161 struct workqueue_struct
*workqueue
;
162 struct s3c64xx_spi_info
*cntrlr_info
;
163 struct spi_device
*tgl_spi
;
164 struct work_struct work
;
165 struct list_head queue
;
167 enum dma_ch rx_dmach
;
168 enum dma_ch tx_dmach
;
169 unsigned long sfr_start
;
170 struct completion xfer_completion
;
172 unsigned cur_mode
, cur_bpw
;
176 static struct s3c2410_dma_client s3c64xx_spi_dma_client
= {
177 .name
= "samsung-spi-dma",
180 static void flush_fifo(struct s3c64xx_spi_driver_data
*sdd
)
182 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
183 void __iomem
*regs
= sdd
->regs
;
187 writel(0, regs
+ S3C64XX_SPI_PACKET_CNT
);
189 val
= readl(regs
+ S3C64XX_SPI_CH_CFG
);
190 val
|= S3C64XX_SPI_CH_SW_RST
;
191 val
&= ~S3C64XX_SPI_CH_HS_EN
;
192 writel(val
, regs
+ S3C64XX_SPI_CH_CFG
);
195 loops
= msecs_to_loops(1);
197 val
= readl(regs
+ S3C64XX_SPI_STATUS
);
198 } while (TX_FIFO_LVL(val
, sci
) && loops
--);
201 dev_warn(&sdd
->pdev
->dev
, "Timed out flushing TX FIFO\n");
204 loops
= msecs_to_loops(1);
206 val
= readl(regs
+ S3C64XX_SPI_STATUS
);
207 if (RX_FIFO_LVL(val
, sci
))
208 readl(regs
+ S3C64XX_SPI_RX_DATA
);
214 dev_warn(&sdd
->pdev
->dev
, "Timed out flushing RX FIFO\n");
216 val
= readl(regs
+ S3C64XX_SPI_CH_CFG
);
217 val
&= ~S3C64XX_SPI_CH_SW_RST
;
218 writel(val
, regs
+ S3C64XX_SPI_CH_CFG
);
220 val
= readl(regs
+ S3C64XX_SPI_MODE_CFG
);
221 val
&= ~(S3C64XX_SPI_MODE_TXDMA_ON
| S3C64XX_SPI_MODE_RXDMA_ON
);
222 writel(val
, regs
+ S3C64XX_SPI_MODE_CFG
);
224 val
= readl(regs
+ S3C64XX_SPI_CH_CFG
);
225 val
&= ~(S3C64XX_SPI_CH_RXCH_ON
| S3C64XX_SPI_CH_TXCH_ON
);
226 writel(val
, regs
+ S3C64XX_SPI_CH_CFG
);
229 static void enable_datapath(struct s3c64xx_spi_driver_data
*sdd
,
230 struct spi_device
*spi
,
231 struct spi_transfer
*xfer
, int dma_mode
)
233 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
234 void __iomem
*regs
= sdd
->regs
;
237 modecfg
= readl(regs
+ S3C64XX_SPI_MODE_CFG
);
238 modecfg
&= ~(S3C64XX_SPI_MODE_TXDMA_ON
| S3C64XX_SPI_MODE_RXDMA_ON
);
240 chcfg
= readl(regs
+ S3C64XX_SPI_CH_CFG
);
241 chcfg
&= ~S3C64XX_SPI_CH_TXCH_ON
;
244 chcfg
&= ~S3C64XX_SPI_CH_RXCH_ON
;
246 /* Always shift in data in FIFO, even if xfer is Tx only,
247 * this helps setting PCKT_CNT value for generating clocks
250 chcfg
|= S3C64XX_SPI_CH_RXCH_ON
;
251 writel(((xfer
->len
* 8 / sdd
->cur_bpw
) & 0xffff)
252 | S3C64XX_SPI_PACKET_CNT_EN
,
253 regs
+ S3C64XX_SPI_PACKET_CNT
);
256 if (xfer
->tx_buf
!= NULL
) {
257 sdd
->state
|= TXBUSY
;
258 chcfg
|= S3C64XX_SPI_CH_TXCH_ON
;
260 modecfg
|= S3C64XX_SPI_MODE_TXDMA_ON
;
261 s3c2410_dma_config(sdd
->tx_dmach
, sdd
->cur_bpw
/ 8);
262 s3c2410_dma_enqueue(sdd
->tx_dmach
, (void *)sdd
,
263 xfer
->tx_dma
, xfer
->len
);
264 s3c2410_dma_ctrl(sdd
->tx_dmach
, S3C2410_DMAOP_START
);
266 switch (sdd
->cur_bpw
) {
268 iowrite32_rep(regs
+ S3C64XX_SPI_TX_DATA
,
269 xfer
->tx_buf
, xfer
->len
/ 4);
272 iowrite16_rep(regs
+ S3C64XX_SPI_TX_DATA
,
273 xfer
->tx_buf
, xfer
->len
/ 2);
276 iowrite8_rep(regs
+ S3C64XX_SPI_TX_DATA
,
277 xfer
->tx_buf
, xfer
->len
);
283 if (xfer
->rx_buf
!= NULL
) {
284 sdd
->state
|= RXBUSY
;
286 if (sci
->high_speed
&& sdd
->cur_speed
>= 30000000UL
287 && !(sdd
->cur_mode
& SPI_CPHA
))
288 chcfg
|= S3C64XX_SPI_CH_HS_EN
;
291 modecfg
|= S3C64XX_SPI_MODE_RXDMA_ON
;
292 chcfg
|= S3C64XX_SPI_CH_RXCH_ON
;
293 writel(((xfer
->len
* 8 / sdd
->cur_bpw
) & 0xffff)
294 | S3C64XX_SPI_PACKET_CNT_EN
,
295 regs
+ S3C64XX_SPI_PACKET_CNT
);
296 s3c2410_dma_config(sdd
->rx_dmach
, sdd
->cur_bpw
/ 8);
297 s3c2410_dma_enqueue(sdd
->rx_dmach
, (void *)sdd
,
298 xfer
->rx_dma
, xfer
->len
);
299 s3c2410_dma_ctrl(sdd
->rx_dmach
, S3C2410_DMAOP_START
);
303 writel(modecfg
, regs
+ S3C64XX_SPI_MODE_CFG
);
304 writel(chcfg
, regs
+ S3C64XX_SPI_CH_CFG
);
307 static inline void enable_cs(struct s3c64xx_spi_driver_data
*sdd
,
308 struct spi_device
*spi
)
310 struct s3c64xx_spi_csinfo
*cs
;
312 if (sdd
->tgl_spi
!= NULL
) { /* If last device toggled after mssg */
313 if (sdd
->tgl_spi
!= spi
) { /* if last mssg on diff device */
314 /* Deselect the last toggled device */
315 cs
= sdd
->tgl_spi
->controller_data
;
316 cs
->set_level(cs
->line
,
317 spi
->mode
& SPI_CS_HIGH
? 0 : 1);
322 cs
= spi
->controller_data
;
323 cs
->set_level(cs
->line
, spi
->mode
& SPI_CS_HIGH
? 1 : 0);
326 static int wait_for_xfer(struct s3c64xx_spi_driver_data
*sdd
,
327 struct spi_transfer
*xfer
, int dma_mode
)
329 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
330 void __iomem
*regs
= sdd
->regs
;
334 /* millisecs to xfer 'len' bytes @ 'cur_speed' */
335 ms
= xfer
->len
* 8 * 1000 / sdd
->cur_speed
;
336 ms
+= 10; /* some tolerance */
339 val
= msecs_to_jiffies(ms
) + 10;
340 val
= wait_for_completion_timeout(&sdd
->xfer_completion
, val
);
343 val
= msecs_to_loops(ms
);
345 status
= readl(regs
+ S3C64XX_SPI_STATUS
);
346 } while (RX_FIFO_LVL(status
, sci
) < xfer
->len
&& --val
);
356 * DmaTx returns after simply writing data in the FIFO,
357 * w/o waiting for real transmission on the bus to finish.
358 * DmaRx returns only after Dma read data from FIFO which
359 * needs bus transmission to finish, so we don't worry if
360 * Xfer involved Rx(with or without Tx).
362 if (xfer
->rx_buf
== NULL
) {
363 val
= msecs_to_loops(10);
364 status
= readl(regs
+ S3C64XX_SPI_STATUS
);
365 while ((TX_FIFO_LVL(status
, sci
)
366 || !S3C64XX_SPI_ST_TX_DONE(status
, sci
))
369 status
= readl(regs
+ S3C64XX_SPI_STATUS
);
376 /* If it was only Tx */
377 if (xfer
->rx_buf
== NULL
) {
378 sdd
->state
&= ~TXBUSY
;
382 switch (sdd
->cur_bpw
) {
384 ioread32_rep(regs
+ S3C64XX_SPI_RX_DATA
,
385 xfer
->rx_buf
, xfer
->len
/ 4);
388 ioread16_rep(regs
+ S3C64XX_SPI_RX_DATA
,
389 xfer
->rx_buf
, xfer
->len
/ 2);
392 ioread8_rep(regs
+ S3C64XX_SPI_RX_DATA
,
393 xfer
->rx_buf
, xfer
->len
);
396 sdd
->state
&= ~RXBUSY
;
402 static inline void disable_cs(struct s3c64xx_spi_driver_data
*sdd
,
403 struct spi_device
*spi
)
405 struct s3c64xx_spi_csinfo
*cs
= spi
->controller_data
;
407 if (sdd
->tgl_spi
== spi
)
410 cs
->set_level(cs
->line
, spi
->mode
& SPI_CS_HIGH
? 0 : 1);
413 static void s3c64xx_spi_config(struct s3c64xx_spi_driver_data
*sdd
)
415 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
416 void __iomem
*regs
= sdd
->regs
;
420 if (sci
->clk_from_cmu
) {
421 clk_disable(sdd
->src_clk
);
423 val
= readl(regs
+ S3C64XX_SPI_CLK_CFG
);
424 val
&= ~S3C64XX_SPI_ENCLK_ENABLE
;
425 writel(val
, regs
+ S3C64XX_SPI_CLK_CFG
);
428 /* Set Polarity and Phase */
429 val
= readl(regs
+ S3C64XX_SPI_CH_CFG
);
430 val
&= ~(S3C64XX_SPI_CH_SLAVE
|
434 if (sdd
->cur_mode
& SPI_CPOL
)
435 val
|= S3C64XX_SPI_CPOL_L
;
437 if (sdd
->cur_mode
& SPI_CPHA
)
438 val
|= S3C64XX_SPI_CPHA_B
;
440 writel(val
, regs
+ S3C64XX_SPI_CH_CFG
);
442 /* Set Channel & DMA Mode */
443 val
= readl(regs
+ S3C64XX_SPI_MODE_CFG
);
444 val
&= ~(S3C64XX_SPI_MODE_BUS_TSZ_MASK
445 | S3C64XX_SPI_MODE_CH_TSZ_MASK
);
447 switch (sdd
->cur_bpw
) {
449 val
|= S3C64XX_SPI_MODE_BUS_TSZ_WORD
;
450 val
|= S3C64XX_SPI_MODE_CH_TSZ_WORD
;
453 val
|= S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD
;
454 val
|= S3C64XX_SPI_MODE_CH_TSZ_HALFWORD
;
457 val
|= S3C64XX_SPI_MODE_BUS_TSZ_BYTE
;
458 val
|= S3C64XX_SPI_MODE_CH_TSZ_BYTE
;
462 writel(val
, regs
+ S3C64XX_SPI_MODE_CFG
);
464 if (sci
->clk_from_cmu
) {
465 /* Configure Clock */
466 /* There is half-multiplier before the SPI */
467 clk_set_rate(sdd
->src_clk
, sdd
->cur_speed
* 2);
469 clk_enable(sdd
->src_clk
);
471 /* Configure Clock */
472 val
= readl(regs
+ S3C64XX_SPI_CLK_CFG
);
473 val
&= ~S3C64XX_SPI_PSR_MASK
;
474 val
|= ((clk_get_rate(sdd
->src_clk
) / sdd
->cur_speed
/ 2 - 1)
475 & S3C64XX_SPI_PSR_MASK
);
476 writel(val
, regs
+ S3C64XX_SPI_CLK_CFG
);
479 val
= readl(regs
+ S3C64XX_SPI_CLK_CFG
);
480 val
|= S3C64XX_SPI_ENCLK_ENABLE
;
481 writel(val
, regs
+ S3C64XX_SPI_CLK_CFG
);
485 static void s3c64xx_spi_dma_rxcb(struct s3c2410_dma_chan
*chan
, void *buf_id
,
486 int size
, enum s3c2410_dma_buffresult res
)
488 struct s3c64xx_spi_driver_data
*sdd
= buf_id
;
491 spin_lock_irqsave(&sdd
->lock
, flags
);
493 if (res
== S3C2410_RES_OK
)
494 sdd
->state
&= ~RXBUSY
;
496 dev_err(&sdd
->pdev
->dev
, "DmaAbrtRx-%d\n", size
);
498 /* If the other done */
499 if (!(sdd
->state
& TXBUSY
))
500 complete(&sdd
->xfer_completion
);
502 spin_unlock_irqrestore(&sdd
->lock
, flags
);
505 static void s3c64xx_spi_dma_txcb(struct s3c2410_dma_chan
*chan
, void *buf_id
,
506 int size
, enum s3c2410_dma_buffresult res
)
508 struct s3c64xx_spi_driver_data
*sdd
= buf_id
;
511 spin_lock_irqsave(&sdd
->lock
, flags
);
513 if (res
== S3C2410_RES_OK
)
514 sdd
->state
&= ~TXBUSY
;
516 dev_err(&sdd
->pdev
->dev
, "DmaAbrtTx-%d \n", size
);
518 /* If the other done */
519 if (!(sdd
->state
& RXBUSY
))
520 complete(&sdd
->xfer_completion
);
522 spin_unlock_irqrestore(&sdd
->lock
, flags
);
525 #define XFER_DMAADDR_INVALID DMA_BIT_MASK(32)
527 static int s3c64xx_spi_map_mssg(struct s3c64xx_spi_driver_data
*sdd
,
528 struct spi_message
*msg
)
530 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
531 struct device
*dev
= &sdd
->pdev
->dev
;
532 struct spi_transfer
*xfer
;
534 if (msg
->is_dma_mapped
)
537 /* First mark all xfer unmapped */
538 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
539 xfer
->rx_dma
= XFER_DMAADDR_INVALID
;
540 xfer
->tx_dma
= XFER_DMAADDR_INVALID
;
543 /* Map until end or first fail */
544 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
546 if (xfer
->len
<= ((sci
->fifo_lvl_mask
>> 1) + 1))
549 if (xfer
->tx_buf
!= NULL
) {
550 xfer
->tx_dma
= dma_map_single(dev
,
551 (void *)xfer
->tx_buf
, xfer
->len
,
553 if (dma_mapping_error(dev
, xfer
->tx_dma
)) {
554 dev_err(dev
, "dma_map_single Tx failed\n");
555 xfer
->tx_dma
= XFER_DMAADDR_INVALID
;
560 if (xfer
->rx_buf
!= NULL
) {
561 xfer
->rx_dma
= dma_map_single(dev
, xfer
->rx_buf
,
562 xfer
->len
, DMA_FROM_DEVICE
);
563 if (dma_mapping_error(dev
, xfer
->rx_dma
)) {
564 dev_err(dev
, "dma_map_single Rx failed\n");
565 dma_unmap_single(dev
, xfer
->tx_dma
,
566 xfer
->len
, DMA_TO_DEVICE
);
567 xfer
->tx_dma
= XFER_DMAADDR_INVALID
;
568 xfer
->rx_dma
= XFER_DMAADDR_INVALID
;
577 static void s3c64xx_spi_unmap_mssg(struct s3c64xx_spi_driver_data
*sdd
,
578 struct spi_message
*msg
)
580 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
581 struct device
*dev
= &sdd
->pdev
->dev
;
582 struct spi_transfer
*xfer
;
584 if (msg
->is_dma_mapped
)
587 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
589 if (xfer
->len
<= ((sci
->fifo_lvl_mask
>> 1) + 1))
592 if (xfer
->rx_buf
!= NULL
593 && xfer
->rx_dma
!= XFER_DMAADDR_INVALID
)
594 dma_unmap_single(dev
, xfer
->rx_dma
,
595 xfer
->len
, DMA_FROM_DEVICE
);
597 if (xfer
->tx_buf
!= NULL
598 && xfer
->tx_dma
!= XFER_DMAADDR_INVALID
)
599 dma_unmap_single(dev
, xfer
->tx_dma
,
600 xfer
->len
, DMA_TO_DEVICE
);
604 static void handle_msg(struct s3c64xx_spi_driver_data
*sdd
,
605 struct spi_message
*msg
)
607 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
608 struct spi_device
*spi
= msg
->spi
;
609 struct s3c64xx_spi_csinfo
*cs
= spi
->controller_data
;
610 struct spi_transfer
*xfer
;
611 int status
= 0, cs_toggle
= 0;
615 /* If Master's(controller) state differs from that needed by Slave */
616 if (sdd
->cur_speed
!= spi
->max_speed_hz
617 || sdd
->cur_mode
!= spi
->mode
618 || sdd
->cur_bpw
!= spi
->bits_per_word
) {
619 sdd
->cur_bpw
= spi
->bits_per_word
;
620 sdd
->cur_speed
= spi
->max_speed_hz
;
621 sdd
->cur_mode
= spi
->mode
;
622 s3c64xx_spi_config(sdd
);
625 /* Map all the transfers if needed */
626 if (s3c64xx_spi_map_mssg(sdd
, msg
)) {
628 "Xfer: Unable to map message buffers!\n");
633 /* Configure feedback delay */
634 writel(cs
->fb_delay
& 0x3, sdd
->regs
+ S3C64XX_SPI_FB_CLK
);
636 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
641 INIT_COMPLETION(sdd
->xfer_completion
);
643 /* Only BPW and Speed may change across transfers */
644 bpw
= xfer
->bits_per_word
? : spi
->bits_per_word
;
645 speed
= xfer
->speed_hz
? : spi
->max_speed_hz
;
647 if (xfer
->len
% (bpw
/ 8)) {
649 "Xfer length(%u) not a multiple of word size(%u)\n",
655 if (bpw
!= sdd
->cur_bpw
|| speed
!= sdd
->cur_speed
) {
657 sdd
->cur_speed
= speed
;
658 s3c64xx_spi_config(sdd
);
661 /* Polling method for xfers not bigger than FIFO capacity */
662 if (xfer
->len
<= ((sci
->fifo_lvl_mask
>> 1) + 1))
667 spin_lock_irqsave(&sdd
->lock
, flags
);
669 /* Pending only which is to be done */
670 sdd
->state
&= ~RXBUSY
;
671 sdd
->state
&= ~TXBUSY
;
673 enable_datapath(sdd
, spi
, xfer
, use_dma
);
678 /* Start the signals */
679 S3C64XX_SPI_ACT(sdd
);
681 spin_unlock_irqrestore(&sdd
->lock
, flags
);
683 status
= wait_for_xfer(sdd
, xfer
, use_dma
);
685 /* Quiese the signals */
686 S3C64XX_SPI_DEACT(sdd
);
689 dev_err(&spi
->dev
, "I/O Error: "
690 "rx-%d tx-%d res:rx-%c tx-%c len-%d\n",
691 xfer
->rx_buf
? 1 : 0, xfer
->tx_buf
? 1 : 0,
692 (sdd
->state
& RXBUSY
) ? 'f' : 'p',
693 (sdd
->state
& TXBUSY
) ? 'f' : 'p',
697 if (xfer
->tx_buf
!= NULL
698 && (sdd
->state
& TXBUSY
))
699 s3c2410_dma_ctrl(sdd
->tx_dmach
,
700 S3C2410_DMAOP_FLUSH
);
701 if (xfer
->rx_buf
!= NULL
702 && (sdd
->state
& RXBUSY
))
703 s3c2410_dma_ctrl(sdd
->rx_dmach
,
704 S3C2410_DMAOP_FLUSH
);
710 if (xfer
->delay_usecs
)
711 udelay(xfer
->delay_usecs
);
713 if (xfer
->cs_change
) {
714 /* Hint that the next mssg is gonna be
715 for the same device */
716 if (list_is_last(&xfer
->transfer_list
,
720 disable_cs(sdd
, spi
);
723 msg
->actual_length
+= xfer
->len
;
729 if (!cs_toggle
|| status
)
730 disable_cs(sdd
, spi
);
734 s3c64xx_spi_unmap_mssg(sdd
, msg
);
736 msg
->status
= status
;
739 msg
->complete(msg
->context
);
742 static int acquire_dma(struct s3c64xx_spi_driver_data
*sdd
)
744 if (s3c2410_dma_request(sdd
->rx_dmach
,
745 &s3c64xx_spi_dma_client
, NULL
) < 0) {
746 dev_err(&sdd
->pdev
->dev
, "cannot get RxDMA\n");
749 s3c2410_dma_set_buffdone_fn(sdd
->rx_dmach
, s3c64xx_spi_dma_rxcb
);
750 s3c2410_dma_devconfig(sdd
->rx_dmach
, S3C2410_DMASRC_HW
,
751 sdd
->sfr_start
+ S3C64XX_SPI_RX_DATA
);
753 if (s3c2410_dma_request(sdd
->tx_dmach
,
754 &s3c64xx_spi_dma_client
, NULL
) < 0) {
755 dev_err(&sdd
->pdev
->dev
, "cannot get TxDMA\n");
756 s3c2410_dma_free(sdd
->rx_dmach
, &s3c64xx_spi_dma_client
);
759 s3c2410_dma_set_buffdone_fn(sdd
->tx_dmach
, s3c64xx_spi_dma_txcb
);
760 s3c2410_dma_devconfig(sdd
->tx_dmach
, S3C2410_DMASRC_MEM
,
761 sdd
->sfr_start
+ S3C64XX_SPI_TX_DATA
);
766 static void s3c64xx_spi_work(struct work_struct
*work
)
768 struct s3c64xx_spi_driver_data
*sdd
= container_of(work
,
769 struct s3c64xx_spi_driver_data
, work
);
772 /* Acquire DMA channels */
773 while (!acquire_dma(sdd
))
776 spin_lock_irqsave(&sdd
->lock
, flags
);
778 while (!list_empty(&sdd
->queue
)
779 && !(sdd
->state
& SUSPND
)) {
781 struct spi_message
*msg
;
783 msg
= container_of(sdd
->queue
.next
, struct spi_message
, queue
);
785 list_del_init(&msg
->queue
);
787 /* Set Xfer busy flag */
788 sdd
->state
|= SPIBUSY
;
790 spin_unlock_irqrestore(&sdd
->lock
, flags
);
792 handle_msg(sdd
, msg
);
794 spin_lock_irqsave(&sdd
->lock
, flags
);
796 sdd
->state
&= ~SPIBUSY
;
799 spin_unlock_irqrestore(&sdd
->lock
, flags
);
801 /* Free DMA channels */
802 s3c2410_dma_free(sdd
->tx_dmach
, &s3c64xx_spi_dma_client
);
803 s3c2410_dma_free(sdd
->rx_dmach
, &s3c64xx_spi_dma_client
);
806 static int s3c64xx_spi_transfer(struct spi_device
*spi
,
807 struct spi_message
*msg
)
809 struct s3c64xx_spi_driver_data
*sdd
;
812 sdd
= spi_master_get_devdata(spi
->master
);
814 spin_lock_irqsave(&sdd
->lock
, flags
);
816 if (sdd
->state
& SUSPND
) {
817 spin_unlock_irqrestore(&sdd
->lock
, flags
);
821 msg
->status
= -EINPROGRESS
;
822 msg
->actual_length
= 0;
824 list_add_tail(&msg
->queue
, &sdd
->queue
);
826 queue_work(sdd
->workqueue
, &sdd
->work
);
828 spin_unlock_irqrestore(&sdd
->lock
, flags
);
834 * Here we only check the validity of requested configuration
835 * and save the configuration in a local data-structure.
836 * The controller is actually configured only just before we
837 * get a message to transfer.
839 static int s3c64xx_spi_setup(struct spi_device
*spi
)
841 struct s3c64xx_spi_csinfo
*cs
= spi
->controller_data
;
842 struct s3c64xx_spi_driver_data
*sdd
;
843 struct s3c64xx_spi_info
*sci
;
844 struct spi_message
*msg
;
848 if (cs
== NULL
|| cs
->set_level
== NULL
) {
849 dev_err(&spi
->dev
, "No CS for SPI(%d)\n", spi
->chip_select
);
853 sdd
= spi_master_get_devdata(spi
->master
);
854 sci
= sdd
->cntrlr_info
;
856 spin_lock_irqsave(&sdd
->lock
, flags
);
858 list_for_each_entry(msg
, &sdd
->queue
, queue
) {
859 /* Is some mssg is already queued for this device */
860 if (msg
->spi
== spi
) {
862 "setup: attempt while mssg in queue!\n");
863 spin_unlock_irqrestore(&sdd
->lock
, flags
);
868 if (sdd
->state
& SUSPND
) {
869 spin_unlock_irqrestore(&sdd
->lock
, flags
);
871 "setup: SPI-%d not active!\n", spi
->master
->bus_num
);
875 spin_unlock_irqrestore(&sdd
->lock
, flags
);
877 if (spi
->bits_per_word
!= 8
878 && spi
->bits_per_word
!= 16
879 && spi
->bits_per_word
!= 32) {
880 dev_err(&spi
->dev
, "setup: %dbits/wrd not supported!\n",
886 /* Check if we can provide the requested rate */
887 if (!sci
->clk_from_cmu
) {
891 speed
= clk_get_rate(sdd
->src_clk
) / 2 / (0 + 1);
893 if (spi
->max_speed_hz
> speed
)
894 spi
->max_speed_hz
= speed
;
896 psr
= clk_get_rate(sdd
->src_clk
) / 2 / spi
->max_speed_hz
- 1;
897 psr
&= S3C64XX_SPI_PSR_MASK
;
898 if (psr
== S3C64XX_SPI_PSR_MASK
)
901 speed
= clk_get_rate(sdd
->src_clk
) / 2 / (psr
+ 1);
902 if (spi
->max_speed_hz
< speed
) {
903 if (psr
+1 < S3C64XX_SPI_PSR_MASK
) {
911 speed
= clk_get_rate(sdd
->src_clk
) / 2 / (psr
+ 1);
912 if (spi
->max_speed_hz
>= speed
)
913 spi
->max_speed_hz
= speed
;
920 /* setup() returns with device de-selected */
921 disable_cs(sdd
, spi
);
926 static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data
*sdd
, int channel
)
928 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
929 void __iomem
*regs
= sdd
->regs
;
934 S3C64XX_SPI_DEACT(sdd
);
936 /* Disable Interrupts - we use Polling if not DMA mode */
937 writel(0, regs
+ S3C64XX_SPI_INT_EN
);
939 if (!sci
->clk_from_cmu
)
940 writel(sci
->src_clk_nr
<< S3C64XX_SPI_CLKSEL_SRCSHFT
,
941 regs
+ S3C64XX_SPI_CLK_CFG
);
942 writel(0, regs
+ S3C64XX_SPI_MODE_CFG
);
943 writel(0, regs
+ S3C64XX_SPI_PACKET_CNT
);
945 /* Clear any irq pending bits */
946 writel(readl(regs
+ S3C64XX_SPI_PENDING_CLR
),
947 regs
+ S3C64XX_SPI_PENDING_CLR
);
949 writel(0, regs
+ S3C64XX_SPI_SWAP_CFG
);
951 val
= readl(regs
+ S3C64XX_SPI_MODE_CFG
);
952 val
&= ~S3C64XX_SPI_MODE_4BURST
;
953 val
&= ~(S3C64XX_SPI_MAX_TRAILCNT
<< S3C64XX_SPI_TRAILCNT_OFF
);
954 val
|= (S3C64XX_SPI_TRAILCNT
<< S3C64XX_SPI_TRAILCNT_OFF
);
955 writel(val
, regs
+ S3C64XX_SPI_MODE_CFG
);
960 static int __init
s3c64xx_spi_probe(struct platform_device
*pdev
)
962 struct resource
*mem_res
, *dmatx_res
, *dmarx_res
;
963 struct s3c64xx_spi_driver_data
*sdd
;
964 struct s3c64xx_spi_info
*sci
;
965 struct spi_master
*master
;
970 "Invalid platform device id-%d\n", pdev
->id
);
974 if (pdev
->dev
.platform_data
== NULL
) {
975 dev_err(&pdev
->dev
, "platform_data missing!\n");
979 sci
= pdev
->dev
.platform_data
;
980 if (!sci
->src_clk_name
) {
982 "Board init must call s3c64xx_spi_set_info()\n");
986 /* Check for availability of necessary resource */
988 dmatx_res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
989 if (dmatx_res
== NULL
) {
990 dev_err(&pdev
->dev
, "Unable to get SPI-Tx dma resource\n");
994 dmarx_res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 1);
995 if (dmarx_res
== NULL
) {
996 dev_err(&pdev
->dev
, "Unable to get SPI-Rx dma resource\n");
1000 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1001 if (mem_res
== NULL
) {
1002 dev_err(&pdev
->dev
, "Unable to get SPI MEM resource\n");
1006 master
= spi_alloc_master(&pdev
->dev
,
1007 sizeof(struct s3c64xx_spi_driver_data
));
1008 if (master
== NULL
) {
1009 dev_err(&pdev
->dev
, "Unable to allocate SPI Master\n");
1013 platform_set_drvdata(pdev
, master
);
1015 sdd
= spi_master_get_devdata(master
);
1016 sdd
->master
= master
;
1017 sdd
->cntrlr_info
= sci
;
1019 sdd
->sfr_start
= mem_res
->start
;
1020 sdd
->tx_dmach
= dmatx_res
->start
;
1021 sdd
->rx_dmach
= dmarx_res
->start
;
1025 master
->bus_num
= pdev
->id
;
1026 master
->setup
= s3c64xx_spi_setup
;
1027 master
->transfer
= s3c64xx_spi_transfer
;
1028 master
->num_chipselect
= sci
->num_cs
;
1029 master
->dma_alignment
= 8;
1030 /* the spi->mode bits understood by this driver: */
1031 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
;
1033 if (request_mem_region(mem_res
->start
,
1034 resource_size(mem_res
), pdev
->name
) == NULL
) {
1035 dev_err(&pdev
->dev
, "Req mem region failed\n");
1040 sdd
->regs
= ioremap(mem_res
->start
, resource_size(mem_res
));
1041 if (sdd
->regs
== NULL
) {
1042 dev_err(&pdev
->dev
, "Unable to remap IO\n");
1047 if (sci
->cfg_gpio
== NULL
|| sci
->cfg_gpio(pdev
)) {
1048 dev_err(&pdev
->dev
, "Unable to config gpio\n");
1054 sdd
->clk
= clk_get(&pdev
->dev
, "spi");
1055 if (IS_ERR(sdd
->clk
)) {
1056 dev_err(&pdev
->dev
, "Unable to acquire clock 'spi'\n");
1057 ret
= PTR_ERR(sdd
->clk
);
1061 if (clk_enable(sdd
->clk
)) {
1062 dev_err(&pdev
->dev
, "Couldn't enable clock 'spi'\n");
1067 sdd
->src_clk
= clk_get(&pdev
->dev
, sci
->src_clk_name
);
1068 if (IS_ERR(sdd
->src_clk
)) {
1070 "Unable to acquire clock '%s'\n", sci
->src_clk_name
);
1071 ret
= PTR_ERR(sdd
->src_clk
);
1075 if (clk_enable(sdd
->src_clk
)) {
1076 dev_err(&pdev
->dev
, "Couldn't enable clock '%s'\n",
1082 sdd
->workqueue
= create_singlethread_workqueue(
1083 dev_name(master
->dev
.parent
));
1084 if (sdd
->workqueue
== NULL
) {
1085 dev_err(&pdev
->dev
, "Unable to create workqueue\n");
1090 /* Setup Deufult Mode */
1091 s3c64xx_spi_hwinit(sdd
, pdev
->id
);
1093 spin_lock_init(&sdd
->lock
);
1094 init_completion(&sdd
->xfer_completion
);
1095 INIT_WORK(&sdd
->work
, s3c64xx_spi_work
);
1096 INIT_LIST_HEAD(&sdd
->queue
);
1098 if (spi_register_master(master
)) {
1099 dev_err(&pdev
->dev
, "cannot register SPI master\n");
1104 dev_dbg(&pdev
->dev
, "Samsung SoC SPI Driver loaded for Bus SPI-%d "
1105 "with %d Slaves attached\n",
1106 pdev
->id
, master
->num_chipselect
);
1107 dev_dbg(&pdev
->dev
, "\tIOmem=[0x%x-0x%x]\tDMA=[Rx-%d, Tx-%d]\n",
1108 mem_res
->end
, mem_res
->start
,
1109 sdd
->rx_dmach
, sdd
->tx_dmach
);
1114 destroy_workqueue(sdd
->workqueue
);
1116 clk_disable(sdd
->src_clk
);
1118 clk_put(sdd
->src_clk
);
1120 clk_disable(sdd
->clk
);
1125 iounmap((void *) sdd
->regs
);
1127 release_mem_region(mem_res
->start
, resource_size(mem_res
));
1129 platform_set_drvdata(pdev
, NULL
);
1130 spi_master_put(master
);
1135 static int s3c64xx_spi_remove(struct platform_device
*pdev
)
1137 struct spi_master
*master
= spi_master_get(platform_get_drvdata(pdev
));
1138 struct s3c64xx_spi_driver_data
*sdd
= spi_master_get_devdata(master
);
1139 struct resource
*mem_res
;
1140 unsigned long flags
;
1142 spin_lock_irqsave(&sdd
->lock
, flags
);
1143 sdd
->state
|= SUSPND
;
1144 spin_unlock_irqrestore(&sdd
->lock
, flags
);
1146 while (sdd
->state
& SPIBUSY
)
1149 spi_unregister_master(master
);
1151 destroy_workqueue(sdd
->workqueue
);
1153 clk_disable(sdd
->src_clk
);
1154 clk_put(sdd
->src_clk
);
1156 clk_disable(sdd
->clk
);
1159 iounmap((void *) sdd
->regs
);
1161 mem_res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1162 if (mem_res
!= NULL
)
1163 release_mem_region(mem_res
->start
, resource_size(mem_res
));
1165 platform_set_drvdata(pdev
, NULL
);
1166 spi_master_put(master
);
1172 static int s3c64xx_spi_suspend(struct platform_device
*pdev
, pm_message_t state
)
1174 struct spi_master
*master
= spi_master_get(platform_get_drvdata(pdev
));
1175 struct s3c64xx_spi_driver_data
*sdd
= spi_master_get_devdata(master
);
1176 unsigned long flags
;
1178 spin_lock_irqsave(&sdd
->lock
, flags
);
1179 sdd
->state
|= SUSPND
;
1180 spin_unlock_irqrestore(&sdd
->lock
, flags
);
1182 while (sdd
->state
& SPIBUSY
)
1185 /* Disable the clock */
1186 clk_disable(sdd
->src_clk
);
1187 clk_disable(sdd
->clk
);
1189 sdd
->cur_speed
= 0; /* Output Clock is stopped */
1194 static int s3c64xx_spi_resume(struct platform_device
*pdev
)
1196 struct spi_master
*master
= spi_master_get(platform_get_drvdata(pdev
));
1197 struct s3c64xx_spi_driver_data
*sdd
= spi_master_get_devdata(master
);
1198 struct s3c64xx_spi_info
*sci
= sdd
->cntrlr_info
;
1199 unsigned long flags
;
1201 sci
->cfg_gpio(pdev
);
1203 /* Enable the clock */
1204 clk_enable(sdd
->src_clk
);
1205 clk_enable(sdd
->clk
);
1207 s3c64xx_spi_hwinit(sdd
, pdev
->id
);
1209 spin_lock_irqsave(&sdd
->lock
, flags
);
1210 sdd
->state
&= ~SUSPND
;
1211 spin_unlock_irqrestore(&sdd
->lock
, flags
);
1216 #define s3c64xx_spi_suspend NULL
1217 #define s3c64xx_spi_resume NULL
1218 #endif /* CONFIG_PM */
1220 static struct platform_driver s3c64xx_spi_driver
= {
1222 .name
= "s3c64xx-spi",
1223 .owner
= THIS_MODULE
,
1225 .remove
= s3c64xx_spi_remove
,
1226 .suspend
= s3c64xx_spi_suspend
,
1227 .resume
= s3c64xx_spi_resume
,
1229 MODULE_ALIAS("platform:s3c64xx-spi");
1231 static int __init
s3c64xx_spi_init(void)
1233 return platform_driver_probe(&s3c64xx_spi_driver
, s3c64xx_spi_probe
);
1235 subsys_initcall(s3c64xx_spi_init
);
1237 static void __exit
s3c64xx_spi_exit(void)
1239 platform_driver_unregister(&s3c64xx_spi_driver
);
1241 module_exit(s3c64xx_spi_exit
);
1243 MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
1244 MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
1245 MODULE_LICENSE("GPL");