1 // SPDX-License-Identifier: GPL-2.0+
3 // Copyright (c) 2009 Samsung Electronics Co., Ltd.
4 // Jaswinder Singh <jassi.brar@samsung.com>
6 #include <linux/init.h>
7 #include <linux/module.h>
8 #include <linux/interrupt.h>
9 #include <linux/delay.h>
10 #include <linux/clk.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/dmaengine.h>
13 #include <linux/platform_device.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/spi/spi.h>
17 #include <linux/of_device.h>
19 #include <linux/platform_data/spi-s3c64xx.h>
21 #define MAX_SPI_PORTS 12
22 #define S3C64XX_SPI_QUIRK_POLL (1 << 0)
23 #define S3C64XX_SPI_QUIRK_CS_AUTO (1 << 1)
24 #define AUTOSUSPEND_TIMEOUT 2000
26 /* Registers and bit-fields */
28 #define S3C64XX_SPI_CH_CFG 0x00
29 #define S3C64XX_SPI_CLK_CFG 0x04
30 #define S3C64XX_SPI_MODE_CFG 0x08
31 #define S3C64XX_SPI_CS_REG 0x0C
32 #define S3C64XX_SPI_INT_EN 0x10
33 #define S3C64XX_SPI_STATUS 0x14
34 #define S3C64XX_SPI_TX_DATA 0x18
35 #define S3C64XX_SPI_RX_DATA 0x1C
36 #define S3C64XX_SPI_PACKET_CNT 0x20
37 #define S3C64XX_SPI_PENDING_CLR 0x24
38 #define S3C64XX_SPI_SWAP_CFG 0x28
39 #define S3C64XX_SPI_FB_CLK 0x2C
41 #define S3C64XX_SPI_CH_HS_EN (1<<6) /* High Speed Enable */
42 #define S3C64XX_SPI_CH_SW_RST (1<<5)
43 #define S3C64XX_SPI_CH_SLAVE (1<<4)
44 #define S3C64XX_SPI_CPOL_L (1<<3)
45 #define S3C64XX_SPI_CPHA_B (1<<2)
46 #define S3C64XX_SPI_CH_RXCH_ON (1<<1)
47 #define S3C64XX_SPI_CH_TXCH_ON (1<<0)
49 #define S3C64XX_SPI_CLKSEL_SRCMSK (3<<9)
50 #define S3C64XX_SPI_CLKSEL_SRCSHFT 9
51 #define S3C64XX_SPI_ENCLK_ENABLE (1<<8)
52 #define S3C64XX_SPI_PSR_MASK 0xff
54 #define S3C64XX_SPI_MODE_CH_TSZ_BYTE (0<<29)
55 #define S3C64XX_SPI_MODE_CH_TSZ_HALFWORD (1<<29)
56 #define S3C64XX_SPI_MODE_CH_TSZ_WORD (2<<29)
57 #define S3C64XX_SPI_MODE_CH_TSZ_MASK (3<<29)
58 #define S3C64XX_SPI_MODE_BUS_TSZ_BYTE (0<<17)
59 #define S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD (1<<17)
60 #define S3C64XX_SPI_MODE_BUS_TSZ_WORD (2<<17)
61 #define S3C64XX_SPI_MODE_BUS_TSZ_MASK (3<<17)
62 #define S3C64XX_SPI_MODE_SELF_LOOPBACK (1<<3)
63 #define S3C64XX_SPI_MODE_RXDMA_ON (1<<2)
64 #define S3C64XX_SPI_MODE_TXDMA_ON (1<<1)
65 #define S3C64XX_SPI_MODE_4BURST (1<<0)
67 #define S3C64XX_SPI_CS_NSC_CNT_2 (2<<4)
68 #define S3C64XX_SPI_CS_AUTO (1<<1)
69 #define S3C64XX_SPI_CS_SIG_INACT (1<<0)
71 #define S3C64XX_SPI_INT_TRAILING_EN (1<<6)
72 #define S3C64XX_SPI_INT_RX_OVERRUN_EN (1<<5)
73 #define S3C64XX_SPI_INT_RX_UNDERRUN_EN (1<<4)
74 #define S3C64XX_SPI_INT_TX_OVERRUN_EN (1<<3)
75 #define S3C64XX_SPI_INT_TX_UNDERRUN_EN (1<<2)
76 #define S3C64XX_SPI_INT_RX_FIFORDY_EN (1<<1)
77 #define S3C64XX_SPI_INT_TX_FIFORDY_EN (1<<0)
79 #define S3C64XX_SPI_ST_RX_OVERRUN_ERR (1<<5)
80 #define S3C64XX_SPI_ST_RX_UNDERRUN_ERR (1<<4)
81 #define S3C64XX_SPI_ST_TX_OVERRUN_ERR (1<<3)
82 #define S3C64XX_SPI_ST_TX_UNDERRUN_ERR (1<<2)
83 #define S3C64XX_SPI_ST_RX_FIFORDY (1<<1)
84 #define S3C64XX_SPI_ST_TX_FIFORDY (1<<0)
86 #define S3C64XX_SPI_PACKET_CNT_EN (1<<16)
87 #define S3C64XX_SPI_PACKET_CNT_MASK GENMASK(15, 0)
89 #define S3C64XX_SPI_PND_TX_UNDERRUN_CLR (1<<4)
90 #define S3C64XX_SPI_PND_TX_OVERRUN_CLR (1<<3)
91 #define S3C64XX_SPI_PND_RX_UNDERRUN_CLR (1<<2)
92 #define S3C64XX_SPI_PND_RX_OVERRUN_CLR (1<<1)
93 #define S3C64XX_SPI_PND_TRAILING_CLR (1<<0)
95 #define S3C64XX_SPI_SWAP_RX_HALF_WORD (1<<7)
96 #define S3C64XX_SPI_SWAP_RX_BYTE (1<<6)
97 #define S3C64XX_SPI_SWAP_RX_BIT (1<<5)
98 #define S3C64XX_SPI_SWAP_RX_EN (1<<4)
99 #define S3C64XX_SPI_SWAP_TX_HALF_WORD (1<<3)
100 #define S3C64XX_SPI_SWAP_TX_BYTE (1<<2)
101 #define S3C64XX_SPI_SWAP_TX_BIT (1<<1)
102 #define S3C64XX_SPI_SWAP_TX_EN (1<<0)
104 #define S3C64XX_SPI_FBCLK_MSK (3<<0)
106 #define FIFO_LVL_MASK(i) ((i)->port_conf->fifo_lvl_mask[i->port_id])
107 #define S3C64XX_SPI_ST_TX_DONE(v, i) (((v) & \
108 (1 << (i)->port_conf->tx_st_done)) ? 1 : 0)
109 #define TX_FIFO_LVL(v, i) (((v) >> 6) & FIFO_LVL_MASK(i))
110 #define RX_FIFO_LVL(v, i) (((v) >> (i)->port_conf->rx_lvl_offset) & \
113 #define S3C64XX_SPI_MAX_TRAILCNT 0x3ff
114 #define S3C64XX_SPI_TRAILCNT_OFF 19
116 #define S3C64XX_SPI_TRAILCNT S3C64XX_SPI_MAX_TRAILCNT
118 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
119 #define is_polling(x) (x->port_conf->quirks & S3C64XX_SPI_QUIRK_POLL)
121 #define RXBUSY (1<<2)
122 #define TXBUSY (1<<3)
124 struct s3c64xx_spi_dma_data {
127 enum dma_transfer_direction direction;
131 * struct s3c64xx_spi_port_config - SPI Controller hardware info
132 * @fifo_lvl_mask: Bit-mask for {TX|RX}_FIFO_LVL bits in SPI_STATUS register.
133 * @rx_lvl_offset: Bit offset of RX_FIFO_LVL bits in SPI_STATUS regiter.
134 * @tx_st_done: Bit offset of TX_DONE bit in SPI_STATUS regiter.
135 * @clk_div: Internal clock divider
136 * @quirks: Bitmask of known quirks
137 * @high_speed: True, if the controller supports HIGH_SPEED_EN bit.
138 * @clk_from_cmu: True, if the controller does not include a clock mux and
140 * @clk_ioclk: True if clock is present on this device
141 * @has_loopback: True if loopback mode can be supported
143 * The Samsung s3c64xx SPI controller are used on various Samsung SoC's but
144 * differ in some aspects such as the size of the fifo and spi bus clock
145 * setup. Such differences are specified to the driver using this structure
146 * which is provided as driver data to the driver.
148 struct s3c64xx_spi_port_config {
149 int fifo_lvl_mask[MAX_SPI_PORTS];
161 * struct s3c64xx_spi_driver_data - Runtime info holder for SPI driver.
162 * @clk: Pointer to the spi clock.
163 * @src_clk: Pointer to the clock used to generate SPI signals.
164 * @ioclk: Pointer to the i/o clock between master and slave
165 * @pdev: Pointer to device's platform device data
166 * @master: Pointer to the SPI Protocol master.
167 * @cntrlr_info: Platform specific data for the controller this driver manages.
168 * @lock: Controller specific lock.
169 * @state: Set of FLAGS to indicate status.
170 * @sfr_start: BUS address of SPI controller regs.
171 * @regs: Pointer to ioremap'ed controller registers.
172 * @xfer_completion: To indicate completion of xfer task.
173 * @cur_mode: Stores the active configuration of the controller.
174 * @cur_bpw: Stores the active bits per word settings.
175 * @cur_speed: Current clock speed
176 * @rx_dma: Local receive DMA data (e.g. chan and direction)
177 * @tx_dma: Local transmit DMA data (e.g. chan and direction)
178 * @port_conf: Local SPI port configuartion data
179 * @port_id: Port identification number
181 struct s3c64xx_spi_driver_data {
186 struct platform_device *pdev;
187 struct spi_master *master;
188 struct s3c64xx_spi_info *cntrlr_info;
190 unsigned long sfr_start;
191 struct completion xfer_completion;
193 unsigned cur_mode, cur_bpw;
195 struct s3c64xx_spi_dma_data rx_dma;
196 struct s3c64xx_spi_dma_data tx_dma;
197 const struct s3c64xx_spi_port_config *port_conf;
198 unsigned int port_id;
201 static void s3c64xx_flush_fifo(struct s3c64xx_spi_driver_data *sdd)
203 void __iomem *regs = sdd->regs;
207 writel(0, regs + S3C64XX_SPI_PACKET_CNT);
209 val = readl(regs + S3C64XX_SPI_CH_CFG);
210 val &= ~(S3C64XX_SPI_CH_RXCH_ON | S3C64XX_SPI_CH_TXCH_ON);
211 writel(val, regs + S3C64XX_SPI_CH_CFG);
213 val = readl(regs + S3C64XX_SPI_CH_CFG);
214 val |= S3C64XX_SPI_CH_SW_RST;
215 val &= ~S3C64XX_SPI_CH_HS_EN;
216 writel(val, regs + S3C64XX_SPI_CH_CFG);
219 loops = msecs_to_loops(1);
221 val = readl(regs + S3C64XX_SPI_STATUS);
222 } while (TX_FIFO_LVL(val, sdd) && loops--);
225 dev_warn(&sdd->pdev->dev, "Timed out flushing TX FIFO\n");
228 loops = msecs_to_loops(1);
230 val = readl(regs + S3C64XX_SPI_STATUS);
231 if (RX_FIFO_LVL(val, sdd))
232 readl(regs + S3C64XX_SPI_RX_DATA);
238 dev_warn(&sdd->pdev->dev, "Timed out flushing RX FIFO\n");
240 val = readl(regs + S3C64XX_SPI_CH_CFG);
241 val &= ~S3C64XX_SPI_CH_SW_RST;
242 writel(val, regs + S3C64XX_SPI_CH_CFG);
244 val = readl(regs + S3C64XX_SPI_MODE_CFG);
245 val &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
246 writel(val, regs + S3C64XX_SPI_MODE_CFG);
249 static void s3c64xx_spi_dmacb(void *data)
251 struct s3c64xx_spi_driver_data *sdd;
252 struct s3c64xx_spi_dma_data *dma = data;
255 if (dma->direction == DMA_DEV_TO_MEM)
256 sdd = container_of(data,
257 struct s3c64xx_spi_driver_data, rx_dma);
259 sdd = container_of(data,
260 struct s3c64xx_spi_driver_data, tx_dma);
262 spin_lock_irqsave(&sdd->lock, flags);
264 if (dma->direction == DMA_DEV_TO_MEM) {
265 sdd->state &= ~RXBUSY;
266 if (!(sdd->state & TXBUSY))
267 complete(&sdd->xfer_completion);
269 sdd->state &= ~TXBUSY;
270 if (!(sdd->state & RXBUSY))
271 complete(&sdd->xfer_completion);
274 spin_unlock_irqrestore(&sdd->lock, flags);
277 static int prepare_dma(struct s3c64xx_spi_dma_data *dma,
278 struct sg_table *sgt)
280 struct s3c64xx_spi_driver_data *sdd;
281 struct dma_slave_config config;
282 struct dma_async_tx_descriptor *desc;
285 memset(&config, 0, sizeof(config));
287 if (dma->direction == DMA_DEV_TO_MEM) {
288 sdd = container_of((void *)dma,
289 struct s3c64xx_spi_driver_data, rx_dma);
290 config.direction = dma->direction;
291 config.src_addr = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
292 config.src_addr_width = sdd->cur_bpw / 8;
293 config.src_maxburst = 1;
294 dmaengine_slave_config(dma->ch, &config);
296 sdd = container_of((void *)dma,
297 struct s3c64xx_spi_driver_data, tx_dma);
298 config.direction = dma->direction;
299 config.dst_addr = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
300 config.dst_addr_width = sdd->cur_bpw / 8;
301 config.dst_maxburst = 1;
302 dmaengine_slave_config(dma->ch, &config);
305 desc = dmaengine_prep_slave_sg(dma->ch, sgt->sgl, sgt->nents,
306 dma->direction, DMA_PREP_INTERRUPT);
308 dev_err(&sdd->pdev->dev, "unable to prepare %s scatterlist",
309 dma->direction == DMA_DEV_TO_MEM ? "rx" : "tx");
313 desc->callback = s3c64xx_spi_dmacb;
314 desc->callback_param = dma;
316 dma->cookie = dmaengine_submit(desc);
317 ret = dma_submit_error(dma->cookie);
319 dev_err(&sdd->pdev->dev, "DMA submission failed");
323 dma_async_issue_pending(dma->ch);
327 static void s3c64xx_spi_set_cs(struct spi_device *spi, bool enable)
329 struct s3c64xx_spi_driver_data *sdd =
330 spi_master_get_devdata(spi->master);
332 if (sdd->cntrlr_info->no_cs)
336 if (!(sdd->port_conf->quirks & S3C64XX_SPI_QUIRK_CS_AUTO)) {
337 writel(0, sdd->regs + S3C64XX_SPI_CS_REG);
339 u32 ssel = readl(sdd->regs + S3C64XX_SPI_CS_REG);
341 ssel |= (S3C64XX_SPI_CS_AUTO |
342 S3C64XX_SPI_CS_NSC_CNT_2);
343 writel(ssel, sdd->regs + S3C64XX_SPI_CS_REG);
346 if (!(sdd->port_conf->quirks & S3C64XX_SPI_QUIRK_CS_AUTO))
347 writel(S3C64XX_SPI_CS_SIG_INACT,
348 sdd->regs + S3C64XX_SPI_CS_REG);
352 static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
354 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
359 /* Requests DMA channels */
360 sdd->rx_dma.ch = dma_request_chan(&sdd->pdev->dev, "rx");
361 if (IS_ERR(sdd->rx_dma.ch)) {
362 dev_err(&sdd->pdev->dev, "Failed to get RX DMA channel\n");
363 sdd->rx_dma.ch = NULL;
367 sdd->tx_dma.ch = dma_request_chan(&sdd->pdev->dev, "tx");
368 if (IS_ERR(sdd->tx_dma.ch)) {
369 dev_err(&sdd->pdev->dev, "Failed to get TX DMA channel\n");
370 dma_release_channel(sdd->rx_dma.ch);
371 sdd->tx_dma.ch = NULL;
372 sdd->rx_dma.ch = NULL;
376 spi->dma_rx = sdd->rx_dma.ch;
377 spi->dma_tx = sdd->tx_dma.ch;
382 static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
384 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
389 /* Releases DMA channels if they are allocated */
390 if (sdd->rx_dma.ch && sdd->tx_dma.ch) {
391 dma_release_channel(sdd->rx_dma.ch);
392 dma_release_channel(sdd->tx_dma.ch);
393 sdd->rx_dma.ch = NULL;
394 sdd->tx_dma.ch = NULL;
400 static bool s3c64xx_spi_can_dma(struct spi_master *master,
401 struct spi_device *spi,
402 struct spi_transfer *xfer)
404 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
406 if (sdd->rx_dma.ch && sdd->tx_dma.ch) {
407 return xfer->len > (FIFO_LVL_MASK(sdd) >> 1) + 1;
414 static int s3c64xx_enable_datapath(struct s3c64xx_spi_driver_data *sdd,
415 struct spi_transfer *xfer, int dma_mode)
417 void __iomem *regs = sdd->regs;
421 modecfg = readl(regs + S3C64XX_SPI_MODE_CFG);
422 modecfg &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
424 chcfg = readl(regs + S3C64XX_SPI_CH_CFG);
425 chcfg &= ~S3C64XX_SPI_CH_TXCH_ON;
428 chcfg &= ~S3C64XX_SPI_CH_RXCH_ON;
430 /* Always shift in data in FIFO, even if xfer is Tx only,
431 * this helps setting PCKT_CNT value for generating clocks
434 chcfg |= S3C64XX_SPI_CH_RXCH_ON;
435 writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
436 | S3C64XX_SPI_PACKET_CNT_EN,
437 regs + S3C64XX_SPI_PACKET_CNT);
440 if (xfer->tx_buf != NULL) {
441 sdd->state |= TXBUSY;
442 chcfg |= S3C64XX_SPI_CH_TXCH_ON;
444 modecfg |= S3C64XX_SPI_MODE_TXDMA_ON;
445 ret = prepare_dma(&sdd->tx_dma, &xfer->tx_sg);
447 switch (sdd->cur_bpw) {
449 iowrite32_rep(regs + S3C64XX_SPI_TX_DATA,
450 xfer->tx_buf, xfer->len / 4);
453 iowrite16_rep(regs + S3C64XX_SPI_TX_DATA,
454 xfer->tx_buf, xfer->len / 2);
457 iowrite8_rep(regs + S3C64XX_SPI_TX_DATA,
458 xfer->tx_buf, xfer->len);
464 if (xfer->rx_buf != NULL) {
465 sdd->state |= RXBUSY;
467 if (sdd->port_conf->high_speed && sdd->cur_speed >= 30000000UL
468 && !(sdd->cur_mode & SPI_CPHA))
469 chcfg |= S3C64XX_SPI_CH_HS_EN;
472 modecfg |= S3C64XX_SPI_MODE_RXDMA_ON;
473 chcfg |= S3C64XX_SPI_CH_RXCH_ON;
474 writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
475 | S3C64XX_SPI_PACKET_CNT_EN,
476 regs + S3C64XX_SPI_PACKET_CNT);
477 ret = prepare_dma(&sdd->rx_dma, &xfer->rx_sg);
484 writel(modecfg, regs + S3C64XX_SPI_MODE_CFG);
485 writel(chcfg, regs + S3C64XX_SPI_CH_CFG);
490 static u32 s3c64xx_spi_wait_for_timeout(struct s3c64xx_spi_driver_data *sdd,
493 void __iomem *regs = sdd->regs;
494 unsigned long val = 1;
497 /* max fifo depth available */
498 u32 max_fifo = (FIFO_LVL_MASK(sdd) >> 1) + 1;
501 val = msecs_to_loops(timeout_ms);
504 status = readl(regs + S3C64XX_SPI_STATUS);
505 } while (RX_FIFO_LVL(status, sdd) < max_fifo && --val);
507 /* return the actual received data length */
508 return RX_FIFO_LVL(status, sdd);
511 static int s3c64xx_wait_for_dma(struct s3c64xx_spi_driver_data *sdd,
512 struct spi_transfer *xfer)
514 void __iomem *regs = sdd->regs;
519 /* millisecs to xfer 'len' bytes @ 'cur_speed' */
520 ms = xfer->len * 8 * 1000 / sdd->cur_speed;
521 ms += 30; /* some tolerance */
522 ms = max(ms, 100); /* minimum timeout */
524 val = msecs_to_jiffies(ms) + 10;
525 val = wait_for_completion_timeout(&sdd->xfer_completion, val);
528 * If the previous xfer was completed within timeout, then
529 * proceed further else return -EIO.
530 * DmaTx returns after simply writing data in the FIFO,
531 * w/o waiting for real transmission on the bus to finish.
532 * DmaRx returns only after Dma read data from FIFO which
533 * needs bus transmission to finish, so we don't worry if
534 * Xfer involved Rx(with or without Tx).
536 if (val && !xfer->rx_buf) {
537 val = msecs_to_loops(10);
538 status = readl(regs + S3C64XX_SPI_STATUS);
539 while ((TX_FIFO_LVL(status, sdd)
540 || !S3C64XX_SPI_ST_TX_DONE(status, sdd))
543 status = readl(regs + S3C64XX_SPI_STATUS);
548 /* If timed out while checking rx/tx status return error */
555 static int s3c64xx_wait_for_pio(struct s3c64xx_spi_driver_data *sdd,
556 struct spi_transfer *xfer)
558 void __iomem *regs = sdd->regs;
566 /* millisecs to xfer 'len' bytes @ 'cur_speed' */
567 ms = xfer->len * 8 * 1000 / sdd->cur_speed;
568 ms += 10; /* some tolerance */
570 val = msecs_to_loops(ms);
572 status = readl(regs + S3C64XX_SPI_STATUS);
573 } while (RX_FIFO_LVL(status, sdd) < xfer->len && --val);
578 /* If it was only Tx */
580 sdd->state &= ~TXBUSY;
585 * If the receive length is bigger than the controller fifo
586 * size, calculate the loops and read the fifo as many times.
587 * loops = length / max fifo size (calculated by using the
589 * For any size less than the fifo size the below code is
590 * executed atleast once.
592 loops = xfer->len / ((FIFO_LVL_MASK(sdd) >> 1) + 1);
595 /* wait for data to be received in the fifo */
596 cpy_len = s3c64xx_spi_wait_for_timeout(sdd,
599 switch (sdd->cur_bpw) {
601 ioread32_rep(regs + S3C64XX_SPI_RX_DATA,
605 ioread16_rep(regs + S3C64XX_SPI_RX_DATA,
609 ioread8_rep(regs + S3C64XX_SPI_RX_DATA,
616 sdd->state &= ~RXBUSY;
621 static int s3c64xx_spi_config(struct s3c64xx_spi_driver_data *sdd)
623 void __iomem *regs = sdd->regs;
626 int div = sdd->port_conf->clk_div;
629 if (!sdd->port_conf->clk_from_cmu) {
630 val = readl(regs + S3C64XX_SPI_CLK_CFG);
631 val &= ~S3C64XX_SPI_ENCLK_ENABLE;
632 writel(val, regs + S3C64XX_SPI_CLK_CFG);
635 /* Set Polarity and Phase */
636 val = readl(regs + S3C64XX_SPI_CH_CFG);
637 val &= ~(S3C64XX_SPI_CH_SLAVE |
641 if (sdd->cur_mode & SPI_CPOL)
642 val |= S3C64XX_SPI_CPOL_L;
644 if (sdd->cur_mode & SPI_CPHA)
645 val |= S3C64XX_SPI_CPHA_B;
647 writel(val, regs + S3C64XX_SPI_CH_CFG);
649 /* Set Channel & DMA Mode */
650 val = readl(regs + S3C64XX_SPI_MODE_CFG);
651 val &= ~(S3C64XX_SPI_MODE_BUS_TSZ_MASK
652 | S3C64XX_SPI_MODE_CH_TSZ_MASK);
654 switch (sdd->cur_bpw) {
656 val |= S3C64XX_SPI_MODE_BUS_TSZ_WORD;
657 val |= S3C64XX_SPI_MODE_CH_TSZ_WORD;
660 val |= S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD;
661 val |= S3C64XX_SPI_MODE_CH_TSZ_HALFWORD;
664 val |= S3C64XX_SPI_MODE_BUS_TSZ_BYTE;
665 val |= S3C64XX_SPI_MODE_CH_TSZ_BYTE;
669 if ((sdd->cur_mode & SPI_LOOP) && sdd->port_conf->has_loopback)
670 val |= S3C64XX_SPI_MODE_SELF_LOOPBACK;
672 writel(val, regs + S3C64XX_SPI_MODE_CFG);
674 if (sdd->port_conf->clk_from_cmu) {
675 ret = clk_set_rate(sdd->src_clk, sdd->cur_speed * div);
678 sdd->cur_speed = clk_get_rate(sdd->src_clk) / div;
680 /* Configure Clock */
681 val = readl(regs + S3C64XX_SPI_CLK_CFG);
682 val &= ~S3C64XX_SPI_PSR_MASK;
683 val |= ((clk_get_rate(sdd->src_clk) / sdd->cur_speed / div - 1)
684 & S3C64XX_SPI_PSR_MASK);
685 writel(val, regs + S3C64XX_SPI_CLK_CFG);
688 val = readl(regs + S3C64XX_SPI_CLK_CFG);
689 val |= S3C64XX_SPI_ENCLK_ENABLE;
690 writel(val, regs + S3C64XX_SPI_CLK_CFG);
696 #define XFER_DMAADDR_INVALID DMA_BIT_MASK(32)
698 static int s3c64xx_spi_prepare_message(struct spi_master *master,
699 struct spi_message *msg)
701 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
702 struct spi_device *spi = msg->spi;
703 struct s3c64xx_spi_csinfo *cs = spi->controller_data;
705 /* Configure feedback delay */
707 /* No delay if not defined */
708 writel(0, sdd->regs + S3C64XX_SPI_FB_CLK);
710 writel(cs->fb_delay & 0x3, sdd->regs + S3C64XX_SPI_FB_CLK);
715 static size_t s3c64xx_spi_max_transfer_size(struct spi_device *spi)
717 struct spi_controller *ctlr = spi->controller;
719 return ctlr->can_dma ? S3C64XX_SPI_PACKET_CNT_MASK : SIZE_MAX;
722 static int s3c64xx_spi_transfer_one(struct spi_master *master,
723 struct spi_device *spi,
724 struct spi_transfer *xfer)
726 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
727 const unsigned int fifo_len = (FIFO_LVL_MASK(sdd) >> 1) + 1;
728 const void *tx_buf = NULL;
730 int target_len = 0, origin_len = 0;
737 reinit_completion(&sdd->xfer_completion);
739 /* Only BPW and Speed may change across transfers */
740 bpw = xfer->bits_per_word;
741 speed = xfer->speed_hz;
743 if (bpw != sdd->cur_bpw || speed != sdd->cur_speed) {
745 sdd->cur_speed = speed;
746 sdd->cur_mode = spi->mode;
747 status = s3c64xx_spi_config(sdd);
752 if (!is_polling(sdd) && (xfer->len > fifo_len) &&
753 sdd->rx_dma.ch && sdd->tx_dma.ch) {
756 } else if (xfer->len > fifo_len) {
757 tx_buf = xfer->tx_buf;
758 rx_buf = xfer->rx_buf;
759 origin_len = xfer->len;
761 target_len = xfer->len;
762 if (xfer->len > fifo_len)
763 xfer->len = fifo_len;
767 spin_lock_irqsave(&sdd->lock, flags);
769 /* Pending only which is to be done */
770 sdd->state &= ~RXBUSY;
771 sdd->state &= ~TXBUSY;
773 /* Start the signals */
774 s3c64xx_spi_set_cs(spi, true);
776 status = s3c64xx_enable_datapath(sdd, xfer, use_dma);
778 spin_unlock_irqrestore(&sdd->lock, flags);
781 dev_err(&spi->dev, "failed to enable data path for transfer: %d\n", status);
786 status = s3c64xx_wait_for_dma(sdd, xfer);
788 status = s3c64xx_wait_for_pio(sdd, xfer);
792 "I/O Error: rx-%d tx-%d rx-%c tx-%c len-%d dma-%d res-(%d)\n",
793 xfer->rx_buf ? 1 : 0, xfer->tx_buf ? 1 : 0,
794 (sdd->state & RXBUSY) ? 'f' : 'p',
795 (sdd->state & TXBUSY) ? 'f' : 'p',
796 xfer->len, use_dma ? 1 : 0, status);
799 struct dma_tx_state s;
801 if (xfer->tx_buf && (sdd->state & TXBUSY)) {
802 dmaengine_pause(sdd->tx_dma.ch);
803 dmaengine_tx_status(sdd->tx_dma.ch, sdd->tx_dma.cookie, &s);
804 dmaengine_terminate_all(sdd->tx_dma.ch);
805 dev_err(&spi->dev, "TX residue: %d\n", s.residue);
808 if (xfer->rx_buf && (sdd->state & RXBUSY)) {
809 dmaengine_pause(sdd->rx_dma.ch);
810 dmaengine_tx_status(sdd->rx_dma.ch, sdd->rx_dma.cookie, &s);
811 dmaengine_terminate_all(sdd->rx_dma.ch);
812 dev_err(&spi->dev, "RX residue: %d\n", s.residue);
816 s3c64xx_flush_fifo(sdd);
818 if (target_len > 0) {
819 target_len -= xfer->len;
822 xfer->tx_buf += xfer->len;
825 xfer->rx_buf += xfer->len;
827 if (target_len > fifo_len)
828 xfer->len = fifo_len;
830 xfer->len = target_len;
832 } while (target_len > 0);
835 /* Restore original xfer buffers and length */
836 xfer->tx_buf = tx_buf;
837 xfer->rx_buf = rx_buf;
838 xfer->len = origin_len;
844 static struct s3c64xx_spi_csinfo *s3c64xx_get_slave_ctrldata(
845 struct spi_device *spi)
847 struct s3c64xx_spi_csinfo *cs;
848 struct device_node *slave_np, *data_np = NULL;
851 slave_np = spi->dev.of_node;
853 dev_err(&spi->dev, "device node not found\n");
854 return ERR_PTR(-EINVAL);
857 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
859 return ERR_PTR(-ENOMEM);
861 data_np = of_get_child_by_name(slave_np, "controller-data");
863 dev_info(&spi->dev, "feedback delay set to default (0)\n");
867 of_property_read_u32(data_np, "samsung,spi-feedback-delay", &fb_delay);
868 cs->fb_delay = fb_delay;
869 of_node_put(data_np);
874 * Here we only check the validity of requested configuration
875 * and save the configuration in a local data-structure.
876 * The controller is actually configured only just before we
877 * get a message to transfer.
879 static int s3c64xx_spi_setup(struct spi_device *spi)
881 struct s3c64xx_spi_csinfo *cs = spi->controller_data;
882 struct s3c64xx_spi_driver_data *sdd;
886 sdd = spi_master_get_devdata(spi->master);
887 if (spi->dev.of_node) {
888 cs = s3c64xx_get_slave_ctrldata(spi);
889 spi->controller_data = cs;
892 /* NULL is fine, we just avoid using the FB delay (=0) */
894 dev_err(&spi->dev, "No CS for SPI(%d)\n", spi->chip_select);
898 if (!spi_get_ctldata(spi))
899 spi_set_ctldata(spi, cs);
901 pm_runtime_get_sync(&sdd->pdev->dev);
903 div = sdd->port_conf->clk_div;
905 /* Check if we can provide the requested rate */
906 if (!sdd->port_conf->clk_from_cmu) {
910 speed = clk_get_rate(sdd->src_clk) / div / (0 + 1);
912 if (spi->max_speed_hz > speed)
913 spi->max_speed_hz = speed;
915 psr = clk_get_rate(sdd->src_clk) / div / spi->max_speed_hz - 1;
916 psr &= S3C64XX_SPI_PSR_MASK;
917 if (psr == S3C64XX_SPI_PSR_MASK)
920 speed = clk_get_rate(sdd->src_clk) / div / (psr + 1);
921 if (spi->max_speed_hz < speed) {
922 if (psr+1 < S3C64XX_SPI_PSR_MASK) {
930 speed = clk_get_rate(sdd->src_clk) / div / (psr + 1);
931 if (spi->max_speed_hz >= speed) {
932 spi->max_speed_hz = speed;
934 dev_err(&spi->dev, "Can't set %dHz transfer speed\n",
941 pm_runtime_mark_last_busy(&sdd->pdev->dev);
942 pm_runtime_put_autosuspend(&sdd->pdev->dev);
943 s3c64xx_spi_set_cs(spi, false);
948 pm_runtime_mark_last_busy(&sdd->pdev->dev);
949 pm_runtime_put_autosuspend(&sdd->pdev->dev);
950 /* setup() returns with device de-selected */
951 s3c64xx_spi_set_cs(spi, false);
953 spi_set_ctldata(spi, NULL);
955 /* This was dynamically allocated on the DT path */
956 if (spi->dev.of_node)
962 static void s3c64xx_spi_cleanup(struct spi_device *spi)
964 struct s3c64xx_spi_csinfo *cs = spi_get_ctldata(spi);
966 /* This was dynamically allocated on the DT path */
967 if (spi->dev.of_node)
970 spi_set_ctldata(spi, NULL);
973 static irqreturn_t s3c64xx_spi_irq(int irq, void *data)
975 struct s3c64xx_spi_driver_data *sdd = data;
976 struct spi_master *spi = sdd->master;
977 unsigned int val, clr = 0;
979 val = readl(sdd->regs + S3C64XX_SPI_STATUS);
981 if (val & S3C64XX_SPI_ST_RX_OVERRUN_ERR) {
982 clr = S3C64XX_SPI_PND_RX_OVERRUN_CLR;
983 dev_err(&spi->dev, "RX overrun\n");
985 if (val & S3C64XX_SPI_ST_RX_UNDERRUN_ERR) {
986 clr |= S3C64XX_SPI_PND_RX_UNDERRUN_CLR;
987 dev_err(&spi->dev, "RX underrun\n");
989 if (val & S3C64XX_SPI_ST_TX_OVERRUN_ERR) {
990 clr |= S3C64XX_SPI_PND_TX_OVERRUN_CLR;
991 dev_err(&spi->dev, "TX overrun\n");
993 if (val & S3C64XX_SPI_ST_TX_UNDERRUN_ERR) {
994 clr |= S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
995 dev_err(&spi->dev, "TX underrun\n");
998 /* Clear the pending irq by setting and then clearing it */
999 writel(clr, sdd->regs + S3C64XX_SPI_PENDING_CLR);
1000 writel(0, sdd->regs + S3C64XX_SPI_PENDING_CLR);
1005 static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data *sdd)
1007 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
1008 void __iomem *regs = sdd->regs;
1014 writel(0, sdd->regs + S3C64XX_SPI_CS_REG);
1015 else if (!(sdd->port_conf->quirks & S3C64XX_SPI_QUIRK_CS_AUTO))
1016 writel(S3C64XX_SPI_CS_SIG_INACT, sdd->regs + S3C64XX_SPI_CS_REG);
1018 /* Disable Interrupts - we use Polling if not DMA mode */
1019 writel(0, regs + S3C64XX_SPI_INT_EN);
1021 if (!sdd->port_conf->clk_from_cmu)
1022 writel(sci->src_clk_nr << S3C64XX_SPI_CLKSEL_SRCSHFT,
1023 regs + S3C64XX_SPI_CLK_CFG);
1024 writel(0, regs + S3C64XX_SPI_MODE_CFG);
1025 writel(0, regs + S3C64XX_SPI_PACKET_CNT);
1027 /* Clear any irq pending bits, should set and clear the bits */
1028 val = S3C64XX_SPI_PND_RX_OVERRUN_CLR |
1029 S3C64XX_SPI_PND_RX_UNDERRUN_CLR |
1030 S3C64XX_SPI_PND_TX_OVERRUN_CLR |
1031 S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
1032 writel(val, regs + S3C64XX_SPI_PENDING_CLR);
1033 writel(0, regs + S3C64XX_SPI_PENDING_CLR);
1035 writel(0, regs + S3C64XX_SPI_SWAP_CFG);
1037 val = readl(regs + S3C64XX_SPI_MODE_CFG);
1038 val &= ~S3C64XX_SPI_MODE_4BURST;
1039 val &= ~(S3C64XX_SPI_MAX_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
1040 val |= (S3C64XX_SPI_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
1041 writel(val, regs + S3C64XX_SPI_MODE_CFG);
1043 s3c64xx_flush_fifo(sdd);
1047 static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
1049 struct s3c64xx_spi_info *sci;
1052 sci = devm_kzalloc(dev, sizeof(*sci), GFP_KERNEL);
1054 return ERR_PTR(-ENOMEM);
1056 if (of_property_read_u32(dev->of_node, "samsung,spi-src-clk", &temp)) {
1057 dev_warn(dev, "spi bus clock parent not specified, using clock at index 0 as parent\n");
1058 sci->src_clk_nr = 0;
1060 sci->src_clk_nr = temp;
1063 if (of_property_read_u32(dev->of_node, "num-cs", &temp)) {
1064 dev_warn(dev, "number of chip select lines not specified, assuming 1 chip select line\n");
1070 sci->no_cs = of_property_read_bool(dev->of_node, "no-cs-readback");
1075 static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
1077 return dev_get_platdata(dev);
1081 static inline const struct s3c64xx_spi_port_config *s3c64xx_spi_get_port_config(
1082 struct platform_device *pdev)
1085 if (pdev->dev.of_node)
1086 return of_device_get_match_data(&pdev->dev);
1088 return (const struct s3c64xx_spi_port_config *)platform_get_device_id(pdev)->driver_data;
1091 static int s3c64xx_spi_probe(struct platform_device *pdev)
1093 struct resource *mem_res;
1094 struct s3c64xx_spi_driver_data *sdd;
1095 struct s3c64xx_spi_info *sci = dev_get_platdata(&pdev->dev);
1096 struct spi_master *master;
1100 if (!sci && pdev->dev.of_node) {
1101 sci = s3c64xx_spi_parse_dt(&pdev->dev);
1103 return PTR_ERR(sci);
1107 dev_err(&pdev->dev, "platform_data missing!\n");
1111 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1112 if (mem_res == NULL) {
1113 dev_err(&pdev->dev, "Unable to get SPI MEM resource\n");
1117 irq = platform_get_irq(pdev, 0);
1119 dev_warn(&pdev->dev, "Failed to get IRQ: %d\n", irq);
1123 master = spi_alloc_master(&pdev->dev,
1124 sizeof(struct s3c64xx_spi_driver_data));
1125 if (master == NULL) {
1126 dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
1130 platform_set_drvdata(pdev, master);
1132 sdd = spi_master_get_devdata(master);
1133 sdd->port_conf = s3c64xx_spi_get_port_config(pdev);
1134 sdd->master = master;
1135 sdd->cntrlr_info = sci;
1137 sdd->sfr_start = mem_res->start;
1138 if (pdev->dev.of_node) {
1139 ret = of_alias_get_id(pdev->dev.of_node, "spi");
1141 dev_err(&pdev->dev, "failed to get alias id, errno %d\n",
1143 goto err_deref_master;
1147 sdd->port_id = pdev->id;
1152 sdd->tx_dma.direction = DMA_MEM_TO_DEV;
1153 sdd->rx_dma.direction = DMA_DEV_TO_MEM;
1155 master->dev.of_node = pdev->dev.of_node;
1156 master->bus_num = sdd->port_id;
1157 master->setup = s3c64xx_spi_setup;
1158 master->cleanup = s3c64xx_spi_cleanup;
1159 master->prepare_transfer_hardware = s3c64xx_spi_prepare_transfer;
1160 master->unprepare_transfer_hardware = s3c64xx_spi_unprepare_transfer;
1161 master->prepare_message = s3c64xx_spi_prepare_message;
1162 master->transfer_one = s3c64xx_spi_transfer_one;
1163 master->max_transfer_size = s3c64xx_spi_max_transfer_size;
1164 master->num_chipselect = sci->num_cs;
1165 master->use_gpio_descriptors = true;
1166 master->dma_alignment = 8;
1167 master->bits_per_word_mask = SPI_BPW_MASK(32) | SPI_BPW_MASK(16) |
1169 /* the spi->mode bits understood by this driver: */
1170 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1171 if (sdd->port_conf->has_loopback)
1172 master->mode_bits |= SPI_LOOP;
1173 master->auto_runtime_pm = true;
1174 if (!is_polling(sdd))
1175 master->can_dma = s3c64xx_spi_can_dma;
1177 sdd->regs = devm_ioremap_resource(&pdev->dev, mem_res);
1178 if (IS_ERR(sdd->regs)) {
1179 ret = PTR_ERR(sdd->regs);
1180 goto err_deref_master;
1183 if (sci->cfg_gpio && sci->cfg_gpio()) {
1184 dev_err(&pdev->dev, "Unable to config gpio\n");
1186 goto err_deref_master;
1190 sdd->clk = devm_clk_get(&pdev->dev, "spi");
1191 if (IS_ERR(sdd->clk)) {
1192 dev_err(&pdev->dev, "Unable to acquire clock 'spi'\n");
1193 ret = PTR_ERR(sdd->clk);
1194 goto err_deref_master;
1197 ret = clk_prepare_enable(sdd->clk);
1199 dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n");
1200 goto err_deref_master;
1203 sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr);
1204 sdd->src_clk = devm_clk_get(&pdev->dev, clk_name);
1205 if (IS_ERR(sdd->src_clk)) {
1207 "Unable to acquire clock '%s'\n", clk_name);
1208 ret = PTR_ERR(sdd->src_clk);
1209 goto err_disable_clk;
1212 ret = clk_prepare_enable(sdd->src_clk);
1214 dev_err(&pdev->dev, "Couldn't enable clock '%s'\n", clk_name);
1215 goto err_disable_clk;
1218 if (sdd->port_conf->clk_ioclk) {
1219 sdd->ioclk = devm_clk_get(&pdev->dev, "spi_ioclk");
1220 if (IS_ERR(sdd->ioclk)) {
1221 dev_err(&pdev->dev, "Unable to acquire 'ioclk'\n");
1222 ret = PTR_ERR(sdd->ioclk);
1223 goto err_disable_src_clk;
1226 ret = clk_prepare_enable(sdd->ioclk);
1228 dev_err(&pdev->dev, "Couldn't enable clock 'ioclk'\n");
1229 goto err_disable_src_clk;
1233 pm_runtime_set_autosuspend_delay(&pdev->dev, AUTOSUSPEND_TIMEOUT);
1234 pm_runtime_use_autosuspend(&pdev->dev);
1235 pm_runtime_set_active(&pdev->dev);
1236 pm_runtime_enable(&pdev->dev);
1237 pm_runtime_get_sync(&pdev->dev);
1239 /* Setup Deufult Mode */
1240 s3c64xx_spi_hwinit(sdd);
1242 spin_lock_init(&sdd->lock);
1243 init_completion(&sdd->xfer_completion);
1245 ret = devm_request_irq(&pdev->dev, irq, s3c64xx_spi_irq, 0,
1246 "spi-s3c64xx", sdd);
1248 dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n",
1253 writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
1254 S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
1255 sdd->regs + S3C64XX_SPI_INT_EN);
1257 ret = devm_spi_register_master(&pdev->dev, master);
1259 dev_err(&pdev->dev, "cannot register SPI master: %d\n", ret);
1263 dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d with %d Slaves attached\n",
1264 sdd->port_id, master->num_chipselect);
1265 dev_dbg(&pdev->dev, "\tIOmem=[%pR]\tFIFO %dbytes\n",
1266 mem_res, (FIFO_LVL_MASK(sdd) >> 1) + 1);
1268 pm_runtime_mark_last_busy(&pdev->dev);
1269 pm_runtime_put_autosuspend(&pdev->dev);
1274 pm_runtime_put_noidle(&pdev->dev);
1275 pm_runtime_disable(&pdev->dev);
1276 pm_runtime_set_suspended(&pdev->dev);
1278 clk_disable_unprepare(sdd->ioclk);
1279 err_disable_src_clk:
1280 clk_disable_unprepare(sdd->src_clk);
1282 clk_disable_unprepare(sdd->clk);
1284 spi_master_put(master);
1289 static int s3c64xx_spi_remove(struct platform_device *pdev)
1291 struct spi_master *master = platform_get_drvdata(pdev);
1292 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1294 pm_runtime_get_sync(&pdev->dev);
1296 writel(0, sdd->regs + S3C64XX_SPI_INT_EN);
1298 if (!is_polling(sdd)) {
1299 dma_release_channel(sdd->rx_dma.ch);
1300 dma_release_channel(sdd->tx_dma.ch);
1303 clk_disable_unprepare(sdd->ioclk);
1305 clk_disable_unprepare(sdd->src_clk);
1307 clk_disable_unprepare(sdd->clk);
1309 pm_runtime_put_noidle(&pdev->dev);
1310 pm_runtime_disable(&pdev->dev);
1311 pm_runtime_set_suspended(&pdev->dev);
1316 #ifdef CONFIG_PM_SLEEP
1317 static int s3c64xx_spi_suspend(struct device *dev)
1319 struct spi_master *master = dev_get_drvdata(dev);
1320 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1322 int ret = spi_master_suspend(master);
1326 ret = pm_runtime_force_suspend(dev);
1330 sdd->cur_speed = 0; /* Output Clock is stopped */
1335 static int s3c64xx_spi_resume(struct device *dev)
1337 struct spi_master *master = dev_get_drvdata(dev);
1338 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1339 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
1345 ret = pm_runtime_force_resume(dev);
1349 return spi_master_resume(master);
1351 #endif /* CONFIG_PM_SLEEP */
1354 static int s3c64xx_spi_runtime_suspend(struct device *dev)
1356 struct spi_master *master = dev_get_drvdata(dev);
1357 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1359 clk_disable_unprepare(sdd->clk);
1360 clk_disable_unprepare(sdd->src_clk);
1361 clk_disable_unprepare(sdd->ioclk);
1366 static int s3c64xx_spi_runtime_resume(struct device *dev)
1368 struct spi_master *master = dev_get_drvdata(dev);
1369 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1372 if (sdd->port_conf->clk_ioclk) {
1373 ret = clk_prepare_enable(sdd->ioclk);
1378 ret = clk_prepare_enable(sdd->src_clk);
1380 goto err_disable_ioclk;
1382 ret = clk_prepare_enable(sdd->clk);
1384 goto err_disable_src_clk;
1386 s3c64xx_spi_hwinit(sdd);
1388 writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
1389 S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
1390 sdd->regs + S3C64XX_SPI_INT_EN);
1394 err_disable_src_clk:
1395 clk_disable_unprepare(sdd->src_clk);
1397 clk_disable_unprepare(sdd->ioclk);
1401 #endif /* CONFIG_PM */
1403 static const struct dev_pm_ops s3c64xx_spi_pm = {
1404 SET_SYSTEM_SLEEP_PM_OPS(s3c64xx_spi_suspend, s3c64xx_spi_resume)
1405 SET_RUNTIME_PM_OPS(s3c64xx_spi_runtime_suspend,
1406 s3c64xx_spi_runtime_resume, NULL)
1409 static const struct s3c64xx_spi_port_config s3c2443_spi_port_config = {
1410 .fifo_lvl_mask = { 0x7f },
1411 .rx_lvl_offset = 13,
1417 static const struct s3c64xx_spi_port_config s3c6410_spi_port_config = {
1418 .fifo_lvl_mask = { 0x7f, 0x7F },
1419 .rx_lvl_offset = 13,
1424 static const struct s3c64xx_spi_port_config s5pv210_spi_port_config = {
1425 .fifo_lvl_mask = { 0x1ff, 0x7F },
1426 .rx_lvl_offset = 15,
1432 static const struct s3c64xx_spi_port_config exynos4_spi_port_config = {
1433 .fifo_lvl_mask = { 0x1ff, 0x7F, 0x7F },
1434 .rx_lvl_offset = 15,
1438 .clk_from_cmu = true,
1439 .quirks = S3C64XX_SPI_QUIRK_CS_AUTO,
1442 static const struct s3c64xx_spi_port_config exynos7_spi_port_config = {
1443 .fifo_lvl_mask = { 0x1ff, 0x7F, 0x7F, 0x7F, 0x7F, 0x1ff},
1444 .rx_lvl_offset = 15,
1448 .clk_from_cmu = true,
1449 .quirks = S3C64XX_SPI_QUIRK_CS_AUTO,
1452 static const struct s3c64xx_spi_port_config exynos5433_spi_port_config = {
1453 .fifo_lvl_mask = { 0x1ff, 0x7f, 0x7f, 0x7f, 0x7f, 0x1ff},
1454 .rx_lvl_offset = 15,
1458 .clk_from_cmu = true,
1460 .quirks = S3C64XX_SPI_QUIRK_CS_AUTO,
1463 static const struct s3c64xx_spi_port_config exynosautov9_spi_port_config = {
1464 .fifo_lvl_mask = { 0x1ff, 0x1ff, 0x7f, 0x7f, 0x7f, 0x7f, 0x1ff, 0x7f,
1465 0x7f, 0x7f, 0x7f, 0x7f},
1466 .rx_lvl_offset = 15,
1470 .clk_from_cmu = true,
1472 .has_loopback = true,
1473 .quirks = S3C64XX_SPI_QUIRK_CS_AUTO,
1476 static const struct s3c64xx_spi_port_config fsd_spi_port_config = {
1477 .fifo_lvl_mask = { 0x7f, 0x7f, 0x7f, 0x7f, 0x7f},
1478 .rx_lvl_offset = 15,
1482 .clk_from_cmu = true,
1484 .quirks = S3C64XX_SPI_QUIRK_CS_AUTO,
1487 static const struct platform_device_id s3c64xx_spi_driver_ids[] = {
1489 .name = "s3c2443-spi",
1490 .driver_data = (kernel_ulong_t)&s3c2443_spi_port_config,
1492 .name = "s3c6410-spi",
1493 .driver_data = (kernel_ulong_t)&s3c6410_spi_port_config,
1498 static const struct of_device_id s3c64xx_spi_dt_match[] = {
1499 { .compatible = "samsung,s3c2443-spi",
1500 .data = (void *)&s3c2443_spi_port_config,
1502 { .compatible = "samsung,s3c6410-spi",
1503 .data = (void *)&s3c6410_spi_port_config,
1505 { .compatible = "samsung,s5pv210-spi",
1506 .data = (void *)&s5pv210_spi_port_config,
1508 { .compatible = "samsung,exynos4210-spi",
1509 .data = (void *)&exynos4_spi_port_config,
1511 { .compatible = "samsung,exynos7-spi",
1512 .data = (void *)&exynos7_spi_port_config,
1514 { .compatible = "samsung,exynos5433-spi",
1515 .data = (void *)&exynos5433_spi_port_config,
1517 { .compatible = "samsung,exynosautov9-spi",
1518 .data = (void *)&exynosautov9_spi_port_config,
1520 { .compatible = "tesla,fsd-spi",
1521 .data = (void *)&fsd_spi_port_config,
1525 MODULE_DEVICE_TABLE(of, s3c64xx_spi_dt_match);
1527 static struct platform_driver s3c64xx_spi_driver = {
1529 .name = "s3c64xx-spi",
1530 .pm = &s3c64xx_spi_pm,
1531 .of_match_table = of_match_ptr(s3c64xx_spi_dt_match),
1533 .probe = s3c64xx_spi_probe,
1534 .remove = s3c64xx_spi_remove,
1535 .id_table = s3c64xx_spi_driver_ids,
1537 MODULE_ALIAS("platform:s3c64xx-spi");
1539 module_platform_driver(s3c64xx_spi_driver);
1541 MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
1542 MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
1543 MODULE_LICENSE("GPL");