blob: 352eed7463aca0dc23d104178538236f5534257c [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.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <linux/kernel.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070025#include <linux/interrupt.h>
26#include <linux/module.h>
27#include <linux/device.h>
28#include <linux/delay.h>
29#include <linux/dma-mapping.h>
Russell King53741ed2012-04-23 13:51:48 +010030#include <linux/dmaengine.h>
31#include <linux/omap-dma.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070032#include <linux/platform_device.h>
33#include <linux/err.h>
34#include <linux/clk.h>
35#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Govindraj.R1f1a4382011-02-02 17:52:15 +053037#include <linux/pm_runtime.h>
Benoit Coussond5a80032012-02-15 18:37:34 +010038#include <linux/of.h>
39#include <linux/of_device.h>
Illia Smyrnovd33f4732013-06-17 16:31:06 +030040#include <linux/gcd.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070041
42#include <linux/spi/spi.h>
43
Arnd Bergmann22037472012-08-24 15:21:06 +020044#include <linux/platform_data/spi-omap2-mcspi.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070045
46#define OMAP2_MCSPI_MAX_FREQ 48000000
Stefan Sørensenfaee9b02014-02-02 16:24:25 +010047#define OMAP2_MCSPI_MAX_DIVIDER 4096
Illia Smyrnovd33f4732013-06-17 16:31:06 +030048#define OMAP2_MCSPI_MAX_FIFODEPTH 64
49#define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
Shubhrajyoti D27b52842012-03-26 17:04:22 +053050#define SPI_AUTOSUSPEND_TIMEOUT 2000
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070051
52#define OMAP2_MCSPI_REVISION 0x00
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070053#define OMAP2_MCSPI_SYSSTATUS 0x14
54#define OMAP2_MCSPI_IRQSTATUS 0x18
55#define OMAP2_MCSPI_IRQENABLE 0x1c
56#define OMAP2_MCSPI_WAKEUPENABLE 0x20
57#define OMAP2_MCSPI_SYST 0x24
58#define OMAP2_MCSPI_MODULCTRL 0x28
Illia Smyrnovd33f4732013-06-17 16:31:06 +030059#define OMAP2_MCSPI_XFERLEVEL 0x7c
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070060
61/* per-channel banks, 0x14 bytes each, first is: */
62#define OMAP2_MCSPI_CHCONF0 0x2c
63#define OMAP2_MCSPI_CHSTAT0 0x30
64#define OMAP2_MCSPI_CHCTRL0 0x34
65#define OMAP2_MCSPI_TX0 0x38
66#define OMAP2_MCSPI_RX0 0x3c
67
68/* per-register bitmasks: */
Illia Smyrnovd33f4732013-06-17 16:31:06 +030069#define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070070
Jouni Hogander7a8fa722009-09-22 16:45:58 -070071#define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
72#define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
73#define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070074
Jouni Hogander7a8fa722009-09-22 16:45:58 -070075#define OMAP2_MCSPI_CHCONF_PHA BIT(0)
76#define OMAP2_MCSPI_CHCONF_POL BIT(1)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070077#define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070078#define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070079#define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070080#define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
81#define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070082#define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070083#define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
84#define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
85#define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
86#define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
87#define OMAP2_MCSPI_CHCONF_IS BIT(18)
88#define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
89#define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
Illia Smyrnovd33f4732013-06-17 16:31:06 +030090#define OMAP2_MCSPI_CHCONF_FFET BIT(27)
91#define OMAP2_MCSPI_CHCONF_FFER BIT(28)
Stefan Sørensenfaee9b02014-02-02 16:24:25 +010092#define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070093
Jouni Hogander7a8fa722009-09-22 16:45:58 -070094#define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
95#define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
96#define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
Illia Smyrnovd33f4732013-06-17 16:31:06 +030097#define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070098
Jouni Hogander7a8fa722009-09-22 16:45:58 -070099#define OMAP2_MCSPI_CHCTRL_EN BIT(0)
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100100#define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700101
Jouni Hogander7a8fa722009-09-22 16:45:58 -0700102#define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700103
104/* We have 2 DMA channels per CS, one for RX and one for TX */
105struct omap2_mcspi_dma {
Russell King53741ed2012-04-23 13:51:48 +0100106 struct dma_chan *dma_tx;
107 struct dma_chan *dma_rx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700108
109 int dma_tx_sync_dev;
110 int dma_rx_sync_dev;
111
112 struct completion dma_tx_completion;
113 struct completion dma_rx_completion;
Matt Porter74f3aaa2013-06-22 23:07:38 +0530114
115 char dma_rx_ch_name[14];
116 char dma_tx_ch_name[14];
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700117};
118
119/* use PIO for small transfers, avoiding DMA setup/teardown overhead and
120 * cache operations; better heuristics consider wordsize and bitrate.
121 */
Roman Tereshonkov8b66c132010-04-12 09:07:54 +0000122#define DMA_MIN_BYTES 160
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700123
124
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530125/*
126 * Used for context save and restore, structure members to be updated whenever
127 * corresponding registers are modified.
128 */
129struct omap2_mcspi_regs {
130 u32 modulctrl;
131 u32 wakeupenable;
132 struct list_head cs;
133};
134
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700135struct omap2_mcspi {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700136 struct spi_master *master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700137 /* Virtual base address of the controller */
138 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100139 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700140 /* SPI1 has 4 channels, while SPI2 has 2 */
141 struct omap2_mcspi_dma *dma_channels;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530142 struct device *dev;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530143 struct omap2_mcspi_regs ctx;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300144 int fifo_depth;
Daniel Mack0384e902012-10-07 18:19:44 +0200145 unsigned int pin_dir:1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700146};
147
148struct omap2_mcspi_cs {
149 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100150 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700151 int word_len;
Mark A. Greer97ca0d62014-07-01 20:28:32 -0700152 u16 mode;
Tero Kristo89c05372009-09-22 16:46:17 -0700153 struct list_head node;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700154 /* Context save and restore shadow register */
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100155 u32 chconf0, chctrl0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700156};
157
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700158static inline void mcspi_write_reg(struct spi_master *master,
159 int idx, u32 val)
160{
161 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
162
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200163 writel_relaxed(val, mcspi->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700164}
165
166static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
167{
168 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
169
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200170 return readl_relaxed(mcspi->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700171}
172
173static inline void mcspi_write_cs_reg(const struct spi_device *spi,
174 int idx, u32 val)
175{
176 struct omap2_mcspi_cs *cs = spi->controller_state;
177
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200178 writel_relaxed(val, cs->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700179}
180
181static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
182{
183 struct omap2_mcspi_cs *cs = spi->controller_state;
184
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200185 return readl_relaxed(cs->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700186}
187
Hemanth Va41ae1a2009-09-22 16:46:16 -0700188static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
189{
190 struct omap2_mcspi_cs *cs = spi->controller_state;
191
192 return cs->chconf0;
193}
194
195static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
196{
197 struct omap2_mcspi_cs *cs = spi->controller_state;
198
199 cs->chconf0 = val;
200 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
Roman Tereshonkova330ce22010-03-15 09:06:28 +0000201 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700202}
203
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300204static inline int mcspi_bytes_per_word(int word_len)
205{
206 if (word_len <= 8)
207 return 1;
208 else if (word_len <= 16)
209 return 2;
210 else /* word_len <= 32 */
211 return 4;
212}
213
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700214static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
215 int is_read, int enable)
216{
217 u32 l, rw;
218
Hemanth Va41ae1a2009-09-22 16:46:16 -0700219 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700220
221 if (is_read) /* 1 is read, 0 write */
222 rw = OMAP2_MCSPI_CHCONF_DMAR;
223 else
224 rw = OMAP2_MCSPI_CHCONF_DMAW;
225
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530226 if (enable)
227 l |= rw;
228 else
229 l &= ~rw;
230
Hemanth Va41ae1a2009-09-22 16:46:16 -0700231 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700232}
233
234static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
235{
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100236 struct omap2_mcspi_cs *cs = spi->controller_state;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700237 u32 l;
238
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100239 l = cs->chctrl0;
240 if (enable)
241 l |= OMAP2_MCSPI_CHCTRL_EN;
242 else
243 l &= ~OMAP2_MCSPI_CHCTRL_EN;
244 cs->chctrl0 = l;
245 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000246 /* Flash post-writes */
247 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700248}
249
250static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
251{
252 u32 l;
253
Hemanth Va41ae1a2009-09-22 16:46:16 -0700254 l = mcspi_cached_chconf0(spi);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530255 if (cs_active)
256 l |= OMAP2_MCSPI_CHCONF_FORCE;
257 else
258 l &= ~OMAP2_MCSPI_CHCONF_FORCE;
259
Hemanth Va41ae1a2009-09-22 16:46:16 -0700260 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700261}
262
263static void omap2_mcspi_set_master_mode(struct spi_master *master)
264{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530265 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
266 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700267 u32 l;
268
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530269 /*
270 * Setup when switching from (reset default) slave mode
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700271 * to single-channel master mode
272 */
273 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530274 l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
275 l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700276 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700277
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530278 ctx->modulctrl = l;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700279}
280
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300281static void omap2_mcspi_set_fifo(const struct spi_device *spi,
282 struct spi_transfer *t, int enable)
283{
284 struct spi_master *master = spi->master;
285 struct omap2_mcspi_cs *cs = spi->controller_state;
286 struct omap2_mcspi *mcspi;
287 unsigned int wcnt;
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300288 int max_fifo_depth, fifo_depth, bytes_per_word;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300289 u32 chconf, xferlevel;
290
291 mcspi = spi_master_get_devdata(master);
292
293 chconf = mcspi_cached_chconf0(spi);
294 if (enable) {
295 bytes_per_word = mcspi_bytes_per_word(cs->word_len);
296 if (t->len % bytes_per_word != 0)
297 goto disable_fifo;
298
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300299 if (t->rx_buf != NULL && t->tx_buf != NULL)
300 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
301 else
302 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
303
304 fifo_depth = gcd(t->len, max_fifo_depth);
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300305 if (fifo_depth < 2 || fifo_depth % bytes_per_word != 0)
306 goto disable_fifo;
307
308 wcnt = t->len / bytes_per_word;
309 if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
310 goto disable_fifo;
311
312 xferlevel = wcnt << 16;
313 if (t->rx_buf != NULL) {
314 chconf |= OMAP2_MCSPI_CHCONF_FFER;
315 xferlevel |= (fifo_depth - 1) << 8;
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300316 }
317 if (t->tx_buf != NULL) {
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300318 chconf |= OMAP2_MCSPI_CHCONF_FFET;
319 xferlevel |= fifo_depth - 1;
320 }
321
322 mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
323 mcspi_write_chconf0(spi, chconf);
324 mcspi->fifo_depth = fifo_depth;
325
326 return;
327 }
328
329disable_fifo:
330 if (t->rx_buf != NULL)
331 chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
Jorge A. Ventura3d0763c2014-08-09 16:06:58 -0500332
333 if (t->tx_buf != NULL)
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300334 chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
335
336 mcspi_write_chconf0(spi, chconf);
337 mcspi->fifo_depth = 0;
338}
339
Hemanth Va41ae1a2009-09-22 16:46:16 -0700340static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
341{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530342 struct spi_master *spi_cntrl = mcspi->master;
343 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
344 struct omap2_mcspi_cs *cs;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700345
346 /* McSPI: context restore */
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530347 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
348 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700349
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530350 list_for_each_entry(cs, &ctx->cs, node)
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200351 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700352}
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700353
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300354static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
355{
356 unsigned long timeout;
357
358 timeout = jiffies + msecs_to_jiffies(1000);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200359 while (!(readl_relaxed(reg) & bit)) {
Sebastian Andrzej Siewiorff23fa32013-03-21 13:22:48 +0100360 if (time_after(jiffies, timeout)) {
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200361 if (!(readl_relaxed(reg) & bit))
Sebastian Andrzej Siewiorff23fa32013-03-21 13:22:48 +0100362 return -ETIMEDOUT;
363 else
364 return 0;
365 }
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300366 cpu_relax();
367 }
368 return 0;
369}
370
Russell King53741ed2012-04-23 13:51:48 +0100371static void omap2_mcspi_rx_callback(void *data)
372{
373 struct spi_device *spi = data;
374 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
375 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
376
Russell King53741ed2012-04-23 13:51:48 +0100377 /* We must disable the DMA RX request */
378 omap2_mcspi_set_dma_req(spi, 1, 0);
Felipe Balbi830379e2012-12-12 10:45:59 +0200379
380 complete(&mcspi_dma->dma_rx_completion);
Russell King53741ed2012-04-23 13:51:48 +0100381}
382
383static void omap2_mcspi_tx_callback(void *data)
384{
385 struct spi_device *spi = data;
386 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
387 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
388
Russell King53741ed2012-04-23 13:51:48 +0100389 /* We must disable the DMA TX request */
390 omap2_mcspi_set_dma_req(spi, 0, 0);
Felipe Balbi830379e2012-12-12 10:45:59 +0200391
392 complete(&mcspi_dma->dma_tx_completion);
Russell King53741ed2012-04-23 13:51:48 +0100393}
394
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530395static void omap2_mcspi_tx_dma(struct spi_device *spi,
396 struct spi_transfer *xfer,
397 struct dma_slave_config cfg)
398{
399 struct omap2_mcspi *mcspi;
400 struct omap2_mcspi_dma *mcspi_dma;
401 unsigned int count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530402
403 mcspi = spi_master_get_devdata(spi->master);
404 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
405 count = xfer->len;
406
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530407 if (mcspi_dma->dma_tx) {
408 struct dma_async_tx_descriptor *tx;
409 struct scatterlist sg;
410
411 dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
412
413 sg_init_table(&sg, 1);
414 sg_dma_address(&sg) = xfer->tx_dma;
415 sg_dma_len(&sg) = xfer->len;
416
417 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
418 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
419 if (tx) {
420 tx->callback = omap2_mcspi_tx_callback;
421 tx->callback_param = spi;
422 dmaengine_submit(tx);
423 } else {
424 /* FIXME: fall back to PIO? */
425 }
426 }
427 dma_async_issue_pending(mcspi_dma->dma_tx);
428 omap2_mcspi_set_dma_req(spi, 0, 1);
429
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530430}
431
432static unsigned
433omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
434 struct dma_slave_config cfg,
435 unsigned es)
436{
437 struct omap2_mcspi *mcspi;
438 struct omap2_mcspi_dma *mcspi_dma;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300439 unsigned int count, dma_count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530440 u32 l;
441 int elements = 0;
442 int word_len, element_count;
443 struct omap2_mcspi_cs *cs = spi->controller_state;
444 mcspi = spi_master_get_devdata(spi->master);
445 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
446 count = xfer->len;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300447 dma_count = xfer->len;
448
449 if (mcspi->fifo_depth == 0)
450 dma_count -= es;
451
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530452 word_len = cs->word_len;
453 l = mcspi_cached_chconf0(spi);
454
455 if (word_len <= 8)
456 element_count = count;
457 else if (word_len <= 16)
458 element_count = count >> 1;
459 else /* word_len <= 32 */
460 element_count = count >> 2;
461
462 if (mcspi_dma->dma_rx) {
463 struct dma_async_tx_descriptor *tx;
464 struct scatterlist sg;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530465
466 dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
467
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300468 if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
469 dma_count -= es;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530470
471 sg_init_table(&sg, 1);
472 sg_dma_address(&sg) = xfer->rx_dma;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300473 sg_dma_len(&sg) = dma_count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530474
475 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
476 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
477 DMA_CTRL_ACK);
478 if (tx) {
479 tx->callback = omap2_mcspi_rx_callback;
480 tx->callback_param = spi;
481 dmaengine_submit(tx);
482 } else {
483 /* FIXME: fall back to PIO? */
484 }
485 }
486
487 dma_async_issue_pending(mcspi_dma->dma_rx);
488 omap2_mcspi_set_dma_req(spi, 1, 1);
489
490 wait_for_completion(&mcspi_dma->dma_rx_completion);
491 dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
492 DMA_FROM_DEVICE);
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300493
494 if (mcspi->fifo_depth > 0)
495 return count;
496
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530497 omap2_mcspi_set_enable(spi, 0);
498
499 elements = element_count - 1;
500
501 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
502 elements--;
503
504 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
505 & OMAP2_MCSPI_CHSTAT_RXS)) {
506 u32 w;
507
508 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
509 if (word_len <= 8)
510 ((u8 *)xfer->rx_buf)[elements++] = w;
511 else if (word_len <= 16)
512 ((u16 *)xfer->rx_buf)[elements++] = w;
513 else /* word_len <= 32 */
514 ((u32 *)xfer->rx_buf)[elements++] = w;
515 } else {
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300516 int bytes_per_word = mcspi_bytes_per_word(word_len);
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300517 dev_err(&spi->dev, "DMA RX penultimate word empty\n");
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300518 count -= (bytes_per_word << 1);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530519 omap2_mcspi_set_enable(spi, 1);
520 return count;
521 }
522 }
523 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
524 & OMAP2_MCSPI_CHSTAT_RXS)) {
525 u32 w;
526
527 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
528 if (word_len <= 8)
529 ((u8 *)xfer->rx_buf)[elements] = w;
530 else if (word_len <= 16)
531 ((u16 *)xfer->rx_buf)[elements] = w;
532 else /* word_len <= 32 */
533 ((u32 *)xfer->rx_buf)[elements] = w;
534 } else {
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300535 dev_err(&spi->dev, "DMA RX last word empty\n");
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300536 count -= mcspi_bytes_per_word(word_len);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530537 }
538 omap2_mcspi_set_enable(spi, 1);
539 return count;
540}
541
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700542static unsigned
543omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
544{
545 struct omap2_mcspi *mcspi;
546 struct omap2_mcspi_cs *cs = spi->controller_state;
547 struct omap2_mcspi_dma *mcspi_dma;
Russell King8c7494a2012-04-23 13:56:25 +0100548 unsigned int count;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000549 u32 l;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530550 u8 *rx;
551 const u8 *tx;
Russell King53741ed2012-04-23 13:51:48 +0100552 struct dma_slave_config cfg;
553 enum dma_slave_buswidth width;
554 unsigned es;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300555 u32 burst;
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530556 void __iomem *chstat_reg;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300557 void __iomem *irqstat_reg;
558 int wait_res;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700559
560 mcspi = spi_master_get_devdata(spi->master);
561 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000562 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700563
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300564
Russell King53741ed2012-04-23 13:51:48 +0100565 if (cs->word_len <= 8) {
566 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
567 es = 1;
568 } else if (cs->word_len <= 16) {
569 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
570 es = 2;
571 } else {
572 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
573 es = 4;
574 }
575
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300576 count = xfer->len;
577 burst = 1;
578
579 if (mcspi->fifo_depth > 0) {
580 if (count > mcspi->fifo_depth)
581 burst = mcspi->fifo_depth / es;
582 else
583 burst = count / es;
584 }
585
Russell King53741ed2012-04-23 13:51:48 +0100586 memset(&cfg, 0, sizeof(cfg));
587 cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
588 cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
589 cfg.src_addr_width = width;
590 cfg.dst_addr_width = width;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300591 cfg.src_maxburst = burst;
592 cfg.dst_maxburst = burst;
Russell King53741ed2012-04-23 13:51:48 +0100593
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700594 rx = xfer->rx_buf;
595 tx = xfer->tx_buf;
596
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530597 if (tx != NULL)
598 omap2_mcspi_tx_dma(spi, xfer, cfg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700599
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530600 if (rx != NULL)
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530601 count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700602
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530603 if (tx != NULL) {
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530604 wait_for_completion(&mcspi_dma->dma_tx_completion);
605 dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len,
606 DMA_TO_DEVICE);
607
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300608 if (mcspi->fifo_depth > 0) {
609 irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
610
611 if (mcspi_wait_for_reg_bit(irqstat_reg,
612 OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
613 dev_err(&spi->dev, "EOW timed out\n");
614
615 mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
616 OMAP2_MCSPI_IRQSTATUS_EOW);
617 }
618
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530619 /* for TX_ONLY mode, be sure all words have shifted out */
620 if (rx == NULL) {
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300621 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
622 if (mcspi->fifo_depth > 0) {
623 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
624 OMAP2_MCSPI_CHSTAT_TXFFE);
625 if (wait_res < 0)
626 dev_err(&spi->dev, "TXFFE timed out\n");
627 } else {
628 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
629 OMAP2_MCSPI_CHSTAT_TXS);
630 if (wait_res < 0)
631 dev_err(&spi->dev, "TXS timed out\n");
632 }
633 if (wait_res >= 0 &&
634 (mcspi_wait_for_reg_bit(chstat_reg,
635 OMAP2_MCSPI_CHSTAT_EOT) < 0))
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530636 dev_err(&spi->dev, "EOT timed out\n");
637 }
638 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700639 return count;
640}
641
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700642static unsigned
643omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
644{
645 struct omap2_mcspi *mcspi;
646 struct omap2_mcspi_cs *cs = spi->controller_state;
647 unsigned int count, c;
648 u32 l;
649 void __iomem *base = cs->base;
650 void __iomem *tx_reg;
651 void __iomem *rx_reg;
652 void __iomem *chstat_reg;
653 int word_len;
654
655 mcspi = spi_master_get_devdata(spi->master);
656 count = xfer->len;
657 c = count;
658 word_len = cs->word_len;
659
Hemanth Va41ae1a2009-09-22 16:46:16 -0700660 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700661
662 /* We store the pre-calculated register addresses on stack to speed
663 * up the transfer loop. */
664 tx_reg = base + OMAP2_MCSPI_TX0;
665 rx_reg = base + OMAP2_MCSPI_RX0;
666 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
667
Michael Jonesadef6582011-02-25 16:55:11 +0100668 if (c < (word_len>>3))
669 return 0;
670
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700671 if (word_len <= 8) {
672 u8 *rx;
673 const u8 *tx;
674
675 rx = xfer->rx_buf;
676 tx = xfer->tx_buf;
677
678 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800679 c -= 1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700680 if (tx != NULL) {
681 if (mcspi_wait_for_reg_bit(chstat_reg,
682 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
683 dev_err(&spi->dev, "TXS timed out\n");
684 goto out;
685 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900686 dev_vdbg(&spi->dev, "write-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700687 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200688 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700689 }
690 if (rx != NULL) {
691 if (mcspi_wait_for_reg_bit(chstat_reg,
692 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
693 dev_err(&spi->dev, "RXS timed out\n");
694 goto out;
695 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000696
697 if (c == 1 && tx == NULL &&
698 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
699 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200700 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900701 dev_vdbg(&spi->dev, "read-%d %02x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000702 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000703 if (mcspi_wait_for_reg_bit(chstat_reg,
704 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
705 dev_err(&spi->dev,
706 "RXS timed out\n");
707 goto out;
708 }
709 c = 0;
710 } else if (c == 0 && tx == NULL) {
711 omap2_mcspi_set_enable(spi, 0);
712 }
713
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200714 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900715 dev_vdbg(&spi->dev, "read-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700716 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700717 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200718 } while (c);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700719 } else if (word_len <= 16) {
720 u16 *rx;
721 const u16 *tx;
722
723 rx = xfer->rx_buf;
724 tx = xfer->tx_buf;
725 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800726 c -= 2;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700727 if (tx != NULL) {
728 if (mcspi_wait_for_reg_bit(chstat_reg,
729 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
730 dev_err(&spi->dev, "TXS timed out\n");
731 goto out;
732 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900733 dev_vdbg(&spi->dev, "write-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700734 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200735 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700736 }
737 if (rx != NULL) {
738 if (mcspi_wait_for_reg_bit(chstat_reg,
739 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
740 dev_err(&spi->dev, "RXS timed out\n");
741 goto out;
742 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000743
744 if (c == 2 && tx == NULL &&
745 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
746 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200747 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900748 dev_vdbg(&spi->dev, "read-%d %04x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000749 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000750 if (mcspi_wait_for_reg_bit(chstat_reg,
751 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
752 dev_err(&spi->dev,
753 "RXS timed out\n");
754 goto out;
755 }
756 c = 0;
757 } else if (c == 0 && tx == NULL) {
758 omap2_mcspi_set_enable(spi, 0);
759 }
760
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200761 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900762 dev_vdbg(&spi->dev, "read-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700763 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700764 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200765 } while (c >= 2);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700766 } else if (word_len <= 32) {
767 u32 *rx;
768 const u32 *tx;
769
770 rx = xfer->rx_buf;
771 tx = xfer->tx_buf;
772 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800773 c -= 4;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700774 if (tx != NULL) {
775 if (mcspi_wait_for_reg_bit(chstat_reg,
776 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
777 dev_err(&spi->dev, "TXS timed out\n");
778 goto out;
779 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900780 dev_vdbg(&spi->dev, "write-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700781 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200782 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700783 }
784 if (rx != NULL) {
785 if (mcspi_wait_for_reg_bit(chstat_reg,
786 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
787 dev_err(&spi->dev, "RXS timed out\n");
788 goto out;
789 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000790
791 if (c == 4 && tx == NULL &&
792 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
793 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200794 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900795 dev_vdbg(&spi->dev, "read-%d %08x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000796 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000797 if (mcspi_wait_for_reg_bit(chstat_reg,
798 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
799 dev_err(&spi->dev,
800 "RXS timed out\n");
801 goto out;
802 }
803 c = 0;
804 } else if (c == 0 && tx == NULL) {
805 omap2_mcspi_set_enable(spi, 0);
806 }
807
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200808 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900809 dev_vdbg(&spi->dev, "read-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700810 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700811 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200812 } while (c >= 4);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700813 }
814
815 /* for TX_ONLY mode, be sure all words have shifted out */
816 if (xfer->rx_buf == NULL) {
817 if (mcspi_wait_for_reg_bit(chstat_reg,
818 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
819 dev_err(&spi->dev, "TXS timed out\n");
820 } else if (mcspi_wait_for_reg_bit(chstat_reg,
821 OMAP2_MCSPI_CHSTAT_EOT) < 0)
822 dev_err(&spi->dev, "EOT timed out\n");
Jason Wange1993ed2010-10-19 18:03:27 +0800823
824 /* disable chan to purge rx datas received in TX_ONLY transfer,
825 * otherwise these rx datas will affect the direct following
826 * RX_ONLY transfer.
827 */
828 omap2_mcspi_set_enable(spi, 0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700829 }
830out:
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000831 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700832 return count - c;
833}
834
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200835static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
836{
837 u32 div;
838
839 for (div = 0; div < 15; div++)
840 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
841 return div;
842
843 return 15;
844}
845
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700846/* called only when no transfer is active to this device */
847static int omap2_mcspi_setup_transfer(struct spi_device *spi,
848 struct spi_transfer *t)
849{
850 struct omap2_mcspi_cs *cs = spi->controller_state;
851 struct omap2_mcspi *mcspi;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700852 struct spi_master *spi_cntrl;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100853 u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700854 u8 word_len = spi->bits_per_word;
Scott Ellis9bd45172010-03-10 14:23:13 -0700855 u32 speed_hz = spi->max_speed_hz;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700856
857 mcspi = spi_master_get_devdata(spi->master);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700858 spi_cntrl = mcspi->master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700859
860 if (t != NULL && t->bits_per_word)
861 word_len = t->bits_per_word;
862
863 cs->word_len = word_len;
864
Scott Ellis9bd45172010-03-10 14:23:13 -0700865 if (t && t->speed_hz)
866 speed_hz = t->speed_hz;
867
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200868 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100869 if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) {
870 clkd = omap2_mcspi_calc_divisor(speed_hz);
871 speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd;
872 clkg = 0;
873 } else {
874 div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz;
875 speed_hz = OMAP2_MCSPI_MAX_FREQ / div;
876 clkd = (div - 1) & 0xf;
877 extclk = (div - 1) >> 4;
878 clkg = OMAP2_MCSPI_CHCONF_CLKG;
879 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700880
Hemanth Va41ae1a2009-09-22 16:46:16 -0700881 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700882
883 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
884 * REVISIT: this controller could support SPI_3WIRE mode.
885 */
Daniel Mack2cd45172012-11-14 11:14:26 +0800886 if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
Daniel Mack0384e902012-10-07 18:19:44 +0200887 l &= ~OMAP2_MCSPI_CHCONF_IS;
888 l &= ~OMAP2_MCSPI_CHCONF_DPE1;
889 l |= OMAP2_MCSPI_CHCONF_DPE0;
890 } else {
891 l |= OMAP2_MCSPI_CHCONF_IS;
892 l |= OMAP2_MCSPI_CHCONF_DPE1;
893 l &= ~OMAP2_MCSPI_CHCONF_DPE0;
894 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700895
896 /* wordlength */
897 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
898 l |= (word_len - 1) << 7;
899
900 /* set chipselect polarity; manage with FORCE */
901 if (!(spi->mode & SPI_CS_HIGH))
902 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
903 else
904 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
905
906 /* set clock divisor */
907 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100908 l |= clkd << 2;
909
910 /* set clock granularity */
911 l &= ~OMAP2_MCSPI_CHCONF_CLKG;
912 l |= clkg;
913 if (clkg) {
914 cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
915 cs->chctrl0 |= extclk << 8;
916 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
917 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700918
919 /* set SPI mode 0..3 */
920 if (spi->mode & SPI_CPOL)
921 l |= OMAP2_MCSPI_CHCONF_POL;
922 else
923 l &= ~OMAP2_MCSPI_CHCONF_POL;
924 if (spi->mode & SPI_CPHA)
925 l |= OMAP2_MCSPI_CHCONF_PHA;
926 else
927 l &= ~OMAP2_MCSPI_CHCONF_PHA;
928
Hemanth Va41ae1a2009-09-22 16:46:16 -0700929 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700930
Mark A. Greer97ca0d62014-07-01 20:28:32 -0700931 cs->mode = spi->mode;
932
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700933 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100934 speed_hz,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700935 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
936 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
937
938 return 0;
939}
940
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700941/*
942 * Note that we currently allow DMA only if we get a channel
943 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
944 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700945static int omap2_mcspi_request_dma(struct spi_device *spi)
946{
947 struct spi_master *master = spi->master;
948 struct omap2_mcspi *mcspi;
949 struct omap2_mcspi_dma *mcspi_dma;
Russell King53741ed2012-04-23 13:51:48 +0100950 dma_cap_mask_t mask;
951 unsigned sig;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700952
953 mcspi = spi_master_get_devdata(master);
954 mcspi_dma = mcspi->dma_channels + spi->chip_select;
955
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700956 init_completion(&mcspi_dma->dma_rx_completion);
957 init_completion(&mcspi_dma->dma_tx_completion);
958
Russell King53741ed2012-04-23 13:51:48 +0100959 dma_cap_zero(mask);
960 dma_cap_set(DMA_SLAVE, mask);
Russell King53741ed2012-04-23 13:51:48 +0100961 sig = mcspi_dma->dma_rx_sync_dev;
Matt Porter74f3aaa2013-06-22 23:07:38 +0530962
963 mcspi_dma->dma_rx =
964 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
965 &sig, &master->dev,
966 mcspi_dma->dma_rx_ch_name);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700967 if (!mcspi_dma->dma_rx)
968 goto no_dma;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700969
Russell King53741ed2012-04-23 13:51:48 +0100970 sig = mcspi_dma->dma_tx_sync_dev;
Matt Porter74f3aaa2013-06-22 23:07:38 +0530971 mcspi_dma->dma_tx =
972 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
973 &sig, &master->dev,
974 mcspi_dma->dma_tx_ch_name);
975
Russell King53741ed2012-04-23 13:51:48 +0100976 if (!mcspi_dma->dma_tx) {
Russell King53741ed2012-04-23 13:51:48 +0100977 dma_release_channel(mcspi_dma->dma_rx);
978 mcspi_dma->dma_rx = NULL;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700979 goto no_dma;
Russell King53741ed2012-04-23 13:51:48 +0100980 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700981
982 return 0;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700983
984no_dma:
985 dev_warn(&spi->dev, "not using DMA for McSPI\n");
986 return -EAGAIN;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700987}
988
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700989static int omap2_mcspi_setup(struct spi_device *spi)
990{
991 int ret;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530992 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
993 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700994 struct omap2_mcspi_dma *mcspi_dma;
995 struct omap2_mcspi_cs *cs = spi->controller_state;
996
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700997 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
998
999 if (!cs) {
Russell King10aa5a32012-06-18 11:27:04 +01001000 cs = kzalloc(sizeof *cs, GFP_KERNEL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001001 if (!cs)
1002 return -ENOMEM;
1003 cs->base = mcspi->base + spi->chip_select * 0x14;
Russell Kinge5480b732008-09-01 21:51:50 +01001004 cs->phys = mcspi->phys + spi->chip_select * 0x14;
Mark A. Greer97ca0d62014-07-01 20:28:32 -07001005 cs->mode = 0;
Hemanth Va41ae1a2009-09-22 16:46:16 -07001006 cs->chconf0 = 0;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +01001007 cs->chctrl0 = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001008 spi->controller_state = cs;
Tero Kristo89c05372009-09-22 16:46:17 -07001009 /* Link this to context save list */
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301010 list_add_tail(&cs->node, &ctx->cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001011 }
1012
Russell King8c7494a2012-04-23 13:56:25 +01001013 if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001014 ret = omap2_mcspi_request_dma(spi);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001015 if (ret < 0 && ret != -EAGAIN)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001016 return ret;
1017 }
1018
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301019 ret = pm_runtime_get_sync(mcspi->dev);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301020 if (ret < 0)
1021 return ret;
Hemanth Va41ae1a2009-09-22 16:46:16 -07001022
Kyungmin Park86eeb6f2007-10-16 01:27:45 -07001023 ret = omap2_mcspi_setup_transfer(spi, NULL);
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301024 pm_runtime_mark_last_busy(mcspi->dev);
1025 pm_runtime_put_autosuspend(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001026
1027 return ret;
1028}
1029
1030static void omap2_mcspi_cleanup(struct spi_device *spi)
1031{
1032 struct omap2_mcspi *mcspi;
1033 struct omap2_mcspi_dma *mcspi_dma;
Tero Kristo89c05372009-09-22 16:46:17 -07001034 struct omap2_mcspi_cs *cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001035
1036 mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001037
Scott Ellis5e774942010-03-10 14:22:45 -07001038 if (spi->controller_state) {
1039 /* Unlink controller state from context save list */
1040 cs = spi->controller_state;
1041 list_del(&cs->node);
Tero Kristo89c05372009-09-22 16:46:17 -07001042
Russell King10aa5a32012-06-18 11:27:04 +01001043 kfree(cs);
Scott Ellis5e774942010-03-10 14:22:45 -07001044 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001045
Scott Ellis99f1a432010-05-24 14:20:27 +00001046 if (spi->chip_select < spi->master->num_chipselect) {
1047 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
1048
Russell King53741ed2012-04-23 13:51:48 +01001049 if (mcspi_dma->dma_rx) {
1050 dma_release_channel(mcspi_dma->dma_rx);
1051 mcspi_dma->dma_rx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +00001052 }
Russell King53741ed2012-04-23 13:51:48 +01001053 if (mcspi_dma->dma_tx) {
1054 dma_release_channel(mcspi_dma->dma_tx);
1055 mcspi_dma->dma_tx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +00001056 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001057 }
1058}
1059
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301060static void omap2_mcspi_work(struct omap2_mcspi *mcspi, struct spi_message *m)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001061{
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001062
1063 /* We only enable one channel at a time -- the one whose message is
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301064 * -- although this controller would gladly
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001065 * arbitrate among multiple channels. This corresponds to "single
1066 * channel" master mode. As a side effect, we need to manage the
1067 * chipselect with the FORCE bit ... CS != channel enable.
1068 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001069
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301070 struct spi_device *spi;
1071 struct spi_transfer *t = NULL;
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001072 struct spi_master *master;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001073 struct omap2_mcspi_dma *mcspi_dma;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301074 int cs_active = 0;
1075 struct omap2_mcspi_cs *cs;
1076 struct omap2_mcspi_device_config *cd;
1077 int par_override = 0;
1078 int status = 0;
1079 u32 chconf;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001080
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301081 spi = m->spi;
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001082 master = spi->master;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001083 mcspi_dma = mcspi->dma_channels + spi->chip_select;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301084 cs = spi->controller_state;
1085 cd = spi->controller_data;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001086
Mark A. Greer97ca0d62014-07-01 20:28:32 -07001087 /*
1088 * The slave driver could have changed spi->mode in which case
1089 * it will be different from cs->mode (the current hardware setup).
1090 * If so, set par_override (even though its not a parity issue) so
1091 * omap2_mcspi_setup_transfer will be called to configure the hardware
1092 * with the correct mode on the first iteration of the loop below.
1093 */
1094 if (spi->mode != cs->mode)
1095 par_override = 1;
1096
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001097 omap2_mcspi_set_enable(spi, 0);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301098 list_for_each_entry(t, &m->transfers, transfer_list) {
1099 if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
1100 status = -EINVAL;
1101 break;
1102 }
Stefan Sørensen2bd16e32014-02-02 16:24:12 +01001103 if (par_override ||
1104 (t->speed_hz != spi->max_speed_hz) ||
1105 (t->bits_per_word != spi->bits_per_word)) {
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301106 par_override = 1;
1107 status = omap2_mcspi_setup_transfer(spi, t);
1108 if (status < 0)
1109 break;
Stefan Sørensen2bd16e32014-02-02 16:24:12 +01001110 if (t->speed_hz == spi->max_speed_hz &&
1111 t->bits_per_word == spi->bits_per_word)
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301112 par_override = 0;
1113 }
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001114 if (cd && cd->cs_per_word) {
1115 chconf = mcspi->ctx.modulctrl;
1116 chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
1117 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1118 mcspi->ctx.modulctrl =
1119 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1120 }
1121
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001122
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301123 if (!cs_active) {
1124 omap2_mcspi_force_cs(spi, 1);
1125 cs_active = 1;
1126 }
1127
1128 chconf = mcspi_cached_chconf0(spi);
1129 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
1130 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
1131
1132 if (t->tx_buf == NULL)
1133 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
1134 else if (t->rx_buf == NULL)
1135 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
1136
1137 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
1138 /* Turbo mode is for more than one word */
1139 if (t->len > ((cs->word_len + 7) >> 3))
1140 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
1141 }
1142
1143 mcspi_write_chconf0(spi, chconf);
1144
1145 if (t->len) {
1146 unsigned count;
1147
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001148 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1149 (m->is_dma_mapped || t->len >= DMA_MIN_BYTES))
1150 omap2_mcspi_set_fifo(spi, t, 1);
1151
1152 omap2_mcspi_set_enable(spi, 1);
1153
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301154 /* RX_ONLY mode needs dummy data in TX reg */
1155 if (t->tx_buf == NULL)
Victor Kamensky21b2ce52013-11-16 02:01:16 +02001156 writel_relaxed(0, cs->base
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301157 + OMAP2_MCSPI_TX0);
1158
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001159 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1160 (m->is_dma_mapped || t->len >= DMA_MIN_BYTES))
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301161 count = omap2_mcspi_txrx_dma(spi, t);
1162 else
1163 count = omap2_mcspi_txrx_pio(spi, t);
1164 m->actual_length += count;
1165
1166 if (count != t->len) {
1167 status = -EIO;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001168 break;
1169 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001170 }
1171
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301172 if (t->delay_usecs)
1173 udelay(t->delay_usecs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001174
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301175 /* ignore the "leave it on after last xfer" hint */
1176 if (t->cs_change) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001177 omap2_mcspi_force_cs(spi, 0);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301178 cs_active = 0;
1179 }
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001180
1181 omap2_mcspi_set_enable(spi, 0);
1182
1183 if (mcspi->fifo_depth > 0)
1184 omap2_mcspi_set_fifo(spi, t, 0);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301185 }
1186 /* Restore defaults if they were overriden */
1187 if (par_override) {
1188 par_override = 0;
1189 status = omap2_mcspi_setup_transfer(spi, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001190 }
1191
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301192 if (cs_active)
1193 omap2_mcspi_force_cs(spi, 0);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301194
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001195 if (cd && cd->cs_per_word) {
1196 chconf = mcspi->ctx.modulctrl;
1197 chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
1198 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1199 mcspi->ctx.modulctrl =
1200 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1201 }
1202
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301203 omap2_mcspi_set_enable(spi, 0);
1204
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001205 if (mcspi->fifo_depth > 0 && t)
1206 omap2_mcspi_set_fifo(spi, t, 0);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301207
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001208 m->status = status;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001209}
1210
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301211static int omap2_mcspi_transfer_one_message(struct spi_master *master,
Matthias Brugger18dd6192013-01-24 13:28:58 +01001212 struct spi_message *m)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001213{
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001214 struct spi_device *spi;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001215 struct omap2_mcspi *mcspi;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001216 struct omap2_mcspi_dma *mcspi_dma;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001217 struct spi_transfer *t;
1218
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001219 spi = m->spi;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301220 mcspi = spi_master_get_devdata(master);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001221 mcspi_dma = mcspi->dma_channels + spi->chip_select;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001222 m->actual_length = 0;
1223 m->status = 0;
1224
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001225 list_for_each_entry(t, &m->transfers, transfer_list) {
1226 const void *tx_buf = t->tx_buf;
1227 void *rx_buf = t->rx_buf;
1228 unsigned len = t->len;
1229
Axel Linaca09242014-02-18 22:02:47 +08001230 if ((len && !(rx_buf || tx_buf))) {
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301231 dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001232 t->speed_hz,
1233 len,
1234 tx_buf ? "tx" : "",
1235 rx_buf ? "rx" : "",
1236 t->bits_per_word);
1237 return -EINVAL;
1238 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001239
1240 if (m->is_dma_mapped || len < DMA_MIN_BYTES)
1241 continue;
1242
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001243 if (mcspi_dma->dma_tx && tx_buf != NULL) {
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301244 t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001245 len, DMA_TO_DEVICE);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301246 if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
1247 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001248 'T', len);
1249 return -EINVAL;
1250 }
1251 }
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001252 if (mcspi_dma->dma_rx && rx_buf != NULL) {
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301253 t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001254 DMA_FROM_DEVICE);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301255 if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
1256 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001257 'R', len);
1258 if (tx_buf != NULL)
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301259 dma_unmap_single(mcspi->dev, t->tx_dma,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001260 len, DMA_TO_DEVICE);
1261 return -EINVAL;
1262 }
1263 }
1264 }
1265
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301266 omap2_mcspi_work(mcspi, m);
1267 spi_finalize_current_message(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001268 return 0;
1269}
1270
Grant Likelyfd4a3192012-12-07 16:57:14 +00001271static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001272{
1273 struct spi_master *master = mcspi->master;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301274 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301275 int ret = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001276
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301277 ret = pm_runtime_get_sync(mcspi->dev);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301278 if (ret < 0)
1279 return ret;
Jouni Hoganderddb22192009-07-29 15:02:11 -07001280
Shubhrajyoti D39f80522012-03-29 22:11:07 +05301281 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
Matthias Brugger18dd6192013-01-24 13:28:58 +01001282 OMAP2_MCSPI_WAKEUPENABLE_WKEN);
Shubhrajyoti D39f80522012-03-29 22:11:07 +05301283 ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001284
1285 omap2_mcspi_set_master_mode(master);
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301286 pm_runtime_mark_last_busy(mcspi->dev);
1287 pm_runtime_put_autosuspend(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001288 return 0;
1289}
1290
Govindraj.R1f1a4382011-02-02 17:52:15 +05301291static int omap_mcspi_runtime_resume(struct device *dev)
1292{
1293 struct omap2_mcspi *mcspi;
1294 struct spi_master *master;
1295
1296 master = dev_get_drvdata(dev);
1297 mcspi = spi_master_get_devdata(master);
1298 omap2_mcspi_restore_ctx(mcspi);
1299
1300 return 0;
1301}
1302
Benoit Coussond5a80032012-02-15 18:37:34 +01001303static struct omap2_mcspi_platform_config omap2_pdata = {
1304 .regs_offset = 0,
1305};
1306
1307static struct omap2_mcspi_platform_config omap4_pdata = {
1308 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1309};
1310
1311static const struct of_device_id omap_mcspi_of_match[] = {
1312 {
1313 .compatible = "ti,omap2-mcspi",
1314 .data = &omap2_pdata,
1315 },
1316 {
1317 .compatible = "ti,omap4-mcspi",
1318 .data = &omap4_pdata,
1319 },
1320 { },
1321};
1322MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
Girishccc7bae2008-02-06 01:38:16 -08001323
Grant Likelyfd4a3192012-12-07 16:57:14 +00001324static int omap2_mcspi_probe(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001325{
1326 struct spi_master *master;
Uwe Kleine-König83a01e72012-05-21 21:57:39 +02001327 const struct omap2_mcspi_platform_config *pdata;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001328 struct omap2_mcspi *mcspi;
1329 struct resource *r;
1330 int status = 0, i;
Benoit Coussond5a80032012-02-15 18:37:34 +01001331 u32 regs_offset = 0;
1332 static int bus_num = 1;
1333 struct device_node *node = pdev->dev.of_node;
1334 const struct of_device_id *match;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001335
1336 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1337 if (master == NULL) {
1338 dev_dbg(&pdev->dev, "master allocation failed\n");
1339 return -ENOMEM;
1340 }
1341
David Brownelle7db06b2009-06-17 16:26:04 -07001342 /* the spi->mode bits understood by this driver: */
1343 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
Stephen Warren24778be2013-05-21 20:36:35 -06001344 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001345 master->setup = omap2_mcspi_setup;
Mark Brownf0278a12013-07-28 15:34:37 +01001346 master->auto_runtime_pm = true;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301347 master->transfer_one_message = omap2_mcspi_transfer_one_message;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001348 master->cleanup = omap2_mcspi_cleanup;
Benoit Coussond5a80032012-02-15 18:37:34 +01001349 master->dev.of_node = node;
Axel Linaca09242014-02-18 22:02:47 +08001350 master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ;
1351 master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15;
Benoit Coussond5a80032012-02-15 18:37:34 +01001352
Jingoo Han24b5a822013-05-23 19:20:40 +09001353 platform_set_drvdata(pdev, master);
Daniel Mack0384e902012-10-07 18:19:44 +02001354
1355 mcspi = spi_master_get_devdata(master);
1356 mcspi->master = master;
1357
Benoit Coussond5a80032012-02-15 18:37:34 +01001358 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1359 if (match) {
1360 u32 num_cs = 1; /* default number of chipselect */
1361 pdata = match->data;
1362
1363 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1364 master->num_chipselect = num_cs;
1365 master->bus_num = bus_num++;
Daniel Mack2cd45172012-11-14 11:14:26 +08001366 if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
1367 mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
Benoit Coussond5a80032012-02-15 18:37:34 +01001368 } else {
Jingoo Han8074cf02013-07-30 16:58:59 +09001369 pdata = dev_get_platdata(&pdev->dev);
Benoit Coussond5a80032012-02-15 18:37:34 +01001370 master->num_chipselect = pdata->num_cs;
1371 if (pdev->id != -1)
1372 master->bus_num = pdev->id;
Daniel Mack0384e902012-10-07 18:19:44 +02001373 mcspi->pin_dir = pdata->pin_dir;
Benoit Coussond5a80032012-02-15 18:37:34 +01001374 }
1375 regs_offset = pdata->regs_offset;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001376
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001377 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1378 if (r == NULL) {
1379 status = -ENODEV;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301380 goto free_master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001381 }
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301382
Benoit Coussond5a80032012-02-15 18:37:34 +01001383 r->start += regs_offset;
1384 r->end += regs_offset;
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301385 mcspi->phys = r->start;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001386
Thierry Redingb0ee5602013-01-21 11:09:18 +01001387 mcspi->base = devm_ioremap_resource(&pdev->dev, r);
1388 if (IS_ERR(mcspi->base)) {
1389 status = PTR_ERR(mcspi->base);
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301390 goto free_master;
Russell King55c381e2008-09-04 14:07:22 +01001391 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001392
Govindraj.R1f1a4382011-02-02 17:52:15 +05301393 mcspi->dev = &pdev->dev;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001394
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301395 INIT_LIST_HEAD(&mcspi->ctx.cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001396
Axel Lina6f936d2014-03-29 21:37:44 +08001397 mcspi->dma_channels = devm_kcalloc(&pdev->dev, master->num_chipselect,
1398 sizeof(struct omap2_mcspi_dma),
1399 GFP_KERNEL);
1400 if (mcspi->dma_channels == NULL) {
1401 status = -ENOMEM;
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301402 goto free_master;
Axel Lina6f936d2014-03-29 21:37:44 +08001403 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001404
Charulatha V1a5d8192011-02-02 17:52:14 +05301405 for (i = 0; i < master->num_chipselect; i++) {
Matt Porter74f3aaa2013-06-22 23:07:38 +05301406 char *dma_rx_ch_name = mcspi->dma_channels[i].dma_rx_ch_name;
1407 char *dma_tx_ch_name = mcspi->dma_channels[i].dma_tx_ch_name;
Charulatha V1a5d8192011-02-02 17:52:14 +05301408 struct resource *dma_res;
1409
Matt Porter74f3aaa2013-06-22 23:07:38 +05301410 sprintf(dma_rx_ch_name, "rx%d", i);
1411 if (!pdev->dev.of_node) {
1412 dma_res =
1413 platform_get_resource_byname(pdev,
1414 IORESOURCE_DMA,
1415 dma_rx_ch_name);
1416 if (!dma_res) {
1417 dev_dbg(&pdev->dev,
1418 "cannot get DMA RX channel\n");
1419 status = -ENODEV;
1420 break;
1421 }
Charulatha V1a5d8192011-02-02 17:52:14 +05301422
Matt Porter74f3aaa2013-06-22 23:07:38 +05301423 mcspi->dma_channels[i].dma_rx_sync_dev =
1424 dma_res->start;
Charulatha V1a5d8192011-02-02 17:52:14 +05301425 }
Matt Porter74f3aaa2013-06-22 23:07:38 +05301426 sprintf(dma_tx_ch_name, "tx%d", i);
1427 if (!pdev->dev.of_node) {
1428 dma_res =
1429 platform_get_resource_byname(pdev,
1430 IORESOURCE_DMA,
1431 dma_tx_ch_name);
1432 if (!dma_res) {
1433 dev_dbg(&pdev->dev,
1434 "cannot get DMA TX channel\n");
1435 status = -ENODEV;
1436 break;
1437 }
Charulatha V1a5d8192011-02-02 17:52:14 +05301438
Matt Porter74f3aaa2013-06-22 23:07:38 +05301439 mcspi->dma_channels[i].dma_tx_sync_dev =
1440 dma_res->start;
1441 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001442 }
1443
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301444 if (status < 0)
Axel Lina6f936d2014-03-29 21:37:44 +08001445 goto free_master;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301446
Shubhrajyoti D27b52842012-03-26 17:04:22 +05301447 pm_runtime_use_autosuspend(&pdev->dev);
1448 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301449 pm_runtime_enable(&pdev->dev);
1450
Wei Yongjun142e07b2013-04-18 11:14:59 +08001451 status = omap2_mcspi_master_setup(mcspi);
1452 if (status < 0)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301453 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001454
Jingoo Hanb95e02b2013-09-24 13:40:29 +09001455 status = devm_spi_register_master(&pdev->dev, master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001456 if (status < 0)
Shubhrajyoti D37a2d842012-08-02 16:41:25 +05301457 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001458
1459 return status;
1460
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301461disable_pm:
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301462 pm_runtime_disable(&pdev->dev);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301463free_master:
Shubhrajyoti D37a2d842012-08-02 16:41:25 +05301464 spi_master_put(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001465 return status;
1466}
1467
Grant Likelyfd4a3192012-12-07 16:57:14 +00001468static int omap2_mcspi_remove(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001469{
Axel Lina6f936d2014-03-29 21:37:44 +08001470 struct spi_master *master = platform_get_drvdata(pdev);
1471 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001472
Shubhrajyoti Da93a2022012-08-22 11:35:14 +05301473 pm_runtime_put_sync(mcspi->dev);
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301474 pm_runtime_disable(&pdev->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001475
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001476 return 0;
1477}
1478
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001479/* work with hotplug and coldplug */
1480MODULE_ALIAS("platform:omap2_mcspi");
1481
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001482#ifdef CONFIG_SUSPEND
1483/*
1484 * When SPI wake up from off-mode, CS is in activate state. If it was in
1485 * unactive state when driver was suspend, then force it to unactive state at
1486 * wake up.
1487 */
1488static int omap2_mcspi_resume(struct device *dev)
1489{
1490 struct spi_master *master = dev_get_drvdata(dev);
1491 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301492 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1493 struct omap2_mcspi_cs *cs;
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001494
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301495 pm_runtime_get_sync(mcspi->dev);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301496 list_for_each_entry(cs, &ctx->cs, node) {
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001497 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001498 /*
1499 * We need to toggle CS state for OMAP take this
1500 * change in account.
1501 */
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +05301502 cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
Victor Kamensky21b2ce52013-11-16 02:01:16 +02001503 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +05301504 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
Victor Kamensky21b2ce52013-11-16 02:01:16 +02001505 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001506 }
1507 }
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301508 pm_runtime_mark_last_busy(mcspi->dev);
1509 pm_runtime_put_autosuspend(mcspi->dev);
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001510 return 0;
1511}
1512#else
1513#define omap2_mcspi_resume NULL
1514#endif
1515
1516static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1517 .resume = omap2_mcspi_resume,
Govindraj.R1f1a4382011-02-02 17:52:15 +05301518 .runtime_resume = omap_mcspi_runtime_resume,
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001519};
1520
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001521static struct platform_driver omap2_mcspi_driver = {
1522 .driver = {
1523 .name = "omap2_mcspi",
1524 .owner = THIS_MODULE,
Benoit Coussond5a80032012-02-15 18:37:34 +01001525 .pm = &omap2_mcspi_pm_ops,
1526 .of_match_table = omap_mcspi_of_match,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001527 },
Felipe Balbi7d6b6d82012-03-14 11:18:30 +02001528 .probe = omap2_mcspi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001529 .remove = omap2_mcspi_remove,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001530};
1531
Felipe Balbi9fdca9d2012-03-14 11:18:31 +02001532module_platform_driver(omap2_mcspi_driver);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001533MODULE_LICENSE("GPL");