blob: 04f947d8d3c759a7adad7145f42067c6217efe00 [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>
Thomas Abraham1c20c202012-07-13 07:15:14 +090030#include <linux/gpio.h>
Thomas Abraham2b908072012-07-13 07:15:15 +090031#include <linux/of.h>
32#include <linux/of_gpio.h>
Jassi Brar230d42d2009-11-30 07:39:42 +000033
34#include <mach/dma.h>
Jassi Brare6b873c2010-01-20 13:49:45 -070035#include <plat/s3c64xx-spi.h>
Jassi Brar230d42d2009-11-30 07:39:42 +000036
Thomas Abrahama5238e32012-07-13 07:15:14 +090037#define MAX_SPI_PORTS 3
38
Jassi Brar230d42d2009-11-30 07:39:42 +000039/* Registers and bit-fields */
40
41#define S3C64XX_SPI_CH_CFG 0x00
42#define S3C64XX_SPI_CLK_CFG 0x04
43#define S3C64XX_SPI_MODE_CFG 0x08
44#define S3C64XX_SPI_SLAVE_SEL 0x0C
45#define S3C64XX_SPI_INT_EN 0x10
46#define S3C64XX_SPI_STATUS 0x14
47#define S3C64XX_SPI_TX_DATA 0x18
48#define S3C64XX_SPI_RX_DATA 0x1C
49#define S3C64XX_SPI_PACKET_CNT 0x20
50#define S3C64XX_SPI_PENDING_CLR 0x24
51#define S3C64XX_SPI_SWAP_CFG 0x28
52#define S3C64XX_SPI_FB_CLK 0x2C
53
54#define S3C64XX_SPI_CH_HS_EN (1<<6) /* High Speed Enable */
55#define S3C64XX_SPI_CH_SW_RST (1<<5)
56#define S3C64XX_SPI_CH_SLAVE (1<<4)
57#define S3C64XX_SPI_CPOL_L (1<<3)
58#define S3C64XX_SPI_CPHA_B (1<<2)
59#define S3C64XX_SPI_CH_RXCH_ON (1<<1)
60#define S3C64XX_SPI_CH_TXCH_ON (1<<0)
61
62#define S3C64XX_SPI_CLKSEL_SRCMSK (3<<9)
63#define S3C64XX_SPI_CLKSEL_SRCSHFT 9
64#define S3C64XX_SPI_ENCLK_ENABLE (1<<8)
65#define S3C64XX_SPI_PSR_MASK 0xff
66
67#define S3C64XX_SPI_MODE_CH_TSZ_BYTE (0<<29)
68#define S3C64XX_SPI_MODE_CH_TSZ_HALFWORD (1<<29)
69#define S3C64XX_SPI_MODE_CH_TSZ_WORD (2<<29)
70#define S3C64XX_SPI_MODE_CH_TSZ_MASK (3<<29)
71#define S3C64XX_SPI_MODE_BUS_TSZ_BYTE (0<<17)
72#define S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD (1<<17)
73#define S3C64XX_SPI_MODE_BUS_TSZ_WORD (2<<17)
74#define S3C64XX_SPI_MODE_BUS_TSZ_MASK (3<<17)
75#define S3C64XX_SPI_MODE_RXDMA_ON (1<<2)
76#define S3C64XX_SPI_MODE_TXDMA_ON (1<<1)
77#define S3C64XX_SPI_MODE_4BURST (1<<0)
78
79#define S3C64XX_SPI_SLAVE_AUTO (1<<1)
80#define S3C64XX_SPI_SLAVE_SIG_INACT (1<<0)
81
82#define S3C64XX_SPI_ACT(c) writel(0, (c)->regs + S3C64XX_SPI_SLAVE_SEL)
83
84#define S3C64XX_SPI_DEACT(c) writel(S3C64XX_SPI_SLAVE_SIG_INACT, \
85 (c)->regs + S3C64XX_SPI_SLAVE_SEL)
86
87#define S3C64XX_SPI_INT_TRAILING_EN (1<<6)
88#define S3C64XX_SPI_INT_RX_OVERRUN_EN (1<<5)
89#define S3C64XX_SPI_INT_RX_UNDERRUN_EN (1<<4)
90#define S3C64XX_SPI_INT_TX_OVERRUN_EN (1<<3)
91#define S3C64XX_SPI_INT_TX_UNDERRUN_EN (1<<2)
92#define S3C64XX_SPI_INT_RX_FIFORDY_EN (1<<1)
93#define S3C64XX_SPI_INT_TX_FIFORDY_EN (1<<0)
94
95#define S3C64XX_SPI_ST_RX_OVERRUN_ERR (1<<5)
96#define S3C64XX_SPI_ST_RX_UNDERRUN_ERR (1<<4)
97#define S3C64XX_SPI_ST_TX_OVERRUN_ERR (1<<3)
98#define S3C64XX_SPI_ST_TX_UNDERRUN_ERR (1<<2)
99#define S3C64XX_SPI_ST_RX_FIFORDY (1<<1)
100#define S3C64XX_SPI_ST_TX_FIFORDY (1<<0)
101
102#define S3C64XX_SPI_PACKET_CNT_EN (1<<16)
103
104#define S3C64XX_SPI_PND_TX_UNDERRUN_CLR (1<<4)
105#define S3C64XX_SPI_PND_TX_OVERRUN_CLR (1<<3)
106#define S3C64XX_SPI_PND_RX_UNDERRUN_CLR (1<<2)
107#define S3C64XX_SPI_PND_RX_OVERRUN_CLR (1<<1)
108#define S3C64XX_SPI_PND_TRAILING_CLR (1<<0)
109
110#define S3C64XX_SPI_SWAP_RX_HALF_WORD (1<<7)
111#define S3C64XX_SPI_SWAP_RX_BYTE (1<<6)
112#define S3C64XX_SPI_SWAP_RX_BIT (1<<5)
113#define S3C64XX_SPI_SWAP_RX_EN (1<<4)
114#define S3C64XX_SPI_SWAP_TX_HALF_WORD (1<<3)
115#define S3C64XX_SPI_SWAP_TX_BYTE (1<<2)
116#define S3C64XX_SPI_SWAP_TX_BIT (1<<1)
117#define S3C64XX_SPI_SWAP_TX_EN (1<<0)
118
119#define S3C64XX_SPI_FBCLK_MSK (3<<0)
120
Thomas Abrahama5238e32012-07-13 07:15:14 +0900121#define FIFO_LVL_MASK(i) ((i)->port_conf->fifo_lvl_mask[i->port_id])
122#define S3C64XX_SPI_ST_TX_DONE(v, i) (((v) & \
123 (1 << (i)->port_conf->tx_st_done)) ? 1 : 0)
124#define TX_FIFO_LVL(v, i) (((v) >> 6) & FIFO_LVL_MASK(i))
125#define RX_FIFO_LVL(v, i) (((v) >> (i)->port_conf->rx_lvl_offset) & \
126 FIFO_LVL_MASK(i))
Jassi Brar230d42d2009-11-30 07:39:42 +0000127
128#define S3C64XX_SPI_MAX_TRAILCNT 0x3ff
129#define S3C64XX_SPI_TRAILCNT_OFF 19
130
131#define S3C64XX_SPI_TRAILCNT S3C64XX_SPI_MAX_TRAILCNT
132
133#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
134
Jassi Brar230d42d2009-11-30 07:39:42 +0000135#define RXBUSY (1<<2)
136#define TXBUSY (1<<3)
137
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900138struct s3c64xx_spi_dma_data {
139 unsigned ch;
140 enum dma_data_direction direction;
141 enum dma_ch dmach;
Thomas Abraham2b908072012-07-13 07:15:15 +0900142 struct property *dma_prop;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900143};
144
Jassi Brar230d42d2009-11-30 07:39:42 +0000145/**
Thomas Abrahama5238e32012-07-13 07:15:14 +0900146 * struct s3c64xx_spi_info - SPI Controller hardware info
147 * @fifo_lvl_mask: Bit-mask for {TX|RX}_FIFO_LVL bits in SPI_STATUS register.
148 * @rx_lvl_offset: Bit offset of RX_FIFO_LVL bits in SPI_STATUS regiter.
149 * @tx_st_done: Bit offset of TX_DONE bit in SPI_STATUS regiter.
150 * @high_speed: True, if the controller supports HIGH_SPEED_EN bit.
151 * @clk_from_cmu: True, if the controller does not include a clock mux and
152 * prescaler unit.
153 *
154 * The Samsung s3c64xx SPI controller are used on various Samsung SoC's but
155 * differ in some aspects such as the size of the fifo and spi bus clock
156 * setup. Such differences are specified to the driver using this structure
157 * which is provided as driver data to the driver.
158 */
159struct s3c64xx_spi_port_config {
160 int fifo_lvl_mask[MAX_SPI_PORTS];
161 int rx_lvl_offset;
162 int tx_st_done;
163 bool high_speed;
164 bool clk_from_cmu;
165};
166
167/**
Jassi Brar230d42d2009-11-30 07:39:42 +0000168 * struct s3c64xx_spi_driver_data - Runtime info holder for SPI driver.
169 * @clk: Pointer to the spi clock.
Jassi Brarb0d5d6e2010-01-20 13:49:44 -0700170 * @src_clk: Pointer to the clock used to generate SPI signals.
Jassi Brar230d42d2009-11-30 07:39:42 +0000171 * @master: Pointer to the SPI Protocol master.
Jassi Brar230d42d2009-11-30 07:39:42 +0000172 * @cntrlr_info: Platform specific data for the controller this driver manages.
173 * @tgl_spi: Pointer to the last CS left untoggled by the cs_change hint.
Jassi Brar230d42d2009-11-30 07:39:42 +0000174 * @queue: To log SPI xfer requests.
175 * @lock: Controller specific lock.
176 * @state: Set of FLAGS to indicate status.
177 * @rx_dmach: Controller's DMA channel for Rx.
178 * @tx_dmach: Controller's DMA channel for Tx.
179 * @sfr_start: BUS address of SPI controller regs.
180 * @regs: Pointer to ioremap'ed controller registers.
Mark Brownc2573122011-11-10 10:57:32 +0000181 * @irq: interrupt
Jassi Brar230d42d2009-11-30 07:39:42 +0000182 * @xfer_completion: To indicate completion of xfer task.
183 * @cur_mode: Stores the active configuration of the controller.
184 * @cur_bpw: Stores the active bits per word settings.
185 * @cur_speed: Stores the active xfer clock speed.
186 */
187struct s3c64xx_spi_driver_data {
188 void __iomem *regs;
189 struct clk *clk;
Jassi Brarb0d5d6e2010-01-20 13:49:44 -0700190 struct clk *src_clk;
Jassi Brar230d42d2009-11-30 07:39:42 +0000191 struct platform_device *pdev;
192 struct spi_master *master;
Jassi Brarad7de722010-01-20 13:49:44 -0700193 struct s3c64xx_spi_info *cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +0000194 struct spi_device *tgl_spi;
Jassi Brar230d42d2009-11-30 07:39:42 +0000195 struct list_head queue;
196 spinlock_t lock;
Jassi Brar230d42d2009-11-30 07:39:42 +0000197 unsigned long sfr_start;
198 struct completion xfer_completion;
199 unsigned state;
200 unsigned cur_mode, cur_bpw;
201 unsigned cur_speed;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900202 struct s3c64xx_spi_dma_data rx_dma;
203 struct s3c64xx_spi_dma_data tx_dma;
Boojin Kim39d3e802011-09-02 09:44:41 +0900204 struct samsung_dma_ops *ops;
Thomas Abrahama5238e32012-07-13 07:15:14 +0900205 struct s3c64xx_spi_port_config *port_conf;
206 unsigned int port_id;
Thomas Abraham2b908072012-07-13 07:15:15 +0900207 unsigned long gpios[4];
Jassi Brar230d42d2009-11-30 07:39:42 +0000208};
209
210static struct s3c2410_dma_client s3c64xx_spi_dma_client = {
211 .name = "samsung-spi-dma",
212};
213
214static void flush_fifo(struct s3c64xx_spi_driver_data *sdd)
215{
Jassi Brar230d42d2009-11-30 07:39:42 +0000216 void __iomem *regs = sdd->regs;
217 unsigned long loops;
218 u32 val;
219
220 writel(0, regs + S3C64XX_SPI_PACKET_CNT);
221
222 val = readl(regs + S3C64XX_SPI_CH_CFG);
223 val |= S3C64XX_SPI_CH_SW_RST;
224 val &= ~S3C64XX_SPI_CH_HS_EN;
225 writel(val, regs + S3C64XX_SPI_CH_CFG);
226
227 /* Flush TxFIFO*/
228 loops = msecs_to_loops(1);
229 do {
230 val = readl(regs + S3C64XX_SPI_STATUS);
Thomas Abrahama5238e32012-07-13 07:15:14 +0900231 } while (TX_FIFO_LVL(val, sdd) && loops--);
Jassi Brar230d42d2009-11-30 07:39:42 +0000232
Mark Brownbe7852a2010-08-23 17:40:56 +0100233 if (loops == 0)
234 dev_warn(&sdd->pdev->dev, "Timed out flushing TX FIFO\n");
235
Jassi Brar230d42d2009-11-30 07:39:42 +0000236 /* Flush RxFIFO*/
237 loops = msecs_to_loops(1);
238 do {
239 val = readl(regs + S3C64XX_SPI_STATUS);
Thomas Abrahama5238e32012-07-13 07:15:14 +0900240 if (RX_FIFO_LVL(val, sdd))
Jassi Brar230d42d2009-11-30 07:39:42 +0000241 readl(regs + S3C64XX_SPI_RX_DATA);
242 else
243 break;
244 } while (loops--);
245
Mark Brownbe7852a2010-08-23 17:40:56 +0100246 if (loops == 0)
247 dev_warn(&sdd->pdev->dev, "Timed out flushing RX FIFO\n");
248
Jassi Brar230d42d2009-11-30 07:39:42 +0000249 val = readl(regs + S3C64XX_SPI_CH_CFG);
250 val &= ~S3C64XX_SPI_CH_SW_RST;
251 writel(val, regs + S3C64XX_SPI_CH_CFG);
252
253 val = readl(regs + S3C64XX_SPI_MODE_CFG);
254 val &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
255 writel(val, regs + S3C64XX_SPI_MODE_CFG);
256
257 val = readl(regs + S3C64XX_SPI_CH_CFG);
258 val &= ~(S3C64XX_SPI_CH_RXCH_ON | S3C64XX_SPI_CH_TXCH_ON);
259 writel(val, regs + S3C64XX_SPI_CH_CFG);
260}
261
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900262static void s3c64xx_spi_dmacb(void *data)
Boojin Kim39d3e802011-09-02 09:44:41 +0900263{
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900264 struct s3c64xx_spi_driver_data *sdd;
265 struct s3c64xx_spi_dma_data *dma = data;
Boojin Kim39d3e802011-09-02 09:44:41 +0900266 unsigned long flags;
267
Kyoungil Kim054ebcc2012-03-10 09:48:46 +0900268 if (dma->direction == DMA_DEV_TO_MEM)
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900269 sdd = container_of(data,
270 struct s3c64xx_spi_driver_data, rx_dma);
271 else
272 sdd = container_of(data,
273 struct s3c64xx_spi_driver_data, tx_dma);
274
Boojin Kim39d3e802011-09-02 09:44:41 +0900275 spin_lock_irqsave(&sdd->lock, flags);
276
Kyoungil Kim054ebcc2012-03-10 09:48:46 +0900277 if (dma->direction == DMA_DEV_TO_MEM) {
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900278 sdd->state &= ~RXBUSY;
279 if (!(sdd->state & TXBUSY))
280 complete(&sdd->xfer_completion);
281 } else {
282 sdd->state &= ~TXBUSY;
283 if (!(sdd->state & RXBUSY))
284 complete(&sdd->xfer_completion);
285 }
Boojin Kim39d3e802011-09-02 09:44:41 +0900286
287 spin_unlock_irqrestore(&sdd->lock, flags);
288}
289
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900290static void prepare_dma(struct s3c64xx_spi_dma_data *dma,
291 unsigned len, dma_addr_t buf)
Boojin Kim39d3e802011-09-02 09:44:41 +0900292{
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900293 struct s3c64xx_spi_driver_data *sdd;
Boojin Kim4969c322012-06-19 13:27:03 +0900294 struct samsung_dma_prep info;
295 struct samsung_dma_config config;
Boojin Kim39d3e802011-09-02 09:44:41 +0900296
Boojin Kim4969c322012-06-19 13:27:03 +0900297 if (dma->direction == DMA_DEV_TO_MEM) {
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900298 sdd = container_of((void *)dma,
299 struct s3c64xx_spi_driver_data, rx_dma);
Boojin Kim4969c322012-06-19 13:27:03 +0900300 config.direction = sdd->rx_dma.direction;
301 config.fifo = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
302 config.width = sdd->cur_bpw / 8;
303 sdd->ops->config(sdd->rx_dma.ch, &config);
304 } else {
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900305 sdd = container_of((void *)dma,
306 struct s3c64xx_spi_driver_data, tx_dma);
Boojin Kim4969c322012-06-19 13:27:03 +0900307 config.direction = sdd->tx_dma.direction;
308 config.fifo = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
309 config.width = sdd->cur_bpw / 8;
310 sdd->ops->config(sdd->tx_dma.ch, &config);
311 }
Boojin Kim39d3e802011-09-02 09:44:41 +0900312
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900313 info.cap = DMA_SLAVE;
314 info.len = len;
315 info.fp = s3c64xx_spi_dmacb;
316 info.fp_param = dma;
317 info.direction = dma->direction;
318 info.buf = buf;
Boojin Kim39d3e802011-09-02 09:44:41 +0900319
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900320 sdd->ops->prepare(dma->ch, &info);
321 sdd->ops->trigger(dma->ch);
322}
323
324static int acquire_dma(struct s3c64xx_spi_driver_data *sdd)
325{
Boojin Kim4969c322012-06-19 13:27:03 +0900326 struct samsung_dma_req req;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900327
328 sdd->ops = samsung_dma_get_ops();
329
Boojin Kim4969c322012-06-19 13:27:03 +0900330 req.cap = DMA_SLAVE;
331 req.client = &s3c64xx_spi_dma_client;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900332
Thomas Abraham2b908072012-07-13 07:15:15 +0900333 req.dt_dmach_prop = sdd->rx_dma.dma_prop;
Boojin Kim4969c322012-06-19 13:27:03 +0900334 sdd->rx_dma.ch = sdd->ops->request(sdd->rx_dma.dmach, &req);
Thomas Abraham2b908072012-07-13 07:15:15 +0900335 req.dt_dmach_prop = sdd->tx_dma.dma_prop;
Boojin Kim4969c322012-06-19 13:27:03 +0900336 sdd->tx_dma.ch = sdd->ops->request(sdd->tx_dma.dmach, &req);
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900337
338 return 1;
Boojin Kim39d3e802011-09-02 09:44:41 +0900339}
340
Jassi Brar230d42d2009-11-30 07:39:42 +0000341static void enable_datapath(struct s3c64xx_spi_driver_data *sdd,
342 struct spi_device *spi,
343 struct spi_transfer *xfer, int dma_mode)
344{
Jassi Brar230d42d2009-11-30 07:39:42 +0000345 void __iomem *regs = sdd->regs;
346 u32 modecfg, chcfg;
347
348 modecfg = readl(regs + S3C64XX_SPI_MODE_CFG);
349 modecfg &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
350
351 chcfg = readl(regs + S3C64XX_SPI_CH_CFG);
352 chcfg &= ~S3C64XX_SPI_CH_TXCH_ON;
353
354 if (dma_mode) {
355 chcfg &= ~S3C64XX_SPI_CH_RXCH_ON;
356 } else {
357 /* Always shift in data in FIFO, even if xfer is Tx only,
358 * this helps setting PCKT_CNT value for generating clocks
359 * as exactly needed.
360 */
361 chcfg |= S3C64XX_SPI_CH_RXCH_ON;
362 writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
363 | S3C64XX_SPI_PACKET_CNT_EN,
364 regs + S3C64XX_SPI_PACKET_CNT);
365 }
366
367 if (xfer->tx_buf != NULL) {
368 sdd->state |= TXBUSY;
369 chcfg |= S3C64XX_SPI_CH_TXCH_ON;
370 if (dma_mode) {
371 modecfg |= S3C64XX_SPI_MODE_TXDMA_ON;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900372 prepare_dma(&sdd->tx_dma, xfer->len, xfer->tx_dma);
Jassi Brar230d42d2009-11-30 07:39:42 +0000373 } else {
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900374 switch (sdd->cur_bpw) {
375 case 32:
376 iowrite32_rep(regs + S3C64XX_SPI_TX_DATA,
377 xfer->tx_buf, xfer->len / 4);
378 break;
379 case 16:
380 iowrite16_rep(regs + S3C64XX_SPI_TX_DATA,
381 xfer->tx_buf, xfer->len / 2);
382 break;
383 default:
384 iowrite8_rep(regs + S3C64XX_SPI_TX_DATA,
385 xfer->tx_buf, xfer->len);
386 break;
387 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000388 }
389 }
390
391 if (xfer->rx_buf != NULL) {
392 sdd->state |= RXBUSY;
393
Thomas Abrahama5238e32012-07-13 07:15:14 +0900394 if (sdd->port_conf->high_speed && sdd->cur_speed >= 30000000UL
Jassi Brar230d42d2009-11-30 07:39:42 +0000395 && !(sdd->cur_mode & SPI_CPHA))
396 chcfg |= S3C64XX_SPI_CH_HS_EN;
397
398 if (dma_mode) {
399 modecfg |= S3C64XX_SPI_MODE_RXDMA_ON;
400 chcfg |= S3C64XX_SPI_CH_RXCH_ON;
401 writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
402 | S3C64XX_SPI_PACKET_CNT_EN,
403 regs + S3C64XX_SPI_PACKET_CNT);
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900404 prepare_dma(&sdd->rx_dma, xfer->len, xfer->rx_dma);
Jassi Brar230d42d2009-11-30 07:39:42 +0000405 }
406 }
407
408 writel(modecfg, regs + S3C64XX_SPI_MODE_CFG);
409 writel(chcfg, regs + S3C64XX_SPI_CH_CFG);
410}
411
412static inline void enable_cs(struct s3c64xx_spi_driver_data *sdd,
413 struct spi_device *spi)
414{
415 struct s3c64xx_spi_csinfo *cs;
416
417 if (sdd->tgl_spi != NULL) { /* If last device toggled after mssg */
418 if (sdd->tgl_spi != spi) { /* if last mssg on diff device */
419 /* Deselect the last toggled device */
420 cs = sdd->tgl_spi->controller_data;
Thomas Abraham1c20c202012-07-13 07:15:14 +0900421 gpio_set_value(cs->line,
422 spi->mode & SPI_CS_HIGH ? 0 : 1);
Jassi Brar230d42d2009-11-30 07:39:42 +0000423 }
424 sdd->tgl_spi = NULL;
425 }
426
427 cs = spi->controller_data;
Thomas Abraham1c20c202012-07-13 07:15:14 +0900428 gpio_set_value(cs->line, spi->mode & SPI_CS_HIGH ? 1 : 0);
Jassi Brar230d42d2009-11-30 07:39:42 +0000429}
430
431static int wait_for_xfer(struct s3c64xx_spi_driver_data *sdd,
432 struct spi_transfer *xfer, int dma_mode)
433{
Jassi Brar230d42d2009-11-30 07:39:42 +0000434 void __iomem *regs = sdd->regs;
435 unsigned long val;
436 int ms;
437
438 /* millisecs to xfer 'len' bytes @ 'cur_speed' */
439 ms = xfer->len * 8 * 1000 / sdd->cur_speed;
Mark Brown9d8f86b2010-09-07 16:37:52 +0100440 ms += 10; /* some tolerance */
Jassi Brar230d42d2009-11-30 07:39:42 +0000441
442 if (dma_mode) {
443 val = msecs_to_jiffies(ms) + 10;
444 val = wait_for_completion_timeout(&sdd->xfer_completion, val);
445 } else {
Jassi Brarc3f139b2010-09-03 10:36:46 +0900446 u32 status;
Jassi Brar230d42d2009-11-30 07:39:42 +0000447 val = msecs_to_loops(ms);
448 do {
Jassi Brarc3f139b2010-09-03 10:36:46 +0900449 status = readl(regs + S3C64XX_SPI_STATUS);
Thomas Abrahama5238e32012-07-13 07:15:14 +0900450 } while (RX_FIFO_LVL(status, sdd) < xfer->len && --val);
Jassi Brar230d42d2009-11-30 07:39:42 +0000451 }
452
453 if (!val)
454 return -EIO;
455
456 if (dma_mode) {
457 u32 status;
458
459 /*
460 * DmaTx returns after simply writing data in the FIFO,
461 * w/o waiting for real transmission on the bus to finish.
462 * DmaRx returns only after Dma read data from FIFO which
463 * needs bus transmission to finish, so we don't worry if
464 * Xfer involved Rx(with or without Tx).
465 */
466 if (xfer->rx_buf == NULL) {
467 val = msecs_to_loops(10);
468 status = readl(regs + S3C64XX_SPI_STATUS);
Thomas Abrahama5238e32012-07-13 07:15:14 +0900469 while ((TX_FIFO_LVL(status, sdd)
470 || !S3C64XX_SPI_ST_TX_DONE(status, sdd))
Jassi Brar230d42d2009-11-30 07:39:42 +0000471 && --val) {
472 cpu_relax();
473 status = readl(regs + S3C64XX_SPI_STATUS);
474 }
475
476 if (!val)
477 return -EIO;
478 }
479 } else {
Jassi Brar230d42d2009-11-30 07:39:42 +0000480 /* If it was only Tx */
481 if (xfer->rx_buf == NULL) {
482 sdd->state &= ~TXBUSY;
483 return 0;
484 }
485
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900486 switch (sdd->cur_bpw) {
487 case 32:
488 ioread32_rep(regs + S3C64XX_SPI_RX_DATA,
489 xfer->rx_buf, xfer->len / 4);
490 break;
491 case 16:
492 ioread16_rep(regs + S3C64XX_SPI_RX_DATA,
493 xfer->rx_buf, xfer->len / 2);
494 break;
495 default:
496 ioread8_rep(regs + S3C64XX_SPI_RX_DATA,
497 xfer->rx_buf, xfer->len);
498 break;
499 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000500 sdd->state &= ~RXBUSY;
501 }
502
503 return 0;
504}
505
506static inline void disable_cs(struct s3c64xx_spi_driver_data *sdd,
507 struct spi_device *spi)
508{
509 struct s3c64xx_spi_csinfo *cs = spi->controller_data;
510
511 if (sdd->tgl_spi == spi)
512 sdd->tgl_spi = NULL;
513
Thomas Abraham1c20c202012-07-13 07:15:14 +0900514 gpio_set_value(cs->line, spi->mode & SPI_CS_HIGH ? 0 : 1);
Jassi Brar230d42d2009-11-30 07:39:42 +0000515}
516
517static void s3c64xx_spi_config(struct s3c64xx_spi_driver_data *sdd)
518{
Jassi Brar230d42d2009-11-30 07:39:42 +0000519 void __iomem *regs = sdd->regs;
520 u32 val;
521
522 /* Disable Clock */
Thomas Abrahama5238e32012-07-13 07:15:14 +0900523 if (sdd->port_conf->clk_from_cmu) {
Jassi Brarb42a81c2010-09-29 17:31:33 +0900524 clk_disable(sdd->src_clk);
525 } else {
526 val = readl(regs + S3C64XX_SPI_CLK_CFG);
527 val &= ~S3C64XX_SPI_ENCLK_ENABLE;
528 writel(val, regs + S3C64XX_SPI_CLK_CFG);
529 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000530
531 /* Set Polarity and Phase */
532 val = readl(regs + S3C64XX_SPI_CH_CFG);
533 val &= ~(S3C64XX_SPI_CH_SLAVE |
534 S3C64XX_SPI_CPOL_L |
535 S3C64XX_SPI_CPHA_B);
536
537 if (sdd->cur_mode & SPI_CPOL)
538 val |= S3C64XX_SPI_CPOL_L;
539
540 if (sdd->cur_mode & SPI_CPHA)
541 val |= S3C64XX_SPI_CPHA_B;
542
543 writel(val, regs + S3C64XX_SPI_CH_CFG);
544
545 /* Set Channel & DMA Mode */
546 val = readl(regs + S3C64XX_SPI_MODE_CFG);
547 val &= ~(S3C64XX_SPI_MODE_BUS_TSZ_MASK
548 | S3C64XX_SPI_MODE_CH_TSZ_MASK);
549
550 switch (sdd->cur_bpw) {
551 case 32:
552 val |= S3C64XX_SPI_MODE_BUS_TSZ_WORD;
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900553 val |= S3C64XX_SPI_MODE_CH_TSZ_WORD;
Jassi Brar230d42d2009-11-30 07:39:42 +0000554 break;
555 case 16:
556 val |= S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD;
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900557 val |= S3C64XX_SPI_MODE_CH_TSZ_HALFWORD;
Jassi Brar230d42d2009-11-30 07:39:42 +0000558 break;
559 default:
560 val |= S3C64XX_SPI_MODE_BUS_TSZ_BYTE;
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900561 val |= S3C64XX_SPI_MODE_CH_TSZ_BYTE;
Jassi Brar230d42d2009-11-30 07:39:42 +0000562 break;
563 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000564
565 writel(val, regs + S3C64XX_SPI_MODE_CFG);
566
Thomas Abrahama5238e32012-07-13 07:15:14 +0900567 if (sdd->port_conf->clk_from_cmu) {
Jassi Brarb42a81c2010-09-29 17:31:33 +0900568 /* Configure Clock */
569 /* There is half-multiplier before the SPI */
570 clk_set_rate(sdd->src_clk, sdd->cur_speed * 2);
571 /* Enable Clock */
572 clk_enable(sdd->src_clk);
573 } else {
574 /* Configure Clock */
575 val = readl(regs + S3C64XX_SPI_CLK_CFG);
576 val &= ~S3C64XX_SPI_PSR_MASK;
577 val |= ((clk_get_rate(sdd->src_clk) / sdd->cur_speed / 2 - 1)
578 & S3C64XX_SPI_PSR_MASK);
579 writel(val, regs + S3C64XX_SPI_CLK_CFG);
Jassi Brar230d42d2009-11-30 07:39:42 +0000580
Jassi Brarb42a81c2010-09-29 17:31:33 +0900581 /* Enable Clock */
582 val = readl(regs + S3C64XX_SPI_CLK_CFG);
583 val |= S3C64XX_SPI_ENCLK_ENABLE;
584 writel(val, regs + S3C64XX_SPI_CLK_CFG);
585 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000586}
587
Jassi Brar230d42d2009-11-30 07:39:42 +0000588#define XFER_DMAADDR_INVALID DMA_BIT_MASK(32)
589
590static int s3c64xx_spi_map_mssg(struct s3c64xx_spi_driver_data *sdd,
591 struct spi_message *msg)
592{
593 struct device *dev = &sdd->pdev->dev;
594 struct spi_transfer *xfer;
595
596 if (msg->is_dma_mapped)
597 return 0;
598
599 /* First mark all xfer unmapped */
600 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
601 xfer->rx_dma = XFER_DMAADDR_INVALID;
602 xfer->tx_dma = XFER_DMAADDR_INVALID;
603 }
604
605 /* Map until end or first fail */
606 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
607
Thomas Abrahama5238e32012-07-13 07:15:14 +0900608 if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
Jassi Brare02ddd42010-09-29 17:31:31 +0900609 continue;
610
Jassi Brar230d42d2009-11-30 07:39:42 +0000611 if (xfer->tx_buf != NULL) {
Jassi Brar251ee472010-09-03 10:36:26 +0900612 xfer->tx_dma = dma_map_single(dev,
613 (void *)xfer->tx_buf, xfer->len,
614 DMA_TO_DEVICE);
Jassi Brar230d42d2009-11-30 07:39:42 +0000615 if (dma_mapping_error(dev, xfer->tx_dma)) {
616 dev_err(dev, "dma_map_single Tx failed\n");
617 xfer->tx_dma = XFER_DMAADDR_INVALID;
618 return -ENOMEM;
619 }
620 }
621
622 if (xfer->rx_buf != NULL) {
623 xfer->rx_dma = dma_map_single(dev, xfer->rx_buf,
624 xfer->len, DMA_FROM_DEVICE);
625 if (dma_mapping_error(dev, xfer->rx_dma)) {
626 dev_err(dev, "dma_map_single Rx failed\n");
627 dma_unmap_single(dev, xfer->tx_dma,
628 xfer->len, DMA_TO_DEVICE);
629 xfer->tx_dma = XFER_DMAADDR_INVALID;
630 xfer->rx_dma = XFER_DMAADDR_INVALID;
631 return -ENOMEM;
632 }
633 }
634 }
635
636 return 0;
637}
638
639static void s3c64xx_spi_unmap_mssg(struct s3c64xx_spi_driver_data *sdd,
640 struct spi_message *msg)
641{
642 struct device *dev = &sdd->pdev->dev;
643 struct spi_transfer *xfer;
644
645 if (msg->is_dma_mapped)
646 return;
647
648 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
649
Thomas Abrahama5238e32012-07-13 07:15:14 +0900650 if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
Jassi Brare02ddd42010-09-29 17:31:31 +0900651 continue;
652
Jassi Brar230d42d2009-11-30 07:39:42 +0000653 if (xfer->rx_buf != NULL
654 && xfer->rx_dma != XFER_DMAADDR_INVALID)
655 dma_unmap_single(dev, xfer->rx_dma,
656 xfer->len, DMA_FROM_DEVICE);
657
658 if (xfer->tx_buf != NULL
659 && xfer->tx_dma != XFER_DMAADDR_INVALID)
660 dma_unmap_single(dev, xfer->tx_dma,
661 xfer->len, DMA_TO_DEVICE);
662 }
663}
664
Mark Brownad2a99a2012-02-15 14:48:32 -0800665static int s3c64xx_spi_transfer_one_message(struct spi_master *master,
666 struct spi_message *msg)
Jassi Brar230d42d2009-11-30 07:39:42 +0000667{
Mark Brownad2a99a2012-02-15 14:48:32 -0800668 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Jassi Brar230d42d2009-11-30 07:39:42 +0000669 struct spi_device *spi = msg->spi;
670 struct s3c64xx_spi_csinfo *cs = spi->controller_data;
671 struct spi_transfer *xfer;
672 int status = 0, cs_toggle = 0;
673 u32 speed;
674 u8 bpw;
675
676 /* If Master's(controller) state differs from that needed by Slave */
677 if (sdd->cur_speed != spi->max_speed_hz
678 || sdd->cur_mode != spi->mode
679 || sdd->cur_bpw != spi->bits_per_word) {
680 sdd->cur_bpw = spi->bits_per_word;
681 sdd->cur_speed = spi->max_speed_hz;
682 sdd->cur_mode = spi->mode;
683 s3c64xx_spi_config(sdd);
684 }
685
686 /* Map all the transfers if needed */
687 if (s3c64xx_spi_map_mssg(sdd, msg)) {
688 dev_err(&spi->dev,
689 "Xfer: Unable to map message buffers!\n");
690 status = -ENOMEM;
691 goto out;
692 }
693
694 /* Configure feedback delay */
695 writel(cs->fb_delay & 0x3, sdd->regs + S3C64XX_SPI_FB_CLK);
696
697 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
698
699 unsigned long flags;
700 int use_dma;
701
702 INIT_COMPLETION(sdd->xfer_completion);
703
704 /* Only BPW and Speed may change across transfers */
705 bpw = xfer->bits_per_word ? : spi->bits_per_word;
706 speed = xfer->speed_hz ? : spi->max_speed_hz;
707
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900708 if (xfer->len % (bpw / 8)) {
709 dev_err(&spi->dev,
710 "Xfer length(%u) not a multiple of word size(%u)\n",
711 xfer->len, bpw / 8);
712 status = -EIO;
713 goto out;
714 }
715
Jassi Brar230d42d2009-11-30 07:39:42 +0000716 if (bpw != sdd->cur_bpw || speed != sdd->cur_speed) {
717 sdd->cur_bpw = bpw;
718 sdd->cur_speed = speed;
719 s3c64xx_spi_config(sdd);
720 }
721
722 /* Polling method for xfers not bigger than FIFO capacity */
Thomas Abrahama5238e32012-07-13 07:15:14 +0900723 if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
Jassi Brar230d42d2009-11-30 07:39:42 +0000724 use_dma = 0;
725 else
726 use_dma = 1;
727
728 spin_lock_irqsave(&sdd->lock, flags);
729
730 /* Pending only which is to be done */
731 sdd->state &= ~RXBUSY;
732 sdd->state &= ~TXBUSY;
733
734 enable_datapath(sdd, spi, xfer, use_dma);
735
736 /* Slave Select */
737 enable_cs(sdd, spi);
738
739 /* Start the signals */
740 S3C64XX_SPI_ACT(sdd);
741
742 spin_unlock_irqrestore(&sdd->lock, flags);
743
744 status = wait_for_xfer(sdd, xfer, use_dma);
745
746 /* Quiese the signals */
747 S3C64XX_SPI_DEACT(sdd);
748
749 if (status) {
Joe Perches8a349d42010-02-02 07:22:13 +0000750 dev_err(&spi->dev, "I/O Error: "
751 "rx-%d tx-%d res:rx-%c tx-%c len-%d\n",
Jassi Brar230d42d2009-11-30 07:39:42 +0000752 xfer->rx_buf ? 1 : 0, xfer->tx_buf ? 1 : 0,
753 (sdd->state & RXBUSY) ? 'f' : 'p',
754 (sdd->state & TXBUSY) ? 'f' : 'p',
755 xfer->len);
756
757 if (use_dma) {
758 if (xfer->tx_buf != NULL
759 && (sdd->state & TXBUSY))
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900760 sdd->ops->stop(sdd->tx_dma.ch);
Jassi Brar230d42d2009-11-30 07:39:42 +0000761 if (xfer->rx_buf != NULL
762 && (sdd->state & RXBUSY))
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900763 sdd->ops->stop(sdd->rx_dma.ch);
Jassi Brar230d42d2009-11-30 07:39:42 +0000764 }
765
766 goto out;
767 }
768
769 if (xfer->delay_usecs)
770 udelay(xfer->delay_usecs);
771
772 if (xfer->cs_change) {
773 /* Hint that the next mssg is gonna be
774 for the same device */
775 if (list_is_last(&xfer->transfer_list,
776 &msg->transfers))
777 cs_toggle = 1;
778 else
779 disable_cs(sdd, spi);
780 }
781
782 msg->actual_length += xfer->len;
783
784 flush_fifo(sdd);
785 }
786
787out:
788 if (!cs_toggle || status)
789 disable_cs(sdd, spi);
790 else
791 sdd->tgl_spi = spi;
792
793 s3c64xx_spi_unmap_mssg(sdd, msg);
794
795 msg->status = status;
796
Mark Brownad2a99a2012-02-15 14:48:32 -0800797 spi_finalize_current_message(master);
798
799 return 0;
Jassi Brar230d42d2009-11-30 07:39:42 +0000800}
801
Mark Brownad2a99a2012-02-15 14:48:32 -0800802static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
Jassi Brar230d42d2009-11-30 07:39:42 +0000803{
Mark Brownad2a99a2012-02-15 14:48:32 -0800804 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
Jassi Brar230d42d2009-11-30 07:39:42 +0000805
806 /* Acquire DMA channels */
807 while (!acquire_dma(sdd))
808 msleep(10);
809
Mark Brownb97b6622011-12-04 00:58:06 +0000810 pm_runtime_get_sync(&sdd->pdev->dev);
811
Mark Brownad2a99a2012-02-15 14:48:32 -0800812 return 0;
813}
Jassi Brar230d42d2009-11-30 07:39:42 +0000814
Mark Brownad2a99a2012-02-15 14:48:32 -0800815static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
816{
817 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
Jassi Brar230d42d2009-11-30 07:39:42 +0000818
819 /* Free DMA channels */
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900820 sdd->ops->release(sdd->rx_dma.ch, &s3c64xx_spi_dma_client);
821 sdd->ops->release(sdd->tx_dma.ch, &s3c64xx_spi_dma_client);
Mark Brownb97b6622011-12-04 00:58:06 +0000822
823 pm_runtime_put(&sdd->pdev->dev);
Jassi Brar230d42d2009-11-30 07:39:42 +0000824
825 return 0;
826}
827
Thomas Abraham2b908072012-07-13 07:15:15 +0900828static struct s3c64xx_spi_csinfo *s3c64xx_get_slave_ctrldata(
829 struct s3c64xx_spi_driver_data *sdd,
830 struct spi_device *spi)
831{
832 struct s3c64xx_spi_csinfo *cs;
833 struct device_node *slave_np, *data_np;
834 u32 fb_delay = 0;
835
836 slave_np = spi->dev.of_node;
837 if (!slave_np) {
838 dev_err(&spi->dev, "device node not found\n");
839 return ERR_PTR(-EINVAL);
840 }
841
842 for_each_child_of_node(slave_np, data_np)
843 if (!strcmp(data_np->name, "controller-data"))
844 break;
845 if (!data_np) {
846 dev_err(&spi->dev, "child node 'controller-data' not found\n");
847 return ERR_PTR(-EINVAL);
848 }
849
850 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
851 if (!cs) {
852 dev_err(&spi->dev, "could not allocate memory for controller"
853 " data\n");
854 return ERR_PTR(-ENOMEM);
855 }
856
857 cs->line = of_get_named_gpio(data_np, "cs-gpio", 0);
858 if (!gpio_is_valid(cs->line)) {
859 dev_err(&spi->dev, "chip select gpio is not specified or "
860 "invalid\n");
861 kfree(cs);
862 return ERR_PTR(-EINVAL);
863 }
864
865 of_property_read_u32(data_np, "samsung,spi-feedback-delay", &fb_delay);
866 cs->fb_delay = fb_delay;
867 return cs;
868}
869
Jassi Brar230d42d2009-11-30 07:39:42 +0000870/*
871 * Here we only check the validity of requested configuration
872 * and save the configuration in a local data-structure.
873 * The controller is actually configured only just before we
874 * get a message to transfer.
875 */
876static int s3c64xx_spi_setup(struct spi_device *spi)
877{
878 struct s3c64xx_spi_csinfo *cs = spi->controller_data;
879 struct s3c64xx_spi_driver_data *sdd;
Jassi Brarad7de722010-01-20 13:49:44 -0700880 struct s3c64xx_spi_info *sci;
Jassi Brar230d42d2009-11-30 07:39:42 +0000881 struct spi_message *msg;
Jassi Brar230d42d2009-11-30 07:39:42 +0000882 unsigned long flags;
Thomas Abraham2b908072012-07-13 07:15:15 +0900883 int err;
Jassi Brar230d42d2009-11-30 07:39:42 +0000884
Thomas Abraham2b908072012-07-13 07:15:15 +0900885 sdd = spi_master_get_devdata(spi->master);
886 if (!cs && spi->dev.of_node) {
887 cs = s3c64xx_get_slave_ctrldata(sdd, spi);
888 spi->controller_data = cs;
889 }
890
891 if (IS_ERR_OR_NULL(cs)) {
Jassi Brar230d42d2009-11-30 07:39:42 +0000892 dev_err(&spi->dev, "No CS for SPI(%d)\n", spi->chip_select);
893 return -ENODEV;
894 }
895
Thomas Abraham1c20c202012-07-13 07:15:14 +0900896 if (!spi_get_ctldata(spi)) {
Mark Brown707214d2012-07-19 14:36:16 +0900897 err = gpio_request_one(cs->line, GPIOF_OUT_INIT_HIGH,
898 dev_name(&spi->dev));
Thomas Abraham1c20c202012-07-13 07:15:14 +0900899 if (err) {
Mark Brown49f3eac2012-07-19 14:36:13 +0900900 dev_err(&spi->dev,
901 "Failed to get /CS gpio [%d]: %d\n",
902 cs->line, err);
Thomas Abraham2b908072012-07-13 07:15:15 +0900903 goto err_gpio_req;
Thomas Abraham1c20c202012-07-13 07:15:14 +0900904 }
905 spi_set_ctldata(spi, cs);
906 }
907
Jassi Brar230d42d2009-11-30 07:39:42 +0000908 sci = sdd->cntrlr_info;
909
910 spin_lock_irqsave(&sdd->lock, flags);
911
912 list_for_each_entry(msg, &sdd->queue, queue) {
913 /* Is some mssg is already queued for this device */
914 if (msg->spi == spi) {
915 dev_err(&spi->dev,
916 "setup: attempt while mssg in queue!\n");
917 spin_unlock_irqrestore(&sdd->lock, flags);
Thomas Abraham2b908072012-07-13 07:15:15 +0900918 err = -EBUSY;
919 goto err_msgq;
Jassi Brar230d42d2009-11-30 07:39:42 +0000920 }
921 }
922
Jassi Brar230d42d2009-11-30 07:39:42 +0000923 spin_unlock_irqrestore(&sdd->lock, flags);
924
925 if (spi->bits_per_word != 8
926 && spi->bits_per_word != 16
927 && spi->bits_per_word != 32) {
928 dev_err(&spi->dev, "setup: %dbits/wrd not supported!\n",
929 spi->bits_per_word);
930 err = -EINVAL;
931 goto setup_exit;
932 }
933
Mark Brownb97b6622011-12-04 00:58:06 +0000934 pm_runtime_get_sync(&sdd->pdev->dev);
935
Jassi Brar230d42d2009-11-30 07:39:42 +0000936 /* Check if we can provide the requested rate */
Thomas Abrahama5238e32012-07-13 07:15:14 +0900937 if (!sdd->port_conf->clk_from_cmu) {
Jassi Brarb42a81c2010-09-29 17:31:33 +0900938 u32 psr, speed;
Jassi Brar230d42d2009-11-30 07:39:42 +0000939
Jassi Brarb42a81c2010-09-29 17:31:33 +0900940 /* Max possible */
941 speed = clk_get_rate(sdd->src_clk) / 2 / (0 + 1);
Jassi Brar230d42d2009-11-30 07:39:42 +0000942
Jassi Brarb42a81c2010-09-29 17:31:33 +0900943 if (spi->max_speed_hz > speed)
944 spi->max_speed_hz = speed;
Jassi Brar230d42d2009-11-30 07:39:42 +0000945
Jassi Brarb42a81c2010-09-29 17:31:33 +0900946 psr = clk_get_rate(sdd->src_clk) / 2 / spi->max_speed_hz - 1;
947 psr &= S3C64XX_SPI_PSR_MASK;
948 if (psr == S3C64XX_SPI_PSR_MASK)
949 psr--;
950
951 speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
952 if (spi->max_speed_hz < speed) {
953 if (psr+1 < S3C64XX_SPI_PSR_MASK) {
954 psr++;
955 } else {
956 err = -EINVAL;
957 goto setup_exit;
958 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000959 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000960
Jassi Brarb42a81c2010-09-29 17:31:33 +0900961 speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
Thomas Abraham2b908072012-07-13 07:15:15 +0900962 if (spi->max_speed_hz >= speed) {
Jassi Brarb42a81c2010-09-29 17:31:33 +0900963 spi->max_speed_hz = speed;
Thomas Abraham2b908072012-07-13 07:15:15 +0900964 } else {
Jassi Brarb42a81c2010-09-29 17:31:33 +0900965 err = -EINVAL;
Thomas Abraham2b908072012-07-13 07:15:15 +0900966 goto setup_exit;
967 }
Jassi Brarb42a81c2010-09-29 17:31:33 +0900968 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000969
Mark Brownb97b6622011-12-04 00:58:06 +0000970 pm_runtime_put(&sdd->pdev->dev);
Thomas Abraham2b908072012-07-13 07:15:15 +0900971 disable_cs(sdd, spi);
972 return 0;
Mark Brownb97b6622011-12-04 00:58:06 +0000973
Jassi Brar230d42d2009-11-30 07:39:42 +0000974setup_exit:
Jassi Brar230d42d2009-11-30 07:39:42 +0000975 /* setup() returns with device de-selected */
976 disable_cs(sdd, spi);
977
Thomas Abraham2b908072012-07-13 07:15:15 +0900978err_msgq:
979 gpio_free(cs->line);
980 spi_set_ctldata(spi, NULL);
981
982err_gpio_req:
983 kfree(cs);
984
Jassi Brar230d42d2009-11-30 07:39:42 +0000985 return err;
986}
987
Thomas Abraham1c20c202012-07-13 07:15:14 +0900988static void s3c64xx_spi_cleanup(struct spi_device *spi)
989{
990 struct s3c64xx_spi_csinfo *cs = spi_get_ctldata(spi);
991
Thomas Abraham2b908072012-07-13 07:15:15 +0900992 if (cs) {
Thomas Abraham1c20c202012-07-13 07:15:14 +0900993 gpio_free(cs->line);
Thomas Abraham2b908072012-07-13 07:15:15 +0900994 if (spi->dev.of_node)
995 kfree(cs);
996 }
Thomas Abraham1c20c202012-07-13 07:15:14 +0900997 spi_set_ctldata(spi, NULL);
998}
999
Mark Brownc2573122011-11-10 10:57:32 +00001000static irqreturn_t s3c64xx_spi_irq(int irq, void *data)
1001{
1002 struct s3c64xx_spi_driver_data *sdd = data;
1003 struct spi_master *spi = sdd->master;
1004 unsigned int val;
1005
1006 val = readl(sdd->regs + S3C64XX_SPI_PENDING_CLR);
1007
1008 val &= S3C64XX_SPI_PND_RX_OVERRUN_CLR |
1009 S3C64XX_SPI_PND_RX_UNDERRUN_CLR |
1010 S3C64XX_SPI_PND_TX_OVERRUN_CLR |
1011 S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
1012
1013 writel(val, sdd->regs + S3C64XX_SPI_PENDING_CLR);
1014
1015 if (val & S3C64XX_SPI_PND_RX_OVERRUN_CLR)
1016 dev_err(&spi->dev, "RX overrun\n");
1017 if (val & S3C64XX_SPI_PND_RX_UNDERRUN_CLR)
1018 dev_err(&spi->dev, "RX underrun\n");
1019 if (val & S3C64XX_SPI_PND_TX_OVERRUN_CLR)
1020 dev_err(&spi->dev, "TX overrun\n");
1021 if (val & S3C64XX_SPI_PND_TX_UNDERRUN_CLR)
1022 dev_err(&spi->dev, "TX underrun\n");
1023
1024 return IRQ_HANDLED;
1025}
1026
Jassi Brar230d42d2009-11-30 07:39:42 +00001027static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data *sdd, int channel)
1028{
Jassi Brarad7de722010-01-20 13:49:44 -07001029 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +00001030 void __iomem *regs = sdd->regs;
1031 unsigned int val;
1032
1033 sdd->cur_speed = 0;
1034
1035 S3C64XX_SPI_DEACT(sdd);
1036
1037 /* Disable Interrupts - we use Polling if not DMA mode */
1038 writel(0, regs + S3C64XX_SPI_INT_EN);
1039
Thomas Abrahama5238e32012-07-13 07:15:14 +09001040 if (!sdd->port_conf->clk_from_cmu)
Jassi Brarb42a81c2010-09-29 17:31:33 +09001041 writel(sci->src_clk_nr << S3C64XX_SPI_CLKSEL_SRCSHFT,
Jassi Brar230d42d2009-11-30 07:39:42 +00001042 regs + S3C64XX_SPI_CLK_CFG);
1043 writel(0, regs + S3C64XX_SPI_MODE_CFG);
1044 writel(0, regs + S3C64XX_SPI_PACKET_CNT);
1045
1046 /* Clear any irq pending bits */
1047 writel(readl(regs + S3C64XX_SPI_PENDING_CLR),
1048 regs + S3C64XX_SPI_PENDING_CLR);
1049
1050 writel(0, regs + S3C64XX_SPI_SWAP_CFG);
1051
1052 val = readl(regs + S3C64XX_SPI_MODE_CFG);
1053 val &= ~S3C64XX_SPI_MODE_4BURST;
1054 val &= ~(S3C64XX_SPI_MAX_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
1055 val |= (S3C64XX_SPI_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
1056 writel(val, regs + S3C64XX_SPI_MODE_CFG);
1057
1058 flush_fifo(sdd);
1059}
1060
Thomas Abraham2b908072012-07-13 07:15:15 +09001061static int __devinit s3c64xx_spi_get_dmares(
1062 struct s3c64xx_spi_driver_data *sdd, bool tx)
1063{
1064 struct platform_device *pdev = sdd->pdev;
1065 struct s3c64xx_spi_dma_data *dma_data;
1066 struct property *prop;
1067 struct resource *res;
1068 char prop_name[15], *chan_str;
1069
1070 if (tx) {
1071 dma_data = &sdd->tx_dma;
1072 dma_data->direction = DMA_TO_DEVICE;
1073 chan_str = "tx";
1074 } else {
1075 dma_data = &sdd->rx_dma;
1076 dma_data->direction = DMA_FROM_DEVICE;
1077 chan_str = "rx";
1078 }
1079
1080 if (!sdd->pdev->dev.of_node) {
1081 res = platform_get_resource(pdev, IORESOURCE_DMA, tx ? 0 : 1);
1082 if (!res) {
1083 dev_err(&pdev->dev, "Unable to get SPI-%s dma "
1084 "resource\n", chan_str);
1085 return -ENXIO;
1086 }
1087 dma_data->dmach = res->start;
1088 return 0;
1089 }
1090
1091 sprintf(prop_name, "%s-dma-channel", chan_str);
1092 prop = of_find_property(pdev->dev.of_node, prop_name, NULL);
1093 if (!prop) {
1094 dev_err(&pdev->dev, "%s dma channel property not specified\n",
1095 chan_str);
1096 return -ENXIO;
1097 }
1098
1099 dma_data->dmach = DMACH_DT_PROP;
1100 dma_data->dma_prop = prop;
1101 return 0;
1102}
1103
1104#ifdef CONFIG_OF
1105static int s3c64xx_spi_parse_dt_gpio(struct s3c64xx_spi_driver_data *sdd)
1106{
1107 struct device *dev = &sdd->pdev->dev;
1108 int idx, gpio, ret;
1109
1110 /* find gpios for mosi, miso and clock lines */
1111 for (idx = 0; idx < 3; idx++) {
1112 gpio = of_get_gpio(dev->of_node, idx);
1113 if (!gpio_is_valid(gpio)) {
1114 dev_err(dev, "invalid gpio[%d]: %d\n", idx, gpio);
1115 goto free_gpio;
1116 }
1117
1118 ret = gpio_request(gpio, "spi-bus");
1119 if (ret) {
Mark Brown49f3eac2012-07-19 14:36:13 +09001120 dev_err(dev, "gpio [%d] request failed: %d\n",
1121 gpio, ret);
Thomas Abraham2b908072012-07-13 07:15:15 +09001122 goto free_gpio;
1123 }
1124 }
1125 return 0;
1126
1127free_gpio:
1128 while (--idx >= 0)
1129 gpio_free(sdd->gpios[idx]);
1130 return -EINVAL;
1131}
1132
1133static void s3c64xx_spi_dt_gpio_free(struct s3c64xx_spi_driver_data *sdd)
1134{
1135 unsigned int idx;
1136 for (idx = 0; idx < 3; idx++)
1137 gpio_free(sdd->gpios[idx]);
1138}
1139
1140static struct __devinit s3c64xx_spi_info * s3c64xx_spi_parse_dt(
1141 struct device *dev)
1142{
1143 struct s3c64xx_spi_info *sci;
1144 u32 temp;
1145
1146 sci = devm_kzalloc(dev, sizeof(*sci), GFP_KERNEL);
1147 if (!sci) {
1148 dev_err(dev, "memory allocation for spi_info failed\n");
1149 return ERR_PTR(-ENOMEM);
1150 }
1151
1152 if (of_property_read_u32(dev->of_node, "samsung,spi-src-clk", &temp)) {
1153 dev_warn(dev, "spi bus clock parent not specified, using "
1154 "clock at index 0 as parent\n");
1155 sci->src_clk_nr = 0;
1156 } else {
1157 sci->src_clk_nr = temp;
1158 }
1159
1160 if (of_property_read_u32(dev->of_node, "num-cs", &temp)) {
1161 dev_warn(dev, "number of chip select lines not specified, "
1162 "assuming 1 chip select line\n");
1163 sci->num_cs = 1;
1164 } else {
1165 sci->num_cs = temp;
1166 }
1167
1168 return sci;
1169}
1170#else
1171static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
1172{
1173 return dev->platform_data;
1174}
1175
1176static int s3c64xx_spi_parse_dt_gpio(struct s3c64xx_spi_driver_data *sdd)
1177{
1178 return -EINVAL;
1179}
1180
1181static void s3c64xx_spi_dt_gpio_free(struct s3c64xx_spi_driver_data *sdd)
1182{
1183}
1184#endif
1185
1186static const struct of_device_id s3c64xx_spi_dt_match[];
1187
Thomas Abrahama5238e32012-07-13 07:15:14 +09001188static inline struct s3c64xx_spi_port_config *s3c64xx_spi_get_port_config(
1189 struct platform_device *pdev)
1190{
Thomas Abraham2b908072012-07-13 07:15:15 +09001191#ifdef CONFIG_OF
1192 if (pdev->dev.of_node) {
1193 const struct of_device_id *match;
1194 match = of_match_node(s3c64xx_spi_dt_match, pdev->dev.of_node);
1195 return (struct s3c64xx_spi_port_config *)match->data;
1196 }
1197#endif
Thomas Abrahama5238e32012-07-13 07:15:14 +09001198 return (struct s3c64xx_spi_port_config *)
1199 platform_get_device_id(pdev)->driver_data;
1200}
1201
Jassi Brar230d42d2009-11-30 07:39:42 +00001202static int __init s3c64xx_spi_probe(struct platform_device *pdev)
1203{
Thomas Abraham2b908072012-07-13 07:15:15 +09001204 struct resource *mem_res;
Jassi Brar230d42d2009-11-30 07:39:42 +00001205 struct s3c64xx_spi_driver_data *sdd;
Thomas Abraham2b908072012-07-13 07:15:15 +09001206 struct s3c64xx_spi_info *sci = pdev->dev.platform_data;
Jassi Brar230d42d2009-11-30 07:39:42 +00001207 struct spi_master *master;
Mark Brownc2573122011-11-10 10:57:32 +00001208 int ret, irq;
Padmavathi Vennaa24d8502011-11-02 20:04:19 +09001209 char clk_name[16];
Jassi Brar230d42d2009-11-30 07:39:42 +00001210
Thomas Abraham2b908072012-07-13 07:15:15 +09001211 if (!sci && pdev->dev.of_node) {
1212 sci = s3c64xx_spi_parse_dt(&pdev->dev);
1213 if (IS_ERR(sci))
1214 return PTR_ERR(sci);
Jassi Brar230d42d2009-11-30 07:39:42 +00001215 }
1216
Thomas Abraham2b908072012-07-13 07:15:15 +09001217 if (!sci) {
Jassi Brar230d42d2009-11-30 07:39:42 +00001218 dev_err(&pdev->dev, "platform_data missing!\n");
1219 return -ENODEV;
1220 }
1221
Jassi Brar230d42d2009-11-30 07:39:42 +00001222 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1223 if (mem_res == NULL) {
1224 dev_err(&pdev->dev, "Unable to get SPI MEM resource\n");
1225 return -ENXIO;
1226 }
1227
Mark Brownc2573122011-11-10 10:57:32 +00001228 irq = platform_get_irq(pdev, 0);
1229 if (irq < 0) {
1230 dev_warn(&pdev->dev, "Failed to get IRQ: %d\n", irq);
1231 return irq;
1232 }
1233
Jassi Brar230d42d2009-11-30 07:39:42 +00001234 master = spi_alloc_master(&pdev->dev,
1235 sizeof(struct s3c64xx_spi_driver_data));
1236 if (master == NULL) {
1237 dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
1238 return -ENOMEM;
1239 }
1240
Jassi Brar230d42d2009-11-30 07:39:42 +00001241 platform_set_drvdata(pdev, master);
1242
1243 sdd = spi_master_get_devdata(master);
Thomas Abrahama5238e32012-07-13 07:15:14 +09001244 sdd->port_conf = s3c64xx_spi_get_port_config(pdev);
Jassi Brar230d42d2009-11-30 07:39:42 +00001245 sdd->master = master;
1246 sdd->cntrlr_info = sci;
1247 sdd->pdev = pdev;
1248 sdd->sfr_start = mem_res->start;
Thomas Abraham2b908072012-07-13 07:15:15 +09001249 if (pdev->dev.of_node) {
1250 ret = of_alias_get_id(pdev->dev.of_node, "spi");
1251 if (ret < 0) {
1252 dev_err(&pdev->dev, "failed to get alias id, "
1253 "errno %d\n", ret);
1254 goto err0;
1255 }
1256 sdd->port_id = ret;
1257 } else {
1258 sdd->port_id = pdev->id;
1259 }
Jassi Brar230d42d2009-11-30 07:39:42 +00001260
1261 sdd->cur_bpw = 8;
1262
Thomas Abraham2b908072012-07-13 07:15:15 +09001263 ret = s3c64xx_spi_get_dmares(sdd, true);
1264 if (ret)
1265 goto err0;
1266
1267 ret = s3c64xx_spi_get_dmares(sdd, false);
1268 if (ret)
1269 goto err0;
1270
1271 master->dev.of_node = pdev->dev.of_node;
Thomas Abrahama5238e32012-07-13 07:15:14 +09001272 master->bus_num = sdd->port_id;
Jassi Brar230d42d2009-11-30 07:39:42 +00001273 master->setup = s3c64xx_spi_setup;
Thomas Abraham1c20c202012-07-13 07:15:14 +09001274 master->cleanup = s3c64xx_spi_cleanup;
Mark Brownad2a99a2012-02-15 14:48:32 -08001275 master->prepare_transfer_hardware = s3c64xx_spi_prepare_transfer;
1276 master->transfer_one_message = s3c64xx_spi_transfer_one_message;
1277 master->unprepare_transfer_hardware = s3c64xx_spi_unprepare_transfer;
Jassi Brar230d42d2009-11-30 07:39:42 +00001278 master->num_chipselect = sci->num_cs;
1279 master->dma_alignment = 8;
1280 /* the spi->mode bits understood by this driver: */
1281 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1282
Mark Brownb1ec4302012-07-19 14:36:19 +09001283 sdd->regs = devm_request_and_ioremap(&pdev->dev, mem_res);
Jassi Brar230d42d2009-11-30 07:39:42 +00001284 if (sdd->regs == NULL) {
1285 dev_err(&pdev->dev, "Unable to remap IO\n");
1286 ret = -ENXIO;
1287 goto err1;
1288 }
1289
Thomas Abraham2b908072012-07-13 07:15:15 +09001290 if (!sci->cfg_gpio && pdev->dev.of_node) {
1291 if (s3c64xx_spi_parse_dt_gpio(sdd))
1292 return -EBUSY;
1293 } else if (sci->cfg_gpio == NULL || sci->cfg_gpio()) {
Jassi Brar230d42d2009-11-30 07:39:42 +00001294 dev_err(&pdev->dev, "Unable to config gpio\n");
1295 ret = -EBUSY;
1296 goto err2;
1297 }
1298
1299 /* Setup clocks */
1300 sdd->clk = clk_get(&pdev->dev, "spi");
1301 if (IS_ERR(sdd->clk)) {
1302 dev_err(&pdev->dev, "Unable to acquire clock 'spi'\n");
1303 ret = PTR_ERR(sdd->clk);
1304 goto err3;
1305 }
1306
1307 if (clk_enable(sdd->clk)) {
1308 dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n");
1309 ret = -EBUSY;
1310 goto err4;
1311 }
1312
Padmavathi Vennaa24d8502011-11-02 20:04:19 +09001313 sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr);
1314 sdd->src_clk = clk_get(&pdev->dev, clk_name);
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001315 if (IS_ERR(sdd->src_clk)) {
Jassi Brar230d42d2009-11-30 07:39:42 +00001316 dev_err(&pdev->dev,
Padmavathi Vennaa24d8502011-11-02 20:04:19 +09001317 "Unable to acquire clock '%s'\n", clk_name);
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001318 ret = PTR_ERR(sdd->src_clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001319 goto err5;
1320 }
1321
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001322 if (clk_enable(sdd->src_clk)) {
Padmavathi Vennaa24d8502011-11-02 20:04:19 +09001323 dev_err(&pdev->dev, "Couldn't enable clock '%s'\n", clk_name);
Jassi Brar230d42d2009-11-30 07:39:42 +00001324 ret = -EBUSY;
1325 goto err6;
1326 }
1327
Jassi Brar230d42d2009-11-30 07:39:42 +00001328 /* Setup Deufult Mode */
Thomas Abrahama5238e32012-07-13 07:15:14 +09001329 s3c64xx_spi_hwinit(sdd, sdd->port_id);
Jassi Brar230d42d2009-11-30 07:39:42 +00001330
1331 spin_lock_init(&sdd->lock);
1332 init_completion(&sdd->xfer_completion);
Jassi Brar230d42d2009-11-30 07:39:42 +00001333 INIT_LIST_HEAD(&sdd->queue);
1334
Mark Brownc2573122011-11-10 10:57:32 +00001335 ret = request_irq(irq, s3c64xx_spi_irq, 0, "spi-s3c64xx", sdd);
1336 if (ret != 0) {
1337 dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n",
1338 irq, ret);
Mark Brownad2a99a2012-02-15 14:48:32 -08001339 goto err7;
Mark Brownc2573122011-11-10 10:57:32 +00001340 }
1341
1342 writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
1343 S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
1344 sdd->regs + S3C64XX_SPI_INT_EN);
1345
Jassi Brar230d42d2009-11-30 07:39:42 +00001346 if (spi_register_master(master)) {
1347 dev_err(&pdev->dev, "cannot register SPI master\n");
1348 ret = -EBUSY;
Mark Brownad2a99a2012-02-15 14:48:32 -08001349 goto err8;
Jassi Brar230d42d2009-11-30 07:39:42 +00001350 }
1351
Joe Perches8a349d42010-02-02 07:22:13 +00001352 dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d "
1353 "with %d Slaves attached\n",
Thomas Abrahama5238e32012-07-13 07:15:14 +09001354 sdd->port_id, master->num_chipselect);
Joe Perches8a349d42010-02-02 07:22:13 +00001355 dev_dbg(&pdev->dev, "\tIOmem=[0x%x-0x%x]\tDMA=[Rx-%d, Tx-%d]\n",
Jassi Brar230d42d2009-11-30 07:39:42 +00001356 mem_res->end, mem_res->start,
Boojin Kim82ab8cd2011-09-02 09:44:42 +09001357 sdd->rx_dma.dmach, sdd->tx_dma.dmach);
Jassi Brar230d42d2009-11-30 07:39:42 +00001358
Mark Brownb97b6622011-12-04 00:58:06 +00001359 pm_runtime_enable(&pdev->dev);
1360
Jassi Brar230d42d2009-11-30 07:39:42 +00001361 return 0;
1362
1363err8:
Mark Brownad2a99a2012-02-15 14:48:32 -08001364 free_irq(irq, sdd);
Jassi Brar230d42d2009-11-30 07:39:42 +00001365err7:
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001366 clk_disable(sdd->src_clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001367err6:
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001368 clk_put(sdd->src_clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001369err5:
1370 clk_disable(sdd->clk);
1371err4:
1372 clk_put(sdd->clk);
1373err3:
Thomas Abraham2b908072012-07-13 07:15:15 +09001374 if (!sdd->cntrlr_info->cfg_gpio && pdev->dev.of_node)
1375 s3c64xx_spi_dt_gpio_free(sdd);
Jassi Brar230d42d2009-11-30 07:39:42 +00001376err2:
Jassi Brar230d42d2009-11-30 07:39:42 +00001377err1:
Jassi Brar230d42d2009-11-30 07:39:42 +00001378err0:
1379 platform_set_drvdata(pdev, NULL);
1380 spi_master_put(master);
1381
1382 return ret;
1383}
1384
1385static int s3c64xx_spi_remove(struct platform_device *pdev)
1386{
1387 struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
1388 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Jassi Brar230d42d2009-11-30 07:39:42 +00001389
Mark Brownb97b6622011-12-04 00:58:06 +00001390 pm_runtime_disable(&pdev->dev);
1391
Jassi Brar230d42d2009-11-30 07:39:42 +00001392 spi_unregister_master(master);
1393
Mark Brownc2573122011-11-10 10:57:32 +00001394 writel(0, sdd->regs + S3C64XX_SPI_INT_EN);
1395
1396 free_irq(platform_get_irq(pdev, 0), sdd);
1397
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001398 clk_disable(sdd->src_clk);
1399 clk_put(sdd->src_clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001400
1401 clk_disable(sdd->clk);
1402 clk_put(sdd->clk);
1403
Thomas Abraham2b908072012-07-13 07:15:15 +09001404 if (!sdd->cntrlr_info->cfg_gpio && pdev->dev.of_node)
1405 s3c64xx_spi_dt_gpio_free(sdd);
1406
Jassi Brar230d42d2009-11-30 07:39:42 +00001407 platform_set_drvdata(pdev, NULL);
1408 spi_master_put(master);
1409
1410 return 0;
1411}
1412
1413#ifdef CONFIG_PM
Mark Browne25d0bf2011-12-04 00:36:18 +00001414static int s3c64xx_spi_suspend(struct device *dev)
Jassi Brar230d42d2009-11-30 07:39:42 +00001415{
Mark Browne25d0bf2011-12-04 00:36:18 +00001416 struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
Jassi Brar230d42d2009-11-30 07:39:42 +00001417 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Jassi Brar230d42d2009-11-30 07:39:42 +00001418
Mark Brownad2a99a2012-02-15 14:48:32 -08001419 spi_master_suspend(master);
Jassi Brar230d42d2009-11-30 07:39:42 +00001420
1421 /* Disable the clock */
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001422 clk_disable(sdd->src_clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001423 clk_disable(sdd->clk);
1424
Thomas Abraham2b908072012-07-13 07:15:15 +09001425 if (!sdd->cntrlr_info->cfg_gpio && dev->of_node)
1426 s3c64xx_spi_dt_gpio_free(sdd);
1427
Jassi Brar230d42d2009-11-30 07:39:42 +00001428 sdd->cur_speed = 0; /* Output Clock is stopped */
1429
1430 return 0;
1431}
1432
Mark Browne25d0bf2011-12-04 00:36:18 +00001433static int s3c64xx_spi_resume(struct device *dev)
Jassi Brar230d42d2009-11-30 07:39:42 +00001434{
Mark Browne25d0bf2011-12-04 00:36:18 +00001435 struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
Jassi Brar230d42d2009-11-30 07:39:42 +00001436 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Jassi Brarad7de722010-01-20 13:49:44 -07001437 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +00001438
Thomas Abraham2b908072012-07-13 07:15:15 +09001439 if (!sci->cfg_gpio && dev->of_node)
1440 s3c64xx_spi_parse_dt_gpio(sdd);
1441 else
1442 sci->cfg_gpio();
Jassi Brar230d42d2009-11-30 07:39:42 +00001443
1444 /* Enable the clock */
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001445 clk_enable(sdd->src_clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001446 clk_enable(sdd->clk);
1447
Thomas Abrahama5238e32012-07-13 07:15:14 +09001448 s3c64xx_spi_hwinit(sdd, sdd->port_id);
Jassi Brar230d42d2009-11-30 07:39:42 +00001449
Mark Brownad2a99a2012-02-15 14:48:32 -08001450 spi_master_resume(master);
Jassi Brar230d42d2009-11-30 07:39:42 +00001451
1452 return 0;
1453}
Jassi Brar230d42d2009-11-30 07:39:42 +00001454#endif /* CONFIG_PM */
1455
Mark Brownb97b6622011-12-04 00:58:06 +00001456#ifdef CONFIG_PM_RUNTIME
1457static int s3c64xx_spi_runtime_suspend(struct device *dev)
1458{
1459 struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
1460 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1461
1462 clk_disable(sdd->clk);
1463 clk_disable(sdd->src_clk);
1464
1465 return 0;
1466}
1467
1468static int s3c64xx_spi_runtime_resume(struct device *dev)
1469{
1470 struct spi_master *master = spi_master_get(dev_get_drvdata(dev));
1471 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1472
1473 clk_enable(sdd->src_clk);
1474 clk_enable(sdd->clk);
1475
1476 return 0;
1477}
1478#endif /* CONFIG_PM_RUNTIME */
1479
Mark Browne25d0bf2011-12-04 00:36:18 +00001480static const struct dev_pm_ops s3c64xx_spi_pm = {
1481 SET_SYSTEM_SLEEP_PM_OPS(s3c64xx_spi_suspend, s3c64xx_spi_resume)
Mark Brownb97b6622011-12-04 00:58:06 +00001482 SET_RUNTIME_PM_OPS(s3c64xx_spi_runtime_suspend,
1483 s3c64xx_spi_runtime_resume, NULL)
Mark Browne25d0bf2011-12-04 00:36:18 +00001484};
1485
Thomas Abrahama5238e32012-07-13 07:15:14 +09001486struct s3c64xx_spi_port_config s3c2443_spi_port_config = {
1487 .fifo_lvl_mask = { 0x7f },
1488 .rx_lvl_offset = 13,
1489 .tx_st_done = 21,
1490 .high_speed = true,
1491};
1492
1493struct s3c64xx_spi_port_config s3c6410_spi_port_config = {
1494 .fifo_lvl_mask = { 0x7f, 0x7F },
1495 .rx_lvl_offset = 13,
1496 .tx_st_done = 21,
1497};
1498
1499struct s3c64xx_spi_port_config s5p64x0_spi_port_config = {
1500 .fifo_lvl_mask = { 0x1ff, 0x7F },
1501 .rx_lvl_offset = 15,
1502 .tx_st_done = 25,
1503};
1504
1505struct s3c64xx_spi_port_config s5pc100_spi_port_config = {
1506 .fifo_lvl_mask = { 0x7f, 0x7F },
1507 .rx_lvl_offset = 13,
1508 .tx_st_done = 21,
1509 .high_speed = true,
1510};
1511
1512struct s3c64xx_spi_port_config s5pv210_spi_port_config = {
1513 .fifo_lvl_mask = { 0x1ff, 0x7F },
1514 .rx_lvl_offset = 15,
1515 .tx_st_done = 25,
1516 .high_speed = true,
1517};
1518
1519struct s3c64xx_spi_port_config exynos4_spi_port_config = {
1520 .fifo_lvl_mask = { 0x1ff, 0x7F, 0x7F },
1521 .rx_lvl_offset = 15,
1522 .tx_st_done = 25,
1523 .high_speed = true,
1524 .clk_from_cmu = true,
1525};
1526
1527static struct platform_device_id s3c64xx_spi_driver_ids[] = {
1528 {
1529 .name = "s3c2443-spi",
1530 .driver_data = (kernel_ulong_t)&s3c2443_spi_port_config,
1531 }, {
1532 .name = "s3c6410-spi",
1533 .driver_data = (kernel_ulong_t)&s3c6410_spi_port_config,
1534 }, {
1535 .name = "s5p64x0-spi",
1536 .driver_data = (kernel_ulong_t)&s5p64x0_spi_port_config,
1537 }, {
1538 .name = "s5pc100-spi",
1539 .driver_data = (kernel_ulong_t)&s5pc100_spi_port_config,
1540 }, {
1541 .name = "s5pv210-spi",
1542 .driver_data = (kernel_ulong_t)&s5pv210_spi_port_config,
1543 }, {
1544 .name = "exynos4210-spi",
1545 .driver_data = (kernel_ulong_t)&exynos4_spi_port_config,
1546 },
1547 { },
1548};
1549
Thomas Abraham2b908072012-07-13 07:15:15 +09001550#ifdef CONFIG_OF
1551static const struct of_device_id s3c64xx_spi_dt_match[] = {
1552 { .compatible = "samsung,exynos4210-spi",
1553 .data = (void *)&exynos4_spi_port_config,
1554 },
1555 { },
1556};
1557MODULE_DEVICE_TABLE(of, s3c64xx_spi_dt_match);
1558#endif /* CONFIG_OF */
1559
Jassi Brar230d42d2009-11-30 07:39:42 +00001560static struct platform_driver s3c64xx_spi_driver = {
1561 .driver = {
1562 .name = "s3c64xx-spi",
1563 .owner = THIS_MODULE,
Mark Browne25d0bf2011-12-04 00:36:18 +00001564 .pm = &s3c64xx_spi_pm,
Thomas Abraham2b908072012-07-13 07:15:15 +09001565 .of_match_table = of_match_ptr(s3c64xx_spi_dt_match),
Jassi Brar230d42d2009-11-30 07:39:42 +00001566 },
1567 .remove = s3c64xx_spi_remove,
Thomas Abrahama5238e32012-07-13 07:15:14 +09001568 .id_table = s3c64xx_spi_driver_ids,
Jassi Brar230d42d2009-11-30 07:39:42 +00001569};
1570MODULE_ALIAS("platform:s3c64xx-spi");
1571
1572static int __init s3c64xx_spi_init(void)
1573{
1574 return platform_driver_probe(&s3c64xx_spi_driver, s3c64xx_spi_probe);
1575}
Mark Brownd2a787f2010-09-07 11:29:17 +01001576subsys_initcall(s3c64xx_spi_init);
Jassi Brar230d42d2009-11-30 07:39:42 +00001577
1578static void __exit s3c64xx_spi_exit(void)
1579{
1580 platform_driver_unregister(&s3c64xx_spi_driver);
1581}
1582module_exit(s3c64xx_spi_exit);
1583
1584MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
1585MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
1586MODULE_LICENSE("GPL");