2 * Copyright (C) 2009 Samsung Electronics Ltd.
3 * Jaswinder Singh <jassi.brar@samsung.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/workqueue.h>
23 #include <linux/interrupt.h>
24 #include <linux/delay.h>
25 #include <linux/clk.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/dmaengine.h>
28 #include <linux/platform_device.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/spi/spi.h>
31 #include <linux/gpio.h>
33 #include <linux/of_gpio.h>
35 #include <linux/platform_data/spi-s3c64xx.h>
41 #define MAX_SPI_PORTS 3
43 /* Registers and bit-fields */
45 #define S3C64XX_SPI_CH_CFG 0x00
46 #define S3C64XX_SPI_CLK_CFG 0x04
47 #define S3C64XX_SPI_MODE_CFG 0x08
48 #define S3C64XX_SPI_SLAVE_SEL 0x0C
49 #define S3C64XX_SPI_INT_EN 0x10
50 #define S3C64XX_SPI_STATUS 0x14
51 #define S3C64XX_SPI_TX_DATA 0x18
52 #define S3C64XX_SPI_RX_DATA 0x1C
53 #define S3C64XX_SPI_PACKET_CNT 0x20
54 #define S3C64XX_SPI_PENDING_CLR 0x24
55 #define S3C64XX_SPI_SWAP_CFG 0x28
56 #define S3C64XX_SPI_FB_CLK 0x2C
58 #define S3C64XX_SPI_CH_HS_EN (1<<6) /* High Speed Enable */
59 #define S3C64XX_SPI_CH_SW_RST (1<<5)
60 #define S3C64XX_SPI_CH_SLAVE (1<<4)
61 #define S3C64XX_SPI_CPOL_L (1<<3)
62 #define S3C64XX_SPI_CPHA_B (1<<2)
63 #define S3C64XX_SPI_CH_RXCH_ON (1<<1)
64 #define S3C64XX_SPI_CH_TXCH_ON (1<<0)
66 #define S3C64XX_SPI_CLKSEL_SRCMSK (3<<9)
67 #define S3C64XX_SPI_CLKSEL_SRCSHFT 9
68 #define S3C64XX_SPI_ENCLK_ENABLE (1<<8)
69 #define S3C64XX_SPI_PSR_MASK 0xff
71 #define S3C64XX_SPI_MODE_CH_TSZ_BYTE (0<<29)
72 #define S3C64XX_SPI_MODE_CH_TSZ_HALFWORD (1<<29)
73 #define S3C64XX_SPI_MODE_CH_TSZ_WORD (2<<29)
74 #define S3C64XX_SPI_MODE_CH_TSZ_MASK (3<<29)
75 #define S3C64XX_SPI_MODE_BUS_TSZ_BYTE (0<<17)
76 #define S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD (1<<17)
77 #define S3C64XX_SPI_MODE_BUS_TSZ_WORD (2<<17)
78 #define S3C64XX_SPI_MODE_BUS_TSZ_MASK (3<<17)
79 #define S3C64XX_SPI_MODE_RXDMA_ON (1<<2)
80 #define S3C64XX_SPI_MODE_TXDMA_ON (1<<1)
81 #define S3C64XX_SPI_MODE_4BURST (1<<0)
83 #define S3C64XX_SPI_SLAVE_AUTO (1<<1)
84 #define S3C64XX_SPI_SLAVE_SIG_INACT (1<<0)
86 #define S3C64XX_SPI_INT_TRAILING_EN (1<<6)
87 #define S3C64XX_SPI_INT_RX_OVERRUN_EN (1<<5)
88 #define S3C64XX_SPI_INT_RX_UNDERRUN_EN (1<<4)
89 #define S3C64XX_SPI_INT_TX_OVERRUN_EN (1<<3)
90 #define S3C64XX_SPI_INT_TX_UNDERRUN_EN (1<<2)
91 #define S3C64XX_SPI_INT_RX_FIFORDY_EN (1<<1)
92 #define S3C64XX_SPI_INT_TX_FIFORDY_EN (1<<0)
94 #define S3C64XX_SPI_ST_RX_OVERRUN_ERR (1<<5)
95 #define S3C64XX_SPI_ST_RX_UNDERRUN_ERR (1<<4)
96 #define S3C64XX_SPI_ST_TX_OVERRUN_ERR (1<<3)
97 #define S3C64XX_SPI_ST_TX_UNDERRUN_ERR (1<<2)
98 #define S3C64XX_SPI_ST_RX_FIFORDY (1<<1)
99 #define S3C64XX_SPI_ST_TX_FIFORDY (1<<0)
101 #define S3C64XX_SPI_PACKET_CNT_EN (1<<16)
103 #define S3C64XX_SPI_PND_TX_UNDERRUN_CLR (1<<4)
104 #define S3C64XX_SPI_PND_TX_OVERRUN_CLR (1<<3)
105 #define S3C64XX_SPI_PND_RX_UNDERRUN_CLR (1<<2)
106 #define S3C64XX_SPI_PND_RX_OVERRUN_CLR (1<<1)
107 #define S3C64XX_SPI_PND_TRAILING_CLR (1<<0)
109 #define S3C64XX_SPI_SWAP_RX_HALF_WORD (1<<7)
110 #define S3C64XX_SPI_SWAP_RX_BYTE (1<<6)
111 #define S3C64XX_SPI_SWAP_RX_BIT (1<<5)
112 #define S3C64XX_SPI_SWAP_RX_EN (1<<4)
113 #define S3C64XX_SPI_SWAP_TX_HALF_WORD (1<<3)
114 #define S3C64XX_SPI_SWAP_TX_BYTE (1<<2)
115 #define S3C64XX_SPI_SWAP_TX_BIT (1<<1)
116 #define S3C64XX_SPI_SWAP_TX_EN (1<<0)
118 #define S3C64XX_SPI_FBCLK_MSK (3<<0)
120 #define FIFO_LVL_MASK(i) ((i)->port_conf->fifo_lvl_mask[i->port_id])
121 #define S3C64XX_SPI_ST_TX_DONE(v, i) (((v) & \
122 (1 << (i)->port_conf->tx_st_done)) ? 1 : 0)
123 #define TX_FIFO_LVL(v, i) (((v) >> 6) & FIFO_LVL_MASK(i))
124 #define RX_FIFO_LVL(v, i) (((v) >> (i)->port_conf->rx_lvl_offset) & \
127 #define S3C64XX_SPI_MAX_TRAILCNT 0x3ff
128 #define S3C64XX_SPI_TRAILCNT_OFF 19
130 #define S3C64XX_SPI_TRAILCNT S3C64XX_SPI_MAX_TRAILCNT
132 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
134 #define RXBUSY (1<<2)
135 #define TXBUSY (1<<3)
137 struct s3c64xx_spi_dma_data {
139 enum dma_transfer_direction direction;
144 * struct s3c64xx_spi_info - SPI Controller hardware info
145 * @fifo_lvl_mask: Bit-mask for {TX|RX}_FIFO_LVL bits in SPI_STATUS register.
146 * @rx_lvl_offset: Bit offset of RX_FIFO_LVL bits in SPI_STATUS regiter.
147 * @tx_st_done: Bit offset of TX_DONE bit in SPI_STATUS regiter.
148 * @high_speed: True, if the controller supports HIGH_SPEED_EN bit.
149 * @clk_from_cmu: True, if the controller does not include a clock mux and
152 * The Samsung s3c64xx SPI controller are used on various Samsung SoC's but
153 * differ in some aspects such as the size of the fifo and spi bus clock
154 * setup. Such differences are specified to the driver using this structure
155 * which is provided as driver data to the driver.
157 struct s3c64xx_spi_port_config {
158 int fifo_lvl_mask[MAX_SPI_PORTS];
166 * struct s3c64xx_spi_driver_data - Runtime info holder for SPI driver.
167 * @clk: Pointer to the spi clock.
168 * @src_clk: Pointer to the clock used to generate SPI signals.
169 * @master: Pointer to the SPI Protocol master.
170 * @cntrlr_info: Platform specific data for the controller this driver manages.
171 * @tgl_spi: Pointer to the last CS left untoggled by the cs_change hint.
172 * @queue: To log SPI xfer requests.
173 * @lock: Controller specific lock.
174 * @state: Set of FLAGS to indicate status.
175 * @rx_dmach: Controller's DMA channel for Rx.
176 * @tx_dmach: Controller's DMA channel for Tx.
177 * @sfr_start: BUS address of SPI controller regs.
178 * @regs: Pointer to ioremap'ed controller registers.
180 * @xfer_completion: To indicate completion of xfer task.
181 * @cur_mode: Stores the active configuration of the controller.
182 * @cur_bpw: Stores the active bits per word settings.
183 * @cur_speed: Stores the active xfer clock speed.
185 struct s3c64xx_spi_driver_data {
189 struct platform_device *pdev;
190 struct spi_master *master;
191 struct s3c64xx_spi_info *cntrlr_info;
192 struct spi_device *tgl_spi;
193 struct list_head queue;
195 unsigned long sfr_start;
196 struct completion xfer_completion;
198 unsigned cur_mode, cur_bpw;
200 struct s3c64xx_spi_dma_data rx_dma;
201 struct s3c64xx_spi_dma_data tx_dma;
202 #ifdef CONFIG_S3C_DMA
203 struct samsung_dma_ops *ops;
205 struct s3c64xx_spi_port_config *port_conf;
206 unsigned int port_id;
207 unsigned long gpios[4];
210 static void flush_fifo(struct s3c64xx_spi_driver_data *sdd)
212 void __iomem *regs = sdd->regs;
216 writel(0, regs + S3C64XX_SPI_PACKET_CNT);
218 val = readl(regs + S3C64XX_SPI_CH_CFG);
219 val &= ~(S3C64XX_SPI_CH_RXCH_ON | S3C64XX_SPI_CH_TXCH_ON);
220 writel(val, regs + S3C64XX_SPI_CH_CFG);
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);
228 loops = msecs_to_loops(1);
230 val = readl(regs + S3C64XX_SPI_STATUS);
231 } while (TX_FIFO_LVL(val, sdd) && loops--);
234 dev_warn(&sdd->pdev->dev, "Timed out flushing TX FIFO\n");
237 loops = msecs_to_loops(1);
239 val = readl(regs + S3C64XX_SPI_STATUS);
240 if (RX_FIFO_LVL(val, sdd))
241 readl(regs + S3C64XX_SPI_RX_DATA);
247 dev_warn(&sdd->pdev->dev, "Timed out flushing RX FIFO\n");
249 val = readl(regs + S3C64XX_SPI_CH_CFG);
250 val &= ~S3C64XX_SPI_CH_SW_RST;
251 writel(val, regs + S3C64XX_SPI_CH_CFG);
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);
258 static void s3c64xx_spi_dmacb(void *data)
260 struct s3c64xx_spi_driver_data *sdd;
261 struct s3c64xx_spi_dma_data *dma = data;
264 if (dma->direction == DMA_DEV_TO_MEM)
265 sdd = container_of(data,
266 struct s3c64xx_spi_driver_data, rx_dma);
268 sdd = container_of(data,
269 struct s3c64xx_spi_driver_data, tx_dma);
271 spin_lock_irqsave(&sdd->lock, flags);
273 if (dma->direction == DMA_DEV_TO_MEM) {
274 sdd->state &= ~RXBUSY;
275 if (!(sdd->state & TXBUSY))
276 complete(&sdd->xfer_completion);
278 sdd->state &= ~TXBUSY;
279 if (!(sdd->state & RXBUSY))
280 complete(&sdd->xfer_completion);
283 spin_unlock_irqrestore(&sdd->lock, flags);
286 #ifdef CONFIG_S3C_DMA
287 /* FIXME: remove this section once arch/arm/mach-s3c64xx uses dmaengine */
289 static struct s3c2410_dma_client s3c64xx_spi_dma_client = {
290 .name = "samsung-spi-dma",
293 static void prepare_dma(struct s3c64xx_spi_dma_data *dma,
294 unsigned len, dma_addr_t buf)
296 struct s3c64xx_spi_driver_data *sdd;
297 struct samsung_dma_prep info;
298 struct samsung_dma_config config;
300 if (dma->direction == DMA_DEV_TO_MEM) {
301 sdd = container_of((void *)dma,
302 struct s3c64xx_spi_driver_data, rx_dma);
303 config.direction = sdd->rx_dma.direction;
304 config.fifo = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
305 config.width = sdd->cur_bpw / 8;
306 sdd->ops->config((enum dma_ch)sdd->rx_dma.ch, &config);
308 sdd = container_of((void *)dma,
309 struct s3c64xx_spi_driver_data, tx_dma);
310 config.direction = sdd->tx_dma.direction;
311 config.fifo = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
312 config.width = sdd->cur_bpw / 8;
313 sdd->ops->config((enum dma_ch)sdd->tx_dma.ch, &config);
316 info.cap = DMA_SLAVE;
318 info.fp = s3c64xx_spi_dmacb;
320 info.direction = dma->direction;
323 sdd->ops->prepare((enum dma_ch)dma->ch, &info);
324 sdd->ops->trigger((enum dma_ch)dma->ch);
327 static int acquire_dma(struct s3c64xx_spi_driver_data *sdd)
329 struct samsung_dma_req req;
330 struct device *dev = &sdd->pdev->dev;
332 sdd->ops = samsung_dma_get_ops();
335 req.client = &s3c64xx_spi_dma_client;
337 sdd->rx_dma.ch = (void *)sdd->ops->request(sdd->rx_dma.dmach, &req, dev, "rx");
338 sdd->tx_dma.ch = (void *)sdd->ops->request(sdd->tx_dma.dmach, &req, dev, "tx");
343 static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
345 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
347 /* Acquire DMA channels */
348 while (!acquire_dma(sdd))
349 usleep_range(10000, 11000);
351 pm_runtime_get_sync(&sdd->pdev->dev);
356 static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
358 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
360 /* Free DMA channels */
361 sdd->ops->release((enum dma_ch)sdd->rx_dma.ch, &s3c64xx_spi_dma_client);
362 sdd->ops->release((enum dma_ch)sdd->tx_dma.ch, &s3c64xx_spi_dma_client);
364 pm_runtime_put(&sdd->pdev->dev);
369 static void s3c64xx_spi_dma_stop(struct s3c64xx_spi_driver_data *sdd,
370 struct s3c64xx_spi_dma_data *dma)
372 sdd->ops->stop((enum dma_ch)dma->ch);
376 static void prepare_dma(struct s3c64xx_spi_dma_data *dma,
377 unsigned len, dma_addr_t buf)
379 struct s3c64xx_spi_driver_data *sdd;
380 struct dma_slave_config config;
381 struct scatterlist sg;
382 struct dma_async_tx_descriptor *desc;
384 if (dma->direction == DMA_DEV_TO_MEM) {
385 sdd = container_of((void *)dma,
386 struct s3c64xx_spi_driver_data, rx_dma);
387 config.direction = dma->direction;
388 config.src_addr = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
389 config.src_addr_width = sdd->cur_bpw / 8;
390 config.src_maxburst = 1;
391 dmaengine_slave_config(dma->ch, &config);
393 sdd = container_of((void *)dma,
394 struct s3c64xx_spi_driver_data, tx_dma);
395 config.direction = dma->direction;
396 config.dst_addr = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
397 config.dst_addr_width = sdd->cur_bpw / 8;
398 config.dst_maxburst = 1;
399 dmaengine_slave_config(dma->ch, &config);
402 sg_init_table(&sg, 1);
403 sg_dma_len(&sg) = len;
404 sg_set_page(&sg, pfn_to_page(PFN_DOWN(buf)),
405 len, offset_in_page(buf));
406 sg_dma_address(&sg) = buf;
408 desc = dmaengine_prep_slave_sg(dma->ch,
409 &sg, 1, dma->direction, DMA_PREP_INTERRUPT);
411 desc->callback = s3c64xx_spi_dmacb;
412 desc->callback_param = dma;
414 dmaengine_submit(desc);
415 dma_async_issue_pending(dma->ch);
418 static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
420 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
421 dma_filter_fn filter = sdd->cntrlr_info->filter;
422 struct device *dev = &sdd->pdev->dev;
427 dma_cap_set(DMA_SLAVE, mask);
429 /* Acquire DMA channels */
430 sdd->rx_dma.ch = dma_request_slave_channel_compat(mask, filter,
431 (void*)sdd->rx_dma.dmach, dev, "rx");
432 if (!sdd->rx_dma.ch) {
433 dev_err(dev, "Failed to get RX DMA channel\n");
438 sdd->tx_dma.ch = dma_request_slave_channel_compat(mask, filter,
439 (void*)sdd->tx_dma.dmach, dev, "tx");
440 if (!sdd->tx_dma.ch) {
441 dev_err(dev, "Failed to get TX DMA channel\n");
446 ret = pm_runtime_get_sync(&sdd->pdev->dev);
448 dev_err(dev, "Failed to enable device: %d\n", ret);
455 dma_release_channel(sdd->tx_dma.ch);
457 dma_release_channel(sdd->rx_dma.ch);
462 static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
464 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
466 /* Free DMA channels */
467 dma_release_channel(sdd->rx_dma.ch);
468 dma_release_channel(sdd->tx_dma.ch);
470 pm_runtime_put(&sdd->pdev->dev);
474 static void s3c64xx_spi_dma_stop(struct s3c64xx_spi_driver_data *sdd,
475 struct s3c64xx_spi_dma_data *dma)
477 dmaengine_terminate_all(dma->ch);
481 static void enable_datapath(struct s3c64xx_spi_driver_data *sdd,
482 struct spi_device *spi,
483 struct spi_transfer *xfer, int dma_mode)
485 void __iomem *regs = sdd->regs;
488 modecfg = readl(regs + S3C64XX_SPI_MODE_CFG);
489 modecfg &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
491 chcfg = readl(regs + S3C64XX_SPI_CH_CFG);
492 chcfg &= ~S3C64XX_SPI_CH_TXCH_ON;
495 chcfg &= ~S3C64XX_SPI_CH_RXCH_ON;
497 /* Always shift in data in FIFO, even if xfer is Tx only,
498 * this helps setting PCKT_CNT value for generating clocks
501 chcfg |= S3C64XX_SPI_CH_RXCH_ON;
502 writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
503 | S3C64XX_SPI_PACKET_CNT_EN,
504 regs + S3C64XX_SPI_PACKET_CNT);
507 if (xfer->tx_buf != NULL) {
508 sdd->state |= TXBUSY;
509 chcfg |= S3C64XX_SPI_CH_TXCH_ON;
511 modecfg |= S3C64XX_SPI_MODE_TXDMA_ON;
512 prepare_dma(&sdd->tx_dma, xfer->len, xfer->tx_dma);
514 switch (sdd->cur_bpw) {
516 iowrite32_rep(regs + S3C64XX_SPI_TX_DATA,
517 xfer->tx_buf, xfer->len / 4);
520 iowrite16_rep(regs + S3C64XX_SPI_TX_DATA,
521 xfer->tx_buf, xfer->len / 2);
524 iowrite8_rep(regs + S3C64XX_SPI_TX_DATA,
525 xfer->tx_buf, xfer->len);
531 if (xfer->rx_buf != NULL) {
532 sdd->state |= RXBUSY;
534 if (sdd->port_conf->high_speed && sdd->cur_speed >= 30000000UL
535 && !(sdd->cur_mode & SPI_CPHA))
536 chcfg |= S3C64XX_SPI_CH_HS_EN;
539 modecfg |= S3C64XX_SPI_MODE_RXDMA_ON;
540 chcfg |= S3C64XX_SPI_CH_RXCH_ON;
541 writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
542 | S3C64XX_SPI_PACKET_CNT_EN,
543 regs + S3C64XX_SPI_PACKET_CNT);
544 prepare_dma(&sdd->rx_dma, xfer->len, xfer->rx_dma);
548 writel(modecfg, regs + S3C64XX_SPI_MODE_CFG);
549 writel(chcfg, regs + S3C64XX_SPI_CH_CFG);
552 static inline void enable_cs(struct s3c64xx_spi_driver_data *sdd,
553 struct spi_device *spi)
555 struct s3c64xx_spi_csinfo *cs;
557 if (sdd->tgl_spi != NULL) { /* If last device toggled after mssg */
558 if (sdd->tgl_spi != spi) { /* if last mssg on diff device */
559 /* Deselect the last toggled device */
560 cs = sdd->tgl_spi->controller_data;
561 gpio_set_value(cs->line,
562 spi->mode & SPI_CS_HIGH ? 0 : 1);
567 cs = spi->controller_data;
568 gpio_set_value(cs->line, spi->mode & SPI_CS_HIGH ? 1 : 0);
571 static int wait_for_xfer(struct s3c64xx_spi_driver_data *sdd,
572 struct spi_transfer *xfer, int dma_mode)
574 void __iomem *regs = sdd->regs;
578 /* millisecs to xfer 'len' bytes @ 'cur_speed' */
579 ms = xfer->len * 8 * 1000 / sdd->cur_speed;
580 ms += 10; /* some tolerance */
583 val = msecs_to_jiffies(ms) + 10;
584 val = wait_for_completion_timeout(&sdd->xfer_completion, val);
587 val = msecs_to_loops(ms);
589 status = readl(regs + S3C64XX_SPI_STATUS);
590 } while (RX_FIFO_LVL(status, sdd) < xfer->len && --val);
600 * DmaTx returns after simply writing data in the FIFO,
601 * w/o waiting for real transmission on the bus to finish.
602 * DmaRx returns only after Dma read data from FIFO which
603 * needs bus transmission to finish, so we don't worry if
604 * Xfer involved Rx(with or without Tx).
606 if (xfer->rx_buf == NULL) {
607 val = msecs_to_loops(10);
608 status = readl(regs + S3C64XX_SPI_STATUS);
609 while ((TX_FIFO_LVL(status, sdd)
610 || !S3C64XX_SPI_ST_TX_DONE(status, sdd))
613 status = readl(regs + S3C64XX_SPI_STATUS);
620 /* If it was only Tx */
621 if (xfer->rx_buf == NULL) {
622 sdd->state &= ~TXBUSY;
626 switch (sdd->cur_bpw) {
628 ioread32_rep(regs + S3C64XX_SPI_RX_DATA,
629 xfer->rx_buf, xfer->len / 4);
632 ioread16_rep(regs + S3C64XX_SPI_RX_DATA,
633 xfer->rx_buf, xfer->len / 2);
636 ioread8_rep(regs + S3C64XX_SPI_RX_DATA,
637 xfer->rx_buf, xfer->len);
640 sdd->state &= ~RXBUSY;
646 static inline void disable_cs(struct s3c64xx_spi_driver_data *sdd,
647 struct spi_device *spi)
649 struct s3c64xx_spi_csinfo *cs = spi->controller_data;
651 if (sdd->tgl_spi == spi)
654 gpio_set_value(cs->line, spi->mode & SPI_CS_HIGH ? 0 : 1);
657 static void s3c64xx_spi_config(struct s3c64xx_spi_driver_data *sdd)
659 void __iomem *regs = sdd->regs;
663 if (sdd->port_conf->clk_from_cmu) {
664 clk_disable_unprepare(sdd->src_clk);
666 val = readl(regs + S3C64XX_SPI_CLK_CFG);
667 val &= ~S3C64XX_SPI_ENCLK_ENABLE;
668 writel(val, regs + S3C64XX_SPI_CLK_CFG);
671 /* Set Polarity and Phase */
672 val = readl(regs + S3C64XX_SPI_CH_CFG);
673 val &= ~(S3C64XX_SPI_CH_SLAVE |
677 if (sdd->cur_mode & SPI_CPOL)
678 val |= S3C64XX_SPI_CPOL_L;
680 if (sdd->cur_mode & SPI_CPHA)
681 val |= S3C64XX_SPI_CPHA_B;
683 writel(val, regs + S3C64XX_SPI_CH_CFG);
685 /* Set Channel & DMA Mode */
686 val = readl(regs + S3C64XX_SPI_MODE_CFG);
687 val &= ~(S3C64XX_SPI_MODE_BUS_TSZ_MASK
688 | S3C64XX_SPI_MODE_CH_TSZ_MASK);
690 switch (sdd->cur_bpw) {
692 val |= S3C64XX_SPI_MODE_BUS_TSZ_WORD;
693 val |= S3C64XX_SPI_MODE_CH_TSZ_WORD;
696 val |= S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD;
697 val |= S3C64XX_SPI_MODE_CH_TSZ_HALFWORD;
700 val |= S3C64XX_SPI_MODE_BUS_TSZ_BYTE;
701 val |= S3C64XX_SPI_MODE_CH_TSZ_BYTE;
705 writel(val, regs + S3C64XX_SPI_MODE_CFG);
707 if (sdd->port_conf->clk_from_cmu) {
708 /* Configure Clock */
709 /* There is half-multiplier before the SPI */
710 clk_set_rate(sdd->src_clk, sdd->cur_speed * 2);
712 clk_prepare_enable(sdd->src_clk);
714 /* Configure Clock */
715 val = readl(regs + S3C64XX_SPI_CLK_CFG);
716 val &= ~S3C64XX_SPI_PSR_MASK;
717 val |= ((clk_get_rate(sdd->src_clk) / sdd->cur_speed / 2 - 1)
718 & S3C64XX_SPI_PSR_MASK);
719 writel(val, regs + S3C64XX_SPI_CLK_CFG);
722 val = readl(regs + S3C64XX_SPI_CLK_CFG);
723 val |= S3C64XX_SPI_ENCLK_ENABLE;
724 writel(val, regs + S3C64XX_SPI_CLK_CFG);
728 #define XFER_DMAADDR_INVALID DMA_BIT_MASK(32)
730 static int s3c64xx_spi_map_mssg(struct s3c64xx_spi_driver_data *sdd,
731 struct spi_message *msg)
733 struct device *dev = &sdd->pdev->dev;
734 struct spi_transfer *xfer;
736 if (msg->is_dma_mapped)
739 /* First mark all xfer unmapped */
740 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
741 xfer->rx_dma = XFER_DMAADDR_INVALID;
742 xfer->tx_dma = XFER_DMAADDR_INVALID;
745 /* Map until end or first fail */
746 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
748 if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
751 if (xfer->tx_buf != NULL) {
752 xfer->tx_dma = dma_map_single(dev,
753 (void *)xfer->tx_buf, xfer->len,
755 if (dma_mapping_error(dev, xfer->tx_dma)) {
756 dev_err(dev, "dma_map_single Tx failed\n");
757 xfer->tx_dma = XFER_DMAADDR_INVALID;
762 if (xfer->rx_buf != NULL) {
763 xfer->rx_dma = dma_map_single(dev, xfer->rx_buf,
764 xfer->len, DMA_FROM_DEVICE);
765 if (dma_mapping_error(dev, xfer->rx_dma)) {
766 dev_err(dev, "dma_map_single Rx failed\n");
767 dma_unmap_single(dev, xfer->tx_dma,
768 xfer->len, DMA_TO_DEVICE);
769 xfer->tx_dma = XFER_DMAADDR_INVALID;
770 xfer->rx_dma = XFER_DMAADDR_INVALID;
779 static void s3c64xx_spi_unmap_mssg(struct s3c64xx_spi_driver_data *sdd,
780 struct spi_message *msg)
782 struct device *dev = &sdd->pdev->dev;
783 struct spi_transfer *xfer;
785 if (msg->is_dma_mapped)
788 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
790 if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
793 if (xfer->rx_buf != NULL
794 && xfer->rx_dma != XFER_DMAADDR_INVALID)
795 dma_unmap_single(dev, xfer->rx_dma,
796 xfer->len, DMA_FROM_DEVICE);
798 if (xfer->tx_buf != NULL
799 && xfer->tx_dma != XFER_DMAADDR_INVALID)
800 dma_unmap_single(dev, xfer->tx_dma,
801 xfer->len, DMA_TO_DEVICE);
805 static int s3c64xx_spi_transfer_one_message(struct spi_master *master,
806 struct spi_message *msg)
808 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
809 struct spi_device *spi = msg->spi;
810 struct s3c64xx_spi_csinfo *cs = spi->controller_data;
811 struct spi_transfer *xfer;
812 int status = 0, cs_toggle = 0;
816 /* If Master's(controller) state differs from that needed by Slave */
817 if (sdd->cur_speed != spi->max_speed_hz
818 || sdd->cur_mode != spi->mode
819 || sdd->cur_bpw != spi->bits_per_word) {
820 sdd->cur_bpw = spi->bits_per_word;
821 sdd->cur_speed = spi->max_speed_hz;
822 sdd->cur_mode = spi->mode;
823 s3c64xx_spi_config(sdd);
826 /* Map all the transfers if needed */
827 if (s3c64xx_spi_map_mssg(sdd, msg)) {
829 "Xfer: Unable to map message buffers!\n");
834 /* Configure feedback delay */
835 writel(cs->fb_delay & 0x3, sdd->regs + S3C64XX_SPI_FB_CLK);
837 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
842 INIT_COMPLETION(sdd->xfer_completion);
844 /* Only BPW and Speed may change across transfers */
845 bpw = xfer->bits_per_word;
846 speed = xfer->speed_hz ? : spi->max_speed_hz;
848 if (xfer->len % (bpw / 8)) {
850 "Xfer length(%u) not a multiple of word size(%u)\n",
856 if (bpw != sdd->cur_bpw || speed != sdd->cur_speed) {
858 sdd->cur_speed = speed;
859 s3c64xx_spi_config(sdd);
862 /* Polling method for xfers not bigger than FIFO capacity */
864 if (sdd->rx_dma.ch && sdd->tx_dma.ch &&
865 (xfer->len > ((FIFO_LVL_MASK(sdd) >> 1) + 1)))
868 spin_lock_irqsave(&sdd->lock, flags);
870 /* Pending only which is to be done */
871 sdd->state &= ~RXBUSY;
872 sdd->state &= ~TXBUSY;
874 enable_datapath(sdd, spi, xfer, use_dma);
879 /* Start the signals */
880 writel(0, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
882 spin_unlock_irqrestore(&sdd->lock, flags);
884 status = wait_for_xfer(sdd, xfer, use_dma);
886 /* Quiese the signals */
887 writel(S3C64XX_SPI_SLAVE_SIG_INACT,
888 sdd->regs + S3C64XX_SPI_SLAVE_SEL);
891 dev_err(&spi->dev, "I/O Error: rx-%d tx-%d res:rx-%c tx-%c len-%d\n",
892 xfer->rx_buf ? 1 : 0, xfer->tx_buf ? 1 : 0,
893 (sdd->state & RXBUSY) ? 'f' : 'p',
894 (sdd->state & TXBUSY) ? 'f' : 'p',
898 if (xfer->tx_buf != NULL
899 && (sdd->state & TXBUSY))
900 s3c64xx_spi_dma_stop(sdd, &sdd->tx_dma);
901 if (xfer->rx_buf != NULL
902 && (sdd->state & RXBUSY))
903 s3c64xx_spi_dma_stop(sdd, &sdd->rx_dma);
909 if (xfer->delay_usecs)
910 udelay(xfer->delay_usecs);
912 if (xfer->cs_change) {
913 /* Hint that the next mssg is gonna be
914 for the same device */
915 if (list_is_last(&xfer->transfer_list,
920 msg->actual_length += xfer->len;
926 if (!cs_toggle || status)
927 disable_cs(sdd, spi);
931 s3c64xx_spi_unmap_mssg(sdd, msg);
933 msg->status = status;
935 spi_finalize_current_message(master);
940 static struct s3c64xx_spi_csinfo *s3c64xx_get_slave_ctrldata(
941 struct spi_device *spi)
943 struct s3c64xx_spi_csinfo *cs;
944 struct device_node *slave_np, *data_np = NULL;
947 slave_np = spi->dev.of_node;
949 dev_err(&spi->dev, "device node not found\n");
950 return ERR_PTR(-EINVAL);
953 data_np = of_get_child_by_name(slave_np, "controller-data");
955 dev_err(&spi->dev, "child node 'controller-data' not found\n");
956 return ERR_PTR(-EINVAL);
959 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
961 dev_err(&spi->dev, "could not allocate memory for controller data\n");
962 of_node_put(data_np);
963 return ERR_PTR(-ENOMEM);
966 cs->line = of_get_named_gpio(data_np, "cs-gpio", 0);
967 if (!gpio_is_valid(cs->line)) {
968 dev_err(&spi->dev, "chip select gpio is not specified or invalid\n");
970 of_node_put(data_np);
971 return ERR_PTR(-EINVAL);
974 of_property_read_u32(data_np, "samsung,spi-feedback-delay", &fb_delay);
975 cs->fb_delay = fb_delay;
976 of_node_put(data_np);
981 * Here we only check the validity of requested configuration
982 * and save the configuration in a local data-structure.
983 * The controller is actually configured only just before we
984 * get a message to transfer.
986 static int s3c64xx_spi_setup(struct spi_device *spi)
988 struct s3c64xx_spi_csinfo *cs = spi->controller_data;
989 struct s3c64xx_spi_driver_data *sdd;
990 struct s3c64xx_spi_info *sci;
991 struct spi_message *msg;
995 sdd = spi_master_get_devdata(spi->master);
996 if (!cs && spi->dev.of_node) {
997 cs = s3c64xx_get_slave_ctrldata(spi);
998 spi->controller_data = cs;
1001 if (IS_ERR_OR_NULL(cs)) {
1002 dev_err(&spi->dev, "No CS for SPI(%d)\n", spi->chip_select);
1006 if (!spi_get_ctldata(spi)) {
1007 err = gpio_request_one(cs->line, GPIOF_OUT_INIT_HIGH,
1008 dev_name(&spi->dev));
1011 "Failed to get /CS gpio [%d]: %d\n",
1015 spi_set_ctldata(spi, cs);
1018 sci = sdd->cntrlr_info;
1020 spin_lock_irqsave(&sdd->lock, flags);
1022 list_for_each_entry(msg, &sdd->queue, queue) {
1023 /* Is some mssg is already queued for this device */
1024 if (msg->spi == spi) {
1026 "setup: attempt while mssg in queue!\n");
1027 spin_unlock_irqrestore(&sdd->lock, flags);
1033 spin_unlock_irqrestore(&sdd->lock, flags);
1035 pm_runtime_get_sync(&sdd->pdev->dev);
1037 /* Check if we can provide the requested rate */
1038 if (!sdd->port_conf->clk_from_cmu) {
1042 speed = clk_get_rate(sdd->src_clk) / 2 / (0 + 1);
1044 if (spi->max_speed_hz > speed)
1045 spi->max_speed_hz = speed;
1047 psr = clk_get_rate(sdd->src_clk) / 2 / spi->max_speed_hz - 1;
1048 psr &= S3C64XX_SPI_PSR_MASK;
1049 if (psr == S3C64XX_SPI_PSR_MASK)
1052 speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
1053 if (spi->max_speed_hz < speed) {
1054 if (psr+1 < S3C64XX_SPI_PSR_MASK) {
1062 speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
1063 if (spi->max_speed_hz >= speed) {
1064 spi->max_speed_hz = speed;
1066 dev_err(&spi->dev, "Can't set %dHz transfer speed\n",
1073 pm_runtime_put(&sdd->pdev->dev);
1074 disable_cs(sdd, spi);
1078 /* setup() returns with device de-selected */
1079 disable_cs(sdd, spi);
1082 gpio_free(cs->line);
1083 spi_set_ctldata(spi, NULL);
1086 if (spi->dev.of_node)
1092 static void s3c64xx_spi_cleanup(struct spi_device *spi)
1094 struct s3c64xx_spi_csinfo *cs = spi_get_ctldata(spi);
1097 gpio_free(cs->line);
1098 if (spi->dev.of_node)
1101 spi_set_ctldata(spi, NULL);
1104 static irqreturn_t s3c64xx_spi_irq(int irq, void *data)
1106 struct s3c64xx_spi_driver_data *sdd = data;
1107 struct spi_master *spi = sdd->master;
1108 unsigned int val, clr = 0;
1110 val = readl(sdd->regs + S3C64XX_SPI_STATUS);
1112 if (val & S3C64XX_SPI_ST_RX_OVERRUN_ERR) {
1113 clr = S3C64XX_SPI_PND_RX_OVERRUN_CLR;
1114 dev_err(&spi->dev, "RX overrun\n");
1116 if (val & S3C64XX_SPI_ST_RX_UNDERRUN_ERR) {
1117 clr |= S3C64XX_SPI_PND_RX_UNDERRUN_CLR;
1118 dev_err(&spi->dev, "RX underrun\n");
1120 if (val & S3C64XX_SPI_ST_TX_OVERRUN_ERR) {
1121 clr |= S3C64XX_SPI_PND_TX_OVERRUN_CLR;
1122 dev_err(&spi->dev, "TX overrun\n");
1124 if (val & S3C64XX_SPI_ST_TX_UNDERRUN_ERR) {
1125 clr |= S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
1126 dev_err(&spi->dev, "TX underrun\n");
1129 /* Clear the pending irq by setting and then clearing it */
1130 writel(clr, sdd->regs + S3C64XX_SPI_PENDING_CLR);
1131 writel(0, sdd->regs + S3C64XX_SPI_PENDING_CLR);
1136 static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data *sdd, int channel)
1138 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
1139 void __iomem *regs = sdd->regs;
1144 writel(S3C64XX_SPI_SLAVE_SIG_INACT, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
1146 /* Disable Interrupts - we use Polling if not DMA mode */
1147 writel(0, regs + S3C64XX_SPI_INT_EN);
1149 if (!sdd->port_conf->clk_from_cmu)
1150 writel(sci->src_clk_nr << S3C64XX_SPI_CLKSEL_SRCSHFT,
1151 regs + S3C64XX_SPI_CLK_CFG);
1152 writel(0, regs + S3C64XX_SPI_MODE_CFG);
1153 writel(0, regs + S3C64XX_SPI_PACKET_CNT);
1155 /* Clear any irq pending bits, should set and clear the bits */
1156 val = S3C64XX_SPI_PND_RX_OVERRUN_CLR |
1157 S3C64XX_SPI_PND_RX_UNDERRUN_CLR |
1158 S3C64XX_SPI_PND_TX_OVERRUN_CLR |
1159 S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
1160 writel(val, regs + S3C64XX_SPI_PENDING_CLR);
1161 writel(0, regs + S3C64XX_SPI_PENDING_CLR);
1163 writel(0, regs + S3C64XX_SPI_SWAP_CFG);
1165 val = readl(regs + S3C64XX_SPI_MODE_CFG);
1166 val &= ~S3C64XX_SPI_MODE_4BURST;
1167 val &= ~(S3C64XX_SPI_MAX_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
1168 val |= (S3C64XX_SPI_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
1169 writel(val, regs + S3C64XX_SPI_MODE_CFG);
1175 static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
1177 struct s3c64xx_spi_info *sci;
1180 sci = devm_kzalloc(dev, sizeof(*sci), GFP_KERNEL);
1182 dev_err(dev, "memory allocation for spi_info failed\n");
1183 return ERR_PTR(-ENOMEM);
1186 if (of_property_read_u32(dev->of_node, "samsung,spi-src-clk", &temp)) {
1187 dev_warn(dev, "spi bus clock parent not specified, using clock at index 0 as parent\n");
1188 sci->src_clk_nr = 0;
1190 sci->src_clk_nr = temp;
1193 if (of_property_read_u32(dev->of_node, "num-cs", &temp)) {
1194 dev_warn(dev, "number of chip select lines not specified, assuming 1 chip select line\n");
1203 static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
1205 return dev->platform_data;
1209 static const struct of_device_id s3c64xx_spi_dt_match[];
1211 static inline struct s3c64xx_spi_port_config *s3c64xx_spi_get_port_config(
1212 struct platform_device *pdev)
1215 if (pdev->dev.of_node) {
1216 const struct of_device_id *match;
1217 match = of_match_node(s3c64xx_spi_dt_match, pdev->dev.of_node);
1218 return (struct s3c64xx_spi_port_config *)match->data;
1221 return (struct s3c64xx_spi_port_config *)
1222 platform_get_device_id(pdev)->driver_data;
1225 static int s3c64xx_spi_probe(struct platform_device *pdev)
1227 struct resource *mem_res;
1228 struct resource *res;
1229 struct s3c64xx_spi_driver_data *sdd;
1230 struct s3c64xx_spi_info *sci = pdev->dev.platform_data;
1231 struct spi_master *master;
1235 if (!sci && pdev->dev.of_node) {
1236 sci = s3c64xx_spi_parse_dt(&pdev->dev);
1238 return PTR_ERR(sci);
1242 dev_err(&pdev->dev, "platform_data missing!\n");
1246 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1247 if (mem_res == NULL) {
1248 dev_err(&pdev->dev, "Unable to get SPI MEM resource\n");
1252 irq = platform_get_irq(pdev, 0);
1254 dev_warn(&pdev->dev, "Failed to get IRQ: %d\n", irq);
1258 master = spi_alloc_master(&pdev->dev,
1259 sizeof(struct s3c64xx_spi_driver_data));
1260 if (master == NULL) {
1261 dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
1265 platform_set_drvdata(pdev, master);
1267 sdd = spi_master_get_devdata(master);
1268 sdd->port_conf = s3c64xx_spi_get_port_config(pdev);
1269 sdd->master = master;
1270 sdd->cntrlr_info = sci;
1272 sdd->sfr_start = mem_res->start;
1273 if (pdev->dev.of_node) {
1274 ret = of_alias_get_id(pdev->dev.of_node, "spi");
1276 dev_err(&pdev->dev, "failed to get alias id, errno %d\n",
1282 sdd->port_id = pdev->id;
1287 if (!sdd->pdev->dev.of_node) {
1288 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1290 dev_err(&pdev->dev, "Unable to get SPI tx dma "
1294 sdd->tx_dma.dmach = res->start;
1296 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1298 dev_err(&pdev->dev, "Unable to get SPI rx dma "
1302 sdd->rx_dma.dmach = res->start;
1305 sdd->tx_dma.direction = DMA_MEM_TO_DEV;
1306 sdd->rx_dma.direction = DMA_DEV_TO_MEM;
1308 master->dev.of_node = pdev->dev.of_node;
1309 master->bus_num = sdd->port_id;
1310 master->setup = s3c64xx_spi_setup;
1311 master->cleanup = s3c64xx_spi_cleanup;
1312 master->prepare_transfer_hardware = s3c64xx_spi_prepare_transfer;
1313 master->transfer_one_message = s3c64xx_spi_transfer_one_message;
1314 master->unprepare_transfer_hardware = s3c64xx_spi_unprepare_transfer;
1315 master->num_chipselect = sci->num_cs;
1316 master->dma_alignment = 8;
1317 master->bits_per_word_mask = BIT(32 - 1) | BIT(16 - 1) | BIT(8 - 1);
1318 /* the spi->mode bits understood by this driver: */
1319 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1321 sdd->regs = devm_ioremap_resource(&pdev->dev, mem_res);
1322 if (IS_ERR(sdd->regs)) {
1323 ret = PTR_ERR(sdd->regs);
1327 if (sci->cfg_gpio && sci->cfg_gpio()) {
1328 dev_err(&pdev->dev, "Unable to config gpio\n");
1334 sdd->clk = devm_clk_get(&pdev->dev, "spi");
1335 if (IS_ERR(sdd->clk)) {
1336 dev_err(&pdev->dev, "Unable to acquire clock 'spi'\n");
1337 ret = PTR_ERR(sdd->clk);
1341 if (clk_prepare_enable(sdd->clk)) {
1342 dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n");
1347 sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr);
1348 sdd->src_clk = devm_clk_get(&pdev->dev, clk_name);
1349 if (IS_ERR(sdd->src_clk)) {
1351 "Unable to acquire clock '%s'\n", clk_name);
1352 ret = PTR_ERR(sdd->src_clk);
1356 if (clk_prepare_enable(sdd->src_clk)) {
1357 dev_err(&pdev->dev, "Couldn't enable clock '%s'\n", clk_name);
1362 /* Setup Deufult Mode */
1363 s3c64xx_spi_hwinit(sdd, sdd->port_id);
1365 spin_lock_init(&sdd->lock);
1366 init_completion(&sdd->xfer_completion);
1367 INIT_LIST_HEAD(&sdd->queue);
1369 ret = devm_request_irq(&pdev->dev, irq, s3c64xx_spi_irq, 0,
1370 "spi-s3c64xx", sdd);
1372 dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n",
1377 writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
1378 S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
1379 sdd->regs + S3C64XX_SPI_INT_EN);
1381 if (spi_register_master(master)) {
1382 dev_err(&pdev->dev, "cannot register SPI master\n");
1387 dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d with %d Slaves attached\n",
1388 sdd->port_id, master->num_chipselect);
1389 dev_dbg(&pdev->dev, "\tIOmem=[0x%x-0x%x]\tDMA=[Rx-%d, Tx-%d]\n",
1390 mem_res->end, mem_res->start,
1391 sdd->rx_dma.dmach, sdd->tx_dma.dmach);
1393 pm_runtime_enable(&pdev->dev);
1398 clk_disable_unprepare(sdd->src_clk);
1400 clk_disable_unprepare(sdd->clk);
1402 platform_set_drvdata(pdev, NULL);
1403 spi_master_put(master);
1408 static int s3c64xx_spi_remove(struct platform_device *pdev)
1410 struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
1411 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1413 pm_runtime_disable(&pdev->dev);
1415 spi_unregister_master(master);
1417 writel(0, sdd->regs + S3C64XX_SPI_INT_EN);
1419 clk_disable_unprepare(sdd->src_clk);
1421 clk_disable_unprepare(sdd->clk);
1423 platform_set_drvdata(pdev, NULL);
1424 spi_master_put(master);
1429 #ifdef CONFIG_PM_SLEEP
1430 static int s3c64xx_spi_suspend(struct device *dev)
1432 struct spi_master *master = dev_get_drvdata(dev);
1433 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1435 spi_master_suspend(master);
1437 /* Disable the clock */
1438 clk_disable_unprepare(sdd->src_clk);
1439 clk_disable_unprepare(sdd->clk);
1441 sdd->cur_speed = 0; /* Output Clock is stopped */
1446 static int s3c64xx_spi_resume(struct device *dev)
1448 struct spi_master *master = dev_get_drvdata(dev);
1449 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1450 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
1455 /* Enable the clock */
1456 clk_prepare_enable(sdd->src_clk);
1457 clk_prepare_enable(sdd->clk);
1459 s3c64xx_spi_hwinit(sdd, sdd->port_id);
1461 spi_master_resume(master);
1465 #endif /* CONFIG_PM_SLEEP */
1467 #ifdef CONFIG_PM_RUNTIME
1468 static int s3c64xx_spi_runtime_suspend(struct device *dev)
1470 struct spi_master *master = dev_get_drvdata(dev);
1471 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1473 clk_disable_unprepare(sdd->clk);
1474 clk_disable_unprepare(sdd->src_clk);
1479 static int s3c64xx_spi_runtime_resume(struct device *dev)
1481 struct spi_master *master = dev_get_drvdata(dev);
1482 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1484 clk_prepare_enable(sdd->src_clk);
1485 clk_prepare_enable(sdd->clk);
1489 #endif /* CONFIG_PM_RUNTIME */
1491 static const struct dev_pm_ops s3c64xx_spi_pm = {
1492 SET_SYSTEM_SLEEP_PM_OPS(s3c64xx_spi_suspend, s3c64xx_spi_resume)
1493 SET_RUNTIME_PM_OPS(s3c64xx_spi_runtime_suspend,
1494 s3c64xx_spi_runtime_resume, NULL)
1497 static struct s3c64xx_spi_port_config s3c2443_spi_port_config = {
1498 .fifo_lvl_mask = { 0x7f },
1499 .rx_lvl_offset = 13,
1504 static struct s3c64xx_spi_port_config s3c6410_spi_port_config = {
1505 .fifo_lvl_mask = { 0x7f, 0x7F },
1506 .rx_lvl_offset = 13,
1510 static struct s3c64xx_spi_port_config s5p64x0_spi_port_config = {
1511 .fifo_lvl_mask = { 0x1ff, 0x7F },
1512 .rx_lvl_offset = 15,
1516 static struct s3c64xx_spi_port_config s5pc100_spi_port_config = {
1517 .fifo_lvl_mask = { 0x7f, 0x7F },
1518 .rx_lvl_offset = 13,
1523 static struct s3c64xx_spi_port_config s5pv210_spi_port_config = {
1524 .fifo_lvl_mask = { 0x1ff, 0x7F },
1525 .rx_lvl_offset = 15,
1530 static struct s3c64xx_spi_port_config exynos4_spi_port_config = {
1531 .fifo_lvl_mask = { 0x1ff, 0x7F, 0x7F },
1532 .rx_lvl_offset = 15,
1535 .clk_from_cmu = true,
1538 static struct platform_device_id s3c64xx_spi_driver_ids[] = {
1540 .name = "s3c2443-spi",
1541 .driver_data = (kernel_ulong_t)&s3c2443_spi_port_config,
1543 .name = "s3c6410-spi",
1544 .driver_data = (kernel_ulong_t)&s3c6410_spi_port_config,
1546 .name = "s5p64x0-spi",
1547 .driver_data = (kernel_ulong_t)&s5p64x0_spi_port_config,
1549 .name = "s5pc100-spi",
1550 .driver_data = (kernel_ulong_t)&s5pc100_spi_port_config,
1552 .name = "s5pv210-spi",
1553 .driver_data = (kernel_ulong_t)&s5pv210_spi_port_config,
1555 .name = "exynos4210-spi",
1556 .driver_data = (kernel_ulong_t)&exynos4_spi_port_config,
1562 static const struct of_device_id s3c64xx_spi_dt_match[] = {
1563 { .compatible = "samsung,exynos4210-spi",
1564 .data = (void *)&exynos4_spi_port_config,
1568 MODULE_DEVICE_TABLE(of, s3c64xx_spi_dt_match);
1569 #endif /* CONFIG_OF */
1571 static struct platform_driver s3c64xx_spi_driver = {
1573 .name = "s3c64xx-spi",
1574 .owner = THIS_MODULE,
1575 .pm = &s3c64xx_spi_pm,
1576 .of_match_table = of_match_ptr(s3c64xx_spi_dt_match),
1578 .remove = s3c64xx_spi_remove,
1579 .id_table = s3c64xx_spi_driver_ids,
1581 MODULE_ALIAS("platform:s3c64xx-spi");
1583 static int __init s3c64xx_spi_init(void)
1585 return platform_driver_probe(&s3c64xx_spi_driver, s3c64xx_spi_probe);
1587 subsys_initcall(s3c64xx_spi_init);
1589 static void __exit s3c64xx_spi_exit(void)
1591 platform_driver_unregister(&s3c64xx_spi_driver);
1593 module_exit(s3c64xx_spi_exit);
1595 MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
1596 MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
1597 MODULE_LICENSE("GPL");