xref: /linux/drivers/spi/spi-s3c64xx.c (revision 60b9f47eb3b01f829a94f7fea81bc8d59ff93dc2)
178b5d705SAndi Shyti // SPDX-License-Identifier: GPL-2.0+
278b5d705SAndi Shyti //
378b5d705SAndi Shyti // Copyright (c) 2009 Samsung Electronics Co., Ltd.
478b5d705SAndi Shyti //      Jaswinder Singh <jassi.brar@samsung.com>
5ca632f55SGrant Likely 
6d6911cf2STudor Ambarus #include <linux/bitops.h>
74568fa57STudor Ambarus #include <linux/bits.h>
8ca632f55SGrant Likely #include <linux/clk.h>
9a77ce80fSTudor Ambarus #include <linux/delay.h>
10ca632f55SGrant Likely #include <linux/dma-mapping.h>
1178843727SArnd Bergmann #include <linux/dmaengine.h>
12a77ce80fSTudor Ambarus #include <linux/init.h>
13a77ce80fSTudor Ambarus #include <linux/interrupt.h>
1442a9ac37STudor Ambarus #include <linux/io.h>
15a77ce80fSTudor Ambarus #include <linux/module.h>
16a77ce80fSTudor Ambarus #include <linux/of.h>
17a77ce80fSTudor Ambarus #include <linux/platform_data/spi-s3c64xx.h>
18ca632f55SGrant Likely #include <linux/platform_device.h>
19b97b6621SMark Brown #include <linux/pm_runtime.h>
20ca632f55SGrant Likely #include <linux/spi/spi.h>
217256d6bdSTudor Ambarus #include <linux/types.h>
22ca632f55SGrant Likely 
23e8b16c7aSTudor Ambarus #define MAX_SPI_PORTS		12
24bf77cba9SPadmavathi Venna #define S3C64XX_SPI_QUIRK_CS_AUTO	(1 << 1)
25483867eeSHeiner Kallweit #define AUTOSUSPEND_TIMEOUT	2000
26a5238e36SThomas Abraham 
27ca632f55SGrant Likely /* Registers and bit-fields */
28ca632f55SGrant Likely 
29ca632f55SGrant Likely #define S3C64XX_SPI_CH_CFG		0x00
30ca632f55SGrant Likely #define S3C64XX_SPI_CLK_CFG		0x04
31ca632f55SGrant Likely #define S3C64XX_SPI_MODE_CFG		0x08
32913ba5c9SŁukasz Stelmach #define S3C64XX_SPI_CS_REG		0x0C
33ca632f55SGrant Likely #define S3C64XX_SPI_INT_EN		0x10
34ca632f55SGrant Likely #define S3C64XX_SPI_STATUS		0x14
35ca632f55SGrant Likely #define S3C64XX_SPI_TX_DATA		0x18
36ca632f55SGrant Likely #define S3C64XX_SPI_RX_DATA		0x1C
37ca632f55SGrant Likely #define S3C64XX_SPI_PACKET_CNT		0x20
38ca632f55SGrant Likely #define S3C64XX_SPI_PENDING_CLR		0x24
39ca632f55SGrant Likely #define S3C64XX_SPI_SWAP_CFG		0x28
40ca632f55SGrant Likely #define S3C64XX_SPI_FB_CLK		0x2C
41ca632f55SGrant Likely 
42ca632f55SGrant Likely #define S3C64XX_SPI_CH_HS_EN		(1<<6)	/* High Speed Enable */
43ca632f55SGrant Likely #define S3C64XX_SPI_CH_SW_RST		(1<<5)
44ca632f55SGrant Likely #define S3C64XX_SPI_CH_SLAVE		(1<<4)
45ca632f55SGrant Likely #define S3C64XX_SPI_CPOL_L		(1<<3)
46ca632f55SGrant Likely #define S3C64XX_SPI_CPHA_B		(1<<2)
47ca632f55SGrant Likely #define S3C64XX_SPI_CH_RXCH_ON		(1<<1)
48ca632f55SGrant Likely #define S3C64XX_SPI_CH_TXCH_ON		(1<<0)
49ca632f55SGrant Likely 
50ca632f55SGrant Likely #define S3C64XX_SPI_CLKSEL_SRCMSK	(3<<9)
51ca632f55SGrant Likely #define S3C64XX_SPI_CLKSEL_SRCSHFT	9
52ca632f55SGrant Likely #define S3C64XX_SPI_ENCLK_ENABLE	(1<<8)
53ca632f55SGrant Likely #define S3C64XX_SPI_PSR_MASK		0xff
54ca632f55SGrant Likely 
55ca632f55SGrant Likely #define S3C64XX_SPI_MODE_CH_TSZ_BYTE		(0<<29)
56ca632f55SGrant Likely #define S3C64XX_SPI_MODE_CH_TSZ_HALFWORD	(1<<29)
57ca632f55SGrant Likely #define S3C64XX_SPI_MODE_CH_TSZ_WORD		(2<<29)
58ca632f55SGrant Likely #define S3C64XX_SPI_MODE_CH_TSZ_MASK		(3<<29)
59ca632f55SGrant Likely #define S3C64XX_SPI_MODE_BUS_TSZ_BYTE		(0<<17)
60ca632f55SGrant Likely #define S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD	(1<<17)
61ca632f55SGrant Likely #define S3C64XX_SPI_MODE_BUS_TSZ_WORD		(2<<17)
62ca632f55SGrant Likely #define S3C64XX_SPI_MODE_BUS_TSZ_MASK		(3<<17)
631ee80671SJaewon Kim #define S3C64XX_SPI_MODE_RX_RDY_LVL		GENMASK(16, 11)
641ee80671SJaewon Kim #define S3C64XX_SPI_MODE_RX_RDY_LVL_SHIFT	11
65ffb7bcd3SChanho Park #define S3C64XX_SPI_MODE_SELF_LOOPBACK		(1<<3)
66ca632f55SGrant Likely #define S3C64XX_SPI_MODE_RXDMA_ON		(1<<2)
67ca632f55SGrant Likely #define S3C64XX_SPI_MODE_TXDMA_ON		(1<<1)
68ca632f55SGrant Likely #define S3C64XX_SPI_MODE_4BURST			(1<<0)
69ca632f55SGrant Likely 
70913ba5c9SŁukasz Stelmach #define S3C64XX_SPI_CS_NSC_CNT_2		(2<<4)
71913ba5c9SŁukasz Stelmach #define S3C64XX_SPI_CS_AUTO			(1<<1)
72913ba5c9SŁukasz Stelmach #define S3C64XX_SPI_CS_SIG_INACT		(1<<0)
73ca632f55SGrant Likely 
74ca632f55SGrant Likely #define S3C64XX_SPI_INT_TRAILING_EN		(1<<6)
75ca632f55SGrant Likely #define S3C64XX_SPI_INT_RX_OVERRUN_EN		(1<<5)
76ca632f55SGrant Likely #define S3C64XX_SPI_INT_RX_UNDERRUN_EN		(1<<4)
77ca632f55SGrant Likely #define S3C64XX_SPI_INT_TX_OVERRUN_EN		(1<<3)
78ca632f55SGrant Likely #define S3C64XX_SPI_INT_TX_UNDERRUN_EN		(1<<2)
79ca632f55SGrant Likely #define S3C64XX_SPI_INT_RX_FIFORDY_EN		(1<<1)
80ca632f55SGrant Likely #define S3C64XX_SPI_INT_TX_FIFORDY_EN		(1<<0)
81ca632f55SGrant Likely 
82e8b16c7aSTudor Ambarus #define S3C64XX_SPI_ST_RX_FIFO_RDY_V2		GENMASK(23, 15)
83e8b16c7aSTudor Ambarus #define S3C64XX_SPI_ST_TX_FIFO_RDY_V2		GENMASK(14, 6)
84ff8faa8aSTudor Ambarus #define S3C64XX_SPI_ST_TX_FIFO_LVL_SHIFT	6
85ca632f55SGrant Likely #define S3C64XX_SPI_ST_RX_OVERRUN_ERR		(1<<5)
86ca632f55SGrant Likely #define S3C64XX_SPI_ST_RX_UNDERRUN_ERR		(1<<4)
87ca632f55SGrant Likely #define S3C64XX_SPI_ST_TX_OVERRUN_ERR		(1<<3)
88ca632f55SGrant Likely #define S3C64XX_SPI_ST_TX_UNDERRUN_ERR		(1<<2)
89ca632f55SGrant Likely #define S3C64XX_SPI_ST_RX_FIFORDY		(1<<1)
90ca632f55SGrant Likely #define S3C64XX_SPI_ST_TX_FIFORDY		(1<<0)
91ca632f55SGrant Likely 
92ca632f55SGrant Likely #define S3C64XX_SPI_PACKET_CNT_EN		(1<<16)
931224e295SVincent Whitchurch #define S3C64XX_SPI_PACKET_CNT_MASK		GENMASK(15, 0)
94ca632f55SGrant Likely 
95ca632f55SGrant Likely #define S3C64XX_SPI_PND_TX_UNDERRUN_CLR		(1<<4)
96ca632f55SGrant Likely #define S3C64XX_SPI_PND_TX_OVERRUN_CLR		(1<<3)
97ca632f55SGrant Likely #define S3C64XX_SPI_PND_RX_UNDERRUN_CLR		(1<<2)
98ca632f55SGrant Likely #define S3C64XX_SPI_PND_RX_OVERRUN_CLR		(1<<1)
99ca632f55SGrant Likely #define S3C64XX_SPI_PND_TRAILING_CLR		(1<<0)
100ca632f55SGrant Likely 
101ca632f55SGrant Likely #define S3C64XX_SPI_SWAP_RX_HALF_WORD		(1<<7)
102ca632f55SGrant Likely #define S3C64XX_SPI_SWAP_RX_BYTE		(1<<6)
103ca632f55SGrant Likely #define S3C64XX_SPI_SWAP_RX_BIT			(1<<5)
104ca632f55SGrant Likely #define S3C64XX_SPI_SWAP_RX_EN			(1<<4)
105ca632f55SGrant Likely #define S3C64XX_SPI_SWAP_TX_HALF_WORD		(1<<3)
106ca632f55SGrant Likely #define S3C64XX_SPI_SWAP_TX_BYTE		(1<<2)
107ca632f55SGrant Likely #define S3C64XX_SPI_SWAP_TX_BIT			(1<<1)
108ca632f55SGrant Likely #define S3C64XX_SPI_SWAP_TX_EN			(1<<0)
109ca632f55SGrant Likely 
110ca632f55SGrant Likely #define S3C64XX_SPI_FBCLK_MSK			(3<<0)
111ca632f55SGrant Likely 
112a5238e36SThomas Abraham #define FIFO_LVL_MASK(i) ((i)->port_conf->fifo_lvl_mask[i->port_id])
113a5238e36SThomas Abraham #define S3C64XX_SPI_ST_TX_DONE(v, i) (((v) & \
114a5238e36SThomas Abraham 				(1 << (i)->port_conf->tx_st_done)) ? 1 : 0)
115d6911cf2STudor Ambarus #define TX_FIFO_LVL(v, sdd)	(((v) & (sdd)->tx_fifomask) >>		\
116d6911cf2STudor Ambarus 				 __ffs((sdd)->tx_fifomask))
117d6911cf2STudor Ambarus #define RX_FIFO_LVL(v, sdd)	(((v) & (sdd)->rx_fifomask) >>		\
118d6911cf2STudor Ambarus 				 __ffs((sdd)->rx_fifomask))
119460efee7SSam Protsenko #define FIFO_DEPTH(i) ((FIFO_LVL_MASK(i) >> 1) + 1)
120ca632f55SGrant Likely 
121ca632f55SGrant Likely #define S3C64XX_SPI_MAX_TRAILCNT	0x3ff
122ca632f55SGrant Likely #define S3C64XX_SPI_TRAILCNT_OFF	19
123ca632f55SGrant Likely 
1241ee80671SJaewon Kim #define S3C64XX_SPI_POLLING_SIZE	32
1251ee80671SJaewon Kim 
126ca632f55SGrant Likely #define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
127d1a7718eSJaewon Kim #define is_polling(x)	(x->cntrlr_info->polling)
128ca632f55SGrant Likely 
129ca632f55SGrant Likely #define RXBUSY    (1<<2)
130ca632f55SGrant Likely #define TXBUSY    (1<<3)
131ca632f55SGrant Likely 
13282ab8cd7SBoojin Kim struct s3c64xx_spi_dma_data {
13378843727SArnd Bergmann 	struct dma_chan *ch;
1342f4db6f7SŁukasz Stelmach 	dma_cookie_t cookie;
135c10356b9SArnd Bergmann 	enum dma_transfer_direction direction;
13682ab8cd7SBoojin Kim };
13782ab8cd7SBoojin Kim 
138ca632f55SGrant Likely /**
1397d712f79SKrzysztof Kozlowski  * struct s3c64xx_spi_port_config - SPI Controller hardware info
140ad0adac8STudor Ambarus  * @fifo_lvl_mask: [DEPRECATED] use @{rx, tx}_fifomask instead.
141ad0adac8STudor Ambarus  * @rx_lvl_offset: [DEPRECATED] use @{rx,tx}_fifomask instead.
14282b98fb8STudor Ambarus  * @fifo_depth: depth of the FIFO.
143d6911cf2STudor Ambarus  * @rx_fifomask: SPI_STATUS.RX_FIFO_LVL mask. Shifted mask defining the field's
144d6911cf2STudor Ambarus  *               length and position.
145d6911cf2STudor Ambarus  * @tx_fifomask: SPI_STATUS.TX_FIFO_LVL mask. Shifted mask defining the field's
146d6911cf2STudor Ambarus  *               length and position.
147a5238e36SThomas Abraham  * @tx_st_done: Bit offset of TX_DONE bit in SPI_STATUS regiter.
148bfcd27dcSChanho Park  * @clk_div: Internal clock divider
1496b8d1e47SLee Jones  * @quirks: Bitmask of known quirks
150a5238e36SThomas Abraham  * @high_speed: True, if the controller supports HIGH_SPEED_EN bit.
151a5238e36SThomas Abraham  * @clk_from_cmu: True, if the controller does not include a clock mux and
152a5238e36SThomas Abraham  *	prescaler unit.
1536b8d1e47SLee Jones  * @clk_ioclk: True if clock is present on this device
154ffb7bcd3SChanho Park  * @has_loopback: True if loopback mode can be supported
155b7bafb9fSTudor Ambarus  * @use_32bit_io: True if the SoC allows only 32-bit register accesses.
156a5238e36SThomas Abraham  *
157a5238e36SThomas Abraham  * The Samsung s3c64xx SPI controller are used on various Samsung SoC's but
158a5238e36SThomas Abraham  * differ in some aspects such as the size of the fifo and spi bus clock
159a5238e36SThomas Abraham  * setup. Such differences are specified to the driver using this structure
160a5238e36SThomas Abraham  * which is provided as driver data to the driver.
161a5238e36SThomas Abraham  */
162a5238e36SThomas Abraham struct s3c64xx_spi_port_config {
163a5238e36SThomas Abraham 	int	fifo_lvl_mask[MAX_SPI_PORTS];
164a5238e36SThomas Abraham 	int	rx_lvl_offset;
16582b98fb8STudor Ambarus 	unsigned int fifo_depth;
166d6911cf2STudor Ambarus 	u32	rx_fifomask;
167d6911cf2STudor Ambarus 	u32	tx_fifomask;
168a5238e36SThomas Abraham 	int	tx_st_done;
1697e995556SGirish K S 	int	quirks;
170bfcd27dcSChanho Park 	int	clk_div;
171a5238e36SThomas Abraham 	bool	high_speed;
172a5238e36SThomas Abraham 	bool	clk_from_cmu;
1737990b008SAndi Shyti 	bool	clk_ioclk;
174ffb7bcd3SChanho Park 	bool	has_loopback;
175b7bafb9fSTudor Ambarus 	bool	use_32bit_io;
176a5238e36SThomas Abraham };
177a5238e36SThomas Abraham 
178a5238e36SThomas Abraham /**
179ca632f55SGrant Likely  * struct s3c64xx_spi_driver_data - Runtime info holder for SPI driver.
180ca632f55SGrant Likely  * @clk: Pointer to the spi clock.
181ca632f55SGrant Likely  * @src_clk: Pointer to the clock used to generate SPI signals.
182f4bc49edSYang Yingliang  * @ioclk: Pointer to the i/o clock between host and target
1836b8d1e47SLee Jones  * @pdev: Pointer to device's platform device data
184f4bc49edSYang Yingliang  * @host: Pointer to the SPI Protocol host.
185ca632f55SGrant Likely  * @cntrlr_info: Platform specific data for the controller this driver manages.
186ca632f55SGrant Likely  * @lock: Controller specific lock.
187ca632f55SGrant Likely  * @state: Set of FLAGS to indicate status.
188ca632f55SGrant Likely  * @sfr_start: BUS address of SPI controller regs.
189ca632f55SGrant Likely  * @regs: Pointer to ioremap'ed controller registers.
190ca632f55SGrant Likely  * @xfer_completion: To indicate completion of xfer task.
191ca632f55SGrant Likely  * @cur_mode: Stores the active configuration of the controller.
192ca632f55SGrant Likely  * @cur_bpw: Stores the active bits per word settings.
1936b8d1e47SLee Jones  * @cur_speed: Current clock speed
1946b8d1e47SLee Jones  * @rx_dma: Local receive DMA data (e.g. chan and direction)
1956b8d1e47SLee Jones  * @tx_dma: Local transmit DMA data (e.g. chan and direction)
19697b63f47STudor Ambarus  * @port_conf: Local SPI port configuration data
197ad0adac8STudor Ambarus  * @port_id: [DEPRECATED] use @{rx,tx}_fifomask instead.
198c6e776abSTudor Ambarus  * @fifo_depth: depth of the FIFO.
199d6911cf2STudor Ambarus  * @rx_fifomask: SPI_STATUS.RX_FIFO_LVL mask. Shifted mask defining the field's
200d6911cf2STudor Ambarus  *               length and position.
201d6911cf2STudor Ambarus  * @tx_fifomask: SPI_STATUS.TX_FIFO_LVL mask. Shifted mask defining the field's
202d6911cf2STudor Ambarus  *               length and position.
203ca632f55SGrant Likely  */
204ca632f55SGrant Likely struct s3c64xx_spi_driver_data {
205ca632f55SGrant Likely 	void __iomem                    *regs;
206ca632f55SGrant Likely 	struct clk                      *clk;
207ca632f55SGrant Likely 	struct clk                      *src_clk;
2087990b008SAndi Shyti 	struct clk                      *ioclk;
209ca632f55SGrant Likely 	struct platform_device          *pdev;
210f4bc49edSYang Yingliang 	struct spi_controller           *host;
211ca632f55SGrant Likely 	struct s3c64xx_spi_info         *cntrlr_info;
212ca632f55SGrant Likely 	spinlock_t                      lock;
213ca632f55SGrant Likely 	unsigned long                   sfr_start;
214ca632f55SGrant Likely 	struct completion               xfer_completion;
215ca632f55SGrant Likely 	unsigned                        state;
216ca632f55SGrant Likely 	unsigned                        cur_mode, cur_bpw;
217ca632f55SGrant Likely 	unsigned                        cur_speed;
21882ab8cd7SBoojin Kim 	struct s3c64xx_spi_dma_data	rx_dma;
21982ab8cd7SBoojin Kim 	struct s3c64xx_spi_dma_data	tx_dma;
220d6371415SKrzysztof Kozlowski 	const struct s3c64xx_spi_port_config	*port_conf;
221a5238e36SThomas Abraham 	unsigned int			port_id;
222c6e776abSTudor Ambarus 	unsigned int			fifo_depth;
223d6911cf2STudor Ambarus 	u32				rx_fifomask;
224d6911cf2STudor Ambarus 	u32				tx_fifomask;
225ca632f55SGrant Likely };
226ca632f55SGrant Likely 
s3c64xx_flush_fifo(struct s3c64xx_spi_driver_data * sdd)2273655d30cSSylwester Nawrocki static void s3c64xx_flush_fifo(struct s3c64xx_spi_driver_data *sdd)
228ca632f55SGrant Likely {
229ca632f55SGrant Likely 	void __iomem *regs = sdd->regs;
230ca632f55SGrant Likely 	unsigned long loops;
231ca632f55SGrant Likely 	u32 val;
232ca632f55SGrant Likely 
233ca632f55SGrant Likely 	writel(0, regs + S3C64XX_SPI_PACKET_CNT);
234ca632f55SGrant Likely 
235ca632f55SGrant Likely 	val = readl(regs + S3C64XX_SPI_CH_CFG);
2367d859ff4SKyoungil Kim 	val &= ~(S3C64XX_SPI_CH_RXCH_ON | S3C64XX_SPI_CH_TXCH_ON);
2377d859ff4SKyoungil Kim 	writel(val, regs + S3C64XX_SPI_CH_CFG);
2387d859ff4SKyoungil Kim 
2397d859ff4SKyoungil Kim 	val = readl(regs + S3C64XX_SPI_CH_CFG);
240ca632f55SGrant Likely 	val |= S3C64XX_SPI_CH_SW_RST;
241ca632f55SGrant Likely 	val &= ~S3C64XX_SPI_CH_HS_EN;
242ca632f55SGrant Likely 	writel(val, regs + S3C64XX_SPI_CH_CFG);
243ca632f55SGrant Likely 
244ca632f55SGrant Likely 	/* Flush TxFIFO*/
245ca632f55SGrant Likely 	loops = msecs_to_loops(1);
246ca632f55SGrant Likely 	do {
247ca632f55SGrant Likely 		val = readl(regs + S3C64XX_SPI_STATUS);
248*68a16708SBen Dooks 	} while (TX_FIFO_LVL(val, sdd) && --loops);
249ca632f55SGrant Likely 
250ca632f55SGrant Likely 	if (loops == 0)
251ca632f55SGrant Likely 		dev_warn(&sdd->pdev->dev, "Timed out flushing TX FIFO\n");
252ca632f55SGrant Likely 
253ca632f55SGrant Likely 	/* Flush RxFIFO*/
254ca632f55SGrant Likely 	loops = msecs_to_loops(1);
255ca632f55SGrant Likely 	do {
256ca632f55SGrant Likely 		val = readl(regs + S3C64XX_SPI_STATUS);
257a5238e36SThomas Abraham 		if (RX_FIFO_LVL(val, sdd))
258ca632f55SGrant Likely 			readl(regs + S3C64XX_SPI_RX_DATA);
259ca632f55SGrant Likely 		else
260ca632f55SGrant Likely 			break;
261*68a16708SBen Dooks 	} while (--loops);
262ca632f55SGrant Likely 
263ca632f55SGrant Likely 	if (loops == 0)
264ca632f55SGrant Likely 		dev_warn(&sdd->pdev->dev, "Timed out flushing RX FIFO\n");
265ca632f55SGrant Likely 
266ca632f55SGrant Likely 	val = readl(regs + S3C64XX_SPI_CH_CFG);
267ca632f55SGrant Likely 	val &= ~S3C64XX_SPI_CH_SW_RST;
268ca632f55SGrant Likely 	writel(val, regs + S3C64XX_SPI_CH_CFG);
269ca632f55SGrant Likely 
270ca632f55SGrant Likely 	val = readl(regs + S3C64XX_SPI_MODE_CFG);
271ca632f55SGrant Likely 	val &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
272ca632f55SGrant Likely 	writel(val, regs + S3C64XX_SPI_MODE_CFG);
273ca632f55SGrant Likely }
274ca632f55SGrant Likely 
s3c64xx_spi_dmacb(void * data)27582ab8cd7SBoojin Kim static void s3c64xx_spi_dmacb(void *data)
27639d3e807SBoojin Kim {
27782ab8cd7SBoojin Kim 	struct s3c64xx_spi_driver_data *sdd;
27882ab8cd7SBoojin Kim 	struct s3c64xx_spi_dma_data *dma = data;
27939d3e807SBoojin Kim 	unsigned long flags;
28039d3e807SBoojin Kim 
281054ebcc4SKyoungil Kim 	if (dma->direction == DMA_DEV_TO_MEM)
28282ab8cd7SBoojin Kim 		sdd = container_of(data,
28382ab8cd7SBoojin Kim 			struct s3c64xx_spi_driver_data, rx_dma);
28482ab8cd7SBoojin Kim 	else
28582ab8cd7SBoojin Kim 		sdd = container_of(data,
28682ab8cd7SBoojin Kim 			struct s3c64xx_spi_driver_data, tx_dma);
28782ab8cd7SBoojin Kim 
28839d3e807SBoojin Kim 	spin_lock_irqsave(&sdd->lock, flags);
28939d3e807SBoojin Kim 
290054ebcc4SKyoungil Kim 	if (dma->direction == DMA_DEV_TO_MEM) {
29139d3e807SBoojin Kim 		sdd->state &= ~RXBUSY;
29239d3e807SBoojin Kim 		if (!(sdd->state & TXBUSY))
29339d3e807SBoojin Kim 			complete(&sdd->xfer_completion);
29482ab8cd7SBoojin Kim 	} else {
29582ab8cd7SBoojin Kim 		sdd->state &= ~TXBUSY;
29682ab8cd7SBoojin Kim 		if (!(sdd->state & RXBUSY))
29782ab8cd7SBoojin Kim 			complete(&sdd->xfer_completion);
29882ab8cd7SBoojin Kim 	}
29939d3e807SBoojin Kim 
30039d3e807SBoojin Kim 	spin_unlock_irqrestore(&sdd->lock, flags);
30139d3e807SBoojin Kim }
30239d3e807SBoojin Kim 
s3c64xx_prepare_dma(struct s3c64xx_spi_dma_data * dma,struct sg_table * sgt)3034c645205STudor Ambarus static int s3c64xx_prepare_dma(struct s3c64xx_spi_dma_data *dma,
3046ad45a27SMark Brown 			       struct sg_table *sgt)
30578843727SArnd Bergmann {
30678843727SArnd Bergmann 	struct s3c64xx_spi_driver_data *sdd;
30778843727SArnd Bergmann 	struct dma_slave_config config;
30878843727SArnd Bergmann 	struct dma_async_tx_descriptor *desc;
3092f4db6f7SŁukasz Stelmach 	int ret;
31078843727SArnd Bergmann 
311b1a8e78dSTomasz Figa 	memset(&config, 0, sizeof(config));
312b1a8e78dSTomasz Figa 
31378843727SArnd Bergmann 	if (dma->direction == DMA_DEV_TO_MEM) {
31478843727SArnd Bergmann 		sdd = container_of((void *)dma,
31578843727SArnd Bergmann 			struct s3c64xx_spi_driver_data, rx_dma);
31678843727SArnd Bergmann 		config.src_addr = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
31778843727SArnd Bergmann 		config.src_addr_width = sdd->cur_bpw / 8;
31878843727SArnd Bergmann 		config.src_maxburst = 1;
31978843727SArnd Bergmann 	} else {
32078843727SArnd Bergmann 		sdd = container_of((void *)dma,
32178843727SArnd Bergmann 			struct s3c64xx_spi_driver_data, tx_dma);
32278843727SArnd Bergmann 		config.dst_addr = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
32378843727SArnd Bergmann 		config.dst_addr_width = sdd->cur_bpw / 8;
32478843727SArnd Bergmann 		config.dst_maxburst = 1;
32578843727SArnd Bergmann 	}
3265d7f4f43STudor Ambarus 	config.direction = dma->direction;
327e9c49effSTudor Ambarus 	ret = dmaengine_slave_config(dma->ch, &config);
328e9c49effSTudor Ambarus 	if (ret)
329e9c49effSTudor Ambarus 		return ret;
33078843727SArnd Bergmann 
3316ad45a27SMark Brown 	desc = dmaengine_prep_slave_sg(dma->ch, sgt->sgl, sgt->nents,
33290438c4bSTomasz Figa 				       dma->direction, DMA_PREP_INTERRUPT);
3332f4db6f7SŁukasz Stelmach 	if (!desc) {
3342f4db6f7SŁukasz Stelmach 		dev_err(&sdd->pdev->dev, "unable to prepare %s scatterlist",
3352f4db6f7SŁukasz Stelmach 			dma->direction == DMA_DEV_TO_MEM ? "rx" : "tx");
3362f4db6f7SŁukasz Stelmach 		return -ENOMEM;
3372f4db6f7SŁukasz Stelmach 	}
33878843727SArnd Bergmann 
33978843727SArnd Bergmann 	desc->callback = s3c64xx_spi_dmacb;
34078843727SArnd Bergmann 	desc->callback_param = dma;
34178843727SArnd Bergmann 
3422f4db6f7SŁukasz Stelmach 	dma->cookie = dmaengine_submit(desc);
3432f4db6f7SŁukasz Stelmach 	ret = dma_submit_error(dma->cookie);
3442f4db6f7SŁukasz Stelmach 	if (ret) {
3452f4db6f7SŁukasz Stelmach 		dev_err(&sdd->pdev->dev, "DMA submission failed");
34660dc8d34STudor Ambarus 		return ret;
3472f4db6f7SŁukasz Stelmach 	}
3482f4db6f7SŁukasz Stelmach 
34978843727SArnd Bergmann 	dma_async_issue_pending(dma->ch);
3502f4db6f7SŁukasz Stelmach 	return 0;
35178843727SArnd Bergmann }
35278843727SArnd Bergmann 
s3c64xx_spi_set_cs(struct spi_device * spi,bool enable)353aa4964c4SAndi Shyti static void s3c64xx_spi_set_cs(struct spi_device *spi, bool enable)
354aa4964c4SAndi Shyti {
355aa4964c4SAndi Shyti 	struct s3c64xx_spi_driver_data *sdd =
356f4bc49edSYang Yingliang 					spi_controller_get_devdata(spi->controller);
357aa4964c4SAndi Shyti 
358a92e7c3dSAndi Shyti 	if (sdd->cntrlr_info->no_cs)
359a92e7c3dSAndi Shyti 		return;
360a92e7c3dSAndi Shyti 
361aa4964c4SAndi Shyti 	if (enable) {
362aa4964c4SAndi Shyti 		if (!(sdd->port_conf->quirks & S3C64XX_SPI_QUIRK_CS_AUTO)) {
363913ba5c9SŁukasz Stelmach 			writel(0, sdd->regs + S3C64XX_SPI_CS_REG);
364aa4964c4SAndi Shyti 		} else {
365913ba5c9SŁukasz Stelmach 			u32 ssel = readl(sdd->regs + S3C64XX_SPI_CS_REG);
366aa4964c4SAndi Shyti 
367913ba5c9SŁukasz Stelmach 			ssel |= (S3C64XX_SPI_CS_AUTO |
368913ba5c9SŁukasz Stelmach 						S3C64XX_SPI_CS_NSC_CNT_2);
369913ba5c9SŁukasz Stelmach 			writel(ssel, sdd->regs + S3C64XX_SPI_CS_REG);
370aa4964c4SAndi Shyti 		}
371aa4964c4SAndi Shyti 	} else {
372aa4964c4SAndi Shyti 		if (!(sdd->port_conf->quirks & S3C64XX_SPI_QUIRK_CS_AUTO))
373913ba5c9SŁukasz Stelmach 			writel(S3C64XX_SPI_CS_SIG_INACT,
374913ba5c9SŁukasz Stelmach 			       sdd->regs + S3C64XX_SPI_CS_REG);
375aa4964c4SAndi Shyti 	}
376aa4964c4SAndi Shyti }
377aa4964c4SAndi Shyti 
s3c64xx_spi_prepare_transfer(struct spi_controller * spi)378f4bc49edSYang Yingliang static int s3c64xx_spi_prepare_transfer(struct spi_controller *spi)
37978843727SArnd Bergmann {
380f4bc49edSYang Yingliang 	struct s3c64xx_spi_driver_data *sdd = spi_controller_get_devdata(spi);
38178843727SArnd Bergmann 
382730d9d4dSAndi Shyti 	if (is_polling(sdd))
383730d9d4dSAndi Shyti 		return 0;
384730d9d4dSAndi Shyti 
385f52b03c7SAdithya K V 	/* Requests DMA channels */
386f52b03c7SAdithya K V 	sdd->rx_dma.ch = dma_request_chan(&sdd->pdev->dev, "rx");
387f52b03c7SAdithya K V 	if (IS_ERR(sdd->rx_dma.ch)) {
388f52b03c7SAdithya K V 		dev_err(&sdd->pdev->dev, "Failed to get RX DMA channel\n");
389845d3fd8SColin Ian King 		sdd->rx_dma.ch = NULL;
390f52b03c7SAdithya K V 		return 0;
391f52b03c7SAdithya K V 	}
392f52b03c7SAdithya K V 
393f52b03c7SAdithya K V 	sdd->tx_dma.ch = dma_request_chan(&sdd->pdev->dev, "tx");
394f52b03c7SAdithya K V 	if (IS_ERR(sdd->tx_dma.ch)) {
3959723070eSColin Ian King 		dev_err(&sdd->pdev->dev, "Failed to get TX DMA channel\n");
396f52b03c7SAdithya K V 		dma_release_channel(sdd->rx_dma.ch);
397845d3fd8SColin Ian King 		sdd->tx_dma.ch = NULL;
398845d3fd8SColin Ian King 		sdd->rx_dma.ch = NULL;
399f52b03c7SAdithya K V 		return 0;
400f52b03c7SAdithya K V 	}
401f52b03c7SAdithya K V 
4023f295887SMark Brown 	spi->dma_rx = sdd->rx_dma.ch;
4033f295887SMark Brown 	spi->dma_tx = sdd->tx_dma.ch;
404fb9d044eSMark Brown 
40578843727SArnd Bergmann 	return 0;
40678843727SArnd Bergmann }
40778843727SArnd Bergmann 
s3c64xx_spi_unprepare_transfer(struct spi_controller * spi)408f4bc49edSYang Yingliang static int s3c64xx_spi_unprepare_transfer(struct spi_controller *spi)
40982295bc0SChanho Park {
410f4bc49edSYang Yingliang 	struct s3c64xx_spi_driver_data *sdd = spi_controller_get_devdata(spi);
41182295bc0SChanho Park 
41282295bc0SChanho Park 	if (is_polling(sdd))
41382295bc0SChanho Park 		return 0;
41482295bc0SChanho Park 
41582295bc0SChanho Park 	/* Releases DMA channels if they are allocated */
41682295bc0SChanho Park 	if (sdd->rx_dma.ch && sdd->tx_dma.ch) {
41782295bc0SChanho Park 		dma_release_channel(sdd->rx_dma.ch);
41882295bc0SChanho Park 		dma_release_channel(sdd->tx_dma.ch);
419dad57a51SChanho Park 		sdd->rx_dma.ch = NULL;
420dad57a51SChanho Park 		sdd->tx_dma.ch = NULL;
42182295bc0SChanho Park 	}
42282295bc0SChanho Park 
42382295bc0SChanho Park 	return 0;
42482295bc0SChanho Park }
42582295bc0SChanho Park 
s3c64xx_spi_can_dma(struct spi_controller * host,struct spi_device * spi,struct spi_transfer * xfer)426f4bc49edSYang Yingliang static bool s3c64xx_spi_can_dma(struct spi_controller *host,
4273f295887SMark Brown 				struct spi_device *spi,
4283f295887SMark Brown 				struct spi_transfer *xfer)
4293f295887SMark Brown {
430f4bc49edSYang Yingliang 	struct s3c64xx_spi_driver_data *sdd = spi_controller_get_devdata(host);
4313f295887SMark Brown 
4329d47e411STudor Ambarus 	if (sdd->rx_dma.ch && sdd->tx_dma.ch)
433a3d3eab6SJaewon Kim 		return xfer->len >= sdd->fifo_depth;
434f52b03c7SAdithya K V 
4359d47e411STudor Ambarus 	return false;
4363f295887SMark Brown }
4373f295887SMark Brown 
s3c64xx_iowrite8_32_rep(volatile void __iomem * addr,const void * buffer,unsigned int count)438b7bafb9fSTudor Ambarus static void s3c64xx_iowrite8_32_rep(volatile void __iomem *addr,
439b7bafb9fSTudor Ambarus 				    const void *buffer, unsigned int count)
440b7bafb9fSTudor Ambarus {
441b7bafb9fSTudor Ambarus 	if (count) {
442b7bafb9fSTudor Ambarus 		const u8 *buf = buffer;
443b7bafb9fSTudor Ambarus 
444b7bafb9fSTudor Ambarus 		do {
445b7bafb9fSTudor Ambarus 			__raw_writel(*buf++, addr);
446b7bafb9fSTudor Ambarus 		} while (--count);
447b7bafb9fSTudor Ambarus 	}
448b7bafb9fSTudor Ambarus }
449b7bafb9fSTudor Ambarus 
s3c64xx_iowrite16_32_rep(volatile void __iomem * addr,const void * buffer,unsigned int count)450b7bafb9fSTudor Ambarus static void s3c64xx_iowrite16_32_rep(volatile void __iomem *addr,
451b7bafb9fSTudor Ambarus 				     const void *buffer, unsigned int count)
452b7bafb9fSTudor Ambarus {
453b7bafb9fSTudor Ambarus 	if (count) {
454b7bafb9fSTudor Ambarus 		const u16 *buf = buffer;
455b7bafb9fSTudor Ambarus 
456b7bafb9fSTudor Ambarus 		do {
457b7bafb9fSTudor Ambarus 			__raw_writel(*buf++, addr);
458b7bafb9fSTudor Ambarus 		} while (--count);
459b7bafb9fSTudor Ambarus 	}
460b7bafb9fSTudor Ambarus }
461b7bafb9fSTudor Ambarus 
s3c64xx_iowrite_rep(const struct s3c64xx_spi_driver_data * sdd,struct spi_transfer * xfer)46280d3204aSTudor Ambarus static void s3c64xx_iowrite_rep(const struct s3c64xx_spi_driver_data *sdd,
46380d3204aSTudor Ambarus 				struct spi_transfer *xfer)
46480d3204aSTudor Ambarus {
46580d3204aSTudor Ambarus 	void __iomem *addr = sdd->regs + S3C64XX_SPI_TX_DATA;
46680d3204aSTudor Ambarus 	const void *buf = xfer->tx_buf;
46780d3204aSTudor Ambarus 	unsigned int len = xfer->len;
46880d3204aSTudor Ambarus 
46980d3204aSTudor Ambarus 	switch (sdd->cur_bpw) {
47080d3204aSTudor Ambarus 	case 32:
47180d3204aSTudor Ambarus 		iowrite32_rep(addr, buf, len / 4);
47280d3204aSTudor Ambarus 		break;
47380d3204aSTudor Ambarus 	case 16:
474b7bafb9fSTudor Ambarus 		if (sdd->port_conf->use_32bit_io)
475b7bafb9fSTudor Ambarus 			s3c64xx_iowrite16_32_rep(addr, buf, len / 2);
476b7bafb9fSTudor Ambarus 		else
47780d3204aSTudor Ambarus 			iowrite16_rep(addr, buf, len / 2);
47880d3204aSTudor Ambarus 		break;
47980d3204aSTudor Ambarus 	default:
480b7bafb9fSTudor Ambarus 		if (sdd->port_conf->use_32bit_io)
481b7bafb9fSTudor Ambarus 			s3c64xx_iowrite8_32_rep(addr, buf, len);
482b7bafb9fSTudor Ambarus 		else
48380d3204aSTudor Ambarus 			iowrite8_rep(addr, buf, len);
48480d3204aSTudor Ambarus 		break;
48580d3204aSTudor Ambarus 	}
48680d3204aSTudor Ambarus }
48780d3204aSTudor Ambarus 
s3c64xx_enable_datapath(struct s3c64xx_spi_driver_data * sdd,struct spi_transfer * xfer,int dma_mode)4882f4db6f7SŁukasz Stelmach static int s3c64xx_enable_datapath(struct s3c64xx_spi_driver_data *sdd,
489ca632f55SGrant Likely 				    struct spi_transfer *xfer, int dma_mode)
490ca632f55SGrant Likely {
491ca632f55SGrant Likely 	void __iomem *regs = sdd->regs;
492ca632f55SGrant Likely 	u32 modecfg, chcfg;
4932f4db6f7SŁukasz Stelmach 	int ret = 0;
494ca632f55SGrant Likely 
495ca632f55SGrant Likely 	modecfg = readl(regs + S3C64XX_SPI_MODE_CFG);
496ca632f55SGrant Likely 	modecfg &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
497ca632f55SGrant Likely 
498ca632f55SGrant Likely 	chcfg = readl(regs + S3C64XX_SPI_CH_CFG);
499ca632f55SGrant Likely 	chcfg &= ~S3C64XX_SPI_CH_TXCH_ON;
500ca632f55SGrant Likely 
501ca632f55SGrant Likely 	if (dma_mode) {
502ca632f55SGrant Likely 		chcfg &= ~S3C64XX_SPI_CH_RXCH_ON;
503ca632f55SGrant Likely 	} else {
504ca632f55SGrant Likely 		/* Always shift in data in FIFO, even if xfer is Tx only,
505ca632f55SGrant Likely 		 * this helps setting PCKT_CNT value for generating clocks
506ca632f55SGrant Likely 		 * as exactly needed.
507ca632f55SGrant Likely 		 */
508ca632f55SGrant Likely 		chcfg |= S3C64XX_SPI_CH_RXCH_ON;
509ca632f55SGrant Likely 		writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
510ca632f55SGrant Likely 					| S3C64XX_SPI_PACKET_CNT_EN,
511ca632f55SGrant Likely 					regs + S3C64XX_SPI_PACKET_CNT);
512ca632f55SGrant Likely 	}
513ca632f55SGrant Likely 
514ca632f55SGrant Likely 	if (xfer->tx_buf != NULL) {
515ca632f55SGrant Likely 		sdd->state |= TXBUSY;
516ca632f55SGrant Likely 		chcfg |= S3C64XX_SPI_CH_TXCH_ON;
517ca632f55SGrant Likely 		if (dma_mode) {
518ca632f55SGrant Likely 			modecfg |= S3C64XX_SPI_MODE_TXDMA_ON;
5194c645205STudor Ambarus 			ret = s3c64xx_prepare_dma(&sdd->tx_dma, &xfer->tx_sg);
520ca632f55SGrant Likely 		} else {
52180d3204aSTudor Ambarus 			s3c64xx_iowrite_rep(sdd, xfer);
522ca632f55SGrant Likely 		}
523ca632f55SGrant Likely 	}
524ca632f55SGrant Likely 
525ca632f55SGrant Likely 	if (xfer->rx_buf != NULL) {
526ca632f55SGrant Likely 		sdd->state |= RXBUSY;
527ca632f55SGrant Likely 
528a5238e36SThomas Abraham 		if (sdd->port_conf->high_speed && sdd->cur_speed >= 30000000UL
529ca632f55SGrant Likely 					&& !(sdd->cur_mode & SPI_CPHA))
530ca632f55SGrant Likely 			chcfg |= S3C64XX_SPI_CH_HS_EN;
531ca632f55SGrant Likely 
532ca632f55SGrant Likely 		if (dma_mode) {
533ca632f55SGrant Likely 			modecfg |= S3C64XX_SPI_MODE_RXDMA_ON;
534ca632f55SGrant Likely 			chcfg |= S3C64XX_SPI_CH_RXCH_ON;
535ca632f55SGrant Likely 			writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
536ca632f55SGrant Likely 					| S3C64XX_SPI_PACKET_CNT_EN,
537ca632f55SGrant Likely 					regs + S3C64XX_SPI_PACKET_CNT);
5384c645205STudor Ambarus 			ret = s3c64xx_prepare_dma(&sdd->rx_dma, &xfer->rx_sg);
539ca632f55SGrant Likely 		}
540ca632f55SGrant Likely 	}
541ca632f55SGrant Likely 
5422f4db6f7SŁukasz Stelmach 	if (ret)
5432f4db6f7SŁukasz Stelmach 		return ret;
5442f4db6f7SŁukasz Stelmach 
545ca632f55SGrant Likely 	writel(modecfg, regs + S3C64XX_SPI_MODE_CFG);
546ca632f55SGrant Likely 	writel(chcfg, regs + S3C64XX_SPI_CH_CFG);
5472f4db6f7SŁukasz Stelmach 
5482f4db6f7SŁukasz Stelmach 	return 0;
549ca632f55SGrant Likely }
550ca632f55SGrant Likely 
s3c64xx_spi_wait_for_timeout(struct s3c64xx_spi_driver_data * sdd,int timeout_ms)55179617073SMark Brown static u32 s3c64xx_spi_wait_for_timeout(struct s3c64xx_spi_driver_data *sdd,
5527e995556SGirish K S 					int timeout_ms)
5537e995556SGirish K S {
5547e995556SGirish K S 	void __iomem *regs = sdd->regs;
5557e995556SGirish K S 	unsigned long val = 1;
5567e995556SGirish K S 	u32 status;
557c6e776abSTudor Ambarus 	u32 max_fifo = sdd->fifo_depth;
5587e995556SGirish K S 
5597e995556SGirish K S 	if (timeout_ms)
5607e995556SGirish K S 		val = msecs_to_loops(timeout_ms);
5617e995556SGirish K S 
5627e995556SGirish K S 	do {
5637e995556SGirish K S 		status = readl(regs + S3C64XX_SPI_STATUS);
5647e995556SGirish K S 	} while (RX_FIFO_LVL(status, sdd) < max_fifo && --val);
5657e995556SGirish K S 
5667e995556SGirish K S 	/* return the actual received data length */
5677e995556SGirish K S 	return RX_FIFO_LVL(status, sdd);
568ca632f55SGrant Likely }
569ca632f55SGrant Likely 
s3c64xx_wait_for_dma(struct s3c64xx_spi_driver_data * sdd,struct spi_transfer * xfer)5703655d30cSSylwester Nawrocki static int s3c64xx_wait_for_dma(struct s3c64xx_spi_driver_data *sdd,
5713700c6ebSMark Brown 				struct spi_transfer *xfer)
572ca632f55SGrant Likely {
573ca632f55SGrant Likely 	void __iomem *regs = sdd->regs;
574ca632f55SGrant Likely 	unsigned long val;
5753700c6ebSMark Brown 	u32 status;
576ca632f55SGrant Likely 	int ms;
577ca632f55SGrant Likely 
578ca632f55SGrant Likely 	/* millisecs to xfer 'len' bytes @ 'cur_speed' */
579ca632f55SGrant Likely 	ms = xfer->len * 8 * 1000 / sdd->cur_speed;
5809fe26adbSŁukasz Stelmach 	ms += 30;               /* some tolerance */
5819fe26adbSŁukasz Stelmach 	ms = max(ms, 100);      /* minimum timeout */
582ca632f55SGrant Likely 
583ca632f55SGrant Likely 	val = msecs_to_jiffies(ms) + 10;
584ca632f55SGrant Likely 	val = wait_for_completion_timeout(&sdd->xfer_completion, val);
585ca632f55SGrant Likely 
586ca632f55SGrant Likely 	/*
5877e995556SGirish K S 	 * If the previous xfer was completed within timeout, then
5881a234accSTudor Ambarus 	 * proceed further else return -ETIMEDOUT.
589ca632f55SGrant Likely 	 * DmaTx returns after simply writing data in the FIFO,
590ca632f55SGrant Likely 	 * w/o waiting for real transmission on the bus to finish.
591ca632f55SGrant Likely 	 * DmaRx returns only after Dma read data from FIFO which
592ca632f55SGrant Likely 	 * needs bus transmission to finish, so we don't worry if
593ca632f55SGrant Likely 	 * Xfer involved Rx(with or without Tx).
594ca632f55SGrant Likely 	 */
5957e995556SGirish K S 	if (val && !xfer->rx_buf) {
596ca632f55SGrant Likely 		val = msecs_to_loops(10);
597ca632f55SGrant Likely 		status = readl(regs + S3C64XX_SPI_STATUS);
598a5238e36SThomas Abraham 		while ((TX_FIFO_LVL(status, sdd)
599a5238e36SThomas Abraham 			|| !S3C64XX_SPI_ST_TX_DONE(status, sdd))
600ca632f55SGrant Likely 		       && --val) {
601ca632f55SGrant Likely 			cpu_relax();
602ca632f55SGrant Likely 			status = readl(regs + S3C64XX_SPI_STATUS);
603ca632f55SGrant Likely 		}
604ca632f55SGrant Likely 
6057e995556SGirish K S 	}
6067e995556SGirish K S 
6077e995556SGirish K S 	/* If timed out while checking rx/tx status return error */
608ca632f55SGrant Likely 	if (!val)
6091a234accSTudor Ambarus 		return -ETIMEDOUT;
6103700c6ebSMark Brown 
6113700c6ebSMark Brown 	return 0;
6123700c6ebSMark Brown }
6133700c6ebSMark Brown 
s3c64xx_wait_for_pio(struct s3c64xx_spi_driver_data * sdd,struct spi_transfer * xfer,bool use_irq)6143655d30cSSylwester Nawrocki static int s3c64xx_wait_for_pio(struct s3c64xx_spi_driver_data *sdd,
6151ee80671SJaewon Kim 				struct spi_transfer *xfer, bool use_irq)
6163700c6ebSMark Brown {
6173700c6ebSMark Brown 	void __iomem *regs = sdd->regs;
6183700c6ebSMark Brown 	unsigned long val;
6193700c6ebSMark Brown 	u32 status;
6207e995556SGirish K S 	int loops;
6217e995556SGirish K S 	u32 cpy_len;
6227e995556SGirish K S 	u8 *buf;
6233700c6ebSMark Brown 	int ms;
6243456674fSJaewon Kim 	unsigned long time_us;
6253700c6ebSMark Brown 
6263456674fSJaewon Kim 	/* microsecs to xfer 'len' bytes @ 'cur_speed' */
6273456674fSJaewon Kim 	time_us = (xfer->len * 8 * 1000 * 1000) / sdd->cur_speed;
6283456674fSJaewon Kim 	ms = (time_us / 1000);
6293700c6ebSMark Brown 	ms += 10; /* some tolerance */
6303700c6ebSMark Brown 
6313456674fSJaewon Kim 	/* sleep during signal transfer time */
6323456674fSJaewon Kim 	status = readl(regs + S3C64XX_SPI_STATUS);
6333456674fSJaewon Kim 	if (RX_FIFO_LVL(status, sdd) < xfer->len)
6343456674fSJaewon Kim 		usleep_range(time_us / 2, time_us);
6353456674fSJaewon Kim 
6361ee80671SJaewon Kim 	if (use_irq) {
6371ee80671SJaewon Kim 		val = msecs_to_jiffies(ms);
6381ee80671SJaewon Kim 		if (!wait_for_completion_timeout(&sdd->xfer_completion, val))
6391a234accSTudor Ambarus 			return -ETIMEDOUT;
6401ee80671SJaewon Kim 	}
6411ee80671SJaewon Kim 
6423700c6ebSMark Brown 	val = msecs_to_loops(ms);
6433700c6ebSMark Brown 	do {
6443700c6ebSMark Brown 		status = readl(regs + S3C64XX_SPI_STATUS);
6453700c6ebSMark Brown 	} while (RX_FIFO_LVL(status, sdd) < xfer->len && --val);
6463700c6ebSMark Brown 
6474e0b82eeSSylwester Nawrocki 	if (!val)
6484e0b82eeSSylwester Nawrocki 		return -EIO;
6497e995556SGirish K S 
650ca632f55SGrant Likely 	/* If it was only Tx */
6517e995556SGirish K S 	if (!xfer->rx_buf) {
652ca632f55SGrant Likely 		sdd->state &= ~TXBUSY;
653ca632f55SGrant Likely 		return 0;
654ca632f55SGrant Likely 	}
655ca632f55SGrant Likely 
6567e995556SGirish K S 	/*
6577e995556SGirish K S 	 * If the receive length is bigger than the controller fifo
6587e995556SGirish K S 	 * size, calculate the loops and read the fifo as many times.
6597e995556SGirish K S 	 * loops = length / max fifo size (calculated by using the
6607e995556SGirish K S 	 * fifo mask).
6617e995556SGirish K S 	 * For any size less than the fifo size the below code is
6627e995556SGirish K S 	 * executed atleast once.
6637e995556SGirish K S 	 */
664c6e776abSTudor Ambarus 	loops = xfer->len / sdd->fifo_depth;
6657e995556SGirish K S 	buf = xfer->rx_buf;
6667e995556SGirish K S 	do {
6677e995556SGirish K S 		/* wait for data to be received in the fifo */
66879617073SMark Brown 		cpy_len = s3c64xx_spi_wait_for_timeout(sdd,
66979617073SMark Brown 						       (loops ? ms : 0));
6707e995556SGirish K S 
671ca632f55SGrant Likely 		switch (sdd->cur_bpw) {
672ca632f55SGrant Likely 		case 32:
673ca632f55SGrant Likely 			ioread32_rep(regs + S3C64XX_SPI_RX_DATA,
6747e995556SGirish K S 				     buf, cpy_len / 4);
675ca632f55SGrant Likely 			break;
676ca632f55SGrant Likely 		case 16:
677ca632f55SGrant Likely 			ioread16_rep(regs + S3C64XX_SPI_RX_DATA,
6787e995556SGirish K S 				     buf, cpy_len / 2);
679ca632f55SGrant Likely 			break;
680ca632f55SGrant Likely 		default:
681ca632f55SGrant Likely 			ioread8_rep(regs + S3C64XX_SPI_RX_DATA,
6827e995556SGirish K S 				    buf, cpy_len);
683ca632f55SGrant Likely 			break;
684ca632f55SGrant Likely 		}
6857e995556SGirish K S 
6867e995556SGirish K S 		buf = buf + cpy_len;
6877e995556SGirish K S 	} while (loops--);
688ca632f55SGrant Likely 	sdd->state &= ~RXBUSY;
689ca632f55SGrant Likely 
690ca632f55SGrant Likely 	return 0;
691ca632f55SGrant Likely }
692ca632f55SGrant Likely 
s3c64xx_spi_config(struct s3c64xx_spi_driver_data * sdd)6932f4db6f7SŁukasz Stelmach static int s3c64xx_spi_config(struct s3c64xx_spi_driver_data *sdd)
694ca632f55SGrant Likely {
695ca632f55SGrant Likely 	void __iomem *regs = sdd->regs;
6962f4db6f7SŁukasz Stelmach 	int ret;
697ca632f55SGrant Likely 	u32 val;
698bfcd27dcSChanho Park 	int div = sdd->port_conf->clk_div;
699ca632f55SGrant Likely 
700ca632f55SGrant Likely 	/* Disable Clock */
701d9aaf1dcSAndi Shyti 	if (!sdd->port_conf->clk_from_cmu) {
702ca632f55SGrant Likely 		val = readl(regs + S3C64XX_SPI_CLK_CFG);
703ca632f55SGrant Likely 		val &= ~S3C64XX_SPI_ENCLK_ENABLE;
704ca632f55SGrant Likely 		writel(val, regs + S3C64XX_SPI_CLK_CFG);
705ca632f55SGrant Likely 	}
706ca632f55SGrant Likely 
707ca632f55SGrant Likely 	/* Set Polarity and Phase */
708ca632f55SGrant Likely 	val = readl(regs + S3C64XX_SPI_CH_CFG);
709ca632f55SGrant Likely 	val &= ~(S3C64XX_SPI_CH_SLAVE |
710ca632f55SGrant Likely 			S3C64XX_SPI_CPOL_L |
711ca632f55SGrant Likely 			S3C64XX_SPI_CPHA_B);
712ca632f55SGrant Likely 
713ca632f55SGrant Likely 	if (sdd->cur_mode & SPI_CPOL)
714ca632f55SGrant Likely 		val |= S3C64XX_SPI_CPOL_L;
715ca632f55SGrant Likely 
716ca632f55SGrant Likely 	if (sdd->cur_mode & SPI_CPHA)
717ca632f55SGrant Likely 		val |= S3C64XX_SPI_CPHA_B;
718ca632f55SGrant Likely 
719ca632f55SGrant Likely 	writel(val, regs + S3C64XX_SPI_CH_CFG);
720ca632f55SGrant Likely 
721ca632f55SGrant Likely 	/* Set Channel & DMA Mode */
722ca632f55SGrant Likely 	val = readl(regs + S3C64XX_SPI_MODE_CFG);
723ca632f55SGrant Likely 	val &= ~(S3C64XX_SPI_MODE_BUS_TSZ_MASK
724ca632f55SGrant Likely 			| S3C64XX_SPI_MODE_CH_TSZ_MASK);
725ca632f55SGrant Likely 
726ca632f55SGrant Likely 	switch (sdd->cur_bpw) {
727ca632f55SGrant Likely 	case 32:
728ca632f55SGrant Likely 		val |= S3C64XX_SPI_MODE_BUS_TSZ_WORD;
729ca632f55SGrant Likely 		val |= S3C64XX_SPI_MODE_CH_TSZ_WORD;
730ca632f55SGrant Likely 		break;
731ca632f55SGrant Likely 	case 16:
732ca632f55SGrant Likely 		val |= S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD;
733ca632f55SGrant Likely 		val |= S3C64XX_SPI_MODE_CH_TSZ_HALFWORD;
734ca632f55SGrant Likely 		break;
735ca632f55SGrant Likely 	default:
736ca632f55SGrant Likely 		val |= S3C64XX_SPI_MODE_BUS_TSZ_BYTE;
737ca632f55SGrant Likely 		val |= S3C64XX_SPI_MODE_CH_TSZ_BYTE;
738ca632f55SGrant Likely 		break;
739ca632f55SGrant Likely 	}
740ca632f55SGrant Likely 
741ffb7bcd3SChanho Park 	if ((sdd->cur_mode & SPI_LOOP) && sdd->port_conf->has_loopback)
742ffb7bcd3SChanho Park 		val |= S3C64XX_SPI_MODE_SELF_LOOPBACK;
7439ec3c551SJaewon Kim 	else
7449ec3c551SJaewon Kim 		val &= ~S3C64XX_SPI_MODE_SELF_LOOPBACK;
745ffb7bcd3SChanho Park 
746ca632f55SGrant Likely 	writel(val, regs + S3C64XX_SPI_MODE_CFG);
747ca632f55SGrant Likely 
748a5238e36SThomas Abraham 	if (sdd->port_conf->clk_from_cmu) {
749bfcd27dcSChanho Park 		ret = clk_set_rate(sdd->src_clk, sdd->cur_speed * div);
7502f4db6f7SŁukasz Stelmach 		if (ret)
7512f4db6f7SŁukasz Stelmach 			return ret;
752bfcd27dcSChanho Park 		sdd->cur_speed = clk_get_rate(sdd->src_clk) / div;
753ca632f55SGrant Likely 	} else {
754ca632f55SGrant Likely 		/* Configure Clock */
755ca632f55SGrant Likely 		val = readl(regs + S3C64XX_SPI_CLK_CFG);
756ca632f55SGrant Likely 		val &= ~S3C64XX_SPI_PSR_MASK;
757bfcd27dcSChanho Park 		val |= ((clk_get_rate(sdd->src_clk) / sdd->cur_speed / div - 1)
758ca632f55SGrant Likely 				& S3C64XX_SPI_PSR_MASK);
759ca632f55SGrant Likely 		writel(val, regs + S3C64XX_SPI_CLK_CFG);
760ca632f55SGrant Likely 
761ca632f55SGrant Likely 		/* Enable Clock */
762ca632f55SGrant Likely 		val = readl(regs + S3C64XX_SPI_CLK_CFG);
763ca632f55SGrant Likely 		val |= S3C64XX_SPI_ENCLK_ENABLE;
764ca632f55SGrant Likely 		writel(val, regs + S3C64XX_SPI_CLK_CFG);
765ca632f55SGrant Likely 	}
7662f4db6f7SŁukasz Stelmach 
7672f4db6f7SŁukasz Stelmach 	return 0;
768ca632f55SGrant Likely }
769ca632f55SGrant Likely 
770ca632f55SGrant Likely #define XFER_DMAADDR_INVALID DMA_BIT_MASK(32)
771ca632f55SGrant Likely 
s3c64xx_spi_prepare_message(struct spi_controller * host,struct spi_message * msg)772f4bc49edSYang Yingliang static int s3c64xx_spi_prepare_message(struct spi_controller *host,
773ca632f55SGrant Likely 				       struct spi_message *msg)
774ca632f55SGrant Likely {
775f4bc49edSYang Yingliang 	struct s3c64xx_spi_driver_data *sdd = spi_controller_get_devdata(host);
776ca632f55SGrant Likely 	struct spi_device *spi = msg->spi;
777ca632f55SGrant Likely 	struct s3c64xx_spi_csinfo *cs = spi->controller_data;
778ca632f55SGrant Likely 
779ca632f55SGrant Likely 	/* Configure feedback delay */
780a45cf3ccSLinus Walleij 	if (!cs)
781a45cf3ccSLinus Walleij 		/* No delay if not defined */
782a45cf3ccSLinus Walleij 		writel(0, sdd->regs + S3C64XX_SPI_FB_CLK);
783a45cf3ccSLinus Walleij 	else
784ca632f55SGrant Likely 		writel(cs->fb_delay & 0x3, sdd->regs + S3C64XX_SPI_FB_CLK);
785ca632f55SGrant Likely 
7866bb9c0e3SMark Brown 	return 0;
7876bb9c0e3SMark Brown }
788ca632f55SGrant Likely 
s3c64xx_spi_max_transfer_size(struct spi_device * spi)7891224e295SVincent Whitchurch static size_t s3c64xx_spi_max_transfer_size(struct spi_device *spi)
7901224e295SVincent Whitchurch {
7911224e295SVincent Whitchurch 	struct spi_controller *ctlr = spi->controller;
7921224e295SVincent Whitchurch 
7931224e295SVincent Whitchurch 	return ctlr->can_dma ? S3C64XX_SPI_PACKET_CNT_MASK : SIZE_MAX;
7941224e295SVincent Whitchurch }
7951224e295SVincent Whitchurch 
s3c64xx_spi_transfer_one(struct spi_controller * host,struct spi_device * spi,struct spi_transfer * xfer)796f4bc49edSYang Yingliang static int s3c64xx_spi_transfer_one(struct spi_controller *host,
7970732a9d2SMark Brown 				    struct spi_device *spi,
7980732a9d2SMark Brown 				    struct spi_transfer *xfer)
7996bb9c0e3SMark Brown {
800f4bc49edSYang Yingliang 	struct s3c64xx_spi_driver_data *sdd = spi_controller_get_devdata(host);
801c6e776abSTudor Ambarus 	const unsigned int fifo_len = sdd->fifo_depth;
8020af7af7dSSylwester Nawrocki 	const void *tx_buf = NULL;
8030af7af7dSSylwester Nawrocki 	void *rx_buf = NULL;
8040af7af7dSSylwester Nawrocki 	int target_len = 0, origin_len = 0;
8050af7af7dSSylwester Nawrocki 	int use_dma = 0;
8061ee80671SJaewon Kim 	bool use_irq = false;
8070732a9d2SMark Brown 	int status;
8086bb9c0e3SMark Brown 	u32 speed;
8096bb9c0e3SMark Brown 	u8 bpw;
810ca632f55SGrant Likely 	unsigned long flags;
8111ee80671SJaewon Kim 	u32 rdy_lv;
8121ee80671SJaewon Kim 	u32 val;
813ca632f55SGrant Likely 
81416735d02SWolfram Sang 	reinit_completion(&sdd->xfer_completion);
815ca632f55SGrant Likely 
816ca632f55SGrant Likely 	/* Only BPW and Speed may change across transfers */
817766ed704SLaxman Dewangan 	bpw = xfer->bits_per_word;
81888d4a744SJarkko Nikula 	speed = xfer->speed_hz;
819ca632f55SGrant Likely 
820ca632f55SGrant Likely 	if (bpw != sdd->cur_bpw || speed != sdd->cur_speed) {
821ca632f55SGrant Likely 		sdd->cur_bpw = bpw;
822ca632f55SGrant Likely 		sdd->cur_speed = speed;
82311f66f09SAndi Shyti 		sdd->cur_mode = spi->mode;
8242f4db6f7SŁukasz Stelmach 		status = s3c64xx_spi_config(sdd);
8252f4db6f7SŁukasz Stelmach 		if (status)
8262f4db6f7SŁukasz Stelmach 			return status;
827ca632f55SGrant Likely 	}
828ca632f55SGrant Likely 
829a3d3eab6SJaewon Kim 	if (!is_polling(sdd) && xfer->len >= fifo_len &&
8300af7af7dSSylwester Nawrocki 	    sdd->rx_dma.ch && sdd->tx_dma.ch) {
831ca632f55SGrant Likely 		use_dma = 1;
8321ee80671SJaewon Kim 	} else if (xfer->len >= fifo_len) {
8330af7af7dSSylwester Nawrocki 		tx_buf = xfer->tx_buf;
8340af7af7dSSylwester Nawrocki 		rx_buf = xfer->rx_buf;
8350af7af7dSSylwester Nawrocki 		origin_len = xfer->len;
8360af7af7dSSylwester Nawrocki 		target_len = xfer->len;
8371ee80671SJaewon Kim 		xfer->len = fifo_len - 1;
8380af7af7dSSylwester Nawrocki 	}
8390af7af7dSSylwester Nawrocki 
8400af7af7dSSylwester Nawrocki 	do {
8411ee80671SJaewon Kim 		/* transfer size is greater than 32, change to IRQ mode */
84226cd10a0SJaewon Kim 		if (!use_dma && xfer->len > S3C64XX_SPI_POLLING_SIZE)
8431ee80671SJaewon Kim 			use_irq = true;
8441ee80671SJaewon Kim 
8451ee80671SJaewon Kim 		if (use_irq) {
8461ee80671SJaewon Kim 			reinit_completion(&sdd->xfer_completion);
8471ee80671SJaewon Kim 
8481ee80671SJaewon Kim 			rdy_lv = xfer->len;
8491ee80671SJaewon Kim 			/* Setup RDY_FIFO trigger Level
8501ee80671SJaewon Kim 			 * RDY_LVL =
8511ee80671SJaewon Kim 			 * fifo_lvl up to 64 byte -> N bytes
8521ee80671SJaewon Kim 			 *               128 byte -> RDY_LVL * 2 bytes
8531ee80671SJaewon Kim 			 *               256 byte -> RDY_LVL * 4 bytes
8541ee80671SJaewon Kim 			 */
8551ee80671SJaewon Kim 			if (fifo_len == 128)
8561ee80671SJaewon Kim 				rdy_lv /= 2;
8571ee80671SJaewon Kim 			else if (fifo_len == 256)
8581ee80671SJaewon Kim 				rdy_lv /= 4;
8591ee80671SJaewon Kim 
8601ee80671SJaewon Kim 			val = readl(sdd->regs + S3C64XX_SPI_MODE_CFG);
8611ee80671SJaewon Kim 			val &= ~S3C64XX_SPI_MODE_RX_RDY_LVL;
8621ee80671SJaewon Kim 			val |= (rdy_lv << S3C64XX_SPI_MODE_RX_RDY_LVL_SHIFT);
8631ee80671SJaewon Kim 			writel(val, sdd->regs + S3C64XX_SPI_MODE_CFG);
8641ee80671SJaewon Kim 
8651ee80671SJaewon Kim 			/* Enable FIFO_RDY_EN IRQ */
8661ee80671SJaewon Kim 			val = readl(sdd->regs + S3C64XX_SPI_INT_EN);
8671ee80671SJaewon Kim 			writel((val | S3C64XX_SPI_INT_RX_FIFORDY_EN),
8681ee80671SJaewon Kim 					sdd->regs + S3C64XX_SPI_INT_EN);
8691ee80671SJaewon Kim 
8701ee80671SJaewon Kim 		}
8711ee80671SJaewon Kim 
872ca632f55SGrant Likely 		spin_lock_irqsave(&sdd->lock, flags);
873ca632f55SGrant Likely 
874ca632f55SGrant Likely 		/* Pending only which is to be done */
875ca632f55SGrant Likely 		sdd->state &= ~RXBUSY;
876ca632f55SGrant Likely 		sdd->state &= ~TXBUSY;
877ca632f55SGrant Likely 
8788c09daa1SMark Brown 		/* Start the signals */
879aa4964c4SAndi Shyti 		s3c64xx_spi_set_cs(spi, true);
8808c09daa1SMark Brown 
8812f4db6f7SŁukasz Stelmach 		status = s3c64xx_enable_datapath(sdd, xfer, use_dma);
882581e2b41SŁukasz Stelmach 
883ca632f55SGrant Likely 		spin_unlock_irqrestore(&sdd->lock, flags);
884ca632f55SGrant Likely 
8852f4db6f7SŁukasz Stelmach 		if (status) {
8862f4db6f7SŁukasz Stelmach 			dev_err(&spi->dev, "failed to enable data path for transfer: %d\n", status);
8872f4db6f7SŁukasz Stelmach 			break;
8882f4db6f7SŁukasz Stelmach 		}
8892f4db6f7SŁukasz Stelmach 
8903700c6ebSMark Brown 		if (use_dma)
8913655d30cSSylwester Nawrocki 			status = s3c64xx_wait_for_dma(sdd, xfer);
8923700c6ebSMark Brown 		else
8931ee80671SJaewon Kim 			status = s3c64xx_wait_for_pio(sdd, xfer, use_irq);
894ca632f55SGrant Likely 
895ca632f55SGrant Likely 		if (status) {
8960af7af7dSSylwester Nawrocki 			dev_err(&spi->dev,
897df7cd1bbSŁukasz Stelmach 				"I/O Error: rx-%d tx-%d rx-%c tx-%c len-%d dma-%d res-(%d)\n",
898ca632f55SGrant Likely 				xfer->rx_buf ? 1 : 0, xfer->tx_buf ? 1 : 0,
899ca632f55SGrant Likely 				(sdd->state & RXBUSY) ? 'f' : 'p',
900ca632f55SGrant Likely 				(sdd->state & TXBUSY) ? 'f' : 'p',
901df7cd1bbSŁukasz Stelmach 				xfer->len, use_dma ? 1 : 0, status);
902ca632f55SGrant Likely 
903ca632f55SGrant Likely 			if (use_dma) {
904df7cd1bbSŁukasz Stelmach 				struct dma_tx_state s;
905df7cd1bbSŁukasz Stelmach 
906df7cd1bbSŁukasz Stelmach 				if (xfer->tx_buf && (sdd->state & TXBUSY)) {
907df7cd1bbSŁukasz Stelmach 					dmaengine_pause(sdd->tx_dma.ch);
908df7cd1bbSŁukasz Stelmach 					dmaengine_tx_status(sdd->tx_dma.ch, sdd->tx_dma.cookie, &s);
9091b5e1b69SMark Brown 					dmaengine_terminate_all(sdd->tx_dma.ch);
910df7cd1bbSŁukasz Stelmach 					dev_err(&spi->dev, "TX residue: %d\n", s.residue);
911df7cd1bbSŁukasz Stelmach 
912df7cd1bbSŁukasz Stelmach 				}
913df7cd1bbSŁukasz Stelmach 				if (xfer->rx_buf && (sdd->state & RXBUSY)) {
914df7cd1bbSŁukasz Stelmach 					dmaengine_pause(sdd->rx_dma.ch);
915df7cd1bbSŁukasz Stelmach 					dmaengine_tx_status(sdd->rx_dma.ch, sdd->rx_dma.cookie, &s);
9161b5e1b69SMark Brown 					dmaengine_terminate_all(sdd->rx_dma.ch);
917df7cd1bbSŁukasz Stelmach 					dev_err(&spi->dev, "RX residue: %d\n", s.residue);
918df7cd1bbSŁukasz Stelmach 				}
919ca632f55SGrant Likely 			}
9208c09daa1SMark Brown 		} else {
9213655d30cSSylwester Nawrocki 			s3c64xx_flush_fifo(sdd);
922ca632f55SGrant Likely 		}
9230af7af7dSSylwester Nawrocki 		if (target_len > 0) {
9240af7af7dSSylwester Nawrocki 			target_len -= xfer->len;
9250af7af7dSSylwester Nawrocki 
9260af7af7dSSylwester Nawrocki 			if (xfer->tx_buf)
9270af7af7dSSylwester Nawrocki 				xfer->tx_buf += xfer->len;
9280af7af7dSSylwester Nawrocki 
9290af7af7dSSylwester Nawrocki 			if (xfer->rx_buf)
9300af7af7dSSylwester Nawrocki 				xfer->rx_buf += xfer->len;
9310af7af7dSSylwester Nawrocki 
9321ee80671SJaewon Kim 			if (target_len >= fifo_len)
9331ee80671SJaewon Kim 				xfer->len = fifo_len - 1;
9340af7af7dSSylwester Nawrocki 			else
9350af7af7dSSylwester Nawrocki 				xfer->len = target_len;
9360af7af7dSSylwester Nawrocki 		}
9370af7af7dSSylwester Nawrocki 	} while (target_len > 0);
9380af7af7dSSylwester Nawrocki 
9390af7af7dSSylwester Nawrocki 	if (origin_len) {
9400af7af7dSSylwester Nawrocki 		/* Restore original xfer buffers and length */
9410af7af7dSSylwester Nawrocki 		xfer->tx_buf = tx_buf;
9420af7af7dSSylwester Nawrocki 		xfer->rx_buf = rx_buf;
9430af7af7dSSylwester Nawrocki 		xfer->len = origin_len;
9440af7af7dSSylwester Nawrocki 	}
945ca632f55SGrant Likely 
9460732a9d2SMark Brown 	return status;
947ca632f55SGrant Likely }
948ca632f55SGrant Likely 
s3c64xx_get_target_ctrldata(struct spi_device * spi)949f4bc49edSYang Yingliang static struct s3c64xx_spi_csinfo *s3c64xx_get_target_ctrldata(
9502b908075SThomas Abraham 				struct spi_device *spi)
9512b908075SThomas Abraham {
9522b908075SThomas Abraham 	struct s3c64xx_spi_csinfo *cs;
953abba116fSShivani Gupta 	struct device_node *target_np;
9542b908075SThomas Abraham 	u32 fb_delay = 0;
9552b908075SThomas Abraham 
956f4bc49edSYang Yingliang 	target_np = spi->dev.of_node;
957f4bc49edSYang Yingliang 	if (!target_np) {
9582b908075SThomas Abraham 		dev_err(&spi->dev, "device node not found\n");
9592b908075SThomas Abraham 		return ERR_PTR(-EINVAL);
9602b908075SThomas Abraham 	}
9612b908075SThomas Abraham 
9627db7a246SKrzysztof Kozlowski 	cs = kzalloc(sizeof(*cs), GFP_KERNEL);
9637db7a246SKrzysztof Kozlowski 	if (!cs)
9647db7a246SKrzysztof Kozlowski 		return ERR_PTR(-ENOMEM);
9657db7a246SKrzysztof Kozlowski 
966abba116fSShivani Gupta 	struct device_node *data_np __free(device_node) =
967abba116fSShivani Gupta 			of_get_child_by_name(target_np, "controller-data");
9682b908075SThomas Abraham 	if (!data_np) {
9697db7a246SKrzysztof Kozlowski 		dev_info(&spi->dev, "feedback delay set to default (0)\n");
9707db7a246SKrzysztof Kozlowski 		return cs;
9712b908075SThomas Abraham 	}
9722b908075SThomas Abraham 
9732b908075SThomas Abraham 	of_property_read_u32(data_np, "samsung,spi-feedback-delay", &fb_delay);
9742b908075SThomas Abraham 	cs->fb_delay = fb_delay;
9752b908075SThomas Abraham 	return cs;
9762b908075SThomas Abraham }
9772b908075SThomas Abraham 
978ca632f55SGrant Likely /*
979ca632f55SGrant Likely  * Here we only check the validity of requested configuration
980ca632f55SGrant Likely  * and save the configuration in a local data-structure.
981ca632f55SGrant Likely  * The controller is actually configured only just before we
982ca632f55SGrant Likely  * get a message to transfer.
983ca632f55SGrant Likely  */
s3c64xx_spi_setup(struct spi_device * spi)984ca632f55SGrant Likely static int s3c64xx_spi_setup(struct spi_device *spi)
985ca632f55SGrant Likely {
986ca632f55SGrant Likely 	struct s3c64xx_spi_csinfo *cs = spi->controller_data;
987ca632f55SGrant Likely 	struct s3c64xx_spi_driver_data *sdd;
9882b908075SThomas Abraham 	int err;
989bfcd27dcSChanho Park 	int div;
990ca632f55SGrant Likely 
991f4bc49edSYang Yingliang 	sdd = spi_controller_get_devdata(spi->controller);
992306972ceSNaveen Krishna Chatradhi 	if (spi->dev.of_node) {
993f4bc49edSYang Yingliang 		cs = s3c64xx_get_target_ctrldata(spi);
9942b908075SThomas Abraham 		spi->controller_data = cs;
9952b908075SThomas Abraham 	}
9962b908075SThomas Abraham 
997a45cf3ccSLinus Walleij 	/* NULL is fine, we just avoid using the FB delay (=0) */
998a45cf3ccSLinus Walleij 	if (IS_ERR(cs)) {
9999e264f3fSAmit Kumar Mahapatra via Alsa-devel 		dev_err(&spi->dev, "No CS for SPI(%d)\n", spi_get_chipselect(spi, 0));
1000ca632f55SGrant Likely 		return -ENODEV;
1001ca632f55SGrant Likely 	}
1002ca632f55SGrant Likely 
1003a45cf3ccSLinus Walleij 	if (!spi_get_ctldata(spi))
10043146beecSGirish K S 		spi_set_ctldata(spi, cs);
10053146beecSGirish K S 
1006b97b6621SMark Brown 	pm_runtime_get_sync(&sdd->pdev->dev);
1007b97b6621SMark Brown 
1008bfcd27dcSChanho Park 	div = sdd->port_conf->clk_div;
1009bfcd27dcSChanho Park 
1010ca632f55SGrant Likely 	/* Check if we can provide the requested rate */
1011a5238e36SThomas Abraham 	if (!sdd->port_conf->clk_from_cmu) {
1012ca632f55SGrant Likely 		u32 psr, speed;
1013ca632f55SGrant Likely 
1014ca632f55SGrant Likely 		/* Max possible */
1015bfcd27dcSChanho Park 		speed = clk_get_rate(sdd->src_clk) / div / (0 + 1);
1016ca632f55SGrant Likely 
1017ca632f55SGrant Likely 		if (spi->max_speed_hz > speed)
1018ca632f55SGrant Likely 			spi->max_speed_hz = speed;
1019ca632f55SGrant Likely 
1020bfcd27dcSChanho Park 		psr = clk_get_rate(sdd->src_clk) / div / spi->max_speed_hz - 1;
1021ca632f55SGrant Likely 		psr &= S3C64XX_SPI_PSR_MASK;
1022ca632f55SGrant Likely 		if (psr == S3C64XX_SPI_PSR_MASK)
1023ca632f55SGrant Likely 			psr--;
1024ca632f55SGrant Likely 
1025bfcd27dcSChanho Park 		speed = clk_get_rate(sdd->src_clk) / div / (psr + 1);
1026ca632f55SGrant Likely 		if (spi->max_speed_hz < speed) {
1027ca632f55SGrant Likely 			if (psr+1 < S3C64XX_SPI_PSR_MASK) {
1028ca632f55SGrant Likely 				psr++;
1029ca632f55SGrant Likely 			} else {
1030ca632f55SGrant Likely 				err = -EINVAL;
1031ca632f55SGrant Likely 				goto setup_exit;
1032ca632f55SGrant Likely 			}
1033ca632f55SGrant Likely 		}
1034ca632f55SGrant Likely 
1035bfcd27dcSChanho Park 		speed = clk_get_rate(sdd->src_clk) / div / (psr + 1);
10362b908075SThomas Abraham 		if (spi->max_speed_hz >= speed) {
1037ca632f55SGrant Likely 			spi->max_speed_hz = speed;
10382b908075SThomas Abraham 		} else {
1039e1b0f0dfSMark Brown 			dev_err(&spi->dev, "Can't set %dHz transfer speed\n",
1040e1b0f0dfSMark Brown 				spi->max_speed_hz);
1041ca632f55SGrant Likely 			err = -EINVAL;
10422b908075SThomas Abraham 			goto setup_exit;
10432b908075SThomas Abraham 		}
1044ca632f55SGrant Likely 	}
1045ca632f55SGrant Likely 
1046483867eeSHeiner Kallweit 	pm_runtime_mark_last_busy(&sdd->pdev->dev);
1047483867eeSHeiner Kallweit 	pm_runtime_put_autosuspend(&sdd->pdev->dev);
1048aa4964c4SAndi Shyti 	s3c64xx_spi_set_cs(spi, false);
1049aa4964c4SAndi Shyti 
10502b908075SThomas Abraham 	return 0;
1051b97b6621SMark Brown 
1052ca632f55SGrant Likely setup_exit:
1053483867eeSHeiner Kallweit 	pm_runtime_mark_last_busy(&sdd->pdev->dev);
1054483867eeSHeiner Kallweit 	pm_runtime_put_autosuspend(&sdd->pdev->dev);
1055ca632f55SGrant Likely 	/* setup() returns with device de-selected */
1056aa4964c4SAndi Shyti 	s3c64xx_spi_set_cs(spi, false);
1057ca632f55SGrant Likely 
10582b908075SThomas Abraham 	spi_set_ctldata(spi, NULL);
10592b908075SThomas Abraham 
1060a45cf3ccSLinus Walleij 	/* This was dynamically allocated on the DT path */
10615bee3b94SSylwester Nawrocki 	if (spi->dev.of_node)
10622b908075SThomas Abraham 		kfree(cs);
10632b908075SThomas Abraham 
1064ca632f55SGrant Likely 	return err;
1065ca632f55SGrant Likely }
1066ca632f55SGrant Likely 
s3c64xx_spi_cleanup(struct spi_device * spi)10671c20c200SThomas Abraham static void s3c64xx_spi_cleanup(struct spi_device *spi)
10681c20c200SThomas Abraham {
10691c20c200SThomas Abraham 	struct s3c64xx_spi_csinfo *cs = spi_get_ctldata(spi);
10701c20c200SThomas Abraham 
1071a45cf3ccSLinus Walleij 	/* This was dynamically allocated on the DT path */
10722b908075SThomas Abraham 	if (spi->dev.of_node)
10732b908075SThomas Abraham 		kfree(cs);
1074306972ceSNaveen Krishna Chatradhi 
10751c20c200SThomas Abraham 	spi_set_ctldata(spi, NULL);
10761c20c200SThomas Abraham }
10771c20c200SThomas Abraham 
s3c64xx_spi_irq(int irq,void * data)1078c2573128SMark Brown static irqreturn_t s3c64xx_spi_irq(int irq, void *data)
1079c2573128SMark Brown {
1080c2573128SMark Brown 	struct s3c64xx_spi_driver_data *sdd = data;
1081f4bc49edSYang Yingliang 	struct spi_controller *spi = sdd->host;
1082375981f2SGirish K S 	unsigned int val, clr = 0;
1083c2573128SMark Brown 
1084375981f2SGirish K S 	val = readl(sdd->regs + S3C64XX_SPI_STATUS);
1085c2573128SMark Brown 
1086375981f2SGirish K S 	if (val & S3C64XX_SPI_ST_RX_OVERRUN_ERR) {
1087375981f2SGirish K S 		clr = S3C64XX_SPI_PND_RX_OVERRUN_CLR;
1088c2573128SMark Brown 		dev_err(&spi->dev, "RX overrun\n");
1089375981f2SGirish K S 	}
1090375981f2SGirish K S 	if (val & S3C64XX_SPI_ST_RX_UNDERRUN_ERR) {
1091375981f2SGirish K S 		clr |= S3C64XX_SPI_PND_RX_UNDERRUN_CLR;
1092c2573128SMark Brown 		dev_err(&spi->dev, "RX underrun\n");
1093375981f2SGirish K S 	}
1094375981f2SGirish K S 	if (val & S3C64XX_SPI_ST_TX_OVERRUN_ERR) {
1095375981f2SGirish K S 		clr |= S3C64XX_SPI_PND_TX_OVERRUN_CLR;
1096c2573128SMark Brown 		dev_err(&spi->dev, "TX overrun\n");
1097375981f2SGirish K S 	}
1098375981f2SGirish K S 	if (val & S3C64XX_SPI_ST_TX_UNDERRUN_ERR) {
1099375981f2SGirish K S 		clr |= S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
1100c2573128SMark Brown 		dev_err(&spi->dev, "TX underrun\n");
1101375981f2SGirish K S 	}
1102375981f2SGirish K S 
11031ee80671SJaewon Kim 	if (val & S3C64XX_SPI_ST_RX_FIFORDY) {
11041ee80671SJaewon Kim 		complete(&sdd->xfer_completion);
11051ee80671SJaewon Kim 		/* No pending clear irq, turn-off INT_EN_RX_FIFO_RDY */
11061ee80671SJaewon Kim 		val = readl(sdd->regs + S3C64XX_SPI_INT_EN);
11071ee80671SJaewon Kim 		writel((val & ~S3C64XX_SPI_INT_RX_FIFORDY_EN),
11081ee80671SJaewon Kim 				sdd->regs + S3C64XX_SPI_INT_EN);
11091ee80671SJaewon Kim 	}
11101ee80671SJaewon Kim 
1111375981f2SGirish K S 	/* Clear the pending irq by setting and then clearing it */
1112375981f2SGirish K S 	writel(clr, sdd->regs + S3C64XX_SPI_PENDING_CLR);
1113375981f2SGirish K S 	writel(0, sdd->regs + S3C64XX_SPI_PENDING_CLR);
1114c2573128SMark Brown 
1115c2573128SMark Brown 	return IRQ_HANDLED;
1116c2573128SMark Brown }
1117c2573128SMark Brown 
s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data * sdd)11181c75862dSSylwester Nawrocki static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data *sdd)
1119ca632f55SGrant Likely {
1120ca632f55SGrant Likely 	struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
1121ca632f55SGrant Likely 	void __iomem *regs = sdd->regs;
1122ca632f55SGrant Likely 	unsigned int val;
1123ca632f55SGrant Likely 
1124ca632f55SGrant Likely 	sdd->cur_speed = 0;
1125ca632f55SGrant Likely 
1126a92e7c3dSAndi Shyti 	if (sci->no_cs)
1127913ba5c9SŁukasz Stelmach 		writel(0, sdd->regs + S3C64XX_SPI_CS_REG);
1128a92e7c3dSAndi Shyti 	else if (!(sdd->port_conf->quirks & S3C64XX_SPI_QUIRK_CS_AUTO))
1129913ba5c9SŁukasz Stelmach 		writel(S3C64XX_SPI_CS_SIG_INACT, sdd->regs + S3C64XX_SPI_CS_REG);
1130ca632f55SGrant Likely 
1131ca632f55SGrant Likely 	/* Disable Interrupts - we use Polling if not DMA mode */
1132ca632f55SGrant Likely 	writel(0, regs + S3C64XX_SPI_INT_EN);
1133ca632f55SGrant Likely 
1134a5238e36SThomas Abraham 	if (!sdd->port_conf->clk_from_cmu)
1135ca632f55SGrant Likely 		writel(sci->src_clk_nr << S3C64XX_SPI_CLKSEL_SRCSHFT,
1136ca632f55SGrant Likely 				regs + S3C64XX_SPI_CLK_CFG);
1137ca632f55SGrant Likely 	writel(0, regs + S3C64XX_SPI_MODE_CFG);
1138ca632f55SGrant Likely 	writel(0, regs + S3C64XX_SPI_PACKET_CNT);
1139ca632f55SGrant Likely 
1140375981f2SGirish K S 	/* Clear any irq pending bits, should set and clear the bits */
1141375981f2SGirish K S 	val = S3C64XX_SPI_PND_RX_OVERRUN_CLR |
1142375981f2SGirish K S 		S3C64XX_SPI_PND_RX_UNDERRUN_CLR |
1143375981f2SGirish K S 		S3C64XX_SPI_PND_TX_OVERRUN_CLR |
1144375981f2SGirish K S 		S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
1145375981f2SGirish K S 	writel(val, regs + S3C64XX_SPI_PENDING_CLR);
1146375981f2SGirish K S 	writel(0, regs + S3C64XX_SPI_PENDING_CLR);
1147ca632f55SGrant Likely 
1148ca632f55SGrant Likely 	writel(0, regs + S3C64XX_SPI_SWAP_CFG);
1149ca632f55SGrant Likely 
1150ca632f55SGrant Likely 	val = readl(regs + S3C64XX_SPI_MODE_CFG);
1151ca632f55SGrant Likely 	val &= ~S3C64XX_SPI_MODE_4BURST;
1152eb8096c3STudor Ambarus 	val |= (S3C64XX_SPI_MAX_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
1153ca632f55SGrant Likely 	writel(val, regs + S3C64XX_SPI_MODE_CFG);
1154ca632f55SGrant Likely 
11553655d30cSSylwester Nawrocki 	s3c64xx_flush_fifo(sdd);
1156ca632f55SGrant Likely }
1157ca632f55SGrant Likely 
11582b908075SThomas Abraham #ifdef CONFIG_OF
s3c64xx_spi_parse_dt(struct device * dev)115975bf3361SJingoo Han static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
11602b908075SThomas Abraham {
11612b908075SThomas Abraham 	struct s3c64xx_spi_info *sci;
11622b908075SThomas Abraham 	u32 temp;
11632b908075SThomas Abraham 
11642b908075SThomas Abraham 	sci = devm_kzalloc(dev, sizeof(*sci), GFP_KERNEL);
11651273eb05SJingoo Han 	if (!sci)
11662b908075SThomas Abraham 		return ERR_PTR(-ENOMEM);
11672b908075SThomas Abraham 
11682b908075SThomas Abraham 	if (of_property_read_u32(dev->of_node, "samsung,spi-src-clk", &temp)) {
1169f186d340STudor Ambarus 		dev_dbg(dev, "spi bus clock parent not specified, using clock at index 0 as parent\n");
11702b908075SThomas Abraham 		sci->src_clk_nr = 0;
11712b908075SThomas Abraham 	} else {
11722b908075SThomas Abraham 		sci->src_clk_nr = temp;
11732b908075SThomas Abraham 	}
11742b908075SThomas Abraham 
11752b908075SThomas Abraham 	if (of_property_read_u32(dev->of_node, "num-cs", &temp)) {
1176f186d340STudor Ambarus 		dev_dbg(dev, "number of chip select lines not specified, assuming 1 chip select line\n");
11772b908075SThomas Abraham 		sci->num_cs = 1;
11782b908075SThomas Abraham 	} else {
11792b908075SThomas Abraham 		sci->num_cs = temp;
11802b908075SThomas Abraham 	}
11812b908075SThomas Abraham 
1182379f831aSAndi Shyti 	sci->no_cs = of_property_read_bool(dev->of_node, "no-cs-readback");
1183d1a7718eSJaewon Kim 	sci->polling = !of_property_present(dev->of_node, "dmas");
1184a92e7c3dSAndi Shyti 
11852b908075SThomas Abraham 	return sci;
11862b908075SThomas Abraham }
11872b908075SThomas Abraham #else
s3c64xx_spi_parse_dt(struct device * dev)11882b908075SThomas Abraham static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
11892b908075SThomas Abraham {
11908074cf06SJingoo Han 	return dev_get_platdata(dev);
11912b908075SThomas Abraham }
11922b908075SThomas Abraham #endif
11932b908075SThomas Abraham 
s3c64xx_spi_get_port_config(struct platform_device * pdev)1194d6371415SKrzysztof Kozlowski static inline const struct s3c64xx_spi_port_config *s3c64xx_spi_get_port_config(
1195a5238e36SThomas Abraham 						struct platform_device *pdev)
1196a5238e36SThomas Abraham {
11972b908075SThomas Abraham #ifdef CONFIG_OF
1198609a2f95SKrzysztof Kozlowski 	if (pdev->dev.of_node)
1199d6371415SKrzysztof Kozlowski 		return of_device_get_match_data(&pdev->dev);
12002b908075SThomas Abraham #endif
1201d6371415SKrzysztof Kozlowski 	return (const struct s3c64xx_spi_port_config *)platform_get_device_id(pdev)->driver_data;
1202a5238e36SThomas Abraham }
1203a5238e36SThomas Abraham 
s3c64xx_spi_set_port_id(struct platform_device * pdev,struct s3c64xx_spi_driver_data * sdd)12042cda3623STudor Ambarus static int s3c64xx_spi_set_port_id(struct platform_device *pdev,
12052cda3623STudor Ambarus 				   struct s3c64xx_spi_driver_data *sdd)
12062cda3623STudor Ambarus {
1207ea3fba7cSTudor Ambarus 	const struct s3c64xx_spi_port_config *port_conf = sdd->port_conf;
12082cda3623STudor Ambarus 	int ret;
12092cda3623STudor Ambarus 
1210ea3fba7cSTudor Ambarus 	if (port_conf->rx_fifomask && port_conf->tx_fifomask)
1211ea3fba7cSTudor Ambarus 		return 0;
1212ea3fba7cSTudor Ambarus 
12132cda3623STudor Ambarus 	if (pdev->dev.of_node) {
12142cda3623STudor Ambarus 		ret = of_alias_get_id(pdev->dev.of_node, "spi");
12152cda3623STudor Ambarus 		if (ret < 0)
12162cda3623STudor Ambarus 			return dev_err_probe(&pdev->dev, ret,
12172cda3623STudor Ambarus 					     "Failed to get alias id\n");
12182cda3623STudor Ambarus 		sdd->port_id = ret;
12192cda3623STudor Ambarus 	} else {
12202cda3623STudor Ambarus 		if (pdev->id < 0)
12212cda3623STudor Ambarus 			return dev_err_probe(&pdev->dev, -EINVAL,
12222cda3623STudor Ambarus 					     "Negative platform ID is not allowed\n");
12232cda3623STudor Ambarus 		sdd->port_id = pdev->id;
12242cda3623STudor Ambarus 	}
12252cda3623STudor Ambarus 
12262cda3623STudor Ambarus 	return 0;
12272cda3623STudor Ambarus }
12282cda3623STudor Ambarus 
s3c64xx_spi_set_fifomask(struct s3c64xx_spi_driver_data * sdd)1229d6911cf2STudor Ambarus static void s3c64xx_spi_set_fifomask(struct s3c64xx_spi_driver_data *sdd)
1230d6911cf2STudor Ambarus {
1231d6911cf2STudor Ambarus 	const struct s3c64xx_spi_port_config *port_conf = sdd->port_conf;
1232d6911cf2STudor Ambarus 
1233d6911cf2STudor Ambarus 	if (port_conf->rx_fifomask)
1234d6911cf2STudor Ambarus 		sdd->rx_fifomask = port_conf->rx_fifomask;
1235d6911cf2STudor Ambarus 	else
1236d6911cf2STudor Ambarus 		sdd->rx_fifomask = FIFO_LVL_MASK(sdd) <<
1237d6911cf2STudor Ambarus 			port_conf->rx_lvl_offset;
1238d6911cf2STudor Ambarus 
1239d6911cf2STudor Ambarus 	if (port_conf->tx_fifomask)
1240d6911cf2STudor Ambarus 		sdd->tx_fifomask = port_conf->tx_fifomask;
1241d6911cf2STudor Ambarus 	else
1242d6911cf2STudor Ambarus 		sdd->tx_fifomask = FIFO_LVL_MASK(sdd) <<
1243d6911cf2STudor Ambarus 			S3C64XX_SPI_ST_TX_FIFO_LVL_SHIFT;
1244d6911cf2STudor Ambarus }
1245d6911cf2STudor Ambarus 
s3c64xx_spi_probe(struct platform_device * pdev)12462deff8d6SGrant Likely static int s3c64xx_spi_probe(struct platform_device *pdev)
1247ca632f55SGrant Likely {
12482b908075SThomas Abraham 	struct resource	*mem_res;
1249ca632f55SGrant Likely 	struct s3c64xx_spi_driver_data *sdd;
12508074cf06SJingoo Han 	struct s3c64xx_spi_info *sci = dev_get_platdata(&pdev->dev);
1251f4bc49edSYang Yingliang 	struct spi_controller *host;
1252c2573128SMark Brown 	int ret, irq;
1253a24d850bSPadmavathi Venna 	char clk_name[16];
1254ca632f55SGrant Likely 
12552b908075SThomas Abraham 	if (!sci && pdev->dev.of_node) {
12562b908075SThomas Abraham 		sci = s3c64xx_spi_parse_dt(&pdev->dev);
12572b908075SThomas Abraham 		if (IS_ERR(sci))
12582b908075SThomas Abraham 			return PTR_ERR(sci);
1259ca632f55SGrant Likely 	}
1260ca632f55SGrant Likely 
1261b4f27377SAndi Shyti 	if (!sci)
1262b4f27377SAndi Shyti 		return dev_err_probe(&pdev->dev, -ENODEV,
1263b4f27377SAndi Shyti 				     "Platform_data missing!\n");
1264ca632f55SGrant Likely 
1265c2573128SMark Brown 	irq = platform_get_irq(pdev, 0);
1266b4f27377SAndi Shyti 	if (irq < 0)
1267b2b56175SChen Jiahao 		return irq;
1268c2573128SMark Brown 
1269f4bc49edSYang Yingliang 	host = devm_spi_alloc_host(&pdev->dev, sizeof(*sdd));
1270f4bc49edSYang Yingliang 	if (!host)
1271b4f27377SAndi Shyti 		return dev_err_probe(&pdev->dev, -ENOMEM,
1272f4bc49edSYang Yingliang 				     "Unable to allocate SPI Host\n");
1273ca632f55SGrant Likely 
1274f4bc49edSYang Yingliang 	platform_set_drvdata(pdev, host);
1275ca632f55SGrant Likely 
1276f4bc49edSYang Yingliang 	sdd = spi_controller_get_devdata(host);
1277a5238e36SThomas Abraham 	sdd->port_conf = s3c64xx_spi_get_port_config(pdev);
1278f4bc49edSYang Yingliang 	sdd->host = host;
1279ca632f55SGrant Likely 	sdd->cntrlr_info = sci;
1280ca632f55SGrant Likely 	sdd->pdev = pdev;
12812cda3623STudor Ambarus 
12822cda3623STudor Ambarus 	ret = s3c64xx_spi_set_port_id(pdev, sdd);
12832cda3623STudor Ambarus 	if (ret)
12842cda3623STudor Ambarus 		return ret;
1285ca632f55SGrant Likely 
128682b98fb8STudor Ambarus 	if (sdd->port_conf->fifo_depth)
128782b98fb8STudor Ambarus 		sdd->fifo_depth = sdd->port_conf->fifo_depth;
128882b98fb8STudor Ambarus 	else if (of_property_read_u32(pdev->dev.of_node, "fifo-depth",
1289414d7b8cSTudor Ambarus 				      &sdd->fifo_depth))
1290c6e776abSTudor Ambarus 		sdd->fifo_depth = FIFO_DEPTH(sdd);
1291c6e776abSTudor Ambarus 
1292d6911cf2STudor Ambarus 	s3c64xx_spi_set_fifomask(sdd);
1293d6911cf2STudor Ambarus 
1294ca632f55SGrant Likely 	sdd->cur_bpw = 8;
1295ca632f55SGrant Likely 
1296b5be04d3SPadmavathi Venna 	sdd->tx_dma.direction = DMA_MEM_TO_DEV;
1297b5be04d3SPadmavathi Venna 	sdd->rx_dma.direction = DMA_DEV_TO_MEM;
12982b908075SThomas Abraham 
1299f4bc49edSYang Yingliang 	host->dev.of_node = pdev->dev.of_node;
1300e08433e0STudor Ambarus 	host->bus_num = -1;
1301f4bc49edSYang Yingliang 	host->setup = s3c64xx_spi_setup;
1302f4bc49edSYang Yingliang 	host->cleanup = s3c64xx_spi_cleanup;
1303f4bc49edSYang Yingliang 	host->prepare_transfer_hardware = s3c64xx_spi_prepare_transfer;
1304f4bc49edSYang Yingliang 	host->unprepare_transfer_hardware = s3c64xx_spi_unprepare_transfer;
1305f4bc49edSYang Yingliang 	host->prepare_message = s3c64xx_spi_prepare_message;
1306f4bc49edSYang Yingliang 	host->transfer_one = s3c64xx_spi_transfer_one;
1307f4bc49edSYang Yingliang 	host->max_transfer_size = s3c64xx_spi_max_transfer_size;
1308f4bc49edSYang Yingliang 	host->num_chipselect = sci->num_cs;
1309f4bc49edSYang Yingliang 	host->use_gpio_descriptors = true;
1310f4bc49edSYang Yingliang 	host->dma_alignment = 8;
1311f4bc49edSYang Yingliang 	host->bits_per_word_mask = SPI_BPW_MASK(32) | SPI_BPW_MASK(16) |
131224778be2SStephen Warren 				   SPI_BPW_MASK(8);
1313ca632f55SGrant Likely 	/* the spi->mode bits understood by this driver: */
1314f4bc49edSYang Yingliang 	host->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1315ffb7bcd3SChanho Park 	if (sdd->port_conf->has_loopback)
1316f4bc49edSYang Yingliang 		host->mode_bits |= SPI_LOOP;
1317f4bc49edSYang Yingliang 	host->auto_runtime_pm = true;
13183f295887SMark Brown 	if (!is_polling(sdd))
1319f4bc49edSYang Yingliang 		host->can_dma = s3c64xx_spi_can_dma;
1320ca632f55SGrant Likely 
13214f81b540SYangtao Li 	sdd->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &mem_res);
132276fbad41SAndi Shyti 	if (IS_ERR(sdd->regs))
132376fbad41SAndi Shyti 		return PTR_ERR(sdd->regs);
13244f81b540SYangtao Li 	sdd->sfr_start = mem_res->start;
1325ca632f55SGrant Likely 
1326b4f27377SAndi Shyti 	if (sci->cfg_gpio && sci->cfg_gpio())
1327b4f27377SAndi Shyti 		return dev_err_probe(&pdev->dev, -EBUSY,
1328b4f27377SAndi Shyti 				     "Unable to config gpio\n");
1329ca632f55SGrant Likely 
1330ca632f55SGrant Likely 	/* Setup clocks */
133120c475d2SAndi Shyti 	sdd->clk = devm_clk_get_enabled(&pdev->dev, "spi");
1332b4f27377SAndi Shyti 	if (IS_ERR(sdd->clk))
1333b4f27377SAndi Shyti 		return dev_err_probe(&pdev->dev, PTR_ERR(sdd->clk),
1334b4f27377SAndi Shyti 				     "Unable to acquire clock 'spi'\n");
1335ca632f55SGrant Likely 
1336a24d850bSPadmavathi Venna 	sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr);
133720c475d2SAndi Shyti 	sdd->src_clk = devm_clk_get_enabled(&pdev->dev, clk_name);
1338b4f27377SAndi Shyti 	if (IS_ERR(sdd->src_clk))
1339b4f27377SAndi Shyti 		return dev_err_probe(&pdev->dev, PTR_ERR(sdd->src_clk),
1340b4f27377SAndi Shyti 				     "Unable to acquire clock '%s'\n",
1341b4f27377SAndi Shyti 				     clk_name);
1342ca632f55SGrant Likely 
13437990b008SAndi Shyti 	if (sdd->port_conf->clk_ioclk) {
134420c475d2SAndi Shyti 		sdd->ioclk = devm_clk_get_enabled(&pdev->dev, "spi_ioclk");
1345b4f27377SAndi Shyti 		if (IS_ERR(sdd->ioclk))
1346b4f27377SAndi Shyti 			return dev_err_probe(&pdev->dev, PTR_ERR(sdd->ioclk),
1347b4f27377SAndi Shyti 					     "Unable to acquire 'ioclk'\n");
13487990b008SAndi Shyti 	}
13497990b008SAndi Shyti 
1350483867eeSHeiner Kallweit 	pm_runtime_set_autosuspend_delay(&pdev->dev, AUTOSUSPEND_TIMEOUT);
1351483867eeSHeiner Kallweit 	pm_runtime_use_autosuspend(&pdev->dev);
1352483867eeSHeiner Kallweit 	pm_runtime_set_active(&pdev->dev);
1353483867eeSHeiner Kallweit 	pm_runtime_enable(&pdev->dev);
1354483867eeSHeiner Kallweit 	pm_runtime_get_sync(&pdev->dev);
1355483867eeSHeiner Kallweit 
1356ca632f55SGrant Likely 	/* Setup Deufult Mode */
13571c75862dSSylwester Nawrocki 	s3c64xx_spi_hwinit(sdd);
1358ca632f55SGrant Likely 
1359ca632f55SGrant Likely 	spin_lock_init(&sdd->lock);
1360ca632f55SGrant Likely 	init_completion(&sdd->xfer_completion);
1361ca632f55SGrant Likely 
13624eb77006SJingoo Han 	ret = devm_request_irq(&pdev->dev, irq, s3c64xx_spi_irq, 0,
13634eb77006SJingoo Han 				"spi-s3c64xx", sdd);
1364c2573128SMark Brown 	if (ret != 0) {
1365c2573128SMark Brown 		dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n",
1366c2573128SMark Brown 			irq, ret);
136760a9a964SAndi Shyti 		goto err_pm_put;
1368c2573128SMark Brown 	}
1369c2573128SMark Brown 
1370c2573128SMark Brown 	writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
1371c2573128SMark Brown 	       S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
1372c2573128SMark Brown 	       sdd->regs + S3C64XX_SPI_INT_EN);
1373c2573128SMark Brown 
1374f4bc49edSYang Yingliang 	ret = devm_spi_register_controller(&pdev->dev, host);
137591800f0eSMark Brown 	if (ret != 0) {
1376f4bc49edSYang Yingliang 		dev_err(&pdev->dev, "cannot register SPI host: %d\n", ret);
137760a9a964SAndi Shyti 		goto err_pm_put;
1378ca632f55SGrant Likely 	}
1379ca632f55SGrant Likely 
1380f4bc49edSYang Yingliang 	dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d with %d Targets attached\n",
1381e08433e0STudor Ambarus 		host->bus_num, host->num_chipselect);
13826f8dc9d4SSylwester Nawrocki 	dev_dbg(&pdev->dev, "\tIOmem=[%pR]\tFIFO %dbytes\n",
1383c6e776abSTudor Ambarus 		mem_res, sdd->fifo_depth);
1384ca632f55SGrant Likely 
1385483867eeSHeiner Kallweit 	pm_runtime_mark_last_busy(&pdev->dev);
1386483867eeSHeiner Kallweit 	pm_runtime_put_autosuspend(&pdev->dev);
1387483867eeSHeiner Kallweit 
1388ca632f55SGrant Likely 	return 0;
1389ca632f55SGrant Likely 
139060a9a964SAndi Shyti err_pm_put:
1391483867eeSHeiner Kallweit 	pm_runtime_put_noidle(&pdev->dev);
13923c863792SHeiner Kallweit 	pm_runtime_disable(&pdev->dev);
13933c863792SHeiner Kallweit 	pm_runtime_set_suspended(&pdev->dev);
1394483867eeSHeiner Kallweit 
1395ca632f55SGrant Likely 	return ret;
1396ca632f55SGrant Likely }
1397ca632f55SGrant Likely 
s3c64xx_spi_remove(struct platform_device * pdev)13982b7981a6SUwe Kleine-König static void s3c64xx_spi_remove(struct platform_device *pdev)
1399ca632f55SGrant Likely {
1400f4bc49edSYang Yingliang 	struct spi_controller *host = platform_get_drvdata(pdev);
1401f4bc49edSYang Yingliang 	struct s3c64xx_spi_driver_data *sdd = spi_controller_get_devdata(host);
1402ca632f55SGrant Likely 
14038ebe9d16SHeiner Kallweit 	pm_runtime_get_sync(&pdev->dev);
1404b97b6621SMark Brown 
1405c2573128SMark Brown 	writel(0, sdd->regs + S3C64XX_SPI_INT_EN);
1406c2573128SMark Brown 
14073d63a47aSMarek Szyprowski 	if (!is_polling(sdd)) {
14083d63a47aSMarek Szyprowski 		dma_release_channel(sdd->rx_dma.ch);
14093d63a47aSMarek Szyprowski 		dma_release_channel(sdd->tx_dma.ch);
14103d63a47aSMarek Szyprowski 	}
14113d63a47aSMarek Szyprowski 
14128ebe9d16SHeiner Kallweit 	pm_runtime_put_noidle(&pdev->dev);
14138ebe9d16SHeiner Kallweit 	pm_runtime_disable(&pdev->dev);
14148ebe9d16SHeiner Kallweit 	pm_runtime_set_suspended(&pdev->dev);
1415ca632f55SGrant Likely }
1416ca632f55SGrant Likely 
1417997230d0SJingoo Han #ifdef CONFIG_PM_SLEEP
s3c64xx_spi_suspend(struct device * dev)1418e25d0bf9SMark Brown static int s3c64xx_spi_suspend(struct device *dev)
1419ca632f55SGrant Likely {
1420f4bc49edSYang Yingliang 	struct spi_controller *host = dev_get_drvdata(dev);
1421f4bc49edSYang Yingliang 	struct s3c64xx_spi_driver_data *sdd = spi_controller_get_devdata(host);
142291a9b8e6STudor Ambarus 	int ret;
1423ca632f55SGrant Likely 
142491a9b8e6STudor Ambarus 	ret = spi_controller_suspend(host);
1425347de6baSKrzysztof Kozlowski 	if (ret)
1426347de6baSKrzysztof Kozlowski 		return ret;
1427ca632f55SGrant Likely 
14284fcd9b9eSHeiner Kallweit 	ret = pm_runtime_force_suspend(dev);
14294fcd9b9eSHeiner Kallweit 	if (ret < 0)
14304fcd9b9eSHeiner Kallweit 		return ret;
1431ca632f55SGrant Likely 
1432ca632f55SGrant Likely 	sdd->cur_speed = 0; /* Output Clock is stopped */
1433ca632f55SGrant Likely 
1434ca632f55SGrant Likely 	return 0;
1435ca632f55SGrant Likely }
1436ca632f55SGrant Likely 
s3c64xx_spi_resume(struct device * dev)1437e25d0bf9SMark Brown static int s3c64xx_spi_resume(struct device *dev)
1438ca632f55SGrant Likely {
1439f4bc49edSYang Yingliang 	struct spi_controller *host = dev_get_drvdata(dev);
1440f4bc49edSYang Yingliang 	struct s3c64xx_spi_driver_data *sdd = spi_controller_get_devdata(host);
1441ca632f55SGrant Likely 	struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
14424fcd9b9eSHeiner Kallweit 	int ret;
1443ca632f55SGrant Likely 
144400ab5392SThomas Abraham 	if (sci->cfg_gpio)
1445868dee91SThomas Abraham 		sci->cfg_gpio();
1446ca632f55SGrant Likely 
14474fcd9b9eSHeiner Kallweit 	ret = pm_runtime_force_resume(dev);
14484fcd9b9eSHeiner Kallweit 	if (ret < 0)
14494fcd9b9eSHeiner Kallweit 		return ret;
1450ca632f55SGrant Likely 
1451f4bc49edSYang Yingliang 	return spi_controller_resume(host);
1452ca632f55SGrant Likely }
1453997230d0SJingoo Han #endif /* CONFIG_PM_SLEEP */
1454ca632f55SGrant Likely 
1455ec833050SRafael J. Wysocki #ifdef CONFIG_PM
s3c64xx_spi_runtime_suspend(struct device * dev)1456b97b6621SMark Brown static int s3c64xx_spi_runtime_suspend(struct device *dev)
1457b97b6621SMark Brown {
1458f4bc49edSYang Yingliang 	struct spi_controller *host = dev_get_drvdata(dev);
1459f4bc49edSYang Yingliang 	struct s3c64xx_spi_driver_data *sdd = spi_controller_get_devdata(host);
1460b97b6621SMark Brown 
14619f667bffSThomas Abraham 	clk_disable_unprepare(sdd->clk);
14629f667bffSThomas Abraham 	clk_disable_unprepare(sdd->src_clk);
14637990b008SAndi Shyti 	clk_disable_unprepare(sdd->ioclk);
1464b97b6621SMark Brown 
1465b97b6621SMark Brown 	return 0;
1466b97b6621SMark Brown }
1467b97b6621SMark Brown 
s3c64xx_spi_runtime_resume(struct device * dev)1468b97b6621SMark Brown static int s3c64xx_spi_runtime_resume(struct device *dev)
1469b97b6621SMark Brown {
1470f4bc49edSYang Yingliang 	struct spi_controller *host = dev_get_drvdata(dev);
1471f4bc49edSYang Yingliang 	struct s3c64xx_spi_driver_data *sdd = spi_controller_get_devdata(host);
14728b06d5b8SMark Brown 	int ret;
1473b97b6621SMark Brown 
14747990b008SAndi Shyti 	if (sdd->port_conf->clk_ioclk) {
14757990b008SAndi Shyti 		ret = clk_prepare_enable(sdd->ioclk);
14768b06d5b8SMark Brown 		if (ret != 0)
14778b06d5b8SMark Brown 			return ret;
14788b06d5b8SMark Brown 	}
1479b97b6621SMark Brown 
14807990b008SAndi Shyti 	ret = clk_prepare_enable(sdd->src_clk);
14817990b008SAndi Shyti 	if (ret != 0)
14827990b008SAndi Shyti 		goto err_disable_ioclk;
14837990b008SAndi Shyti 
14847990b008SAndi Shyti 	ret = clk_prepare_enable(sdd->clk);
14857990b008SAndi Shyti 	if (ret != 0)
14867990b008SAndi Shyti 		goto err_disable_src_clk;
14877990b008SAndi Shyti 
1488e935dba1SMarek Szyprowski 	s3c64xx_spi_hwinit(sdd);
1489e935dba1SMarek Szyprowski 
14903f32131fSŁukasz Stelmach 	writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
14913f32131fSŁukasz Stelmach 	       S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
14923f32131fSŁukasz Stelmach 	       sdd->regs + S3C64XX_SPI_INT_EN);
14933f32131fSŁukasz Stelmach 
1494b97b6621SMark Brown 	return 0;
14957990b008SAndi Shyti 
14967990b008SAndi Shyti err_disable_src_clk:
14977990b008SAndi Shyti 	clk_disable_unprepare(sdd->src_clk);
14987990b008SAndi Shyti err_disable_ioclk:
14997990b008SAndi Shyti 	clk_disable_unprepare(sdd->ioclk);
15007990b008SAndi Shyti 
15017990b008SAndi Shyti 	return ret;
1502b97b6621SMark Brown }
1503ec833050SRafael J. Wysocki #endif /* CONFIG_PM */
1504b97b6621SMark Brown 
1505e25d0bf9SMark Brown static const struct dev_pm_ops s3c64xx_spi_pm = {
1506e25d0bf9SMark Brown 	SET_SYSTEM_SLEEP_PM_OPS(s3c64xx_spi_suspend, s3c64xx_spi_resume)
1507b97b6621SMark Brown 	SET_RUNTIME_PM_OPS(s3c64xx_spi_runtime_suspend,
1508b97b6621SMark Brown 			   s3c64xx_spi_runtime_resume, NULL)
1509e25d0bf9SMark Brown };
1510e25d0bf9SMark Brown 
1511d6371415SKrzysztof Kozlowski static const struct s3c64xx_spi_port_config s3c2443_spi_port_config = {
1512ad0adac8STudor Ambarus 	/* fifo_lvl_mask is deprecated. Use {rx, tx}_fifomask instead. */
1513a5238e36SThomas Abraham 	.fifo_lvl_mask	= { 0x7f },
1514ad0adac8STudor Ambarus 	/* rx_lvl_offset is deprecated. Use {rx, tx}_fifomask instead. */
1515a5238e36SThomas Abraham 	.rx_lvl_offset	= 13,
1516a5238e36SThomas Abraham 	.tx_st_done	= 21,
1517bfcd27dcSChanho Park 	.clk_div	= 2,
1518a5238e36SThomas Abraham 	.high_speed	= true,
1519a5238e36SThomas Abraham };
1520a5238e36SThomas Abraham 
1521d6371415SKrzysztof Kozlowski static const struct s3c64xx_spi_port_config s3c6410_spi_port_config = {
1522ad0adac8STudor Ambarus 	/* fifo_lvl_mask is deprecated. Use {rx, tx}_fifomask instead. */
1523a5238e36SThomas Abraham 	.fifo_lvl_mask	= { 0x7f, 0x7F },
1524ad0adac8STudor Ambarus 	/* rx_lvl_offset is deprecated. Use {rx, tx}_fifomask instead. */
1525a5238e36SThomas Abraham 	.rx_lvl_offset	= 13,
1526a5238e36SThomas Abraham 	.tx_st_done	= 21,
1527bfcd27dcSChanho Park 	.clk_div	= 2,
1528a5238e36SThomas Abraham };
1529a5238e36SThomas Abraham 
1530d6371415SKrzysztof Kozlowski static const struct s3c64xx_spi_port_config s5pv210_spi_port_config = {
1531ad0adac8STudor Ambarus 	/* fifo_lvl_mask is deprecated. Use {rx, tx}_fifomask instead. */
1532a5238e36SThomas Abraham 	.fifo_lvl_mask	= { 0x1ff, 0x7F },
1533ad0adac8STudor Ambarus 	/* rx_lvl_offset is deprecated. Use {rx, tx}_fifomask instead. */
1534a5238e36SThomas Abraham 	.rx_lvl_offset	= 15,
1535a5238e36SThomas Abraham 	.tx_st_done	= 25,
1536bfcd27dcSChanho Park 	.clk_div	= 2,
1537a5238e36SThomas Abraham 	.high_speed	= true,
1538a5238e36SThomas Abraham };
1539a5238e36SThomas Abraham 
1540d6371415SKrzysztof Kozlowski static const struct s3c64xx_spi_port_config exynos4_spi_port_config = {
1541ad0adac8STudor Ambarus 	/* fifo_lvl_mask is deprecated. Use {rx, tx}_fifomask instead. */
1542a5238e36SThomas Abraham 	.fifo_lvl_mask	= { 0x1ff, 0x7F, 0x7F },
1543ad0adac8STudor Ambarus 	/* rx_lvl_offset is deprecated. Use {rx, tx}_fifomask instead. */
1544a5238e36SThomas Abraham 	.rx_lvl_offset	= 15,
1545a5238e36SThomas Abraham 	.tx_st_done	= 25,
1546bfcd27dcSChanho Park 	.clk_div	= 2,
1547a5238e36SThomas Abraham 	.high_speed	= true,
1548a5238e36SThomas Abraham 	.clk_from_cmu	= true,
1549ab4efca2SŁukasz Stelmach 	.quirks		= S3C64XX_SPI_QUIRK_CS_AUTO,
1550a5238e36SThomas Abraham };
1551a5238e36SThomas Abraham 
1552d6371415SKrzysztof Kozlowski static const struct s3c64xx_spi_port_config exynos7_spi_port_config = {
1553ad0adac8STudor Ambarus 	/* fifo_lvl_mask is deprecated. Use {rx, tx}_fifomask instead. */
1554bf77cba9SPadmavathi Venna 	.fifo_lvl_mask	= { 0x1ff, 0x7F, 0x7F, 0x7F, 0x7F, 0x1ff},
1555ad0adac8STudor Ambarus 	/* rx_lvl_offset is deprecated. Use {rx, tx}_fifomask instead. */
1556bf77cba9SPadmavathi Venna 	.rx_lvl_offset	= 15,
1557bf77cba9SPadmavathi Venna 	.tx_st_done	= 25,
1558bfcd27dcSChanho Park 	.clk_div	= 2,
1559bf77cba9SPadmavathi Venna 	.high_speed	= true,
1560bf77cba9SPadmavathi Venna 	.clk_from_cmu	= true,
1561bf77cba9SPadmavathi Venna 	.quirks		= S3C64XX_SPI_QUIRK_CS_AUTO,
1562bf77cba9SPadmavathi Venna };
1563bf77cba9SPadmavathi Venna 
1564d6371415SKrzysztof Kozlowski static const struct s3c64xx_spi_port_config exynos5433_spi_port_config = {
1565ad0adac8STudor Ambarus 	/* fifo_lvl_mask is deprecated. Use {rx, tx}_fifomask instead. */
15667990b008SAndi Shyti 	.fifo_lvl_mask	= { 0x1ff, 0x7f, 0x7f, 0x7f, 0x7f, 0x1ff},
1567ad0adac8STudor Ambarus 	/* rx_lvl_offset is deprecated. Use {rx, tx}_fifomask instead. */
15687990b008SAndi Shyti 	.rx_lvl_offset	= 15,
15697990b008SAndi Shyti 	.tx_st_done	= 25,
1570bfcd27dcSChanho Park 	.clk_div	= 2,
15717990b008SAndi Shyti 	.high_speed	= true,
15727990b008SAndi Shyti 	.clk_from_cmu	= true,
15737990b008SAndi Shyti 	.clk_ioclk	= true,
15747990b008SAndi Shyti 	.quirks		= S3C64XX_SPI_QUIRK_CS_AUTO,
15757990b008SAndi Shyti };
15767990b008SAndi Shyti 
15770229278bSSam Protsenko static const struct s3c64xx_spi_port_config exynos850_spi_port_config = {
15787ad28820STudor Ambarus 	.fifo_depth	= 64,
15797ad28820STudor Ambarus 	.rx_fifomask	= S3C64XX_SPI_ST_RX_FIFO_RDY_V2,
15807ad28820STudor Ambarus 	.tx_fifomask	= S3C64XX_SPI_ST_TX_FIFO_RDY_V2,
15810229278bSSam Protsenko 	.tx_st_done	= 25,
15820229278bSSam Protsenko 	.clk_div	= 4,
15830229278bSSam Protsenko 	.high_speed	= true,
15840229278bSSam Protsenko 	.clk_from_cmu	= true,
15850229278bSSam Protsenko 	.has_loopback	= true,
15860229278bSSam Protsenko 	.quirks		= S3C64XX_SPI_QUIRK_CS_AUTO,
15870229278bSSam Protsenko };
15880229278bSSam Protsenko 
158911d50d85SChanho Park static const struct s3c64xx_spi_port_config exynosautov9_spi_port_config = {
1590ad0adac8STudor Ambarus 	/* fifo_lvl_mask is deprecated. Use {rx, tx}_fifomask instead. */
159111d50d85SChanho Park 	.fifo_lvl_mask	= { 0x1ff, 0x1ff, 0x7f, 0x7f, 0x7f, 0x7f, 0x1ff, 0x7f,
159211d50d85SChanho Park 			    0x7f, 0x7f, 0x7f, 0x7f},
1593ad0adac8STudor Ambarus 	/* rx_lvl_offset is deprecated. Use {rx, tx}_fifomask instead. */
159411d50d85SChanho Park 	.rx_lvl_offset	= 15,
159511d50d85SChanho Park 	.tx_st_done	= 25,
159611d50d85SChanho Park 	.clk_div	= 4,
159711d50d85SChanho Park 	.high_speed	= true,
159811d50d85SChanho Park 	.clk_from_cmu	= true,
159911d50d85SChanho Park 	.clk_ioclk	= true,
160011d50d85SChanho Park 	.has_loopback	= true,
160111d50d85SChanho Park 	.quirks		= S3C64XX_SPI_QUIRK_CS_AUTO,
160211d50d85SChanho Park };
160311d50d85SChanho Park 
1604a813c47dSKrzysztof Kozlowski static const struct s3c64xx_spi_port_config fsd_spi_port_config = {
1605ad0adac8STudor Ambarus 	/* fifo_lvl_mask is deprecated. Use {rx, tx}_fifomask instead. */
16064ebb15a1SAlim Akhtar 	.fifo_lvl_mask	= { 0x7f, 0x7f, 0x7f, 0x7f, 0x7f},
1607ad0adac8STudor Ambarus 	/* rx_lvl_offset is deprecated. Use {rx, tx}_fifomask instead. */
16084ebb15a1SAlim Akhtar 	.rx_lvl_offset	= 15,
16094ebb15a1SAlim Akhtar 	.tx_st_done	= 25,
1610bfcd27dcSChanho Park 	.clk_div	= 2,
16114ebb15a1SAlim Akhtar 	.high_speed	= true,
16124ebb15a1SAlim Akhtar 	.clk_from_cmu	= true,
16134ebb15a1SAlim Akhtar 	.clk_ioclk	= false,
16144ebb15a1SAlim Akhtar 	.quirks		= S3C64XX_SPI_QUIRK_CS_AUTO,
16154ebb15a1SAlim Akhtar };
16164ebb15a1SAlim Akhtar 
1617e010c049STudor Ambarus static const struct s3c64xx_spi_port_config gs101_spi_port_config = {
1618e8b16c7aSTudor Ambarus 	.fifo_depth	= 64,
1619e8b16c7aSTudor Ambarus 	.rx_fifomask	= S3C64XX_SPI_ST_RX_FIFO_RDY_V2,
1620e8b16c7aSTudor Ambarus 	.tx_fifomask	= S3C64XX_SPI_ST_TX_FIFO_RDY_V2,
1621e010c049STudor Ambarus 	.tx_st_done	= 25,
1622e010c049STudor Ambarus 	.clk_div	= 4,
1623e010c049STudor Ambarus 	.high_speed	= true,
1624e010c049STudor Ambarus 	.clk_from_cmu	= true,
1625e010c049STudor Ambarus 	.has_loopback	= true,
1626e010c049STudor Ambarus 	.use_32bit_io	= true,
1627e010c049STudor Ambarus 	.quirks		= S3C64XX_SPI_QUIRK_CS_AUTO,
1628e010c049STudor Ambarus };
1629e010c049STudor Ambarus 
163023f6d39eSKrzysztof Kozlowski static const struct platform_device_id s3c64xx_spi_driver_ids[] = {
1631a5238e36SThomas Abraham 	{
1632a5238e36SThomas Abraham 		.name		= "s3c2443-spi",
1633a5238e36SThomas Abraham 		.driver_data	= (kernel_ulong_t)&s3c2443_spi_port_config,
1634a5238e36SThomas Abraham 	}, {
1635a5238e36SThomas Abraham 		.name		= "s3c6410-spi",
1636a5238e36SThomas Abraham 		.driver_data	= (kernel_ulong_t)&s3c6410_spi_port_config,
1637a5238e36SThomas Abraham 	},
1638a5238e36SThomas Abraham 	{ },
1639a5238e36SThomas Abraham };
1640aa6e8296SJinjie Ruan MODULE_DEVICE_TABLE(platform, s3c64xx_spi_driver_ids);
1641a5238e36SThomas Abraham 
16422b908075SThomas Abraham static const struct of_device_id s3c64xx_spi_dt_match[] = {
1643e010c049STudor Ambarus 	{ .compatible = "google,gs101-spi",
1644e010c049STudor Ambarus 			.data = &gs101_spi_port_config,
1645e010c049STudor Ambarus 	},
1646a3b924dfSMateusz Krawczuk 	{ .compatible = "samsung,s3c2443-spi",
1647271f1881STudor Ambarus 			.data = &s3c2443_spi_port_config,
1648a3b924dfSMateusz Krawczuk 	},
1649a3b924dfSMateusz Krawczuk 	{ .compatible = "samsung,s3c6410-spi",
1650271f1881STudor Ambarus 			.data = &s3c6410_spi_port_config,
1651a3b924dfSMateusz Krawczuk 	},
1652a3b924dfSMateusz Krawczuk 	{ .compatible = "samsung,s5pv210-spi",
1653271f1881STudor Ambarus 			.data = &s5pv210_spi_port_config,
1654a3b924dfSMateusz Krawczuk 	},
16552b908075SThomas Abraham 	{ .compatible = "samsung,exynos4210-spi",
1656271f1881STudor Ambarus 			.data = &exynos4_spi_port_config,
16572b908075SThomas Abraham 	},
1658bf77cba9SPadmavathi Venna 	{ .compatible = "samsung,exynos7-spi",
1659271f1881STudor Ambarus 			.data = &exynos7_spi_port_config,
1660bf77cba9SPadmavathi Venna 	},
16617990b008SAndi Shyti 	{ .compatible = "samsung,exynos5433-spi",
1662271f1881STudor Ambarus 			.data = &exynos5433_spi_port_config,
16637990b008SAndi Shyti 	},
16640229278bSSam Protsenko 	{ .compatible = "samsung,exynos850-spi",
1665271f1881STudor Ambarus 			.data = &exynos850_spi_port_config,
16660229278bSSam Protsenko 	},
166711d50d85SChanho Park 	{ .compatible = "samsung,exynosautov9-spi",
1668271f1881STudor Ambarus 			.data = &exynosautov9_spi_port_config,
166911d50d85SChanho Park 	},
16704ebb15a1SAlim Akhtar 	{ .compatible = "tesla,fsd-spi",
1671271f1881STudor Ambarus 			.data = &fsd_spi_port_config,
16724ebb15a1SAlim Akhtar 	},
16732b908075SThomas Abraham 	{ },
16742b908075SThomas Abraham };
16752b908075SThomas Abraham MODULE_DEVICE_TABLE(of, s3c64xx_spi_dt_match);
16762b908075SThomas Abraham 
1677ca632f55SGrant Likely static struct platform_driver s3c64xx_spi_driver = {
1678ca632f55SGrant Likely 	.driver = {
1679ca632f55SGrant Likely 		.name	= "s3c64xx-spi",
1680e25d0bf9SMark Brown 		.pm = &s3c64xx_spi_pm,
16812b908075SThomas Abraham 		.of_match_table = of_match_ptr(s3c64xx_spi_dt_match),
1682ca632f55SGrant Likely 	},
168350c959fcSLukasz Czerwinski 	.probe = s3c64xx_spi_probe,
16842b7981a6SUwe Kleine-König 	.remove_new = s3c64xx_spi_remove,
1685a5238e36SThomas Abraham 	.id_table = s3c64xx_spi_driver_ids,
1686ca632f55SGrant Likely };
1687ca632f55SGrant Likely MODULE_ALIAS("platform:s3c64xx-spi");
1688ca632f55SGrant Likely 
168950c959fcSLukasz Czerwinski module_platform_driver(s3c64xx_spi_driver);
1690ca632f55SGrant Likely 
1691ca632f55SGrant Likely MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
1692ca632f55SGrant Likely MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
1693ca632f55SGrant Likely MODULE_LICENSE("GPL");
1694