blob: 502db29fd0fe79685e3bec7591e9d9064eb46b51 [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>
Michael Wellingbc7f9bb2015-05-08 13:31:01 -050038#include <linux/gpio.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070039
Arnd Bergmann22037472012-08-24 15:21:06 +020040#include <linux/platform_data/spi-omap2-mcspi.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070041
42#define OMAP2_MCSPI_MAX_FREQ 48000000
Stefan Sørensenfaee9b02014-02-02 16:24:25 +010043#define OMAP2_MCSPI_MAX_DIVIDER 4096
Illia Smyrnovd33f4732013-06-17 16:31:06 +030044#define OMAP2_MCSPI_MAX_FIFODEPTH 64
45#define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
Shubhrajyoti D27b52842012-03-26 17:04:22 +053046#define SPI_AUTOSUSPEND_TIMEOUT 2000
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070047
48#define OMAP2_MCSPI_REVISION 0x00
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070049#define OMAP2_MCSPI_SYSSTATUS 0x14
50#define OMAP2_MCSPI_IRQSTATUS 0x18
51#define OMAP2_MCSPI_IRQENABLE 0x1c
52#define OMAP2_MCSPI_WAKEUPENABLE 0x20
53#define OMAP2_MCSPI_SYST 0x24
54#define OMAP2_MCSPI_MODULCTRL 0x28
Illia Smyrnovd33f4732013-06-17 16:31:06 +030055#define OMAP2_MCSPI_XFERLEVEL 0x7c
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070056
57/* per-channel banks, 0x14 bytes each, first is: */
58#define OMAP2_MCSPI_CHCONF0 0x2c
59#define OMAP2_MCSPI_CHSTAT0 0x30
60#define OMAP2_MCSPI_CHCTRL0 0x34
61#define OMAP2_MCSPI_TX0 0x38
62#define OMAP2_MCSPI_RX0 0x3c
63
64/* per-register bitmasks: */
Illia Smyrnovd33f4732013-06-17 16:31:06 +030065#define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070066
Jouni Hogander7a8fa722009-09-22 16:45:58 -070067#define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
68#define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
69#define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070070
Jouni Hogander7a8fa722009-09-22 16:45:58 -070071#define OMAP2_MCSPI_CHCONF_PHA BIT(0)
72#define OMAP2_MCSPI_CHCONF_POL BIT(1)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070073#define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070074#define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070075#define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070076#define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
77#define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070078#define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070079#define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
80#define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
81#define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
82#define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
83#define OMAP2_MCSPI_CHCONF_IS BIT(18)
84#define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
85#define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
Illia Smyrnovd33f4732013-06-17 16:31:06 +030086#define OMAP2_MCSPI_CHCONF_FFET BIT(27)
87#define OMAP2_MCSPI_CHCONF_FFER BIT(28)
Stefan Sørensenfaee9b02014-02-02 16:24:25 +010088#define OMAP2_MCSPI_CHCONF_CLKG BIT(29)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070089
Jouni Hogander7a8fa722009-09-22 16:45:58 -070090#define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
91#define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
92#define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
Illia Smyrnovd33f4732013-06-17 16:31:06 +030093#define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070094
Jouni Hogander7a8fa722009-09-22 16:45:58 -070095#define OMAP2_MCSPI_CHCTRL_EN BIT(0)
Stefan Sørensenfaee9b02014-02-02 16:24:25 +010096#define OMAP2_MCSPI_CHCTRL_EXTCLK_MASK (0xff << 8)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070097
Jouni Hogander7a8fa722009-09-22 16:45:58 -070098#define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070099
100/* We have 2 DMA channels per CS, one for RX and one for TX */
101struct omap2_mcspi_dma {
Russell King53741ed2012-04-23 13:51:48 +0100102 struct dma_chan *dma_tx;
103 struct dma_chan *dma_rx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700104
105 int dma_tx_sync_dev;
106 int dma_rx_sync_dev;
107
108 struct completion dma_tx_completion;
109 struct completion dma_rx_completion;
Matt Porter74f3aaa2013-06-22 23:07:38 +0530110
111 char dma_rx_ch_name[14];
112 char dma_tx_ch_name[14];
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700113};
114
115/* use PIO for small transfers, avoiding DMA setup/teardown overhead and
116 * cache operations; better heuristics consider wordsize and bitrate.
117 */
Roman Tereshonkov8b66c132010-04-12 09:07:54 +0000118#define DMA_MIN_BYTES 160
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700119
120
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530121/*
122 * Used for context save and restore, structure members to be updated whenever
123 * corresponding registers are modified.
124 */
125struct omap2_mcspi_regs {
126 u32 modulctrl;
127 u32 wakeupenable;
128 struct list_head cs;
129};
130
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700131struct omap2_mcspi {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700132 struct spi_master *master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700133 /* Virtual base address of the controller */
134 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100135 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700136 /* SPI1 has 4 channels, while SPI2 has 2 */
137 struct omap2_mcspi_dma *dma_channels;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530138 struct device *dev;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530139 struct omap2_mcspi_regs ctx;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300140 int fifo_depth;
Daniel Mack0384e902012-10-07 18:19:44 +0200141 unsigned int pin_dir:1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700142};
143
144struct omap2_mcspi_cs {
145 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100146 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700147 int word_len;
Mark A. Greer97ca0d62014-07-01 20:28:32 -0700148 u16 mode;
Tero Kristo89c05372009-09-22 16:46:17 -0700149 struct list_head node;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700150 /* Context save and restore shadow register */
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100151 u32 chconf0, chctrl0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700152};
153
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700154static inline void mcspi_write_reg(struct spi_master *master,
155 int idx, u32 val)
156{
157 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
158
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200159 writel_relaxed(val, mcspi->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700160}
161
162static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
163{
164 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
165
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200166 return readl_relaxed(mcspi->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700167}
168
169static inline void mcspi_write_cs_reg(const struct spi_device *spi,
170 int idx, u32 val)
171{
172 struct omap2_mcspi_cs *cs = spi->controller_state;
173
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200174 writel_relaxed(val, cs->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700175}
176
177static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
178{
179 struct omap2_mcspi_cs *cs = spi->controller_state;
180
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200181 return readl_relaxed(cs->base + idx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700182}
183
Hemanth Va41ae1a2009-09-22 16:46:16 -0700184static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
185{
186 struct omap2_mcspi_cs *cs = spi->controller_state;
187
188 return cs->chconf0;
189}
190
191static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
192{
193 struct omap2_mcspi_cs *cs = spi->controller_state;
194
195 cs->chconf0 = val;
196 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
Roman Tereshonkova330ce22010-03-15 09:06:28 +0000197 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700198}
199
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300200static inline int mcspi_bytes_per_word(int word_len)
201{
202 if (word_len <= 8)
203 return 1;
204 else if (word_len <= 16)
205 return 2;
206 else /* word_len <= 32 */
207 return 4;
208}
209
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700210static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
211 int is_read, int enable)
212{
213 u32 l, rw;
214
Hemanth Va41ae1a2009-09-22 16:46:16 -0700215 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700216
217 if (is_read) /* 1 is read, 0 write */
218 rw = OMAP2_MCSPI_CHCONF_DMAR;
219 else
220 rw = OMAP2_MCSPI_CHCONF_DMAW;
221
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530222 if (enable)
223 l |= rw;
224 else
225 l &= ~rw;
226
Hemanth Va41ae1a2009-09-22 16:46:16 -0700227 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700228}
229
230static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
231{
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100232 struct omap2_mcspi_cs *cs = spi->controller_state;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700233 u32 l;
234
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100235 l = cs->chctrl0;
236 if (enable)
237 l |= OMAP2_MCSPI_CHCTRL_EN;
238 else
239 l &= ~OMAP2_MCSPI_CHCTRL_EN;
240 cs->chctrl0 = l;
241 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000242 /* Flash post-writes */
243 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700244}
245
Michael Wellingddcad7e2015-05-12 12:38:57 -0500246static void omap2_mcspi_set_cs(struct spi_device *spi, bool enable)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700247{
248 u32 l;
249
Michael Welling4373f8b2015-05-23 21:13:43 -0500250 /* The controller handles the inverted chip selects
251 * using the OMAP2_MCSPI_CHCONF_EPOL bit so revert
252 * the inversion from the core spi_set_cs function.
253 */
254 if (spi->mode & SPI_CS_HIGH)
255 enable = !enable;
256
Michael Wellingddcad7e2015-05-12 12:38:57 -0500257 if (spi->controller_state) {
258 l = mcspi_cached_chconf0(spi);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530259
Michael Wellingddcad7e2015-05-12 12:38:57 -0500260 if (enable)
261 l &= ~OMAP2_MCSPI_CHCONF_FORCE;
262 else
263 l |= OMAP2_MCSPI_CHCONF_FORCE;
264
265 mcspi_write_chconf0(spi, l);
266 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700267}
268
269static void omap2_mcspi_set_master_mode(struct spi_master *master)
270{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530271 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
272 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700273 u32 l;
274
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530275 /*
276 * Setup when switching from (reset default) slave mode
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700277 * to single-channel master mode
278 */
279 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530280 l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
281 l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700282 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700283
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530284 ctx->modulctrl = l;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700285}
286
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300287static void omap2_mcspi_set_fifo(const struct spi_device *spi,
288 struct spi_transfer *t, int enable)
289{
290 struct spi_master *master = spi->master;
291 struct omap2_mcspi_cs *cs = spi->controller_state;
292 struct omap2_mcspi *mcspi;
293 unsigned int wcnt;
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300294 int max_fifo_depth, fifo_depth, bytes_per_word;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300295 u32 chconf, xferlevel;
296
297 mcspi = spi_master_get_devdata(master);
298
299 chconf = mcspi_cached_chconf0(spi);
300 if (enable) {
301 bytes_per_word = mcspi_bytes_per_word(cs->word_len);
302 if (t->len % bytes_per_word != 0)
303 goto disable_fifo;
304
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300305 if (t->rx_buf != NULL && t->tx_buf != NULL)
306 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
307 else
308 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
309
310 fifo_depth = gcd(t->len, max_fifo_depth);
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300311 if (fifo_depth < 2 || fifo_depth % bytes_per_word != 0)
312 goto disable_fifo;
313
314 wcnt = t->len / bytes_per_word;
315 if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
316 goto disable_fifo;
317
318 xferlevel = wcnt << 16;
319 if (t->rx_buf != NULL) {
320 chconf |= OMAP2_MCSPI_CHCONF_FFER;
321 xferlevel |= (fifo_depth - 1) << 8;
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300322 }
323 if (t->tx_buf != NULL) {
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300324 chconf |= OMAP2_MCSPI_CHCONF_FFET;
325 xferlevel |= fifo_depth - 1;
326 }
327
328 mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
329 mcspi_write_chconf0(spi, chconf);
330 mcspi->fifo_depth = fifo_depth;
331
332 return;
333 }
334
335disable_fifo:
336 if (t->rx_buf != NULL)
337 chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
Jorge A. Ventura3d0763c2014-08-09 16:06:58 -0500338
339 if (t->tx_buf != NULL)
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300340 chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
341
342 mcspi_write_chconf0(spi, chconf);
343 mcspi->fifo_depth = 0;
344}
345
Hemanth Va41ae1a2009-09-22 16:46:16 -0700346static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
347{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530348 struct spi_master *spi_cntrl = mcspi->master;
349 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
350 struct omap2_mcspi_cs *cs;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700351
352 /* McSPI: context restore */
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530353 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
354 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700355
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530356 list_for_each_entry(cs, &ctx->cs, node)
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200357 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700358}
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700359
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300360static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
361{
362 unsigned long timeout;
363
364 timeout = jiffies + msecs_to_jiffies(1000);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200365 while (!(readl_relaxed(reg) & bit)) {
Sebastian Andrzej Siewiorff23fa32013-03-21 13:22:48 +0100366 if (time_after(jiffies, timeout)) {
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200367 if (!(readl_relaxed(reg) & bit))
Sebastian Andrzej Siewiorff23fa32013-03-21 13:22:48 +0100368 return -ETIMEDOUT;
369 else
370 return 0;
371 }
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300372 cpu_relax();
373 }
374 return 0;
375}
376
Russell King53741ed2012-04-23 13:51:48 +0100377static void omap2_mcspi_rx_callback(void *data)
378{
379 struct spi_device *spi = data;
380 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
381 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
382
Russell King53741ed2012-04-23 13:51:48 +0100383 /* We must disable the DMA RX request */
384 omap2_mcspi_set_dma_req(spi, 1, 0);
Felipe Balbi830379e2012-12-12 10:45:59 +0200385
386 complete(&mcspi_dma->dma_rx_completion);
Russell King53741ed2012-04-23 13:51:48 +0100387}
388
389static void omap2_mcspi_tx_callback(void *data)
390{
391 struct spi_device *spi = data;
392 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
393 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
394
Russell King53741ed2012-04-23 13:51:48 +0100395 /* We must disable the DMA TX request */
396 omap2_mcspi_set_dma_req(spi, 0, 0);
Felipe Balbi830379e2012-12-12 10:45:59 +0200397
398 complete(&mcspi_dma->dma_tx_completion);
Russell King53741ed2012-04-23 13:51:48 +0100399}
400
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530401static void omap2_mcspi_tx_dma(struct spi_device *spi,
402 struct spi_transfer *xfer,
403 struct dma_slave_config cfg)
404{
405 struct omap2_mcspi *mcspi;
406 struct omap2_mcspi_dma *mcspi_dma;
407 unsigned int count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530408
409 mcspi = spi_master_get_devdata(spi->master);
410 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
411 count = xfer->len;
412
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530413 if (mcspi_dma->dma_tx) {
414 struct dma_async_tx_descriptor *tx;
415 struct scatterlist sg;
416
417 dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
418
419 sg_init_table(&sg, 1);
420 sg_dma_address(&sg) = xfer->tx_dma;
421 sg_dma_len(&sg) = xfer->len;
422
423 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
424 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
425 if (tx) {
426 tx->callback = omap2_mcspi_tx_callback;
427 tx->callback_param = spi;
428 dmaengine_submit(tx);
429 } else {
430 /* FIXME: fall back to PIO? */
431 }
432 }
433 dma_async_issue_pending(mcspi_dma->dma_tx);
434 omap2_mcspi_set_dma_req(spi, 0, 1);
435
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530436}
437
438static unsigned
439omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
440 struct dma_slave_config cfg,
441 unsigned es)
442{
443 struct omap2_mcspi *mcspi;
444 struct omap2_mcspi_dma *mcspi_dma;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300445 unsigned int count, dma_count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530446 u32 l;
447 int elements = 0;
448 int word_len, element_count;
449 struct omap2_mcspi_cs *cs = spi->controller_state;
450 mcspi = spi_master_get_devdata(spi->master);
451 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
452 count = xfer->len;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300453 dma_count = xfer->len;
454
455 if (mcspi->fifo_depth == 0)
456 dma_count -= es;
457
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530458 word_len = cs->word_len;
459 l = mcspi_cached_chconf0(spi);
460
461 if (word_len <= 8)
462 element_count = count;
463 else if (word_len <= 16)
464 element_count = count >> 1;
465 else /* word_len <= 32 */
466 element_count = count >> 2;
467
468 if (mcspi_dma->dma_rx) {
469 struct dma_async_tx_descriptor *tx;
470 struct scatterlist sg;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530471
472 dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
473
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300474 if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
475 dma_count -= es;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530476
477 sg_init_table(&sg, 1);
478 sg_dma_address(&sg) = xfer->rx_dma;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300479 sg_dma_len(&sg) = dma_count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530480
481 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
482 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
483 DMA_CTRL_ACK);
484 if (tx) {
485 tx->callback = omap2_mcspi_rx_callback;
486 tx->callback_param = spi;
487 dmaengine_submit(tx);
488 } else {
489 /* FIXME: fall back to PIO? */
490 }
491 }
492
493 dma_async_issue_pending(mcspi_dma->dma_rx);
494 omap2_mcspi_set_dma_req(spi, 1, 1);
495
496 wait_for_completion(&mcspi_dma->dma_rx_completion);
497 dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
498 DMA_FROM_DEVICE);
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300499
500 if (mcspi->fifo_depth > 0)
501 return count;
502
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530503 omap2_mcspi_set_enable(spi, 0);
504
505 elements = element_count - 1;
506
507 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
508 elements--;
509
510 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
511 & OMAP2_MCSPI_CHSTAT_RXS)) {
512 u32 w;
513
514 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
515 if (word_len <= 8)
516 ((u8 *)xfer->rx_buf)[elements++] = w;
517 else if (word_len <= 16)
518 ((u16 *)xfer->rx_buf)[elements++] = w;
519 else /* word_len <= 32 */
520 ((u32 *)xfer->rx_buf)[elements++] = w;
521 } else {
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300522 int bytes_per_word = mcspi_bytes_per_word(word_len);
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300523 dev_err(&spi->dev, "DMA RX penultimate word empty\n");
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300524 count -= (bytes_per_word << 1);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530525 omap2_mcspi_set_enable(spi, 1);
526 return count;
527 }
528 }
529 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
530 & OMAP2_MCSPI_CHSTAT_RXS)) {
531 u32 w;
532
533 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
534 if (word_len <= 8)
535 ((u8 *)xfer->rx_buf)[elements] = w;
536 else if (word_len <= 16)
537 ((u16 *)xfer->rx_buf)[elements] = w;
538 else /* word_len <= 32 */
539 ((u32 *)xfer->rx_buf)[elements] = w;
540 } else {
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300541 dev_err(&spi->dev, "DMA RX last word empty\n");
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300542 count -= mcspi_bytes_per_word(word_len);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530543 }
544 omap2_mcspi_set_enable(spi, 1);
545 return count;
546}
547
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700548static unsigned
549omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
550{
551 struct omap2_mcspi *mcspi;
552 struct omap2_mcspi_cs *cs = spi->controller_state;
553 struct omap2_mcspi_dma *mcspi_dma;
Russell King8c7494a2012-04-23 13:56:25 +0100554 unsigned int count;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000555 u32 l;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530556 u8 *rx;
557 const u8 *tx;
Russell King53741ed2012-04-23 13:51:48 +0100558 struct dma_slave_config cfg;
559 enum dma_slave_buswidth width;
560 unsigned es;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300561 u32 burst;
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530562 void __iomem *chstat_reg;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300563 void __iomem *irqstat_reg;
564 int wait_res;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700565
566 mcspi = spi_master_get_devdata(spi->master);
567 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000568 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700569
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300570
Russell King53741ed2012-04-23 13:51:48 +0100571 if (cs->word_len <= 8) {
572 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
573 es = 1;
574 } else if (cs->word_len <= 16) {
575 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
576 es = 2;
577 } else {
578 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
579 es = 4;
580 }
581
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300582 count = xfer->len;
583 burst = 1;
584
585 if (mcspi->fifo_depth > 0) {
586 if (count > mcspi->fifo_depth)
587 burst = mcspi->fifo_depth / es;
588 else
589 burst = count / es;
590 }
591
Russell King53741ed2012-04-23 13:51:48 +0100592 memset(&cfg, 0, sizeof(cfg));
593 cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
594 cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
595 cfg.src_addr_width = width;
596 cfg.dst_addr_width = width;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300597 cfg.src_maxburst = burst;
598 cfg.dst_maxburst = burst;
Russell King53741ed2012-04-23 13:51:48 +0100599
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700600 rx = xfer->rx_buf;
601 tx = xfer->tx_buf;
602
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530603 if (tx != NULL)
604 omap2_mcspi_tx_dma(spi, xfer, cfg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700605
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530606 if (rx != NULL)
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530607 count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700608
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530609 if (tx != NULL) {
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530610 wait_for_completion(&mcspi_dma->dma_tx_completion);
611 dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len,
612 DMA_TO_DEVICE);
613
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300614 if (mcspi->fifo_depth > 0) {
615 irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
616
617 if (mcspi_wait_for_reg_bit(irqstat_reg,
618 OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
619 dev_err(&spi->dev, "EOW timed out\n");
620
621 mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
622 OMAP2_MCSPI_IRQSTATUS_EOW);
623 }
624
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530625 /* for TX_ONLY mode, be sure all words have shifted out */
626 if (rx == NULL) {
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300627 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
628 if (mcspi->fifo_depth > 0) {
629 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
630 OMAP2_MCSPI_CHSTAT_TXFFE);
631 if (wait_res < 0)
632 dev_err(&spi->dev, "TXFFE timed out\n");
633 } else {
634 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
635 OMAP2_MCSPI_CHSTAT_TXS);
636 if (wait_res < 0)
637 dev_err(&spi->dev, "TXS timed out\n");
638 }
639 if (wait_res >= 0 &&
640 (mcspi_wait_for_reg_bit(chstat_reg,
641 OMAP2_MCSPI_CHSTAT_EOT) < 0))
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530642 dev_err(&spi->dev, "EOT timed out\n");
643 }
644 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700645 return count;
646}
647
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700648static unsigned
649omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
650{
651 struct omap2_mcspi *mcspi;
652 struct omap2_mcspi_cs *cs = spi->controller_state;
653 unsigned int count, c;
654 u32 l;
655 void __iomem *base = cs->base;
656 void __iomem *tx_reg;
657 void __iomem *rx_reg;
658 void __iomem *chstat_reg;
659 int word_len;
660
661 mcspi = spi_master_get_devdata(spi->master);
662 count = xfer->len;
663 c = count;
664 word_len = cs->word_len;
665
Hemanth Va41ae1a2009-09-22 16:46:16 -0700666 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700667
668 /* We store the pre-calculated register addresses on stack to speed
669 * up the transfer loop. */
670 tx_reg = base + OMAP2_MCSPI_TX0;
671 rx_reg = base + OMAP2_MCSPI_RX0;
672 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
673
Michael Jonesadef6582011-02-25 16:55:11 +0100674 if (c < (word_len>>3))
675 return 0;
676
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700677 if (word_len <= 8) {
678 u8 *rx;
679 const u8 *tx;
680
681 rx = xfer->rx_buf;
682 tx = xfer->tx_buf;
683
684 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800685 c -= 1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700686 if (tx != NULL) {
687 if (mcspi_wait_for_reg_bit(chstat_reg,
688 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
689 dev_err(&spi->dev, "TXS timed out\n");
690 goto out;
691 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900692 dev_vdbg(&spi->dev, "write-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700693 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200694 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700695 }
696 if (rx != NULL) {
697 if (mcspi_wait_for_reg_bit(chstat_reg,
698 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
699 dev_err(&spi->dev, "RXS timed out\n");
700 goto out;
701 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000702
703 if (c == 1 && tx == NULL &&
704 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
705 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200706 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900707 dev_vdbg(&spi->dev, "read-%d %02x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000708 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000709 if (mcspi_wait_for_reg_bit(chstat_reg,
710 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
711 dev_err(&spi->dev,
712 "RXS timed out\n");
713 goto out;
714 }
715 c = 0;
716 } else if (c == 0 && tx == NULL) {
717 omap2_mcspi_set_enable(spi, 0);
718 }
719
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200720 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900721 dev_vdbg(&spi->dev, "read-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700722 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700723 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200724 } while (c);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700725 } else if (word_len <= 16) {
726 u16 *rx;
727 const u16 *tx;
728
729 rx = xfer->rx_buf;
730 tx = xfer->tx_buf;
731 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800732 c -= 2;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700733 if (tx != NULL) {
734 if (mcspi_wait_for_reg_bit(chstat_reg,
735 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
736 dev_err(&spi->dev, "TXS timed out\n");
737 goto out;
738 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900739 dev_vdbg(&spi->dev, "write-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700740 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200741 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700742 }
743 if (rx != NULL) {
744 if (mcspi_wait_for_reg_bit(chstat_reg,
745 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
746 dev_err(&spi->dev, "RXS timed out\n");
747 goto out;
748 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000749
750 if (c == 2 && tx == NULL &&
751 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
752 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200753 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900754 dev_vdbg(&spi->dev, "read-%d %04x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000755 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000756 if (mcspi_wait_for_reg_bit(chstat_reg,
757 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
758 dev_err(&spi->dev,
759 "RXS timed out\n");
760 goto out;
761 }
762 c = 0;
763 } else if (c == 0 && tx == NULL) {
764 omap2_mcspi_set_enable(spi, 0);
765 }
766
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200767 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900768 dev_vdbg(&spi->dev, "read-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700769 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700770 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200771 } while (c >= 2);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700772 } else if (word_len <= 32) {
773 u32 *rx;
774 const u32 *tx;
775
776 rx = xfer->rx_buf;
777 tx = xfer->tx_buf;
778 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800779 c -= 4;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700780 if (tx != NULL) {
781 if (mcspi_wait_for_reg_bit(chstat_reg,
782 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
783 dev_err(&spi->dev, "TXS timed out\n");
784 goto out;
785 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900786 dev_vdbg(&spi->dev, "write-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700787 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200788 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700789 }
790 if (rx != NULL) {
791 if (mcspi_wait_for_reg_bit(chstat_reg,
792 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
793 dev_err(&spi->dev, "RXS timed out\n");
794 goto out;
795 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000796
797 if (c == 4 && tx == NULL &&
798 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
799 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200800 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900801 dev_vdbg(&spi->dev, "read-%d %08x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000802 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000803 if (mcspi_wait_for_reg_bit(chstat_reg,
804 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
805 dev_err(&spi->dev,
806 "RXS timed out\n");
807 goto out;
808 }
809 c = 0;
810 } else if (c == 0 && tx == NULL) {
811 omap2_mcspi_set_enable(spi, 0);
812 }
813
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200814 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900815 dev_vdbg(&spi->dev, "read-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700816 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700817 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200818 } while (c >= 4);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700819 }
820
821 /* for TX_ONLY mode, be sure all words have shifted out */
822 if (xfer->rx_buf == NULL) {
823 if (mcspi_wait_for_reg_bit(chstat_reg,
824 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
825 dev_err(&spi->dev, "TXS timed out\n");
826 } else if (mcspi_wait_for_reg_bit(chstat_reg,
827 OMAP2_MCSPI_CHSTAT_EOT) < 0)
828 dev_err(&spi->dev, "EOT timed out\n");
Jason Wange1993ed2010-10-19 18:03:27 +0800829
830 /* disable chan to purge rx datas received in TX_ONLY transfer,
831 * otherwise these rx datas will affect the direct following
832 * RX_ONLY transfer.
833 */
834 omap2_mcspi_set_enable(spi, 0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700835 }
836out:
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000837 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700838 return count - c;
839}
840
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200841static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
842{
843 u32 div;
844
845 for (div = 0; div < 15; div++)
846 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
847 return div;
848
849 return 15;
850}
851
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700852/* called only when no transfer is active to this device */
853static int omap2_mcspi_setup_transfer(struct spi_device *spi,
854 struct spi_transfer *t)
855{
856 struct omap2_mcspi_cs *cs = spi->controller_state;
857 struct omap2_mcspi *mcspi;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700858 struct spi_master *spi_cntrl;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100859 u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700860 u8 word_len = spi->bits_per_word;
Scott Ellis9bd45172010-03-10 14:23:13 -0700861 u32 speed_hz = spi->max_speed_hz;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700862
863 mcspi = spi_master_get_devdata(spi->master);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700864 spi_cntrl = mcspi->master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700865
866 if (t != NULL && t->bits_per_word)
867 word_len = t->bits_per_word;
868
869 cs->word_len = word_len;
870
Scott Ellis9bd45172010-03-10 14:23:13 -0700871 if (t && t->speed_hz)
872 speed_hz = t->speed_hz;
873
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200874 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100875 if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) {
876 clkd = omap2_mcspi_calc_divisor(speed_hz);
877 speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd;
878 clkg = 0;
879 } else {
880 div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz;
881 speed_hz = OMAP2_MCSPI_MAX_FREQ / div;
882 clkd = (div - 1) & 0xf;
883 extclk = (div - 1) >> 4;
884 clkg = OMAP2_MCSPI_CHCONF_CLKG;
885 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700886
Hemanth Va41ae1a2009-09-22 16:46:16 -0700887 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700888
889 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
890 * REVISIT: this controller could support SPI_3WIRE mode.
891 */
Daniel Mack2cd45172012-11-14 11:14:26 +0800892 if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
Daniel Mack0384e902012-10-07 18:19:44 +0200893 l &= ~OMAP2_MCSPI_CHCONF_IS;
894 l &= ~OMAP2_MCSPI_CHCONF_DPE1;
895 l |= OMAP2_MCSPI_CHCONF_DPE0;
896 } else {
897 l |= OMAP2_MCSPI_CHCONF_IS;
898 l |= OMAP2_MCSPI_CHCONF_DPE1;
899 l &= ~OMAP2_MCSPI_CHCONF_DPE0;
900 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700901
902 /* wordlength */
903 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
904 l |= (word_len - 1) << 7;
905
906 /* set chipselect polarity; manage with FORCE */
907 if (!(spi->mode & SPI_CS_HIGH))
908 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
909 else
910 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
911
912 /* set clock divisor */
913 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100914 l |= clkd << 2;
915
916 /* set clock granularity */
917 l &= ~OMAP2_MCSPI_CHCONF_CLKG;
918 l |= clkg;
919 if (clkg) {
920 cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
921 cs->chctrl0 |= extclk << 8;
922 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
923 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700924
925 /* set SPI mode 0..3 */
926 if (spi->mode & SPI_CPOL)
927 l |= OMAP2_MCSPI_CHCONF_POL;
928 else
929 l &= ~OMAP2_MCSPI_CHCONF_POL;
930 if (spi->mode & SPI_CPHA)
931 l |= OMAP2_MCSPI_CHCONF_PHA;
932 else
933 l &= ~OMAP2_MCSPI_CHCONF_PHA;
934
Hemanth Va41ae1a2009-09-22 16:46:16 -0700935 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700936
Mark A. Greer97ca0d62014-07-01 20:28:32 -0700937 cs->mode = spi->mode;
938
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700939 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100940 speed_hz,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700941 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
942 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
943
944 return 0;
945}
946
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700947/*
948 * Note that we currently allow DMA only if we get a channel
949 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
950 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700951static int omap2_mcspi_request_dma(struct spi_device *spi)
952{
953 struct spi_master *master = spi->master;
954 struct omap2_mcspi *mcspi;
955 struct omap2_mcspi_dma *mcspi_dma;
Russell King53741ed2012-04-23 13:51:48 +0100956 dma_cap_mask_t mask;
957 unsigned sig;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700958
959 mcspi = spi_master_get_devdata(master);
960 mcspi_dma = mcspi->dma_channels + spi->chip_select;
961
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700962 init_completion(&mcspi_dma->dma_rx_completion);
963 init_completion(&mcspi_dma->dma_tx_completion);
964
Russell King53741ed2012-04-23 13:51:48 +0100965 dma_cap_zero(mask);
966 dma_cap_set(DMA_SLAVE, mask);
Russell King53741ed2012-04-23 13:51:48 +0100967 sig = mcspi_dma->dma_rx_sync_dev;
Matt Porter74f3aaa2013-06-22 23:07:38 +0530968
969 mcspi_dma->dma_rx =
970 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
971 &sig, &master->dev,
972 mcspi_dma->dma_rx_ch_name);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700973 if (!mcspi_dma->dma_rx)
974 goto no_dma;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700975
Russell King53741ed2012-04-23 13:51:48 +0100976 sig = mcspi_dma->dma_tx_sync_dev;
Matt Porter74f3aaa2013-06-22 23:07:38 +0530977 mcspi_dma->dma_tx =
978 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
979 &sig, &master->dev,
980 mcspi_dma->dma_tx_ch_name);
981
Russell King53741ed2012-04-23 13:51:48 +0100982 if (!mcspi_dma->dma_tx) {
Russell King53741ed2012-04-23 13:51:48 +0100983 dma_release_channel(mcspi_dma->dma_rx);
984 mcspi_dma->dma_rx = NULL;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700985 goto no_dma;
Russell King53741ed2012-04-23 13:51:48 +0100986 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700987
988 return 0;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700989
990no_dma:
991 dev_warn(&spi->dev, "not using DMA for McSPI\n");
992 return -EAGAIN;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700993}
994
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700995static int omap2_mcspi_setup(struct spi_device *spi)
996{
997 int ret;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530998 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
999 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001000 struct omap2_mcspi_dma *mcspi_dma;
1001 struct omap2_mcspi_cs *cs = spi->controller_state;
1002
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001003 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
1004
1005 if (!cs) {
Russell King10aa5a32012-06-18 11:27:04 +01001006 cs = kzalloc(sizeof *cs, GFP_KERNEL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001007 if (!cs)
1008 return -ENOMEM;
1009 cs->base = mcspi->base + spi->chip_select * 0x14;
Russell Kinge5480b732008-09-01 21:51:50 +01001010 cs->phys = mcspi->phys + spi->chip_select * 0x14;
Mark A. Greer97ca0d62014-07-01 20:28:32 -07001011 cs->mode = 0;
Hemanth Va41ae1a2009-09-22 16:46:16 -07001012 cs->chconf0 = 0;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +01001013 cs->chctrl0 = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001014 spi->controller_state = cs;
Tero Kristo89c05372009-09-22 16:46:17 -07001015 /* Link this to context save list */
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301016 list_add_tail(&cs->node, &ctx->cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001017 }
1018
Russell King8c7494a2012-04-23 13:56:25 +01001019 if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001020 ret = omap2_mcspi_request_dma(spi);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001021 if (ret < 0 && ret != -EAGAIN)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001022 return ret;
1023 }
1024
Michael Wellingbc7f9bb2015-05-08 13:31:01 -05001025 if (gpio_is_valid(spi->cs_gpio)) {
1026 if (gpio_request(spi->cs_gpio, dev_name(&spi->dev)) == 0)
1027 gpio_direction_output(spi->cs_gpio,
1028 !(spi->mode & SPI_CS_HIGH));
1029 }
1030
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301031 ret = pm_runtime_get_sync(mcspi->dev);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301032 if (ret < 0)
1033 return ret;
Hemanth Va41ae1a2009-09-22 16:46:16 -07001034
Kyungmin Park86eeb6f2007-10-16 01:27:45 -07001035 ret = omap2_mcspi_setup_transfer(spi, NULL);
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301036 pm_runtime_mark_last_busy(mcspi->dev);
1037 pm_runtime_put_autosuspend(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001038
1039 return ret;
1040}
1041
1042static void omap2_mcspi_cleanup(struct spi_device *spi)
1043{
1044 struct omap2_mcspi *mcspi;
1045 struct omap2_mcspi_dma *mcspi_dma;
Tero Kristo89c05372009-09-22 16:46:17 -07001046 struct omap2_mcspi_cs *cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001047
1048 mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001049
Scott Ellis5e774942010-03-10 14:22:45 -07001050 if (spi->controller_state) {
1051 /* Unlink controller state from context save list */
1052 cs = spi->controller_state;
1053 list_del(&cs->node);
Tero Kristo89c05372009-09-22 16:46:17 -07001054
Russell King10aa5a32012-06-18 11:27:04 +01001055 kfree(cs);
Scott Ellis5e774942010-03-10 14:22:45 -07001056 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001057
Scott Ellis99f1a432010-05-24 14:20:27 +00001058 if (spi->chip_select < spi->master->num_chipselect) {
1059 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
1060
Russell King53741ed2012-04-23 13:51:48 +01001061 if (mcspi_dma->dma_rx) {
1062 dma_release_channel(mcspi_dma->dma_rx);
1063 mcspi_dma->dma_rx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +00001064 }
Russell King53741ed2012-04-23 13:51:48 +01001065 if (mcspi_dma->dma_tx) {
1066 dma_release_channel(mcspi_dma->dma_tx);
1067 mcspi_dma->dma_tx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +00001068 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001069 }
Michael Wellingbc7f9bb2015-05-08 13:31:01 -05001070
1071 if (gpio_is_valid(spi->cs_gpio))
1072 gpio_free(spi->cs_gpio);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001073}
1074
Michael Wellingb28cb942015-05-07 18:36:53 -05001075static int omap2_mcspi_work_one(struct omap2_mcspi *mcspi,
1076 struct spi_device *spi, struct spi_transfer *t)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001077{
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001078
1079 /* We only enable one channel at a time -- the one whose message is
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301080 * -- although this controller would gladly
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001081 * arbitrate among multiple channels. This corresponds to "single
1082 * channel" master mode. As a side effect, we need to manage the
1083 * chipselect with the FORCE bit ... CS != channel enable.
1084 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001085
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001086 struct spi_master *master;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001087 struct omap2_mcspi_dma *mcspi_dma;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301088 struct omap2_mcspi_cs *cs;
1089 struct omap2_mcspi_device_config *cd;
1090 int par_override = 0;
1091 int status = 0;
1092 u32 chconf;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001093
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001094 master = spi->master;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001095 mcspi_dma = mcspi->dma_channels + spi->chip_select;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301096 cs = spi->controller_state;
1097 cd = spi->controller_data;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001098
Mark A. Greer97ca0d62014-07-01 20:28:32 -07001099 /*
1100 * The slave driver could have changed spi->mode in which case
1101 * it will be different from cs->mode (the current hardware setup).
1102 * If so, set par_override (even though its not a parity issue) so
1103 * omap2_mcspi_setup_transfer will be called to configure the hardware
1104 * with the correct mode on the first iteration of the loop below.
1105 */
1106 if (spi->mode != cs->mode)
1107 par_override = 1;
1108
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001109 omap2_mcspi_set_enable(spi, 0);
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001110
Michael Wellingb28cb942015-05-07 18:36:53 -05001111 if (par_override ||
1112 (t->speed_hz != spi->max_speed_hz) ||
1113 (t->bits_per_word != spi->bits_per_word)) {
1114 par_override = 1;
1115 status = omap2_mcspi_setup_transfer(spi, t);
1116 if (status < 0)
1117 goto out;
1118 if (t->speed_hz == spi->max_speed_hz &&
1119 t->bits_per_word == spi->bits_per_word)
1120 par_override = 0;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301121 }
Michael Wellingb28cb942015-05-07 18:36:53 -05001122 if (cd && cd->cs_per_word) {
1123 chconf = mcspi->ctx.modulctrl;
1124 chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
1125 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1126 mcspi->ctx.modulctrl =
1127 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1128 }
1129
Michael Wellingb28cb942015-05-07 18:36:53 -05001130 chconf = mcspi_cached_chconf0(spi);
1131 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
1132 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
1133
1134 if (t->tx_buf == NULL)
1135 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
1136 else if (t->rx_buf == NULL)
1137 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
1138
1139 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
1140 /* Turbo mode is for more than one word */
1141 if (t->len > ((cs->word_len + 7) >> 3))
1142 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
1143 }
1144
1145 mcspi_write_chconf0(spi, chconf);
1146
1147 if (t->len) {
1148 unsigned count;
1149
1150 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1151 (t->len >= DMA_MIN_BYTES))
1152 omap2_mcspi_set_fifo(spi, t, 1);
1153
1154 omap2_mcspi_set_enable(spi, 1);
1155
1156 /* RX_ONLY mode needs dummy data in TX reg */
1157 if (t->tx_buf == NULL)
1158 writel_relaxed(0, cs->base
1159 + OMAP2_MCSPI_TX0);
1160
1161 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1162 (t->len >= DMA_MIN_BYTES))
1163 count = omap2_mcspi_txrx_dma(spi, t);
1164 else
1165 count = omap2_mcspi_txrx_pio(spi, t);
1166
1167 if (count != t->len) {
1168 status = -EIO;
1169 goto out;
1170 }
1171 }
1172
Michael Wellingb28cb942015-05-07 18:36:53 -05001173 omap2_mcspi_set_enable(spi, 0);
1174
1175 if (mcspi->fifo_depth > 0)
1176 omap2_mcspi_set_fifo(spi, t, 0);
1177
1178out:
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301179 /* Restore defaults if they were overriden */
1180 if (par_override) {
1181 par_override = 0;
1182 status = omap2_mcspi_setup_transfer(spi, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001183 }
1184
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001185 if (cd && cd->cs_per_word) {
1186 chconf = mcspi->ctx.modulctrl;
1187 chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
1188 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1189 mcspi->ctx.modulctrl =
1190 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1191 }
1192
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301193 omap2_mcspi_set_enable(spi, 0);
1194
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001195 if (mcspi->fifo_depth > 0 && t)
1196 omap2_mcspi_set_fifo(spi, t, 0);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301197
Michael Wellingb28cb942015-05-07 18:36:53 -05001198 return status;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001199}
1200
Michael Wellingb28cb942015-05-07 18:36:53 -05001201static int omap2_mcspi_transfer_one(struct spi_master *master,
1202 struct spi_device *spi, struct spi_transfer *t)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001203{
1204 struct omap2_mcspi *mcspi;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001205 struct omap2_mcspi_dma *mcspi_dma;
Michael Wellingb28cb942015-05-07 18:36:53 -05001206 const void *tx_buf = t->tx_buf;
1207 void *rx_buf = t->rx_buf;
1208 unsigned len = t->len;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001209
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301210 mcspi = spi_master_get_devdata(master);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001211 mcspi_dma = mcspi->dma_channels + spi->chip_select;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001212
Michael Wellingb28cb942015-05-07 18:36:53 -05001213 if ((len && !(rx_buf || tx_buf))) {
1214 dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
1215 t->speed_hz,
1216 len,
1217 tx_buf ? "tx" : "",
1218 rx_buf ? "rx" : "",
1219 t->bits_per_word);
1220 return -EINVAL;
1221 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001222
Michael Wellingb28cb942015-05-07 18:36:53 -05001223 if (len < DMA_MIN_BYTES)
1224 goto skip_dma_map;
1225
1226 if (mcspi_dma->dma_tx && tx_buf != NULL) {
1227 t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
1228 len, DMA_TO_DEVICE);
1229 if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
1230 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1231 'T', len);
1232 return -EINVAL;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001233 }
Michael Wellingb28cb942015-05-07 18:36:53 -05001234 }
1235 if (mcspi_dma->dma_rx && rx_buf != NULL) {
1236 t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
1237 DMA_FROM_DEVICE);
1238 if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
1239 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1240 'R', len);
1241 if (tx_buf != NULL)
1242 dma_unmap_single(mcspi->dev, t->tx_dma,
1243 len, DMA_TO_DEVICE);
1244 return -EINVAL;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001245 }
1246 }
1247
Michael Wellingb28cb942015-05-07 18:36:53 -05001248skip_dma_map:
1249 return omap2_mcspi_work_one(mcspi, spi, t);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001250}
1251
Grant Likelyfd4a3192012-12-07 16:57:14 +00001252static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001253{
1254 struct spi_master *master = mcspi->master;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301255 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301256 int ret = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001257
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301258 ret = pm_runtime_get_sync(mcspi->dev);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301259 if (ret < 0)
1260 return ret;
Jouni Hoganderddb22192009-07-29 15:02:11 -07001261
Shubhrajyoti D39f80522012-03-29 22:11:07 +05301262 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
Matthias Brugger18dd6192013-01-24 13:28:58 +01001263 OMAP2_MCSPI_WAKEUPENABLE_WKEN);
Shubhrajyoti D39f80522012-03-29 22:11:07 +05301264 ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001265
1266 omap2_mcspi_set_master_mode(master);
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301267 pm_runtime_mark_last_busy(mcspi->dev);
1268 pm_runtime_put_autosuspend(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001269 return 0;
1270}
1271
Govindraj.R1f1a4382011-02-02 17:52:15 +05301272static int omap_mcspi_runtime_resume(struct device *dev)
1273{
1274 struct omap2_mcspi *mcspi;
1275 struct spi_master *master;
1276
1277 master = dev_get_drvdata(dev);
1278 mcspi = spi_master_get_devdata(master);
1279 omap2_mcspi_restore_ctx(mcspi);
1280
1281 return 0;
1282}
1283
Benoit Coussond5a80032012-02-15 18:37:34 +01001284static struct omap2_mcspi_platform_config omap2_pdata = {
1285 .regs_offset = 0,
1286};
1287
1288static struct omap2_mcspi_platform_config omap4_pdata = {
1289 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1290};
1291
1292static const struct of_device_id omap_mcspi_of_match[] = {
1293 {
1294 .compatible = "ti,omap2-mcspi",
1295 .data = &omap2_pdata,
1296 },
1297 {
1298 .compatible = "ti,omap4-mcspi",
1299 .data = &omap4_pdata,
1300 },
1301 { },
1302};
1303MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
Girishccc7bae2008-02-06 01:38:16 -08001304
Grant Likelyfd4a3192012-12-07 16:57:14 +00001305static int omap2_mcspi_probe(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001306{
1307 struct spi_master *master;
Uwe Kleine-König83a01e72012-05-21 21:57:39 +02001308 const struct omap2_mcspi_platform_config *pdata;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001309 struct omap2_mcspi *mcspi;
1310 struct resource *r;
1311 int status = 0, i;
Benoit Coussond5a80032012-02-15 18:37:34 +01001312 u32 regs_offset = 0;
1313 static int bus_num = 1;
1314 struct device_node *node = pdev->dev.of_node;
1315 const struct of_device_id *match;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001316
1317 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1318 if (master == NULL) {
1319 dev_dbg(&pdev->dev, "master allocation failed\n");
1320 return -ENOMEM;
1321 }
1322
David Brownelle7db06b2009-06-17 16:26:04 -07001323 /* the spi->mode bits understood by this driver: */
1324 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
Stephen Warren24778be2013-05-21 20:36:35 -06001325 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001326 master->setup = omap2_mcspi_setup;
Mark Brownf0278a12013-07-28 15:34:37 +01001327 master->auto_runtime_pm = true;
Michael Wellingb28cb942015-05-07 18:36:53 -05001328 master->transfer_one = omap2_mcspi_transfer_one;
Michael Wellingddcad7e2015-05-12 12:38:57 -05001329 master->set_cs = omap2_mcspi_set_cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001330 master->cleanup = omap2_mcspi_cleanup;
Benoit Coussond5a80032012-02-15 18:37:34 +01001331 master->dev.of_node = node;
Axel Linaca09242014-02-18 22:02:47 +08001332 master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ;
1333 master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15;
Benoit Coussond5a80032012-02-15 18:37:34 +01001334
Jingoo Han24b5a822013-05-23 19:20:40 +09001335 platform_set_drvdata(pdev, master);
Daniel Mack0384e902012-10-07 18:19:44 +02001336
1337 mcspi = spi_master_get_devdata(master);
1338 mcspi->master = master;
1339
Benoit Coussond5a80032012-02-15 18:37:34 +01001340 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1341 if (match) {
1342 u32 num_cs = 1; /* default number of chipselect */
1343 pdata = match->data;
1344
1345 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1346 master->num_chipselect = num_cs;
1347 master->bus_num = bus_num++;
Daniel Mack2cd45172012-11-14 11:14:26 +08001348 if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
1349 mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
Benoit Coussond5a80032012-02-15 18:37:34 +01001350 } else {
Jingoo Han8074cf02013-07-30 16:58:59 +09001351 pdata = dev_get_platdata(&pdev->dev);
Benoit Coussond5a80032012-02-15 18:37:34 +01001352 master->num_chipselect = pdata->num_cs;
1353 if (pdev->id != -1)
1354 master->bus_num = pdev->id;
Daniel Mack0384e902012-10-07 18:19:44 +02001355 mcspi->pin_dir = pdata->pin_dir;
Benoit Coussond5a80032012-02-15 18:37:34 +01001356 }
1357 regs_offset = pdata->regs_offset;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001358
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001359 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1360 if (r == NULL) {
1361 status = -ENODEV;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301362 goto free_master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001363 }
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301364
Benoit Coussond5a80032012-02-15 18:37:34 +01001365 r->start += regs_offset;
1366 r->end += regs_offset;
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301367 mcspi->phys = r->start;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001368
Thierry Redingb0ee5602013-01-21 11:09:18 +01001369 mcspi->base = devm_ioremap_resource(&pdev->dev, r);
1370 if (IS_ERR(mcspi->base)) {
1371 status = PTR_ERR(mcspi->base);
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301372 goto free_master;
Russell King55c381e2008-09-04 14:07:22 +01001373 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001374
Govindraj.R1f1a4382011-02-02 17:52:15 +05301375 mcspi->dev = &pdev->dev;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001376
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301377 INIT_LIST_HEAD(&mcspi->ctx.cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001378
Axel Lina6f936d2014-03-29 21:37:44 +08001379 mcspi->dma_channels = devm_kcalloc(&pdev->dev, master->num_chipselect,
1380 sizeof(struct omap2_mcspi_dma),
1381 GFP_KERNEL);
1382 if (mcspi->dma_channels == NULL) {
1383 status = -ENOMEM;
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301384 goto free_master;
Axel Lina6f936d2014-03-29 21:37:44 +08001385 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001386
Charulatha V1a5d8192011-02-02 17:52:14 +05301387 for (i = 0; i < master->num_chipselect; i++) {
Matt Porter74f3aaa2013-06-22 23:07:38 +05301388 char *dma_rx_ch_name = mcspi->dma_channels[i].dma_rx_ch_name;
1389 char *dma_tx_ch_name = mcspi->dma_channels[i].dma_tx_ch_name;
Charulatha V1a5d8192011-02-02 17:52:14 +05301390 struct resource *dma_res;
1391
Matt Porter74f3aaa2013-06-22 23:07:38 +05301392 sprintf(dma_rx_ch_name, "rx%d", i);
1393 if (!pdev->dev.of_node) {
1394 dma_res =
1395 platform_get_resource_byname(pdev,
1396 IORESOURCE_DMA,
1397 dma_rx_ch_name);
1398 if (!dma_res) {
1399 dev_dbg(&pdev->dev,
1400 "cannot get DMA RX channel\n");
1401 status = -ENODEV;
1402 break;
1403 }
Charulatha V1a5d8192011-02-02 17:52:14 +05301404
Matt Porter74f3aaa2013-06-22 23:07:38 +05301405 mcspi->dma_channels[i].dma_rx_sync_dev =
1406 dma_res->start;
Charulatha V1a5d8192011-02-02 17:52:14 +05301407 }
Matt Porter74f3aaa2013-06-22 23:07:38 +05301408 sprintf(dma_tx_ch_name, "tx%d", i);
1409 if (!pdev->dev.of_node) {
1410 dma_res =
1411 platform_get_resource_byname(pdev,
1412 IORESOURCE_DMA,
1413 dma_tx_ch_name);
1414 if (!dma_res) {
1415 dev_dbg(&pdev->dev,
1416 "cannot get DMA TX channel\n");
1417 status = -ENODEV;
1418 break;
1419 }
Charulatha V1a5d8192011-02-02 17:52:14 +05301420
Matt Porter74f3aaa2013-06-22 23:07:38 +05301421 mcspi->dma_channels[i].dma_tx_sync_dev =
1422 dma_res->start;
1423 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001424 }
1425
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301426 if (status < 0)
Axel Lina6f936d2014-03-29 21:37:44 +08001427 goto free_master;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301428
Shubhrajyoti D27b52842012-03-26 17:04:22 +05301429 pm_runtime_use_autosuspend(&pdev->dev);
1430 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301431 pm_runtime_enable(&pdev->dev);
1432
Wei Yongjun142e07b2013-04-18 11:14:59 +08001433 status = omap2_mcspi_master_setup(mcspi);
1434 if (status < 0)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301435 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001436
Jingoo Hanb95e02b2013-09-24 13:40:29 +09001437 status = devm_spi_register_master(&pdev->dev, master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001438 if (status < 0)
Shubhrajyoti D37a2d842012-08-02 16:41:25 +05301439 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001440
1441 return status;
1442
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301443disable_pm:
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301444 pm_runtime_disable(&pdev->dev);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301445free_master:
Shubhrajyoti D37a2d842012-08-02 16:41:25 +05301446 spi_master_put(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001447 return status;
1448}
1449
Grant Likelyfd4a3192012-12-07 16:57:14 +00001450static int omap2_mcspi_remove(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001451{
Axel Lina6f936d2014-03-29 21:37:44 +08001452 struct spi_master *master = platform_get_drvdata(pdev);
1453 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001454
Shubhrajyoti Da93a2022012-08-22 11:35:14 +05301455 pm_runtime_put_sync(mcspi->dev);
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301456 pm_runtime_disable(&pdev->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001457
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001458 return 0;
1459}
1460
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001461/* work with hotplug and coldplug */
1462MODULE_ALIAS("platform:omap2_mcspi");
1463
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001464#ifdef CONFIG_SUSPEND
1465/*
1466 * When SPI wake up from off-mode, CS is in activate state. If it was in
1467 * unactive state when driver was suspend, then force it to unactive state at
1468 * wake up.
1469 */
1470static int omap2_mcspi_resume(struct device *dev)
1471{
1472 struct spi_master *master = dev_get_drvdata(dev);
1473 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301474 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1475 struct omap2_mcspi_cs *cs;
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001476
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301477 pm_runtime_get_sync(mcspi->dev);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301478 list_for_each_entry(cs, &ctx->cs, node) {
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001479 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001480 /*
1481 * We need to toggle CS state for OMAP take this
1482 * change in account.
1483 */
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +05301484 cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
Victor Kamensky21b2ce52013-11-16 02:01:16 +02001485 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +05301486 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
Victor Kamensky21b2ce52013-11-16 02:01:16 +02001487 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001488 }
1489 }
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301490 pm_runtime_mark_last_busy(mcspi->dev);
1491 pm_runtime_put_autosuspend(mcspi->dev);
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001492 return 0;
1493}
1494#else
1495#define omap2_mcspi_resume NULL
1496#endif
1497
1498static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1499 .resume = omap2_mcspi_resume,
Govindraj.R1f1a4382011-02-02 17:52:15 +05301500 .runtime_resume = omap_mcspi_runtime_resume,
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001501};
1502
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001503static struct platform_driver omap2_mcspi_driver = {
1504 .driver = {
1505 .name = "omap2_mcspi",
Benoit Coussond5a80032012-02-15 18:37:34 +01001506 .pm = &omap2_mcspi_pm_ops,
1507 .of_match_table = omap_mcspi_of_match,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001508 },
Felipe Balbi7d6b6d82012-03-14 11:18:30 +02001509 .probe = omap2_mcspi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001510 .remove = omap2_mcspi_remove,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001511};
1512
Felipe Balbi9fdca9d2012-03-14 11:18:31 +02001513module_platform_driver(omap2_mcspi_driver);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001514MODULE_LICENSE("GPL");