xref: /openbmc/linux/drivers/spi/spi-sh-msiof.c (revision 96d3c5a7d20ec546e44695983fe0508c6f904248)
19135bac3SWolfram Sang // SPDX-License-Identifier: GPL-2.0
2ca632f55SGrant Likely /*
335c35fd9SGeert Uytterhoeven  * SuperH MSIOF SPI Controller Interface
4ca632f55SGrant Likely  *
5ca632f55SGrant Likely  * Copyright (c) 2009 Magnus Damm
6cf9e4784SHisashi Nakamura  * Copyright (C) 2014 Renesas Electronics Corporation
7cf9e4784SHisashi Nakamura  * Copyright (C) 2014-2017 Glider bvba
8ca632f55SGrant Likely  */
9ca632f55SGrant Likely 
10ca632f55SGrant Likely #include <linux/bitmap.h>
11ca632f55SGrant Likely #include <linux/clk.h>
12ca632f55SGrant Likely #include <linux/completion.h>
13ca632f55SGrant Likely #include <linux/delay.h>
14b0d0ce8bSGeert Uytterhoeven #include <linux/dma-mapping.h>
15b0d0ce8bSGeert Uytterhoeven #include <linux/dmaengine.h>
16ca632f55SGrant Likely #include <linux/err.h>
17ca632f55SGrant Likely #include <linux/interrupt.h>
18ca632f55SGrant Likely #include <linux/io.h>
199115b4d8SGeert Uytterhoeven #include <linux/iopoll.h>
20ca632f55SGrant Likely #include <linux/kernel.h>
21d7614de4SPaul Gortmaker #include <linux/module.h>
22cf9c86efSBastian Hecht #include <linux/of.h>
23ca632f55SGrant Likely #include <linux/platform_device.h>
24ca632f55SGrant Likely #include <linux/pm_runtime.h>
25b0d0ce8bSGeert Uytterhoeven #include <linux/sh_dma.h>
26ca632f55SGrant Likely 
27ca632f55SGrant Likely #include <linux/spi/sh_msiof.h>
28ca632f55SGrant Likely #include <linux/spi/spi.h>
29ca632f55SGrant Likely 
30ca632f55SGrant Likely #include <asm/unaligned.h>
31ca632f55SGrant Likely 
32ed492c47SWolfram Sang #define SH_MSIOF_FLAG_FIXED_DTDL_200	BIT(0)
33ed492c47SWolfram Sang 
3450a7e23fSGeert Uytterhoeven struct sh_msiof_chipdata {
350e836c3bSGeert Uytterhoeven 	u32 bits_per_word_mask;
3650a7e23fSGeert Uytterhoeven 	u16 tx_fifo_size;
3750a7e23fSGeert Uytterhoeven 	u16 rx_fifo_size;
3835c35fd9SGeert Uytterhoeven 	u16 ctlr_flags;
3951093cbaSVladimir Zapolskiy 	u16 min_div_pow;
40ed492c47SWolfram Sang 	u32 flags;
4150a7e23fSGeert Uytterhoeven };
4250a7e23fSGeert Uytterhoeven 
43ca632f55SGrant Likely struct sh_msiof_spi_priv {
4435c35fd9SGeert Uytterhoeven 	struct spi_controller *ctlr;
45ca632f55SGrant Likely 	void __iomem *mapbase;
46ca632f55SGrant Likely 	struct clk *clk;
47ca632f55SGrant Likely 	struct platform_device *pdev;
48ca632f55SGrant Likely 	struct sh_msiof_spi_info *info;
49ca632f55SGrant Likely 	struct completion done;
5008ba7ae3SGeert Uytterhoeven 	struct completion done_txdma;
51fe78d0b7SKoji Matsuoka 	unsigned int tx_fifo_size;
52fe78d0b7SKoji Matsuoka 	unsigned int rx_fifo_size;
5351093cbaSVladimir Zapolskiy 	unsigned int min_div_pow;
54b0d0ce8bSGeert Uytterhoeven 	void *tx_dma_page;
55b0d0ce8bSGeert Uytterhoeven 	void *rx_dma_page;
56b0d0ce8bSGeert Uytterhoeven 	dma_addr_t tx_dma_addr;
57b0d0ce8bSGeert Uytterhoeven 	dma_addr_t rx_dma_addr;
587ff0b53cSGeert Uytterhoeven 	bool native_cs_inited;
597ff0b53cSGeert Uytterhoeven 	bool native_cs_high;
601cb3ebc4SYang Yingliang 	bool target_aborted;
61ca632f55SGrant Likely };
62ca632f55SGrant Likely 
639cce882bSGeert Uytterhoeven #define MAX_SS	3	/* Maximum number of native chip selects */
649cce882bSGeert Uytterhoeven 
658ae7d442SKrzysztof Kozlowski #define SITMDR1	0x00	/* Transmit Mode Register 1 */
668ae7d442SKrzysztof Kozlowski #define SITMDR2	0x04	/* Transmit Mode Register 2 */
678ae7d442SKrzysztof Kozlowski #define SITMDR3	0x08	/* Transmit Mode Register 3 */
688ae7d442SKrzysztof Kozlowski #define SIRMDR1	0x10	/* Receive Mode Register 1 */
698ae7d442SKrzysztof Kozlowski #define SIRMDR2	0x14	/* Receive Mode Register 2 */
708ae7d442SKrzysztof Kozlowski #define SIRMDR3	0x18	/* Receive Mode Register 3 */
718ae7d442SKrzysztof Kozlowski #define SITSCR	0x20	/* Transmit Clock Select Register */
728ae7d442SKrzysztof Kozlowski #define SIRSCR	0x22	/* Receive Clock Select Register (SH, A1, APE6) */
738ae7d442SKrzysztof Kozlowski #define SICTR	0x28	/* Control Register */
748ae7d442SKrzysztof Kozlowski #define SIFCTR	0x30	/* FIFO Control Register */
758ae7d442SKrzysztof Kozlowski #define SISTR	0x40	/* Status Register */
768ae7d442SKrzysztof Kozlowski #define SIIER	0x44	/* Interrupt Enable Register */
778ae7d442SKrzysztof Kozlowski #define SITDR1	0x48	/* Transmit Control Data Register 1 (SH, A1) */
788ae7d442SKrzysztof Kozlowski #define SITDR2	0x4c	/* Transmit Control Data Register 2 (SH, A1) */
798ae7d442SKrzysztof Kozlowski #define SITFDR	0x50	/* Transmit FIFO Data Register */
808ae7d442SKrzysztof Kozlowski #define SIRDR1	0x58	/* Receive Control Data Register 1 (SH, A1) */
818ae7d442SKrzysztof Kozlowski #define SIRDR2	0x5c	/* Receive Control Data Register 2 (SH, A1) */
828ae7d442SKrzysztof Kozlowski #define SIRFDR	0x60	/* Receive FIFO Data Register */
83ca632f55SGrant Likely 
848ae7d442SKrzysztof Kozlowski /* SITMDR1 and SIRMDR1 */
858ae7d442SKrzysztof Kozlowski #define SIMDR1_TRMD		BIT(31)		/* Transfer Mode (1 = Master mode) */
868ae7d442SKrzysztof Kozlowski #define SIMDR1_SYNCMD_MASK	GENMASK(29, 28)	/* SYNC Mode */
878ae7d442SKrzysztof Kozlowski #define SIMDR1_SYNCMD_SPI	(2 << 28)	/*   Level mode/SPI */
888ae7d442SKrzysztof Kozlowski #define SIMDR1_SYNCMD_LR	(3 << 28)	/*   L/R mode */
898ae7d442SKrzysztof Kozlowski #define SIMDR1_SYNCAC_SHIFT	25		/* Sync Polarity (1 = Active-low) */
908ae7d442SKrzysztof Kozlowski #define SIMDR1_BITLSB_SHIFT	24		/* MSB/LSB First (1 = LSB first) */
918ae7d442SKrzysztof Kozlowski #define SIMDR1_DTDL_SHIFT	20		/* Data Pin Bit Delay for MSIOF_SYNC */
928ae7d442SKrzysztof Kozlowski #define SIMDR1_SYNCDL_SHIFT	16		/* Frame Sync Signal Timing Delay */
938ae7d442SKrzysztof Kozlowski #define SIMDR1_FLD_MASK		GENMASK(3, 2)	/* Frame Sync Signal Interval (0-3) */
948ae7d442SKrzysztof Kozlowski #define SIMDR1_FLD_SHIFT	2
958ae7d442SKrzysztof Kozlowski #define SIMDR1_XXSTP		BIT(0)		/* Transmission/Reception Stop on FIFO */
968ae7d442SKrzysztof Kozlowski /* SITMDR1 */
978ae7d442SKrzysztof Kozlowski #define SITMDR1_PCON		BIT(30)		/* Transfer Signal Connection */
988ae7d442SKrzysztof Kozlowski #define SITMDR1_SYNCCH_MASK	GENMASK(27, 26)	/* Sync Signal Channel Select */
998ae7d442SKrzysztof Kozlowski #define SITMDR1_SYNCCH_SHIFT	26		/* 0=MSIOF_SYNC, 1=MSIOF_SS1, 2=MSIOF_SS2 */
100ca632f55SGrant Likely 
1018ae7d442SKrzysztof Kozlowski /* SITMDR2 and SIRMDR2 */
1028ae7d442SKrzysztof Kozlowski #define SIMDR2_BITLEN1(i)	(((i) - 1) << 24) /* Data Size (8-32 bits) */
1038ae7d442SKrzysztof Kozlowski #define SIMDR2_WDLEN1(i)	(((i) - 1) << 16) /* Word Count (1-64/256 (SH, A1))) */
1048ae7d442SKrzysztof Kozlowski #define SIMDR2_GRPMASK1		BIT(0)		/* Group Output Mask 1 (SH, A1) */
10501cfef57SGeert Uytterhoeven 
1068ae7d442SKrzysztof Kozlowski /* SITSCR and SIRSCR */
1078ae7d442SKrzysztof Kozlowski #define SISCR_BRPS_MASK		GENMASK(12, 8)	/* Prescaler Setting (1-32) */
1088ae7d442SKrzysztof Kozlowski #define SISCR_BRPS(i)		(((i) - 1) << 8)
1098ae7d442SKrzysztof Kozlowski #define SISCR_BRDV_MASK		GENMASK(2, 0)	/* Baud Rate Generator's Division Ratio */
1108ae7d442SKrzysztof Kozlowski #define SISCR_BRDV_DIV_2	0
1118ae7d442SKrzysztof Kozlowski #define SISCR_BRDV_DIV_4	1
1128ae7d442SKrzysztof Kozlowski #define SISCR_BRDV_DIV_8	2
1138ae7d442SKrzysztof Kozlowski #define SISCR_BRDV_DIV_16	3
1148ae7d442SKrzysztof Kozlowski #define SISCR_BRDV_DIV_32	4
1158ae7d442SKrzysztof Kozlowski #define SISCR_BRDV_DIV_1	7
11601cfef57SGeert Uytterhoeven 
1178ae7d442SKrzysztof Kozlowski /* SICTR */
1188ae7d442SKrzysztof Kozlowski #define SICTR_TSCKIZ_MASK	GENMASK(31, 30)	/* Transmit Clock I/O Polarity Select */
1198ae7d442SKrzysztof Kozlowski #define SICTR_TSCKIZ_SCK	BIT(31)		/*   Disable SCK when TX disabled */
1208ae7d442SKrzysztof Kozlowski #define SICTR_TSCKIZ_POL_SHIFT	30		/*   Transmit Clock Polarity */
1218ae7d442SKrzysztof Kozlowski #define SICTR_RSCKIZ_MASK	GENMASK(29, 28) /* Receive Clock Polarity Select */
1228ae7d442SKrzysztof Kozlowski #define SICTR_RSCKIZ_SCK	BIT(29)		/*   Must match CTR_TSCKIZ_SCK */
1238ae7d442SKrzysztof Kozlowski #define SICTR_RSCKIZ_POL_SHIFT	28		/*   Receive Clock Polarity */
1248ae7d442SKrzysztof Kozlowski #define SICTR_TEDG_SHIFT	27		/* Transmit Timing (1 = falling edge) */
1258ae7d442SKrzysztof Kozlowski #define SICTR_REDG_SHIFT	26		/* Receive Timing (1 = falling edge) */
1268ae7d442SKrzysztof Kozlowski #define SICTR_TXDIZ_MASK	GENMASK(23, 22)	/* Pin Output When TX is Disabled */
1278ae7d442SKrzysztof Kozlowski #define SICTR_TXDIZ_LOW		(0 << 22)	/*   0 */
1288ae7d442SKrzysztof Kozlowski #define SICTR_TXDIZ_HIGH	(1 << 22)	/*   1 */
1298ae7d442SKrzysztof Kozlowski #define SICTR_TXDIZ_HIZ		(2 << 22)	/*   High-impedance */
1308ae7d442SKrzysztof Kozlowski #define SICTR_TSCKE		BIT(15)		/* Transmit Serial Clock Output Enable */
1318ae7d442SKrzysztof Kozlowski #define SICTR_TFSE		BIT(14)		/* Transmit Frame Sync Signal Output Enable */
1328ae7d442SKrzysztof Kozlowski #define SICTR_TXE		BIT(9)		/* Transmit Enable */
1338ae7d442SKrzysztof Kozlowski #define SICTR_RXE		BIT(8)		/* Receive Enable */
1348ae7d442SKrzysztof Kozlowski #define SICTR_TXRST		BIT(1)		/* Transmit Reset */
1358ae7d442SKrzysztof Kozlowski #define SICTR_RXRST		BIT(0)		/* Receive Reset */
13601cfef57SGeert Uytterhoeven 
1378ae7d442SKrzysztof Kozlowski /* SIFCTR */
1388ae7d442SKrzysztof Kozlowski #define SIFCTR_TFWM_MASK	GENMASK(31, 29)	/* Transmit FIFO Watermark */
139*2c889761SWolfram Sang #define SIFCTR_TFWM_64		(0UL << 29)	/*  Transfer Request when 64 empty stages */
140*2c889761SWolfram Sang #define SIFCTR_TFWM_32		(1UL << 29)	/*  Transfer Request when 32 empty stages */
141*2c889761SWolfram Sang #define SIFCTR_TFWM_24		(2UL << 29)	/*  Transfer Request when 24 empty stages */
142*2c889761SWolfram Sang #define SIFCTR_TFWM_16		(3UL << 29)	/*  Transfer Request when 16 empty stages */
143*2c889761SWolfram Sang #define SIFCTR_TFWM_12		(4UL << 29)	/*  Transfer Request when 12 empty stages */
144*2c889761SWolfram Sang #define SIFCTR_TFWM_8		(5UL << 29)	/*  Transfer Request when 8 empty stages */
145*2c889761SWolfram Sang #define SIFCTR_TFWM_4		(6UL << 29)	/*  Transfer Request when 4 empty stages */
146*2c889761SWolfram Sang #define SIFCTR_TFWM_1		(7UL << 29)	/*  Transfer Request when 1 empty stage */
1478ae7d442SKrzysztof Kozlowski #define SIFCTR_TFUA_MASK	GENMASK(26, 20) /* Transmit FIFO Usable Area */
1488ae7d442SKrzysztof Kozlowski #define SIFCTR_TFUA_SHIFT	20
1498ae7d442SKrzysztof Kozlowski #define SIFCTR_TFUA(i)		((i) << SIFCTR_TFUA_SHIFT)
1508ae7d442SKrzysztof Kozlowski #define SIFCTR_RFWM_MASK	GENMASK(15, 13)	/* Receive FIFO Watermark */
1518ae7d442SKrzysztof Kozlowski #define SIFCTR_RFWM_1		(0 << 13)	/*  Transfer Request when 1 valid stages */
1528ae7d442SKrzysztof Kozlowski #define SIFCTR_RFWM_4		(1 << 13)	/*  Transfer Request when 4 valid stages */
1538ae7d442SKrzysztof Kozlowski #define SIFCTR_RFWM_8		(2 << 13)	/*  Transfer Request when 8 valid stages */
1548ae7d442SKrzysztof Kozlowski #define SIFCTR_RFWM_16		(3 << 13)	/*  Transfer Request when 16 valid stages */
1558ae7d442SKrzysztof Kozlowski #define SIFCTR_RFWM_32		(4 << 13)	/*  Transfer Request when 32 valid stages */
1568ae7d442SKrzysztof Kozlowski #define SIFCTR_RFWM_64		(5 << 13)	/*  Transfer Request when 64 valid stages */
1578ae7d442SKrzysztof Kozlowski #define SIFCTR_RFWM_128		(6 << 13)	/*  Transfer Request when 128 valid stages */
1588ae7d442SKrzysztof Kozlowski #define SIFCTR_RFWM_256		(7 << 13)	/*  Transfer Request when 256 valid stages */
1598ae7d442SKrzysztof Kozlowski #define SIFCTR_RFUA_MASK	GENMASK(12, 4)	/* Receive FIFO Usable Area (0x40 = full) */
1608ae7d442SKrzysztof Kozlowski #define SIFCTR_RFUA_SHIFT	4
1618ae7d442SKrzysztof Kozlowski #define SIFCTR_RFUA(i)		((i) << SIFCTR_RFUA_SHIFT)
1622e2b3687SGeert Uytterhoeven 
1638ae7d442SKrzysztof Kozlowski /* SISTR */
1648ae7d442SKrzysztof Kozlowski #define SISTR_TFEMP		BIT(29) /* Transmit FIFO Empty */
1658ae7d442SKrzysztof Kozlowski #define SISTR_TDREQ		BIT(28) /* Transmit Data Transfer Request */
1668ae7d442SKrzysztof Kozlowski #define SISTR_TEOF		BIT(23) /* Frame Transmission End */
1678ae7d442SKrzysztof Kozlowski #define SISTR_TFSERR		BIT(21) /* Transmit Frame Synchronization Error */
1688ae7d442SKrzysztof Kozlowski #define SISTR_TFOVF		BIT(20) /* Transmit FIFO Overflow */
1698ae7d442SKrzysztof Kozlowski #define SISTR_TFUDF		BIT(19) /* Transmit FIFO Underflow */
1708ae7d442SKrzysztof Kozlowski #define SISTR_RFFUL		BIT(13) /* Receive FIFO Full */
1718ae7d442SKrzysztof Kozlowski #define SISTR_RDREQ		BIT(12) /* Receive Data Transfer Request */
1728ae7d442SKrzysztof Kozlowski #define SISTR_REOF		BIT(7)  /* Frame Reception End */
1738ae7d442SKrzysztof Kozlowski #define SISTR_RFSERR		BIT(5)  /* Receive Frame Synchronization Error */
1748ae7d442SKrzysztof Kozlowski #define SISTR_RFUDF		BIT(4)  /* Receive FIFO Underflow */
1758ae7d442SKrzysztof Kozlowski #define SISTR_RFOVF		BIT(3)  /* Receive FIFO Overflow */
1762e2b3687SGeert Uytterhoeven 
1778ae7d442SKrzysztof Kozlowski /* SIIER */
1788ae7d442SKrzysztof Kozlowski #define SIIER_TDMAE		BIT(31) /* Transmit Data DMA Transfer Req. Enable */
1798ae7d442SKrzysztof Kozlowski #define SIIER_TFEMPE		BIT(29) /* Transmit FIFO Empty Enable */
1808ae7d442SKrzysztof Kozlowski #define SIIER_TDREQE		BIT(28) /* Transmit Data Transfer Request Enable */
1818ae7d442SKrzysztof Kozlowski #define SIIER_TEOFE		BIT(23) /* Frame Transmission End Enable */
1828ae7d442SKrzysztof Kozlowski #define SIIER_TFSERRE		BIT(21) /* Transmit Frame Sync Error Enable */
1838ae7d442SKrzysztof Kozlowski #define SIIER_TFOVFE		BIT(20) /* Transmit FIFO Overflow Enable */
1848ae7d442SKrzysztof Kozlowski #define SIIER_TFUDFE		BIT(19) /* Transmit FIFO Underflow Enable */
1858ae7d442SKrzysztof Kozlowski #define SIIER_RDMAE		BIT(15) /* Receive Data DMA Transfer Req. Enable */
1868ae7d442SKrzysztof Kozlowski #define SIIER_RFFULE		BIT(13) /* Receive FIFO Full Enable */
1878ae7d442SKrzysztof Kozlowski #define SIIER_RDREQE		BIT(12) /* Receive Data Transfer Request Enable */
1888ae7d442SKrzysztof Kozlowski #define SIIER_REOFE		BIT(7)  /* Frame Reception End Enable */
1898ae7d442SKrzysztof Kozlowski #define SIIER_RFSERRE		BIT(5)  /* Receive Frame Sync Error Enable */
1908ae7d442SKrzysztof Kozlowski #define SIIER_RFUDFE		BIT(4)  /* Receive FIFO Underflow Enable */
1918ae7d442SKrzysztof Kozlowski #define SIIER_RFOVFE		BIT(3)  /* Receive FIFO Overflow Enable */
19201cfef57SGeert Uytterhoeven 
193ca632f55SGrant Likely 
sh_msiof_read(struct sh_msiof_spi_priv * p,int reg_offs)194ca632f55SGrant Likely static u32 sh_msiof_read(struct sh_msiof_spi_priv *p, int reg_offs)
195ca632f55SGrant Likely {
196ca632f55SGrant Likely 	switch (reg_offs) {
1978ae7d442SKrzysztof Kozlowski 	case SITSCR:
1988ae7d442SKrzysztof Kozlowski 	case SIRSCR:
199ca632f55SGrant Likely 		return ioread16(p->mapbase + reg_offs);
200ca632f55SGrant Likely 	default:
201ca632f55SGrant Likely 		return ioread32(p->mapbase + reg_offs);
202ca632f55SGrant Likely 	}
203ca632f55SGrant Likely }
204ca632f55SGrant Likely 
sh_msiof_write(struct sh_msiof_spi_priv * p,int reg_offs,u32 value)205ca632f55SGrant Likely static void sh_msiof_write(struct sh_msiof_spi_priv *p, int reg_offs,
206ca632f55SGrant Likely 			   u32 value)
207ca632f55SGrant Likely {
208ca632f55SGrant Likely 	switch (reg_offs) {
2098ae7d442SKrzysztof Kozlowski 	case SITSCR:
2108ae7d442SKrzysztof Kozlowski 	case SIRSCR:
211ca632f55SGrant Likely 		iowrite16(value, p->mapbase + reg_offs);
212ca632f55SGrant Likely 		break;
213ca632f55SGrant Likely 	default:
214ca632f55SGrant Likely 		iowrite32(value, p->mapbase + reg_offs);
215ca632f55SGrant Likely 		break;
216ca632f55SGrant Likely 	}
217ca632f55SGrant Likely }
218ca632f55SGrant Likely 
sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv * p,u32 clr,u32 set)219ca632f55SGrant Likely static int sh_msiof_modify_ctr_wait(struct sh_msiof_spi_priv *p,
220ca632f55SGrant Likely 				    u32 clr, u32 set)
221ca632f55SGrant Likely {
222ca632f55SGrant Likely 	u32 mask = clr | set;
223ca632f55SGrant Likely 	u32 data;
224ca632f55SGrant Likely 
2258ae7d442SKrzysztof Kozlowski 	data = sh_msiof_read(p, SICTR);
226ca632f55SGrant Likely 	data &= ~clr;
227ca632f55SGrant Likely 	data |= set;
2288ae7d442SKrzysztof Kozlowski 	sh_msiof_write(p, SICTR, data);
229ca632f55SGrant Likely 
2308ae7d442SKrzysztof Kozlowski 	return readl_poll_timeout_atomic(p->mapbase + SICTR, data,
231635bdb7aSGeert Uytterhoeven 					 (data & mask) == set, 1, 100);
232ca632f55SGrant Likely }
233ca632f55SGrant Likely 
sh_msiof_spi_irq(int irq,void * data)234ca632f55SGrant Likely static irqreturn_t sh_msiof_spi_irq(int irq, void *data)
235ca632f55SGrant Likely {
236ca632f55SGrant Likely 	struct sh_msiof_spi_priv *p = data;
237ca632f55SGrant Likely 
238ca632f55SGrant Likely 	/* just disable the interrupt and wake up */
2398ae7d442SKrzysztof Kozlowski 	sh_msiof_write(p, SIIER, 0);
240ca632f55SGrant Likely 	complete(&p->done);
241ca632f55SGrant Likely 
242ca632f55SGrant Likely 	return IRQ_HANDLED;
243ca632f55SGrant Likely }
244ca632f55SGrant Likely 
sh_msiof_spi_reset_regs(struct sh_msiof_spi_priv * p)245fedd6940SGeert Uytterhoeven static void sh_msiof_spi_reset_regs(struct sh_msiof_spi_priv *p)
246fedd6940SGeert Uytterhoeven {
2478ae7d442SKrzysztof Kozlowski 	u32 mask = SICTR_TXRST | SICTR_RXRST;
248fedd6940SGeert Uytterhoeven 	u32 data;
249fedd6940SGeert Uytterhoeven 
2508ae7d442SKrzysztof Kozlowski 	data = sh_msiof_read(p, SICTR);
251fedd6940SGeert Uytterhoeven 	data |= mask;
2528ae7d442SKrzysztof Kozlowski 	sh_msiof_write(p, SICTR, data);
253fedd6940SGeert Uytterhoeven 
2548ae7d442SKrzysztof Kozlowski 	readl_poll_timeout_atomic(p->mapbase + SICTR, data, !(data & mask), 1,
255fedd6940SGeert Uytterhoeven 				  100);
256fedd6940SGeert Uytterhoeven }
257fedd6940SGeert Uytterhoeven 
25851093cbaSVladimir Zapolskiy static const u32 sh_msiof_spi_div_array[] = {
2598ae7d442SKrzysztof Kozlowski 	SISCR_BRDV_DIV_1, SISCR_BRDV_DIV_2, SISCR_BRDV_DIV_4,
2608ae7d442SKrzysztof Kozlowski 	SISCR_BRDV_DIV_8, SISCR_BRDV_DIV_16, SISCR_BRDV_DIV_32,
261ca632f55SGrant Likely };
262ca632f55SGrant Likely 
sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv * p,struct spi_transfer * t)263ca632f55SGrant Likely static void sh_msiof_spi_set_clk_regs(struct sh_msiof_spi_priv *p,
2649a133f7bSGeert Uytterhoeven 				      struct spi_transfer *t)
265ca632f55SGrant Likely {
2669a133f7bSGeert Uytterhoeven 	unsigned long parent_rate = clk_get_rate(p->clk);
2679a133f7bSGeert Uytterhoeven 	unsigned int div_pow = p->min_div_pow;
2689a133f7bSGeert Uytterhoeven 	u32 spi_hz = t->speed_hz;
26951093cbaSVladimir Zapolskiy 	unsigned long div;
27065d5665bSNobuhiro Iwamatsu 	u32 brps, scr;
271ca632f55SGrant Likely 
27251093cbaSVladimir Zapolskiy 	if (!spi_hz || !parent_rate) {
27351093cbaSVladimir Zapolskiy 		WARN(1, "Invalid clock rate parameters %lu and %u\n",
27451093cbaSVladimir Zapolskiy 		     parent_rate, spi_hz);
27551093cbaSVladimir Zapolskiy 		return;
276ca632f55SGrant Likely 	}
277ca632f55SGrant Likely 
27851093cbaSVladimir Zapolskiy 	div = DIV_ROUND_UP(parent_rate, spi_hz);
27951093cbaSVladimir Zapolskiy 	if (div <= 1024) {
2808ae7d442SKrzysztof Kozlowski 		/* SISCR_BRDV_DIV_1 is valid only if BRPS is x 1/1 or x 1/2 */
28151093cbaSVladimir Zapolskiy 		if (!div_pow && div <= 32 && div > 2)
28251093cbaSVladimir Zapolskiy 			div_pow = 1;
283ca632f55SGrant Likely 
28451093cbaSVladimir Zapolskiy 		if (div_pow)
28551093cbaSVladimir Zapolskiy 			brps = (div + 1) >> div_pow;
28651093cbaSVladimir Zapolskiy 		else
28751093cbaSVladimir Zapolskiy 			brps = div;
28851093cbaSVladimir Zapolskiy 
28951093cbaSVladimir Zapolskiy 		for (; brps > 32; div_pow++)
29051093cbaSVladimir Zapolskiy 			brps = (brps + 1) >> 1;
29151093cbaSVladimir Zapolskiy 	} else {
29251093cbaSVladimir Zapolskiy 		/* Set transfer rate composite divisor to 2^5 * 32 = 1024 */
29351093cbaSVladimir Zapolskiy 		dev_err(&p->pdev->dev,
29451093cbaSVladimir Zapolskiy 			"Requested SPI transfer rate %d is too low\n", spi_hz);
29551093cbaSVladimir Zapolskiy 		div_pow = 5;
29651093cbaSVladimir Zapolskiy 		brps = 32;
29751093cbaSVladimir Zapolskiy 	}
29851093cbaSVladimir Zapolskiy 
2999a133f7bSGeert Uytterhoeven 	t->effective_speed_hz = parent_rate / (brps << div_pow);
3009a133f7bSGeert Uytterhoeven 
3018ae7d442SKrzysztof Kozlowski 	scr = sh_msiof_spi_div_array[div_pow] | SISCR_BRPS(brps);
3028ae7d442SKrzysztof Kozlowski 	sh_msiof_write(p, SITSCR, scr);
30335c35fd9SGeert Uytterhoeven 	if (!(p->ctlr->flags & SPI_CONTROLLER_MUST_TX))
3048ae7d442SKrzysztof Kozlowski 		sh_msiof_write(p, SIRSCR, scr);
305ca632f55SGrant Likely }
306ca632f55SGrant Likely 
sh_msiof_get_delay_bit(u32 dtdl_or_syncdl)3073110628dSYoshihiro Shimoda static u32 sh_msiof_get_delay_bit(u32 dtdl_or_syncdl)
3083110628dSYoshihiro Shimoda {
3093110628dSYoshihiro Shimoda 	/*
3103110628dSYoshihiro Shimoda 	 * DTDL/SYNCDL bit	: p->info->dtdl or p->info->syncdl
3113110628dSYoshihiro Shimoda 	 * b'000		: 0
3123110628dSYoshihiro Shimoda 	 * b'001		: 100
3133110628dSYoshihiro Shimoda 	 * b'010		: 200
3143110628dSYoshihiro Shimoda 	 * b'011 (SYNCDL only)	: 300
3153110628dSYoshihiro Shimoda 	 * b'101		: 50
3163110628dSYoshihiro Shimoda 	 * b'110		: 150
3173110628dSYoshihiro Shimoda 	 */
3183110628dSYoshihiro Shimoda 	if (dtdl_or_syncdl % 100)
3193110628dSYoshihiro Shimoda 		return dtdl_or_syncdl / 100 + 5;
3203110628dSYoshihiro Shimoda 	else
3213110628dSYoshihiro Shimoda 		return dtdl_or_syncdl / 100;
3223110628dSYoshihiro Shimoda }
3233110628dSYoshihiro Shimoda 
sh_msiof_spi_get_dtdl_and_syncdl(struct sh_msiof_spi_priv * p)3243110628dSYoshihiro Shimoda static u32 sh_msiof_spi_get_dtdl_and_syncdl(struct sh_msiof_spi_priv *p)
3253110628dSYoshihiro Shimoda {
3263110628dSYoshihiro Shimoda 	u32 val;
3273110628dSYoshihiro Shimoda 
3283110628dSYoshihiro Shimoda 	if (!p->info)
3293110628dSYoshihiro Shimoda 		return 0;
3303110628dSYoshihiro Shimoda 
3313110628dSYoshihiro Shimoda 	/* check if DTDL and SYNCDL is allowed value */
3323110628dSYoshihiro Shimoda 	if (p->info->dtdl > 200 || p->info->syncdl > 300) {
3333110628dSYoshihiro Shimoda 		dev_warn(&p->pdev->dev, "DTDL or SYNCDL is too large\n");
3343110628dSYoshihiro Shimoda 		return 0;
3353110628dSYoshihiro Shimoda 	}
3363110628dSYoshihiro Shimoda 
3373110628dSYoshihiro Shimoda 	/* check if the sum of DTDL and SYNCDL becomes an integer value  */
3383110628dSYoshihiro Shimoda 	if ((p->info->dtdl + p->info->syncdl) % 100) {
3393110628dSYoshihiro Shimoda 		dev_warn(&p->pdev->dev, "the sum of DTDL/SYNCDL is not good\n");
3403110628dSYoshihiro Shimoda 		return 0;
3413110628dSYoshihiro Shimoda 	}
3423110628dSYoshihiro Shimoda 
3438ae7d442SKrzysztof Kozlowski 	val = sh_msiof_get_delay_bit(p->info->dtdl) << SIMDR1_DTDL_SHIFT;
3448ae7d442SKrzysztof Kozlowski 	val |= sh_msiof_get_delay_bit(p->info->syncdl) << SIMDR1_SYNCDL_SHIFT;
3453110628dSYoshihiro Shimoda 
3463110628dSYoshihiro Shimoda 	return val;
3473110628dSYoshihiro Shimoda }
3483110628dSYoshihiro Shimoda 
sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv * p,u32 ss,u32 cpol,u32 cpha,u32 tx_hi_z,u32 lsb_first,u32 cs_high)3499cce882bSGeert Uytterhoeven static void sh_msiof_spi_set_pin_regs(struct sh_msiof_spi_priv *p, u32 ss,
350ca632f55SGrant Likely 				      u32 cpol, u32 cpha,
35150a77998STakashi Yoshii 				      u32 tx_hi_z, u32 lsb_first, u32 cs_high)
352ca632f55SGrant Likely {
353ca632f55SGrant Likely 	u32 tmp;
354ca632f55SGrant Likely 	int edge;
355ca632f55SGrant Likely 
356ca632f55SGrant Likely 	/*
357ca632f55SGrant Likely 	 * CPOL CPHA     TSCKIZ RSCKIZ TEDG REDG
358ca632f55SGrant Likely 	 *    0    0         10     10    1    1
359ca632f55SGrant Likely 	 *    0    1         10     10    0    0
360ca632f55SGrant Likely 	 *    1    0         11     11    0    0
361ca632f55SGrant Likely 	 *    1    1         11     11    1    1
362ca632f55SGrant Likely 	 */
3638ae7d442SKrzysztof Kozlowski 	tmp = SIMDR1_SYNCMD_SPI | 1 << SIMDR1_FLD_SHIFT | SIMDR1_XXSTP;
3648ae7d442SKrzysztof Kozlowski 	tmp |= !cs_high << SIMDR1_SYNCAC_SHIFT;
3658ae7d442SKrzysztof Kozlowski 	tmp |= lsb_first << SIMDR1_BITLSB_SHIFT;
3663110628dSYoshihiro Shimoda 	tmp |= sh_msiof_spi_get_dtdl_and_syncdl(p);
3671cb3ebc4SYang Yingliang 	if (spi_controller_is_target(p->ctlr)) {
3688ae7d442SKrzysztof Kozlowski 		sh_msiof_write(p, SITMDR1, tmp | SITMDR1_PCON);
3699cce882bSGeert Uytterhoeven 	} else {
3708ae7d442SKrzysztof Kozlowski 		sh_msiof_write(p, SITMDR1,
3718ae7d442SKrzysztof Kozlowski 			       tmp | SIMDR1_TRMD | SITMDR1_PCON |
3728ae7d442SKrzysztof Kozlowski 			       (ss < MAX_SS ? ss : 0) << SITMDR1_SYNCCH_SHIFT);
3739cce882bSGeert Uytterhoeven 	}
37435c35fd9SGeert Uytterhoeven 	if (p->ctlr->flags & SPI_CONTROLLER_MUST_TX) {
375beb74bb0SGeert Uytterhoeven 		/* These bits are reserved if RX needs TX */
376beb74bb0SGeert Uytterhoeven 		tmp &= ~0x0000ffff;
377beb74bb0SGeert Uytterhoeven 	}
3788ae7d442SKrzysztof Kozlowski 	sh_msiof_write(p, SIRMDR1, tmp);
379ca632f55SGrant Likely 
38001cfef57SGeert Uytterhoeven 	tmp = 0;
3818ae7d442SKrzysztof Kozlowski 	tmp |= SICTR_TSCKIZ_SCK | cpol << SICTR_TSCKIZ_POL_SHIFT;
3828ae7d442SKrzysztof Kozlowski 	tmp |= SICTR_RSCKIZ_SCK | cpol << SICTR_RSCKIZ_POL_SHIFT;
383ca632f55SGrant Likely 
384ca632f55SGrant Likely 	edge = cpol ^ !cpha;
385ca632f55SGrant Likely 
3868ae7d442SKrzysztof Kozlowski 	tmp |= edge << SICTR_TEDG_SHIFT;
3878ae7d442SKrzysztof Kozlowski 	tmp |= edge << SICTR_REDG_SHIFT;
3888ae7d442SKrzysztof Kozlowski 	tmp |= tx_hi_z ? SICTR_TXDIZ_HIZ : SICTR_TXDIZ_LOW;
3898ae7d442SKrzysztof Kozlowski 	sh_msiof_write(p, SICTR, tmp);
390ca632f55SGrant Likely }
391ca632f55SGrant Likely 
sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv * p,const void * tx_buf,void * rx_buf,u32 bits,u32 words)392ca632f55SGrant Likely static void sh_msiof_spi_set_mode_regs(struct sh_msiof_spi_priv *p,
393ca632f55SGrant Likely 				       const void *tx_buf, void *rx_buf,
394ca632f55SGrant Likely 				       u32 bits, u32 words)
395ca632f55SGrant Likely {
3968ae7d442SKrzysztof Kozlowski 	u32 dr2 = SIMDR2_BITLEN1(bits) | SIMDR2_WDLEN1(words);
397ca632f55SGrant Likely 
39835c35fd9SGeert Uytterhoeven 	if (tx_buf || (p->ctlr->flags & SPI_CONTROLLER_MUST_TX))
3998ae7d442SKrzysztof Kozlowski 		sh_msiof_write(p, SITMDR2, dr2);
400ca632f55SGrant Likely 	else
4018ae7d442SKrzysztof Kozlowski 		sh_msiof_write(p, SITMDR2, dr2 | SIMDR2_GRPMASK1);
402ca632f55SGrant Likely 
403ca632f55SGrant Likely 	if (rx_buf)
4048ae7d442SKrzysztof Kozlowski 		sh_msiof_write(p, SIRMDR2, dr2);
405ca632f55SGrant Likely }
406ca632f55SGrant Likely 
sh_msiof_reset_str(struct sh_msiof_spi_priv * p)407ca632f55SGrant Likely static void sh_msiof_reset_str(struct sh_msiof_spi_priv *p)
408ca632f55SGrant Likely {
4098ae7d442SKrzysztof Kozlowski 	sh_msiof_write(p, SISTR,
4108ae7d442SKrzysztof Kozlowski 		       sh_msiof_read(p, SISTR) & ~(SISTR_TDREQ | SISTR_RDREQ));
411ca632f55SGrant Likely }
412ca632f55SGrant Likely 
sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv * p,const void * tx_buf,int words,int fs)413ca632f55SGrant Likely static void sh_msiof_spi_write_fifo_8(struct sh_msiof_spi_priv *p,
414ca632f55SGrant Likely 				      const void *tx_buf, int words, int fs)
415ca632f55SGrant Likely {
416ca632f55SGrant Likely 	const u8 *buf_8 = tx_buf;
417ca632f55SGrant Likely 	int k;
418ca632f55SGrant Likely 
419ca632f55SGrant Likely 	for (k = 0; k < words; k++)
4208ae7d442SKrzysztof Kozlowski 		sh_msiof_write(p, SITFDR, buf_8[k] << fs);
421ca632f55SGrant Likely }
422ca632f55SGrant Likely 
sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv * p,const void * tx_buf,int words,int fs)423ca632f55SGrant Likely static void sh_msiof_spi_write_fifo_16(struct sh_msiof_spi_priv *p,
424ca632f55SGrant Likely 				       const void *tx_buf, int words, int fs)
425ca632f55SGrant Likely {
426ca632f55SGrant Likely 	const u16 *buf_16 = tx_buf;
427ca632f55SGrant Likely 	int k;
428ca632f55SGrant Likely 
429ca632f55SGrant Likely 	for (k = 0; k < words; k++)
4308ae7d442SKrzysztof Kozlowski 		sh_msiof_write(p, SITFDR, buf_16[k] << fs);
431ca632f55SGrant Likely }
432ca632f55SGrant Likely 
sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv * p,const void * tx_buf,int words,int fs)433ca632f55SGrant Likely static void sh_msiof_spi_write_fifo_16u(struct sh_msiof_spi_priv *p,
434ca632f55SGrant Likely 					const void *tx_buf, int words, int fs)
435ca632f55SGrant Likely {
436ca632f55SGrant Likely 	const u16 *buf_16 = tx_buf;
437ca632f55SGrant Likely 	int k;
438ca632f55SGrant Likely 
439ca632f55SGrant Likely 	for (k = 0; k < words; k++)
4408ae7d442SKrzysztof Kozlowski 		sh_msiof_write(p, SITFDR, get_unaligned(&buf_16[k]) << fs);
441ca632f55SGrant Likely }
442ca632f55SGrant Likely 
sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv * p,const void * tx_buf,int words,int fs)443ca632f55SGrant Likely static void sh_msiof_spi_write_fifo_32(struct sh_msiof_spi_priv *p,
444ca632f55SGrant Likely 				       const void *tx_buf, int words, int fs)
445ca632f55SGrant Likely {
446ca632f55SGrant Likely 	const u32 *buf_32 = tx_buf;
447ca632f55SGrant Likely 	int k;
448ca632f55SGrant Likely 
449ca632f55SGrant Likely 	for (k = 0; k < words; k++)
4508ae7d442SKrzysztof Kozlowski 		sh_msiof_write(p, SITFDR, buf_32[k] << fs);
451ca632f55SGrant Likely }
452ca632f55SGrant Likely 
sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv * p,const void * tx_buf,int words,int fs)453ca632f55SGrant Likely static void sh_msiof_spi_write_fifo_32u(struct sh_msiof_spi_priv *p,
454ca632f55SGrant Likely 					const void *tx_buf, int words, int fs)
455ca632f55SGrant Likely {
456ca632f55SGrant Likely 	const u32 *buf_32 = tx_buf;
457ca632f55SGrant Likely 	int k;
458ca632f55SGrant Likely 
459ca632f55SGrant Likely 	for (k = 0; k < words; k++)
4608ae7d442SKrzysztof Kozlowski 		sh_msiof_write(p, SITFDR, get_unaligned(&buf_32[k]) << fs);
461ca632f55SGrant Likely }
462ca632f55SGrant Likely 
sh_msiof_spi_write_fifo_s32(struct sh_msiof_spi_priv * p,const void * tx_buf,int words,int fs)463ca632f55SGrant Likely static void sh_msiof_spi_write_fifo_s32(struct sh_msiof_spi_priv *p,
464ca632f55SGrant Likely 					const void *tx_buf, int words, int fs)
465ca632f55SGrant Likely {
466ca632f55SGrant Likely 	const u32 *buf_32 = tx_buf;
467ca632f55SGrant Likely 	int k;
468ca632f55SGrant Likely 
469ca632f55SGrant Likely 	for (k = 0; k < words; k++)
4708ae7d442SKrzysztof Kozlowski 		sh_msiof_write(p, SITFDR, swab32(buf_32[k] << fs));
471ca632f55SGrant Likely }
472ca632f55SGrant Likely 
sh_msiof_spi_write_fifo_s32u(struct sh_msiof_spi_priv * p,const void * tx_buf,int words,int fs)473ca632f55SGrant Likely static void sh_msiof_spi_write_fifo_s32u(struct sh_msiof_spi_priv *p,
474ca632f55SGrant Likely 					 const void *tx_buf, int words, int fs)
475ca632f55SGrant Likely {
476ca632f55SGrant Likely 	const u32 *buf_32 = tx_buf;
477ca632f55SGrant Likely 	int k;
478ca632f55SGrant Likely 
479ca632f55SGrant Likely 	for (k = 0; k < words; k++)
4808ae7d442SKrzysztof Kozlowski 		sh_msiof_write(p, SITFDR, swab32(get_unaligned(&buf_32[k]) << fs));
481ca632f55SGrant Likely }
482ca632f55SGrant Likely 
sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv * p,void * rx_buf,int words,int fs)483ca632f55SGrant Likely static void sh_msiof_spi_read_fifo_8(struct sh_msiof_spi_priv *p,
484ca632f55SGrant Likely 				     void *rx_buf, int words, int fs)
485ca632f55SGrant Likely {
486ca632f55SGrant Likely 	u8 *buf_8 = rx_buf;
487ca632f55SGrant Likely 	int k;
488ca632f55SGrant Likely 
489ca632f55SGrant Likely 	for (k = 0; k < words; k++)
4908ae7d442SKrzysztof Kozlowski 		buf_8[k] = sh_msiof_read(p, SIRFDR) >> fs;
491ca632f55SGrant Likely }
492ca632f55SGrant Likely 
sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv * p,void * rx_buf,int words,int fs)493ca632f55SGrant Likely static void sh_msiof_spi_read_fifo_16(struct sh_msiof_spi_priv *p,
494ca632f55SGrant Likely 				      void *rx_buf, int words, int fs)
495ca632f55SGrant Likely {
496ca632f55SGrant Likely 	u16 *buf_16 = rx_buf;
497ca632f55SGrant Likely 	int k;
498ca632f55SGrant Likely 
499ca632f55SGrant Likely 	for (k = 0; k < words; k++)
5008ae7d442SKrzysztof Kozlowski 		buf_16[k] = sh_msiof_read(p, SIRFDR) >> fs;
501ca632f55SGrant Likely }
502ca632f55SGrant Likely 
sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv * p,void * rx_buf,int words,int fs)503ca632f55SGrant Likely static void sh_msiof_spi_read_fifo_16u(struct sh_msiof_spi_priv *p,
504ca632f55SGrant Likely 				       void *rx_buf, int words, int fs)
505ca632f55SGrant Likely {
506ca632f55SGrant Likely 	u16 *buf_16 = rx_buf;
507ca632f55SGrant Likely 	int k;
508ca632f55SGrant Likely 
509ca632f55SGrant Likely 	for (k = 0; k < words; k++)
5108ae7d442SKrzysztof Kozlowski 		put_unaligned(sh_msiof_read(p, SIRFDR) >> fs, &buf_16[k]);
511ca632f55SGrant Likely }
512ca632f55SGrant Likely 
sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv * p,void * rx_buf,int words,int fs)513ca632f55SGrant Likely static void sh_msiof_spi_read_fifo_32(struct sh_msiof_spi_priv *p,
514ca632f55SGrant Likely 				      void *rx_buf, int words, int fs)
515ca632f55SGrant Likely {
516ca632f55SGrant Likely 	u32 *buf_32 = rx_buf;
517ca632f55SGrant Likely 	int k;
518ca632f55SGrant Likely 
519ca632f55SGrant Likely 	for (k = 0; k < words; k++)
5208ae7d442SKrzysztof Kozlowski 		buf_32[k] = sh_msiof_read(p, SIRFDR) >> fs;
521ca632f55SGrant Likely }
522ca632f55SGrant Likely 
sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv * p,void * rx_buf,int words,int fs)523ca632f55SGrant Likely static void sh_msiof_spi_read_fifo_32u(struct sh_msiof_spi_priv *p,
524ca632f55SGrant Likely 				       void *rx_buf, int words, int fs)
525ca632f55SGrant Likely {
526ca632f55SGrant Likely 	u32 *buf_32 = rx_buf;
527ca632f55SGrant Likely 	int k;
528ca632f55SGrant Likely 
529ca632f55SGrant Likely 	for (k = 0; k < words; k++)
5308ae7d442SKrzysztof Kozlowski 		put_unaligned(sh_msiof_read(p, SIRFDR) >> fs, &buf_32[k]);
531ca632f55SGrant Likely }
532ca632f55SGrant Likely 
sh_msiof_spi_read_fifo_s32(struct sh_msiof_spi_priv * p,void * rx_buf,int words,int fs)533ca632f55SGrant Likely static void sh_msiof_spi_read_fifo_s32(struct sh_msiof_spi_priv *p,
534ca632f55SGrant Likely 				       void *rx_buf, int words, int fs)
535ca632f55SGrant Likely {
536ca632f55SGrant Likely 	u32 *buf_32 = rx_buf;
537ca632f55SGrant Likely 	int k;
538ca632f55SGrant Likely 
539ca632f55SGrant Likely 	for (k = 0; k < words; k++)
5408ae7d442SKrzysztof Kozlowski 		buf_32[k] = swab32(sh_msiof_read(p, SIRFDR) >> fs);
541ca632f55SGrant Likely }
542ca632f55SGrant Likely 
sh_msiof_spi_read_fifo_s32u(struct sh_msiof_spi_priv * p,void * rx_buf,int words,int fs)543ca632f55SGrant Likely static void sh_msiof_spi_read_fifo_s32u(struct sh_msiof_spi_priv *p,
544ca632f55SGrant Likely 				       void *rx_buf, int words, int fs)
545ca632f55SGrant Likely {
546ca632f55SGrant Likely 	u32 *buf_32 = rx_buf;
547ca632f55SGrant Likely 	int k;
548ca632f55SGrant Likely 
549ca632f55SGrant Likely 	for (k = 0; k < words; k++)
5508ae7d442SKrzysztof Kozlowski 		put_unaligned(swab32(sh_msiof_read(p, SIRFDR) >> fs), &buf_32[k]);
551ca632f55SGrant Likely }
552ca632f55SGrant Likely 
sh_msiof_spi_setup(struct spi_device * spi)5538d19534aSGeert Uytterhoeven static int sh_msiof_spi_setup(struct spi_device *spi)
554ca632f55SGrant Likely {
55535c35fd9SGeert Uytterhoeven 	struct sh_msiof_spi_priv *p =
55635c35fd9SGeert Uytterhoeven 		spi_controller_get_devdata(spi->controller);
5577ff0b53cSGeert Uytterhoeven 	u32 clr, set, tmp;
55801576056SHisashi Nakamura 
5591cb3ebc4SYang Yingliang 	if (spi_get_csgpiod(spi, 0) || spi_controller_is_target(p->ctlr))
5607ff0b53cSGeert Uytterhoeven 		return 0;
5611bd6363bSGeert Uytterhoeven 
5627ff0b53cSGeert Uytterhoeven 	if (p->native_cs_inited &&
5637ff0b53cSGeert Uytterhoeven 	    (p->native_cs_high == !!(spi->mode & SPI_CS_HIGH)))
5647ff0b53cSGeert Uytterhoeven 		return 0;
56501576056SHisashi Nakamura 
5667ff0b53cSGeert Uytterhoeven 	/* Configure native chip select mode/polarity early */
5678ae7d442SKrzysztof Kozlowski 	clr = SIMDR1_SYNCMD_MASK;
5688ae7d442SKrzysztof Kozlowski 	set = SIMDR1_SYNCMD_SPI;
5697ff0b53cSGeert Uytterhoeven 	if (spi->mode & SPI_CS_HIGH)
5708ae7d442SKrzysztof Kozlowski 		clr |= BIT(SIMDR1_SYNCAC_SHIFT);
5717ff0b53cSGeert Uytterhoeven 	else
5728ae7d442SKrzysztof Kozlowski 		set |= BIT(SIMDR1_SYNCAC_SHIFT);
5737ff0b53cSGeert Uytterhoeven 	pm_runtime_get_sync(&p->pdev->dev);
5748ae7d442SKrzysztof Kozlowski 	tmp = sh_msiof_read(p, SITMDR1) & ~clr;
5758ae7d442SKrzysztof Kozlowski 	sh_msiof_write(p, SITMDR1, tmp | set | SIMDR1_TRMD | SITMDR1_PCON);
5768ae7d442SKrzysztof Kozlowski 	tmp = sh_msiof_read(p, SIRMDR1) & ~clr;
5778ae7d442SKrzysztof Kozlowski 	sh_msiof_write(p, SIRMDR1, tmp | set);
578c8935ef0SGeert Uytterhoeven 	pm_runtime_put(&p->pdev->dev);
5797ff0b53cSGeert Uytterhoeven 	p->native_cs_high = spi->mode & SPI_CS_HIGH;
5807ff0b53cSGeert Uytterhoeven 	p->native_cs_inited = true;
5811bd6363bSGeert Uytterhoeven 	return 0;
5828d19534aSGeert Uytterhoeven }
5838d19534aSGeert Uytterhoeven 
sh_msiof_prepare_message(struct spi_controller * ctlr,struct spi_message * msg)58435c35fd9SGeert Uytterhoeven static int sh_msiof_prepare_message(struct spi_controller *ctlr,
585c833ff73SGeert Uytterhoeven 				    struct spi_message *msg)
586c833ff73SGeert Uytterhoeven {
58735c35fd9SGeert Uytterhoeven 	struct sh_msiof_spi_priv *p = spi_controller_get_devdata(ctlr);
588c833ff73SGeert Uytterhoeven 	const struct spi_device *spi = msg->spi;
589b8761434SGeert Uytterhoeven 	u32 ss, cs_high;
590c833ff73SGeert Uytterhoeven 
591ca632f55SGrant Likely 	/* Configure pins before asserting CS */
5927859ad5aSGeert Uytterhoeven 	if (spi_get_csgpiod(spi, 0)) {
593aa32f76eSGeert Uytterhoeven 		ss = ctlr->unused_native_cs;
594b8761434SGeert Uytterhoeven 		cs_high = p->native_cs_high;
595b8761434SGeert Uytterhoeven 	} else {
5967859ad5aSGeert Uytterhoeven 		ss = spi_get_chipselect(spi, 0);
597b8761434SGeert Uytterhoeven 		cs_high = !!(spi->mode & SPI_CS_HIGH);
598b8761434SGeert Uytterhoeven 	}
599b8761434SGeert Uytterhoeven 	sh_msiof_spi_set_pin_regs(p, ss, !!(spi->mode & SPI_CPOL),
600ca632f55SGrant Likely 				  !!(spi->mode & SPI_CPHA),
601ca632f55SGrant Likely 				  !!(spi->mode & SPI_3WIRE),
602b8761434SGeert Uytterhoeven 				  !!(spi->mode & SPI_LSB_FIRST), cs_high);
603c833ff73SGeert Uytterhoeven 	return 0;
604ca632f55SGrant Likely }
605ca632f55SGrant Likely 
sh_msiof_spi_start(struct sh_msiof_spi_priv * p,void * rx_buf)60676c02e71SGeert Uytterhoeven static int sh_msiof_spi_start(struct sh_msiof_spi_priv *p, void *rx_buf)
60776c02e71SGeert Uytterhoeven {
6081cb3ebc4SYang Yingliang 	bool target = spi_controller_is_target(p->ctlr);
609cf9e4784SHisashi Nakamura 	int ret = 0;
61076c02e71SGeert Uytterhoeven 
61176c02e71SGeert Uytterhoeven 	/* setup clock and rx/tx signals */
6121cb3ebc4SYang Yingliang 	if (!target)
6138ae7d442SKrzysztof Kozlowski 		ret = sh_msiof_modify_ctr_wait(p, 0, SICTR_TSCKE);
61476c02e71SGeert Uytterhoeven 	if (rx_buf && !ret)
6158ae7d442SKrzysztof Kozlowski 		ret = sh_msiof_modify_ctr_wait(p, 0, SICTR_RXE);
61676c02e71SGeert Uytterhoeven 	if (!ret)
6178ae7d442SKrzysztof Kozlowski 		ret = sh_msiof_modify_ctr_wait(p, 0, SICTR_TXE);
61876c02e71SGeert Uytterhoeven 
61976c02e71SGeert Uytterhoeven 	/* start by setting frame bit */
6201cb3ebc4SYang Yingliang 	if (!ret && !target)
6218ae7d442SKrzysztof Kozlowski 		ret = sh_msiof_modify_ctr_wait(p, 0, SICTR_TFSE);
62276c02e71SGeert Uytterhoeven 
62376c02e71SGeert Uytterhoeven 	return ret;
62476c02e71SGeert Uytterhoeven }
62576c02e71SGeert Uytterhoeven 
sh_msiof_spi_stop(struct sh_msiof_spi_priv * p,void * rx_buf)62676c02e71SGeert Uytterhoeven static int sh_msiof_spi_stop(struct sh_msiof_spi_priv *p, void *rx_buf)
62776c02e71SGeert Uytterhoeven {
6281cb3ebc4SYang Yingliang 	bool target = spi_controller_is_target(p->ctlr);
629cf9e4784SHisashi Nakamura 	int ret = 0;
63076c02e71SGeert Uytterhoeven 
63176c02e71SGeert Uytterhoeven 	/* shut down frame, rx/tx and clock signals */
6321cb3ebc4SYang Yingliang 	if (!target)
6338ae7d442SKrzysztof Kozlowski 		ret = sh_msiof_modify_ctr_wait(p, SICTR_TFSE, 0);
63476c02e71SGeert Uytterhoeven 	if (!ret)
6358ae7d442SKrzysztof Kozlowski 		ret = sh_msiof_modify_ctr_wait(p, SICTR_TXE, 0);
63676c02e71SGeert Uytterhoeven 	if (rx_buf && !ret)
6378ae7d442SKrzysztof Kozlowski 		ret = sh_msiof_modify_ctr_wait(p, SICTR_RXE, 0);
6381cb3ebc4SYang Yingliang 	if (!ret && !target)
6398ae7d442SKrzysztof Kozlowski 		ret = sh_msiof_modify_ctr_wait(p, SICTR_TSCKE, 0);
64076c02e71SGeert Uytterhoeven 
64176c02e71SGeert Uytterhoeven 	return ret;
64276c02e71SGeert Uytterhoeven }
64376c02e71SGeert Uytterhoeven 
sh_msiof_target_abort(struct spi_controller * ctlr)6441cb3ebc4SYang Yingliang static int sh_msiof_target_abort(struct spi_controller *ctlr)
645cf9e4784SHisashi Nakamura {
64635c35fd9SGeert Uytterhoeven 	struct sh_msiof_spi_priv *p = spi_controller_get_devdata(ctlr);
647cf9e4784SHisashi Nakamura 
6481cb3ebc4SYang Yingliang 	p->target_aborted = true;
649cf9e4784SHisashi Nakamura 	complete(&p->done);
65008ba7ae3SGeert Uytterhoeven 	complete(&p->done_txdma);
651cf9e4784SHisashi Nakamura 	return 0;
652cf9e4784SHisashi Nakamura }
653cf9e4784SHisashi Nakamura 
sh_msiof_wait_for_completion(struct sh_msiof_spi_priv * p,struct completion * x)65408ba7ae3SGeert Uytterhoeven static int sh_msiof_wait_for_completion(struct sh_msiof_spi_priv *p,
65508ba7ae3SGeert Uytterhoeven 					struct completion *x)
656cf9e4784SHisashi Nakamura {
6571cb3ebc4SYang Yingliang 	if (spi_controller_is_target(p->ctlr)) {
65808ba7ae3SGeert Uytterhoeven 		if (wait_for_completion_interruptible(x) ||
6591cb3ebc4SYang Yingliang 		    p->target_aborted) {
660cf9e4784SHisashi Nakamura 			dev_dbg(&p->pdev->dev, "interrupted\n");
661cf9e4784SHisashi Nakamura 			return -EINTR;
662cf9e4784SHisashi Nakamura 		}
663cf9e4784SHisashi Nakamura 	} else {
66408ba7ae3SGeert Uytterhoeven 		if (!wait_for_completion_timeout(x, HZ)) {
665cf9e4784SHisashi Nakamura 			dev_err(&p->pdev->dev, "timeout\n");
666cf9e4784SHisashi Nakamura 			return -ETIMEDOUT;
667cf9e4784SHisashi Nakamura 		}
668cf9e4784SHisashi Nakamura 	}
669cf9e4784SHisashi Nakamura 
670cf9e4784SHisashi Nakamura 	return 0;
671cf9e4784SHisashi Nakamura }
672cf9e4784SHisashi Nakamura 
sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv * p,void (* tx_fifo)(struct sh_msiof_spi_priv *,const void *,int,int),void (* rx_fifo)(struct sh_msiof_spi_priv *,void *,int,int),const void * tx_buf,void * rx_buf,int words,int bits)673ca632f55SGrant Likely static int sh_msiof_spi_txrx_once(struct sh_msiof_spi_priv *p,
674ca632f55SGrant Likely 				  void (*tx_fifo)(struct sh_msiof_spi_priv *,
675ca632f55SGrant Likely 						  const void *, int, int),
676ca632f55SGrant Likely 				  void (*rx_fifo)(struct sh_msiof_spi_priv *,
677ca632f55SGrant Likely 						  void *, int, int),
678ca632f55SGrant Likely 				  const void *tx_buf, void *rx_buf,
679ca632f55SGrant Likely 				  int words, int bits)
680ca632f55SGrant Likely {
681ca632f55SGrant Likely 	int fifo_shift;
682ca632f55SGrant Likely 	int ret;
683ca632f55SGrant Likely 
684ca632f55SGrant Likely 	/* limit maximum word transfer to rx/tx fifo size */
685ca632f55SGrant Likely 	if (tx_buf)
686ca632f55SGrant Likely 		words = min_t(int, words, p->tx_fifo_size);
687ca632f55SGrant Likely 	if (rx_buf)
688ca632f55SGrant Likely 		words = min_t(int, words, p->rx_fifo_size);
689ca632f55SGrant Likely 
690ca632f55SGrant Likely 	/* the fifo contents need shifting */
691ca632f55SGrant Likely 	fifo_shift = 32 - bits;
692ca632f55SGrant Likely 
693b0d0ce8bSGeert Uytterhoeven 	/* default FIFO watermarks for PIO */
6948ae7d442SKrzysztof Kozlowski 	sh_msiof_write(p, SIFCTR, 0);
695b0d0ce8bSGeert Uytterhoeven 
696ca632f55SGrant Likely 	/* setup msiof transfer mode registers */
697ca632f55SGrant Likely 	sh_msiof_spi_set_mode_regs(p, tx_buf, rx_buf, bits, words);
6988ae7d442SKrzysztof Kozlowski 	sh_msiof_write(p, SIIER, SIIER_TEOFE | SIIER_REOFE);
699ca632f55SGrant Likely 
700ca632f55SGrant Likely 	/* write tx fifo */
701ca632f55SGrant Likely 	if (tx_buf)
702ca632f55SGrant Likely 		tx_fifo(p, tx_buf, words, fifo_shift);
703ca632f55SGrant Likely 
70416735d02SWolfram Sang 	reinit_completion(&p->done);
7051cb3ebc4SYang Yingliang 	p->target_aborted = false;
70676c02e71SGeert Uytterhoeven 
70776c02e71SGeert Uytterhoeven 	ret = sh_msiof_spi_start(p, rx_buf);
708ca632f55SGrant Likely 	if (ret) {
709ca632f55SGrant Likely 		dev_err(&p->pdev->dev, "failed to start hardware\n");
71075b82e23SGeert Uytterhoeven 		goto stop_ier;
711ca632f55SGrant Likely 	}
712ca632f55SGrant Likely 
713ca632f55SGrant Likely 	/* wait for tx fifo to be emptied / rx fifo to be filled */
71408ba7ae3SGeert Uytterhoeven 	ret = sh_msiof_wait_for_completion(p, &p->done);
715cf9e4784SHisashi Nakamura 	if (ret)
71675b82e23SGeert Uytterhoeven 		goto stop_reset;
717ca632f55SGrant Likely 
718ca632f55SGrant Likely 	/* read rx fifo */
719ca632f55SGrant Likely 	if (rx_buf)
720ca632f55SGrant Likely 		rx_fifo(p, rx_buf, words, fifo_shift);
721ca632f55SGrant Likely 
722ca632f55SGrant Likely 	/* clear status bits */
723ca632f55SGrant Likely 	sh_msiof_reset_str(p);
724ca632f55SGrant Likely 
72576c02e71SGeert Uytterhoeven 	ret = sh_msiof_spi_stop(p, rx_buf);
726ca632f55SGrant Likely 	if (ret) {
727ca632f55SGrant Likely 		dev_err(&p->pdev->dev, "failed to shut down hardware\n");
72875b82e23SGeert Uytterhoeven 		return ret;
729ca632f55SGrant Likely 	}
730ca632f55SGrant Likely 
731ca632f55SGrant Likely 	return words;
732ca632f55SGrant Likely 
73375b82e23SGeert Uytterhoeven stop_reset:
73475b82e23SGeert Uytterhoeven 	sh_msiof_reset_str(p);
73575b82e23SGeert Uytterhoeven 	sh_msiof_spi_stop(p, rx_buf);
73675b82e23SGeert Uytterhoeven stop_ier:
7378ae7d442SKrzysztof Kozlowski 	sh_msiof_write(p, SIIER, 0);
738ca632f55SGrant Likely 	return ret;
739ca632f55SGrant Likely }
740ca632f55SGrant Likely 
sh_msiof_dma_complete(void * arg)741b0d0ce8bSGeert Uytterhoeven static void sh_msiof_dma_complete(void *arg)
742b0d0ce8bSGeert Uytterhoeven {
74308ba7ae3SGeert Uytterhoeven 	complete(arg);
744b0d0ce8bSGeert Uytterhoeven }
745b0d0ce8bSGeert Uytterhoeven 
sh_msiof_dma_once(struct sh_msiof_spi_priv * p,const void * tx,void * rx,unsigned int len)746b0d0ce8bSGeert Uytterhoeven static int sh_msiof_dma_once(struct sh_msiof_spi_priv *p, const void *tx,
747b0d0ce8bSGeert Uytterhoeven 			     void *rx, unsigned int len)
748b0d0ce8bSGeert Uytterhoeven {
749b0d0ce8bSGeert Uytterhoeven 	u32 ier_bits = 0;
750b0d0ce8bSGeert Uytterhoeven 	struct dma_async_tx_descriptor *desc_tx = NULL, *desc_rx = NULL;
751b0d0ce8bSGeert Uytterhoeven 	dma_cookie_t cookie;
752b0d0ce8bSGeert Uytterhoeven 	int ret;
753b0d0ce8bSGeert Uytterhoeven 
7543e81b592SGeert Uytterhoeven 	/* First prepare and submit the DMA request(s), as this may fail */
7553e81b592SGeert Uytterhoeven 	if (rx) {
7568ae7d442SKrzysztof Kozlowski 		ier_bits |= SIIER_RDREQE | SIIER_RDMAE;
75735c35fd9SGeert Uytterhoeven 		desc_rx = dmaengine_prep_slave_single(p->ctlr->dma_rx,
758da779513SGeert Uytterhoeven 					p->rx_dma_addr, len, DMA_DEV_TO_MEM,
7593e81b592SGeert Uytterhoeven 					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
760a5e7c719SGeert Uytterhoeven 		if (!desc_rx)
761a5e7c719SGeert Uytterhoeven 			return -EAGAIN;
7623e81b592SGeert Uytterhoeven 
7633e81b592SGeert Uytterhoeven 		desc_rx->callback = sh_msiof_dma_complete;
76408ba7ae3SGeert Uytterhoeven 		desc_rx->callback_param = &p->done;
7653e81b592SGeert Uytterhoeven 		cookie = dmaengine_submit(desc_rx);
766a5e7c719SGeert Uytterhoeven 		if (dma_submit_error(cookie))
767a5e7c719SGeert Uytterhoeven 			return cookie;
7683e81b592SGeert Uytterhoeven 	}
7693e81b592SGeert Uytterhoeven 
770b0d0ce8bSGeert Uytterhoeven 	if (tx) {
7718ae7d442SKrzysztof Kozlowski 		ier_bits |= SIIER_TDREQE | SIIER_TDMAE;
77235c35fd9SGeert Uytterhoeven 		dma_sync_single_for_device(p->ctlr->dma_tx->device->dev,
7735dabcf2fSGeert Uytterhoeven 					   p->tx_dma_addr, len, DMA_TO_DEVICE);
77435c35fd9SGeert Uytterhoeven 		desc_tx = dmaengine_prep_slave_single(p->ctlr->dma_tx,
775da779513SGeert Uytterhoeven 					p->tx_dma_addr, len, DMA_MEM_TO_DEV,
776b0d0ce8bSGeert Uytterhoeven 					DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
7773e81b592SGeert Uytterhoeven 		if (!desc_tx) {
7783e81b592SGeert Uytterhoeven 			ret = -EAGAIN;
7793e81b592SGeert Uytterhoeven 			goto no_dma_tx;
780b0d0ce8bSGeert Uytterhoeven 		}
781b0d0ce8bSGeert Uytterhoeven 
7823e81b592SGeert Uytterhoeven 		desc_tx->callback = sh_msiof_dma_complete;
78308ba7ae3SGeert Uytterhoeven 		desc_tx->callback_param = &p->done_txdma;
7843e81b592SGeert Uytterhoeven 		cookie = dmaengine_submit(desc_tx);
7853e81b592SGeert Uytterhoeven 		if (dma_submit_error(cookie)) {
7863e81b592SGeert Uytterhoeven 			ret = cookie;
7873e81b592SGeert Uytterhoeven 			goto no_dma_tx;
7883e81b592SGeert Uytterhoeven 		}
789b0d0ce8bSGeert Uytterhoeven 	}
790279d2378SGeert Uytterhoeven 
791279d2378SGeert Uytterhoeven 	/* 1 stage FIFO watermarks for DMA */
7928ae7d442SKrzysztof Kozlowski 	sh_msiof_write(p, SIFCTR, SIFCTR_TFWM_1 | SIFCTR_RFWM_1);
793279d2378SGeert Uytterhoeven 
794279d2378SGeert Uytterhoeven 	/* setup msiof transfer mode registers (32-bit words) */
795279d2378SGeert Uytterhoeven 	sh_msiof_spi_set_mode_regs(p, tx, rx, 32, len / 4);
796279d2378SGeert Uytterhoeven 
7978ae7d442SKrzysztof Kozlowski 	sh_msiof_write(p, SIIER, ier_bits);
798b0d0ce8bSGeert Uytterhoeven 
799b0d0ce8bSGeert Uytterhoeven 	reinit_completion(&p->done);
80008ba7ae3SGeert Uytterhoeven 	if (tx)
80108ba7ae3SGeert Uytterhoeven 		reinit_completion(&p->done_txdma);
8021cb3ebc4SYang Yingliang 	p->target_aborted = false;
803b0d0ce8bSGeert Uytterhoeven 
8043e81b592SGeert Uytterhoeven 	/* Now start DMA */
8053e81b592SGeert Uytterhoeven 	if (rx)
80635c35fd9SGeert Uytterhoeven 		dma_async_issue_pending(p->ctlr->dma_rx);
8077a9f957bSGeert Uytterhoeven 	if (tx)
80835c35fd9SGeert Uytterhoeven 		dma_async_issue_pending(p->ctlr->dma_tx);
809b0d0ce8bSGeert Uytterhoeven 
810b0d0ce8bSGeert Uytterhoeven 	ret = sh_msiof_spi_start(p, rx);
811b0d0ce8bSGeert Uytterhoeven 	if (ret) {
812b0d0ce8bSGeert Uytterhoeven 		dev_err(&p->pdev->dev, "failed to start hardware\n");
8133e81b592SGeert Uytterhoeven 		goto stop_dma;
814b0d0ce8bSGeert Uytterhoeven 	}
815b0d0ce8bSGeert Uytterhoeven 
81608ba7ae3SGeert Uytterhoeven 	if (tx) {
81708ba7ae3SGeert Uytterhoeven 		/* wait for tx DMA completion */
81808ba7ae3SGeert Uytterhoeven 		ret = sh_msiof_wait_for_completion(p, &p->done_txdma);
81908ba7ae3SGeert Uytterhoeven 		if (ret)
82008ba7ae3SGeert Uytterhoeven 			goto stop_reset;
82108ba7ae3SGeert Uytterhoeven 	}
82208ba7ae3SGeert Uytterhoeven 
82308ba7ae3SGeert Uytterhoeven 	if (rx) {
82408ba7ae3SGeert Uytterhoeven 		/* wait for rx DMA completion */
82508ba7ae3SGeert Uytterhoeven 		ret = sh_msiof_wait_for_completion(p, &p->done);
826cf9e4784SHisashi Nakamura 		if (ret)
827b0d0ce8bSGeert Uytterhoeven 			goto stop_reset;
828b0d0ce8bSGeert Uytterhoeven 
8298ae7d442SKrzysztof Kozlowski 		sh_msiof_write(p, SIIER, 0);
83008ba7ae3SGeert Uytterhoeven 	} else {
83189434c3cSGeert Uytterhoeven 		/* wait for tx fifo to be emptied */
8328ae7d442SKrzysztof Kozlowski 		sh_msiof_write(p, SIIER, SIIER_TEOFE);
83308ba7ae3SGeert Uytterhoeven 		ret = sh_msiof_wait_for_completion(p, &p->done);
83489434c3cSGeert Uytterhoeven 		if (ret)
83589434c3cSGeert Uytterhoeven 			goto stop_reset;
83689434c3cSGeert Uytterhoeven 	}
83789434c3cSGeert Uytterhoeven 
838b0d0ce8bSGeert Uytterhoeven 	/* clear status bits */
839b0d0ce8bSGeert Uytterhoeven 	sh_msiof_reset_str(p);
840b0d0ce8bSGeert Uytterhoeven 
841b0d0ce8bSGeert Uytterhoeven 	ret = sh_msiof_spi_stop(p, rx);
842b0d0ce8bSGeert Uytterhoeven 	if (ret) {
843b0d0ce8bSGeert Uytterhoeven 		dev_err(&p->pdev->dev, "failed to shut down hardware\n");
844b0d0ce8bSGeert Uytterhoeven 		return ret;
845b0d0ce8bSGeert Uytterhoeven 	}
846b0d0ce8bSGeert Uytterhoeven 
847b0d0ce8bSGeert Uytterhoeven 	if (rx)
84835c35fd9SGeert Uytterhoeven 		dma_sync_single_for_cpu(p->ctlr->dma_rx->device->dev,
84935c35fd9SGeert Uytterhoeven 					p->rx_dma_addr, len, DMA_FROM_DEVICE);
850b0d0ce8bSGeert Uytterhoeven 
851b0d0ce8bSGeert Uytterhoeven 	return 0;
852b0d0ce8bSGeert Uytterhoeven 
853b0d0ce8bSGeert Uytterhoeven stop_reset:
854b0d0ce8bSGeert Uytterhoeven 	sh_msiof_reset_str(p);
855b0d0ce8bSGeert Uytterhoeven 	sh_msiof_spi_stop(p, rx);
8563e81b592SGeert Uytterhoeven stop_dma:
857b0d0ce8bSGeert Uytterhoeven 	if (tx)
858a26dee29SWolfram Sang 		dmaengine_terminate_sync(p->ctlr->dma_tx);
8593e81b592SGeert Uytterhoeven no_dma_tx:
860b0d0ce8bSGeert Uytterhoeven 	if (rx)
861a26dee29SWolfram Sang 		dmaengine_terminate_sync(p->ctlr->dma_rx);
8628ae7d442SKrzysztof Kozlowski 	sh_msiof_write(p, SIIER, 0);
863b0d0ce8bSGeert Uytterhoeven 	return ret;
864b0d0ce8bSGeert Uytterhoeven }
865b0d0ce8bSGeert Uytterhoeven 
copy_bswap32(u32 * dst,const u32 * src,unsigned int words)866b0d0ce8bSGeert Uytterhoeven static void copy_bswap32(u32 *dst, const u32 *src, unsigned int words)
867b0d0ce8bSGeert Uytterhoeven {
868b0d0ce8bSGeert Uytterhoeven 	/* src or dst can be unaligned, but not both */
869b0d0ce8bSGeert Uytterhoeven 	if ((unsigned long)src & 3) {
870b0d0ce8bSGeert Uytterhoeven 		while (words--) {
871b0d0ce8bSGeert Uytterhoeven 			*dst++ = swab32(get_unaligned(src));
872b0d0ce8bSGeert Uytterhoeven 			src++;
873b0d0ce8bSGeert Uytterhoeven 		}
874b0d0ce8bSGeert Uytterhoeven 	} else if ((unsigned long)dst & 3) {
875b0d0ce8bSGeert Uytterhoeven 		while (words--) {
876b0d0ce8bSGeert Uytterhoeven 			put_unaligned(swab32(*src++), dst);
877b0d0ce8bSGeert Uytterhoeven 			dst++;
878b0d0ce8bSGeert Uytterhoeven 		}
879b0d0ce8bSGeert Uytterhoeven 	} else {
880b0d0ce8bSGeert Uytterhoeven 		while (words--)
881b0d0ce8bSGeert Uytterhoeven 			*dst++ = swab32(*src++);
882b0d0ce8bSGeert Uytterhoeven 	}
883b0d0ce8bSGeert Uytterhoeven }
884b0d0ce8bSGeert Uytterhoeven 
copy_wswap32(u32 * dst,const u32 * src,unsigned int words)885b0d0ce8bSGeert Uytterhoeven static void copy_wswap32(u32 *dst, const u32 *src, unsigned int words)
886b0d0ce8bSGeert Uytterhoeven {
887b0d0ce8bSGeert Uytterhoeven 	/* src or dst can be unaligned, but not both */
888b0d0ce8bSGeert Uytterhoeven 	if ((unsigned long)src & 3) {
889b0d0ce8bSGeert Uytterhoeven 		while (words--) {
890b0d0ce8bSGeert Uytterhoeven 			*dst++ = swahw32(get_unaligned(src));
891b0d0ce8bSGeert Uytterhoeven 			src++;
892b0d0ce8bSGeert Uytterhoeven 		}
893b0d0ce8bSGeert Uytterhoeven 	} else if ((unsigned long)dst & 3) {
894b0d0ce8bSGeert Uytterhoeven 		while (words--) {
895b0d0ce8bSGeert Uytterhoeven 			put_unaligned(swahw32(*src++), dst);
896b0d0ce8bSGeert Uytterhoeven 			dst++;
897b0d0ce8bSGeert Uytterhoeven 		}
898b0d0ce8bSGeert Uytterhoeven 	} else {
899b0d0ce8bSGeert Uytterhoeven 		while (words--)
900b0d0ce8bSGeert Uytterhoeven 			*dst++ = swahw32(*src++);
901b0d0ce8bSGeert Uytterhoeven 	}
902b0d0ce8bSGeert Uytterhoeven }
903b0d0ce8bSGeert Uytterhoeven 
copy_plain32(u32 * dst,const u32 * src,unsigned int words)904b0d0ce8bSGeert Uytterhoeven static void copy_plain32(u32 *dst, const u32 *src, unsigned int words)
905b0d0ce8bSGeert Uytterhoeven {
906b0d0ce8bSGeert Uytterhoeven 	memcpy(dst, src, words * 4);
907b0d0ce8bSGeert Uytterhoeven }
908b0d0ce8bSGeert Uytterhoeven 
sh_msiof_transfer_one(struct spi_controller * ctlr,struct spi_device * spi,struct spi_transfer * t)90935c35fd9SGeert Uytterhoeven static int sh_msiof_transfer_one(struct spi_controller *ctlr,
9101bd6363bSGeert Uytterhoeven 				 struct spi_device *spi,
9111bd6363bSGeert Uytterhoeven 				 struct spi_transfer *t)
912ca632f55SGrant Likely {
91335c35fd9SGeert Uytterhoeven 	struct sh_msiof_spi_priv *p = spi_controller_get_devdata(ctlr);
914b0d0ce8bSGeert Uytterhoeven 	void (*copy32)(u32 *, const u32 *, unsigned int);
915ca632f55SGrant Likely 	void (*tx_fifo)(struct sh_msiof_spi_priv *, const void *, int, int);
916ca632f55SGrant Likely 	void (*rx_fifo)(struct sh_msiof_spi_priv *, void *, int, int);
9170312d591SGeert Uytterhoeven 	const void *tx_buf = t->tx_buf;
9180312d591SGeert Uytterhoeven 	void *rx_buf = t->rx_buf;
9190312d591SGeert Uytterhoeven 	unsigned int len = t->len;
9200312d591SGeert Uytterhoeven 	unsigned int bits = t->bits_per_word;
9210312d591SGeert Uytterhoeven 	unsigned int bytes_per_word;
9220312d591SGeert Uytterhoeven 	unsigned int words;
923ca632f55SGrant Likely 	int n;
924ca632f55SGrant Likely 	bool swab;
925b0d0ce8bSGeert Uytterhoeven 	int ret;
926ca632f55SGrant Likely 
927fedd6940SGeert Uytterhoeven 	/* reset registers */
928fedd6940SGeert Uytterhoeven 	sh_msiof_spi_reset_regs(p);
929fedd6940SGeert Uytterhoeven 
930b0d0ce8bSGeert Uytterhoeven 	/* setup clocks (clock already enabled in chipselect()) */
9311cb3ebc4SYang Yingliang 	if (!spi_controller_is_target(p->ctlr))
9329a133f7bSGeert Uytterhoeven 		sh_msiof_spi_set_clk_regs(p, t);
933b0d0ce8bSGeert Uytterhoeven 
93435c35fd9SGeert Uytterhoeven 	while (ctlr->dma_tx && len > 15) {
935b0d0ce8bSGeert Uytterhoeven 		/*
936b0d0ce8bSGeert Uytterhoeven 		 *  DMA supports 32-bit words only, hence pack 8-bit and 16-bit
937b0d0ce8bSGeert Uytterhoeven 		 *  words, with byte resp. word swapping.
938b0d0ce8bSGeert Uytterhoeven 		 */
939fe78d0b7SKoji Matsuoka 		unsigned int l = 0;
940fe78d0b7SKoji Matsuoka 
941fe78d0b7SKoji Matsuoka 		if (tx_buf)
942d05e3eadSHoan Nguyen An 			l = min(round_down(len, 4), p->tx_fifo_size * 4);
943fe78d0b7SKoji Matsuoka 		if (rx_buf)
944d05e3eadSHoan Nguyen An 			l = min(round_down(len, 4), p->rx_fifo_size * 4);
945b0d0ce8bSGeert Uytterhoeven 
946b0d0ce8bSGeert Uytterhoeven 		if (bits <= 8) {
947b0d0ce8bSGeert Uytterhoeven 			copy32 = copy_bswap32;
948b0d0ce8bSGeert Uytterhoeven 		} else if (bits <= 16) {
949b0d0ce8bSGeert Uytterhoeven 			copy32 = copy_wswap32;
950b0d0ce8bSGeert Uytterhoeven 		} else {
951b0d0ce8bSGeert Uytterhoeven 			copy32 = copy_plain32;
952b0d0ce8bSGeert Uytterhoeven 		}
953b0d0ce8bSGeert Uytterhoeven 
954b0d0ce8bSGeert Uytterhoeven 		if (tx_buf)
955b0d0ce8bSGeert Uytterhoeven 			copy32(p->tx_dma_page, tx_buf, l / 4);
956b0d0ce8bSGeert Uytterhoeven 
957b0d0ce8bSGeert Uytterhoeven 		ret = sh_msiof_dma_once(p, tx_buf, rx_buf, l);
958279d2378SGeert Uytterhoeven 		if (ret == -EAGAIN) {
9595d8e614fSGeert Uytterhoeven 			dev_warn_once(&p->pdev->dev,
9605d8e614fSGeert Uytterhoeven 				"DMA not available, falling back to PIO\n");
961279d2378SGeert Uytterhoeven 			break;
962279d2378SGeert Uytterhoeven 		}
963b0d0ce8bSGeert Uytterhoeven 		if (ret)
964b0d0ce8bSGeert Uytterhoeven 			return ret;
965b0d0ce8bSGeert Uytterhoeven 
966b0d0ce8bSGeert Uytterhoeven 		if (rx_buf) {
967b0d0ce8bSGeert Uytterhoeven 			copy32(rx_buf, p->rx_dma_page, l / 4);
968b0d0ce8bSGeert Uytterhoeven 			rx_buf += l;
969b0d0ce8bSGeert Uytterhoeven 		}
970b0d0ce8bSGeert Uytterhoeven 		if (tx_buf)
971b0d0ce8bSGeert Uytterhoeven 			tx_buf += l;
972b0d0ce8bSGeert Uytterhoeven 
973b0d0ce8bSGeert Uytterhoeven 		len -= l;
974b0d0ce8bSGeert Uytterhoeven 		if (!len)
975b0d0ce8bSGeert Uytterhoeven 			return 0;
976b0d0ce8bSGeert Uytterhoeven 	}
977ca632f55SGrant Likely 
978916d9802SHoan Nguyen An 	if (bits <= 8 && len > 15) {
979ca632f55SGrant Likely 		bits = 32;
980ca632f55SGrant Likely 		swab = true;
981ca632f55SGrant Likely 	} else {
982ca632f55SGrant Likely 		swab = false;
983ca632f55SGrant Likely 	}
984ca632f55SGrant Likely 
985ca632f55SGrant Likely 	/* setup bytes per word and fifo read/write functions */
986ca632f55SGrant Likely 	if (bits <= 8) {
987ca632f55SGrant Likely 		bytes_per_word = 1;
988ca632f55SGrant Likely 		tx_fifo = sh_msiof_spi_write_fifo_8;
989ca632f55SGrant Likely 		rx_fifo = sh_msiof_spi_read_fifo_8;
990ca632f55SGrant Likely 	} else if (bits <= 16) {
991ca632f55SGrant Likely 		bytes_per_word = 2;
9920312d591SGeert Uytterhoeven 		if ((unsigned long)tx_buf & 0x01)
993ca632f55SGrant Likely 			tx_fifo = sh_msiof_spi_write_fifo_16u;
994ca632f55SGrant Likely 		else
995ca632f55SGrant Likely 			tx_fifo = sh_msiof_spi_write_fifo_16;
996ca632f55SGrant Likely 
9970312d591SGeert Uytterhoeven 		if ((unsigned long)rx_buf & 0x01)
998ca632f55SGrant Likely 			rx_fifo = sh_msiof_spi_read_fifo_16u;
999ca632f55SGrant Likely 		else
1000ca632f55SGrant Likely 			rx_fifo = sh_msiof_spi_read_fifo_16;
1001ca632f55SGrant Likely 	} else if (swab) {
1002ca632f55SGrant Likely 		bytes_per_word = 4;
10030312d591SGeert Uytterhoeven 		if ((unsigned long)tx_buf & 0x03)
1004ca632f55SGrant Likely 			tx_fifo = sh_msiof_spi_write_fifo_s32u;
1005ca632f55SGrant Likely 		else
1006ca632f55SGrant Likely 			tx_fifo = sh_msiof_spi_write_fifo_s32;
1007ca632f55SGrant Likely 
10080312d591SGeert Uytterhoeven 		if ((unsigned long)rx_buf & 0x03)
1009ca632f55SGrant Likely 			rx_fifo = sh_msiof_spi_read_fifo_s32u;
1010ca632f55SGrant Likely 		else
1011ca632f55SGrant Likely 			rx_fifo = sh_msiof_spi_read_fifo_s32;
1012ca632f55SGrant Likely 	} else {
1013ca632f55SGrant Likely 		bytes_per_word = 4;
10140312d591SGeert Uytterhoeven 		if ((unsigned long)tx_buf & 0x03)
1015ca632f55SGrant Likely 			tx_fifo = sh_msiof_spi_write_fifo_32u;
1016ca632f55SGrant Likely 		else
1017ca632f55SGrant Likely 			tx_fifo = sh_msiof_spi_write_fifo_32;
1018ca632f55SGrant Likely 
10190312d591SGeert Uytterhoeven 		if ((unsigned long)rx_buf & 0x03)
1020ca632f55SGrant Likely 			rx_fifo = sh_msiof_spi_read_fifo_32u;
1021ca632f55SGrant Likely 		else
1022ca632f55SGrant Likely 			rx_fifo = sh_msiof_spi_read_fifo_32;
1023ca632f55SGrant Likely 	}
1024ca632f55SGrant Likely 
1025ca632f55SGrant Likely 	/* transfer in fifo sized chunks */
10260312d591SGeert Uytterhoeven 	words = len / bytes_per_word;
1027ca632f55SGrant Likely 
10280312d591SGeert Uytterhoeven 	while (words > 0) {
10290312d591SGeert Uytterhoeven 		n = sh_msiof_spi_txrx_once(p, tx_fifo, rx_fifo, tx_buf, rx_buf,
1030ca632f55SGrant Likely 					   words, bits);
1031ca632f55SGrant Likely 		if (n < 0)
103275b82e23SGeert Uytterhoeven 			return n;
1033ca632f55SGrant Likely 
10340312d591SGeert Uytterhoeven 		if (tx_buf)
10350312d591SGeert Uytterhoeven 			tx_buf += n * bytes_per_word;
10360312d591SGeert Uytterhoeven 		if (rx_buf)
10370312d591SGeert Uytterhoeven 			rx_buf += n * bytes_per_word;
1038ca632f55SGrant Likely 		words -= n;
1039916d9802SHoan Nguyen An 
1040916d9802SHoan Nguyen An 		if (words == 0 && (len % bytes_per_word)) {
1041916d9802SHoan Nguyen An 			words = len % bytes_per_word;
1042916d9802SHoan Nguyen An 			bits = t->bits_per_word;
1043916d9802SHoan Nguyen An 			bytes_per_word = 1;
1044916d9802SHoan Nguyen An 			tx_fifo = sh_msiof_spi_write_fifo_8;
1045916d9802SHoan Nguyen An 			rx_fifo = sh_msiof_spi_read_fifo_8;
1046916d9802SHoan Nguyen An 		}
1047ca632f55SGrant Likely 	}
1048ca632f55SGrant Likely 
1049ca632f55SGrant Likely 	return 0;
1050ca632f55SGrant Likely }
1051ca632f55SGrant Likely 
105250a7e23fSGeert Uytterhoeven static const struct sh_msiof_chipdata sh_data = {
10530e836c3bSGeert Uytterhoeven 	.bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 32),
105450a7e23fSGeert Uytterhoeven 	.tx_fifo_size = 64,
105550a7e23fSGeert Uytterhoeven 	.rx_fifo_size = 64,
105635c35fd9SGeert Uytterhoeven 	.ctlr_flags = 0,
105751093cbaSVladimir Zapolskiy 	.min_div_pow = 0,
1058beb74bb0SGeert Uytterhoeven };
1059beb74bb0SGeert Uytterhoeven 
106061a8dec5SGeert Uytterhoeven static const struct sh_msiof_chipdata rcar_gen2_data = {
10610e836c3bSGeert Uytterhoeven 	.bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16) |
10620e836c3bSGeert Uytterhoeven 			      SPI_BPW_MASK(24) | SPI_BPW_MASK(32),
1063beb74bb0SGeert Uytterhoeven 	.tx_fifo_size = 64,
1064fe78d0b7SKoji Matsuoka 	.rx_fifo_size = 64,
106535c35fd9SGeert Uytterhoeven 	.ctlr_flags = SPI_CONTROLLER_MUST_TX,
106651093cbaSVladimir Zapolskiy 	.min_div_pow = 0,
106761a8dec5SGeert Uytterhoeven };
106861a8dec5SGeert Uytterhoeven 
106961a8dec5SGeert Uytterhoeven static const struct sh_msiof_chipdata rcar_gen3_data = {
10700e836c3bSGeert Uytterhoeven 	.bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16) |
10710e836c3bSGeert Uytterhoeven 			      SPI_BPW_MASK(24) | SPI_BPW_MASK(32),
107261a8dec5SGeert Uytterhoeven 	.tx_fifo_size = 64,
107361a8dec5SGeert Uytterhoeven 	.rx_fifo_size = 64,
107435c35fd9SGeert Uytterhoeven 	.ctlr_flags = SPI_CONTROLLER_MUST_TX,
107551093cbaSVladimir Zapolskiy 	.min_div_pow = 1,
107650a7e23fSGeert Uytterhoeven };
107750a7e23fSGeert Uytterhoeven 
1078ed492c47SWolfram Sang static const struct sh_msiof_chipdata rcar_r8a7795_data = {
1079ed492c47SWolfram Sang 	.bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16) |
1080ed492c47SWolfram Sang 			      SPI_BPW_MASK(24) | SPI_BPW_MASK(32),
1081ed492c47SWolfram Sang 	.tx_fifo_size = 64,
1082ed492c47SWolfram Sang 	.rx_fifo_size = 64,
1083ed492c47SWolfram Sang 	.ctlr_flags = SPI_CONTROLLER_MUST_TX,
1084ed492c47SWolfram Sang 	.min_div_pow = 1,
1085ed492c47SWolfram Sang 	.flags = SH_MSIOF_FLAG_FIXED_DTDL_200,
1086ed492c47SWolfram Sang };
1087ed492c47SWolfram Sang 
1088d946b6b0SKrzysztof Kozlowski static const struct of_device_id sh_msiof_match[] __maybe_unused = {
108950a7e23fSGeert Uytterhoeven 	{ .compatible = "renesas,sh-mobile-msiof", .data = &sh_data },
1090bdacfc7bSFabrizio Castro 	{ .compatible = "renesas,msiof-r8a7743",   .data = &rcar_gen2_data },
1091bdacfc7bSFabrizio Castro 	{ .compatible = "renesas,msiof-r8a7745",   .data = &rcar_gen2_data },
109261a8dec5SGeert Uytterhoeven 	{ .compatible = "renesas,msiof-r8a7790",   .data = &rcar_gen2_data },
109361a8dec5SGeert Uytterhoeven 	{ .compatible = "renesas,msiof-r8a7791",   .data = &rcar_gen2_data },
109461a8dec5SGeert Uytterhoeven 	{ .compatible = "renesas,msiof-r8a7792",   .data = &rcar_gen2_data },
109561a8dec5SGeert Uytterhoeven 	{ .compatible = "renesas,msiof-r8a7793",   .data = &rcar_gen2_data },
109661a8dec5SGeert Uytterhoeven 	{ .compatible = "renesas,msiof-r8a7794",   .data = &rcar_gen2_data },
109761a8dec5SGeert Uytterhoeven 	{ .compatible = "renesas,rcar-gen2-msiof", .data = &rcar_gen2_data },
1098ed492c47SWolfram Sang 	{ .compatible = "renesas,msiof-r8a7795",   .data = &rcar_r8a7795_data },
109961a8dec5SGeert Uytterhoeven 	{ .compatible = "renesas,msiof-r8a7796",   .data = &rcar_gen3_data },
110061a8dec5SGeert Uytterhoeven 	{ .compatible = "renesas,rcar-gen3-msiof", .data = &rcar_gen3_data },
1101ea9d0015SWolfram Sang 	{ .compatible = "renesas,rcar-gen4-msiof", .data = &rcar_gen3_data },
1102264c3e8dSSimon Horman 	{ .compatible = "renesas,sh-msiof",        .data = &sh_data }, /* Deprecated */
110350a7e23fSGeert Uytterhoeven 	{},
110450a7e23fSGeert Uytterhoeven };
110550a7e23fSGeert Uytterhoeven MODULE_DEVICE_TABLE(of, sh_msiof_match);
110650a7e23fSGeert Uytterhoeven 
1107cf9c86efSBastian Hecht #ifdef CONFIG_OF
sh_msiof_spi_parse_dt(struct device * dev)1108cf9c86efSBastian Hecht static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
1109cf9c86efSBastian Hecht {
1110cf9c86efSBastian Hecht 	struct sh_msiof_spi_info *info;
1111cf9c86efSBastian Hecht 	struct device_node *np = dev->of_node;
111232d3b2d1SGeert Uytterhoeven 	u32 num_cs = 1;
1113cf9c86efSBastian Hecht 
1114cf9c86efSBastian Hecht 	info = devm_kzalloc(dev, sizeof(struct sh_msiof_spi_info), GFP_KERNEL);
11151e8231b7SJingoo Han 	if (!info)
1116cf9c86efSBastian Hecht 		return NULL;
1117cf9c86efSBastian Hecht 
11181cb3ebc4SYang Yingliang 	info->mode = of_property_read_bool(np, "spi-slave") ? MSIOF_SPI_TARGET
11191cb3ebc4SYang Yingliang 							    : MSIOF_SPI_HOST;
1120cf9e4784SHisashi Nakamura 
1121cf9c86efSBastian Hecht 	/* Parse the MSIOF properties */
11221cb3ebc4SYang Yingliang 	if (info->mode == MSIOF_SPI_HOST)
1123cf9c86efSBastian Hecht 		of_property_read_u32(np, "num-cs", &num_cs);
1124cf9c86efSBastian Hecht 	of_property_read_u32(np, "renesas,tx-fifo-size",
1125cf9c86efSBastian Hecht 					&info->tx_fifo_override);
1126cf9c86efSBastian Hecht 	of_property_read_u32(np, "renesas,rx-fifo-size",
1127cf9c86efSBastian Hecht 					&info->rx_fifo_override);
11283110628dSYoshihiro Shimoda 	of_property_read_u32(np, "renesas,dtdl", &info->dtdl);
11293110628dSYoshihiro Shimoda 	of_property_read_u32(np, "renesas,syncdl", &info->syncdl);
1130cf9c86efSBastian Hecht 
1131cf9c86efSBastian Hecht 	info->num_chipselect = num_cs;
1132cf9c86efSBastian Hecht 
1133cf9c86efSBastian Hecht 	return info;
1134cf9c86efSBastian Hecht }
1135cf9c86efSBastian Hecht #else
sh_msiof_spi_parse_dt(struct device * dev)1136cf9c86efSBastian Hecht static struct sh_msiof_spi_info *sh_msiof_spi_parse_dt(struct device *dev)
1137cf9c86efSBastian Hecht {
1138cf9c86efSBastian Hecht 	return NULL;
1139cf9c86efSBastian Hecht }
1140cf9c86efSBastian Hecht #endif
1141cf9c86efSBastian Hecht 
sh_msiof_request_dma_chan(struct device * dev,enum dma_transfer_direction dir,unsigned int id,dma_addr_t port_addr)1142b0d0ce8bSGeert Uytterhoeven static struct dma_chan *sh_msiof_request_dma_chan(struct device *dev,
1143b0d0ce8bSGeert Uytterhoeven 	enum dma_transfer_direction dir, unsigned int id, dma_addr_t port_addr)
1144b0d0ce8bSGeert Uytterhoeven {
1145b0d0ce8bSGeert Uytterhoeven 	dma_cap_mask_t mask;
1146b0d0ce8bSGeert Uytterhoeven 	struct dma_chan *chan;
1147b0d0ce8bSGeert Uytterhoeven 	struct dma_slave_config cfg;
1148b0d0ce8bSGeert Uytterhoeven 	int ret;
1149b0d0ce8bSGeert Uytterhoeven 
1150b0d0ce8bSGeert Uytterhoeven 	dma_cap_zero(mask);
1151b0d0ce8bSGeert Uytterhoeven 	dma_cap_set(DMA_SLAVE, mask);
1152b0d0ce8bSGeert Uytterhoeven 
1153a6be4de6SGeert Uytterhoeven 	chan = dma_request_slave_channel_compat(mask, shdma_chan_filter,
1154a6be4de6SGeert Uytterhoeven 				(void *)(unsigned long)id, dev,
1155a6be4de6SGeert Uytterhoeven 				dir == DMA_MEM_TO_DEV ? "tx" : "rx");
1156b0d0ce8bSGeert Uytterhoeven 	if (!chan) {
1157a6be4de6SGeert Uytterhoeven 		dev_warn(dev, "dma_request_slave_channel_compat failed\n");
1158b0d0ce8bSGeert Uytterhoeven 		return NULL;
1159b0d0ce8bSGeert Uytterhoeven 	}
1160b0d0ce8bSGeert Uytterhoeven 
1161b0d0ce8bSGeert Uytterhoeven 	memset(&cfg, 0, sizeof(cfg));
1162b0d0ce8bSGeert Uytterhoeven 	cfg.direction = dir;
116352fba2b8SGeert Uytterhoeven 	if (dir == DMA_MEM_TO_DEV) {
1164b0d0ce8bSGeert Uytterhoeven 		cfg.dst_addr = port_addr;
116552fba2b8SGeert Uytterhoeven 		cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
116652fba2b8SGeert Uytterhoeven 	} else {
1167b0d0ce8bSGeert Uytterhoeven 		cfg.src_addr = port_addr;
116852fba2b8SGeert Uytterhoeven 		cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
116952fba2b8SGeert Uytterhoeven 	}
1170b0d0ce8bSGeert Uytterhoeven 
1171b0d0ce8bSGeert Uytterhoeven 	ret = dmaengine_slave_config(chan, &cfg);
1172b0d0ce8bSGeert Uytterhoeven 	if (ret) {
1173b0d0ce8bSGeert Uytterhoeven 		dev_warn(dev, "dmaengine_slave_config failed %d\n", ret);
1174b0d0ce8bSGeert Uytterhoeven 		dma_release_channel(chan);
1175b0d0ce8bSGeert Uytterhoeven 		return NULL;
1176b0d0ce8bSGeert Uytterhoeven 	}
1177b0d0ce8bSGeert Uytterhoeven 
1178b0d0ce8bSGeert Uytterhoeven 	return chan;
1179b0d0ce8bSGeert Uytterhoeven }
1180b0d0ce8bSGeert Uytterhoeven 
sh_msiof_request_dma(struct sh_msiof_spi_priv * p)1181b0d0ce8bSGeert Uytterhoeven static int sh_msiof_request_dma(struct sh_msiof_spi_priv *p)
1182b0d0ce8bSGeert Uytterhoeven {
1183b0d0ce8bSGeert Uytterhoeven 	struct platform_device *pdev = p->pdev;
1184b0d0ce8bSGeert Uytterhoeven 	struct device *dev = &pdev->dev;
1185f70351aeSHoan Nguyen An 	const struct sh_msiof_spi_info *info = p->info;
1186a6be4de6SGeert Uytterhoeven 	unsigned int dma_tx_id, dma_rx_id;
1187b0d0ce8bSGeert Uytterhoeven 	const struct resource *res;
118835c35fd9SGeert Uytterhoeven 	struct spi_controller *ctlr;
11895dabcf2fSGeert Uytterhoeven 	struct device *tx_dev, *rx_dev;
1190b0d0ce8bSGeert Uytterhoeven 
1191a6be4de6SGeert Uytterhoeven 	if (dev->of_node) {
1192a6be4de6SGeert Uytterhoeven 		/* In the OF case we will get the slave IDs from the DT */
1193a6be4de6SGeert Uytterhoeven 		dma_tx_id = 0;
1194a6be4de6SGeert Uytterhoeven 		dma_rx_id = 0;
1195a6be4de6SGeert Uytterhoeven 	} else if (info && info->dma_tx_id && info->dma_rx_id) {
1196a6be4de6SGeert Uytterhoeven 		dma_tx_id = info->dma_tx_id;
1197a6be4de6SGeert Uytterhoeven 		dma_rx_id = info->dma_rx_id;
1198a6be4de6SGeert Uytterhoeven 	} else {
1199a6be4de6SGeert Uytterhoeven 		/* The driver assumes no error */
1200a6be4de6SGeert Uytterhoeven 		return 0;
1201a6be4de6SGeert Uytterhoeven 	}
1202b0d0ce8bSGeert Uytterhoeven 
1203b0d0ce8bSGeert Uytterhoeven 	/* The DMA engine uses the second register set, if present */
1204b0d0ce8bSGeert Uytterhoeven 	res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1205b0d0ce8bSGeert Uytterhoeven 	if (!res)
1206b0d0ce8bSGeert Uytterhoeven 		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1207b0d0ce8bSGeert Uytterhoeven 
120835c35fd9SGeert Uytterhoeven 	ctlr = p->ctlr;
120935c35fd9SGeert Uytterhoeven 	ctlr->dma_tx = sh_msiof_request_dma_chan(dev, DMA_MEM_TO_DEV,
12108ae7d442SKrzysztof Kozlowski 						 dma_tx_id, res->start + SITFDR);
121135c35fd9SGeert Uytterhoeven 	if (!ctlr->dma_tx)
1212b0d0ce8bSGeert Uytterhoeven 		return -ENODEV;
1213b0d0ce8bSGeert Uytterhoeven 
121435c35fd9SGeert Uytterhoeven 	ctlr->dma_rx = sh_msiof_request_dma_chan(dev, DMA_DEV_TO_MEM,
12158ae7d442SKrzysztof Kozlowski 						 dma_rx_id, res->start + SIRFDR);
121635c35fd9SGeert Uytterhoeven 	if (!ctlr->dma_rx)
1217b0d0ce8bSGeert Uytterhoeven 		goto free_tx_chan;
1218b0d0ce8bSGeert Uytterhoeven 
1219b0d0ce8bSGeert Uytterhoeven 	p->tx_dma_page = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
1220b0d0ce8bSGeert Uytterhoeven 	if (!p->tx_dma_page)
1221b0d0ce8bSGeert Uytterhoeven 		goto free_rx_chan;
1222b0d0ce8bSGeert Uytterhoeven 
1223b0d0ce8bSGeert Uytterhoeven 	p->rx_dma_page = (void *)__get_free_page(GFP_KERNEL | GFP_DMA);
1224b0d0ce8bSGeert Uytterhoeven 	if (!p->rx_dma_page)
1225b0d0ce8bSGeert Uytterhoeven 		goto free_tx_page;
1226b0d0ce8bSGeert Uytterhoeven 
122735c35fd9SGeert Uytterhoeven 	tx_dev = ctlr->dma_tx->device->dev;
12285dabcf2fSGeert Uytterhoeven 	p->tx_dma_addr = dma_map_single(tx_dev, p->tx_dma_page, PAGE_SIZE,
1229b0d0ce8bSGeert Uytterhoeven 					DMA_TO_DEVICE);
12305dabcf2fSGeert Uytterhoeven 	if (dma_mapping_error(tx_dev, p->tx_dma_addr))
1231b0d0ce8bSGeert Uytterhoeven 		goto free_rx_page;
1232b0d0ce8bSGeert Uytterhoeven 
123335c35fd9SGeert Uytterhoeven 	rx_dev = ctlr->dma_rx->device->dev;
12345dabcf2fSGeert Uytterhoeven 	p->rx_dma_addr = dma_map_single(rx_dev, p->rx_dma_page, PAGE_SIZE,
1235b0d0ce8bSGeert Uytterhoeven 					DMA_FROM_DEVICE);
12365dabcf2fSGeert Uytterhoeven 	if (dma_mapping_error(rx_dev, p->rx_dma_addr))
1237b0d0ce8bSGeert Uytterhoeven 		goto unmap_tx_page;
1238b0d0ce8bSGeert Uytterhoeven 
1239b0d0ce8bSGeert Uytterhoeven 	dev_info(dev, "DMA available");
1240b0d0ce8bSGeert Uytterhoeven 	return 0;
1241b0d0ce8bSGeert Uytterhoeven 
1242b0d0ce8bSGeert Uytterhoeven unmap_tx_page:
12435dabcf2fSGeert Uytterhoeven 	dma_unmap_single(tx_dev, p->tx_dma_addr, PAGE_SIZE, DMA_TO_DEVICE);
1244b0d0ce8bSGeert Uytterhoeven free_rx_page:
1245b0d0ce8bSGeert Uytterhoeven 	free_page((unsigned long)p->rx_dma_page);
1246b0d0ce8bSGeert Uytterhoeven free_tx_page:
1247b0d0ce8bSGeert Uytterhoeven 	free_page((unsigned long)p->tx_dma_page);
1248b0d0ce8bSGeert Uytterhoeven free_rx_chan:
124935c35fd9SGeert Uytterhoeven 	dma_release_channel(ctlr->dma_rx);
1250b0d0ce8bSGeert Uytterhoeven free_tx_chan:
125135c35fd9SGeert Uytterhoeven 	dma_release_channel(ctlr->dma_tx);
125235c35fd9SGeert Uytterhoeven 	ctlr->dma_tx = NULL;
1253b0d0ce8bSGeert Uytterhoeven 	return -ENODEV;
1254b0d0ce8bSGeert Uytterhoeven }
1255b0d0ce8bSGeert Uytterhoeven 
sh_msiof_release_dma(struct sh_msiof_spi_priv * p)1256b0d0ce8bSGeert Uytterhoeven static void sh_msiof_release_dma(struct sh_msiof_spi_priv *p)
1257b0d0ce8bSGeert Uytterhoeven {
125835c35fd9SGeert Uytterhoeven 	struct spi_controller *ctlr = p->ctlr;
1259b0d0ce8bSGeert Uytterhoeven 
126035c35fd9SGeert Uytterhoeven 	if (!ctlr->dma_tx)
1261b0d0ce8bSGeert Uytterhoeven 		return;
1262b0d0ce8bSGeert Uytterhoeven 
126335c35fd9SGeert Uytterhoeven 	dma_unmap_single(ctlr->dma_rx->device->dev, p->rx_dma_addr, PAGE_SIZE,
126435c35fd9SGeert Uytterhoeven 			 DMA_FROM_DEVICE);
126535c35fd9SGeert Uytterhoeven 	dma_unmap_single(ctlr->dma_tx->device->dev, p->tx_dma_addr, PAGE_SIZE,
126635c35fd9SGeert Uytterhoeven 			 DMA_TO_DEVICE);
1267b0d0ce8bSGeert Uytterhoeven 	free_page((unsigned long)p->rx_dma_page);
1268b0d0ce8bSGeert Uytterhoeven 	free_page((unsigned long)p->tx_dma_page);
126935c35fd9SGeert Uytterhoeven 	dma_release_channel(ctlr->dma_rx);
127035c35fd9SGeert Uytterhoeven 	dma_release_channel(ctlr->dma_tx);
1271b0d0ce8bSGeert Uytterhoeven }
1272b0d0ce8bSGeert Uytterhoeven 
sh_msiof_spi_probe(struct platform_device * pdev)1273ca632f55SGrant Likely static int sh_msiof_spi_probe(struct platform_device *pdev)
1274ca632f55SGrant Likely {
127535c35fd9SGeert Uytterhoeven 	struct spi_controller *ctlr;
1276a6802cc0SGeert Uytterhoeven 	const struct sh_msiof_chipdata *chipdata;
1277cf9e4784SHisashi Nakamura 	struct sh_msiof_spi_info *info;
1278ca632f55SGrant Likely 	struct sh_msiof_spi_priv *p;
127981f68479SGeert Uytterhoeven 	unsigned long clksrc;
1280ca632f55SGrant Likely 	int i;
1281ca632f55SGrant Likely 	int ret;
1282ca632f55SGrant Likely 
1283ecb1596aSGeert Uytterhoeven 	chipdata = of_device_get_match_data(&pdev->dev);
1284ecb1596aSGeert Uytterhoeven 	if (chipdata) {
1285cf9e4784SHisashi Nakamura 		info = sh_msiof_spi_parse_dt(&pdev->dev);
1286cf9e4784SHisashi Nakamura 	} else {
1287cf9e4784SHisashi Nakamura 		chipdata = (const void *)pdev->id_entry->driver_data;
1288cf9e4784SHisashi Nakamura 		info = dev_get_platdata(&pdev->dev);
1289cf9e4784SHisashi Nakamura 	}
1290cf9e4784SHisashi Nakamura 
1291cf9e4784SHisashi Nakamura 	if (!info) {
1292cf9e4784SHisashi Nakamura 		dev_err(&pdev->dev, "failed to obtain device info\n");
1293cf9e4784SHisashi Nakamura 		return -ENXIO;
1294cf9e4784SHisashi Nakamura 	}
1295cf9e4784SHisashi Nakamura 
1296ed492c47SWolfram Sang 	if (chipdata->flags & SH_MSIOF_FLAG_FIXED_DTDL_200)
1297ed492c47SWolfram Sang 		info->dtdl = 200;
1298ed492c47SWolfram Sang 
12991cb3ebc4SYang Yingliang 	if (info->mode == MSIOF_SPI_TARGET)
13001cb3ebc4SYang Yingliang 		ctlr = spi_alloc_target(&pdev->dev,
1301cf9e4784SHisashi Nakamura 				        sizeof(struct sh_msiof_spi_priv));
1302cf9e4784SHisashi Nakamura 	else
13031cb3ebc4SYang Yingliang 		ctlr = spi_alloc_host(&pdev->dev,
1304cf9e4784SHisashi Nakamura 				      sizeof(struct sh_msiof_spi_priv));
130535c35fd9SGeert Uytterhoeven 	if (ctlr == NULL)
1306b4dd05deSLaurent Pinchart 		return -ENOMEM;
1307ca632f55SGrant Likely 
130835c35fd9SGeert Uytterhoeven 	p = spi_controller_get_devdata(ctlr);
1309ca632f55SGrant Likely 
1310ca632f55SGrant Likely 	platform_set_drvdata(pdev, p);
131135c35fd9SGeert Uytterhoeven 	p->ctlr = ctlr;
1312cf9e4784SHisashi Nakamura 	p->info = info;
131351093cbaSVladimir Zapolskiy 	p->min_div_pow = chipdata->min_div_pow;
1314cf9c86efSBastian Hecht 
1315ca632f55SGrant Likely 	init_completion(&p->done);
131608ba7ae3SGeert Uytterhoeven 	init_completion(&p->done_txdma);
1317ca632f55SGrant Likely 
1318b4dd05deSLaurent Pinchart 	p->clk = devm_clk_get(&pdev->dev, NULL);
1319ca632f55SGrant Likely 	if (IS_ERR(p->clk)) {
1320078b6eadSBastian Hecht 		dev_err(&pdev->dev, "cannot get clock\n");
1321ca632f55SGrant Likely 		ret = PTR_ERR(p->clk);
1322ca632f55SGrant Likely 		goto err1;
1323ca632f55SGrant Likely 	}
1324ca632f55SGrant Likely 
1325ca632f55SGrant Likely 	i = platform_get_irq(pdev, 0);
1326b4dd05deSLaurent Pinchart 	if (i < 0) {
1327f34c6e62SSergei Shtylyov 		ret = i;
1328b4dd05deSLaurent Pinchart 		goto err1;
1329ca632f55SGrant Likely 	}
1330ca632f55SGrant Likely 
1331920d947aSGeert Uytterhoeven 	p->mapbase = devm_platform_ioremap_resource(pdev, 0);
1332b4dd05deSLaurent Pinchart 	if (IS_ERR(p->mapbase)) {
1333b4dd05deSLaurent Pinchart 		ret = PTR_ERR(p->mapbase);
1334b4dd05deSLaurent Pinchart 		goto err1;
1335b4dd05deSLaurent Pinchart 	}
1336b4dd05deSLaurent Pinchart 
1337b4dd05deSLaurent Pinchart 	ret = devm_request_irq(&pdev->dev, i, sh_msiof_spi_irq, 0,
1338ca632f55SGrant Likely 			       dev_name(&pdev->dev), p);
1339ca632f55SGrant Likely 	if (ret) {
1340ca632f55SGrant Likely 		dev_err(&pdev->dev, "unable to request irq\n");
1341b4dd05deSLaurent Pinchart 		goto err1;
1342ca632f55SGrant Likely 	}
1343ca632f55SGrant Likely 
1344ca632f55SGrant Likely 	p->pdev = pdev;
1345ca632f55SGrant Likely 	pm_runtime_enable(&pdev->dev);
1346ca632f55SGrant Likely 
1347ca632f55SGrant Likely 	/* Platform data may override FIFO sizes */
1348a6802cc0SGeert Uytterhoeven 	p->tx_fifo_size = chipdata->tx_fifo_size;
1349a6802cc0SGeert Uytterhoeven 	p->rx_fifo_size = chipdata->rx_fifo_size;
1350ca632f55SGrant Likely 	if (p->info->tx_fifo_override)
1351ca632f55SGrant Likely 		p->tx_fifo_size = p->info->tx_fifo_override;
1352ca632f55SGrant Likely 	if (p->info->rx_fifo_override)
1353ca632f55SGrant Likely 		p->rx_fifo_size = p->info->rx_fifo_override;
1354ca632f55SGrant Likely 
135535c35fd9SGeert Uytterhoeven 	/* init controller code */
135635c35fd9SGeert Uytterhoeven 	ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
135735c35fd9SGeert Uytterhoeven 	ctlr->mode_bits |= SPI_LSB_FIRST | SPI_3WIRE;
135881f68479SGeert Uytterhoeven 	clksrc = clk_get_rate(p->clk);
135981f68479SGeert Uytterhoeven 	ctlr->min_speed_hz = DIV_ROUND_UP(clksrc, 1024);
136081f68479SGeert Uytterhoeven 	ctlr->max_speed_hz = DIV_ROUND_UP(clksrc, 1 << p->min_div_pow);
136135c35fd9SGeert Uytterhoeven 	ctlr->flags = chipdata->ctlr_flags;
136235c35fd9SGeert Uytterhoeven 	ctlr->bus_num = pdev->id;
1363aa32f76eSGeert Uytterhoeven 	ctlr->num_chipselect = p->info->num_chipselect;
136435c35fd9SGeert Uytterhoeven 	ctlr->dev.of_node = pdev->dev.of_node;
136535c35fd9SGeert Uytterhoeven 	ctlr->setup = sh_msiof_spi_setup;
136635c35fd9SGeert Uytterhoeven 	ctlr->prepare_message = sh_msiof_prepare_message;
13671cb3ebc4SYang Yingliang 	ctlr->target_abort = sh_msiof_target_abort;
13680e836c3bSGeert Uytterhoeven 	ctlr->bits_per_word_mask = chipdata->bits_per_word_mask;
136935c35fd9SGeert Uytterhoeven 	ctlr->auto_runtime_pm = true;
137035c35fd9SGeert Uytterhoeven 	ctlr->transfer_one = sh_msiof_transfer_one;
13719fda6693SGeert Uytterhoeven 	ctlr->use_gpio_descriptors = true;
1372aa32f76eSGeert Uytterhoeven 	ctlr->max_native_cs = MAX_SS;
1373ca632f55SGrant Likely 
1374b0d0ce8bSGeert Uytterhoeven 	ret = sh_msiof_request_dma(p);
1375b0d0ce8bSGeert Uytterhoeven 	if (ret < 0)
1376b0d0ce8bSGeert Uytterhoeven 		dev_warn(&pdev->dev, "DMA not available, using PIO\n");
1377b0d0ce8bSGeert Uytterhoeven 
137835c35fd9SGeert Uytterhoeven 	ret = devm_spi_register_controller(&pdev->dev, ctlr);
13791bd6363bSGeert Uytterhoeven 	if (ret < 0) {
138035c35fd9SGeert Uytterhoeven 		dev_err(&pdev->dev, "devm_spi_register_controller error.\n");
13811bd6363bSGeert Uytterhoeven 		goto err2;
13821bd6363bSGeert Uytterhoeven 	}
1383ca632f55SGrant Likely 
1384ca632f55SGrant Likely 	return 0;
1385ca632f55SGrant Likely 
13861bd6363bSGeert Uytterhoeven  err2:
1387b0d0ce8bSGeert Uytterhoeven 	sh_msiof_release_dma(p);
1388ca632f55SGrant Likely 	pm_runtime_disable(&pdev->dev);
1389ca632f55SGrant Likely  err1:
139035c35fd9SGeert Uytterhoeven 	spi_controller_put(ctlr);
1391ca632f55SGrant Likely 	return ret;
1392ca632f55SGrant Likely }
1393ca632f55SGrant Likely 
sh_msiof_spi_remove(struct platform_device * pdev)139474af1328SUwe Kleine-König static void sh_msiof_spi_remove(struct platform_device *pdev)
1395ca632f55SGrant Likely {
1396b0d0ce8bSGeert Uytterhoeven 	struct sh_msiof_spi_priv *p = platform_get_drvdata(pdev);
1397b0d0ce8bSGeert Uytterhoeven 
1398b0d0ce8bSGeert Uytterhoeven 	sh_msiof_release_dma(p);
1399ca632f55SGrant Likely 	pm_runtime_disable(&pdev->dev);
1400ca632f55SGrant Likely }
1401ca632f55SGrant Likely 
14023789c852SKrzysztof Kozlowski static const struct platform_device_id spi_driver_ids[] = {
140350a7e23fSGeert Uytterhoeven 	{ "spi_sh_msiof",	(kernel_ulong_t)&sh_data },
1404cf9c86efSBastian Hecht 	{},
1405cf9c86efSBastian Hecht };
140650a7e23fSGeert Uytterhoeven MODULE_DEVICE_TABLE(platform, spi_driver_ids);
1407cf9c86efSBastian Hecht 
1408ffa69d6aSGaku Inami #ifdef CONFIG_PM_SLEEP
sh_msiof_spi_suspend(struct device * dev)1409ffa69d6aSGaku Inami static int sh_msiof_spi_suspend(struct device *dev)
1410ffa69d6aSGaku Inami {
141107c7df3eSWolfram Sang 	struct sh_msiof_spi_priv *p = dev_get_drvdata(dev);
1412ffa69d6aSGaku Inami 
141335c35fd9SGeert Uytterhoeven 	return spi_controller_suspend(p->ctlr);
1414ffa69d6aSGaku Inami }
1415ffa69d6aSGaku Inami 
sh_msiof_spi_resume(struct device * dev)1416ffa69d6aSGaku Inami static int sh_msiof_spi_resume(struct device *dev)
1417ffa69d6aSGaku Inami {
141807c7df3eSWolfram Sang 	struct sh_msiof_spi_priv *p = dev_get_drvdata(dev);
1419ffa69d6aSGaku Inami 
142035c35fd9SGeert Uytterhoeven 	return spi_controller_resume(p->ctlr);
1421ffa69d6aSGaku Inami }
1422ffa69d6aSGaku Inami 
1423ffa69d6aSGaku Inami static SIMPLE_DEV_PM_OPS(sh_msiof_spi_pm_ops, sh_msiof_spi_suspend,
1424ffa69d6aSGaku Inami 			 sh_msiof_spi_resume);
142521fb1f41SAishwarya R #define DEV_PM_OPS	(&sh_msiof_spi_pm_ops)
1426ffa69d6aSGaku Inami #else
1427ffa69d6aSGaku Inami #define DEV_PM_OPS	NULL
1428ffa69d6aSGaku Inami #endif /* CONFIG_PM_SLEEP */
1429ffa69d6aSGaku Inami 
1430ca632f55SGrant Likely static struct platform_driver sh_msiof_spi_drv = {
1431ca632f55SGrant Likely 	.probe		= sh_msiof_spi_probe,
143274af1328SUwe Kleine-König 	.remove_new	= sh_msiof_spi_remove,
143350a7e23fSGeert Uytterhoeven 	.id_table	= spi_driver_ids,
1434ca632f55SGrant Likely 	.driver		= {
1435ca632f55SGrant Likely 		.name		= "spi_sh_msiof",
1436ffa69d6aSGaku Inami 		.pm		= DEV_PM_OPS,
1437691ee4edSSachin Kamat 		.of_match_table = of_match_ptr(sh_msiof_match),
1438ca632f55SGrant Likely 	},
1439ca632f55SGrant Likely };
1440940ab889SGrant Likely module_platform_driver(sh_msiof_spi_drv);
1441ca632f55SGrant Likely 
144235c35fd9SGeert Uytterhoeven MODULE_DESCRIPTION("SuperH MSIOF SPI Controller Interface Driver");
1443ca632f55SGrant Likely MODULE_AUTHOR("Magnus Damm");
1444ca632f55SGrant Likely MODULE_LICENSE("GPL v2");
1445