blob: 3c36cfaa1b9309aa5e3eff32d9b613b6e6cda4fd [file] [log] [blame]
Grant Likelyca632f52011-06-06 01:16:30 -06001/*
Jassi Brar230d42d2009-11-30 07:39:42 +00002 * Copyright (C) 2009 Samsung Electronics Ltd.
3 * Jaswinder Singh <jassi.brar@samsung.com>
4 *
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.
9 *
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.
14 *
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.
18 */
19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/workqueue.h>
Mark Brownc2573122011-11-10 10:57:32 +000023#include <linux/interrupt.h>
Jassi Brar230d42d2009-11-30 07:39:42 +000024#include <linux/delay.h>
25#include <linux/clk.h>
26#include <linux/dma-mapping.h>
27#include <linux/platform_device.h>
Mark Brownb97b6622011-12-04 00:58:06 +000028#include <linux/pm_runtime.h>
Jassi Brar230d42d2009-11-30 07:39:42 +000029#include <linux/spi/spi.h>
30
31#include <mach/dma.h>
Jassi Brare6b873c2010-01-20 13:49:45 -070032#include <plat/s3c64xx-spi.h>
Jassi Brar230d42d2009-11-30 07:39:42 +000033
34/* Registers and bit-fields */
35
36#define S3C64XX_SPI_CH_CFG 0x00
37#define S3C64XX_SPI_CLK_CFG 0x04
38#define S3C64XX_SPI_MODE_CFG 0x08
39#define S3C64XX_SPI_SLAVE_SEL 0x0C
40#define S3C64XX_SPI_INT_EN 0x10
41#define S3C64XX_SPI_STATUS 0x14
42#define S3C64XX_SPI_TX_DATA 0x18
43#define S3C64XX_SPI_RX_DATA 0x1C
44#define S3C64XX_SPI_PACKET_CNT 0x20
45#define S3C64XX_SPI_PENDING_CLR 0x24
46#define S3C64XX_SPI_SWAP_CFG 0x28
47#define S3C64XX_SPI_FB_CLK 0x2C
48
49#define S3C64XX_SPI_CH_HS_EN (1<<6) /* High Speed Enable */
50#define S3C64XX_SPI_CH_SW_RST (1<<5)
51#define S3C64XX_SPI_CH_SLAVE (1<<4)
52#define S3C64XX_SPI_CPOL_L (1<<3)
53#define S3C64XX_SPI_CPHA_B (1<<2)
54#define S3C64XX_SPI_CH_RXCH_ON (1<<1)
55#define S3C64XX_SPI_CH_TXCH_ON (1<<0)
56
57#define S3C64XX_SPI_CLKSEL_SRCMSK (3<<9)
58#define S3C64XX_SPI_CLKSEL_SRCSHFT 9
59#define S3C64XX_SPI_ENCLK_ENABLE (1<<8)
60#define S3C64XX_SPI_PSR_MASK 0xff
61
62#define S3C64XX_SPI_MODE_CH_TSZ_BYTE (0<<29)
63#define S3C64XX_SPI_MODE_CH_TSZ_HALFWORD (1<<29)
64#define S3C64XX_SPI_MODE_CH_TSZ_WORD (2<<29)
65#define S3C64XX_SPI_MODE_CH_TSZ_MASK (3<<29)
66#define S3C64XX_SPI_MODE_BUS_TSZ_BYTE (0<<17)
67#define S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD (1<<17)
68#define S3C64XX_SPI_MODE_BUS_TSZ_WORD (2<<17)
69#define S3C64XX_SPI_MODE_BUS_TSZ_MASK (3<<17)
70#define S3C64XX_SPI_MODE_RXDMA_ON (1<<2)
71#define S3C64XX_SPI_MODE_TXDMA_ON (1<<1)
72#define S3C64XX_SPI_MODE_4BURST (1<<0)
73
74#define S3C64XX_SPI_SLAVE_AUTO (1<<1)
75#define S3C64XX_SPI_SLAVE_SIG_INACT (1<<0)
76
77#define S3C64XX_SPI_ACT(c) writel(0, (c)->regs + S3C64XX_SPI_SLAVE_SEL)
78
79#define S3C64XX_SPI_DEACT(c) writel(S3C64XX_SPI_SLAVE_SIG_INACT, \
80 (c)->regs + S3C64XX_SPI_SLAVE_SEL)
81
82#define S3C64XX_SPI_INT_TRAILING_EN (1<<6)
83#define S3C64XX_SPI_INT_RX_OVERRUN_EN (1<<5)
84#define S3C64XX_SPI_INT_RX_UNDERRUN_EN (1<<4)
85#define S3C64XX_SPI_INT_TX_OVERRUN_EN (1<<3)
86#define S3C64XX_SPI_INT_TX_UNDERRUN_EN (1<<2)
87#define S3C64XX_SPI_INT_RX_FIFORDY_EN (1<<1)
88#define S3C64XX_SPI_INT_TX_FIFORDY_EN (1<<0)
89
90#define S3C64XX_SPI_ST_RX_OVERRUN_ERR (1<<5)
91#define S3C64XX_SPI_ST_RX_UNDERRUN_ERR (1<<4)
92#define S3C64XX_SPI_ST_TX_OVERRUN_ERR (1<<3)
93#define S3C64XX_SPI_ST_TX_UNDERRUN_ERR (1<<2)
94#define S3C64XX_SPI_ST_RX_FIFORDY (1<<1)
95#define S3C64XX_SPI_ST_TX_FIFORDY (1<<0)
96
97#define S3C64XX_SPI_PACKET_CNT_EN (1<<16)
98
99#define S3C64XX_SPI_PND_TX_UNDERRUN_CLR (1<<4)
100#define S3C64XX_SPI_PND_TX_OVERRUN_CLR (1<<3)
101#define S3C64XX_SPI_PND_RX_UNDERRUN_CLR (1<<2)
102#define S3C64XX_SPI_PND_RX_OVERRUN_CLR (1<<1)
103#define S3C64XX_SPI_PND_TRAILING_CLR (1<<0)
104
105#define S3C64XX_SPI_SWAP_RX_HALF_WORD (1<<7)
106#define S3C64XX_SPI_SWAP_RX_BYTE (1<<6)
107#define S3C64XX_SPI_SWAP_RX_BIT (1<<5)
108#define S3C64XX_SPI_SWAP_RX_EN (1<<4)
109#define S3C64XX_SPI_SWAP_TX_HALF_WORD (1<<3)
110#define S3C64XX_SPI_SWAP_TX_BYTE (1<<2)
111#define S3C64XX_SPI_SWAP_TX_BIT (1<<1)
112#define S3C64XX_SPI_SWAP_TX_EN (1<<0)
113
114#define S3C64XX_SPI_FBCLK_MSK (3<<0)
115
116#define S3C64XX_SPI_ST_TRLCNTZ(v, i) ((((v) >> (i)->rx_lvl_offset) & \
117 (((i)->fifo_lvl_mask + 1))) \
118 ? 1 : 0)
119
Padmavathi Venna30757412011-07-05 17:14:02 +0900120#define S3C64XX_SPI_ST_TX_DONE(v, i) (((v) & (1 << (i)->tx_st_done)) ? 1 : 0)
Jassi Brar230d42d2009-11-30 07:39:42 +0000121#define TX_FIFO_LVL(v, i) (((v) >> 6) & (i)->fifo_lvl_mask)
122#define RX_FIFO_LVL(v, i) (((v) >> (i)->rx_lvl_offset) & (i)->fifo_lvl_mask)
123
124#define S3C64XX_SPI_MAX_TRAILCNT 0x3ff
125#define S3C64XX_SPI_TRAILCNT_OFF 19
126
127#define S3C64XX_SPI_TRAILCNT S3C64XX_SPI_MAX_TRAILCNT
128
129#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
130
Jassi Brar230d42d2009-11-30 07:39:42 +0000131#define RXBUSY (1<<2)
132#define TXBUSY (1<<3)
133
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900134struct s3c64xx_spi_dma_data {
135 unsigned ch;
136 enum dma_data_direction direction;
137 enum dma_ch dmach;
138};
139
Jassi Brar230d42d2009-11-30 07:39:42 +0000140/**
141 * struct s3c64xx_spi_driver_data - Runtime info holder for SPI driver.
142 * @clk: Pointer to the spi clock.
Jassi Brarb0d5d6e2010-01-20 13:49:44 -0700143 * @src_clk: Pointer to the clock used to generate SPI signals.
Jassi Brar230d42d2009-11-30 07:39:42 +0000144 * @master: Pointer to the SPI Protocol master.
Jassi Brar230d42d2009-11-30 07:39:42 +0000145 * @cntrlr_info: Platform specific data for the controller this driver manages.
146 * @tgl_spi: Pointer to the last CS left untoggled by the cs_change hint.
Jassi Brar230d42d2009-11-30 07:39:42 +0000147 * @queue: To log SPI xfer requests.
148 * @lock: Controller specific lock.
149 * @state: Set of FLAGS to indicate status.
150 * @rx_dmach: Controller's DMA channel for Rx.
151 * @tx_dmach: Controller's DMA channel for Tx.
152 * @sfr_start: BUS address of SPI controller regs.
153 * @regs: Pointer to ioremap'ed controller registers.
Mark Brownc2573122011-11-10 10:57:32 +0000154 * @irq: interrupt
Jassi Brar230d42d2009-11-30 07:39:42 +0000155 * @xfer_completion: To indicate completion of xfer task.
156 * @cur_mode: Stores the active configuration of the controller.
157 * @cur_bpw: Stores the active bits per word settings.
158 * @cur_speed: Stores the active xfer clock speed.
159 */
160struct s3c64xx_spi_driver_data {
161 void __iomem *regs;
162 struct clk *clk;
Jassi Brarb0d5d6e2010-01-20 13:49:44 -0700163 struct clk *src_clk;
Jassi Brar230d42d2009-11-30 07:39:42 +0000164 struct platform_device *pdev;
165 struct spi_master *master;
Jassi Brarad7de722010-01-20 13:49:44 -0700166 struct s3c64xx_spi_info *cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +0000167 struct spi_device *tgl_spi;
Jassi Brar230d42d2009-11-30 07:39:42 +0000168 struct list_head queue;
169 spinlock_t lock;
Jassi Brar230d42d2009-11-30 07:39:42 +0000170 unsigned long sfr_start;
171 struct completion xfer_completion;
172 unsigned state;
173 unsigned cur_mode, cur_bpw;
174 unsigned cur_speed;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900175 struct s3c64xx_spi_dma_data rx_dma;
176 struct s3c64xx_spi_dma_data tx_dma;
Boojin Kim39d3e802011-09-02 09:44:41 +0900177 struct samsung_dma_ops *ops;
Jassi Brar230d42d2009-11-30 07:39:42 +0000178};
179
180static struct s3c2410_dma_client s3c64xx_spi_dma_client = {
181 .name = "samsung-spi-dma",
182};
183
184static void flush_fifo(struct s3c64xx_spi_driver_data *sdd)
185{
Jassi Brarad7de722010-01-20 13:49:44 -0700186 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +0000187 void __iomem *regs = sdd->regs;
188 unsigned long loops;
189 u32 val;
190
191 writel(0, regs + S3C64XX_SPI_PACKET_CNT);
192
193 val = readl(regs + S3C64XX_SPI_CH_CFG);
194 val |= S3C64XX_SPI_CH_SW_RST;
195 val &= ~S3C64XX_SPI_CH_HS_EN;
196 writel(val, regs + S3C64XX_SPI_CH_CFG);
197
198 /* Flush TxFIFO*/
199 loops = msecs_to_loops(1);
200 do {
201 val = readl(regs + S3C64XX_SPI_STATUS);
202 } while (TX_FIFO_LVL(val, sci) && loops--);
203
Mark Brownbe7852a2010-08-23 17:40:56 +0100204 if (loops == 0)
205 dev_warn(&sdd->pdev->dev, "Timed out flushing TX FIFO\n");
206
Jassi Brar230d42d2009-11-30 07:39:42 +0000207 /* Flush RxFIFO*/
208 loops = msecs_to_loops(1);
209 do {
210 val = readl(regs + S3C64XX_SPI_STATUS);
211 if (RX_FIFO_LVL(val, sci))
212 readl(regs + S3C64XX_SPI_RX_DATA);
213 else
214 break;
215 } while (loops--);
216
Mark Brownbe7852a2010-08-23 17:40:56 +0100217 if (loops == 0)
218 dev_warn(&sdd->pdev->dev, "Timed out flushing RX FIFO\n");
219
Jassi Brar230d42d2009-11-30 07:39:42 +0000220 val = readl(regs + S3C64XX_SPI_CH_CFG);
221 val &= ~S3C64XX_SPI_CH_SW_RST;
222 writel(val, regs + S3C64XX_SPI_CH_CFG);
223
224 val = readl(regs + S3C64XX_SPI_MODE_CFG);
225 val &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
226 writel(val, regs + S3C64XX_SPI_MODE_CFG);
227
228 val = readl(regs + S3C64XX_SPI_CH_CFG);
229 val &= ~(S3C64XX_SPI_CH_RXCH_ON | S3C64XX_SPI_CH_TXCH_ON);
230 writel(val, regs + S3C64XX_SPI_CH_CFG);
231}
232
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900233static void s3c64xx_spi_dmacb(void *data)
Boojin Kim39d3e802011-09-02 09:44:41 +0900234{
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900235 struct s3c64xx_spi_driver_data *sdd;
236 struct s3c64xx_spi_dma_data *dma = data;
Boojin Kim39d3e802011-09-02 09:44:41 +0900237 unsigned long flags;
238
Kyoungil Kim054ebcc2012-03-10 09:48:46 +0900239 if (dma->direction == DMA_DEV_TO_MEM)
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900240 sdd = container_of(data,
241 struct s3c64xx_spi_driver_data, rx_dma);
242 else
243 sdd = container_of(data,
244 struct s3c64xx_spi_driver_data, tx_dma);
245
Boojin Kim39d3e802011-09-02 09:44:41 +0900246 spin_lock_irqsave(&sdd->lock, flags);
247
Kyoungil Kim054ebcc2012-03-10 09:48:46 +0900248 if (dma->direction == DMA_DEV_TO_MEM) {
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900249 sdd->state &= ~RXBUSY;
250 if (!(sdd->state & TXBUSY))
251 complete(&sdd->xfer_completion);
252 } else {
253 sdd->state &= ~TXBUSY;
254 if (!(sdd->state & RXBUSY))
255 complete(&sdd->xfer_completion);
256 }
Boojin Kim39d3e802011-09-02 09:44:41 +0900257
258 spin_unlock_irqrestore(&sdd->lock, flags);
259}
260
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900261static void prepare_dma(struct s3c64xx_spi_dma_data *dma,
262 unsigned len, dma_addr_t buf)
Boojin Kim39d3e802011-09-02 09:44:41 +0900263{
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900264 struct s3c64xx_spi_driver_data *sdd;
Boojin Kim4969c322012-06-19 13:27:03 +0900265 struct samsung_dma_prep info;
266 struct samsung_dma_config config;
Boojin Kim39d3e802011-09-02 09:44:41 +0900267
Boojin Kim4969c322012-06-19 13:27:03 +0900268 if (dma->direction == DMA_DEV_TO_MEM) {
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900269 sdd = container_of((void *)dma,
270 struct s3c64xx_spi_driver_data, rx_dma);
Boojin Kim4969c322012-06-19 13:27:03 +0900271 config.direction = sdd->rx_dma.direction;
272 config.fifo = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
273 config.width = sdd->cur_bpw / 8;
274 sdd->ops->config(sdd->rx_dma.ch, &config);
275 } else {
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900276 sdd = container_of((void *)dma,
277 struct s3c64xx_spi_driver_data, tx_dma);
Boojin Kim4969c322012-06-19 13:27:03 +0900278 config.direction = sdd->tx_dma.direction;
279 config.fifo = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
280 config.width = sdd->cur_bpw / 8;
281 sdd->ops->config(sdd->tx_dma.ch, &config);
282 }
Boojin Kim39d3e802011-09-02 09:44:41 +0900283
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900284 info.cap = DMA_SLAVE;
285 info.len = len;
286 info.fp = s3c64xx_spi_dmacb;
287 info.fp_param = dma;
288 info.direction = dma->direction;
289 info.buf = buf;
Boojin Kim39d3e802011-09-02 09:44:41 +0900290
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900291 sdd->ops->prepare(dma->ch, &info);
292 sdd->ops->trigger(dma->ch);
293}
294
295static int acquire_dma(struct s3c64xx_spi_driver_data *sdd)
296{
Boojin Kim4969c322012-06-19 13:27:03 +0900297 struct samsung_dma_req req;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900298
299 sdd->ops = samsung_dma_get_ops();
300
Boojin Kim4969c322012-06-19 13:27:03 +0900301 req.cap = DMA_SLAVE;
302 req.client = &s3c64xx_spi_dma_client;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900303
Boojin Kim4969c322012-06-19 13:27:03 +0900304 sdd->rx_dma.ch = sdd->ops->request(sdd->rx_dma.dmach, &req);
305 sdd->tx_dma.ch = sdd->ops->request(sdd->tx_dma.dmach, &req);
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900306
307 return 1;
Boojin Kim39d3e802011-09-02 09:44:41 +0900308}
309
Jassi Brar230d42d2009-11-30 07:39:42 +0000310static void enable_datapath(struct s3c64xx_spi_driver_data *sdd,
311 struct spi_device *spi,
312 struct spi_transfer *xfer, int dma_mode)
313{
Jassi Brarad7de722010-01-20 13:49:44 -0700314 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +0000315 void __iomem *regs = sdd->regs;
316 u32 modecfg, chcfg;
317
318 modecfg = readl(regs + S3C64XX_SPI_MODE_CFG);
319 modecfg &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
320
321 chcfg = readl(regs + S3C64XX_SPI_CH_CFG);
322 chcfg &= ~S3C64XX_SPI_CH_TXCH_ON;
323
324 if (dma_mode) {
325 chcfg &= ~S3C64XX_SPI_CH_RXCH_ON;
326 } else {
327 /* Always shift in data in FIFO, even if xfer is Tx only,
328 * this helps setting PCKT_CNT value for generating clocks
329 * as exactly needed.
330 */
331 chcfg |= S3C64XX_SPI_CH_RXCH_ON;
332 writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
333 | S3C64XX_SPI_PACKET_CNT_EN,
334 regs + S3C64XX_SPI_PACKET_CNT);
335 }
336
337 if (xfer->tx_buf != NULL) {
338 sdd->state |= TXBUSY;
339 chcfg |= S3C64XX_SPI_CH_TXCH_ON;
340 if (dma_mode) {
341 modecfg |= S3C64XX_SPI_MODE_TXDMA_ON;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900342 prepare_dma(&sdd->tx_dma, xfer->len, xfer->tx_dma);
Jassi Brar230d42d2009-11-30 07:39:42 +0000343 } else {
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900344 switch (sdd->cur_bpw) {
345 case 32:
346 iowrite32_rep(regs + S3C64XX_SPI_TX_DATA,
347 xfer->tx_buf, xfer->len / 4);
348 break;
349 case 16:
350 iowrite16_rep(regs + S3C64XX_SPI_TX_DATA,
351 xfer->tx_buf, xfer->len / 2);
352 break;
353 default:
354 iowrite8_rep(regs + S3C64XX_SPI_TX_DATA,
355 xfer->tx_buf, xfer->len);
356 break;
357 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000358 }
359 }
360
361 if (xfer->rx_buf != NULL) {
362 sdd->state |= RXBUSY;
363
364 if (sci->high_speed && sdd->cur_speed >= 30000000UL
365 && !(sdd->cur_mode & SPI_CPHA))
366 chcfg |= S3C64XX_SPI_CH_HS_EN;
367
368 if (dma_mode) {
369 modecfg |= S3C64XX_SPI_MODE_RXDMA_ON;
370 chcfg |= S3C64XX_SPI_CH_RXCH_ON;
371 writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
372 | S3C64XX_SPI_PACKET_CNT_EN,
373 regs + S3C64XX_SPI_PACKET_CNT);
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900374 prepare_dma(&sdd->rx_dma, xfer->len, xfer->rx_dma);
Jassi Brar230d42d2009-11-30 07:39:42 +0000375 }
376 }
377
378 writel(modecfg, regs + S3C64XX_SPI_MODE_CFG);
379 writel(chcfg, regs + S3C64XX_SPI_CH_CFG);
380}
381
382static inline void enable_cs(struct s3c64xx_spi_driver_data *sdd,
383 struct spi_device *spi)
384{
385 struct s3c64xx_spi_csinfo *cs;
386
387 if (sdd->tgl_spi != NULL) { /* If last device toggled after mssg */
388 if (sdd->tgl_spi != spi) { /* if last mssg on diff device */
389 /* Deselect the last toggled device */
390 cs = sdd->tgl_spi->controller_data;
Jassi Brarfa0fcde2010-01-20 13:49:45 -0700391 cs->set_level(cs->line,
392 spi->mode & SPI_CS_HIGH ? 0 : 1);
Jassi Brar230d42d2009-11-30 07:39:42 +0000393 }
394 sdd->tgl_spi = NULL;
395 }
396
397 cs = spi->controller_data;
Jassi Brarfa0fcde2010-01-20 13:49:45 -0700398 cs->set_level(cs->line, spi->mode & SPI_CS_HIGH ? 1 : 0);
Jassi Brar230d42d2009-11-30 07:39:42 +0000399}
400
401static int wait_for_xfer(struct s3c64xx_spi_driver_data *sdd,
402 struct spi_transfer *xfer, int dma_mode)
403{
Jassi Brarad7de722010-01-20 13:49:44 -0700404 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +0000405 void __iomem *regs = sdd->regs;
406 unsigned long val;
407 int ms;
408
409 /* millisecs to xfer 'len' bytes @ 'cur_speed' */
410 ms = xfer->len * 8 * 1000 / sdd->cur_speed;
Mark Brown9d8f86b2010-09-07 16:37:52 +0100411 ms += 10; /* some tolerance */
Jassi Brar230d42d2009-11-30 07:39:42 +0000412
413 if (dma_mode) {
414 val = msecs_to_jiffies(ms) + 10;
415 val = wait_for_completion_timeout(&sdd->xfer_completion, val);
416 } else {
Jassi Brarc3f139b2010-09-03 10:36:46 +0900417 u32 status;
Jassi Brar230d42d2009-11-30 07:39:42 +0000418 val = msecs_to_loops(ms);
419 do {
Jassi Brarc3f139b2010-09-03 10:36:46 +0900420 status = readl(regs + S3C64XX_SPI_STATUS);
421 } while (RX_FIFO_LVL(status, sci) < xfer->len && --val);
Jassi Brar230d42d2009-11-30 07:39:42 +0000422 }
423
424 if (!val)
425 return -EIO;
426
427 if (dma_mode) {
428 u32 status;
429
430 /*
431 * DmaTx returns after simply writing data in the FIFO,
432 * w/o waiting for real transmission on the bus to finish.
433 * DmaRx returns only after Dma read data from FIFO which
434 * needs bus transmission to finish, so we don't worry if
435 * Xfer involved Rx(with or without Tx).
436 */
437 if (xfer->rx_buf == NULL) {
438 val = msecs_to_loops(10);
439 status = readl(regs + S3C64XX_SPI_STATUS);
440 while ((TX_FIFO_LVL(status, sci)
441 || !S3C64XX_SPI_ST_TX_DONE(status, sci))
442 && --val) {
443 cpu_relax();
444 status = readl(regs + S3C64XX_SPI_STATUS);
445 }
446
447 if (!val)
448 return -EIO;
449 }
450 } else {
Jassi Brar230d42d2009-11-30 07:39:42 +0000451 /* If it was only Tx */
452 if (xfer->rx_buf == NULL) {
453 sdd->state &= ~TXBUSY;
454 return 0;
455 }
456
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900457 switch (sdd->cur_bpw) {
458 case 32:
459 ioread32_rep(regs + S3C64XX_SPI_RX_DATA,
460 xfer->rx_buf, xfer->len / 4);
461 break;
462 case 16:
463 ioread16_rep(regs + S3C64XX_SPI_RX_DATA,
464 xfer->rx_buf, xfer->len / 2);
465 break;
466 default:
467 ioread8_rep(regs + S3C64XX_SPI_RX_DATA,
468 xfer->rx_buf, xfer->len);
469 break;
470 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000471 sdd->state &= ~RXBUSY;
472 }
473
474 return 0;
475}
476
477static inline void disable_cs(struct s3c64xx_spi_driver_data *sdd,
478 struct spi_device *spi)
479{
480 struct s3c64xx_spi_csinfo *cs = spi->controller_data;
481
482 if (sdd->tgl_spi == spi)
483 sdd->tgl_spi = NULL;
484
Jassi Brarfa0fcde2010-01-20 13:49:45 -0700485 cs->set_level(cs->line, spi->mode & SPI_CS_HIGH ? 0 : 1);
Jassi Brar230d42d2009-11-30 07:39:42 +0000486}
487
488static void s3c64xx_spi_config(struct s3c64xx_spi_driver_data *sdd)
489{
Jassi Brarb42a81c2010-09-29 17:31:33 +0900490 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +0000491 void __iomem *regs = sdd->regs;
492 u32 val;
493
494 /* Disable Clock */
Jassi Brarb42a81c2010-09-29 17:31:33 +0900495 if (sci->clk_from_cmu) {
496 clk_disable(sdd->src_clk);
497 } else {
498 val = readl(regs + S3C64XX_SPI_CLK_CFG);
499 val &= ~S3C64XX_SPI_ENCLK_ENABLE;
500 writel(val, regs + S3C64XX_SPI_CLK_CFG);
501 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000502
503 /* Set Polarity and Phase */
504 val = readl(regs + S3C64XX_SPI_CH_CFG);
505 val &= ~(S3C64XX_SPI_CH_SLAVE |
506 S3C64XX_SPI_CPOL_L |
507 S3C64XX_SPI_CPHA_B);
508
509 if (sdd->cur_mode & SPI_CPOL)
510 val |= S3C64XX_SPI_CPOL_L;
511
512 if (sdd->cur_mode & SPI_CPHA)
513 val |= S3C64XX_SPI_CPHA_B;
514
515 writel(val, regs + S3C64XX_SPI_CH_CFG);
516
517 /* Set Channel & DMA Mode */
518 val = readl(regs + S3C64XX_SPI_MODE_CFG);
519 val &= ~(S3C64XX_SPI_MODE_BUS_TSZ_MASK
520 | S3C64XX_SPI_MODE_CH_TSZ_MASK);
521
522 switch (sdd->cur_bpw) {
523 case 32:
524 val |= S3C64XX_SPI_MODE_BUS_TSZ_WORD;
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900525 val |= S3C64XX_SPI_MODE_CH_TSZ_WORD;
Jassi Brar230d42d2009-11-30 07:39:42 +0000526 break;
527 case 16:
528 val |= S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD;
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900529 val |= S3C64XX_SPI_MODE_CH_TSZ_HALFWORD;
Jassi Brar230d42d2009-11-30 07:39:42 +0000530 break;
531 default:
532 val |= S3C64XX_SPI_MODE_BUS_TSZ_BYTE;
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900533 val |= S3C64XX_SPI_MODE_CH_TSZ_BYTE;
Jassi Brar230d42d2009-11-30 07:39:42 +0000534 break;
535 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000536
537 writel(val, regs + S3C64XX_SPI_MODE_CFG);
538
Jassi Brarb42a81c2010-09-29 17:31:33 +0900539 if (sci->clk_from_cmu) {
540 /* Configure Clock */
541 /* There is half-multiplier before the SPI */
542 clk_set_rate(sdd->src_clk, sdd->cur_speed * 2);
543 /* Enable Clock */
544 clk_enable(sdd->src_clk);
545 } else {
546 /* Configure Clock */
547 val = readl(regs + S3C64XX_SPI_CLK_CFG);
548 val &= ~S3C64XX_SPI_PSR_MASK;
549 val |= ((clk_get_rate(sdd->src_clk) / sdd->cur_speed / 2 - 1)
550 & S3C64XX_SPI_PSR_MASK);
551 writel(val, regs + S3C64XX_SPI_CLK_CFG);
Jassi Brar230d42d2009-11-30 07:39:42 +0000552
Jassi Brarb42a81c2010-09-29 17:31:33 +0900553 /* Enable Clock */
554 val = readl(regs + S3C64XX_SPI_CLK_CFG);
555 val |= S3C64XX_SPI_ENCLK_ENABLE;
556 writel(val, regs + S3C64XX_SPI_CLK_CFG);
557 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000558}
559
Jassi Brar230d42d2009-11-30 07:39:42 +0000560#define XFER_DMAADDR_INVALID DMA_BIT_MASK(32)
561
562static int s3c64xx_spi_map_mssg(struct s3c64xx_spi_driver_data *sdd,
563 struct spi_message *msg)
564{
Jassi Brare02ddd42010-09-29 17:31:31 +0900565 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +0000566 struct device *dev = &sdd->pdev->dev;
567 struct spi_transfer *xfer;
568
569 if (msg->is_dma_mapped)
570 return 0;
571
572 /* First mark all xfer unmapped */
573 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
574 xfer->rx_dma = XFER_DMAADDR_INVALID;
575 xfer->tx_dma = XFER_DMAADDR_INVALID;
576 }
577
578 /* Map until end or first fail */
579 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
580
Jassi Brare02ddd42010-09-29 17:31:31 +0900581 if (xfer->len <= ((sci->fifo_lvl_mask >> 1) + 1))
582 continue;
583
Jassi Brar230d42d2009-11-30 07:39:42 +0000584 if (xfer->tx_buf != NULL) {
Jassi Brar251ee472010-09-03 10:36:26 +0900585 xfer->tx_dma = dma_map_single(dev,
586 (void *)xfer->tx_buf, xfer->len,
587 DMA_TO_DEVICE);
Jassi Brar230d42d2009-11-30 07:39:42 +0000588 if (dma_mapping_error(dev, xfer->tx_dma)) {
589 dev_err(dev, "dma_map_single Tx failed\n");
590 xfer->tx_dma = XFER_DMAADDR_INVALID;
591 return -ENOMEM;
592 }
593 }
594
595 if (xfer->rx_buf != NULL) {
596 xfer->rx_dma = dma_map_single(dev, xfer->rx_buf,
597 xfer->len, DMA_FROM_DEVICE);
598 if (dma_mapping_error(dev, xfer->rx_dma)) {
599 dev_err(dev, "dma_map_single Rx failed\n");
600 dma_unmap_single(dev, xfer->tx_dma,
601 xfer->len, DMA_TO_DEVICE);
602 xfer->tx_dma = XFER_DMAADDR_INVALID;
603 xfer->rx_dma = XFER_DMAADDR_INVALID;
604 return -ENOMEM;
605 }
606 }
607 }
608
609 return 0;
610}
611
612static void s3c64xx_spi_unmap_mssg(struct s3c64xx_spi_driver_data *sdd,
613 struct spi_message *msg)
614{
Jassi Brare02ddd42010-09-29 17:31:31 +0900615 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +0000616 struct device *dev = &sdd->pdev->dev;
617 struct spi_transfer *xfer;
618
619 if (msg->is_dma_mapped)
620 return;
621
622 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
623
Jassi Brare02ddd42010-09-29 17:31:31 +0900624 if (xfer->len <= ((sci->fifo_lvl_mask >> 1) + 1))
625 continue;
626
Jassi Brar230d42d2009-11-30 07:39:42 +0000627 if (xfer->rx_buf != NULL
628 && xfer->rx_dma != XFER_DMAADDR_INVALID)
629 dma_unmap_single(dev, xfer->rx_dma,
630 xfer->len, DMA_FROM_DEVICE);
631
632 if (xfer->tx_buf != NULL
633 && xfer->tx_dma != XFER_DMAADDR_INVALID)
634 dma_unmap_single(dev, xfer->tx_dma,
635 xfer->len, DMA_TO_DEVICE);
636 }
637}
638
Mark Brownad2a99a2012-02-15 14:48:32 -0800639static int s3c64xx_spi_transfer_one_message(struct spi_master *master,
640 struct spi_message *msg)
Jassi Brar230d42d2009-11-30 07:39:42 +0000641{
Mark Brownad2a99a2012-02-15 14:48:32 -0800642 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Jassi Brarad7de722010-01-20 13:49:44 -0700643 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +0000644 struct spi_device *spi = msg->spi;
645 struct s3c64xx_spi_csinfo *cs = spi->controller_data;
646 struct spi_transfer *xfer;
647 int status = 0, cs_toggle = 0;
648 u32 speed;
649 u8 bpw;
650
651 /* If Master's(controller) state differs from that needed by Slave */
652 if (sdd->cur_speed != spi->max_speed_hz
653 || sdd->cur_mode != spi->mode
654 || sdd->cur_bpw != spi->bits_per_word) {
655 sdd->cur_bpw = spi->bits_per_word;
656 sdd->cur_speed = spi->max_speed_hz;
657 sdd->cur_mode = spi->mode;
658 s3c64xx_spi_config(sdd);
659 }
660
661 /* Map all the transfers if needed */
662 if (s3c64xx_spi_map_mssg(sdd, msg)) {
663 dev_err(&spi->dev,
664 "Xfer: Unable to map message buffers!\n");
665 status = -ENOMEM;
666 goto out;
667 }
668
669 /* Configure feedback delay */
670 writel(cs->fb_delay & 0x3, sdd->regs + S3C64XX_SPI_FB_CLK);
671
672 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
673
674 unsigned long flags;
675 int use_dma;
676
677 INIT_COMPLETION(sdd->xfer_completion);
678
679 /* Only BPW and Speed may change across transfers */
680 bpw = xfer->bits_per_word ? : spi->bits_per_word;
681 speed = xfer->speed_hz ? : spi->max_speed_hz;
682
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900683 if (xfer->len % (bpw / 8)) {
684 dev_err(&spi->dev,
685 "Xfer length(%u) not a multiple of word size(%u)\n",
686 xfer->len, bpw / 8);
687 status = -EIO;
688 goto out;
689 }
690
Jassi Brar230d42d2009-11-30 07:39:42 +0000691 if (bpw != sdd->cur_bpw || speed != sdd->cur_speed) {
692 sdd->cur_bpw = bpw;
693 sdd->cur_speed = speed;
694 s3c64xx_spi_config(sdd);
695 }
696
697 /* Polling method for xfers not bigger than FIFO capacity */
698 if (xfer->len <= ((sci->fifo_lvl_mask >> 1) + 1))
699 use_dma = 0;
700 else
701 use_dma = 1;
702
703 spin_lock_irqsave(&sdd->lock, flags);
704
705 /* Pending only which is to be done */
706 sdd->state &= ~RXBUSY;
707 sdd->state &= ~TXBUSY;
708
709 enable_datapath(sdd, spi, xfer, use_dma);
710
711 /* Slave Select */
712 enable_cs(sdd, spi);
713
714 /* Start the signals */
715 S3C64XX_SPI_ACT(sdd);
716
717 spin_unlock_irqrestore(&sdd->lock, flags);
718
719 status = wait_for_xfer(sdd, xfer, use_dma);
720
721 /* Quiese the signals */
722 S3C64XX_SPI_DEACT(sdd);
723
724 if (status) {
Joe Perches8a349d42010-02-02 07:22:13 +0000725 dev_err(&spi->dev, "I/O Error: "
726 "rx-%d tx-%d res:rx-%c tx-%c len-%d\n",
Jassi Brar230d42d2009-11-30 07:39:42 +0000727 xfer->rx_buf ? 1 : 0, xfer->tx_buf ? 1 : 0,
728 (sdd->state & RXBUSY) ? 'f' : 'p',
729 (sdd->state & TXBUSY) ? 'f' : 'p',
730 xfer->len);
731
732 if (use_dma) {
733 if (xfer->tx_buf != NULL
734 && (sdd->state & TXBUSY))
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900735 sdd->ops->stop(sdd->tx_dma.ch);
Jassi Brar230d42d2009-11-30 07:39:42 +0000736 if (xfer->rx_buf != NULL
737 && (sdd->state & RXBUSY))
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900738 sdd->ops->stop(sdd->rx_dma.ch);
Jassi Brar230d42d2009-11-30 07:39:42 +0000739 }
740
741 goto out;
742 }
743
744 if (xfer->delay_usecs)
745 udelay(xfer->delay_usecs);
746
747 if (xfer->cs_change) {
748 /* Hint that the next mssg is gonna be
749 for the same device */
750 if (list_is_last(&xfer->transfer_list,
751 &msg->transfers))
752 cs_toggle = 1;
753 else
754 disable_cs(sdd, spi);
755 }
756
757 msg->actual_length += xfer->len;
758
759 flush_fifo(sdd);
760 }
761
762out:
763 if (!cs_toggle || status)
764 disable_cs(sdd, spi);
765 else
766 sdd->tgl_spi = spi;
767
768 s3c64xx_spi_unmap_mssg(sdd, msg);
769
770 msg->status = status;
771
Mark Brownad2a99a2012-02-15 14:48:32 -0800772 spi_finalize_current_message(master);
773
774 return 0;
Jassi Brar230d42d2009-11-30 07:39:42 +0000775}
776
Mark Brownad2a99a2012-02-15 14:48:32 -0800777static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
Jassi Brar230d42d2009-11-30 07:39:42 +0000778{
Mark Brownad2a99a2012-02-15 14:48:32 -0800779 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
Jassi Brar230d42d2009-11-30 07:39:42 +0000780
781 /* Acquire DMA channels */
782 while (!acquire_dma(sdd))
783 msleep(10);
784
Mark Brownb97b6622011-12-04 00:58:06 +0000785 pm_runtime_get_sync(&sdd->pdev->dev);
786
Mark Brownad2a99a2012-02-15 14:48:32 -0800787 return 0;
788}
Jassi Brar230d42d2009-11-30 07:39:42 +0000789
Mark Brownad2a99a2012-02-15 14:48:32 -0800790static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
791{
792 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
Jassi Brar230d42d2009-11-30 07:39:42 +0000793
794 /* Free DMA channels */
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900795 sdd->ops->release(sdd->rx_dma.ch, &s3c64xx_spi_dma_client);
796 sdd->ops->release(sdd->tx_dma.ch, &s3c64xx_spi_dma_client);
Mark Brownb97b6622011-12-04 00:58:06 +0000797
798 pm_runtime_put(&sdd->pdev->dev);
Jassi Brar230d42d2009-11-30 07:39:42 +0000799
800 return 0;
801}
802
803/*
804 * Here we only check the validity of requested configuration
805 * and save the configuration in a local data-structure.
806 * The controller is actually configured only just before we
807 * get a message to transfer.
808 */
809static int s3c64xx_spi_setup(struct spi_device *spi)
810{
811 struct s3c64xx_spi_csinfo *cs = spi->controller_data;
812 struct s3c64xx_spi_driver_data *sdd;
Jassi Brarad7de722010-01-20 13:49:44 -0700813 struct s3c64xx_spi_info *sci;
Jassi Brar230d42d2009-11-30 07:39:42 +0000814 struct spi_message *msg;
Jassi Brar230d42d2009-11-30 07:39:42 +0000815 unsigned long flags;
816 int err = 0;
817
818 if (cs == NULL || cs->set_level == NULL) {
819 dev_err(&spi->dev, "No CS for SPI(%d)\n", spi->chip_select);
820 return -ENODEV;
821 }
822
823 sdd = spi_master_get_devdata(spi->master);
824 sci = sdd->cntrlr_info;
825
826 spin_lock_irqsave(&sdd->lock, flags);
827
828 list_for_each_entry(msg, &sdd->queue, queue) {
829 /* Is some mssg is already queued for this device */
830 if (msg->spi == spi) {
831 dev_err(&spi->dev,
832 "setup: attempt while mssg in queue!\n");
833 spin_unlock_irqrestore(&sdd->lock, flags);
834 return -EBUSY;
835 }
836 }
837
Jassi Brar230d42d2009-11-30 07:39:42 +0000838 spin_unlock_irqrestore(&sdd->lock, flags);
839
840 if (spi->bits_per_word != 8
841 && spi->bits_per_word != 16
842 && spi->bits_per_word != 32) {
843 dev_err(&spi->dev, "setup: %dbits/wrd not supported!\n",
844 spi->bits_per_word);
845 err = -EINVAL;
846 goto setup_exit;
847 }
848
Mark Brownb97b6622011-12-04 00:58:06 +0000849 pm_runtime_get_sync(&sdd->pdev->dev);
850
Jassi Brar230d42d2009-11-30 07:39:42 +0000851 /* Check if we can provide the requested rate */
Jassi Brarb42a81c2010-09-29 17:31:33 +0900852 if (!sci->clk_from_cmu) {
853 u32 psr, speed;
Jassi Brar230d42d2009-11-30 07:39:42 +0000854
Jassi Brarb42a81c2010-09-29 17:31:33 +0900855 /* Max possible */
856 speed = clk_get_rate(sdd->src_clk) / 2 / (0 + 1);
Jassi Brar230d42d2009-11-30 07:39:42 +0000857
Jassi Brarb42a81c2010-09-29 17:31:33 +0900858 if (spi->max_speed_hz > speed)
859 spi->max_speed_hz = speed;
Jassi Brar230d42d2009-11-30 07:39:42 +0000860
Jassi Brarb42a81c2010-09-29 17:31:33 +0900861 psr = clk_get_rate(sdd->src_clk) / 2 / spi->max_speed_hz - 1;
862 psr &= S3C64XX_SPI_PSR_MASK;
863 if (psr == S3C64XX_SPI_PSR_MASK)
864 psr--;
865
866 speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
867 if (spi->max_speed_hz < speed) {
868 if (psr+1 < S3C64XX_SPI_PSR_MASK) {
869 psr++;
870 } else {
871 err = -EINVAL;
872 goto setup_exit;
873 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000874 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000875
Jassi Brarb42a81c2010-09-29 17:31:33 +0900876 speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
877 if (spi->max_speed_hz >= speed)
878 spi->max_speed_hz = speed;
879 else
880 err = -EINVAL;
881 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000882
Mark Brownb97b6622011-12-04 00:58:06 +0000883 pm_runtime_put(&sdd->pdev->dev);
884
Jassi Brar230d42d2009-11-30 07:39:42 +0000885setup_exit:
886
887 /* setup() returns with device de-selected */
888 disable_cs(sdd, spi);
889
890 return err;
891}
892
Mark Brownc2573122011-11-10 10:57:32 +0000893static irqreturn_t s3c64xx_spi_irq(int irq, void *data)
894{
895 struct s3c64xx_spi_driver_data *sdd = data;
896 struct spi_master *spi = sdd->master;
897 unsigned int val;
898
899 val = readl(sdd->regs + S3C64XX_SPI_PENDING_CLR);
900
901 val &= S3C64XX_SPI_PND_RX_OVERRUN_CLR |
902 S3C64XX_SPI_PND_RX_UNDERRUN_CLR |
903 S3C64XX_SPI_PND_TX_OVERRUN_CLR |
904 S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
905
906 writel(val, sdd->regs + S3C64XX_SPI_PENDING_CLR);
907
908 if (val & S3C64XX_SPI_PND_RX_OVERRUN_CLR)
909 dev_err(&spi->dev, "RX overrun\n");
910 if (val & S3C64XX_SPI_PND_RX_UNDERRUN_CLR)
911 dev_err(&spi->dev, "RX underrun\n");
912 if (val & S3C64XX_SPI_PND_TX_OVERRUN_CLR)
913 dev_err(&spi->dev, "TX overrun\n");
914 if (val & S3C64XX_SPI_PND_TX_UNDERRUN_CLR)
915 dev_err(&spi->dev, "TX underrun\n");
916
917 return IRQ_HANDLED;
918}
919
Jassi Brar230d42d2009-11-30 07:39:42 +0000920static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data *sdd, int channel)
921{
Jassi Brarad7de722010-01-20 13:49:44 -0700922 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +0000923 void __iomem *regs = sdd->regs;
924 unsigned int val;
925
926 sdd->cur_speed = 0;
927
928 S3C64XX_SPI_DEACT(sdd);
929
930 /* Disable Interrupts - we use Polling if not DMA mode */
931 writel(0, regs + S3C64XX_SPI_INT_EN);
932
Jassi Brarb42a81c2010-09-29 17:31:33 +0900933 if (!sci->clk_from_cmu)
934 writel(sci->src_clk_nr << S3C64XX_SPI_CLKSEL_SRCSHFT,
Jassi Brar230d42d2009-11-30 07:39:42 +0000935 regs + S3C64XX_SPI_CLK_CFG);
936 writel(0, regs + S3C64XX_SPI_MODE_CFG);
937 writel(0, regs + S3C64XX_SPI_PACKET_CNT);
938
939 /* Clear any irq pending bits */
940 writel(readl(regs + S3C64XX_SPI_PENDING_CLR),
941 regs + S3C64XX_SPI_PENDING_CLR);
942
943 writel(0, regs + S3C64XX_SPI_SWAP_CFG);
944
945 val = readl(regs + S3C64XX_SPI_MODE_CFG);
946 val &= ~S3C64XX_SPI_MODE_4BURST;
947 val &= ~(S3C64XX_SPI_MAX_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
948 val |= (S3C64XX_SPI_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
949 writel(val, regs + S3C64XX_SPI_MODE_CFG);
950
951 flush_fifo(sdd);
952}
953
954static int __init s3c64xx_spi_probe(struct platform_device *pdev)
955{
956 struct resource *mem_res, *dmatx_res, *dmarx_res;
957 struct s3c64xx_spi_driver_data *sdd;
Jassi Brarad7de722010-01-20 13:49:44 -0700958 struct s3c64xx_spi_info *sci;
Jassi Brar230d42d2009-11-30 07:39:42 +0000959 struct spi_master *master;
Mark Brownc2573122011-11-10 10:57:32 +0000960 int ret, irq;
Padmavathi Vennaa24d8502011-11-02 20:04:19 +0900961 char clk_name[16];
Jassi Brar230d42d2009-11-30 07:39:42 +0000962
963 if (pdev->id < 0) {
964 dev_err(&pdev->dev,
965 "Invalid platform device id-%d\n", pdev->id);
966 return -ENODEV;
967 }
968
969 if (pdev->dev.platform_data == NULL) {
970 dev_err(&pdev->dev, "platform_data missing!\n");
971 return -ENODEV;
972 }
973
Mark Browncc0fc0b2010-09-01 08:55:22 -0600974 sci = pdev->dev.platform_data;
Mark Browncc0fc0b2010-09-01 08:55:22 -0600975
Jassi Brar230d42d2009-11-30 07:39:42 +0000976 /* Check for availability of necessary resource */
977
978 dmatx_res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
979 if (dmatx_res == NULL) {
980 dev_err(&pdev->dev, "Unable to get SPI-Tx dma resource\n");
981 return -ENXIO;
982 }
983
984 dmarx_res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
985 if (dmarx_res == NULL) {
986 dev_err(&pdev->dev, "Unable to get SPI-Rx dma resource\n");
987 return -ENXIO;
988 }
989
990 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
991 if (mem_res == NULL) {
992 dev_err(&pdev->dev, "Unable to get SPI MEM resource\n");
993 return -ENXIO;
994 }
995
Mark Brownc2573122011-11-10 10:57:32 +0000996 irq = platform_get_irq(pdev, 0);
997 if (irq < 0) {
998 dev_warn(&pdev->dev, "Failed to get IRQ: %d\n", irq);
999 return irq;
1000 }
1001
Jassi Brar230d42d2009-11-30 07:39:42 +00001002 master = spi_alloc_master(&pdev->dev,
1003 sizeof(struct s3c64xx_spi_driver_data));
1004 if (master == NULL) {
1005 dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
1006 return -ENOMEM;
1007 }
1008
Jassi Brar230d42d2009-11-30 07:39:42 +00001009 platform_set_drvdata(pdev, master);
1010
1011 sdd = spi_master_get_devdata(master);
1012 sdd->master = master;
1013 sdd->cntrlr_info = sci;
1014 sdd->pdev = pdev;
1015 sdd->sfr_start = mem_res->start;
Boojin Kim82ab8cd2011-09-02 09:44:42 +09001016 sdd->tx_dma.dmach = dmatx_res->start;
Kyoungil Kim054ebcc2012-03-10 09:48:46 +09001017 sdd->tx_dma.direction = DMA_MEM_TO_DEV;
Boojin Kim82ab8cd2011-09-02 09:44:42 +09001018 sdd->rx_dma.dmach = dmarx_res->start;
Kyoungil Kim054ebcc2012-03-10 09:48:46 +09001019 sdd->rx_dma.direction = DMA_DEV_TO_MEM;
Jassi Brar230d42d2009-11-30 07:39:42 +00001020
1021 sdd->cur_bpw = 8;
1022
1023 master->bus_num = pdev->id;
1024 master->setup = s3c64xx_spi_setup;
Mark Brownad2a99a2012-02-15 14:48:32 -08001025 master->prepare_transfer_hardware = s3c64xx_spi_prepare_transfer;
1026 master->transfer_one_message = s3c64xx_spi_transfer_one_message;
1027 master->unprepare_transfer_hardware = s3c64xx_spi_unprepare_transfer;
Jassi Brar230d42d2009-11-30 07:39:42 +00001028 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;
1032
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");
1036 ret = -ENXIO;
1037 goto err0;
1038 }
1039
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");
1043 ret = -ENXIO;
1044 goto err1;
1045 }
1046
1047 if (sci->cfg_gpio == NULL || sci->cfg_gpio(pdev)) {
1048 dev_err(&pdev->dev, "Unable to config gpio\n");
1049 ret = -EBUSY;
1050 goto err2;
1051 }
1052
1053 /* Setup clocks */
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);
1058 goto err3;
1059 }
1060
1061 if (clk_enable(sdd->clk)) {
1062 dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n");
1063 ret = -EBUSY;
1064 goto err4;
1065 }
1066
Padmavathi Vennaa24d8502011-11-02 20:04:19 +09001067 sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr);
1068 sdd->src_clk = clk_get(&pdev->dev, clk_name);
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001069 if (IS_ERR(sdd->src_clk)) {
Jassi Brar230d42d2009-11-30 07:39:42 +00001070 dev_err(&pdev->dev,
Padmavathi Vennaa24d8502011-11-02 20:04:19 +09001071 "Unable to acquire clock '%s'\n", clk_name);
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001072 ret = PTR_ERR(sdd->src_clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001073 goto err5;
1074 }
1075
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001076 if (clk_enable(sdd->src_clk)) {
Padmavathi Vennaa24d8502011-11-02 20:04:19 +09001077 dev_err(&pdev->dev, "Couldn't enable clock '%s'\n", clk_name);
Jassi Brar230d42d2009-11-30 07:39:42 +00001078 ret = -EBUSY;
1079 goto err6;
1080 }
1081
Jassi Brar230d42d2009-11-30 07:39:42 +00001082 /* Setup Deufult Mode */
1083 s3c64xx_spi_hwinit(sdd, pdev->id);
1084
1085 spin_lock_init(&sdd->lock);
1086 init_completion(&sdd->xfer_completion);
Jassi Brar230d42d2009-11-30 07:39:42 +00001087 INIT_LIST_HEAD(&sdd->queue);
1088
Mark Brownc2573122011-11-10 10:57:32 +00001089 ret = request_irq(irq, s3c64xx_spi_irq, 0, "spi-s3c64xx", sdd);
1090 if (ret != 0) {
1091 dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n",
1092 irq, ret);
Mark Brownad2a99a2012-02-15 14:48:32 -08001093 goto err7;
Mark Brownc2573122011-11-10 10:57:32 +00001094 }
1095
1096 writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
1097 S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
1098 sdd->regs + S3C64XX_SPI_INT_EN);
1099
Jassi Brar230d42d2009-11-30 07:39:42 +00001100 if (spi_register_master(master)) {
1101 dev_err(&pdev->dev, "cannot register SPI master\n");
1102 ret = -EBUSY;
Mark Brownad2a99a2012-02-15 14:48:32 -08001103 goto err8;
Jassi Brar230d42d2009-11-30 07:39:42 +00001104 }
1105
Joe Perches8a349d42010-02-02 07:22:13 +00001106 dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d "
1107 "with %d Slaves attached\n",
Jassi Brar230d42d2009-11-30 07:39:42 +00001108 pdev->id, master->num_chipselect);
Joe Perches8a349d42010-02-02 07:22:13 +00001109 dev_dbg(&pdev->dev, "\tIOmem=[0x%x-0x%x]\tDMA=[Rx-%d, Tx-%d]\n",
Jassi Brar230d42d2009-11-30 07:39:42 +00001110 mem_res->end, mem_res->start,
Boojin Kim82ab8cd2011-09-02 09:44:42 +09001111 sdd->rx_dma.dmach, sdd->tx_dma.dmach);
Jassi Brar230d42d2009-11-30 07:39:42 +00001112
Mark Brownb97b6622011-12-04 00:58:06 +00001113 pm_runtime_enable(&pdev->dev);
1114
Jassi Brar230d42d2009-11-30 07:39:42 +00001115 return 0;
1116
1117err8:
Mark Brownad2a99a2012-02-15 14:48:32 -08001118 free_irq(irq, sdd);
Jassi Brar230d42d2009-11-30 07:39:42 +00001119err7:
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001120 clk_disable(sdd->src_clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001121err6:
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001122 clk_put(sdd->src_clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001123err5:
1124 clk_disable(sdd->clk);
1125err4:
1126 clk_put(sdd->clk);
1127err3:
1128err2:
1129 iounmap((void *) sdd->regs);
1130err1:
1131 release_mem_region(mem_res->start, resource_size(mem_res));
1132err0:
1133 platform_set_drvdata(pdev, NULL);
1134 spi_master_put(master);
1135
1136 return ret;
1137}
1138
1139static int s3c64xx_spi_remove(struct platform_device *pdev)
1140{
1141 struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
1142 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Jassi Brar230d42d2009-11-30 07:39:42 +00001143 struct resource *mem_res;
Jassi Brar230d42d2009-11-30 07:39:42 +00001144
Mark Brownb97b6622011-12-04 00:58:06 +00001145 pm_runtime_disable(&pdev->dev);
1146
Jassi Brar230d42d2009-11-30 07:39:42 +00001147 spi_unregister_master(master);
1148
Mark Brownc2573122011-11-10 10:57:32 +00001149 writel(0, sdd->regs + S3C64XX_SPI_INT_EN);
1150
1151 free_irq(platform_get_irq(pdev, 0), sdd);
1152
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001153 clk_disable(sdd->src_clk);
1154 clk_put(sdd->src_clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001155
1156 clk_disable(sdd->clk);
1157 clk_put(sdd->clk);
1158
1159 iounmap((void *) sdd->regs);
1160
1161 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Jassi Braref6c6802010-01-20 13:49:44 -07001162 if (mem_res != NULL)
1163 release_mem_region(mem_res->start, resource_size(mem_res));
Jassi Brar230d42d2009-11-30 07:39:42 +00001164
1165 platform_set_drvdata(pdev, NULL);
1166 spi_master_put(master);
1167
1168 return 0;
1169}
1170
1171#ifdef CONFIG_PM
Mark Browne25d0bf2011-12-04 00:36:18 +00001172static int s3c64xx_spi_suspend(struct device *dev)
Jassi Brar230d42d2009-11-30 07:39:42 +00001173{
Mark Browne25d0bf2011-12-04 00:36:18 +00001174 struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
Jassi Brar230d42d2009-11-30 07:39:42 +00001175 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Jassi Brar230d42d2009-11-30 07:39:42 +00001176
Mark Brownad2a99a2012-02-15 14:48:32 -08001177 spi_master_suspend(master);
Jassi Brar230d42d2009-11-30 07:39:42 +00001178
1179 /* Disable the clock */
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001180 clk_disable(sdd->src_clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001181 clk_disable(sdd->clk);
1182
1183 sdd->cur_speed = 0; /* Output Clock is stopped */
1184
1185 return 0;
1186}
1187
Mark Browne25d0bf2011-12-04 00:36:18 +00001188static int s3c64xx_spi_resume(struct device *dev)
Jassi Brar230d42d2009-11-30 07:39:42 +00001189{
Mark Browne25d0bf2011-12-04 00:36:18 +00001190 struct platform_device *pdev = to_platform_device(dev);
1191 struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
Jassi Brar230d42d2009-11-30 07:39:42 +00001192 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Jassi Brarad7de722010-01-20 13:49:44 -07001193 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +00001194
1195 sci->cfg_gpio(pdev);
1196
1197 /* Enable the clock */
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001198 clk_enable(sdd->src_clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001199 clk_enable(sdd->clk);
1200
1201 s3c64xx_spi_hwinit(sdd, pdev->id);
1202
Mark Brownad2a99a2012-02-15 14:48:32 -08001203 spi_master_resume(master);
Jassi Brar230d42d2009-11-30 07:39:42 +00001204
1205 return 0;
1206}
Jassi Brar230d42d2009-11-30 07:39:42 +00001207#endif /* CONFIG_PM */
1208
Mark Brownb97b6622011-12-04 00:58:06 +00001209#ifdef CONFIG_PM_RUNTIME
1210static int s3c64xx_spi_runtime_suspend(struct device *dev)
1211{
1212 struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
1213 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1214
1215 clk_disable(sdd->clk);
1216 clk_disable(sdd->src_clk);
1217
1218 return 0;
1219}
1220
1221static int s3c64xx_spi_runtime_resume(struct device *dev)
1222{
1223 struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
1224 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1225
1226 clk_enable(sdd->src_clk);
1227 clk_enable(sdd->clk);
1228
1229 return 0;
1230}
1231#endif /* CONFIG_PM_RUNTIME */
1232
Mark Browne25d0bf2011-12-04 00:36:18 +00001233static const struct dev_pm_ops s3c64xx_spi_pm = {
1234 SET_SYSTEM_SLEEP_PM_OPS(s3c64xx_spi_suspend, s3c64xx_spi_resume)
Mark Brownb97b6622011-12-04 00:58:06 +00001235 SET_RUNTIME_PM_OPS(s3c64xx_spi_runtime_suspend,
1236 s3c64xx_spi_runtime_resume, NULL)
Mark Browne25d0bf2011-12-04 00:36:18 +00001237};
1238
Jassi Brar230d42d2009-11-30 07:39:42 +00001239static struct platform_driver s3c64xx_spi_driver = {
1240 .driver = {
1241 .name = "s3c64xx-spi",
1242 .owner = THIS_MODULE,
Mark Browne25d0bf2011-12-04 00:36:18 +00001243 .pm = &s3c64xx_spi_pm,
Jassi Brar230d42d2009-11-30 07:39:42 +00001244 },
1245 .remove = s3c64xx_spi_remove,
Jassi Brar230d42d2009-11-30 07:39:42 +00001246};
1247MODULE_ALIAS("platform:s3c64xx-spi");
1248
1249static int __init s3c64xx_spi_init(void)
1250{
1251 return platform_driver_probe(&s3c64xx_spi_driver, s3c64xx_spi_probe);
1252}
Mark Brownd2a787f2010-09-07 11:29:17 +01001253subsys_initcall(s3c64xx_spi_init);
Jassi Brar230d42d2009-11-30 07:39:42 +00001254
1255static void __exit s3c64xx_spi_exit(void)
1256{
1257 platform_driver_unregister(&s3c64xx_spi_driver);
1258}
1259module_exit(s3c64xx_spi_exit);
1260
1261MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
1262MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
1263MODULE_LICENSE("GPL");