blob: 3ac06ade23d0f2ca3ca1c77e17fd68b6dd6ef9d7 [file] [log] [blame]
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001/*
2 * OMAP2 McSPI controller driver
3 *
4 * Copyright (C) 2005, 2006 Nokia Corporation
5 * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
Charulatha V1a5d8192011-02-02 17:52:14 +05306 * Juha Yrj�l� <juha.yrjola@nokia.com>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070017 */
18
19#include <linux/kernel.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070020#include <linux/interrupt.h>
21#include <linux/module.h>
22#include <linux/device.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
Russell King53741ed2012-04-23 13:51:48 +010025#include <linux/dmaengine.h>
26#include <linux/omap-dma.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070027#include <linux/platform_device.h>
28#include <linux/err.h>
29#include <linux/clk.h>
30#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Govindraj.R1f1a4382011-02-02 17:52:15 +053032#include <linux/pm_runtime.h>
Benoit Coussond5a80032012-02-15 18:37:34 +010033#include <linux/of.h>
34#include <linux/of_device.h>
Illia Smyrnovd33f4732013-06-17 16:31:06 +030035#include <linux/gcd.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070036
37#include <linux/spi/spi.h>
38
Arnd Bergmann22037472012-08-24 15:21:06 +020039#include <linux/platform_data/spi-omap2-mcspi.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070040
41#define OMAP2_MCSPI_MAX_FREQ 48000000
Stefan Sørensenfaee9b02014-02-02 16:24:25 +010042#define OMAP2_MCSPI_MAX_DIVIDER 4096
Illia Smyrnovd33f4732013-06-17 16:31:06 +030043#define OMAP2_MCSPI_MAX_FIFODEPTH 64
44#define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
Shubhrajyoti D27b52842012-03-26 17:04:22 +053045#define SPI_AUTOSUSPEND_TIMEOUT 2000
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070046
47#define OMAP2_MCSPI_REVISION 0x00
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070048#define OMAP2_MCSPI_SYSSTATUS 0x14
49#define OMAP2_MCSPI_IRQSTATUS 0x18
50#define OMAP2_MCSPI_IRQENABLE 0x1c
51#define OMAP2_MCSPI_WAKEUPENABLE 0x20
52#define OMAP2_MCSPI_SYST 0x24
53#define OMAP2_MCSPI_MODULCTRL 0x28
Illia Smyrnovd33f4732013-06-17 16:31:06 +030054#define OMAP2_MCSPI_XFERLEVEL 0x7c
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070055
56/* per-channel banks, 0x14 bytes each, first is: */
57#define OMAP2_MCSPI_CHCONF0 0x2c
58#define OMAP2_MCSPI_CHSTAT0 0x30
59#define OMAP2_MCSPI_CHCTRL0 0x34
60#define OMAP2_MCSPI_TX0 0x38
61#define OMAP2_MCSPI_RX0 0x3c
62
63/* per-register bitmasks: */
Illia Smyrnovd33f4732013-06-17 16:31:06 +030064#define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070065
Jouni Hogander7a8fa722009-09-22 16:45:58 -070066#define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
67#define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
68#define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070069
Jouni Hogander7a8fa722009-09-22 16:45:58 -070070#define OMAP2_MCSPI_CHCONF_PHA BIT(0)
71#define OMAP2_MCSPI_CHCONF_POL BIT(1)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070072#define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070073#define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070074#define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070075#define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
76#define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070077#define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070078#define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
79#define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
80#define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
81#define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
82#define OMAP2_MCSPI_CHCONF_IS BIT(18)
83#define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
84#define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
Illia Smyrnovd33f4732013-06-17 16:31:06 +030085#define OMAP2_MCSPI_CHCONF_FFET BIT(27)
86#define OMAP2_MCSPI_CHCONF_FFER BIT(28)
Stefan Sørensenfaee9b02014-02-02 16:24:25 +010087#define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070088
Jouni Hogander7a8fa722009-09-22 16:45:58 -070089#define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
90#define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
91#define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
Illia Smyrnovd33f4732013-06-17 16:31:06 +030092#define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070093
Jouni Hogander7a8fa722009-09-22 16:45:58 -070094#define OMAP2_MCSPI_CHCTRL_EN BIT(0)
Stefan Sørensenfaee9b02014-02-02 16:24:25 +010095#define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070096
Jouni Hogander7a8fa722009-09-22 16:45:58 -070097#define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070098
99/* We have 2 DMA channels per CS, one for RX and one for TX */
100struct omap2_mcspi_dma {
Russell King53741ed2012-04-23 13:51:48 +0100101 struct dma_chan *dma_tx;
102 struct dma_chan *dma_rx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700103
104 int dma_tx_sync_dev;
105 int dma_rx_sync_dev;
106
107 struct completion dma_tx_completion;
108 struct completion dma_rx_completion;
Matt Porter74f3aaa2013-06-22 23:07:38 +0530109
110 char dma_rx_ch_name[14];
111 char dma_tx_ch_name[14];
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700112};
113
114/* use PIO for small transfers, avoiding DMA setup/teardown overhead and
115 * cache operations; better heuristics consider wordsize and bitrate.
116 */
Roman Tereshonkov8b66c132010-04-12 09:07:54 +0000117#define DMA_MIN_BYTES 160
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700118
119
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530120/*
121 * Used for context save and restore, structure members to be updated whenever
122 * corresponding registers are modified.
123 */
124struct omap2_mcspi_regs {
125 u32 modulctrl;
126 u32 wakeupenable;
127 struct list_head cs;
128};
129
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700130struct omap2_mcspi {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700131 struct spi_master *master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700132 /* Virtual base address of the controller */
133 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100134 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700135 /* SPI1 has 4 channels, while SPI2 has 2 */
136 struct omap2_mcspi_dma *dma_channels;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530137 struct device *dev;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530138 struct omap2_mcspi_regs ctx;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300139 int fifo_depth;
Daniel Mack0384e902012-10-07 18:19:44 +0200140 unsigned int pin_dir:1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700141};
142
143struct omap2_mcspi_cs {
144 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100145 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700146 int word_len;
Mark A. Greer97ca0d62014-07-01 20:28:32 -0700147 u16 mode;
Tero Kristo89c05372009-09-22 16:46:17 -0700148 struct list_head node;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700149 /* Context save and restore shadow register */
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100150 u32 chconf0, chctrl0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700151};
152
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700153static inline void mcspi_write_reg(struct spi_master *master,
154 int idx, u32 val)
155{
156 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
157
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200158 writel_relaxed(val, mcspi->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700159}
160
161static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
162{
163 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
164
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200165 return readl_relaxed(mcspi->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700166}
167
168static inline void mcspi_write_cs_reg(const struct spi_device *spi,
169 int idx, u32 val)
170{
171 struct omap2_mcspi_cs *cs = spi->controller_state;
172
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200173 writel_relaxed(val, cs->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700174}
175
176static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
177{
178 struct omap2_mcspi_cs *cs = spi->controller_state;
179
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200180 return readl_relaxed(cs->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700181}
182
Hemanth Va41ae1a2009-09-22 16:46:16 -0700183static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
184{
185 struct omap2_mcspi_cs *cs = spi->controller_state;
186
187 return cs->chconf0;
188}
189
190static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
191{
192 struct omap2_mcspi_cs *cs = spi->controller_state;
193
194 cs->chconf0 = val;
195 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
Roman Tereshonkova330ce22010-03-15 09:06:28 +0000196 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700197}
198
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300199static inline int mcspi_bytes_per_word(int word_len)
200{
201 if (word_len <= 8)
202 return 1;
203 else if (word_len <= 16)
204 return 2;
205 else /* word_len <= 32 */
206 return 4;
207}
208
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700209static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
210 int is_read, int enable)
211{
212 u32 l, rw;
213
Hemanth Va41ae1a2009-09-22 16:46:16 -0700214 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700215
216 if (is_read) /* 1 is read, 0 write */
217 rw = OMAP2_MCSPI_CHCONF_DMAR;
218 else
219 rw = OMAP2_MCSPI_CHCONF_DMAW;
220
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530221 if (enable)
222 l |= rw;
223 else
224 l &= ~rw;
225
Hemanth Va41ae1a2009-09-22 16:46:16 -0700226 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700227}
228
229static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
230{
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100231 struct omap2_mcspi_cs *cs = spi->controller_state;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700232 u32 l;
233
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100234 l = cs->chctrl0;
235 if (enable)
236 l |= OMAP2_MCSPI_CHCTRL_EN;
237 else
238 l &= ~OMAP2_MCSPI_CHCTRL_EN;
239 cs->chctrl0 = l;
240 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000241 /* Flash post-writes */
242 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700243}
244
245static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
246{
247 u32 l;
248
Hemanth Va41ae1a2009-09-22 16:46:16 -0700249 l = mcspi_cached_chconf0(spi);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530250 if (cs_active)
251 l |= OMAP2_MCSPI_CHCONF_FORCE;
252 else
253 l &= ~OMAP2_MCSPI_CHCONF_FORCE;
254
Hemanth Va41ae1a2009-09-22 16:46:16 -0700255 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700256}
257
258static void omap2_mcspi_set_master_mode(struct spi_master *master)
259{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530260 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
261 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700262 u32 l;
263
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530264 /*
265 * Setup when switching from (reset default) slave mode
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700266 * to single-channel master mode
267 */
268 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530269 l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
270 l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700271 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700272
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530273 ctx->modulctrl = l;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700274}
275
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300276static void omap2_mcspi_set_fifo(const struct spi_device *spi,
277 struct spi_transfer *t, int enable)
278{
279 struct spi_master *master = spi->master;
280 struct omap2_mcspi_cs *cs = spi->controller_state;
281 struct omap2_mcspi *mcspi;
282 unsigned int wcnt;
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300283 int max_fifo_depth, fifo_depth, bytes_per_word;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300284 u32 chconf, xferlevel;
285
286 mcspi = spi_master_get_devdata(master);
287
288 chconf = mcspi_cached_chconf0(spi);
289 if (enable) {
290 bytes_per_word = mcspi_bytes_per_word(cs->word_len);
291 if (t->len % bytes_per_word != 0)
292 goto disable_fifo;
293
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300294 if (t->rx_buf != NULL && t->tx_buf != NULL)
295 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
296 else
297 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
298
299 fifo_depth = gcd(t->len, max_fifo_depth);
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300300 if (fifo_depth < 2 || fifo_depth % bytes_per_word != 0)
301 goto disable_fifo;
302
303 wcnt = t->len / bytes_per_word;
304 if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
305 goto disable_fifo;
306
307 xferlevel = wcnt << 16;
308 if (t->rx_buf != NULL) {
309 chconf |= OMAP2_MCSPI_CHCONF_FFER;
310 xferlevel |= (fifo_depth - 1) << 8;
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300311 }
312 if (t->tx_buf != NULL) {
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300313 chconf |= OMAP2_MCSPI_CHCONF_FFET;
314 xferlevel |= fifo_depth - 1;
315 }
316
317 mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
318 mcspi_write_chconf0(spi, chconf);
319 mcspi->fifo_depth = fifo_depth;
320
321 return;
322 }
323
324disable_fifo:
325 if (t->rx_buf != NULL)
326 chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
Jorge A. Ventura3d0763c2014-08-09 16:06:58 -0500327
328 if (t->tx_buf != NULL)
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300329 chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
330
331 mcspi_write_chconf0(spi, chconf);
332 mcspi->fifo_depth = 0;
333}
334
Hemanth Va41ae1a2009-09-22 16:46:16 -0700335static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
336{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530337 struct spi_master *spi_cntrl = mcspi->master;
338 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
339 struct omap2_mcspi_cs *cs;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700340
341 /* McSPI: context restore */
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530342 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
343 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700344
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530345 list_for_each_entry(cs, &ctx->cs, node)
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200346 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700347}
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700348
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300349static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
350{
351 unsigned long timeout;
352
353 timeout = jiffies + msecs_to_jiffies(1000);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200354 while (!(readl_relaxed(reg) & bit)) {
Sebastian Andrzej Siewiorff23fa32013-03-21 13:22:48 +0100355 if (time_after(jiffies, timeout)) {
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200356 if (!(readl_relaxed(reg) & bit))
Sebastian Andrzej Siewiorff23fa32013-03-21 13:22:48 +0100357 return -ETIMEDOUT;
358 else
359 return 0;
360 }
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300361 cpu_relax();
362 }
363 return 0;
364}
365
Russell King53741ed2012-04-23 13:51:48 +0100366static void omap2_mcspi_rx_callback(void *data)
367{
368 struct spi_device *spi = data;
369 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
370 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
371
Russell King53741ed2012-04-23 13:51:48 +0100372 /* We must disable the DMA RX request */
373 omap2_mcspi_set_dma_req(spi, 1, 0);
Felipe Balbi830379e2012-12-12 10:45:59 +0200374
375 complete(&mcspi_dma->dma_rx_completion);
Russell King53741ed2012-04-23 13:51:48 +0100376}
377
378static void omap2_mcspi_tx_callback(void *data)
379{
380 struct spi_device *spi = data;
381 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
382 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
383
Russell King53741ed2012-04-23 13:51:48 +0100384 /* We must disable the DMA TX request */
385 omap2_mcspi_set_dma_req(spi, 0, 0);
Felipe Balbi830379e2012-12-12 10:45:59 +0200386
387 complete(&mcspi_dma->dma_tx_completion);
Russell King53741ed2012-04-23 13:51:48 +0100388}
389
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530390static void omap2_mcspi_tx_dma(struct spi_device *spi,
391 struct spi_transfer *xfer,
392 struct dma_slave_config cfg)
393{
394 struct omap2_mcspi *mcspi;
395 struct omap2_mcspi_dma *mcspi_dma;
396 unsigned int count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530397
398 mcspi = spi_master_get_devdata(spi->master);
399 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
400 count = xfer->len;
401
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530402 if (mcspi_dma->dma_tx) {
403 struct dma_async_tx_descriptor *tx;
404 struct scatterlist sg;
405
406 dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
407
408 sg_init_table(&sg, 1);
409 sg_dma_address(&sg) = xfer->tx_dma;
410 sg_dma_len(&sg) = xfer->len;
411
412 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
413 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
414 if (tx) {
415 tx->callback = omap2_mcspi_tx_callback;
416 tx->callback_param = spi;
417 dmaengine_submit(tx);
418 } else {
419 /* FIXME: fall back to PIO? */
420 }
421 }
422 dma_async_issue_pending(mcspi_dma->dma_tx);
423 omap2_mcspi_set_dma_req(spi, 0, 1);
424
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530425}
426
427static unsigned
428omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
429 struct dma_slave_config cfg,
430 unsigned es)
431{
432 struct omap2_mcspi *mcspi;
433 struct omap2_mcspi_dma *mcspi_dma;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300434 unsigned int count, dma_count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530435 u32 l;
436 int elements = 0;
437 int word_len, element_count;
438 struct omap2_mcspi_cs *cs = spi->controller_state;
439 mcspi = spi_master_get_devdata(spi->master);
440 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
441 count = xfer->len;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300442 dma_count = xfer->len;
443
444 if (mcspi->fifo_depth == 0)
445 dma_count -= es;
446
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530447 word_len = cs->word_len;
448 l = mcspi_cached_chconf0(spi);
449
450 if (word_len <= 8)
451 element_count = count;
452 else if (word_len <= 16)
453 element_count = count >> 1;
454 else /* word_len <= 32 */
455 element_count = count >> 2;
456
457 if (mcspi_dma->dma_rx) {
458 struct dma_async_tx_descriptor *tx;
459 struct scatterlist sg;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530460
461 dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
462
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300463 if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
464 dma_count -= es;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530465
466 sg_init_table(&sg, 1);
467 sg_dma_address(&sg) = xfer->rx_dma;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300468 sg_dma_len(&sg) = dma_count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530469
470 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
471 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
472 DMA_CTRL_ACK);
473 if (tx) {
474 tx->callback = omap2_mcspi_rx_callback;
475 tx->callback_param = spi;
476 dmaengine_submit(tx);
477 } else {
478 /* FIXME: fall back to PIO? */
479 }
480 }
481
482 dma_async_issue_pending(mcspi_dma->dma_rx);
483 omap2_mcspi_set_dma_req(spi, 1, 1);
484
485 wait_for_completion(&mcspi_dma->dma_rx_completion);
486 dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
487 DMA_FROM_DEVICE);
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300488
489 if (mcspi->fifo_depth > 0)
490 return count;
491
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530492 omap2_mcspi_set_enable(spi, 0);
493
494 elements = element_count - 1;
495
496 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
497 elements--;
498
499 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
500 & OMAP2_MCSPI_CHSTAT_RXS)) {
501 u32 w;
502
503 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
504 if (word_len <= 8)
505 ((u8 *)xfer->rx_buf)[elements++] = w;
506 else if (word_len <= 16)
507 ((u16 *)xfer->rx_buf)[elements++] = w;
508 else /* word_len <= 32 */
509 ((u32 *)xfer->rx_buf)[elements++] = w;
510 } else {
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300511 int bytes_per_word = mcspi_bytes_per_word(word_len);
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300512 dev_err(&spi->dev, "DMA RX penultimate word empty\n");
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300513 count -= (bytes_per_word << 1);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530514 omap2_mcspi_set_enable(spi, 1);
515 return count;
516 }
517 }
518 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
519 & OMAP2_MCSPI_CHSTAT_RXS)) {
520 u32 w;
521
522 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
523 if (word_len <= 8)
524 ((u8 *)xfer->rx_buf)[elements] = w;
525 else if (word_len <= 16)
526 ((u16 *)xfer->rx_buf)[elements] = w;
527 else /* word_len <= 32 */
528 ((u32 *)xfer->rx_buf)[elements] = w;
529 } else {
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300530 dev_err(&spi->dev, "DMA RX last word empty\n");
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300531 count -= mcspi_bytes_per_word(word_len);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530532 }
533 omap2_mcspi_set_enable(spi, 1);
534 return count;
535}
536
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700537static unsigned
538omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
539{
540 struct omap2_mcspi *mcspi;
541 struct omap2_mcspi_cs *cs = spi->controller_state;
542 struct omap2_mcspi_dma *mcspi_dma;
Russell King8c7494a2012-04-23 13:56:25 +0100543 unsigned int count;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000544 u32 l;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530545 u8 *rx;
546 const u8 *tx;
Russell King53741ed2012-04-23 13:51:48 +0100547 struct dma_slave_config cfg;
548 enum dma_slave_buswidth width;
549 unsigned es;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300550 u32 burst;
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530551 void __iomem *chstat_reg;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300552 void __iomem *irqstat_reg;
553 int wait_res;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700554
555 mcspi = spi_master_get_devdata(spi->master);
556 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000557 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700558
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300559
Russell King53741ed2012-04-23 13:51:48 +0100560 if (cs->word_len <= 8) {
561 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
562 es = 1;
563 } else if (cs->word_len <= 16) {
564 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
565 es = 2;
566 } else {
567 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
568 es = 4;
569 }
570
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300571 count = xfer->len;
572 burst = 1;
573
574 if (mcspi->fifo_depth > 0) {
575 if (count > mcspi->fifo_depth)
576 burst = mcspi->fifo_depth / es;
577 else
578 burst = count / es;
579 }
580
Russell King53741ed2012-04-23 13:51:48 +0100581 memset(&cfg, 0, sizeof(cfg));
582 cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
583 cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
584 cfg.src_addr_width = width;
585 cfg.dst_addr_width = width;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300586 cfg.src_maxburst = burst;
587 cfg.dst_maxburst = burst;
Russell King53741ed2012-04-23 13:51:48 +0100588
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700589 rx = xfer->rx_buf;
590 tx = xfer->tx_buf;
591
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530592 if (tx != NULL)
593 omap2_mcspi_tx_dma(spi, xfer, cfg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700594
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530595 if (rx != NULL)
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530596 count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700597
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530598 if (tx != NULL) {
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530599 wait_for_completion(&mcspi_dma->dma_tx_completion);
600 dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len,
601 DMA_TO_DEVICE);
602
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300603 if (mcspi->fifo_depth > 0) {
604 irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
605
606 if (mcspi_wait_for_reg_bit(irqstat_reg,
607 OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
608 dev_err(&spi->dev, "EOW timed out\n");
609
610 mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
611 OMAP2_MCSPI_IRQSTATUS_EOW);
612 }
613
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530614 /* for TX_ONLY mode, be sure all words have shifted out */
615 if (rx == NULL) {
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300616 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
617 if (mcspi->fifo_depth > 0) {
618 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
619 OMAP2_MCSPI_CHSTAT_TXFFE);
620 if (wait_res < 0)
621 dev_err(&spi->dev, "TXFFE timed out\n");
622 } else {
623 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
624 OMAP2_MCSPI_CHSTAT_TXS);
625 if (wait_res < 0)
626 dev_err(&spi->dev, "TXS timed out\n");
627 }
628 if (wait_res >= 0 &&
629 (mcspi_wait_for_reg_bit(chstat_reg,
630 OMAP2_MCSPI_CHSTAT_EOT) < 0))
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530631 dev_err(&spi->dev, "EOT timed out\n");
632 }
633 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700634 return count;
635}
636
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700637static unsigned
638omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
639{
640 struct omap2_mcspi *mcspi;
641 struct omap2_mcspi_cs *cs = spi->controller_state;
642 unsigned int count, c;
643 u32 l;
644 void __iomem *base = cs->base;
645 void __iomem *tx_reg;
646 void __iomem *rx_reg;
647 void __iomem *chstat_reg;
648 int word_len;
649
650 mcspi = spi_master_get_devdata(spi->master);
651 count = xfer->len;
652 c = count;
653 word_len = cs->word_len;
654
Hemanth Va41ae1a2009-09-22 16:46:16 -0700655 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700656
657 /* We store the pre-calculated register addresses on stack to speed
658 * up the transfer loop. */
659 tx_reg = base + OMAP2_MCSPI_TX0;
660 rx_reg = base + OMAP2_MCSPI_RX0;
661 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
662
Michael Jonesadef6582011-02-25 16:55:11 +0100663 if (c < (word_len>>3))
664 return 0;
665
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700666 if (word_len <= 8) {
667 u8 *rx;
668 const u8 *tx;
669
670 rx = xfer->rx_buf;
671 tx = xfer->tx_buf;
672
673 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800674 c -= 1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700675 if (tx != NULL) {
676 if (mcspi_wait_for_reg_bit(chstat_reg,
677 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
678 dev_err(&spi->dev, "TXS timed out\n");
679 goto out;
680 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900681 dev_vdbg(&spi->dev, "write-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700682 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200683 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700684 }
685 if (rx != NULL) {
686 if (mcspi_wait_for_reg_bit(chstat_reg,
687 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
688 dev_err(&spi->dev, "RXS timed out\n");
689 goto out;
690 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000691
692 if (c == 1 && tx == NULL &&
693 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
694 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200695 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900696 dev_vdbg(&spi->dev, "read-%d %02x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000697 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000698 if (mcspi_wait_for_reg_bit(chstat_reg,
699 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
700 dev_err(&spi->dev,
701 "RXS timed out\n");
702 goto out;
703 }
704 c = 0;
705 } else if (c == 0 && tx == NULL) {
706 omap2_mcspi_set_enable(spi, 0);
707 }
708
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200709 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900710 dev_vdbg(&spi->dev, "read-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700711 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700712 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200713 } while (c);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700714 } else if (word_len <= 16) {
715 u16 *rx;
716 const u16 *tx;
717
718 rx = xfer->rx_buf;
719 tx = xfer->tx_buf;
720 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800721 c -= 2;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700722 if (tx != NULL) {
723 if (mcspi_wait_for_reg_bit(chstat_reg,
724 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
725 dev_err(&spi->dev, "TXS timed out\n");
726 goto out;
727 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900728 dev_vdbg(&spi->dev, "write-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700729 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200730 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700731 }
732 if (rx != NULL) {
733 if (mcspi_wait_for_reg_bit(chstat_reg,
734 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
735 dev_err(&spi->dev, "RXS timed out\n");
736 goto out;
737 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000738
739 if (c == 2 && tx == NULL &&
740 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
741 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200742 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900743 dev_vdbg(&spi->dev, "read-%d %04x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000744 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000745 if (mcspi_wait_for_reg_bit(chstat_reg,
746 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
747 dev_err(&spi->dev,
748 "RXS timed out\n");
749 goto out;
750 }
751 c = 0;
752 } else if (c == 0 && tx == NULL) {
753 omap2_mcspi_set_enable(spi, 0);
754 }
755
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200756 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900757 dev_vdbg(&spi->dev, "read-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700758 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700759 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200760 } while (c >= 2);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700761 } else if (word_len <= 32) {
762 u32 *rx;
763 const u32 *tx;
764
765 rx = xfer->rx_buf;
766 tx = xfer->tx_buf;
767 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800768 c -= 4;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700769 if (tx != NULL) {
770 if (mcspi_wait_for_reg_bit(chstat_reg,
771 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
772 dev_err(&spi->dev, "TXS timed out\n");
773 goto out;
774 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900775 dev_vdbg(&spi->dev, "write-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700776 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200777 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700778 }
779 if (rx != NULL) {
780 if (mcspi_wait_for_reg_bit(chstat_reg,
781 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
782 dev_err(&spi->dev, "RXS timed out\n");
783 goto out;
784 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000785
786 if (c == 4 && tx == NULL &&
787 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
788 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200789 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900790 dev_vdbg(&spi->dev, "read-%d %08x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000791 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000792 if (mcspi_wait_for_reg_bit(chstat_reg,
793 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
794 dev_err(&spi->dev,
795 "RXS timed out\n");
796 goto out;
797 }
798 c = 0;
799 } else if (c == 0 && tx == NULL) {
800 omap2_mcspi_set_enable(spi, 0);
801 }
802
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200803 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900804 dev_vdbg(&spi->dev, "read-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700805 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700806 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200807 } while (c >= 4);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700808 }
809
810 /* for TX_ONLY mode, be sure all words have shifted out */
811 if (xfer->rx_buf == NULL) {
812 if (mcspi_wait_for_reg_bit(chstat_reg,
813 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
814 dev_err(&spi->dev, "TXS timed out\n");
815 } else if (mcspi_wait_for_reg_bit(chstat_reg,
816 OMAP2_MCSPI_CHSTAT_EOT) < 0)
817 dev_err(&spi->dev, "EOT timed out\n");
Jason Wange1993ed2010-10-19 18:03:27 +0800818
819 /* disable chan to purge rx datas received in TX_ONLY transfer,
820 * otherwise these rx datas will affect the direct following
821 * RX_ONLY transfer.
822 */
823 omap2_mcspi_set_enable(spi, 0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700824 }
825out:
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000826 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700827 return count - c;
828}
829
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200830static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
831{
832 u32 div;
833
834 for (div = 0; div < 15; div++)
835 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
836 return div;
837
838 return 15;
839}
840
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700841/* called only when no transfer is active to this device */
842static int omap2_mcspi_setup_transfer(struct spi_device *spi,
843 struct spi_transfer *t)
844{
845 struct omap2_mcspi_cs *cs = spi->controller_state;
846 struct omap2_mcspi *mcspi;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700847 struct spi_master *spi_cntrl;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100848 u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700849 u8 word_len = spi->bits_per_word;
Scott Ellis9bd45172010-03-10 14:23:13 -0700850 u32 speed_hz = spi->max_speed_hz;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700851
852 mcspi = spi_master_get_devdata(spi->master);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700853 spi_cntrl = mcspi->master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700854
855 if (t != NULL && t->bits_per_word)
856 word_len = t->bits_per_word;
857
858 cs->word_len = word_len;
859
Scott Ellis9bd45172010-03-10 14:23:13 -0700860 if (t && t->speed_hz)
861 speed_hz = t->speed_hz;
862
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200863 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100864 if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) {
865 clkd = omap2_mcspi_calc_divisor(speed_hz);
866 speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd;
867 clkg = 0;
868 } else {
869 div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz;
870 speed_hz = OMAP2_MCSPI_MAX_FREQ / div;
871 clkd = (div - 1) & 0xf;
872 extclk = (div - 1) >> 4;
873 clkg = OMAP2_MCSPI_CHCONF_CLKG;
874 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700875
Hemanth Va41ae1a2009-09-22 16:46:16 -0700876 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700877
878 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
879 * REVISIT: this controller could support SPI_3WIRE mode.
880 */
Daniel Mack2cd45172012-11-14 11:14:26 +0800881 if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
Daniel Mack0384e902012-10-07 18:19:44 +0200882 l &= ~OMAP2_MCSPI_CHCONF_IS;
883 l &= ~OMAP2_MCSPI_CHCONF_DPE1;
884 l |= OMAP2_MCSPI_CHCONF_DPE0;
885 } else {
886 l |= OMAP2_MCSPI_CHCONF_IS;
887 l |= OMAP2_MCSPI_CHCONF_DPE1;
888 l &= ~OMAP2_MCSPI_CHCONF_DPE0;
889 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700890
891 /* wordlength */
892 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
893 l |= (word_len - 1) << 7;
894
895 /* set chipselect polarity; manage with FORCE */
896 if (!(spi->mode & SPI_CS_HIGH))
897 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
898 else
899 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
900
901 /* set clock divisor */
902 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100903 l |= clkd << 2;
904
905 /* set clock granularity */
906 l &= ~OMAP2_MCSPI_CHCONF_CLKG;
907 l |= clkg;
908 if (clkg) {
909 cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
910 cs->chctrl0 |= extclk << 8;
911 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
912 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700913
914 /* set SPI mode 0..3 */
915 if (spi->mode & SPI_CPOL)
916 l |= OMAP2_MCSPI_CHCONF_POL;
917 else
918 l &= ~OMAP2_MCSPI_CHCONF_POL;
919 if (spi->mode & SPI_CPHA)
920 l |= OMAP2_MCSPI_CHCONF_PHA;
921 else
922 l &= ~OMAP2_MCSPI_CHCONF_PHA;
923
Hemanth Va41ae1a2009-09-22 16:46:16 -0700924 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700925
Mark A. Greer97ca0d62014-07-01 20:28:32 -0700926 cs->mode = spi->mode;
927
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700928 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100929 speed_hz,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700930 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
931 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
932
933 return 0;
934}
935
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700936/*
937 * Note that we currently allow DMA only if we get a channel
938 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
939 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700940static int omap2_mcspi_request_dma(struct spi_device *spi)
941{
942 struct spi_master *master = spi->master;
943 struct omap2_mcspi *mcspi;
944 struct omap2_mcspi_dma *mcspi_dma;
Russell King53741ed2012-04-23 13:51:48 +0100945 dma_cap_mask_t mask;
946 unsigned sig;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700947
948 mcspi = spi_master_get_devdata(master);
949 mcspi_dma = mcspi->dma_channels + spi->chip_select;
950
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700951 init_completion(&mcspi_dma->dma_rx_completion);
952 init_completion(&mcspi_dma->dma_tx_completion);
953
Russell King53741ed2012-04-23 13:51:48 +0100954 dma_cap_zero(mask);
955 dma_cap_set(DMA_SLAVE, mask);
Russell King53741ed2012-04-23 13:51:48 +0100956 sig = mcspi_dma->dma_rx_sync_dev;
Matt Porter74f3aaa2013-06-22 23:07:38 +0530957
958 mcspi_dma->dma_rx =
959 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
960 &sig, &master->dev,
961 mcspi_dma->dma_rx_ch_name);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700962 if (!mcspi_dma->dma_rx)
963 goto no_dma;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700964
Russell King53741ed2012-04-23 13:51:48 +0100965 sig = mcspi_dma->dma_tx_sync_dev;
Matt Porter74f3aaa2013-06-22 23:07:38 +0530966 mcspi_dma->dma_tx =
967 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
968 &sig, &master->dev,
969 mcspi_dma->dma_tx_ch_name);
970
Russell King53741ed2012-04-23 13:51:48 +0100971 if (!mcspi_dma->dma_tx) {
Russell King53741ed2012-04-23 13:51:48 +0100972 dma_release_channel(mcspi_dma->dma_rx);
973 mcspi_dma->dma_rx = NULL;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700974 goto no_dma;
Russell King53741ed2012-04-23 13:51:48 +0100975 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700976
977 return 0;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700978
979no_dma:
980 dev_warn(&spi->dev, "not using DMA for McSPI\n");
981 return -EAGAIN;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700982}
983
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700984static int omap2_mcspi_setup(struct spi_device *spi)
985{
986 int ret;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530987 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
988 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700989 struct omap2_mcspi_dma *mcspi_dma;
990 struct omap2_mcspi_cs *cs = spi->controller_state;
991
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700992 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
993
994 if (!cs) {
Russell King10aa5a32012-06-18 11:27:04 +0100995 cs = kzalloc(sizeof *cs, GFP_KERNEL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700996 if (!cs)
997 return -ENOMEM;
998 cs->base = mcspi->base + spi->chip_select * 0x14;
Russell Kinge5480b732008-09-01 21:51:50 +0100999 cs->phys = mcspi->phys + spi->chip_select * 0x14;
Mark A. Greer97ca0d62014-07-01 20:28:32 -07001000 cs->mode = 0;
Hemanth Va41ae1a2009-09-22 16:46:16 -07001001 cs->chconf0 = 0;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +01001002 cs->chctrl0 = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001003 spi->controller_state = cs;
Tero Kristo89c05372009-09-22 16:46:17 -07001004 /* Link this to context save list */
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301005 list_add_tail(&cs->node, &ctx->cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001006 }
1007
Russell King8c7494a2012-04-23 13:56:25 +01001008 if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001009 ret = omap2_mcspi_request_dma(spi);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001010 if (ret < 0 && ret != -EAGAIN)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001011 return ret;
1012 }
1013
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301014 ret = pm_runtime_get_sync(mcspi->dev);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301015 if (ret < 0)
1016 return ret;
Hemanth Va41ae1a2009-09-22 16:46:16 -07001017
Kyungmin Park86eeb6f2007-10-16 01:27:45 -07001018 ret = omap2_mcspi_setup_transfer(spi, NULL);
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301019 pm_runtime_mark_last_busy(mcspi->dev);
1020 pm_runtime_put_autosuspend(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001021
1022 return ret;
1023}
1024
1025static void omap2_mcspi_cleanup(struct spi_device *spi)
1026{
1027 struct omap2_mcspi *mcspi;
1028 struct omap2_mcspi_dma *mcspi_dma;
Tero Kristo89c05372009-09-22 16:46:17 -07001029 struct omap2_mcspi_cs *cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001030
1031 mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001032
Scott Ellis5e774942010-03-10 14:22:45 -07001033 if (spi->controller_state) {
1034 /* Unlink controller state from context save list */
1035 cs = spi->controller_state;
1036 list_del(&cs->node);
Tero Kristo89c05372009-09-22 16:46:17 -07001037
Russell King10aa5a32012-06-18 11:27:04 +01001038 kfree(cs);
Scott Ellis5e774942010-03-10 14:22:45 -07001039 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001040
Scott Ellis99f1a432010-05-24 14:20:27 +00001041 if (spi->chip_select < spi->master->num_chipselect) {
1042 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
1043
Russell King53741ed2012-04-23 13:51:48 +01001044 if (mcspi_dma->dma_rx) {
1045 dma_release_channel(mcspi_dma->dma_rx);
1046 mcspi_dma->dma_rx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +00001047 }
Russell King53741ed2012-04-23 13:51:48 +01001048 if (mcspi_dma->dma_tx) {
1049 dma_release_channel(mcspi_dma->dma_tx);
1050 mcspi_dma->dma_tx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +00001051 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001052 }
1053}
1054
Michael Wellingb28cb942015-05-07 18:36:53 -05001055static int omap2_mcspi_work_one(struct omap2_mcspi *mcspi,
1056 struct spi_device *spi, struct spi_transfer *t)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001057{
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001058
1059 /* We only enable one channel at a time -- the one whose message is
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301060 * -- although this controller would gladly
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001061 * arbitrate among multiple channels. This corresponds to "single
1062 * channel" master mode. As a side effect, we need to manage the
1063 * chipselect with the FORCE bit ... CS != channel enable.
1064 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001065
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001066 struct spi_master *master;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001067 struct omap2_mcspi_dma *mcspi_dma;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301068 int cs_active = 0;
1069 struct omap2_mcspi_cs *cs;
1070 struct omap2_mcspi_device_config *cd;
1071 int par_override = 0;
1072 int status = 0;
1073 u32 chconf;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001074
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001075 master = spi->master;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001076 mcspi_dma = mcspi->dma_channels + spi->chip_select;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301077 cs = spi->controller_state;
1078 cd = spi->controller_data;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001079
Mark A. Greer97ca0d62014-07-01 20:28:32 -07001080 /*
1081 * The slave driver could have changed spi->mode in which case
1082 * it will be different from cs->mode (the current hardware setup).
1083 * If so, set par_override (even though its not a parity issue) so
1084 * omap2_mcspi_setup_transfer will be called to configure the hardware
1085 * with the correct mode on the first iteration of the loop below.
1086 */
1087 if (spi->mode != cs->mode)
1088 par_override = 1;
1089
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001090 omap2_mcspi_set_enable(spi, 0);
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001091
Michael Wellingb28cb942015-05-07 18:36:53 -05001092 if (par_override ||
1093 (t->speed_hz != spi->max_speed_hz) ||
1094 (t->bits_per_word != spi->bits_per_word)) {
1095 par_override = 1;
1096 status = omap2_mcspi_setup_transfer(spi, t);
1097 if (status < 0)
1098 goto out;
1099 if (t->speed_hz == spi->max_speed_hz &&
1100 t->bits_per_word == spi->bits_per_word)
1101 par_override = 0;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301102 }
Michael Wellingb28cb942015-05-07 18:36:53 -05001103 if (cd && cd->cs_per_word) {
1104 chconf = mcspi->ctx.modulctrl;
1105 chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
1106 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1107 mcspi->ctx.modulctrl =
1108 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1109 }
1110
1111 if (!cs_active) {
1112 omap2_mcspi_force_cs(spi, 1);
1113 cs_active = 1;
1114 }
1115
1116 chconf = mcspi_cached_chconf0(spi);
1117 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
1118 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
1119
1120 if (t->tx_buf == NULL)
1121 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
1122 else if (t->rx_buf == NULL)
1123 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
1124
1125 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
1126 /* Turbo mode is for more than one word */
1127 if (t->len > ((cs->word_len + 7) >> 3))
1128 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
1129 }
1130
1131 mcspi_write_chconf0(spi, chconf);
1132
1133 if (t->len) {
1134 unsigned count;
1135
1136 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1137 (t->len >= DMA_MIN_BYTES))
1138 omap2_mcspi_set_fifo(spi, t, 1);
1139
1140 omap2_mcspi_set_enable(spi, 1);
1141
1142 /* RX_ONLY mode needs dummy data in TX reg */
1143 if (t->tx_buf == NULL)
1144 writel_relaxed(0, cs->base
1145 + OMAP2_MCSPI_TX0);
1146
1147 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1148 (t->len >= DMA_MIN_BYTES))
1149 count = omap2_mcspi_txrx_dma(spi, t);
1150 else
1151 count = omap2_mcspi_txrx_pio(spi, t);
1152
1153 if (count != t->len) {
1154 status = -EIO;
1155 goto out;
1156 }
1157 }
1158
1159 if (t->delay_usecs)
1160 udelay(t->delay_usecs);
1161
1162 /* ignore the "leave it on after last xfer" hint */
1163 if (t->cs_change) {
1164 omap2_mcspi_force_cs(spi, 0);
1165 cs_active = 0;
1166 }
1167
1168 omap2_mcspi_set_enable(spi, 0);
1169
1170 if (mcspi->fifo_depth > 0)
1171 omap2_mcspi_set_fifo(spi, t, 0);
1172
1173out:
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301174 /* Restore defaults if they were overriden */
1175 if (par_override) {
1176 par_override = 0;
1177 status = omap2_mcspi_setup_transfer(spi, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001178 }
1179
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301180 if (cs_active)
1181 omap2_mcspi_force_cs(spi, 0);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301182
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001183 if (cd && cd->cs_per_word) {
1184 chconf = mcspi->ctx.modulctrl;
1185 chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
1186 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1187 mcspi->ctx.modulctrl =
1188 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1189 }
1190
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301191 omap2_mcspi_set_enable(spi, 0);
1192
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001193 if (mcspi->fifo_depth > 0 && t)
1194 omap2_mcspi_set_fifo(spi, t, 0);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301195
Michael Wellingb28cb942015-05-07 18:36:53 -05001196 return status;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001197}
1198
Michael Wellingb28cb942015-05-07 18:36:53 -05001199static int omap2_mcspi_transfer_one(struct spi_master *master,
1200 struct spi_device *spi, struct spi_transfer *t)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001201{
1202 struct omap2_mcspi *mcspi;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001203 struct omap2_mcspi_dma *mcspi_dma;
Michael Wellingb28cb942015-05-07 18:36:53 -05001204 const void *tx_buf = t->tx_buf;
1205 void *rx_buf = t->rx_buf;
1206 unsigned len = t->len;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001207
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301208 mcspi = spi_master_get_devdata(master);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001209 mcspi_dma = mcspi->dma_channels + spi->chip_select;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001210
Michael Wellingb28cb942015-05-07 18:36:53 -05001211 if ((len && !(rx_buf || tx_buf))) {
1212 dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
1213 t->speed_hz,
1214 len,
1215 tx_buf ? "tx" : "",
1216 rx_buf ? "rx" : "",
1217 t->bits_per_word);
1218 return -EINVAL;
1219 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001220
Michael Wellingb28cb942015-05-07 18:36:53 -05001221 if (len < DMA_MIN_BYTES)
1222 goto skip_dma_map;
1223
1224 if (mcspi_dma->dma_tx && tx_buf != NULL) {
1225 t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
1226 len, DMA_TO_DEVICE);
1227 if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
1228 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1229 'T', len);
1230 return -EINVAL;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001231 }
Michael Wellingb28cb942015-05-07 18:36:53 -05001232 }
1233 if (mcspi_dma->dma_rx && rx_buf != NULL) {
1234 t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
1235 DMA_FROM_DEVICE);
1236 if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
1237 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1238 'R', len);
1239 if (tx_buf != NULL)
1240 dma_unmap_single(mcspi->dev, t->tx_dma,
1241 len, DMA_TO_DEVICE);
1242 return -EINVAL;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001243 }
1244 }
1245
Michael Wellingb28cb942015-05-07 18:36:53 -05001246skip_dma_map:
1247 return omap2_mcspi_work_one(mcspi, spi, t);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001248}
1249
Grant Likelyfd4a3192012-12-07 16:57:14 +00001250static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001251{
1252 struct spi_master *master = mcspi->master;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301253 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301254 int ret = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001255
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301256 ret = pm_runtime_get_sync(mcspi->dev);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301257 if (ret < 0)
1258 return ret;
Jouni Hoganderddb22192009-07-29 15:02:11 -07001259
Shubhrajyoti D39f80522012-03-29 22:11:07 +05301260 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
Matthias Brugger18dd6192013-01-24 13:28:58 +01001261 OMAP2_MCSPI_WAKEUPENABLE_WKEN);
Shubhrajyoti D39f80522012-03-29 22:11:07 +05301262 ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001263
1264 omap2_mcspi_set_master_mode(master);
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301265 pm_runtime_mark_last_busy(mcspi->dev);
1266 pm_runtime_put_autosuspend(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001267 return 0;
1268}
1269
Govindraj.R1f1a4382011-02-02 17:52:15 +05301270static int omap_mcspi_runtime_resume(struct device *dev)
1271{
1272 struct omap2_mcspi *mcspi;
1273 struct spi_master *master;
1274
1275 master = dev_get_drvdata(dev);
1276 mcspi = spi_master_get_devdata(master);
1277 omap2_mcspi_restore_ctx(mcspi);
1278
1279 return 0;
1280}
1281
Benoit Coussond5a80032012-02-15 18:37:34 +01001282static struct omap2_mcspi_platform_config omap2_pdata = {
1283 .regs_offset = 0,
1284};
1285
1286static struct omap2_mcspi_platform_config omap4_pdata = {
1287 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1288};
1289
1290static const struct of_device_id omap_mcspi_of_match[] = {
1291 {
1292 .compatible = "ti,omap2-mcspi",
1293 .data = &omap2_pdata,
1294 },
1295 {
1296 .compatible = "ti,omap4-mcspi",
1297 .data = &omap4_pdata,
1298 },
1299 { },
1300};
1301MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
Girishccc7bae2008-02-06 01:38:16 -08001302
Grant Likelyfd4a3192012-12-07 16:57:14 +00001303static int omap2_mcspi_probe(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001304{
1305 struct spi_master *master;
Uwe Kleine-König83a01e72012-05-21 21:57:39 +02001306 const struct omap2_mcspi_platform_config *pdata;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001307 struct omap2_mcspi *mcspi;
1308 struct resource *r;
1309 int status = 0, i;
Benoit Coussond5a80032012-02-15 18:37:34 +01001310 u32 regs_offset = 0;
1311 static int bus_num = 1;
1312 struct device_node *node = pdev->dev.of_node;
1313 const struct of_device_id *match;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001314
1315 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1316 if (master == NULL) {
1317 dev_dbg(&pdev->dev, "master allocation failed\n");
1318 return -ENOMEM;
1319 }
1320
David Brownelle7db06b2009-06-17 16:26:04 -07001321 /* the spi->mode bits understood by this driver: */
1322 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
Stephen Warren24778be2013-05-21 20:36:35 -06001323 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001324 master->setup = omap2_mcspi_setup;
Mark Brownf0278a12013-07-28 15:34:37 +01001325 master->auto_runtime_pm = true;
Michael Wellingb28cb942015-05-07 18:36:53 -05001326 master->transfer_one = omap2_mcspi_transfer_one;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001327 master->cleanup = omap2_mcspi_cleanup;
Benoit Coussond5a80032012-02-15 18:37:34 +01001328 master->dev.of_node = node;
Axel Linaca09242014-02-18 22:02:47 +08001329 master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ;
1330 master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15;
Benoit Coussond5a80032012-02-15 18:37:34 +01001331
Jingoo Han24b5a822013-05-23 19:20:40 +09001332 platform_set_drvdata(pdev, master);
Daniel Mack0384e902012-10-07 18:19:44 +02001333
1334 mcspi = spi_master_get_devdata(master);
1335 mcspi->master = master;
1336
Benoit Coussond5a80032012-02-15 18:37:34 +01001337 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1338 if (match) {
1339 u32 num_cs = 1; /* default number of chipselect */
1340 pdata = match->data;
1341
1342 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1343 master->num_chipselect = num_cs;
1344 master->bus_num = bus_num++;
Daniel Mack2cd45172012-11-14 11:14:26 +08001345 if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
1346 mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
Benoit Coussond5a80032012-02-15 18:37:34 +01001347 } else {
Jingoo Han8074cf02013-07-30 16:58:59 +09001348 pdata = dev_get_platdata(&pdev->dev);
Benoit Coussond5a80032012-02-15 18:37:34 +01001349 master->num_chipselect = pdata->num_cs;
1350 if (pdev->id != -1)
1351 master->bus_num = pdev->id;
Daniel Mack0384e902012-10-07 18:19:44 +02001352 mcspi->pin_dir = pdata->pin_dir;
Benoit Coussond5a80032012-02-15 18:37:34 +01001353 }
1354 regs_offset = pdata->regs_offset;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001355
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001356 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1357 if (r == NULL) {
1358 status = -ENODEV;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301359 goto free_master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001360 }
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301361
Benoit Coussond5a80032012-02-15 18:37:34 +01001362 r->start += regs_offset;
1363 r->end += regs_offset;
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301364 mcspi->phys = r->start;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001365
Thierry Redingb0ee5602013-01-21 11:09:18 +01001366 mcspi->base = devm_ioremap_resource(&pdev->dev, r);
1367 if (IS_ERR(mcspi->base)) {
1368 status = PTR_ERR(mcspi->base);
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301369 goto free_master;
Russell King55c381e2008-09-04 14:07:22 +01001370 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001371
Govindraj.R1f1a4382011-02-02 17:52:15 +05301372 mcspi->dev = &pdev->dev;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001373
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301374 INIT_LIST_HEAD(&mcspi->ctx.cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001375
Axel Lina6f936d2014-03-29 21:37:44 +08001376 mcspi->dma_channels = devm_kcalloc(&pdev->dev, master->num_chipselect,
1377 sizeof(struct omap2_mcspi_dma),
1378 GFP_KERNEL);
1379 if (mcspi->dma_channels == NULL) {
1380 status = -ENOMEM;
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301381 goto free_master;
Axel Lina6f936d2014-03-29 21:37:44 +08001382 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001383
Charulatha V1a5d8192011-02-02 17:52:14 +05301384 for (i = 0; i < master->num_chipselect; i++) {
Matt Porter74f3aaa2013-06-22 23:07:38 +05301385 char *dma_rx_ch_name = mcspi->dma_channels[i].dma_rx_ch_name;
1386 char *dma_tx_ch_name = mcspi->dma_channels[i].dma_tx_ch_name;
Charulatha V1a5d8192011-02-02 17:52:14 +05301387 struct resource *dma_res;
1388
Matt Porter74f3aaa2013-06-22 23:07:38 +05301389 sprintf(dma_rx_ch_name, "rx%d", i);
1390 if (!pdev->dev.of_node) {
1391 dma_res =
1392 platform_get_resource_byname(pdev,
1393 IORESOURCE_DMA,
1394 dma_rx_ch_name);
1395 if (!dma_res) {
1396 dev_dbg(&pdev->dev,
1397 "cannot get DMA RX channel\n");
1398 status = -ENODEV;
1399 break;
1400 }
Charulatha V1a5d8192011-02-02 17:52:14 +05301401
Matt Porter74f3aaa2013-06-22 23:07:38 +05301402 mcspi->dma_channels[i].dma_rx_sync_dev =
1403 dma_res->start;
Charulatha V1a5d8192011-02-02 17:52:14 +05301404 }
Matt Porter74f3aaa2013-06-22 23:07:38 +05301405 sprintf(dma_tx_ch_name, "tx%d", i);
1406 if (!pdev->dev.of_node) {
1407 dma_res =
1408 platform_get_resource_byname(pdev,
1409 IORESOURCE_DMA,
1410 dma_tx_ch_name);
1411 if (!dma_res) {
1412 dev_dbg(&pdev->dev,
1413 "cannot get DMA TX channel\n");
1414 status = -ENODEV;
1415 break;
1416 }
Charulatha V1a5d8192011-02-02 17:52:14 +05301417
Matt Porter74f3aaa2013-06-22 23:07:38 +05301418 mcspi->dma_channels[i].dma_tx_sync_dev =
1419 dma_res->start;
1420 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001421 }
1422
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301423 if (status < 0)
Axel Lina6f936d2014-03-29 21:37:44 +08001424 goto free_master;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301425
Shubhrajyoti D27b52842012-03-26 17:04:22 +05301426 pm_runtime_use_autosuspend(&pdev->dev);
1427 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301428 pm_runtime_enable(&pdev->dev);
1429
Wei Yongjun142e07b2013-04-18 11:14:59 +08001430 status = omap2_mcspi_master_setup(mcspi);
1431 if (status < 0)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301432 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001433
Jingoo Hanb95e02b2013-09-24 13:40:29 +09001434 status = devm_spi_register_master(&pdev->dev, master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001435 if (status < 0)
Shubhrajyoti D37a2d842012-08-02 16:41:25 +05301436 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001437
1438 return status;
1439
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301440disable_pm:
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301441 pm_runtime_disable(&pdev->dev);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301442free_master:
Shubhrajyoti D37a2d842012-08-02 16:41:25 +05301443 spi_master_put(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001444 return status;
1445}
1446
Grant Likelyfd4a3192012-12-07 16:57:14 +00001447static int omap2_mcspi_remove(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001448{
Axel Lina6f936d2014-03-29 21:37:44 +08001449 struct spi_master *master = platform_get_drvdata(pdev);
1450 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001451
Shubhrajyoti Da93a2022012-08-22 11:35:14 +05301452 pm_runtime_put_sync(mcspi->dev);
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301453 pm_runtime_disable(&pdev->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001454
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001455 return 0;
1456}
1457
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001458/* work with hotplug and coldplug */
1459MODULE_ALIAS("platform:omap2_mcspi");
1460
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001461#ifdef CONFIG_SUSPEND
1462/*
1463 * When SPI wake up from off-mode, CS is in activate state. If it was in
1464 * unactive state when driver was suspend, then force it to unactive state at
1465 * wake up.
1466 */
1467static int omap2_mcspi_resume(struct device *dev)
1468{
1469 struct spi_master *master = dev_get_drvdata(dev);
1470 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301471 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1472 struct omap2_mcspi_cs *cs;
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001473
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301474 pm_runtime_get_sync(mcspi->dev);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301475 list_for_each_entry(cs, &ctx->cs, node) {
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001476 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001477 /*
1478 * We need to toggle CS state for OMAP take this
1479 * change in account.
1480 */
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +05301481 cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
Victor Kamensky21b2ce52013-11-16 02:01:16 +02001482 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +05301483 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
Victor Kamensky21b2ce52013-11-16 02:01:16 +02001484 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001485 }
1486 }
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301487 pm_runtime_mark_last_busy(mcspi->dev);
1488 pm_runtime_put_autosuspend(mcspi->dev);
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001489 return 0;
1490}
1491#else
1492#define omap2_mcspi_resume NULL
1493#endif
1494
1495static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1496 .resume = omap2_mcspi_resume,
Govindraj.R1f1a4382011-02-02 17:52:15 +05301497 .runtime_resume = omap_mcspi_runtime_resume,
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001498};
1499
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001500static struct platform_driver omap2_mcspi_driver = {
1501 .driver = {
1502 .name = "omap2_mcspi",
Benoit Coussond5a80032012-02-15 18:37:34 +01001503 .pm = &omap2_mcspi_pm_ops,
1504 .of_match_table = omap_mcspi_of_match,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001505 },
Felipe Balbi7d6b6d82012-03-14 11:18:30 +02001506 .probe = omap2_mcspi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001507 .remove = omap2_mcspi_remove,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001508};
1509
Felipe Balbi9fdca9d2012-03-14 11:18:31 +02001510module_platform_driver(omap2_mcspi_driver);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001511MODULE_LICENSE("GPL");