xref: /linux/drivers/spi/spi-omap2-mcspi.c (revision 5cbc7ca987fb3f293203dc14a6c53b91b7c978a5)
1ca632f55SGrant Likely /*
2ca632f55SGrant Likely  * OMAP2 McSPI controller driver
3ca632f55SGrant Likely  *
4ca632f55SGrant Likely  * Copyright (C) 2005, 2006 Nokia Corporation
5ca632f55SGrant Likely  * Author:	Samuel Ortiz <samuel.ortiz@nokia.com> and
6ca632f55SGrant Likely  *		Juha Yrj�l� <juha.yrjola@nokia.com>
7ca632f55SGrant Likely  *
8ca632f55SGrant Likely  * This program is free software; you can redistribute it and/or modify
9ca632f55SGrant Likely  * it under the terms of the GNU General Public License as published by
10ca632f55SGrant Likely  * the Free Software Foundation; either version 2 of the License, or
11ca632f55SGrant Likely  * (at your option) any later version.
12ca632f55SGrant Likely  *
13ca632f55SGrant Likely  * This program is distributed in the hope that it will be useful,
14ca632f55SGrant Likely  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15ca632f55SGrant Likely  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16ca632f55SGrant Likely  * GNU General Public License for more details.
17ca632f55SGrant Likely  *
18ca632f55SGrant Likely  * You should have received a copy of the GNU General Public License
19ca632f55SGrant Likely  * along with this program; if not, write to the Free Software
20ca632f55SGrant Likely  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21ca632f55SGrant Likely  *
22ca632f55SGrant Likely  */
23ca632f55SGrant Likely 
24ca632f55SGrant Likely #include <linux/kernel.h>
25ca632f55SGrant Likely #include <linux/init.h>
26ca632f55SGrant Likely #include <linux/interrupt.h>
27ca632f55SGrant Likely #include <linux/module.h>
28ca632f55SGrant Likely #include <linux/device.h>
29ca632f55SGrant Likely #include <linux/delay.h>
30ca632f55SGrant Likely #include <linux/dma-mapping.h>
3153741ed8SRussell King #include <linux/dmaengine.h>
3253741ed8SRussell King #include <linux/omap-dma.h>
33ca632f55SGrant Likely #include <linux/platform_device.h>
34ca632f55SGrant Likely #include <linux/err.h>
35ca632f55SGrant Likely #include <linux/clk.h>
36ca632f55SGrant Likely #include <linux/io.h>
37ca632f55SGrant Likely #include <linux/slab.h>
38ca632f55SGrant Likely #include <linux/pm_runtime.h>
39d5a80031SBenoit Cousson #include <linux/of.h>
40d5a80031SBenoit Cousson #include <linux/of_device.h>
41ec155afaSMatt Porter #include <linux/pinctrl/consumer.h>
42ca632f55SGrant Likely 
43ca632f55SGrant Likely #include <linux/spi/spi.h>
44ca632f55SGrant Likely 
452203747cSArnd Bergmann #include <linux/platform_data/spi-omap2-mcspi.h>
46ca632f55SGrant Likely 
47ca632f55SGrant Likely #define OMAP2_MCSPI_MAX_FREQ		48000000
4827b5284cSShubhrajyoti D #define SPI_AUTOSUSPEND_TIMEOUT		2000
49ca632f55SGrant Likely 
50ca632f55SGrant Likely #define OMAP2_MCSPI_REVISION		0x00
51ca632f55SGrant Likely #define OMAP2_MCSPI_SYSSTATUS		0x14
52ca632f55SGrant Likely #define OMAP2_MCSPI_IRQSTATUS		0x18
53ca632f55SGrant Likely #define OMAP2_MCSPI_IRQENABLE		0x1c
54ca632f55SGrant Likely #define OMAP2_MCSPI_WAKEUPENABLE	0x20
55ca632f55SGrant Likely #define OMAP2_MCSPI_SYST		0x24
56ca632f55SGrant Likely #define OMAP2_MCSPI_MODULCTRL		0x28
57ca632f55SGrant Likely 
58ca632f55SGrant Likely /* per-channel banks, 0x14 bytes each, first is: */
59ca632f55SGrant Likely #define OMAP2_MCSPI_CHCONF0		0x2c
60ca632f55SGrant Likely #define OMAP2_MCSPI_CHSTAT0		0x30
61ca632f55SGrant Likely #define OMAP2_MCSPI_CHCTRL0		0x34
62ca632f55SGrant Likely #define OMAP2_MCSPI_TX0			0x38
63ca632f55SGrant Likely #define OMAP2_MCSPI_RX0			0x3c
64ca632f55SGrant Likely 
65ca632f55SGrant Likely /* per-register bitmasks: */
66ca632f55SGrant Likely 
67ca632f55SGrant Likely #define OMAP2_MCSPI_MODULCTRL_SINGLE	BIT(0)
68ca632f55SGrant Likely #define OMAP2_MCSPI_MODULCTRL_MS	BIT(2)
69ca632f55SGrant Likely #define OMAP2_MCSPI_MODULCTRL_STEST	BIT(3)
70ca632f55SGrant Likely 
71ca632f55SGrant Likely #define OMAP2_MCSPI_CHCONF_PHA		BIT(0)
72ca632f55SGrant Likely #define OMAP2_MCSPI_CHCONF_POL		BIT(1)
73ca632f55SGrant Likely #define OMAP2_MCSPI_CHCONF_CLKD_MASK	(0x0f << 2)
74ca632f55SGrant Likely #define OMAP2_MCSPI_CHCONF_EPOL		BIT(6)
75ca632f55SGrant Likely #define OMAP2_MCSPI_CHCONF_WL_MASK	(0x1f << 7)
76ca632f55SGrant Likely #define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY	BIT(12)
77ca632f55SGrant Likely #define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY	BIT(13)
78ca632f55SGrant Likely #define OMAP2_MCSPI_CHCONF_TRM_MASK	(0x03 << 12)
79ca632f55SGrant Likely #define OMAP2_MCSPI_CHCONF_DMAW		BIT(14)
80ca632f55SGrant Likely #define OMAP2_MCSPI_CHCONF_DMAR		BIT(15)
81ca632f55SGrant Likely #define OMAP2_MCSPI_CHCONF_DPE0		BIT(16)
82ca632f55SGrant Likely #define OMAP2_MCSPI_CHCONF_DPE1		BIT(17)
83ca632f55SGrant Likely #define OMAP2_MCSPI_CHCONF_IS		BIT(18)
84ca632f55SGrant Likely #define OMAP2_MCSPI_CHCONF_TURBO	BIT(19)
85ca632f55SGrant Likely #define OMAP2_MCSPI_CHCONF_FORCE	BIT(20)
86ca632f55SGrant Likely 
87ca632f55SGrant Likely #define OMAP2_MCSPI_CHSTAT_RXS		BIT(0)
88ca632f55SGrant Likely #define OMAP2_MCSPI_CHSTAT_TXS		BIT(1)
89ca632f55SGrant Likely #define OMAP2_MCSPI_CHSTAT_EOT		BIT(2)
90ca632f55SGrant Likely 
91ca632f55SGrant Likely #define OMAP2_MCSPI_CHCTRL_EN		BIT(0)
92ca632f55SGrant Likely 
93ca632f55SGrant Likely #define OMAP2_MCSPI_WAKEUPENABLE_WKEN	BIT(0)
94ca632f55SGrant Likely 
95ca632f55SGrant Likely /* We have 2 DMA channels per CS, one for RX and one for TX */
96ca632f55SGrant Likely struct omap2_mcspi_dma {
9753741ed8SRussell King 	struct dma_chan *dma_tx;
9853741ed8SRussell King 	struct dma_chan *dma_rx;
99ca632f55SGrant Likely 
100ca632f55SGrant Likely 	int dma_tx_sync_dev;
101ca632f55SGrant Likely 	int dma_rx_sync_dev;
102ca632f55SGrant Likely 
103ca632f55SGrant Likely 	struct completion dma_tx_completion;
104ca632f55SGrant Likely 	struct completion dma_rx_completion;
105ca632f55SGrant Likely };
106ca632f55SGrant Likely 
107ca632f55SGrant Likely /* use PIO for small transfers, avoiding DMA setup/teardown overhead and
108ca632f55SGrant Likely  * cache operations; better heuristics consider wordsize and bitrate.
109ca632f55SGrant Likely  */
110ca632f55SGrant Likely #define DMA_MIN_BYTES			160
111ca632f55SGrant Likely 
112ca632f55SGrant Likely 
1131bd897f8SBenoit Cousson /*
1141bd897f8SBenoit Cousson  * Used for context save and restore, structure members to be updated whenever
1151bd897f8SBenoit Cousson  * corresponding registers are modified.
1161bd897f8SBenoit Cousson  */
1171bd897f8SBenoit Cousson struct omap2_mcspi_regs {
1181bd897f8SBenoit Cousson 	u32 modulctrl;
1191bd897f8SBenoit Cousson 	u32 wakeupenable;
1201bd897f8SBenoit Cousson 	struct list_head cs;
1211bd897f8SBenoit Cousson };
1221bd897f8SBenoit Cousson 
123ca632f55SGrant Likely struct omap2_mcspi {
124ca632f55SGrant Likely 	struct spi_master	*master;
125ca632f55SGrant Likely 	/* Virtual base address of the controller */
126ca632f55SGrant Likely 	void __iomem		*base;
127ca632f55SGrant Likely 	unsigned long		phys;
128ca632f55SGrant Likely 	/* SPI1 has 4 channels, while SPI2 has 2 */
129ca632f55SGrant Likely 	struct omap2_mcspi_dma	*dma_channels;
130ca632f55SGrant Likely 	struct device		*dev;
1311bd897f8SBenoit Cousson 	struct omap2_mcspi_regs ctx;
1320384e90bSDaniel Mack 	unsigned int		pin_dir:1;
133ca632f55SGrant Likely };
134ca632f55SGrant Likely 
135ca632f55SGrant Likely struct omap2_mcspi_cs {
136ca632f55SGrant Likely 	void __iomem		*base;
137ca632f55SGrant Likely 	unsigned long		phys;
138ca632f55SGrant Likely 	int			word_len;
139ca632f55SGrant Likely 	struct list_head	node;
140ca632f55SGrant Likely 	/* Context save and restore shadow register */
141ca632f55SGrant Likely 	u32			chconf0;
142ca632f55SGrant Likely };
143ca632f55SGrant Likely 
144ca632f55SGrant Likely static inline void mcspi_write_reg(struct spi_master *master,
145ca632f55SGrant Likely 		int idx, u32 val)
146ca632f55SGrant Likely {
147ca632f55SGrant Likely 	struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
148ca632f55SGrant Likely 
149ca632f55SGrant Likely 	__raw_writel(val, mcspi->base + idx);
150ca632f55SGrant Likely }
151ca632f55SGrant Likely 
152ca632f55SGrant Likely static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
153ca632f55SGrant Likely {
154ca632f55SGrant Likely 	struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
155ca632f55SGrant Likely 
156ca632f55SGrant Likely 	return __raw_readl(mcspi->base + idx);
157ca632f55SGrant Likely }
158ca632f55SGrant Likely 
159ca632f55SGrant Likely static inline void mcspi_write_cs_reg(const struct spi_device *spi,
160ca632f55SGrant Likely 		int idx, u32 val)
161ca632f55SGrant Likely {
162ca632f55SGrant Likely 	struct omap2_mcspi_cs	*cs = spi->controller_state;
163ca632f55SGrant Likely 
164ca632f55SGrant Likely 	__raw_writel(val, cs->base +  idx);
165ca632f55SGrant Likely }
166ca632f55SGrant Likely 
167ca632f55SGrant Likely static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
168ca632f55SGrant Likely {
169ca632f55SGrant Likely 	struct omap2_mcspi_cs	*cs = spi->controller_state;
170ca632f55SGrant Likely 
171ca632f55SGrant Likely 	return __raw_readl(cs->base + idx);
172ca632f55SGrant Likely }
173ca632f55SGrant Likely 
174ca632f55SGrant Likely static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
175ca632f55SGrant Likely {
176ca632f55SGrant Likely 	struct omap2_mcspi_cs *cs = spi->controller_state;
177ca632f55SGrant Likely 
178ca632f55SGrant Likely 	return cs->chconf0;
179ca632f55SGrant Likely }
180ca632f55SGrant Likely 
181ca632f55SGrant Likely static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
182ca632f55SGrant Likely {
183ca632f55SGrant Likely 	struct omap2_mcspi_cs *cs = spi->controller_state;
184ca632f55SGrant Likely 
185ca632f55SGrant Likely 	cs->chconf0 = val;
186ca632f55SGrant Likely 	mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
187ca632f55SGrant Likely 	mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
188ca632f55SGrant Likely }
189ca632f55SGrant Likely 
190ca632f55SGrant Likely static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
191ca632f55SGrant Likely 		int is_read, int enable)
192ca632f55SGrant Likely {
193ca632f55SGrant Likely 	u32 l, rw;
194ca632f55SGrant Likely 
195ca632f55SGrant Likely 	l = mcspi_cached_chconf0(spi);
196ca632f55SGrant Likely 
197ca632f55SGrant Likely 	if (is_read) /* 1 is read, 0 write */
198ca632f55SGrant Likely 		rw = OMAP2_MCSPI_CHCONF_DMAR;
199ca632f55SGrant Likely 	else
200ca632f55SGrant Likely 		rw = OMAP2_MCSPI_CHCONF_DMAW;
201ca632f55SGrant Likely 
202af4e944dSShubhrajyoti D 	if (enable)
203af4e944dSShubhrajyoti D 		l |= rw;
204af4e944dSShubhrajyoti D 	else
205af4e944dSShubhrajyoti D 		l &= ~rw;
206af4e944dSShubhrajyoti D 
207ca632f55SGrant Likely 	mcspi_write_chconf0(spi, l);
208ca632f55SGrant Likely }
209ca632f55SGrant Likely 
210ca632f55SGrant Likely static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
211ca632f55SGrant Likely {
212ca632f55SGrant Likely 	u32 l;
213ca632f55SGrant Likely 
214ca632f55SGrant Likely 	l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
215ca632f55SGrant Likely 	mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
216ca632f55SGrant Likely 	/* Flash post-writes */
217ca632f55SGrant Likely 	mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
218ca632f55SGrant Likely }
219ca632f55SGrant Likely 
220ca632f55SGrant Likely static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
221ca632f55SGrant Likely {
222ca632f55SGrant Likely 	u32 l;
223ca632f55SGrant Likely 
224ca632f55SGrant Likely 	l = mcspi_cached_chconf0(spi);
225af4e944dSShubhrajyoti D 	if (cs_active)
226af4e944dSShubhrajyoti D 		l |= OMAP2_MCSPI_CHCONF_FORCE;
227af4e944dSShubhrajyoti D 	else
228af4e944dSShubhrajyoti D 		l &= ~OMAP2_MCSPI_CHCONF_FORCE;
229af4e944dSShubhrajyoti D 
230ca632f55SGrant Likely 	mcspi_write_chconf0(spi, l);
231ca632f55SGrant Likely }
232ca632f55SGrant Likely 
233ca632f55SGrant Likely static void omap2_mcspi_set_master_mode(struct spi_master *master)
234ca632f55SGrant Likely {
2351bd897f8SBenoit Cousson 	struct omap2_mcspi	*mcspi = spi_master_get_devdata(master);
2361bd897f8SBenoit Cousson 	struct omap2_mcspi_regs	*ctx = &mcspi->ctx;
237ca632f55SGrant Likely 	u32 l;
238ca632f55SGrant Likely 
2391bd897f8SBenoit Cousson 	/*
2401bd897f8SBenoit Cousson 	 * Setup when switching from (reset default) slave mode
241ca632f55SGrant Likely 	 * to single-channel master mode
242ca632f55SGrant Likely 	 */
243ca632f55SGrant Likely 	l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
244af4e944dSShubhrajyoti D 	l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
245af4e944dSShubhrajyoti D 	l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
246ca632f55SGrant Likely 	mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
247ca632f55SGrant Likely 
2481bd897f8SBenoit Cousson 	ctx->modulctrl = l;
249ca632f55SGrant Likely }
250ca632f55SGrant Likely 
251ca632f55SGrant Likely static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
252ca632f55SGrant Likely {
2531bd897f8SBenoit Cousson 	struct spi_master	*spi_cntrl = mcspi->master;
2541bd897f8SBenoit Cousson 	struct omap2_mcspi_regs	*ctx = &mcspi->ctx;
255ca632f55SGrant Likely 	struct omap2_mcspi_cs	*cs;
256ca632f55SGrant Likely 
257ca632f55SGrant Likely 	/* McSPI: context restore */
2581bd897f8SBenoit Cousson 	mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
2591bd897f8SBenoit Cousson 	mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
260ca632f55SGrant Likely 
2611bd897f8SBenoit Cousson 	list_for_each_entry(cs, &ctx->cs, node)
262ca632f55SGrant Likely 		__raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
263ca632f55SGrant Likely }
264ca632f55SGrant Likely 
2655fda88f5SShubhrajyoti D static int omap2_prepare_transfer(struct spi_master *master)
2665fda88f5SShubhrajyoti D {
2675fda88f5SShubhrajyoti D 	struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
2685fda88f5SShubhrajyoti D 
2695fda88f5SShubhrajyoti D 	pm_runtime_get_sync(mcspi->dev);
2705fda88f5SShubhrajyoti D 	return 0;
2715fda88f5SShubhrajyoti D }
2725fda88f5SShubhrajyoti D 
2735fda88f5SShubhrajyoti D static int omap2_unprepare_transfer(struct spi_master *master)
2745fda88f5SShubhrajyoti D {
2755fda88f5SShubhrajyoti D 	struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
2765fda88f5SShubhrajyoti D 
2775fda88f5SShubhrajyoti D 	pm_runtime_mark_last_busy(mcspi->dev);
2785fda88f5SShubhrajyoti D 	pm_runtime_put_autosuspend(mcspi->dev);
2795fda88f5SShubhrajyoti D 	return 0;
2805fda88f5SShubhrajyoti D }
2815fda88f5SShubhrajyoti D 
282ca632f55SGrant Likely static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
283ca632f55SGrant Likely {
284ca632f55SGrant Likely 	unsigned long timeout;
285ca632f55SGrant Likely 
286ca632f55SGrant Likely 	timeout = jiffies + msecs_to_jiffies(1000);
287ca632f55SGrant Likely 	while (!(__raw_readl(reg) & bit)) {
288ca632f55SGrant Likely 		if (time_after(jiffies, timeout))
289ca632f55SGrant Likely 			return -1;
290ca632f55SGrant Likely 		cpu_relax();
291ca632f55SGrant Likely 	}
292ca632f55SGrant Likely 	return 0;
293ca632f55SGrant Likely }
294ca632f55SGrant Likely 
29553741ed8SRussell King static void omap2_mcspi_rx_callback(void *data)
29653741ed8SRussell King {
29753741ed8SRussell King 	struct spi_device *spi = data;
29853741ed8SRussell King 	struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
29953741ed8SRussell King 	struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
30053741ed8SRussell King 
30153741ed8SRussell King 	complete(&mcspi_dma->dma_rx_completion);
30253741ed8SRussell King 
30353741ed8SRussell King 	/* We must disable the DMA RX request */
30453741ed8SRussell King 	omap2_mcspi_set_dma_req(spi, 1, 0);
30553741ed8SRussell King }
30653741ed8SRussell King 
30753741ed8SRussell King static void omap2_mcspi_tx_callback(void *data)
30853741ed8SRussell King {
30953741ed8SRussell King 	struct spi_device *spi = data;
31053741ed8SRussell King 	struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
31153741ed8SRussell King 	struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
31253741ed8SRussell King 
31353741ed8SRussell King 	complete(&mcspi_dma->dma_tx_completion);
31453741ed8SRussell King 
31553741ed8SRussell King 	/* We must disable the DMA TX request */
31653741ed8SRussell King 	omap2_mcspi_set_dma_req(spi, 0, 0);
31753741ed8SRussell King }
31853741ed8SRussell King 
319d7b4394eSShubhrajyoti D static void omap2_mcspi_tx_dma(struct spi_device *spi,
320d7b4394eSShubhrajyoti D 				struct spi_transfer *xfer,
321d7b4394eSShubhrajyoti D 				struct dma_slave_config cfg)
322ca632f55SGrant Likely {
323ca632f55SGrant Likely 	struct omap2_mcspi	*mcspi;
324ca632f55SGrant Likely 	struct omap2_mcspi_dma  *mcspi_dma;
3258c7494a5SRussell King 	unsigned int		count;
326ca632f55SGrant Likely 
327ca632f55SGrant Likely 	mcspi = spi_master_get_devdata(spi->master);
328ca632f55SGrant Likely 	mcspi_dma = &mcspi->dma_channels[spi->chip_select];
329d7b4394eSShubhrajyoti D 	count = xfer->len;
330ca632f55SGrant Likely 
331d7b4394eSShubhrajyoti D 	if (mcspi_dma->dma_tx) {
33253741ed8SRussell King 		struct dma_async_tx_descriptor *tx;
33353741ed8SRussell King 		struct scatterlist sg;
33453741ed8SRussell King 
33553741ed8SRussell King 		dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
33653741ed8SRussell King 
33753741ed8SRussell King 		sg_init_table(&sg, 1);
33853741ed8SRussell King 		sg_dma_address(&sg) = xfer->tx_dma;
33953741ed8SRussell King 		sg_dma_len(&sg) = xfer->len;
34053741ed8SRussell King 
34153741ed8SRussell King 		tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
34253741ed8SRussell King 		DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
34353741ed8SRussell King 		if (tx) {
34453741ed8SRussell King 			tx->callback = omap2_mcspi_tx_callback;
34553741ed8SRussell King 			tx->callback_param = spi;
34653741ed8SRussell King 			dmaengine_submit(tx);
34753741ed8SRussell King 		} else {
34853741ed8SRussell King 			/* FIXME: fall back to PIO? */
34953741ed8SRussell King 		}
35053741ed8SRussell King 	}
35153741ed8SRussell King 	dma_async_issue_pending(mcspi_dma->dma_tx);
352ca632f55SGrant Likely 	omap2_mcspi_set_dma_req(spi, 0, 1);
353ca632f55SGrant Likely 
354ca632f55SGrant Likely }
355ca632f55SGrant Likely 
356d7b4394eSShubhrajyoti D static unsigned
357d7b4394eSShubhrajyoti D omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
358d7b4394eSShubhrajyoti D 				struct dma_slave_config cfg,
359d7b4394eSShubhrajyoti D 				unsigned es)
360d7b4394eSShubhrajyoti D {
361d7b4394eSShubhrajyoti D 	struct omap2_mcspi	*mcspi;
362d7b4394eSShubhrajyoti D 	struct omap2_mcspi_dma  *mcspi_dma;
363d7b4394eSShubhrajyoti D 	unsigned int		count;
364d7b4394eSShubhrajyoti D 	u32			l;
365d7b4394eSShubhrajyoti D 	int			elements = 0;
366d7b4394eSShubhrajyoti D 	int			word_len, element_count;
367d7b4394eSShubhrajyoti D 	struct omap2_mcspi_cs	*cs = spi->controller_state;
368d7b4394eSShubhrajyoti D 	mcspi = spi_master_get_devdata(spi->master);
369d7b4394eSShubhrajyoti D 	mcspi_dma = &mcspi->dma_channels[spi->chip_select];
370d7b4394eSShubhrajyoti D 	count = xfer->len;
371d7b4394eSShubhrajyoti D 	word_len = cs->word_len;
372d7b4394eSShubhrajyoti D 	l = mcspi_cached_chconf0(spi);
373d7b4394eSShubhrajyoti D 
374d7b4394eSShubhrajyoti D 	if (word_len <= 8)
375d7b4394eSShubhrajyoti D 		element_count = count;
376d7b4394eSShubhrajyoti D 	else if (word_len <= 16)
377d7b4394eSShubhrajyoti D 		element_count = count >> 1;
378d7b4394eSShubhrajyoti D 	else /* word_len <= 32 */
379d7b4394eSShubhrajyoti D 		element_count = count >> 2;
380d7b4394eSShubhrajyoti D 
381d7b4394eSShubhrajyoti D 	if (mcspi_dma->dma_rx) {
382d7b4394eSShubhrajyoti D 		struct dma_async_tx_descriptor *tx;
383d7b4394eSShubhrajyoti D 		struct scatterlist sg;
384d7b4394eSShubhrajyoti D 		size_t len = xfer->len - es;
385d7b4394eSShubhrajyoti D 
386d7b4394eSShubhrajyoti D 		dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
387d7b4394eSShubhrajyoti D 
388d7b4394eSShubhrajyoti D 		if (l & OMAP2_MCSPI_CHCONF_TURBO)
389d7b4394eSShubhrajyoti D 			len -= es;
390d7b4394eSShubhrajyoti D 
391d7b4394eSShubhrajyoti D 		sg_init_table(&sg, 1);
392d7b4394eSShubhrajyoti D 		sg_dma_address(&sg) = xfer->rx_dma;
393d7b4394eSShubhrajyoti D 		sg_dma_len(&sg) = len;
394d7b4394eSShubhrajyoti D 
395d7b4394eSShubhrajyoti D 		tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
396d7b4394eSShubhrajyoti D 				DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
397d7b4394eSShubhrajyoti D 				DMA_CTRL_ACK);
398d7b4394eSShubhrajyoti D 		if (tx) {
399d7b4394eSShubhrajyoti D 			tx->callback = omap2_mcspi_rx_callback;
400d7b4394eSShubhrajyoti D 			tx->callback_param = spi;
401d7b4394eSShubhrajyoti D 			dmaengine_submit(tx);
402d7b4394eSShubhrajyoti D 		} else {
403d7b4394eSShubhrajyoti D 				/* FIXME: fall back to PIO? */
404d7b4394eSShubhrajyoti D 		}
405d7b4394eSShubhrajyoti D 	}
406d7b4394eSShubhrajyoti D 
407d7b4394eSShubhrajyoti D 	dma_async_issue_pending(mcspi_dma->dma_rx);
408d7b4394eSShubhrajyoti D 	omap2_mcspi_set_dma_req(spi, 1, 1);
409d7b4394eSShubhrajyoti D 
410ca632f55SGrant Likely 	wait_for_completion(&mcspi_dma->dma_rx_completion);
411a3ce9a80SShubhrajyoti D 	dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
412a3ce9a80SShubhrajyoti D 			 DMA_FROM_DEVICE);
413ca632f55SGrant Likely 	omap2_mcspi_set_enable(spi, 0);
414ca632f55SGrant Likely 
41553741ed8SRussell King 	elements = element_count - 1;
41653741ed8SRussell King 
417ca632f55SGrant Likely 	if (l & OMAP2_MCSPI_CHCONF_TURBO) {
41853741ed8SRussell King 		elements--;
419ca632f55SGrant Likely 
420ca632f55SGrant Likely 		if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
421ca632f55SGrant Likely 				   & OMAP2_MCSPI_CHSTAT_RXS)) {
422ca632f55SGrant Likely 			u32 w;
423ca632f55SGrant Likely 
424ca632f55SGrant Likely 			w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
425ca632f55SGrant Likely 			if (word_len <= 8)
426ca632f55SGrant Likely 				((u8 *)xfer->rx_buf)[elements++] = w;
427ca632f55SGrant Likely 			else if (word_len <= 16)
428ca632f55SGrant Likely 				((u16 *)xfer->rx_buf)[elements++] = w;
429ca632f55SGrant Likely 			else /* word_len <= 32 */
430ca632f55SGrant Likely 				((u32 *)xfer->rx_buf)[elements++] = w;
431ca632f55SGrant Likely 		} else {
432d7b4394eSShubhrajyoti D 			dev_err(&spi->dev, "DMA RX penultimate word empty");
433ca632f55SGrant Likely 			count -= (word_len <= 8)  ? 2 :
434ca632f55SGrant Likely 				(word_len <= 16) ? 4 :
435ca632f55SGrant Likely 				/* word_len <= 32 */ 8;
436ca632f55SGrant Likely 			omap2_mcspi_set_enable(spi, 1);
437ca632f55SGrant Likely 			return count;
438ca632f55SGrant Likely 		}
439ca632f55SGrant Likely 	}
440ca632f55SGrant Likely 	if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
441ca632f55SGrant Likely 				& OMAP2_MCSPI_CHSTAT_RXS)) {
442ca632f55SGrant Likely 		u32 w;
443ca632f55SGrant Likely 
444ca632f55SGrant Likely 		w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
445ca632f55SGrant Likely 		if (word_len <= 8)
446ca632f55SGrant Likely 			((u8 *)xfer->rx_buf)[elements] = w;
447ca632f55SGrant Likely 		else if (word_len <= 16)
448ca632f55SGrant Likely 			((u16 *)xfer->rx_buf)[elements] = w;
449ca632f55SGrant Likely 		else /* word_len <= 32 */
450ca632f55SGrant Likely 			((u32 *)xfer->rx_buf)[elements] = w;
451ca632f55SGrant Likely 	} else {
452ca632f55SGrant Likely 		dev_err(&spi->dev, "DMA RX last word empty");
453ca632f55SGrant Likely 		count -= (word_len <= 8)  ? 1 :
454ca632f55SGrant Likely 			 (word_len <= 16) ? 2 :
455ca632f55SGrant Likely 		       /* word_len <= 32 */ 4;
456ca632f55SGrant Likely 	}
457ca632f55SGrant Likely 	omap2_mcspi_set_enable(spi, 1);
458d7b4394eSShubhrajyoti D 	return count;
459ca632f55SGrant Likely }
460d7b4394eSShubhrajyoti D 
461d7b4394eSShubhrajyoti D static unsigned
462d7b4394eSShubhrajyoti D omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
463d7b4394eSShubhrajyoti D {
464d7b4394eSShubhrajyoti D 	struct omap2_mcspi	*mcspi;
465d7b4394eSShubhrajyoti D 	struct omap2_mcspi_cs	*cs = spi->controller_state;
466d7b4394eSShubhrajyoti D 	struct omap2_mcspi_dma  *mcspi_dma;
467d7b4394eSShubhrajyoti D 	unsigned int		count;
468d7b4394eSShubhrajyoti D 	u32			l;
469d7b4394eSShubhrajyoti D 	u8			*rx;
470d7b4394eSShubhrajyoti D 	const u8		*tx;
471d7b4394eSShubhrajyoti D 	struct dma_slave_config	cfg;
472d7b4394eSShubhrajyoti D 	enum dma_slave_buswidth width;
473d7b4394eSShubhrajyoti D 	unsigned es;
474e47a682aSShubhrajyoti D 	void __iomem		*chstat_reg;
475d7b4394eSShubhrajyoti D 
476d7b4394eSShubhrajyoti D 	mcspi = spi_master_get_devdata(spi->master);
477d7b4394eSShubhrajyoti D 	mcspi_dma = &mcspi->dma_channels[spi->chip_select];
478d7b4394eSShubhrajyoti D 	l = mcspi_cached_chconf0(spi);
479d7b4394eSShubhrajyoti D 
480d7b4394eSShubhrajyoti D 
481d7b4394eSShubhrajyoti D 	if (cs->word_len <= 8) {
482d7b4394eSShubhrajyoti D 		width = DMA_SLAVE_BUSWIDTH_1_BYTE;
483d7b4394eSShubhrajyoti D 		es = 1;
484d7b4394eSShubhrajyoti D 	} else if (cs->word_len <= 16) {
485d7b4394eSShubhrajyoti D 		width = DMA_SLAVE_BUSWIDTH_2_BYTES;
486d7b4394eSShubhrajyoti D 		es = 2;
487d7b4394eSShubhrajyoti D 	} else {
488d7b4394eSShubhrajyoti D 		width = DMA_SLAVE_BUSWIDTH_4_BYTES;
489d7b4394eSShubhrajyoti D 		es = 4;
490d7b4394eSShubhrajyoti D 	}
491d7b4394eSShubhrajyoti D 
492d7b4394eSShubhrajyoti D 	memset(&cfg, 0, sizeof(cfg));
493d7b4394eSShubhrajyoti D 	cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
494d7b4394eSShubhrajyoti D 	cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
495d7b4394eSShubhrajyoti D 	cfg.src_addr_width = width;
496d7b4394eSShubhrajyoti D 	cfg.dst_addr_width = width;
497d7b4394eSShubhrajyoti D 	cfg.src_maxburst = 1;
498d7b4394eSShubhrajyoti D 	cfg.dst_maxburst = 1;
499d7b4394eSShubhrajyoti D 
500d7b4394eSShubhrajyoti D 	rx = xfer->rx_buf;
501d7b4394eSShubhrajyoti D 	tx = xfer->tx_buf;
502d7b4394eSShubhrajyoti D 
503d7b4394eSShubhrajyoti D 	count = xfer->len;
504d7b4394eSShubhrajyoti D 
505d7b4394eSShubhrajyoti D 	if (tx != NULL)
506d7b4394eSShubhrajyoti D 		omap2_mcspi_tx_dma(spi, xfer, cfg);
507d7b4394eSShubhrajyoti D 
508d7b4394eSShubhrajyoti D 	if (rx != NULL)
509e47a682aSShubhrajyoti D 		count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
510d7b4394eSShubhrajyoti D 
511e47a682aSShubhrajyoti D 	if (tx != NULL) {
512e47a682aSShubhrajyoti D 		chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
513e47a682aSShubhrajyoti D 		wait_for_completion(&mcspi_dma->dma_tx_completion);
514e47a682aSShubhrajyoti D 		dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len,
515e47a682aSShubhrajyoti D 				 DMA_TO_DEVICE);
516e47a682aSShubhrajyoti D 
517e47a682aSShubhrajyoti D 		/* for TX_ONLY mode, be sure all words have shifted out */
518e47a682aSShubhrajyoti D 		if (rx == NULL) {
519e47a682aSShubhrajyoti D 			if (mcspi_wait_for_reg_bit(chstat_reg,
520e47a682aSShubhrajyoti D 						OMAP2_MCSPI_CHSTAT_TXS) < 0)
521e47a682aSShubhrajyoti D 				dev_err(&spi->dev, "TXS timed out\n");
522e47a682aSShubhrajyoti D 			else if (mcspi_wait_for_reg_bit(chstat_reg,
523e47a682aSShubhrajyoti D 						OMAP2_MCSPI_CHSTAT_EOT) < 0)
524e47a682aSShubhrajyoti D 				dev_err(&spi->dev, "EOT timed out\n");
525e47a682aSShubhrajyoti D 		}
526e47a682aSShubhrajyoti D 	}
527ca632f55SGrant Likely 	return count;
528ca632f55SGrant Likely }
529ca632f55SGrant Likely 
530ca632f55SGrant Likely static unsigned
531ca632f55SGrant Likely omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
532ca632f55SGrant Likely {
533ca632f55SGrant Likely 	struct omap2_mcspi	*mcspi;
534ca632f55SGrant Likely 	struct omap2_mcspi_cs	*cs = spi->controller_state;
535ca632f55SGrant Likely 	unsigned int		count, c;
536ca632f55SGrant Likely 	u32			l;
537ca632f55SGrant Likely 	void __iomem		*base = cs->base;
538ca632f55SGrant Likely 	void __iomem		*tx_reg;
539ca632f55SGrant Likely 	void __iomem		*rx_reg;
540ca632f55SGrant Likely 	void __iomem		*chstat_reg;
541ca632f55SGrant Likely 	int			word_len;
542ca632f55SGrant Likely 
543ca632f55SGrant Likely 	mcspi = spi_master_get_devdata(spi->master);
544ca632f55SGrant Likely 	count = xfer->len;
545ca632f55SGrant Likely 	c = count;
546ca632f55SGrant Likely 	word_len = cs->word_len;
547ca632f55SGrant Likely 
548ca632f55SGrant Likely 	l = mcspi_cached_chconf0(spi);
549ca632f55SGrant Likely 
550ca632f55SGrant Likely 	/* We store the pre-calculated register addresses on stack to speed
551ca632f55SGrant Likely 	 * up the transfer loop. */
552ca632f55SGrant Likely 	tx_reg		= base + OMAP2_MCSPI_TX0;
553ca632f55SGrant Likely 	rx_reg		= base + OMAP2_MCSPI_RX0;
554ca632f55SGrant Likely 	chstat_reg	= base + OMAP2_MCSPI_CHSTAT0;
555ca632f55SGrant Likely 
556ca632f55SGrant Likely 	if (c < (word_len>>3))
557ca632f55SGrant Likely 		return 0;
558ca632f55SGrant Likely 
559ca632f55SGrant Likely 	if (word_len <= 8) {
560ca632f55SGrant Likely 		u8		*rx;
561ca632f55SGrant Likely 		const u8	*tx;
562ca632f55SGrant Likely 
563ca632f55SGrant Likely 		rx = xfer->rx_buf;
564ca632f55SGrant Likely 		tx = xfer->tx_buf;
565ca632f55SGrant Likely 
566ca632f55SGrant Likely 		do {
567ca632f55SGrant Likely 			c -= 1;
568ca632f55SGrant Likely 			if (tx != NULL) {
569ca632f55SGrant Likely 				if (mcspi_wait_for_reg_bit(chstat_reg,
570ca632f55SGrant Likely 						OMAP2_MCSPI_CHSTAT_TXS) < 0) {
571ca632f55SGrant Likely 					dev_err(&spi->dev, "TXS timed out\n");
572ca632f55SGrant Likely 					goto out;
573ca632f55SGrant Likely 				}
574ca632f55SGrant Likely 				dev_vdbg(&spi->dev, "write-%d %02x\n",
575ca632f55SGrant Likely 						word_len, *tx);
576ca632f55SGrant Likely 				__raw_writel(*tx++, tx_reg);
577ca632f55SGrant Likely 			}
578ca632f55SGrant Likely 			if (rx != NULL) {
579ca632f55SGrant Likely 				if (mcspi_wait_for_reg_bit(chstat_reg,
580ca632f55SGrant Likely 						OMAP2_MCSPI_CHSTAT_RXS) < 0) {
581ca632f55SGrant Likely 					dev_err(&spi->dev, "RXS timed out\n");
582ca632f55SGrant Likely 					goto out;
583ca632f55SGrant Likely 				}
584ca632f55SGrant Likely 
585ca632f55SGrant Likely 				if (c == 1 && tx == NULL &&
586ca632f55SGrant Likely 				    (l & OMAP2_MCSPI_CHCONF_TURBO)) {
587ca632f55SGrant Likely 					omap2_mcspi_set_enable(spi, 0);
588ca632f55SGrant Likely 					*rx++ = __raw_readl(rx_reg);
589ca632f55SGrant Likely 					dev_vdbg(&spi->dev, "read-%d %02x\n",
590ca632f55SGrant Likely 						    word_len, *(rx - 1));
591ca632f55SGrant Likely 					if (mcspi_wait_for_reg_bit(chstat_reg,
592ca632f55SGrant Likely 						OMAP2_MCSPI_CHSTAT_RXS) < 0) {
593ca632f55SGrant Likely 						dev_err(&spi->dev,
594ca632f55SGrant Likely 							"RXS timed out\n");
595ca632f55SGrant Likely 						goto out;
596ca632f55SGrant Likely 					}
597ca632f55SGrant Likely 					c = 0;
598ca632f55SGrant Likely 				} else if (c == 0 && tx == NULL) {
599ca632f55SGrant Likely 					omap2_mcspi_set_enable(spi, 0);
600ca632f55SGrant Likely 				}
601ca632f55SGrant Likely 
602ca632f55SGrant Likely 				*rx++ = __raw_readl(rx_reg);
603ca632f55SGrant Likely 				dev_vdbg(&spi->dev, "read-%d %02x\n",
604ca632f55SGrant Likely 						word_len, *(rx - 1));
605ca632f55SGrant Likely 			}
606ca632f55SGrant Likely 		} while (c);
607ca632f55SGrant Likely 	} else if (word_len <= 16) {
608ca632f55SGrant Likely 		u16		*rx;
609ca632f55SGrant Likely 		const u16	*tx;
610ca632f55SGrant Likely 
611ca632f55SGrant Likely 		rx = xfer->rx_buf;
612ca632f55SGrant Likely 		tx = xfer->tx_buf;
613ca632f55SGrant Likely 		do {
614ca632f55SGrant Likely 			c -= 2;
615ca632f55SGrant Likely 			if (tx != NULL) {
616ca632f55SGrant Likely 				if (mcspi_wait_for_reg_bit(chstat_reg,
617ca632f55SGrant Likely 						OMAP2_MCSPI_CHSTAT_TXS) < 0) {
618ca632f55SGrant Likely 					dev_err(&spi->dev, "TXS timed out\n");
619ca632f55SGrant Likely 					goto out;
620ca632f55SGrant Likely 				}
621ca632f55SGrant Likely 				dev_vdbg(&spi->dev, "write-%d %04x\n",
622ca632f55SGrant Likely 						word_len, *tx);
623ca632f55SGrant Likely 				__raw_writel(*tx++, tx_reg);
624ca632f55SGrant Likely 			}
625ca632f55SGrant Likely 			if (rx != NULL) {
626ca632f55SGrant Likely 				if (mcspi_wait_for_reg_bit(chstat_reg,
627ca632f55SGrant Likely 						OMAP2_MCSPI_CHSTAT_RXS) < 0) {
628ca632f55SGrant Likely 					dev_err(&spi->dev, "RXS timed out\n");
629ca632f55SGrant Likely 					goto out;
630ca632f55SGrant Likely 				}
631ca632f55SGrant Likely 
632ca632f55SGrant Likely 				if (c == 2 && tx == NULL &&
633ca632f55SGrant Likely 				    (l & OMAP2_MCSPI_CHCONF_TURBO)) {
634ca632f55SGrant Likely 					omap2_mcspi_set_enable(spi, 0);
635ca632f55SGrant Likely 					*rx++ = __raw_readl(rx_reg);
636ca632f55SGrant Likely 					dev_vdbg(&spi->dev, "read-%d %04x\n",
637ca632f55SGrant Likely 						    word_len, *(rx - 1));
638ca632f55SGrant Likely 					if (mcspi_wait_for_reg_bit(chstat_reg,
639ca632f55SGrant Likely 						OMAP2_MCSPI_CHSTAT_RXS) < 0) {
640ca632f55SGrant Likely 						dev_err(&spi->dev,
641ca632f55SGrant Likely 							"RXS timed out\n");
642ca632f55SGrant Likely 						goto out;
643ca632f55SGrant Likely 					}
644ca632f55SGrant Likely 					c = 0;
645ca632f55SGrant Likely 				} else if (c == 0 && tx == NULL) {
646ca632f55SGrant Likely 					omap2_mcspi_set_enable(spi, 0);
647ca632f55SGrant Likely 				}
648ca632f55SGrant Likely 
649ca632f55SGrant Likely 				*rx++ = __raw_readl(rx_reg);
650ca632f55SGrant Likely 				dev_vdbg(&spi->dev, "read-%d %04x\n",
651ca632f55SGrant Likely 						word_len, *(rx - 1));
652ca632f55SGrant Likely 			}
653ca632f55SGrant Likely 		} while (c >= 2);
654ca632f55SGrant Likely 	} else if (word_len <= 32) {
655ca632f55SGrant Likely 		u32		*rx;
656ca632f55SGrant Likely 		const u32	*tx;
657ca632f55SGrant Likely 
658ca632f55SGrant Likely 		rx = xfer->rx_buf;
659ca632f55SGrant Likely 		tx = xfer->tx_buf;
660ca632f55SGrant Likely 		do {
661ca632f55SGrant Likely 			c -= 4;
662ca632f55SGrant Likely 			if (tx != NULL) {
663ca632f55SGrant Likely 				if (mcspi_wait_for_reg_bit(chstat_reg,
664ca632f55SGrant Likely 						OMAP2_MCSPI_CHSTAT_TXS) < 0) {
665ca632f55SGrant Likely 					dev_err(&spi->dev, "TXS timed out\n");
666ca632f55SGrant Likely 					goto out;
667ca632f55SGrant Likely 				}
668ca632f55SGrant Likely 				dev_vdbg(&spi->dev, "write-%d %08x\n",
669ca632f55SGrant Likely 						word_len, *tx);
670ca632f55SGrant Likely 				__raw_writel(*tx++, tx_reg);
671ca632f55SGrant Likely 			}
672ca632f55SGrant Likely 			if (rx != NULL) {
673ca632f55SGrant Likely 				if (mcspi_wait_for_reg_bit(chstat_reg,
674ca632f55SGrant Likely 						OMAP2_MCSPI_CHSTAT_RXS) < 0) {
675ca632f55SGrant Likely 					dev_err(&spi->dev, "RXS timed out\n");
676ca632f55SGrant Likely 					goto out;
677ca632f55SGrant Likely 				}
678ca632f55SGrant Likely 
679ca632f55SGrant Likely 				if (c == 4 && tx == NULL &&
680ca632f55SGrant Likely 				    (l & OMAP2_MCSPI_CHCONF_TURBO)) {
681ca632f55SGrant Likely 					omap2_mcspi_set_enable(spi, 0);
682ca632f55SGrant Likely 					*rx++ = __raw_readl(rx_reg);
683ca632f55SGrant Likely 					dev_vdbg(&spi->dev, "read-%d %08x\n",
684ca632f55SGrant Likely 						    word_len, *(rx - 1));
685ca632f55SGrant Likely 					if (mcspi_wait_for_reg_bit(chstat_reg,
686ca632f55SGrant Likely 						OMAP2_MCSPI_CHSTAT_RXS) < 0) {
687ca632f55SGrant Likely 						dev_err(&spi->dev,
688ca632f55SGrant Likely 							"RXS timed out\n");
689ca632f55SGrant Likely 						goto out;
690ca632f55SGrant Likely 					}
691ca632f55SGrant Likely 					c = 0;
692ca632f55SGrant Likely 				} else if (c == 0 && tx == NULL) {
693ca632f55SGrant Likely 					omap2_mcspi_set_enable(spi, 0);
694ca632f55SGrant Likely 				}
695ca632f55SGrant Likely 
696ca632f55SGrant Likely 				*rx++ = __raw_readl(rx_reg);
697ca632f55SGrant Likely 				dev_vdbg(&spi->dev, "read-%d %08x\n",
698ca632f55SGrant Likely 						word_len, *(rx - 1));
699ca632f55SGrant Likely 			}
700ca632f55SGrant Likely 		} while (c >= 4);
701ca632f55SGrant Likely 	}
702ca632f55SGrant Likely 
703ca632f55SGrant Likely 	/* for TX_ONLY mode, be sure all words have shifted out */
704ca632f55SGrant Likely 	if (xfer->rx_buf == NULL) {
705ca632f55SGrant Likely 		if (mcspi_wait_for_reg_bit(chstat_reg,
706ca632f55SGrant Likely 				OMAP2_MCSPI_CHSTAT_TXS) < 0) {
707ca632f55SGrant Likely 			dev_err(&spi->dev, "TXS timed out\n");
708ca632f55SGrant Likely 		} else if (mcspi_wait_for_reg_bit(chstat_reg,
709ca632f55SGrant Likely 				OMAP2_MCSPI_CHSTAT_EOT) < 0)
710ca632f55SGrant Likely 			dev_err(&spi->dev, "EOT timed out\n");
711ca632f55SGrant Likely 
712ca632f55SGrant Likely 		/* disable chan to purge rx datas received in TX_ONLY transfer,
713ca632f55SGrant Likely 		 * otherwise these rx datas will affect the direct following
714ca632f55SGrant Likely 		 * RX_ONLY transfer.
715ca632f55SGrant Likely 		 */
716ca632f55SGrant Likely 		omap2_mcspi_set_enable(spi, 0);
717ca632f55SGrant Likely 	}
718ca632f55SGrant Likely out:
719ca632f55SGrant Likely 	omap2_mcspi_set_enable(spi, 1);
720ca632f55SGrant Likely 	return count - c;
721ca632f55SGrant Likely }
722ca632f55SGrant Likely 
723ca632f55SGrant Likely static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
724ca632f55SGrant Likely {
725ca632f55SGrant Likely 	u32 div;
726ca632f55SGrant Likely 
727ca632f55SGrant Likely 	for (div = 0; div < 15; div++)
728ca632f55SGrant Likely 		if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
729ca632f55SGrant Likely 			return div;
730ca632f55SGrant Likely 
731ca632f55SGrant Likely 	return 15;
732ca632f55SGrant Likely }
733ca632f55SGrant Likely 
734ca632f55SGrant Likely /* called only when no transfer is active to this device */
735ca632f55SGrant Likely static int omap2_mcspi_setup_transfer(struct spi_device *spi,
736ca632f55SGrant Likely 		struct spi_transfer *t)
737ca632f55SGrant Likely {
738ca632f55SGrant Likely 	struct omap2_mcspi_cs *cs = spi->controller_state;
739ca632f55SGrant Likely 	struct omap2_mcspi *mcspi;
740ca632f55SGrant Likely 	struct spi_master *spi_cntrl;
741ca632f55SGrant Likely 	u32 l = 0, div = 0;
742ca632f55SGrant Likely 	u8 word_len = spi->bits_per_word;
743ca632f55SGrant Likely 	u32 speed_hz = spi->max_speed_hz;
744ca632f55SGrant Likely 
745ca632f55SGrant Likely 	mcspi = spi_master_get_devdata(spi->master);
746ca632f55SGrant Likely 	spi_cntrl = mcspi->master;
747ca632f55SGrant Likely 
748ca632f55SGrant Likely 	if (t != NULL && t->bits_per_word)
749ca632f55SGrant Likely 		word_len = t->bits_per_word;
750ca632f55SGrant Likely 
751ca632f55SGrant Likely 	cs->word_len = word_len;
752ca632f55SGrant Likely 
753ca632f55SGrant Likely 	if (t && t->speed_hz)
754ca632f55SGrant Likely 		speed_hz = t->speed_hz;
755ca632f55SGrant Likely 
756ca632f55SGrant Likely 	speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
757ca632f55SGrant Likely 	div = omap2_mcspi_calc_divisor(speed_hz);
758ca632f55SGrant Likely 
759ca632f55SGrant Likely 	l = mcspi_cached_chconf0(spi);
760ca632f55SGrant Likely 
761ca632f55SGrant Likely 	/* standard 4-wire master mode:  SCK, MOSI/out, MISO/in, nCS
762ca632f55SGrant Likely 	 * REVISIT: this controller could support SPI_3WIRE mode.
763ca632f55SGrant Likely 	 */
7642cd45179SDaniel Mack 	if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
7650384e90bSDaniel Mack 		l &= ~OMAP2_MCSPI_CHCONF_IS;
7660384e90bSDaniel Mack 		l &= ~OMAP2_MCSPI_CHCONF_DPE1;
767ca632f55SGrant Likely 		l |= OMAP2_MCSPI_CHCONF_DPE0;
7680384e90bSDaniel Mack 	} else {
7690384e90bSDaniel Mack 		l |= OMAP2_MCSPI_CHCONF_IS;
7700384e90bSDaniel Mack 		l |= OMAP2_MCSPI_CHCONF_DPE1;
7710384e90bSDaniel Mack 		l &= ~OMAP2_MCSPI_CHCONF_DPE0;
7720384e90bSDaniel Mack 	}
773ca632f55SGrant Likely 
774ca632f55SGrant Likely 	/* wordlength */
775ca632f55SGrant Likely 	l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
776ca632f55SGrant Likely 	l |= (word_len - 1) << 7;
777ca632f55SGrant Likely 
778ca632f55SGrant Likely 	/* set chipselect polarity; manage with FORCE */
779ca632f55SGrant Likely 	if (!(spi->mode & SPI_CS_HIGH))
780ca632f55SGrant Likely 		l |= OMAP2_MCSPI_CHCONF_EPOL;	/* active-low; normal */
781ca632f55SGrant Likely 	else
782ca632f55SGrant Likely 		l &= ~OMAP2_MCSPI_CHCONF_EPOL;
783ca632f55SGrant Likely 
784ca632f55SGrant Likely 	/* set clock divisor */
785ca632f55SGrant Likely 	l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
786ca632f55SGrant Likely 	l |= div << 2;
787ca632f55SGrant Likely 
788ca632f55SGrant Likely 	/* set SPI mode 0..3 */
789ca632f55SGrant Likely 	if (spi->mode & SPI_CPOL)
790ca632f55SGrant Likely 		l |= OMAP2_MCSPI_CHCONF_POL;
791ca632f55SGrant Likely 	else
792ca632f55SGrant Likely 		l &= ~OMAP2_MCSPI_CHCONF_POL;
793ca632f55SGrant Likely 	if (spi->mode & SPI_CPHA)
794ca632f55SGrant Likely 		l |= OMAP2_MCSPI_CHCONF_PHA;
795ca632f55SGrant Likely 	else
796ca632f55SGrant Likely 		l &= ~OMAP2_MCSPI_CHCONF_PHA;
797ca632f55SGrant Likely 
798ca632f55SGrant Likely 	mcspi_write_chconf0(spi, l);
799ca632f55SGrant Likely 
800ca632f55SGrant Likely 	dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
801ca632f55SGrant Likely 			OMAP2_MCSPI_MAX_FREQ >> div,
802ca632f55SGrant Likely 			(spi->mode & SPI_CPHA) ? "trailing" : "leading",
803ca632f55SGrant Likely 			(spi->mode & SPI_CPOL) ? "inverted" : "normal");
804ca632f55SGrant Likely 
805ca632f55SGrant Likely 	return 0;
806ca632f55SGrant Likely }
807ca632f55SGrant Likely 
808ca632f55SGrant Likely static int omap2_mcspi_request_dma(struct spi_device *spi)
809ca632f55SGrant Likely {
810ca632f55SGrant Likely 	struct spi_master	*master = spi->master;
811ca632f55SGrant Likely 	struct omap2_mcspi	*mcspi;
812ca632f55SGrant Likely 	struct omap2_mcspi_dma	*mcspi_dma;
81353741ed8SRussell King 	dma_cap_mask_t mask;
81453741ed8SRussell King 	unsigned sig;
815ca632f55SGrant Likely 
816ca632f55SGrant Likely 	mcspi = spi_master_get_devdata(master);
817ca632f55SGrant Likely 	mcspi_dma = mcspi->dma_channels + spi->chip_select;
818ca632f55SGrant Likely 
819ca632f55SGrant Likely 	init_completion(&mcspi_dma->dma_rx_completion);
820ca632f55SGrant Likely 	init_completion(&mcspi_dma->dma_tx_completion);
821ca632f55SGrant Likely 
82253741ed8SRussell King 	dma_cap_zero(mask);
82353741ed8SRussell King 	dma_cap_set(DMA_SLAVE, mask);
82453741ed8SRussell King 	sig = mcspi_dma->dma_rx_sync_dev;
82553741ed8SRussell King 	mcspi_dma->dma_rx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
82653741ed8SRussell King 	if (!mcspi_dma->dma_rx) {
82753741ed8SRussell King 		dev_err(&spi->dev, "no RX DMA engine channel for McSPI\n");
82853741ed8SRussell King 		return -EAGAIN;
82953741ed8SRussell King 	}
830ca632f55SGrant Likely 
83153741ed8SRussell King 	sig = mcspi_dma->dma_tx_sync_dev;
83253741ed8SRussell King 	mcspi_dma->dma_tx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
83353741ed8SRussell King 	if (!mcspi_dma->dma_tx) {
83453741ed8SRussell King 		dev_err(&spi->dev, "no TX DMA engine channel for McSPI\n");
83553741ed8SRussell King 		dma_release_channel(mcspi_dma->dma_rx);
83653741ed8SRussell King 		mcspi_dma->dma_rx = NULL;
83753741ed8SRussell King 		return -EAGAIN;
83853741ed8SRussell King 	}
839ca632f55SGrant Likely 
840ca632f55SGrant Likely 	return 0;
841ca632f55SGrant Likely }
842ca632f55SGrant Likely 
843ca632f55SGrant Likely static int omap2_mcspi_setup(struct spi_device *spi)
844ca632f55SGrant Likely {
845ca632f55SGrant Likely 	int			ret;
8461bd897f8SBenoit Cousson 	struct omap2_mcspi	*mcspi = spi_master_get_devdata(spi->master);
8471bd897f8SBenoit Cousson 	struct omap2_mcspi_regs	*ctx = &mcspi->ctx;
848ca632f55SGrant Likely 	struct omap2_mcspi_dma	*mcspi_dma;
849ca632f55SGrant Likely 	struct omap2_mcspi_cs	*cs = spi->controller_state;
850ca632f55SGrant Likely 
851ca632f55SGrant Likely 	if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
852ca632f55SGrant Likely 		dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
853ca632f55SGrant Likely 			spi->bits_per_word);
854ca632f55SGrant Likely 		return -EINVAL;
855ca632f55SGrant Likely 	}
856ca632f55SGrant Likely 
857ca632f55SGrant Likely 	mcspi_dma = &mcspi->dma_channels[spi->chip_select];
858ca632f55SGrant Likely 
859ca632f55SGrant Likely 	if (!cs) {
86010aa5a35SRussell King 		cs = kzalloc(sizeof *cs, GFP_KERNEL);
861ca632f55SGrant Likely 		if (!cs)
862ca632f55SGrant Likely 			return -ENOMEM;
863ca632f55SGrant Likely 		cs->base = mcspi->base + spi->chip_select * 0x14;
864ca632f55SGrant Likely 		cs->phys = mcspi->phys + spi->chip_select * 0x14;
865ca632f55SGrant Likely 		cs->chconf0 = 0;
866ca632f55SGrant Likely 		spi->controller_state = cs;
867ca632f55SGrant Likely 		/* Link this to context save list */
8681bd897f8SBenoit Cousson 		list_add_tail(&cs->node, &ctx->cs);
869ca632f55SGrant Likely 	}
870ca632f55SGrant Likely 
8718c7494a5SRussell King 	if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
872ca632f55SGrant Likely 		ret = omap2_mcspi_request_dma(spi);
873ca632f55SGrant Likely 		if (ret < 0)
874ca632f55SGrant Likely 			return ret;
875ca632f55SGrant Likely 	}
876ca632f55SGrant Likely 
877034d3dc9SShubhrajyoti D 	ret = pm_runtime_get_sync(mcspi->dev);
878ca632f55SGrant Likely 	if (ret < 0)
879ca632f55SGrant Likely 		return ret;
880ca632f55SGrant Likely 
881ca632f55SGrant Likely 	ret = omap2_mcspi_setup_transfer(spi, NULL);
882034d3dc9SShubhrajyoti D 	pm_runtime_mark_last_busy(mcspi->dev);
883034d3dc9SShubhrajyoti D 	pm_runtime_put_autosuspend(mcspi->dev);
884ca632f55SGrant Likely 
885ca632f55SGrant Likely 	return ret;
886ca632f55SGrant Likely }
887ca632f55SGrant Likely 
888ca632f55SGrant Likely static void omap2_mcspi_cleanup(struct spi_device *spi)
889ca632f55SGrant Likely {
890ca632f55SGrant Likely 	struct omap2_mcspi	*mcspi;
891ca632f55SGrant Likely 	struct omap2_mcspi_dma	*mcspi_dma;
892ca632f55SGrant Likely 	struct omap2_mcspi_cs	*cs;
893ca632f55SGrant Likely 
894ca632f55SGrant Likely 	mcspi = spi_master_get_devdata(spi->master);
895ca632f55SGrant Likely 
896ca632f55SGrant Likely 	if (spi->controller_state) {
897ca632f55SGrant Likely 		/* Unlink controller state from context save list */
898ca632f55SGrant Likely 		cs = spi->controller_state;
899ca632f55SGrant Likely 		list_del(&cs->node);
900ca632f55SGrant Likely 
90110aa5a35SRussell King 		kfree(cs);
902ca632f55SGrant Likely 	}
903ca632f55SGrant Likely 
904ca632f55SGrant Likely 	if (spi->chip_select < spi->master->num_chipselect) {
905ca632f55SGrant Likely 		mcspi_dma = &mcspi->dma_channels[spi->chip_select];
906ca632f55SGrant Likely 
90753741ed8SRussell King 		if (mcspi_dma->dma_rx) {
90853741ed8SRussell King 			dma_release_channel(mcspi_dma->dma_rx);
90953741ed8SRussell King 			mcspi_dma->dma_rx = NULL;
910ca632f55SGrant Likely 		}
91153741ed8SRussell King 		if (mcspi_dma->dma_tx) {
91253741ed8SRussell King 			dma_release_channel(mcspi_dma->dma_tx);
91353741ed8SRussell King 			mcspi_dma->dma_tx = NULL;
914ca632f55SGrant Likely 		}
915ca632f55SGrant Likely 	}
916ca632f55SGrant Likely }
917ca632f55SGrant Likely 
9185fda88f5SShubhrajyoti D static void omap2_mcspi_work(struct omap2_mcspi *mcspi, struct spi_message *m)
919ca632f55SGrant Likely {
920ca632f55SGrant Likely 
921ca632f55SGrant Likely 	/* We only enable one channel at a time -- the one whose message is
9225fda88f5SShubhrajyoti D 	 * -- although this controller would gladly
923ca632f55SGrant Likely 	 * arbitrate among multiple channels.  This corresponds to "single
924ca632f55SGrant Likely 	 * channel" master mode.  As a side effect, we need to manage the
925ca632f55SGrant Likely 	 * chipselect with the FORCE bit ... CS != channel enable.
926ca632f55SGrant Likely 	 */
9275fda88f5SShubhrajyoti D 
928ca632f55SGrant Likely 	struct spi_device		*spi;
929ca632f55SGrant Likely 	struct spi_transfer		*t = NULL;
930*5cbc7ca9SMatthias Brugger 	struct spi_master		*master;
931ca632f55SGrant Likely 	int				cs_active = 0;
932ca632f55SGrant Likely 	struct omap2_mcspi_cs		*cs;
933ca632f55SGrant Likely 	struct omap2_mcspi_device_config *cd;
934ca632f55SGrant Likely 	int				par_override = 0;
935ca632f55SGrant Likely 	int				status = 0;
936ca632f55SGrant Likely 	u32				chconf;
937ca632f55SGrant Likely 
938ca632f55SGrant Likely 	spi = m->spi;
939*5cbc7ca9SMatthias Brugger 	master = spi->master;
940ca632f55SGrant Likely 	cs = spi->controller_state;
941ca632f55SGrant Likely 	cd = spi->controller_data;
942ca632f55SGrant Likely 
943ca632f55SGrant Likely 	omap2_mcspi_set_enable(spi, 1);
944ca632f55SGrant Likely 	list_for_each_entry(t, &m->transfers, transfer_list) {
945ca632f55SGrant Likely 		if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
946ca632f55SGrant Likely 			status = -EINVAL;
947ca632f55SGrant Likely 			break;
948ca632f55SGrant Likely 		}
949ca632f55SGrant Likely 		if (par_override || t->speed_hz || t->bits_per_word) {
950ca632f55SGrant Likely 			par_override = 1;
951ca632f55SGrant Likely 			status = omap2_mcspi_setup_transfer(spi, t);
952ca632f55SGrant Likely 			if (status < 0)
953ca632f55SGrant Likely 				break;
954ca632f55SGrant Likely 			if (!t->speed_hz && !t->bits_per_word)
955ca632f55SGrant Likely 				par_override = 0;
956ca632f55SGrant Likely 		}
957*5cbc7ca9SMatthias Brugger 		if (cd && cd->cs_per_word) {
958*5cbc7ca9SMatthias Brugger 			chconf = mcspi->ctx.modulctrl;
959*5cbc7ca9SMatthias Brugger 			chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
960*5cbc7ca9SMatthias Brugger 			mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
961*5cbc7ca9SMatthias Brugger 			mcspi->ctx.modulctrl =
962*5cbc7ca9SMatthias Brugger 				mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
963*5cbc7ca9SMatthias Brugger 		}
964*5cbc7ca9SMatthias Brugger 
965ca632f55SGrant Likely 
966ca632f55SGrant Likely 		if (!cs_active) {
967ca632f55SGrant Likely 			omap2_mcspi_force_cs(spi, 1);
968ca632f55SGrant Likely 			cs_active = 1;
969ca632f55SGrant Likely 		}
970ca632f55SGrant Likely 
971ca632f55SGrant Likely 		chconf = mcspi_cached_chconf0(spi);
972ca632f55SGrant Likely 		chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
973ca632f55SGrant Likely 		chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
974ca632f55SGrant Likely 
975ca632f55SGrant Likely 		if (t->tx_buf == NULL)
976ca632f55SGrant Likely 			chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
977ca632f55SGrant Likely 		else if (t->rx_buf == NULL)
978ca632f55SGrant Likely 			chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
979ca632f55SGrant Likely 
980ca632f55SGrant Likely 		if (cd && cd->turbo_mode && t->tx_buf == NULL) {
981ca632f55SGrant Likely 			/* Turbo mode is for more than one word */
982ca632f55SGrant Likely 			if (t->len > ((cs->word_len + 7) >> 3))
983ca632f55SGrant Likely 				chconf |= OMAP2_MCSPI_CHCONF_TURBO;
984ca632f55SGrant Likely 		}
985ca632f55SGrant Likely 
986ca632f55SGrant Likely 		mcspi_write_chconf0(spi, chconf);
987ca632f55SGrant Likely 
988ca632f55SGrant Likely 		if (t->len) {
989ca632f55SGrant Likely 			unsigned	count;
990ca632f55SGrant Likely 
991ca632f55SGrant Likely 			/* RX_ONLY mode needs dummy data in TX reg */
992ca632f55SGrant Likely 			if (t->tx_buf == NULL)
993ca632f55SGrant Likely 				__raw_writel(0, cs->base
994ca632f55SGrant Likely 						+ OMAP2_MCSPI_TX0);
995ca632f55SGrant Likely 
996ca632f55SGrant Likely 			if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
997ca632f55SGrant Likely 				count = omap2_mcspi_txrx_dma(spi, t);
998ca632f55SGrant Likely 			else
999ca632f55SGrant Likely 				count = omap2_mcspi_txrx_pio(spi, t);
1000ca632f55SGrant Likely 			m->actual_length += count;
1001ca632f55SGrant Likely 
1002ca632f55SGrant Likely 			if (count != t->len) {
1003ca632f55SGrant Likely 				status = -EIO;
1004ca632f55SGrant Likely 				break;
1005ca632f55SGrant Likely 			}
1006ca632f55SGrant Likely 		}
1007ca632f55SGrant Likely 
1008ca632f55SGrant Likely 		if (t->delay_usecs)
1009ca632f55SGrant Likely 			udelay(t->delay_usecs);
1010ca632f55SGrant Likely 
1011ca632f55SGrant Likely 		/* ignore the "leave it on after last xfer" hint */
1012ca632f55SGrant Likely 		if (t->cs_change) {
1013ca632f55SGrant Likely 			omap2_mcspi_force_cs(spi, 0);
1014ca632f55SGrant Likely 			cs_active = 0;
1015ca632f55SGrant Likely 		}
1016ca632f55SGrant Likely 	}
1017ca632f55SGrant Likely 	/* Restore defaults if they were overriden */
1018ca632f55SGrant Likely 	if (par_override) {
1019ca632f55SGrant Likely 		par_override = 0;
1020ca632f55SGrant Likely 		status = omap2_mcspi_setup_transfer(spi, NULL);
1021ca632f55SGrant Likely 	}
1022ca632f55SGrant Likely 
1023ca632f55SGrant Likely 	if (cs_active)
1024ca632f55SGrant Likely 		omap2_mcspi_force_cs(spi, 0);
1025ca632f55SGrant Likely 
1026*5cbc7ca9SMatthias Brugger 	if (cd && cd->cs_per_word) {
1027*5cbc7ca9SMatthias Brugger 		chconf = mcspi->ctx.modulctrl;
1028*5cbc7ca9SMatthias Brugger 		chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
1029*5cbc7ca9SMatthias Brugger 		mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1030*5cbc7ca9SMatthias Brugger 		mcspi->ctx.modulctrl =
1031*5cbc7ca9SMatthias Brugger 			mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1032*5cbc7ca9SMatthias Brugger 	}
1033*5cbc7ca9SMatthias Brugger 
1034ca632f55SGrant Likely 	omap2_mcspi_set_enable(spi, 0);
1035ca632f55SGrant Likely 
1036ca632f55SGrant Likely 	m->status = status;
1037ca632f55SGrant Likely 
1038ca632f55SGrant Likely }
1039ca632f55SGrant Likely 
10405fda88f5SShubhrajyoti D static int omap2_mcspi_transfer_one_message(struct spi_master *master,
10415fda88f5SShubhrajyoti D 		struct spi_message *m)
1042ca632f55SGrant Likely {
1043ca632f55SGrant Likely 	struct omap2_mcspi	*mcspi;
1044ca632f55SGrant Likely 	struct spi_transfer	*t;
1045ca632f55SGrant Likely 
10465fda88f5SShubhrajyoti D 	mcspi = spi_master_get_devdata(master);
1047ca632f55SGrant Likely 	m->actual_length = 0;
1048ca632f55SGrant Likely 	m->status = 0;
1049ca632f55SGrant Likely 
1050ca632f55SGrant Likely 	/* reject invalid messages and transfers */
10515fda88f5SShubhrajyoti D 	if (list_empty(&m->transfers))
1052ca632f55SGrant Likely 		return -EINVAL;
1053ca632f55SGrant Likely 	list_for_each_entry(t, &m->transfers, transfer_list) {
1054ca632f55SGrant Likely 		const void	*tx_buf = t->tx_buf;
1055ca632f55SGrant Likely 		void		*rx_buf = t->rx_buf;
1056ca632f55SGrant Likely 		unsigned	len = t->len;
1057ca632f55SGrant Likely 
1058ca632f55SGrant Likely 		if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
1059ca632f55SGrant Likely 				|| (len && !(rx_buf || tx_buf))
1060ca632f55SGrant Likely 				|| (t->bits_per_word &&
1061ca632f55SGrant Likely 					(  t->bits_per_word < 4
1062ca632f55SGrant Likely 					   || t->bits_per_word > 32))) {
10635fda88f5SShubhrajyoti D 			dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
1064ca632f55SGrant Likely 					t->speed_hz,
1065ca632f55SGrant Likely 					len,
1066ca632f55SGrant Likely 					tx_buf ? "tx" : "",
1067ca632f55SGrant Likely 					rx_buf ? "rx" : "",
1068ca632f55SGrant Likely 					t->bits_per_word);
1069ca632f55SGrant Likely 			return -EINVAL;
1070ca632f55SGrant Likely 		}
1071ca632f55SGrant Likely 		if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
10725fda88f5SShubhrajyoti D 			dev_dbg(mcspi->dev, "speed_hz %d below minimum %d Hz\n",
1073ca632f55SGrant Likely 					t->speed_hz,
1074ca632f55SGrant Likely 					OMAP2_MCSPI_MAX_FREQ >> 15);
1075ca632f55SGrant Likely 			return -EINVAL;
1076ca632f55SGrant Likely 		}
1077ca632f55SGrant Likely 
1078ca632f55SGrant Likely 		if (m->is_dma_mapped || len < DMA_MIN_BYTES)
1079ca632f55SGrant Likely 			continue;
1080ca632f55SGrant Likely 
1081ca632f55SGrant Likely 		if (tx_buf != NULL) {
10825fda88f5SShubhrajyoti D 			t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
1083ca632f55SGrant Likely 					len, DMA_TO_DEVICE);
10845fda88f5SShubhrajyoti D 			if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
10855fda88f5SShubhrajyoti D 				dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1086ca632f55SGrant Likely 						'T', len);
1087ca632f55SGrant Likely 				return -EINVAL;
1088ca632f55SGrant Likely 			}
1089ca632f55SGrant Likely 		}
1090ca632f55SGrant Likely 		if (rx_buf != NULL) {
10915fda88f5SShubhrajyoti D 			t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
1092ca632f55SGrant Likely 					DMA_FROM_DEVICE);
10935fda88f5SShubhrajyoti D 			if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
10945fda88f5SShubhrajyoti D 				dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1095ca632f55SGrant Likely 						'R', len);
1096ca632f55SGrant Likely 				if (tx_buf != NULL)
10975fda88f5SShubhrajyoti D 					dma_unmap_single(mcspi->dev, t->tx_dma,
1098ca632f55SGrant Likely 							len, DMA_TO_DEVICE);
1099ca632f55SGrant Likely 				return -EINVAL;
1100ca632f55SGrant Likely 			}
1101ca632f55SGrant Likely 		}
1102ca632f55SGrant Likely 	}
1103ca632f55SGrant Likely 
11045fda88f5SShubhrajyoti D 	omap2_mcspi_work(mcspi, m);
11055fda88f5SShubhrajyoti D 	spi_finalize_current_message(master);
1106ca632f55SGrant Likely 	return 0;
1107ca632f55SGrant Likely }
1108ca632f55SGrant Likely 
1109fd4a319bSGrant Likely static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
1110ca632f55SGrant Likely {
1111ca632f55SGrant Likely 	struct spi_master	*master = mcspi->master;
11121bd897f8SBenoit Cousson 	struct omap2_mcspi_regs	*ctx = &mcspi->ctx;
1113ca632f55SGrant Likely 	int			ret = 0;
1114ca632f55SGrant Likely 
1115034d3dc9SShubhrajyoti D 	ret = pm_runtime_get_sync(mcspi->dev);
1116ca632f55SGrant Likely 	if (ret < 0)
1117ca632f55SGrant Likely 		return ret;
1118ca632f55SGrant Likely 
111939f8052dSShubhrajyoti D 	mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
112039f8052dSShubhrajyoti D 			OMAP2_MCSPI_WAKEUPENABLE_WKEN);
112139f8052dSShubhrajyoti D 	ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
1122ca632f55SGrant Likely 
1123ca632f55SGrant Likely 	omap2_mcspi_set_master_mode(master);
1124034d3dc9SShubhrajyoti D 	pm_runtime_mark_last_busy(mcspi->dev);
1125034d3dc9SShubhrajyoti D 	pm_runtime_put_autosuspend(mcspi->dev);
1126ca632f55SGrant Likely 	return 0;
1127ca632f55SGrant Likely }
1128ca632f55SGrant Likely 
1129ca632f55SGrant Likely static int omap_mcspi_runtime_resume(struct device *dev)
1130ca632f55SGrant Likely {
1131ca632f55SGrant Likely 	struct omap2_mcspi	*mcspi;
1132ca632f55SGrant Likely 	struct spi_master	*master;
1133ca632f55SGrant Likely 
1134ca632f55SGrant Likely 	master = dev_get_drvdata(dev);
1135ca632f55SGrant Likely 	mcspi = spi_master_get_devdata(master);
1136ca632f55SGrant Likely 	omap2_mcspi_restore_ctx(mcspi);
1137ca632f55SGrant Likely 
1138ca632f55SGrant Likely 	return 0;
1139ca632f55SGrant Likely }
1140ca632f55SGrant Likely 
1141d5a80031SBenoit Cousson static struct omap2_mcspi_platform_config omap2_pdata = {
1142d5a80031SBenoit Cousson 	.regs_offset = 0,
1143d5a80031SBenoit Cousson };
1144d5a80031SBenoit Cousson 
1145d5a80031SBenoit Cousson static struct omap2_mcspi_platform_config omap4_pdata = {
1146d5a80031SBenoit Cousson 	.regs_offset = OMAP4_MCSPI_REG_OFFSET,
1147d5a80031SBenoit Cousson };
1148d5a80031SBenoit Cousson 
1149d5a80031SBenoit Cousson static const struct of_device_id omap_mcspi_of_match[] = {
1150d5a80031SBenoit Cousson 	{
1151d5a80031SBenoit Cousson 		.compatible = "ti,omap2-mcspi",
1152d5a80031SBenoit Cousson 		.data = &omap2_pdata,
1153d5a80031SBenoit Cousson 	},
1154d5a80031SBenoit Cousson 	{
1155d5a80031SBenoit Cousson 		.compatible = "ti,omap4-mcspi",
1156d5a80031SBenoit Cousson 		.data = &omap4_pdata,
1157d5a80031SBenoit Cousson 	},
1158d5a80031SBenoit Cousson 	{ },
1159d5a80031SBenoit Cousson };
1160d5a80031SBenoit Cousson MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
1161ca632f55SGrant Likely 
1162fd4a319bSGrant Likely static int omap2_mcspi_probe(struct platform_device *pdev)
1163ca632f55SGrant Likely {
1164ca632f55SGrant Likely 	struct spi_master	*master;
116583a01e72SUwe Kleine-König 	const struct omap2_mcspi_platform_config *pdata;
1166ca632f55SGrant Likely 	struct omap2_mcspi	*mcspi;
1167ca632f55SGrant Likely 	struct resource		*r;
1168ca632f55SGrant Likely 	int			status = 0, i;
1169d5a80031SBenoit Cousson 	u32			regs_offset = 0;
1170d5a80031SBenoit Cousson 	static int		bus_num = 1;
1171d5a80031SBenoit Cousson 	struct device_node	*node = pdev->dev.of_node;
1172d5a80031SBenoit Cousson 	const struct of_device_id *match;
1173ec155afaSMatt Porter 	struct pinctrl *pinctrl;
1174ca632f55SGrant Likely 
1175ca632f55SGrant Likely 	master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1176ca632f55SGrant Likely 	if (master == NULL) {
1177ca632f55SGrant Likely 		dev_dbg(&pdev->dev, "master allocation failed\n");
1178ca632f55SGrant Likely 		return -ENOMEM;
1179ca632f55SGrant Likely 	}
1180ca632f55SGrant Likely 
1181ca632f55SGrant Likely 	/* the spi->mode bits understood by this driver: */
1182ca632f55SGrant Likely 	master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1183ca632f55SGrant Likely 
1184ca632f55SGrant Likely 	master->setup = omap2_mcspi_setup;
11855fda88f5SShubhrajyoti D 	master->prepare_transfer_hardware = omap2_prepare_transfer;
11865fda88f5SShubhrajyoti D 	master->unprepare_transfer_hardware = omap2_unprepare_transfer;
11875fda88f5SShubhrajyoti D 	master->transfer_one_message = omap2_mcspi_transfer_one_message;
1188ca632f55SGrant Likely 	master->cleanup = omap2_mcspi_cleanup;
1189d5a80031SBenoit Cousson 	master->dev.of_node = node;
1190d5a80031SBenoit Cousson 
11910384e90bSDaniel Mack 	dev_set_drvdata(&pdev->dev, master);
11920384e90bSDaniel Mack 
11930384e90bSDaniel Mack 	mcspi = spi_master_get_devdata(master);
11940384e90bSDaniel Mack 	mcspi->master = master;
11950384e90bSDaniel Mack 
1196d5a80031SBenoit Cousson 	match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1197d5a80031SBenoit Cousson 	if (match) {
1198d5a80031SBenoit Cousson 		u32 num_cs = 1; /* default number of chipselect */
1199d5a80031SBenoit Cousson 		pdata = match->data;
1200d5a80031SBenoit Cousson 
1201d5a80031SBenoit Cousson 		of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1202d5a80031SBenoit Cousson 		master->num_chipselect = num_cs;
1203d5a80031SBenoit Cousson 		master->bus_num = bus_num++;
12042cd45179SDaniel Mack 		if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
12052cd45179SDaniel Mack 			mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
1206d5a80031SBenoit Cousson 	} else {
1207d5a80031SBenoit Cousson 		pdata = pdev->dev.platform_data;
1208ca632f55SGrant Likely 		master->num_chipselect = pdata->num_cs;
1209d5a80031SBenoit Cousson 		if (pdev->id != -1)
1210d5a80031SBenoit Cousson 			master->bus_num = pdev->id;
12110384e90bSDaniel Mack 		mcspi->pin_dir = pdata->pin_dir;
1212d5a80031SBenoit Cousson 	}
1213d5a80031SBenoit Cousson 	regs_offset = pdata->regs_offset;
1214ca632f55SGrant Likely 
1215ca632f55SGrant Likely 	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1216ca632f55SGrant Likely 	if (r == NULL) {
1217ca632f55SGrant Likely 		status = -ENODEV;
121839f1b565SShubhrajyoti D 		goto free_master;
1219ca632f55SGrant Likely 	}
12201458d160SShubhrajyoti D 
1221d5a80031SBenoit Cousson 	r->start += regs_offset;
1222d5a80031SBenoit Cousson 	r->end += regs_offset;
12231458d160SShubhrajyoti D 	mcspi->phys = r->start;
1224ca632f55SGrant Likely 
12251a77b127SShubhrajyoti D 	mcspi->base = devm_request_and_ioremap(&pdev->dev, r);
1226ca632f55SGrant Likely 	if (!mcspi->base) {
1227ca632f55SGrant Likely 		dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
1228ca632f55SGrant Likely 		status = -ENOMEM;
12291a77b127SShubhrajyoti D 		goto free_master;
1230ca632f55SGrant Likely 	}
1231ca632f55SGrant Likely 
1232ca632f55SGrant Likely 	mcspi->dev = &pdev->dev;
1233ca632f55SGrant Likely 
12341bd897f8SBenoit Cousson 	INIT_LIST_HEAD(&mcspi->ctx.cs);
1235ca632f55SGrant Likely 
1236ca632f55SGrant Likely 	mcspi->dma_channels = kcalloc(master->num_chipselect,
1237ca632f55SGrant Likely 			sizeof(struct omap2_mcspi_dma),
1238ca632f55SGrant Likely 			GFP_KERNEL);
1239ca632f55SGrant Likely 
1240ca632f55SGrant Likely 	if (mcspi->dma_channels == NULL)
12411a77b127SShubhrajyoti D 		goto free_master;
1242ca632f55SGrant Likely 
1243ca632f55SGrant Likely 	for (i = 0; i < master->num_chipselect; i++) {
1244ca632f55SGrant Likely 		char dma_ch_name[14];
1245ca632f55SGrant Likely 		struct resource *dma_res;
1246ca632f55SGrant Likely 
1247ca632f55SGrant Likely 		sprintf(dma_ch_name, "rx%d", i);
1248ca632f55SGrant Likely 		dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1249ca632f55SGrant Likely 				dma_ch_name);
1250ca632f55SGrant Likely 		if (!dma_res) {
1251ca632f55SGrant Likely 			dev_dbg(&pdev->dev, "cannot get DMA RX channel\n");
1252ca632f55SGrant Likely 			status = -ENODEV;
1253ca632f55SGrant Likely 			break;
1254ca632f55SGrant Likely 		}
1255ca632f55SGrant Likely 
1256ca632f55SGrant Likely 		mcspi->dma_channels[i].dma_rx_sync_dev = dma_res->start;
1257ca632f55SGrant Likely 		sprintf(dma_ch_name, "tx%d", i);
1258ca632f55SGrant Likely 		dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1259ca632f55SGrant Likely 				dma_ch_name);
1260ca632f55SGrant Likely 		if (!dma_res) {
1261ca632f55SGrant Likely 			dev_dbg(&pdev->dev, "cannot get DMA TX channel\n");
1262ca632f55SGrant Likely 			status = -ENODEV;
1263ca632f55SGrant Likely 			break;
1264ca632f55SGrant Likely 		}
1265ca632f55SGrant Likely 
1266ca632f55SGrant Likely 		mcspi->dma_channels[i].dma_tx_sync_dev = dma_res->start;
1267ca632f55SGrant Likely 	}
1268ca632f55SGrant Likely 
126939f1b565SShubhrajyoti D 	if (status < 0)
127039f1b565SShubhrajyoti D 		goto dma_chnl_free;
127139f1b565SShubhrajyoti D 
1272ec155afaSMatt Porter 	pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1273ec155afaSMatt Porter 	if (IS_ERR(pinctrl))
1274ec155afaSMatt Porter 		dev_warn(&pdev->dev,
1275ec155afaSMatt Porter 				"pins are not configured from the driver\n");
1276ec155afaSMatt Porter 
127727b5284cSShubhrajyoti D 	pm_runtime_use_autosuspend(&pdev->dev);
127827b5284cSShubhrajyoti D 	pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
1279ca632f55SGrant Likely 	pm_runtime_enable(&pdev->dev);
1280ca632f55SGrant Likely 
1281ca632f55SGrant Likely 	if (status || omap2_mcspi_master_setup(mcspi) < 0)
128239f1b565SShubhrajyoti D 		goto disable_pm;
1283ca632f55SGrant Likely 
1284ca632f55SGrant Likely 	status = spi_register_master(master);
1285ca632f55SGrant Likely 	if (status < 0)
128637a2d84aSShubhrajyoti D 		goto disable_pm;
1287ca632f55SGrant Likely 
1288ca632f55SGrant Likely 	return status;
1289ca632f55SGrant Likely 
129039f1b565SShubhrajyoti D disable_pm:
1291751c925cSShubhrajyoti D 	pm_runtime_disable(&pdev->dev);
129239f1b565SShubhrajyoti D dma_chnl_free:
1293ca632f55SGrant Likely 	kfree(mcspi->dma_channels);
129439f1b565SShubhrajyoti D free_master:
129537a2d84aSShubhrajyoti D 	spi_master_put(master);
1296ca632f55SGrant Likely 	return status;
1297ca632f55SGrant Likely }
1298ca632f55SGrant Likely 
1299fd4a319bSGrant Likely static int omap2_mcspi_remove(struct platform_device *pdev)
1300ca632f55SGrant Likely {
1301ca632f55SGrant Likely 	struct spi_master	*master;
1302ca632f55SGrant Likely 	struct omap2_mcspi	*mcspi;
1303ca632f55SGrant Likely 	struct omap2_mcspi_dma	*dma_channels;
1304ca632f55SGrant Likely 
1305ca632f55SGrant Likely 	master = dev_get_drvdata(&pdev->dev);
1306ca632f55SGrant Likely 	mcspi = spi_master_get_devdata(master);
1307ca632f55SGrant Likely 	dma_channels = mcspi->dma_channels;
1308ca632f55SGrant Likely 
1309a93a2029SShubhrajyoti D 	pm_runtime_put_sync(mcspi->dev);
1310751c925cSShubhrajyoti D 	pm_runtime_disable(&pdev->dev);
1311ca632f55SGrant Likely 
1312ca632f55SGrant Likely 	spi_unregister_master(master);
1313ca632f55SGrant Likely 	kfree(dma_channels);
1314ca632f55SGrant Likely 
1315ca632f55SGrant Likely 	return 0;
1316ca632f55SGrant Likely }
1317ca632f55SGrant Likely 
1318ca632f55SGrant Likely /* work with hotplug and coldplug */
1319ca632f55SGrant Likely MODULE_ALIAS("platform:omap2_mcspi");
1320ca632f55SGrant Likely 
1321ca632f55SGrant Likely #ifdef	CONFIG_SUSPEND
1322ca632f55SGrant Likely /*
1323ca632f55SGrant Likely  * When SPI wake up from off-mode, CS is in activate state. If it was in
1324ca632f55SGrant Likely  * unactive state when driver was suspend, then force it to unactive state at
1325ca632f55SGrant Likely  * wake up.
1326ca632f55SGrant Likely  */
1327ca632f55SGrant Likely static int omap2_mcspi_resume(struct device *dev)
1328ca632f55SGrant Likely {
1329ca632f55SGrant Likely 	struct spi_master	*master = dev_get_drvdata(dev);
1330ca632f55SGrant Likely 	struct omap2_mcspi	*mcspi = spi_master_get_devdata(master);
13311bd897f8SBenoit Cousson 	struct omap2_mcspi_regs	*ctx = &mcspi->ctx;
1332ca632f55SGrant Likely 	struct omap2_mcspi_cs	*cs;
1333ca632f55SGrant Likely 
1334034d3dc9SShubhrajyoti D 	pm_runtime_get_sync(mcspi->dev);
13351bd897f8SBenoit Cousson 	list_for_each_entry(cs, &ctx->cs, node) {
1336ca632f55SGrant Likely 		if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
1337ca632f55SGrant Likely 			/*
1338ca632f55SGrant Likely 			 * We need to toggle CS state for OMAP take this
1339ca632f55SGrant Likely 			 * change in account.
1340ca632f55SGrant Likely 			 */
1341af4e944dSShubhrajyoti D 			cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
1342ca632f55SGrant Likely 			__raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1343af4e944dSShubhrajyoti D 			cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
1344ca632f55SGrant Likely 			__raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1345ca632f55SGrant Likely 		}
1346ca632f55SGrant Likely 	}
1347034d3dc9SShubhrajyoti D 	pm_runtime_mark_last_busy(mcspi->dev);
1348034d3dc9SShubhrajyoti D 	pm_runtime_put_autosuspend(mcspi->dev);
1349ca632f55SGrant Likely 	return 0;
1350ca632f55SGrant Likely }
1351ca632f55SGrant Likely #else
1352ca632f55SGrant Likely #define	omap2_mcspi_resume	NULL
1353ca632f55SGrant Likely #endif
1354ca632f55SGrant Likely 
1355ca632f55SGrant Likely static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1356ca632f55SGrant Likely 	.resume = omap2_mcspi_resume,
1357ca632f55SGrant Likely 	.runtime_resume	= omap_mcspi_runtime_resume,
1358ca632f55SGrant Likely };
1359ca632f55SGrant Likely 
1360ca632f55SGrant Likely static struct platform_driver omap2_mcspi_driver = {
1361ca632f55SGrant Likely 	.driver = {
1362ca632f55SGrant Likely 		.name =		"omap2_mcspi",
1363ca632f55SGrant Likely 		.owner =	THIS_MODULE,
1364d5a80031SBenoit Cousson 		.pm =		&omap2_mcspi_pm_ops,
1365d5a80031SBenoit Cousson 		.of_match_table = omap_mcspi_of_match,
1366ca632f55SGrant Likely 	},
13677d6b6d83SFelipe Balbi 	.probe =	omap2_mcspi_probe,
1368fd4a319bSGrant Likely 	.remove =	omap2_mcspi_remove,
1369ca632f55SGrant Likely };
1370ca632f55SGrant Likely 
13719fdca9dfSFelipe Balbi module_platform_driver(omap2_mcspi_driver);
1372ca632f55SGrant Likely MODULE_LICENSE("GPL");
1373