xref: /openbmc/linux/drivers/spi/spi-s3c64xx.c (revision 185c8f33)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Copyright (c) 2009 Samsung Electronics Co., Ltd.
4 //      Jaswinder Singh <jassi.brar@samsung.com>
5 
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>
16 #include <linux/of.h>
17 #include <linux/of_device.h>
18 
19 #include <linux/platform_data/spi-s3c64xx.h>
20 
21 #define MAX_SPI_PORTS		12
22 #define S3C64XX_SPI_QUIRK_CS_AUTO	(1 << 1)
23 #define AUTOSUSPEND_TIMEOUT	2000
24 
25 /* Registers and bit-fields */
26 
27 #define S3C64XX_SPI_CH_CFG		0x00
28 #define S3C64XX_SPI_CLK_CFG		0x04
29 #define S3C64XX_SPI_MODE_CFG		0x08
30 #define S3C64XX_SPI_CS_REG		0x0C
31 #define S3C64XX_SPI_INT_EN		0x10
32 #define S3C64XX_SPI_STATUS		0x14
33 #define S3C64XX_SPI_TX_DATA		0x18
34 #define S3C64XX_SPI_RX_DATA		0x1C
35 #define S3C64XX_SPI_PACKET_CNT		0x20
36 #define S3C64XX_SPI_PENDING_CLR		0x24
37 #define S3C64XX_SPI_SWAP_CFG		0x28
38 #define S3C64XX_SPI_FB_CLK		0x2C
39 
40 #define S3C64XX_SPI_CH_HS_EN		(1<<6)	/* High Speed Enable */
41 #define S3C64XX_SPI_CH_SW_RST		(1<<5)
42 #define S3C64XX_SPI_CH_SLAVE		(1<<4)
43 #define S3C64XX_SPI_CPOL_L		(1<<3)
44 #define S3C64XX_SPI_CPHA_B		(1<<2)
45 #define S3C64XX_SPI_CH_RXCH_ON		(1<<1)
46 #define S3C64XX_SPI_CH_TXCH_ON		(1<<0)
47 
48 #define S3C64XX_SPI_CLKSEL_SRCMSK	(3<<9)
49 #define S3C64XX_SPI_CLKSEL_SRCSHFT	9
50 #define S3C64XX_SPI_ENCLK_ENABLE	(1<<8)
51 #define S3C64XX_SPI_PSR_MASK		0xff
52 
53 #define S3C64XX_SPI_MODE_CH_TSZ_BYTE		(0<<29)
54 #define S3C64XX_SPI_MODE_CH_TSZ_HALFWORD	(1<<29)
55 #define S3C64XX_SPI_MODE_CH_TSZ_WORD		(2<<29)
56 #define S3C64XX_SPI_MODE_CH_TSZ_MASK		(3<<29)
57 #define S3C64XX_SPI_MODE_BUS_TSZ_BYTE		(0<<17)
58 #define S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD	(1<<17)
59 #define S3C64XX_SPI_MODE_BUS_TSZ_WORD		(2<<17)
60 #define S3C64XX_SPI_MODE_BUS_TSZ_MASK		(3<<17)
61 #define S3C64XX_SPI_MODE_RX_RDY_LVL		GENMASK(16, 11)
62 #define S3C64XX_SPI_MODE_RX_RDY_LVL_SHIFT	11
63 #define S3C64XX_SPI_MODE_SELF_LOOPBACK		(1<<3)
64 #define S3C64XX_SPI_MODE_RXDMA_ON		(1<<2)
65 #define S3C64XX_SPI_MODE_TXDMA_ON		(1<<1)
66 #define S3C64XX_SPI_MODE_4BURST			(1<<0)
67 
68 #define S3C64XX_SPI_CS_NSC_CNT_2		(2<<4)
69 #define S3C64XX_SPI_CS_AUTO			(1<<1)
70 #define S3C64XX_SPI_CS_SIG_INACT		(1<<0)
71 
72 #define S3C64XX_SPI_INT_TRAILING_EN		(1<<6)
73 #define S3C64XX_SPI_INT_RX_OVERRUN_EN		(1<<5)
74 #define S3C64XX_SPI_INT_RX_UNDERRUN_EN		(1<<4)
75 #define S3C64XX_SPI_INT_TX_OVERRUN_EN		(1<<3)
76 #define S3C64XX_SPI_INT_TX_UNDERRUN_EN		(1<<2)
77 #define S3C64XX_SPI_INT_RX_FIFORDY_EN		(1<<1)
78 #define S3C64XX_SPI_INT_TX_FIFORDY_EN		(1<<0)
79 
80 #define S3C64XX_SPI_ST_RX_OVERRUN_ERR		(1<<5)
81 #define S3C64XX_SPI_ST_RX_UNDERRUN_ERR		(1<<4)
82 #define S3C64XX_SPI_ST_TX_OVERRUN_ERR		(1<<3)
83 #define S3C64XX_SPI_ST_TX_UNDERRUN_ERR		(1<<2)
84 #define S3C64XX_SPI_ST_RX_FIFORDY		(1<<1)
85 #define S3C64XX_SPI_ST_TX_FIFORDY		(1<<0)
86 
87 #define S3C64XX_SPI_PACKET_CNT_EN		(1<<16)
88 #define S3C64XX_SPI_PACKET_CNT_MASK		GENMASK(15, 0)
89 
90 #define S3C64XX_SPI_PND_TX_UNDERRUN_CLR		(1<<4)
91 #define S3C64XX_SPI_PND_TX_OVERRUN_CLR		(1<<3)
92 #define S3C64XX_SPI_PND_RX_UNDERRUN_CLR		(1<<2)
93 #define S3C64XX_SPI_PND_RX_OVERRUN_CLR		(1<<1)
94 #define S3C64XX_SPI_PND_TRAILING_CLR		(1<<0)
95 
96 #define S3C64XX_SPI_SWAP_RX_HALF_WORD		(1<<7)
97 #define S3C64XX_SPI_SWAP_RX_BYTE		(1<<6)
98 #define S3C64XX_SPI_SWAP_RX_BIT			(1<<5)
99 #define S3C64XX_SPI_SWAP_RX_EN			(1<<4)
100 #define S3C64XX_SPI_SWAP_TX_HALF_WORD		(1<<3)
101 #define S3C64XX_SPI_SWAP_TX_BYTE		(1<<2)
102 #define S3C64XX_SPI_SWAP_TX_BIT			(1<<1)
103 #define S3C64XX_SPI_SWAP_TX_EN			(1<<0)
104 
105 #define S3C64XX_SPI_FBCLK_MSK			(3<<0)
106 
107 #define FIFO_LVL_MASK(i) ((i)->port_conf->fifo_lvl_mask[i->port_id])
108 #define S3C64XX_SPI_ST_TX_DONE(v, i) (((v) & \
109 				(1 << (i)->port_conf->tx_st_done)) ? 1 : 0)
110 #define TX_FIFO_LVL(v, i) (((v) >> 6) & FIFO_LVL_MASK(i))
111 #define RX_FIFO_LVL(v, i) (((v) >> (i)->port_conf->rx_lvl_offset) & \
112 					FIFO_LVL_MASK(i))
113 
114 #define S3C64XX_SPI_MAX_TRAILCNT	0x3ff
115 #define S3C64XX_SPI_TRAILCNT_OFF	19
116 
117 #define S3C64XX_SPI_TRAILCNT		S3C64XX_SPI_MAX_TRAILCNT
118 
119 #define S3C64XX_SPI_POLLING_SIZE	32
120 
121 #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
122 #define is_polling(x)	(x->cntrlr_info->polling)
123 
124 #define RXBUSY    (1<<2)
125 #define TXBUSY    (1<<3)
126 
127 struct s3c64xx_spi_dma_data {
128 	struct dma_chan *ch;
129 	dma_cookie_t cookie;
130 	enum dma_transfer_direction direction;
131 };
132 
133 /**
134  * struct s3c64xx_spi_port_config - SPI Controller hardware info
135  * @fifo_lvl_mask: Bit-mask for {TX|RX}_FIFO_LVL bits in SPI_STATUS register.
136  * @rx_lvl_offset: Bit offset of RX_FIFO_LVL bits in SPI_STATUS regiter.
137  * @tx_st_done: Bit offset of TX_DONE bit in SPI_STATUS regiter.
138  * @clk_div: Internal clock divider
139  * @quirks: Bitmask of known quirks
140  * @high_speed: True, if the controller supports HIGH_SPEED_EN bit.
141  * @clk_from_cmu: True, if the controller does not include a clock mux and
142  *	prescaler unit.
143  * @clk_ioclk: True if clock is present on this device
144  * @has_loopback: True if loopback mode can be supported
145  *
146  * The Samsung s3c64xx SPI controller are used on various Samsung SoC's but
147  * differ in some aspects such as the size of the fifo and spi bus clock
148  * setup. Such differences are specified to the driver using this structure
149  * which is provided as driver data to the driver.
150  */
151 struct s3c64xx_spi_port_config {
152 	int	fifo_lvl_mask[MAX_SPI_PORTS];
153 	int	rx_lvl_offset;
154 	int	tx_st_done;
155 	int	quirks;
156 	int	clk_div;
157 	bool	high_speed;
158 	bool	clk_from_cmu;
159 	bool	clk_ioclk;
160 	bool	has_loopback;
161 };
162 
163 /**
164  * struct s3c64xx_spi_driver_data - Runtime info holder for SPI driver.
165  * @clk: Pointer to the spi clock.
166  * @src_clk: Pointer to the clock used to generate SPI signals.
167  * @ioclk: Pointer to the i/o clock between master and slave
168  * @pdev: Pointer to device's platform device data
169  * @master: Pointer to the SPI Protocol master.
170  * @cntrlr_info: Platform specific data for the controller this driver manages.
171  * @lock: Controller specific lock.
172  * @state: Set of FLAGS to indicate status.
173  * @sfr_start: BUS address of SPI controller regs.
174  * @regs: Pointer to ioremap'ed controller registers.
175  * @xfer_completion: To indicate completion of xfer task.
176  * @cur_mode: Stores the active configuration of the controller.
177  * @cur_bpw: Stores the active bits per word settings.
178  * @cur_speed: Current clock speed
179  * @rx_dma: Local receive DMA data (e.g. chan and direction)
180  * @tx_dma: Local transmit DMA data (e.g. chan and direction)
181  * @port_conf: Local SPI port configuartion data
182  * @port_id: Port identification number
183  */
184 struct s3c64xx_spi_driver_data {
185 	void __iomem                    *regs;
186 	struct clk                      *clk;
187 	struct clk                      *src_clk;
188 	struct clk                      *ioclk;
189 	struct platform_device          *pdev;
190 	struct spi_master               *master;
191 	struct s3c64xx_spi_info         *cntrlr_info;
192 	spinlock_t                      lock;
193 	unsigned long                   sfr_start;
194 	struct completion               xfer_completion;
195 	unsigned                        state;
196 	unsigned                        cur_mode, cur_bpw;
197 	unsigned                        cur_speed;
198 	struct s3c64xx_spi_dma_data	rx_dma;
199 	struct s3c64xx_spi_dma_data	tx_dma;
200 	const struct s3c64xx_spi_port_config	*port_conf;
201 	unsigned int			port_id;
202 };
203 
204 static void s3c64xx_flush_fifo(struct s3c64xx_spi_driver_data *sdd)
205 {
206 	void __iomem *regs = sdd->regs;
207 	unsigned long loops;
208 	u32 val;
209 
210 	writel(0, regs + S3C64XX_SPI_PACKET_CNT);
211 
212 	val = readl(regs + S3C64XX_SPI_CH_CFG);
213 	val &= ~(S3C64XX_SPI_CH_RXCH_ON | S3C64XX_SPI_CH_TXCH_ON);
214 	writel(val, regs + S3C64XX_SPI_CH_CFG);
215 
216 	val = readl(regs + S3C64XX_SPI_CH_CFG);
217 	val |= S3C64XX_SPI_CH_SW_RST;
218 	val &= ~S3C64XX_SPI_CH_HS_EN;
219 	writel(val, regs + S3C64XX_SPI_CH_CFG);
220 
221 	/* Flush TxFIFO*/
222 	loops = msecs_to_loops(1);
223 	do {
224 		val = readl(regs + S3C64XX_SPI_STATUS);
225 	} while (TX_FIFO_LVL(val, sdd) && loops--);
226 
227 	if (loops == 0)
228 		dev_warn(&sdd->pdev->dev, "Timed out flushing TX FIFO\n");
229 
230 	/* Flush RxFIFO*/
231 	loops = msecs_to_loops(1);
232 	do {
233 		val = readl(regs + S3C64XX_SPI_STATUS);
234 		if (RX_FIFO_LVL(val, sdd))
235 			readl(regs + S3C64XX_SPI_RX_DATA);
236 		else
237 			break;
238 	} while (loops--);
239 
240 	if (loops == 0)
241 		dev_warn(&sdd->pdev->dev, "Timed out flushing RX FIFO\n");
242 
243 	val = readl(regs + S3C64XX_SPI_CH_CFG);
244 	val &= ~S3C64XX_SPI_CH_SW_RST;
245 	writel(val, regs + S3C64XX_SPI_CH_CFG);
246 
247 	val = readl(regs + S3C64XX_SPI_MODE_CFG);
248 	val &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
249 	writel(val, regs + S3C64XX_SPI_MODE_CFG);
250 }
251 
252 static void s3c64xx_spi_dmacb(void *data)
253 {
254 	struct s3c64xx_spi_driver_data *sdd;
255 	struct s3c64xx_spi_dma_data *dma = data;
256 	unsigned long flags;
257 
258 	if (dma->direction == DMA_DEV_TO_MEM)
259 		sdd = container_of(data,
260 			struct s3c64xx_spi_driver_data, rx_dma);
261 	else
262 		sdd = container_of(data,
263 			struct s3c64xx_spi_driver_data, tx_dma);
264 
265 	spin_lock_irqsave(&sdd->lock, flags);
266 
267 	if (dma->direction == DMA_DEV_TO_MEM) {
268 		sdd->state &= ~RXBUSY;
269 		if (!(sdd->state & TXBUSY))
270 			complete(&sdd->xfer_completion);
271 	} else {
272 		sdd->state &= ~TXBUSY;
273 		if (!(sdd->state & RXBUSY))
274 			complete(&sdd->xfer_completion);
275 	}
276 
277 	spin_unlock_irqrestore(&sdd->lock, flags);
278 }
279 
280 static int prepare_dma(struct s3c64xx_spi_dma_data *dma,
281 			struct sg_table *sgt)
282 {
283 	struct s3c64xx_spi_driver_data *sdd;
284 	struct dma_slave_config config;
285 	struct dma_async_tx_descriptor *desc;
286 	int ret;
287 
288 	memset(&config, 0, sizeof(config));
289 
290 	if (dma->direction == DMA_DEV_TO_MEM) {
291 		sdd = container_of((void *)dma,
292 			struct s3c64xx_spi_driver_data, rx_dma);
293 		config.direction = dma->direction;
294 		config.src_addr = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
295 		config.src_addr_width = sdd->cur_bpw / 8;
296 		config.src_maxburst = 1;
297 		dmaengine_slave_config(dma->ch, &config);
298 	} else {
299 		sdd = container_of((void *)dma,
300 			struct s3c64xx_spi_driver_data, tx_dma);
301 		config.direction = dma->direction;
302 		config.dst_addr = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
303 		config.dst_addr_width = sdd->cur_bpw / 8;
304 		config.dst_maxburst = 1;
305 		dmaengine_slave_config(dma->ch, &config);
306 	}
307 
308 	desc = dmaengine_prep_slave_sg(dma->ch, sgt->sgl, sgt->nents,
309 				       dma->direction, DMA_PREP_INTERRUPT);
310 	if (!desc) {
311 		dev_err(&sdd->pdev->dev, "unable to prepare %s scatterlist",
312 			dma->direction == DMA_DEV_TO_MEM ? "rx" : "tx");
313 		return -ENOMEM;
314 	}
315 
316 	desc->callback = s3c64xx_spi_dmacb;
317 	desc->callback_param = dma;
318 
319 	dma->cookie = dmaengine_submit(desc);
320 	ret = dma_submit_error(dma->cookie);
321 	if (ret) {
322 		dev_err(&sdd->pdev->dev, "DMA submission failed");
323 		return -EIO;
324 	}
325 
326 	dma_async_issue_pending(dma->ch);
327 	return 0;
328 }
329 
330 static void s3c64xx_spi_set_cs(struct spi_device *spi, bool enable)
331 {
332 	struct s3c64xx_spi_driver_data *sdd =
333 					spi_master_get_devdata(spi->master);
334 
335 	if (sdd->cntrlr_info->no_cs)
336 		return;
337 
338 	if (enable) {
339 		if (!(sdd->port_conf->quirks & S3C64XX_SPI_QUIRK_CS_AUTO)) {
340 			writel(0, sdd->regs + S3C64XX_SPI_CS_REG);
341 		} else {
342 			u32 ssel = readl(sdd->regs + S3C64XX_SPI_CS_REG);
343 
344 			ssel |= (S3C64XX_SPI_CS_AUTO |
345 						S3C64XX_SPI_CS_NSC_CNT_2);
346 			writel(ssel, sdd->regs + S3C64XX_SPI_CS_REG);
347 		}
348 	} else {
349 		if (!(sdd->port_conf->quirks & S3C64XX_SPI_QUIRK_CS_AUTO))
350 			writel(S3C64XX_SPI_CS_SIG_INACT,
351 			       sdd->regs + S3C64XX_SPI_CS_REG);
352 	}
353 }
354 
355 static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
356 {
357 	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
358 
359 	if (is_polling(sdd))
360 		return 0;
361 
362 	/* Requests DMA channels */
363 	sdd->rx_dma.ch = dma_request_chan(&sdd->pdev->dev, "rx");
364 	if (IS_ERR(sdd->rx_dma.ch)) {
365 		dev_err(&sdd->pdev->dev, "Failed to get RX DMA channel\n");
366 		sdd->rx_dma.ch = NULL;
367 		return 0;
368 	}
369 
370 	sdd->tx_dma.ch = dma_request_chan(&sdd->pdev->dev, "tx");
371 	if (IS_ERR(sdd->tx_dma.ch)) {
372 		dev_err(&sdd->pdev->dev, "Failed to get TX DMA channel\n");
373 		dma_release_channel(sdd->rx_dma.ch);
374 		sdd->tx_dma.ch = NULL;
375 		sdd->rx_dma.ch = NULL;
376 		return 0;
377 	}
378 
379 	spi->dma_rx = sdd->rx_dma.ch;
380 	spi->dma_tx = sdd->tx_dma.ch;
381 
382 	return 0;
383 }
384 
385 static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
386 {
387 	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
388 
389 	if (is_polling(sdd))
390 		return 0;
391 
392 	/* Releases DMA channels if they are allocated */
393 	if (sdd->rx_dma.ch && sdd->tx_dma.ch) {
394 		dma_release_channel(sdd->rx_dma.ch);
395 		dma_release_channel(sdd->tx_dma.ch);
396 		sdd->rx_dma.ch = NULL;
397 		sdd->tx_dma.ch = NULL;
398 	}
399 
400 	return 0;
401 }
402 
403 static bool s3c64xx_spi_can_dma(struct spi_master *master,
404 				struct spi_device *spi,
405 				struct spi_transfer *xfer)
406 {
407 	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
408 
409 	if (sdd->rx_dma.ch && sdd->tx_dma.ch) {
410 		return xfer->len > (FIFO_LVL_MASK(sdd) >> 1) + 1;
411 	} else {
412 		return false;
413 	}
414 
415 }
416 
417 static int s3c64xx_enable_datapath(struct s3c64xx_spi_driver_data *sdd,
418 				    struct spi_transfer *xfer, int dma_mode)
419 {
420 	void __iomem *regs = sdd->regs;
421 	u32 modecfg, chcfg;
422 	int ret = 0;
423 
424 	modecfg = readl(regs + S3C64XX_SPI_MODE_CFG);
425 	modecfg &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
426 
427 	chcfg = readl(regs + S3C64XX_SPI_CH_CFG);
428 	chcfg &= ~S3C64XX_SPI_CH_TXCH_ON;
429 
430 	if (dma_mode) {
431 		chcfg &= ~S3C64XX_SPI_CH_RXCH_ON;
432 	} else {
433 		/* Always shift in data in FIFO, even if xfer is Tx only,
434 		 * this helps setting PCKT_CNT value for generating clocks
435 		 * as exactly needed.
436 		 */
437 		chcfg |= S3C64XX_SPI_CH_RXCH_ON;
438 		writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
439 					| S3C64XX_SPI_PACKET_CNT_EN,
440 					regs + S3C64XX_SPI_PACKET_CNT);
441 	}
442 
443 	if (xfer->tx_buf != NULL) {
444 		sdd->state |= TXBUSY;
445 		chcfg |= S3C64XX_SPI_CH_TXCH_ON;
446 		if (dma_mode) {
447 			modecfg |= S3C64XX_SPI_MODE_TXDMA_ON;
448 			ret = prepare_dma(&sdd->tx_dma, &xfer->tx_sg);
449 		} else {
450 			switch (sdd->cur_bpw) {
451 			case 32:
452 				iowrite32_rep(regs + S3C64XX_SPI_TX_DATA,
453 					xfer->tx_buf, xfer->len / 4);
454 				break;
455 			case 16:
456 				iowrite16_rep(regs + S3C64XX_SPI_TX_DATA,
457 					xfer->tx_buf, xfer->len / 2);
458 				break;
459 			default:
460 				iowrite8_rep(regs + S3C64XX_SPI_TX_DATA,
461 					xfer->tx_buf, xfer->len);
462 				break;
463 			}
464 		}
465 	}
466 
467 	if (xfer->rx_buf != NULL) {
468 		sdd->state |= RXBUSY;
469 
470 		if (sdd->port_conf->high_speed && sdd->cur_speed >= 30000000UL
471 					&& !(sdd->cur_mode & SPI_CPHA))
472 			chcfg |= S3C64XX_SPI_CH_HS_EN;
473 
474 		if (dma_mode) {
475 			modecfg |= S3C64XX_SPI_MODE_RXDMA_ON;
476 			chcfg |= S3C64XX_SPI_CH_RXCH_ON;
477 			writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
478 					| S3C64XX_SPI_PACKET_CNT_EN,
479 					regs + S3C64XX_SPI_PACKET_CNT);
480 			ret = prepare_dma(&sdd->rx_dma, &xfer->rx_sg);
481 		}
482 	}
483 
484 	if (ret)
485 		return ret;
486 
487 	writel(modecfg, regs + S3C64XX_SPI_MODE_CFG);
488 	writel(chcfg, regs + S3C64XX_SPI_CH_CFG);
489 
490 	return 0;
491 }
492 
493 static u32 s3c64xx_spi_wait_for_timeout(struct s3c64xx_spi_driver_data *sdd,
494 					int timeout_ms)
495 {
496 	void __iomem *regs = sdd->regs;
497 	unsigned long val = 1;
498 	u32 status;
499 
500 	/* max fifo depth available */
501 	u32 max_fifo = (FIFO_LVL_MASK(sdd) >> 1) + 1;
502 
503 	if (timeout_ms)
504 		val = msecs_to_loops(timeout_ms);
505 
506 	do {
507 		status = readl(regs + S3C64XX_SPI_STATUS);
508 	} while (RX_FIFO_LVL(status, sdd) < max_fifo && --val);
509 
510 	/* return the actual received data length */
511 	return RX_FIFO_LVL(status, sdd);
512 }
513 
514 static int s3c64xx_wait_for_dma(struct s3c64xx_spi_driver_data *sdd,
515 				struct spi_transfer *xfer)
516 {
517 	void __iomem *regs = sdd->regs;
518 	unsigned long val;
519 	u32 status;
520 	int ms;
521 
522 	/* millisecs to xfer 'len' bytes @ 'cur_speed' */
523 	ms = xfer->len * 8 * 1000 / sdd->cur_speed;
524 	ms += 30;               /* some tolerance */
525 	ms = max(ms, 100);      /* minimum timeout */
526 
527 	val = msecs_to_jiffies(ms) + 10;
528 	val = wait_for_completion_timeout(&sdd->xfer_completion, val);
529 
530 	/*
531 	 * If the previous xfer was completed within timeout, then
532 	 * proceed further else return -EIO.
533 	 * DmaTx returns after simply writing data in the FIFO,
534 	 * w/o waiting for real transmission on the bus to finish.
535 	 * DmaRx returns only after Dma read data from FIFO which
536 	 * needs bus transmission to finish, so we don't worry if
537 	 * Xfer involved Rx(with or without Tx).
538 	 */
539 	if (val && !xfer->rx_buf) {
540 		val = msecs_to_loops(10);
541 		status = readl(regs + S3C64XX_SPI_STATUS);
542 		while ((TX_FIFO_LVL(status, sdd)
543 			|| !S3C64XX_SPI_ST_TX_DONE(status, sdd))
544 		       && --val) {
545 			cpu_relax();
546 			status = readl(regs + S3C64XX_SPI_STATUS);
547 		}
548 
549 	}
550 
551 	/* If timed out while checking rx/tx status return error */
552 	if (!val)
553 		return -EIO;
554 
555 	return 0;
556 }
557 
558 static int s3c64xx_wait_for_pio(struct s3c64xx_spi_driver_data *sdd,
559 				struct spi_transfer *xfer, bool use_irq)
560 {
561 	void __iomem *regs = sdd->regs;
562 	unsigned long val;
563 	u32 status;
564 	int loops;
565 	u32 cpy_len;
566 	u8 *buf;
567 	int ms;
568 	unsigned long time_us;
569 
570 	/* microsecs to xfer 'len' bytes @ 'cur_speed' */
571 	time_us = (xfer->len * 8 * 1000 * 1000) / sdd->cur_speed;
572 	ms = (time_us / 1000);
573 	ms += 10; /* some tolerance */
574 
575 	/* sleep during signal transfer time */
576 	status = readl(regs + S3C64XX_SPI_STATUS);
577 	if (RX_FIFO_LVL(status, sdd) < xfer->len)
578 		usleep_range(time_us / 2, time_us);
579 
580 	if (use_irq) {
581 		val = msecs_to_jiffies(ms);
582 		if (!wait_for_completion_timeout(&sdd->xfer_completion, val))
583 			return -EIO;
584 	}
585 
586 	val = msecs_to_loops(ms);
587 	do {
588 		status = readl(regs + S3C64XX_SPI_STATUS);
589 	} while (RX_FIFO_LVL(status, sdd) < xfer->len && --val);
590 
591 	if (!val)
592 		return -EIO;
593 
594 	/* If it was only Tx */
595 	if (!xfer->rx_buf) {
596 		sdd->state &= ~TXBUSY;
597 		return 0;
598 	}
599 
600 	/*
601 	 * If the receive length is bigger than the controller fifo
602 	 * size, calculate the loops and read the fifo as many times.
603 	 * loops = length / max fifo size (calculated by using the
604 	 * fifo mask).
605 	 * For any size less than the fifo size the below code is
606 	 * executed atleast once.
607 	 */
608 	loops = xfer->len / ((FIFO_LVL_MASK(sdd) >> 1) + 1);
609 	buf = xfer->rx_buf;
610 	do {
611 		/* wait for data to be received in the fifo */
612 		cpy_len = s3c64xx_spi_wait_for_timeout(sdd,
613 						       (loops ? ms : 0));
614 
615 		switch (sdd->cur_bpw) {
616 		case 32:
617 			ioread32_rep(regs + S3C64XX_SPI_RX_DATA,
618 				     buf, cpy_len / 4);
619 			break;
620 		case 16:
621 			ioread16_rep(regs + S3C64XX_SPI_RX_DATA,
622 				     buf, cpy_len / 2);
623 			break;
624 		default:
625 			ioread8_rep(regs + S3C64XX_SPI_RX_DATA,
626 				    buf, cpy_len);
627 			break;
628 		}
629 
630 		buf = buf + cpy_len;
631 	} while (loops--);
632 	sdd->state &= ~RXBUSY;
633 
634 	return 0;
635 }
636 
637 static int s3c64xx_spi_config(struct s3c64xx_spi_driver_data *sdd)
638 {
639 	void __iomem *regs = sdd->regs;
640 	int ret;
641 	u32 val;
642 	int div = sdd->port_conf->clk_div;
643 
644 	/* Disable Clock */
645 	if (!sdd->port_conf->clk_from_cmu) {
646 		val = readl(regs + S3C64XX_SPI_CLK_CFG);
647 		val &= ~S3C64XX_SPI_ENCLK_ENABLE;
648 		writel(val, regs + S3C64XX_SPI_CLK_CFG);
649 	}
650 
651 	/* Set Polarity and Phase */
652 	val = readl(regs + S3C64XX_SPI_CH_CFG);
653 	val &= ~(S3C64XX_SPI_CH_SLAVE |
654 			S3C64XX_SPI_CPOL_L |
655 			S3C64XX_SPI_CPHA_B);
656 
657 	if (sdd->cur_mode & SPI_CPOL)
658 		val |= S3C64XX_SPI_CPOL_L;
659 
660 	if (sdd->cur_mode & SPI_CPHA)
661 		val |= S3C64XX_SPI_CPHA_B;
662 
663 	writel(val, regs + S3C64XX_SPI_CH_CFG);
664 
665 	/* Set Channel & DMA Mode */
666 	val = readl(regs + S3C64XX_SPI_MODE_CFG);
667 	val &= ~(S3C64XX_SPI_MODE_BUS_TSZ_MASK
668 			| S3C64XX_SPI_MODE_CH_TSZ_MASK);
669 
670 	switch (sdd->cur_bpw) {
671 	case 32:
672 		val |= S3C64XX_SPI_MODE_BUS_TSZ_WORD;
673 		val |= S3C64XX_SPI_MODE_CH_TSZ_WORD;
674 		break;
675 	case 16:
676 		val |= S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD;
677 		val |= S3C64XX_SPI_MODE_CH_TSZ_HALFWORD;
678 		break;
679 	default:
680 		val |= S3C64XX_SPI_MODE_BUS_TSZ_BYTE;
681 		val |= S3C64XX_SPI_MODE_CH_TSZ_BYTE;
682 		break;
683 	}
684 
685 	if ((sdd->cur_mode & SPI_LOOP) && sdd->port_conf->has_loopback)
686 		val |= S3C64XX_SPI_MODE_SELF_LOOPBACK;
687 	else
688 		val &= ~S3C64XX_SPI_MODE_SELF_LOOPBACK;
689 
690 	writel(val, regs + S3C64XX_SPI_MODE_CFG);
691 
692 	if (sdd->port_conf->clk_from_cmu) {
693 		ret = clk_set_rate(sdd->src_clk, sdd->cur_speed * div);
694 		if (ret)
695 			return ret;
696 		sdd->cur_speed = clk_get_rate(sdd->src_clk) / div;
697 	} else {
698 		/* Configure Clock */
699 		val = readl(regs + S3C64XX_SPI_CLK_CFG);
700 		val &= ~S3C64XX_SPI_PSR_MASK;
701 		val |= ((clk_get_rate(sdd->src_clk) / sdd->cur_speed / div - 1)
702 				& S3C64XX_SPI_PSR_MASK);
703 		writel(val, regs + S3C64XX_SPI_CLK_CFG);
704 
705 		/* Enable Clock */
706 		val = readl(regs + S3C64XX_SPI_CLK_CFG);
707 		val |= S3C64XX_SPI_ENCLK_ENABLE;
708 		writel(val, regs + S3C64XX_SPI_CLK_CFG);
709 	}
710 
711 	return 0;
712 }
713 
714 #define XFER_DMAADDR_INVALID DMA_BIT_MASK(32)
715 
716 static int s3c64xx_spi_prepare_message(struct spi_master *master,
717 				       struct spi_message *msg)
718 {
719 	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
720 	struct spi_device *spi = msg->spi;
721 	struct s3c64xx_spi_csinfo *cs = spi->controller_data;
722 
723 	/* Configure feedback delay */
724 	if (!cs)
725 		/* No delay if not defined */
726 		writel(0, sdd->regs + S3C64XX_SPI_FB_CLK);
727 	else
728 		writel(cs->fb_delay & 0x3, sdd->regs + S3C64XX_SPI_FB_CLK);
729 
730 	return 0;
731 }
732 
733 static size_t s3c64xx_spi_max_transfer_size(struct spi_device *spi)
734 {
735 	struct spi_controller *ctlr = spi->controller;
736 
737 	return ctlr->can_dma ? S3C64XX_SPI_PACKET_CNT_MASK : SIZE_MAX;
738 }
739 
740 static int s3c64xx_spi_transfer_one(struct spi_master *master,
741 				    struct spi_device *spi,
742 				    struct spi_transfer *xfer)
743 {
744 	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
745 	const unsigned int fifo_len = (FIFO_LVL_MASK(sdd) >> 1) + 1;
746 	const void *tx_buf = NULL;
747 	void *rx_buf = NULL;
748 	int target_len = 0, origin_len = 0;
749 	int use_dma = 0;
750 	bool use_irq = false;
751 	int status;
752 	u32 speed;
753 	u8 bpw;
754 	unsigned long flags;
755 	u32 rdy_lv;
756 	u32 val;
757 
758 	reinit_completion(&sdd->xfer_completion);
759 
760 	/* Only BPW and Speed may change across transfers */
761 	bpw = xfer->bits_per_word;
762 	speed = xfer->speed_hz;
763 
764 	if (bpw != sdd->cur_bpw || speed != sdd->cur_speed) {
765 		sdd->cur_bpw = bpw;
766 		sdd->cur_speed = speed;
767 		sdd->cur_mode = spi->mode;
768 		status = s3c64xx_spi_config(sdd);
769 		if (status)
770 			return status;
771 	}
772 
773 	if (!is_polling(sdd) && (xfer->len > fifo_len) &&
774 	    sdd->rx_dma.ch && sdd->tx_dma.ch) {
775 		use_dma = 1;
776 
777 	} else if (xfer->len >= fifo_len) {
778 		tx_buf = xfer->tx_buf;
779 		rx_buf = xfer->rx_buf;
780 		origin_len = xfer->len;
781 		target_len = xfer->len;
782 		xfer->len = fifo_len - 1;
783 	}
784 
785 	do {
786 		/* transfer size is greater than 32, change to IRQ mode */
787 		if (!use_dma && xfer->len > S3C64XX_SPI_POLLING_SIZE)
788 			use_irq = true;
789 
790 		if (use_irq) {
791 			reinit_completion(&sdd->xfer_completion);
792 
793 			rdy_lv = xfer->len;
794 			/* Setup RDY_FIFO trigger Level
795 			 * RDY_LVL =
796 			 * fifo_lvl up to 64 byte -> N bytes
797 			 *               128 byte -> RDY_LVL * 2 bytes
798 			 *               256 byte -> RDY_LVL * 4 bytes
799 			 */
800 			if (fifo_len == 128)
801 				rdy_lv /= 2;
802 			else if (fifo_len == 256)
803 				rdy_lv /= 4;
804 
805 			val = readl(sdd->regs + S3C64XX_SPI_MODE_CFG);
806 			val &= ~S3C64XX_SPI_MODE_RX_RDY_LVL;
807 			val |= (rdy_lv << S3C64XX_SPI_MODE_RX_RDY_LVL_SHIFT);
808 			writel(val, sdd->regs + S3C64XX_SPI_MODE_CFG);
809 
810 			/* Enable FIFO_RDY_EN IRQ */
811 			val = readl(sdd->regs + S3C64XX_SPI_INT_EN);
812 			writel((val | S3C64XX_SPI_INT_RX_FIFORDY_EN),
813 					sdd->regs + S3C64XX_SPI_INT_EN);
814 
815 		}
816 
817 		spin_lock_irqsave(&sdd->lock, flags);
818 
819 		/* Pending only which is to be done */
820 		sdd->state &= ~RXBUSY;
821 		sdd->state &= ~TXBUSY;
822 
823 		/* Start the signals */
824 		s3c64xx_spi_set_cs(spi, true);
825 
826 		status = s3c64xx_enable_datapath(sdd, xfer, use_dma);
827 
828 		spin_unlock_irqrestore(&sdd->lock, flags);
829 
830 		if (status) {
831 			dev_err(&spi->dev, "failed to enable data path for transfer: %d\n", status);
832 			break;
833 		}
834 
835 		if (use_dma)
836 			status = s3c64xx_wait_for_dma(sdd, xfer);
837 		else
838 			status = s3c64xx_wait_for_pio(sdd, xfer, use_irq);
839 
840 		if (status) {
841 			dev_err(&spi->dev,
842 				"I/O Error: rx-%d tx-%d rx-%c tx-%c len-%d dma-%d res-(%d)\n",
843 				xfer->rx_buf ? 1 : 0, xfer->tx_buf ? 1 : 0,
844 				(sdd->state & RXBUSY) ? 'f' : 'p',
845 				(sdd->state & TXBUSY) ? 'f' : 'p',
846 				xfer->len, use_dma ? 1 : 0, status);
847 
848 			if (use_dma) {
849 				struct dma_tx_state s;
850 
851 				if (xfer->tx_buf && (sdd->state & TXBUSY)) {
852 					dmaengine_pause(sdd->tx_dma.ch);
853 					dmaengine_tx_status(sdd->tx_dma.ch, sdd->tx_dma.cookie, &s);
854 					dmaengine_terminate_all(sdd->tx_dma.ch);
855 					dev_err(&spi->dev, "TX residue: %d\n", s.residue);
856 
857 				}
858 				if (xfer->rx_buf && (sdd->state & RXBUSY)) {
859 					dmaengine_pause(sdd->rx_dma.ch);
860 					dmaengine_tx_status(sdd->rx_dma.ch, sdd->rx_dma.cookie, &s);
861 					dmaengine_terminate_all(sdd->rx_dma.ch);
862 					dev_err(&spi->dev, "RX residue: %d\n", s.residue);
863 				}
864 			}
865 		} else {
866 			s3c64xx_flush_fifo(sdd);
867 		}
868 		if (target_len > 0) {
869 			target_len -= xfer->len;
870 
871 			if (xfer->tx_buf)
872 				xfer->tx_buf += xfer->len;
873 
874 			if (xfer->rx_buf)
875 				xfer->rx_buf += xfer->len;
876 
877 			if (target_len >= fifo_len)
878 				xfer->len = fifo_len - 1;
879 			else
880 				xfer->len = target_len;
881 		}
882 	} while (target_len > 0);
883 
884 	if (origin_len) {
885 		/* Restore original xfer buffers and length */
886 		xfer->tx_buf = tx_buf;
887 		xfer->rx_buf = rx_buf;
888 		xfer->len = origin_len;
889 	}
890 
891 	return status;
892 }
893 
894 static struct s3c64xx_spi_csinfo *s3c64xx_get_slave_ctrldata(
895 				struct spi_device *spi)
896 {
897 	struct s3c64xx_spi_csinfo *cs;
898 	struct device_node *slave_np, *data_np = NULL;
899 	u32 fb_delay = 0;
900 
901 	slave_np = spi->dev.of_node;
902 	if (!slave_np) {
903 		dev_err(&spi->dev, "device node not found\n");
904 		return ERR_PTR(-EINVAL);
905 	}
906 
907 	cs = kzalloc(sizeof(*cs), GFP_KERNEL);
908 	if (!cs)
909 		return ERR_PTR(-ENOMEM);
910 
911 	data_np = of_get_child_by_name(slave_np, "controller-data");
912 	if (!data_np) {
913 		dev_info(&spi->dev, "feedback delay set to default (0)\n");
914 		return cs;
915 	}
916 
917 	of_property_read_u32(data_np, "samsung,spi-feedback-delay", &fb_delay);
918 	cs->fb_delay = fb_delay;
919 	of_node_put(data_np);
920 	return cs;
921 }
922 
923 /*
924  * Here we only check the validity of requested configuration
925  * and save the configuration in a local data-structure.
926  * The controller is actually configured only just before we
927  * get a message to transfer.
928  */
929 static int s3c64xx_spi_setup(struct spi_device *spi)
930 {
931 	struct s3c64xx_spi_csinfo *cs = spi->controller_data;
932 	struct s3c64xx_spi_driver_data *sdd;
933 	int err;
934 	int div;
935 
936 	sdd = spi_master_get_devdata(spi->master);
937 	if (spi->dev.of_node) {
938 		cs = s3c64xx_get_slave_ctrldata(spi);
939 		spi->controller_data = cs;
940 	}
941 
942 	/* NULL is fine, we just avoid using the FB delay (=0) */
943 	if (IS_ERR(cs)) {
944 		dev_err(&spi->dev, "No CS for SPI(%d)\n", spi_get_chipselect(spi, 0));
945 		return -ENODEV;
946 	}
947 
948 	if (!spi_get_ctldata(spi))
949 		spi_set_ctldata(spi, cs);
950 
951 	pm_runtime_get_sync(&sdd->pdev->dev);
952 
953 	div = sdd->port_conf->clk_div;
954 
955 	/* Check if we can provide the requested rate */
956 	if (!sdd->port_conf->clk_from_cmu) {
957 		u32 psr, speed;
958 
959 		/* Max possible */
960 		speed = clk_get_rate(sdd->src_clk) / div / (0 + 1);
961 
962 		if (spi->max_speed_hz > speed)
963 			spi->max_speed_hz = speed;
964 
965 		psr = clk_get_rate(sdd->src_clk) / div / spi->max_speed_hz - 1;
966 		psr &= S3C64XX_SPI_PSR_MASK;
967 		if (psr == S3C64XX_SPI_PSR_MASK)
968 			psr--;
969 
970 		speed = clk_get_rate(sdd->src_clk) / div / (psr + 1);
971 		if (spi->max_speed_hz < speed) {
972 			if (psr+1 < S3C64XX_SPI_PSR_MASK) {
973 				psr++;
974 			} else {
975 				err = -EINVAL;
976 				goto setup_exit;
977 			}
978 		}
979 
980 		speed = clk_get_rate(sdd->src_clk) / div / (psr + 1);
981 		if (spi->max_speed_hz >= speed) {
982 			spi->max_speed_hz = speed;
983 		} else {
984 			dev_err(&spi->dev, "Can't set %dHz transfer speed\n",
985 				spi->max_speed_hz);
986 			err = -EINVAL;
987 			goto setup_exit;
988 		}
989 	}
990 
991 	pm_runtime_mark_last_busy(&sdd->pdev->dev);
992 	pm_runtime_put_autosuspend(&sdd->pdev->dev);
993 	s3c64xx_spi_set_cs(spi, false);
994 
995 	return 0;
996 
997 setup_exit:
998 	pm_runtime_mark_last_busy(&sdd->pdev->dev);
999 	pm_runtime_put_autosuspend(&sdd->pdev->dev);
1000 	/* setup() returns with device de-selected */
1001 	s3c64xx_spi_set_cs(spi, false);
1002 
1003 	spi_set_ctldata(spi, NULL);
1004 
1005 	/* This was dynamically allocated on the DT path */
1006 	if (spi->dev.of_node)
1007 		kfree(cs);
1008 
1009 	return err;
1010 }
1011 
1012 static void s3c64xx_spi_cleanup(struct spi_device *spi)
1013 {
1014 	struct s3c64xx_spi_csinfo *cs = spi_get_ctldata(spi);
1015 
1016 	/* This was dynamically allocated on the DT path */
1017 	if (spi->dev.of_node)
1018 		kfree(cs);
1019 
1020 	spi_set_ctldata(spi, NULL);
1021 }
1022 
1023 static irqreturn_t s3c64xx_spi_irq(int irq, void *data)
1024 {
1025 	struct s3c64xx_spi_driver_data *sdd = data;
1026 	struct spi_master *spi = sdd->master;
1027 	unsigned int val, clr = 0;
1028 
1029 	val = readl(sdd->regs + S3C64XX_SPI_STATUS);
1030 
1031 	if (val & S3C64XX_SPI_ST_RX_OVERRUN_ERR) {
1032 		clr = S3C64XX_SPI_PND_RX_OVERRUN_CLR;
1033 		dev_err(&spi->dev, "RX overrun\n");
1034 	}
1035 	if (val & S3C64XX_SPI_ST_RX_UNDERRUN_ERR) {
1036 		clr |= S3C64XX_SPI_PND_RX_UNDERRUN_CLR;
1037 		dev_err(&spi->dev, "RX underrun\n");
1038 	}
1039 	if (val & S3C64XX_SPI_ST_TX_OVERRUN_ERR) {
1040 		clr |= S3C64XX_SPI_PND_TX_OVERRUN_CLR;
1041 		dev_err(&spi->dev, "TX overrun\n");
1042 	}
1043 	if (val & S3C64XX_SPI_ST_TX_UNDERRUN_ERR) {
1044 		clr |= S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
1045 		dev_err(&spi->dev, "TX underrun\n");
1046 	}
1047 
1048 	if (val & S3C64XX_SPI_ST_RX_FIFORDY) {
1049 		complete(&sdd->xfer_completion);
1050 		/* No pending clear irq, turn-off INT_EN_RX_FIFO_RDY */
1051 		val = readl(sdd->regs + S3C64XX_SPI_INT_EN);
1052 		writel((val & ~S3C64XX_SPI_INT_RX_FIFORDY_EN),
1053 				sdd->regs + S3C64XX_SPI_INT_EN);
1054 	}
1055 
1056 	/* Clear the pending irq by setting and then clearing it */
1057 	writel(clr, sdd->regs + S3C64XX_SPI_PENDING_CLR);
1058 	writel(0, sdd->regs + S3C64XX_SPI_PENDING_CLR);
1059 
1060 	return IRQ_HANDLED;
1061 }
1062 
1063 static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data *sdd)
1064 {
1065 	struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
1066 	void __iomem *regs = sdd->regs;
1067 	unsigned int val;
1068 
1069 	sdd->cur_speed = 0;
1070 
1071 	if (sci->no_cs)
1072 		writel(0, sdd->regs + S3C64XX_SPI_CS_REG);
1073 	else if (!(sdd->port_conf->quirks & S3C64XX_SPI_QUIRK_CS_AUTO))
1074 		writel(S3C64XX_SPI_CS_SIG_INACT, sdd->regs + S3C64XX_SPI_CS_REG);
1075 
1076 	/* Disable Interrupts - we use Polling if not DMA mode */
1077 	writel(0, regs + S3C64XX_SPI_INT_EN);
1078 
1079 	if (!sdd->port_conf->clk_from_cmu)
1080 		writel(sci->src_clk_nr << S3C64XX_SPI_CLKSEL_SRCSHFT,
1081 				regs + S3C64XX_SPI_CLK_CFG);
1082 	writel(0, regs + S3C64XX_SPI_MODE_CFG);
1083 	writel(0, regs + S3C64XX_SPI_PACKET_CNT);
1084 
1085 	/* Clear any irq pending bits, should set and clear the bits */
1086 	val = S3C64XX_SPI_PND_RX_OVERRUN_CLR |
1087 		S3C64XX_SPI_PND_RX_UNDERRUN_CLR |
1088 		S3C64XX_SPI_PND_TX_OVERRUN_CLR |
1089 		S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
1090 	writel(val, regs + S3C64XX_SPI_PENDING_CLR);
1091 	writel(0, regs + S3C64XX_SPI_PENDING_CLR);
1092 
1093 	writel(0, regs + S3C64XX_SPI_SWAP_CFG);
1094 
1095 	val = readl(regs + S3C64XX_SPI_MODE_CFG);
1096 	val &= ~S3C64XX_SPI_MODE_4BURST;
1097 	val &= ~(S3C64XX_SPI_MAX_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
1098 	val |= (S3C64XX_SPI_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
1099 	writel(val, regs + S3C64XX_SPI_MODE_CFG);
1100 
1101 	s3c64xx_flush_fifo(sdd);
1102 }
1103 
1104 #ifdef CONFIG_OF
1105 static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
1106 {
1107 	struct s3c64xx_spi_info *sci;
1108 	u32 temp;
1109 
1110 	sci = devm_kzalloc(dev, sizeof(*sci), GFP_KERNEL);
1111 	if (!sci)
1112 		return ERR_PTR(-ENOMEM);
1113 
1114 	if (of_property_read_u32(dev->of_node, "samsung,spi-src-clk", &temp)) {
1115 		dev_warn(dev, "spi bus clock parent not specified, using clock at index 0 as parent\n");
1116 		sci->src_clk_nr = 0;
1117 	} else {
1118 		sci->src_clk_nr = temp;
1119 	}
1120 
1121 	if (of_property_read_u32(dev->of_node, "num-cs", &temp)) {
1122 		dev_warn(dev, "number of chip select lines not specified, assuming 1 chip select line\n");
1123 		sci->num_cs = 1;
1124 	} else {
1125 		sci->num_cs = temp;
1126 	}
1127 
1128 	sci->no_cs = of_property_read_bool(dev->of_node, "no-cs-readback");
1129 	sci->polling = !of_property_present(dev->of_node, "dmas");
1130 
1131 	return sci;
1132 }
1133 #else
1134 static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
1135 {
1136 	return dev_get_platdata(dev);
1137 }
1138 #endif
1139 
1140 static inline const struct s3c64xx_spi_port_config *s3c64xx_spi_get_port_config(
1141 						struct platform_device *pdev)
1142 {
1143 #ifdef CONFIG_OF
1144 	if (pdev->dev.of_node)
1145 		return of_device_get_match_data(&pdev->dev);
1146 #endif
1147 	return (const struct s3c64xx_spi_port_config *)platform_get_device_id(pdev)->driver_data;
1148 }
1149 
1150 static int s3c64xx_spi_probe(struct platform_device *pdev)
1151 {
1152 	struct resource	*mem_res;
1153 	struct s3c64xx_spi_driver_data *sdd;
1154 	struct s3c64xx_spi_info *sci = dev_get_platdata(&pdev->dev);
1155 	struct spi_master *master;
1156 	int ret, irq;
1157 	char clk_name[16];
1158 
1159 	if (!sci && pdev->dev.of_node) {
1160 		sci = s3c64xx_spi_parse_dt(&pdev->dev);
1161 		if (IS_ERR(sci))
1162 			return PTR_ERR(sci);
1163 	}
1164 
1165 	if (!sci)
1166 		return dev_err_probe(&pdev->dev, -ENODEV,
1167 				     "Platform_data missing!\n");
1168 
1169 	mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1170 	if (!mem_res)
1171 		return dev_err_probe(&pdev->dev, -ENXIO,
1172 				     "Unable to get SPI MEM resource\n");
1173 
1174 	irq = platform_get_irq(pdev, 0);
1175 	if (irq < 0)
1176 		return dev_err_probe(&pdev->dev, irq, "Failed to get IRQ\n");
1177 
1178 	master = devm_spi_alloc_master(&pdev->dev, sizeof(*sdd));
1179 	if (!master)
1180 		return dev_err_probe(&pdev->dev, -ENOMEM,
1181 				     "Unable to allocate SPI Master\n");
1182 
1183 	platform_set_drvdata(pdev, master);
1184 
1185 	sdd = spi_master_get_devdata(master);
1186 	sdd->port_conf = s3c64xx_spi_get_port_config(pdev);
1187 	sdd->master = master;
1188 	sdd->cntrlr_info = sci;
1189 	sdd->pdev = pdev;
1190 	sdd->sfr_start = mem_res->start;
1191 	if (pdev->dev.of_node) {
1192 		ret = of_alias_get_id(pdev->dev.of_node, "spi");
1193 		if (ret < 0)
1194 			return dev_err_probe(&pdev->dev, ret,
1195 					     "Failed to get alias id\n");
1196 		sdd->port_id = ret;
1197 	} else {
1198 		sdd->port_id = pdev->id;
1199 	}
1200 
1201 	sdd->cur_bpw = 8;
1202 
1203 	sdd->tx_dma.direction = DMA_MEM_TO_DEV;
1204 	sdd->rx_dma.direction = DMA_DEV_TO_MEM;
1205 
1206 	master->dev.of_node = pdev->dev.of_node;
1207 	master->bus_num = sdd->port_id;
1208 	master->setup = s3c64xx_spi_setup;
1209 	master->cleanup = s3c64xx_spi_cleanup;
1210 	master->prepare_transfer_hardware = s3c64xx_spi_prepare_transfer;
1211 	master->unprepare_transfer_hardware = s3c64xx_spi_unprepare_transfer;
1212 	master->prepare_message = s3c64xx_spi_prepare_message;
1213 	master->transfer_one = s3c64xx_spi_transfer_one;
1214 	master->max_transfer_size = s3c64xx_spi_max_transfer_size;
1215 	master->num_chipselect = sci->num_cs;
1216 	master->use_gpio_descriptors = true;
1217 	master->dma_alignment = 8;
1218 	master->bits_per_word_mask = SPI_BPW_MASK(32) | SPI_BPW_MASK(16) |
1219 					SPI_BPW_MASK(8);
1220 	/* the spi->mode bits understood by this driver: */
1221 	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1222 	if (sdd->port_conf->has_loopback)
1223 		master->mode_bits |= SPI_LOOP;
1224 	master->auto_runtime_pm = true;
1225 	if (!is_polling(sdd))
1226 		master->can_dma = s3c64xx_spi_can_dma;
1227 
1228 	sdd->regs = devm_ioremap_resource(&pdev->dev, mem_res);
1229 	if (IS_ERR(sdd->regs))
1230 		return PTR_ERR(sdd->regs);
1231 
1232 	if (sci->cfg_gpio && sci->cfg_gpio())
1233 		return dev_err_probe(&pdev->dev, -EBUSY,
1234 				     "Unable to config gpio\n");
1235 
1236 	/* Setup clocks */
1237 	sdd->clk = devm_clk_get_enabled(&pdev->dev, "spi");
1238 	if (IS_ERR(sdd->clk))
1239 		return dev_err_probe(&pdev->dev, PTR_ERR(sdd->clk),
1240 				     "Unable to acquire clock 'spi'\n");
1241 
1242 	sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr);
1243 	sdd->src_clk = devm_clk_get_enabled(&pdev->dev, clk_name);
1244 	if (IS_ERR(sdd->src_clk))
1245 		return dev_err_probe(&pdev->dev, PTR_ERR(sdd->src_clk),
1246 				     "Unable to acquire clock '%s'\n",
1247 				     clk_name);
1248 
1249 	if (sdd->port_conf->clk_ioclk) {
1250 		sdd->ioclk = devm_clk_get_enabled(&pdev->dev, "spi_ioclk");
1251 		if (IS_ERR(sdd->ioclk))
1252 			return dev_err_probe(&pdev->dev, PTR_ERR(sdd->ioclk),
1253 					     "Unable to acquire 'ioclk'\n");
1254 	}
1255 
1256 	pm_runtime_set_autosuspend_delay(&pdev->dev, AUTOSUSPEND_TIMEOUT);
1257 	pm_runtime_use_autosuspend(&pdev->dev);
1258 	pm_runtime_set_active(&pdev->dev);
1259 	pm_runtime_enable(&pdev->dev);
1260 	pm_runtime_get_sync(&pdev->dev);
1261 
1262 	/* Setup Deufult Mode */
1263 	s3c64xx_spi_hwinit(sdd);
1264 
1265 	spin_lock_init(&sdd->lock);
1266 	init_completion(&sdd->xfer_completion);
1267 
1268 	ret = devm_request_irq(&pdev->dev, irq, s3c64xx_spi_irq, 0,
1269 				"spi-s3c64xx", sdd);
1270 	if (ret != 0) {
1271 		dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n",
1272 			irq, ret);
1273 		goto err_pm_put;
1274 	}
1275 
1276 	writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
1277 	       S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
1278 	       sdd->regs + S3C64XX_SPI_INT_EN);
1279 
1280 	ret = devm_spi_register_master(&pdev->dev, master);
1281 	if (ret != 0) {
1282 		dev_err(&pdev->dev, "cannot register SPI master: %d\n", ret);
1283 		goto err_pm_put;
1284 	}
1285 
1286 	dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d with %d Slaves attached\n",
1287 					sdd->port_id, master->num_chipselect);
1288 	dev_dbg(&pdev->dev, "\tIOmem=[%pR]\tFIFO %dbytes\n",
1289 					mem_res, (FIFO_LVL_MASK(sdd) >> 1) + 1);
1290 
1291 	pm_runtime_mark_last_busy(&pdev->dev);
1292 	pm_runtime_put_autosuspend(&pdev->dev);
1293 
1294 	return 0;
1295 
1296 err_pm_put:
1297 	pm_runtime_put_noidle(&pdev->dev);
1298 	pm_runtime_disable(&pdev->dev);
1299 	pm_runtime_set_suspended(&pdev->dev);
1300 
1301 	return ret;
1302 }
1303 
1304 static void s3c64xx_spi_remove(struct platform_device *pdev)
1305 {
1306 	struct spi_master *master = platform_get_drvdata(pdev);
1307 	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1308 
1309 	pm_runtime_get_sync(&pdev->dev);
1310 
1311 	writel(0, sdd->regs + S3C64XX_SPI_INT_EN);
1312 
1313 	if (!is_polling(sdd)) {
1314 		dma_release_channel(sdd->rx_dma.ch);
1315 		dma_release_channel(sdd->tx_dma.ch);
1316 	}
1317 
1318 	pm_runtime_put_noidle(&pdev->dev);
1319 	pm_runtime_disable(&pdev->dev);
1320 	pm_runtime_set_suspended(&pdev->dev);
1321 }
1322 
1323 #ifdef CONFIG_PM_SLEEP
1324 static int s3c64xx_spi_suspend(struct device *dev)
1325 {
1326 	struct spi_master *master = dev_get_drvdata(dev);
1327 	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1328 
1329 	int ret = spi_master_suspend(master);
1330 	if (ret)
1331 		return ret;
1332 
1333 	ret = pm_runtime_force_suspend(dev);
1334 	if (ret < 0)
1335 		return ret;
1336 
1337 	sdd->cur_speed = 0; /* Output Clock is stopped */
1338 
1339 	return 0;
1340 }
1341 
1342 static int s3c64xx_spi_resume(struct device *dev)
1343 {
1344 	struct spi_master *master = dev_get_drvdata(dev);
1345 	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1346 	struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
1347 	int ret;
1348 
1349 	if (sci->cfg_gpio)
1350 		sci->cfg_gpio();
1351 
1352 	ret = pm_runtime_force_resume(dev);
1353 	if (ret < 0)
1354 		return ret;
1355 
1356 	return spi_master_resume(master);
1357 }
1358 #endif /* CONFIG_PM_SLEEP */
1359 
1360 #ifdef CONFIG_PM
1361 static int s3c64xx_spi_runtime_suspend(struct device *dev)
1362 {
1363 	struct spi_master *master = dev_get_drvdata(dev);
1364 	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1365 
1366 	clk_disable_unprepare(sdd->clk);
1367 	clk_disable_unprepare(sdd->src_clk);
1368 	clk_disable_unprepare(sdd->ioclk);
1369 
1370 	return 0;
1371 }
1372 
1373 static int s3c64xx_spi_runtime_resume(struct device *dev)
1374 {
1375 	struct spi_master *master = dev_get_drvdata(dev);
1376 	struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1377 	int ret;
1378 
1379 	if (sdd->port_conf->clk_ioclk) {
1380 		ret = clk_prepare_enable(sdd->ioclk);
1381 		if (ret != 0)
1382 			return ret;
1383 	}
1384 
1385 	ret = clk_prepare_enable(sdd->src_clk);
1386 	if (ret != 0)
1387 		goto err_disable_ioclk;
1388 
1389 	ret = clk_prepare_enable(sdd->clk);
1390 	if (ret != 0)
1391 		goto err_disable_src_clk;
1392 
1393 	s3c64xx_spi_hwinit(sdd);
1394 
1395 	writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
1396 	       S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
1397 	       sdd->regs + S3C64XX_SPI_INT_EN);
1398 
1399 	return 0;
1400 
1401 err_disable_src_clk:
1402 	clk_disable_unprepare(sdd->src_clk);
1403 err_disable_ioclk:
1404 	clk_disable_unprepare(sdd->ioclk);
1405 
1406 	return ret;
1407 }
1408 #endif /* CONFIG_PM */
1409 
1410 static const struct dev_pm_ops s3c64xx_spi_pm = {
1411 	SET_SYSTEM_SLEEP_PM_OPS(s3c64xx_spi_suspend, s3c64xx_spi_resume)
1412 	SET_RUNTIME_PM_OPS(s3c64xx_spi_runtime_suspend,
1413 			   s3c64xx_spi_runtime_resume, NULL)
1414 };
1415 
1416 static const struct s3c64xx_spi_port_config s3c2443_spi_port_config = {
1417 	.fifo_lvl_mask	= { 0x7f },
1418 	.rx_lvl_offset	= 13,
1419 	.tx_st_done	= 21,
1420 	.clk_div	= 2,
1421 	.high_speed	= true,
1422 };
1423 
1424 static const struct s3c64xx_spi_port_config s3c6410_spi_port_config = {
1425 	.fifo_lvl_mask	= { 0x7f, 0x7F },
1426 	.rx_lvl_offset	= 13,
1427 	.tx_st_done	= 21,
1428 	.clk_div	= 2,
1429 };
1430 
1431 static const struct s3c64xx_spi_port_config s5pv210_spi_port_config = {
1432 	.fifo_lvl_mask	= { 0x1ff, 0x7F },
1433 	.rx_lvl_offset	= 15,
1434 	.tx_st_done	= 25,
1435 	.clk_div	= 2,
1436 	.high_speed	= true,
1437 };
1438 
1439 static const struct s3c64xx_spi_port_config exynos4_spi_port_config = {
1440 	.fifo_lvl_mask	= { 0x1ff, 0x7F, 0x7F },
1441 	.rx_lvl_offset	= 15,
1442 	.tx_st_done	= 25,
1443 	.clk_div	= 2,
1444 	.high_speed	= true,
1445 	.clk_from_cmu	= true,
1446 	.quirks		= S3C64XX_SPI_QUIRK_CS_AUTO,
1447 };
1448 
1449 static const struct s3c64xx_spi_port_config exynos7_spi_port_config = {
1450 	.fifo_lvl_mask	= { 0x1ff, 0x7F, 0x7F, 0x7F, 0x7F, 0x1ff},
1451 	.rx_lvl_offset	= 15,
1452 	.tx_st_done	= 25,
1453 	.clk_div	= 2,
1454 	.high_speed	= true,
1455 	.clk_from_cmu	= true,
1456 	.quirks		= S3C64XX_SPI_QUIRK_CS_AUTO,
1457 };
1458 
1459 static const struct s3c64xx_spi_port_config exynos5433_spi_port_config = {
1460 	.fifo_lvl_mask	= { 0x1ff, 0x7f, 0x7f, 0x7f, 0x7f, 0x1ff},
1461 	.rx_lvl_offset	= 15,
1462 	.tx_st_done	= 25,
1463 	.clk_div	= 2,
1464 	.high_speed	= true,
1465 	.clk_from_cmu	= true,
1466 	.clk_ioclk	= true,
1467 	.quirks		= S3C64XX_SPI_QUIRK_CS_AUTO,
1468 };
1469 
1470 static const struct s3c64xx_spi_port_config exynosautov9_spi_port_config = {
1471 	.fifo_lvl_mask	= { 0x1ff, 0x1ff, 0x7f, 0x7f, 0x7f, 0x7f, 0x1ff, 0x7f,
1472 			    0x7f, 0x7f, 0x7f, 0x7f},
1473 	.rx_lvl_offset	= 15,
1474 	.tx_st_done	= 25,
1475 	.clk_div	= 4,
1476 	.high_speed	= true,
1477 	.clk_from_cmu	= true,
1478 	.clk_ioclk	= true,
1479 	.has_loopback	= true,
1480 	.quirks		= S3C64XX_SPI_QUIRK_CS_AUTO,
1481 };
1482 
1483 static const struct s3c64xx_spi_port_config fsd_spi_port_config = {
1484 	.fifo_lvl_mask	= { 0x7f, 0x7f, 0x7f, 0x7f, 0x7f},
1485 	.rx_lvl_offset	= 15,
1486 	.tx_st_done	= 25,
1487 	.clk_div	= 2,
1488 	.high_speed	= true,
1489 	.clk_from_cmu	= true,
1490 	.clk_ioclk	= false,
1491 	.quirks		= S3C64XX_SPI_QUIRK_CS_AUTO,
1492 };
1493 
1494 static const struct platform_device_id s3c64xx_spi_driver_ids[] = {
1495 	{
1496 		.name		= "s3c2443-spi",
1497 		.driver_data	= (kernel_ulong_t)&s3c2443_spi_port_config,
1498 	}, {
1499 		.name		= "s3c6410-spi",
1500 		.driver_data	= (kernel_ulong_t)&s3c6410_spi_port_config,
1501 	},
1502 	{ },
1503 };
1504 
1505 static const struct of_device_id s3c64xx_spi_dt_match[] = {
1506 	{ .compatible = "samsung,s3c2443-spi",
1507 			.data = (void *)&s3c2443_spi_port_config,
1508 	},
1509 	{ .compatible = "samsung,s3c6410-spi",
1510 			.data = (void *)&s3c6410_spi_port_config,
1511 	},
1512 	{ .compatible = "samsung,s5pv210-spi",
1513 			.data = (void *)&s5pv210_spi_port_config,
1514 	},
1515 	{ .compatible = "samsung,exynos4210-spi",
1516 			.data = (void *)&exynos4_spi_port_config,
1517 	},
1518 	{ .compatible = "samsung,exynos7-spi",
1519 			.data = (void *)&exynos7_spi_port_config,
1520 	},
1521 	{ .compatible = "samsung,exynos5433-spi",
1522 			.data = (void *)&exynos5433_spi_port_config,
1523 	},
1524 	{ .compatible = "samsung,exynosautov9-spi",
1525 			.data = (void *)&exynosautov9_spi_port_config,
1526 	},
1527 	{ .compatible = "tesla,fsd-spi",
1528 			.data = (void *)&fsd_spi_port_config,
1529 	},
1530 	{ },
1531 };
1532 MODULE_DEVICE_TABLE(of, s3c64xx_spi_dt_match);
1533 
1534 static struct platform_driver s3c64xx_spi_driver = {
1535 	.driver = {
1536 		.name	= "s3c64xx-spi",
1537 		.pm = &s3c64xx_spi_pm,
1538 		.of_match_table = of_match_ptr(s3c64xx_spi_dt_match),
1539 	},
1540 	.probe = s3c64xx_spi_probe,
1541 	.remove_new = s3c64xx_spi_remove,
1542 	.id_table = s3c64xx_spi_driver_ids,
1543 };
1544 MODULE_ALIAS("platform:s3c64xx-spi");
1545 
1546 module_platform_driver(s3c64xx_spi_driver);
1547 
1548 MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
1549 MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
1550 MODULE_LICENSE("GPL");
1551