blob: 3d09e0b69b73241c58d50e788dbdaf5cd16bf7ea [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{
Sebastian Reichel5f74db12015-07-22 20:46:09 +0200248 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700249 u32 l;
250
Michael Welling4373f8b2015-05-23 21:13:43 -0500251 /* The controller handles the inverted chip selects
252 * using the OMAP2_MCSPI_CHCONF_EPOL bit so revert
253 * the inversion from the core spi_set_cs function.
254 */
255 if (spi->mode & SPI_CS_HIGH)
256 enable = !enable;
257
Michael Wellingddcad7e2015-05-12 12:38:57 -0500258 if (spi->controller_state) {
Sebastian Reichel5f74db12015-07-22 20:46:09 +0200259 int err = pm_runtime_get_sync(mcspi->dev);
260 if (err < 0) {
261 dev_err(mcspi->dev, "failed to get sync: %d\n", err);
262 return;
263 }
264
Michael Wellingddcad7e2015-05-12 12:38:57 -0500265 l = mcspi_cached_chconf0(spi);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530266
Michael Wellingddcad7e2015-05-12 12:38:57 -0500267 if (enable)
268 l &= ~OMAP2_MCSPI_CHCONF_FORCE;
269 else
270 l |= OMAP2_MCSPI_CHCONF_FORCE;
271
272 mcspi_write_chconf0(spi, l);
Sebastian Reichel5f74db12015-07-22 20:46:09 +0200273
274 pm_runtime_mark_last_busy(mcspi->dev);
275 pm_runtime_put_autosuspend(mcspi->dev);
Michael Wellingddcad7e2015-05-12 12:38:57 -0500276 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700277}
278
279static void omap2_mcspi_set_master_mode(struct spi_master *master)
280{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530281 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
282 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700283 u32 l;
284
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530285 /*
286 * Setup when switching from (reset default) slave mode
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700287 * to single-channel master mode
288 */
289 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530290 l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
291 l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700292 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700293
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530294 ctx->modulctrl = l;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700295}
296
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300297static void omap2_mcspi_set_fifo(const struct spi_device *spi,
298 struct spi_transfer *t, int enable)
299{
300 struct spi_master *master = spi->master;
301 struct omap2_mcspi_cs *cs = spi->controller_state;
302 struct omap2_mcspi *mcspi;
303 unsigned int wcnt;
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300304 int max_fifo_depth, fifo_depth, bytes_per_word;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300305 u32 chconf, xferlevel;
306
307 mcspi = spi_master_get_devdata(master);
308
309 chconf = mcspi_cached_chconf0(spi);
310 if (enable) {
311 bytes_per_word = mcspi_bytes_per_word(cs->word_len);
312 if (t->len % bytes_per_word != 0)
313 goto disable_fifo;
314
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300315 if (t->rx_buf != NULL && t->tx_buf != NULL)
316 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH / 2;
317 else
318 max_fifo_depth = OMAP2_MCSPI_MAX_FIFODEPTH;
319
320 fifo_depth = gcd(t->len, max_fifo_depth);
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300321 if (fifo_depth < 2 || fifo_depth % bytes_per_word != 0)
322 goto disable_fifo;
323
324 wcnt = t->len / bytes_per_word;
325 if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
326 goto disable_fifo;
327
328 xferlevel = wcnt << 16;
329 if (t->rx_buf != NULL) {
330 chconf |= OMAP2_MCSPI_CHCONF_FFER;
331 xferlevel |= (fifo_depth - 1) << 8;
Illia Smyrnov5db542e2013-10-09 15:05:08 +0300332 }
333 if (t->tx_buf != NULL) {
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300334 chconf |= OMAP2_MCSPI_CHCONF_FFET;
335 xferlevel |= fifo_depth - 1;
336 }
337
338 mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
339 mcspi_write_chconf0(spi, chconf);
340 mcspi->fifo_depth = fifo_depth;
341
342 return;
343 }
344
345disable_fifo:
346 if (t->rx_buf != NULL)
347 chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
Jorge A. Ventura3d0763c2014-08-09 16:06:58 -0500348
349 if (t->tx_buf != NULL)
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300350 chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
351
352 mcspi_write_chconf0(spi, chconf);
353 mcspi->fifo_depth = 0;
354}
355
Hemanth Va41ae1a2009-09-22 16:46:16 -0700356static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
357{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530358 struct spi_master *spi_cntrl = mcspi->master;
359 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
360 struct omap2_mcspi_cs *cs;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700361
362 /* McSPI: context restore */
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530363 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
364 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700365
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530366 list_for_each_entry(cs, &ctx->cs, node)
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200367 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700368}
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700369
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300370static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
371{
372 unsigned long timeout;
373
374 timeout = jiffies + msecs_to_jiffies(1000);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200375 while (!(readl_relaxed(reg) & bit)) {
Sebastian Andrzej Siewiorff23fa32013-03-21 13:22:48 +0100376 if (time_after(jiffies, timeout)) {
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200377 if (!(readl_relaxed(reg) & bit))
Sebastian Andrzej Siewiorff23fa32013-03-21 13:22:48 +0100378 return -ETIMEDOUT;
379 else
380 return 0;
381 }
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300382 cpu_relax();
383 }
384 return 0;
385}
386
Russell King53741ed2012-04-23 13:51:48 +0100387static void omap2_mcspi_rx_callback(void *data)
388{
389 struct spi_device *spi = data;
390 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
391 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
392
Russell King53741ed2012-04-23 13:51:48 +0100393 /* We must disable the DMA RX request */
394 omap2_mcspi_set_dma_req(spi, 1, 0);
Felipe Balbi830379e2012-12-12 10:45:59 +0200395
396 complete(&mcspi_dma->dma_rx_completion);
Russell King53741ed2012-04-23 13:51:48 +0100397}
398
399static void omap2_mcspi_tx_callback(void *data)
400{
401 struct spi_device *spi = data;
402 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
403 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
404
Russell King53741ed2012-04-23 13:51:48 +0100405 /* We must disable the DMA TX request */
406 omap2_mcspi_set_dma_req(spi, 0, 0);
Felipe Balbi830379e2012-12-12 10:45:59 +0200407
408 complete(&mcspi_dma->dma_tx_completion);
Russell King53741ed2012-04-23 13:51:48 +0100409}
410
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530411static void omap2_mcspi_tx_dma(struct spi_device *spi,
412 struct spi_transfer *xfer,
413 struct dma_slave_config cfg)
414{
415 struct omap2_mcspi *mcspi;
416 struct omap2_mcspi_dma *mcspi_dma;
417 unsigned int count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530418
419 mcspi = spi_master_get_devdata(spi->master);
420 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
421 count = xfer->len;
422
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530423 if (mcspi_dma->dma_tx) {
424 struct dma_async_tx_descriptor *tx;
425 struct scatterlist sg;
426
427 dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
428
429 sg_init_table(&sg, 1);
430 sg_dma_address(&sg) = xfer->tx_dma;
431 sg_dma_len(&sg) = xfer->len;
432
433 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
434 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
435 if (tx) {
436 tx->callback = omap2_mcspi_tx_callback;
437 tx->callback_param = spi;
438 dmaengine_submit(tx);
439 } else {
440 /* FIXME: fall back to PIO? */
441 }
442 }
443 dma_async_issue_pending(mcspi_dma->dma_tx);
444 omap2_mcspi_set_dma_req(spi, 0, 1);
445
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530446}
447
448static unsigned
449omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
450 struct dma_slave_config cfg,
451 unsigned es)
452{
453 struct omap2_mcspi *mcspi;
454 struct omap2_mcspi_dma *mcspi_dma;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300455 unsigned int count, dma_count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530456 u32 l;
457 int elements = 0;
458 int word_len, element_count;
459 struct omap2_mcspi_cs *cs = spi->controller_state;
460 mcspi = spi_master_get_devdata(spi->master);
461 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
462 count = xfer->len;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300463 dma_count = xfer->len;
464
465 if (mcspi->fifo_depth == 0)
466 dma_count -= es;
467
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530468 word_len = cs->word_len;
469 l = mcspi_cached_chconf0(spi);
470
471 if (word_len <= 8)
472 element_count = count;
473 else if (word_len <= 16)
474 element_count = count >> 1;
475 else /* word_len <= 32 */
476 element_count = count >> 2;
477
478 if (mcspi_dma->dma_rx) {
479 struct dma_async_tx_descriptor *tx;
480 struct scatterlist sg;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530481
482 dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
483
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300484 if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
485 dma_count -= es;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530486
487 sg_init_table(&sg, 1);
488 sg_dma_address(&sg) = xfer->rx_dma;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300489 sg_dma_len(&sg) = dma_count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530490
491 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
492 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
493 DMA_CTRL_ACK);
494 if (tx) {
495 tx->callback = omap2_mcspi_rx_callback;
496 tx->callback_param = spi;
497 dmaengine_submit(tx);
498 } else {
499 /* FIXME: fall back to PIO? */
500 }
501 }
502
503 dma_async_issue_pending(mcspi_dma->dma_rx);
504 omap2_mcspi_set_dma_req(spi, 1, 1);
505
506 wait_for_completion(&mcspi_dma->dma_rx_completion);
507 dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
508 DMA_FROM_DEVICE);
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300509
510 if (mcspi->fifo_depth > 0)
511 return count;
512
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530513 omap2_mcspi_set_enable(spi, 0);
514
515 elements = element_count - 1;
516
517 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
518 elements--;
519
520 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
521 & OMAP2_MCSPI_CHSTAT_RXS)) {
522 u32 w;
523
524 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
525 if (word_len <= 8)
526 ((u8 *)xfer->rx_buf)[elements++] = w;
527 else if (word_len <= 16)
528 ((u16 *)xfer->rx_buf)[elements++] = w;
529 else /* word_len <= 32 */
530 ((u32 *)xfer->rx_buf)[elements++] = w;
531 } else {
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300532 int bytes_per_word = mcspi_bytes_per_word(word_len);
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300533 dev_err(&spi->dev, "DMA RX penultimate word empty\n");
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300534 count -= (bytes_per_word << 1);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530535 omap2_mcspi_set_enable(spi, 1);
536 return count;
537 }
538 }
539 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
540 & OMAP2_MCSPI_CHSTAT_RXS)) {
541 u32 w;
542
543 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
544 if (word_len <= 8)
545 ((u8 *)xfer->rx_buf)[elements] = w;
546 else if (word_len <= 16)
547 ((u16 *)xfer->rx_buf)[elements] = w;
548 else /* word_len <= 32 */
549 ((u32 *)xfer->rx_buf)[elements] = w;
550 } else {
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300551 dev_err(&spi->dev, "DMA RX last word empty\n");
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300552 count -= mcspi_bytes_per_word(word_len);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530553 }
554 omap2_mcspi_set_enable(spi, 1);
555 return count;
556}
557
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700558static unsigned
559omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
560{
561 struct omap2_mcspi *mcspi;
562 struct omap2_mcspi_cs *cs = spi->controller_state;
563 struct omap2_mcspi_dma *mcspi_dma;
Russell King8c7494a2012-04-23 13:56:25 +0100564 unsigned int count;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000565 u32 l;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530566 u8 *rx;
567 const u8 *tx;
Russell King53741ed2012-04-23 13:51:48 +0100568 struct dma_slave_config cfg;
569 enum dma_slave_buswidth width;
570 unsigned es;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300571 u32 burst;
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530572 void __iomem *chstat_reg;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300573 void __iomem *irqstat_reg;
574 int wait_res;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700575
576 mcspi = spi_master_get_devdata(spi->master);
577 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000578 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700579
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300580
Russell King53741ed2012-04-23 13:51:48 +0100581 if (cs->word_len <= 8) {
582 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
583 es = 1;
584 } else if (cs->word_len <= 16) {
585 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
586 es = 2;
587 } else {
588 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
589 es = 4;
590 }
591
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300592 count = xfer->len;
593 burst = 1;
594
595 if (mcspi->fifo_depth > 0) {
596 if (count > mcspi->fifo_depth)
597 burst = mcspi->fifo_depth / es;
598 else
599 burst = count / es;
600 }
601
Russell King53741ed2012-04-23 13:51:48 +0100602 memset(&cfg, 0, sizeof(cfg));
603 cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
604 cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
605 cfg.src_addr_width = width;
606 cfg.dst_addr_width = width;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300607 cfg.src_maxburst = burst;
608 cfg.dst_maxburst = burst;
Russell King53741ed2012-04-23 13:51:48 +0100609
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700610 rx = xfer->rx_buf;
611 tx = xfer->tx_buf;
612
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530613 if (tx != NULL)
614 omap2_mcspi_tx_dma(spi, xfer, cfg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700615
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530616 if (rx != NULL)
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530617 count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700618
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530619 if (tx != NULL) {
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530620 wait_for_completion(&mcspi_dma->dma_tx_completion);
621 dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len,
622 DMA_TO_DEVICE);
623
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300624 if (mcspi->fifo_depth > 0) {
625 irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
626
627 if (mcspi_wait_for_reg_bit(irqstat_reg,
628 OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
629 dev_err(&spi->dev, "EOW timed out\n");
630
631 mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
632 OMAP2_MCSPI_IRQSTATUS_EOW);
633 }
634
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530635 /* for TX_ONLY mode, be sure all words have shifted out */
636 if (rx == NULL) {
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300637 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
638 if (mcspi->fifo_depth > 0) {
639 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
640 OMAP2_MCSPI_CHSTAT_TXFFE);
641 if (wait_res < 0)
642 dev_err(&spi->dev, "TXFFE timed out\n");
643 } else {
644 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
645 OMAP2_MCSPI_CHSTAT_TXS);
646 if (wait_res < 0)
647 dev_err(&spi->dev, "TXS timed out\n");
648 }
649 if (wait_res >= 0 &&
650 (mcspi_wait_for_reg_bit(chstat_reg,
651 OMAP2_MCSPI_CHSTAT_EOT) < 0))
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530652 dev_err(&spi->dev, "EOT timed out\n");
653 }
654 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700655 return count;
656}
657
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700658static unsigned
659omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
660{
661 struct omap2_mcspi *mcspi;
662 struct omap2_mcspi_cs *cs = spi->controller_state;
663 unsigned int count, c;
664 u32 l;
665 void __iomem *base = cs->base;
666 void __iomem *tx_reg;
667 void __iomem *rx_reg;
668 void __iomem *chstat_reg;
669 int word_len;
670
671 mcspi = spi_master_get_devdata(spi->master);
672 count = xfer->len;
673 c = count;
674 word_len = cs->word_len;
675
Hemanth Va41ae1a2009-09-22 16:46:16 -0700676 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700677
678 /* We store the pre-calculated register addresses on stack to speed
679 * up the transfer loop. */
680 tx_reg = base + OMAP2_MCSPI_TX0;
681 rx_reg = base + OMAP2_MCSPI_RX0;
682 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
683
Michael Jonesadef6582011-02-25 16:55:11 +0100684 if (c < (word_len>>3))
685 return 0;
686
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700687 if (word_len <= 8) {
688 u8 *rx;
689 const u8 *tx;
690
691 rx = xfer->rx_buf;
692 tx = xfer->tx_buf;
693
694 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800695 c -= 1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700696 if (tx != NULL) {
697 if (mcspi_wait_for_reg_bit(chstat_reg,
698 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
699 dev_err(&spi->dev, "TXS timed out\n");
700 goto out;
701 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900702 dev_vdbg(&spi->dev, "write-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700703 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200704 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700705 }
706 if (rx != NULL) {
707 if (mcspi_wait_for_reg_bit(chstat_reg,
708 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
709 dev_err(&spi->dev, "RXS timed out\n");
710 goto out;
711 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000712
713 if (c == 1 && tx == NULL &&
714 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
715 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200716 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900717 dev_vdbg(&spi->dev, "read-%d %02x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000718 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000719 if (mcspi_wait_for_reg_bit(chstat_reg,
720 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
721 dev_err(&spi->dev,
722 "RXS timed out\n");
723 goto out;
724 }
725 c = 0;
726 } else if (c == 0 && tx == NULL) {
727 omap2_mcspi_set_enable(spi, 0);
728 }
729
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200730 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900731 dev_vdbg(&spi->dev, "read-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700732 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700733 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200734 } while (c);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700735 } else if (word_len <= 16) {
736 u16 *rx;
737 const u16 *tx;
738
739 rx = xfer->rx_buf;
740 tx = xfer->tx_buf;
741 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800742 c -= 2;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700743 if (tx != NULL) {
744 if (mcspi_wait_for_reg_bit(chstat_reg,
745 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
746 dev_err(&spi->dev, "TXS timed out\n");
747 goto out;
748 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900749 dev_vdbg(&spi->dev, "write-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700750 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200751 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700752 }
753 if (rx != NULL) {
754 if (mcspi_wait_for_reg_bit(chstat_reg,
755 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
756 dev_err(&spi->dev, "RXS timed out\n");
757 goto out;
758 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000759
760 if (c == 2 && tx == NULL &&
761 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
762 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200763 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900764 dev_vdbg(&spi->dev, "read-%d %04x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000765 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000766 if (mcspi_wait_for_reg_bit(chstat_reg,
767 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
768 dev_err(&spi->dev,
769 "RXS timed out\n");
770 goto out;
771 }
772 c = 0;
773 } else if (c == 0 && tx == NULL) {
774 omap2_mcspi_set_enable(spi, 0);
775 }
776
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200777 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900778 dev_vdbg(&spi->dev, "read-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700779 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700780 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200781 } while (c >= 2);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700782 } else if (word_len <= 32) {
783 u32 *rx;
784 const u32 *tx;
785
786 rx = xfer->rx_buf;
787 tx = xfer->tx_buf;
788 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800789 c -= 4;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700790 if (tx != NULL) {
791 if (mcspi_wait_for_reg_bit(chstat_reg,
792 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
793 dev_err(&spi->dev, "TXS timed out\n");
794 goto out;
795 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900796 dev_vdbg(&spi->dev, "write-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700797 word_len, *tx);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200798 writel_relaxed(*tx++, tx_reg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700799 }
800 if (rx != NULL) {
801 if (mcspi_wait_for_reg_bit(chstat_reg,
802 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
803 dev_err(&spi->dev, "RXS timed out\n");
804 goto out;
805 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000806
807 if (c == 4 && tx == NULL &&
808 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
809 omap2_mcspi_set_enable(spi, 0);
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200810 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900811 dev_vdbg(&spi->dev, "read-%d %08x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000812 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000813 if (mcspi_wait_for_reg_bit(chstat_reg,
814 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
815 dev_err(&spi->dev,
816 "RXS timed out\n");
817 goto out;
818 }
819 c = 0;
820 } else if (c == 0 && tx == NULL) {
821 omap2_mcspi_set_enable(spi, 0);
822 }
823
Victor Kamensky21b2ce52013-11-16 02:01:16 +0200824 *rx++ = readl_relaxed(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900825 dev_vdbg(&spi->dev, "read-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700826 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700827 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200828 } while (c >= 4);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700829 }
830
831 /* for TX_ONLY mode, be sure all words have shifted out */
832 if (xfer->rx_buf == NULL) {
833 if (mcspi_wait_for_reg_bit(chstat_reg,
834 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
835 dev_err(&spi->dev, "TXS timed out\n");
836 } else if (mcspi_wait_for_reg_bit(chstat_reg,
837 OMAP2_MCSPI_CHSTAT_EOT) < 0)
838 dev_err(&spi->dev, "EOT timed out\n");
Jason Wange1993ed2010-10-19 18:03:27 +0800839
840 /* disable chan to purge rx datas received in TX_ONLY transfer,
841 * otherwise these rx datas will affect the direct following
842 * RX_ONLY transfer.
843 */
844 omap2_mcspi_set_enable(spi, 0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700845 }
846out:
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000847 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700848 return count - c;
849}
850
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200851static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
852{
853 u32 div;
854
855 for (div = 0; div < 15; div++)
856 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
857 return div;
858
859 return 15;
860}
861
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700862/* called only when no transfer is active to this device */
863static int omap2_mcspi_setup_transfer(struct spi_device *spi,
864 struct spi_transfer *t)
865{
866 struct omap2_mcspi_cs *cs = spi->controller_state;
867 struct omap2_mcspi *mcspi;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700868 struct spi_master *spi_cntrl;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100869 u32 l = 0, clkd = 0, div, extclk = 0, clkg = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700870 u8 word_len = spi->bits_per_word;
Scott Ellis9bd45172010-03-10 14:23:13 -0700871 u32 speed_hz = spi->max_speed_hz;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700872
873 mcspi = spi_master_get_devdata(spi->master);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700874 spi_cntrl = mcspi->master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700875
876 if (t != NULL && t->bits_per_word)
877 word_len = t->bits_per_word;
878
879 cs->word_len = word_len;
880
Scott Ellis9bd45172010-03-10 14:23:13 -0700881 if (t && t->speed_hz)
882 speed_hz = t->speed_hz;
883
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200884 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100885 if (speed_hz < (OMAP2_MCSPI_MAX_FREQ / OMAP2_MCSPI_MAX_DIVIDER)) {
886 clkd = omap2_mcspi_calc_divisor(speed_hz);
887 speed_hz = OMAP2_MCSPI_MAX_FREQ >> clkd;
888 clkg = 0;
889 } else {
890 div = (OMAP2_MCSPI_MAX_FREQ + speed_hz - 1) / speed_hz;
891 speed_hz = OMAP2_MCSPI_MAX_FREQ / div;
892 clkd = (div - 1) & 0xf;
893 extclk = (div - 1) >> 4;
894 clkg = OMAP2_MCSPI_CHCONF_CLKG;
895 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700896
Hemanth Va41ae1a2009-09-22 16:46:16 -0700897 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700898
899 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
900 * REVISIT: this controller could support SPI_3WIRE mode.
901 */
Daniel Mack2cd45172012-11-14 11:14:26 +0800902 if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
Daniel Mack0384e902012-10-07 18:19:44 +0200903 l &= ~OMAP2_MCSPI_CHCONF_IS;
904 l &= ~OMAP2_MCSPI_CHCONF_DPE1;
905 l |= OMAP2_MCSPI_CHCONF_DPE0;
906 } else {
907 l |= OMAP2_MCSPI_CHCONF_IS;
908 l |= OMAP2_MCSPI_CHCONF_DPE1;
909 l &= ~OMAP2_MCSPI_CHCONF_DPE0;
910 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700911
912 /* wordlength */
913 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
914 l |= (word_len - 1) << 7;
915
916 /* set chipselect polarity; manage with FORCE */
917 if (!(spi->mode & SPI_CS_HIGH))
918 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
919 else
920 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
921
922 /* set clock divisor */
923 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100924 l |= clkd << 2;
925
926 /* set clock granularity */
927 l &= ~OMAP2_MCSPI_CHCONF_CLKG;
928 l |= clkg;
929 if (clkg) {
930 cs->chctrl0 &= ~OMAP2_MCSPI_CHCTRL_EXTCLK_MASK;
931 cs->chctrl0 |= extclk << 8;
932 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, cs->chctrl0);
933 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700934
935 /* set SPI mode 0..3 */
936 if (spi->mode & SPI_CPOL)
937 l |= OMAP2_MCSPI_CHCONF_POL;
938 else
939 l &= ~OMAP2_MCSPI_CHCONF_POL;
940 if (spi->mode & SPI_CPHA)
941 l |= OMAP2_MCSPI_CHCONF_PHA;
942 else
943 l &= ~OMAP2_MCSPI_CHCONF_PHA;
944
Hemanth Va41ae1a2009-09-22 16:46:16 -0700945 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700946
Mark A. Greer97ca0d62014-07-01 20:28:32 -0700947 cs->mode = spi->mode;
948
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700949 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
Stefan Sørensenfaee9b02014-02-02 16:24:25 +0100950 speed_hz,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700951 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
952 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
953
954 return 0;
955}
956
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700957/*
958 * Note that we currently allow DMA only if we get a channel
959 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
960 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700961static int omap2_mcspi_request_dma(struct spi_device *spi)
962{
963 struct spi_master *master = spi->master;
964 struct omap2_mcspi *mcspi;
965 struct omap2_mcspi_dma *mcspi_dma;
Russell King53741ed2012-04-23 13:51:48 +0100966 dma_cap_mask_t mask;
967 unsigned sig;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700968
969 mcspi = spi_master_get_devdata(master);
970 mcspi_dma = mcspi->dma_channels + spi->chip_select;
971
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700972 init_completion(&mcspi_dma->dma_rx_completion);
973 init_completion(&mcspi_dma->dma_tx_completion);
974
Russell King53741ed2012-04-23 13:51:48 +0100975 dma_cap_zero(mask);
976 dma_cap_set(DMA_SLAVE, mask);
Russell King53741ed2012-04-23 13:51:48 +0100977 sig = mcspi_dma->dma_rx_sync_dev;
Matt Porter74f3aaa2013-06-22 23:07:38 +0530978
979 mcspi_dma->dma_rx =
980 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
981 &sig, &master->dev,
982 mcspi_dma->dma_rx_ch_name);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700983 if (!mcspi_dma->dma_rx)
984 goto no_dma;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700985
Russell King53741ed2012-04-23 13:51:48 +0100986 sig = mcspi_dma->dma_tx_sync_dev;
Matt Porter74f3aaa2013-06-22 23:07:38 +0530987 mcspi_dma->dma_tx =
988 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
989 &sig, &master->dev,
990 mcspi_dma->dma_tx_ch_name);
991
Russell King53741ed2012-04-23 13:51:48 +0100992 if (!mcspi_dma->dma_tx) {
Russell King53741ed2012-04-23 13:51:48 +0100993 dma_release_channel(mcspi_dma->dma_rx);
994 mcspi_dma->dma_rx = NULL;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700995 goto no_dma;
Russell King53741ed2012-04-23 13:51:48 +0100996 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700997
998 return 0;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700999
1000no_dma:
1001 dev_warn(&spi->dev, "not using DMA for McSPI\n");
1002 return -EAGAIN;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001003}
1004
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001005static int omap2_mcspi_setup(struct spi_device *spi)
1006{
1007 int ret;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301008 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
1009 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001010 struct omap2_mcspi_dma *mcspi_dma;
1011 struct omap2_mcspi_cs *cs = spi->controller_state;
1012
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001013 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
1014
1015 if (!cs) {
Russell King10aa5a32012-06-18 11:27:04 +01001016 cs = kzalloc(sizeof *cs, GFP_KERNEL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001017 if (!cs)
1018 return -ENOMEM;
1019 cs->base = mcspi->base + spi->chip_select * 0x14;
Russell Kinge5480b732008-09-01 21:51:50 +01001020 cs->phys = mcspi->phys + spi->chip_select * 0x14;
Mark A. Greer97ca0d62014-07-01 20:28:32 -07001021 cs->mode = 0;
Hemanth Va41ae1a2009-09-22 16:46:16 -07001022 cs->chconf0 = 0;
Stefan Sørensenfaee9b02014-02-02 16:24:25 +01001023 cs->chctrl0 = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001024 spi->controller_state = cs;
Tero Kristo89c05372009-09-22 16:46:17 -07001025 /* Link this to context save list */
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301026 list_add_tail(&cs->node, &ctx->cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001027 }
1028
Russell King8c7494a2012-04-23 13:56:25 +01001029 if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001030 ret = omap2_mcspi_request_dma(spi);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001031 if (ret < 0 && ret != -EAGAIN)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001032 return ret;
1033 }
1034
Michael Wellingbc7f9bb2015-05-08 13:31:01 -05001035 if (gpio_is_valid(spi->cs_gpio)) {
Michael Wellingc4339ac2015-05-23 21:13:45 -05001036 ret = gpio_request(spi->cs_gpio, dev_name(&spi->dev));
1037 if (ret) {
1038 dev_err(&spi->dev, "failed to request gpio\n");
1039 return ret;
1040 }
1041 gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
Michael Wellingbc7f9bb2015-05-08 13:31:01 -05001042 }
1043
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301044 ret = pm_runtime_get_sync(mcspi->dev);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301045 if (ret < 0)
1046 return ret;
Hemanth Va41ae1a2009-09-22 16:46:16 -07001047
Kyungmin Park86eeb6f2007-10-16 01:27:45 -07001048 ret = omap2_mcspi_setup_transfer(spi, NULL);
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301049 pm_runtime_mark_last_busy(mcspi->dev);
1050 pm_runtime_put_autosuspend(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001051
1052 return ret;
1053}
1054
1055static void omap2_mcspi_cleanup(struct spi_device *spi)
1056{
1057 struct omap2_mcspi *mcspi;
1058 struct omap2_mcspi_dma *mcspi_dma;
Tero Kristo89c05372009-09-22 16:46:17 -07001059 struct omap2_mcspi_cs *cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001060
1061 mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001062
Scott Ellis5e774942010-03-10 14:22:45 -07001063 if (spi->controller_state) {
1064 /* Unlink controller state from context save list */
1065 cs = spi->controller_state;
1066 list_del(&cs->node);
Tero Kristo89c05372009-09-22 16:46:17 -07001067
Russell King10aa5a32012-06-18 11:27:04 +01001068 kfree(cs);
Scott Ellis5e774942010-03-10 14:22:45 -07001069 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001070
Scott Ellis99f1a432010-05-24 14:20:27 +00001071 if (spi->chip_select < spi->master->num_chipselect) {
1072 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
1073
Russell King53741ed2012-04-23 13:51:48 +01001074 if (mcspi_dma->dma_rx) {
1075 dma_release_channel(mcspi_dma->dma_rx);
1076 mcspi_dma->dma_rx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +00001077 }
Russell King53741ed2012-04-23 13:51:48 +01001078 if (mcspi_dma->dma_tx) {
1079 dma_release_channel(mcspi_dma->dma_tx);
1080 mcspi_dma->dma_tx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +00001081 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001082 }
Michael Wellingbc7f9bb2015-05-08 13:31:01 -05001083
1084 if (gpio_is_valid(spi->cs_gpio))
1085 gpio_free(spi->cs_gpio);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001086}
1087
Michael Wellingb28cb942015-05-07 18:36:53 -05001088static int omap2_mcspi_work_one(struct omap2_mcspi *mcspi,
1089 struct spi_device *spi, struct spi_transfer *t)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001090{
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001091
1092 /* We only enable one channel at a time -- the one whose message is
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301093 * -- although this controller would gladly
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001094 * arbitrate among multiple channels. This corresponds to "single
1095 * channel" master mode. As a side effect, we need to manage the
1096 * chipselect with the FORCE bit ... CS != channel enable.
1097 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001098
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001099 struct spi_master *master;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001100 struct omap2_mcspi_dma *mcspi_dma;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301101 struct omap2_mcspi_cs *cs;
1102 struct omap2_mcspi_device_config *cd;
1103 int par_override = 0;
1104 int status = 0;
1105 u32 chconf;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001106
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001107 master = spi->master;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001108 mcspi_dma = mcspi->dma_channels + spi->chip_select;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301109 cs = spi->controller_state;
1110 cd = spi->controller_data;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001111
Mark A. Greer97ca0d62014-07-01 20:28:32 -07001112 /*
1113 * The slave driver could have changed spi->mode in which case
1114 * it will be different from cs->mode (the current hardware setup).
1115 * If so, set par_override (even though its not a parity issue) so
1116 * omap2_mcspi_setup_transfer will be called to configure the hardware
1117 * with the correct mode on the first iteration of the loop below.
1118 */
1119 if (spi->mode != cs->mode)
1120 par_override = 1;
1121
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001122 omap2_mcspi_set_enable(spi, 0);
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001123
Michael Wellinga06b4302015-05-23 21:13:44 -05001124 if (gpio_is_valid(spi->cs_gpio))
1125 omap2_mcspi_set_cs(spi, spi->mode & SPI_CS_HIGH);
1126
Michael Wellingb28cb942015-05-07 18:36:53 -05001127 if (par_override ||
1128 (t->speed_hz != spi->max_speed_hz) ||
1129 (t->bits_per_word != spi->bits_per_word)) {
1130 par_override = 1;
1131 status = omap2_mcspi_setup_transfer(spi, t);
1132 if (status < 0)
1133 goto out;
1134 if (t->speed_hz == spi->max_speed_hz &&
1135 t->bits_per_word == spi->bits_per_word)
1136 par_override = 0;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301137 }
Michael Wellingb28cb942015-05-07 18:36:53 -05001138 if (cd && cd->cs_per_word) {
1139 chconf = mcspi->ctx.modulctrl;
1140 chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
1141 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1142 mcspi->ctx.modulctrl =
1143 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1144 }
1145
Michael Wellingb28cb942015-05-07 18:36:53 -05001146 chconf = mcspi_cached_chconf0(spi);
1147 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
1148 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
1149
1150 if (t->tx_buf == NULL)
1151 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
1152 else if (t->rx_buf == NULL)
1153 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
1154
1155 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
1156 /* Turbo mode is for more than one word */
1157 if (t->len > ((cs->word_len + 7) >> 3))
1158 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
1159 }
1160
1161 mcspi_write_chconf0(spi, chconf);
1162
1163 if (t->len) {
1164 unsigned count;
1165
1166 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1167 (t->len >= DMA_MIN_BYTES))
1168 omap2_mcspi_set_fifo(spi, t, 1);
1169
1170 omap2_mcspi_set_enable(spi, 1);
1171
1172 /* RX_ONLY mode needs dummy data in TX reg */
1173 if (t->tx_buf == NULL)
1174 writel_relaxed(0, cs->base
1175 + OMAP2_MCSPI_TX0);
1176
1177 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1178 (t->len >= DMA_MIN_BYTES))
1179 count = omap2_mcspi_txrx_dma(spi, t);
1180 else
1181 count = omap2_mcspi_txrx_pio(spi, t);
1182
1183 if (count != t->len) {
1184 status = -EIO;
1185 goto out;
1186 }
1187 }
1188
Michael Wellingb28cb942015-05-07 18:36:53 -05001189 omap2_mcspi_set_enable(spi, 0);
1190
1191 if (mcspi->fifo_depth > 0)
1192 omap2_mcspi_set_fifo(spi, t, 0);
1193
1194out:
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301195 /* Restore defaults if they were overriden */
1196 if (par_override) {
1197 par_override = 0;
1198 status = omap2_mcspi_setup_transfer(spi, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001199 }
1200
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001201 if (cd && cd->cs_per_word) {
1202 chconf = mcspi->ctx.modulctrl;
1203 chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
1204 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1205 mcspi->ctx.modulctrl =
1206 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1207 }
1208
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301209 omap2_mcspi_set_enable(spi, 0);
1210
Michael Wellinga06b4302015-05-23 21:13:44 -05001211 if (gpio_is_valid(spi->cs_gpio))
1212 omap2_mcspi_set_cs(spi, !(spi->mode & SPI_CS_HIGH));
1213
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001214 if (mcspi->fifo_depth > 0 && t)
1215 omap2_mcspi_set_fifo(spi, t, 0);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301216
Michael Wellingb28cb942015-05-07 18:36:53 -05001217 return status;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001218}
1219
Michael Wellingb28cb942015-05-07 18:36:53 -05001220static int omap2_mcspi_transfer_one(struct spi_master *master,
1221 struct spi_device *spi, struct spi_transfer *t)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001222{
1223 struct omap2_mcspi *mcspi;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001224 struct omap2_mcspi_dma *mcspi_dma;
Michael Wellingb28cb942015-05-07 18:36:53 -05001225 const void *tx_buf = t->tx_buf;
1226 void *rx_buf = t->rx_buf;
1227 unsigned len = t->len;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001228
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301229 mcspi = spi_master_get_devdata(master);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001230 mcspi_dma = mcspi->dma_channels + spi->chip_select;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001231
Michael Wellingb28cb942015-05-07 18:36:53 -05001232 if ((len && !(rx_buf || tx_buf))) {
1233 dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
1234 t->speed_hz,
1235 len,
1236 tx_buf ? "tx" : "",
1237 rx_buf ? "rx" : "",
1238 t->bits_per_word);
1239 return -EINVAL;
1240 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001241
Michael Wellingb28cb942015-05-07 18:36:53 -05001242 if (len < DMA_MIN_BYTES)
1243 goto skip_dma_map;
1244
1245 if (mcspi_dma->dma_tx && tx_buf != NULL) {
1246 t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
1247 len, DMA_TO_DEVICE);
1248 if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
1249 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1250 'T', len);
1251 return -EINVAL;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001252 }
Michael Wellingb28cb942015-05-07 18:36:53 -05001253 }
1254 if (mcspi_dma->dma_rx && rx_buf != NULL) {
1255 t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
1256 DMA_FROM_DEVICE);
1257 if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
1258 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
1259 'R', len);
1260 if (tx_buf != NULL)
1261 dma_unmap_single(mcspi->dev, t->tx_dma,
1262 len, DMA_TO_DEVICE);
1263 return -EINVAL;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001264 }
1265 }
1266
Michael Wellingb28cb942015-05-07 18:36:53 -05001267skip_dma_map:
1268 return omap2_mcspi_work_one(mcspi, spi, t);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001269}
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;
Michael Wellingb28cb942015-05-07 18:36:53 -05001347 master->transfer_one = omap2_mcspi_transfer_one;
Michael Wellingddcad7e2015-05-12 12:38:57 -05001348 master->set_cs = omap2_mcspi_set_cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001349 master->cleanup = omap2_mcspi_cleanup;
Benoit Coussond5a80032012-02-15 18:37:34 +01001350 master->dev.of_node = node;
Axel Linaca09242014-02-18 22:02:47 +08001351 master->max_speed_hz = OMAP2_MCSPI_MAX_FREQ;
1352 master->min_speed_hz = OMAP2_MCSPI_MAX_FREQ >> 15;
Benoit Coussond5a80032012-02-15 18:37:34 +01001353
Jingoo Han24b5a822013-05-23 19:20:40 +09001354 platform_set_drvdata(pdev, master);
Daniel Mack0384e902012-10-07 18:19:44 +02001355
1356 mcspi = spi_master_get_devdata(master);
1357 mcspi->master = master;
1358
Benoit Coussond5a80032012-02-15 18:37:34 +01001359 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1360 if (match) {
1361 u32 num_cs = 1; /* default number of chipselect */
1362 pdata = match->data;
1363
1364 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1365 master->num_chipselect = num_cs;
1366 master->bus_num = bus_num++;
Daniel Mack2cd45172012-11-14 11:14:26 +08001367 if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
1368 mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
Benoit Coussond5a80032012-02-15 18:37:34 +01001369 } else {
Jingoo Han8074cf02013-07-30 16:58:59 +09001370 pdata = dev_get_platdata(&pdev->dev);
Benoit Coussond5a80032012-02-15 18:37:34 +01001371 master->num_chipselect = pdata->num_cs;
1372 if (pdev->id != -1)
1373 master->bus_num = pdev->id;
Daniel Mack0384e902012-10-07 18:19:44 +02001374 mcspi->pin_dir = pdata->pin_dir;
Benoit Coussond5a80032012-02-15 18:37:34 +01001375 }
1376 regs_offset = pdata->regs_offset;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001377
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001378 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1379 if (r == NULL) {
1380 status = -ENODEV;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301381 goto free_master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001382 }
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301383
Benoit Coussond5a80032012-02-15 18:37:34 +01001384 r->start += regs_offset;
1385 r->end += regs_offset;
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301386 mcspi->phys = r->start;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001387
Thierry Redingb0ee5602013-01-21 11:09:18 +01001388 mcspi->base = devm_ioremap_resource(&pdev->dev, r);
1389 if (IS_ERR(mcspi->base)) {
1390 status = PTR_ERR(mcspi->base);
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301391 goto free_master;
Russell King55c381e2008-09-04 14:07:22 +01001392 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001393
Govindraj.R1f1a4382011-02-02 17:52:15 +05301394 mcspi->dev = &pdev->dev;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001395
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301396 INIT_LIST_HEAD(&mcspi->ctx.cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001397
Axel Lina6f936d2014-03-29 21:37:44 +08001398 mcspi->dma_channels = devm_kcalloc(&pdev->dev, master->num_chipselect,
1399 sizeof(struct omap2_mcspi_dma),
1400 GFP_KERNEL);
1401 if (mcspi->dma_channels == NULL) {
1402 status = -ENOMEM;
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301403 goto free_master;
Axel Lina6f936d2014-03-29 21:37:44 +08001404 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001405
Charulatha V1a5d8192011-02-02 17:52:14 +05301406 for (i = 0; i < master->num_chipselect; i++) {
Matt Porter74f3aaa2013-06-22 23:07:38 +05301407 char *dma_rx_ch_name = mcspi->dma_channels[i].dma_rx_ch_name;
1408 char *dma_tx_ch_name = mcspi->dma_channels[i].dma_tx_ch_name;
Charulatha V1a5d8192011-02-02 17:52:14 +05301409 struct resource *dma_res;
1410
Matt Porter74f3aaa2013-06-22 23:07:38 +05301411 sprintf(dma_rx_ch_name, "rx%d", i);
1412 if (!pdev->dev.of_node) {
1413 dma_res =
1414 platform_get_resource_byname(pdev,
1415 IORESOURCE_DMA,
1416 dma_rx_ch_name);
1417 if (!dma_res) {
1418 dev_dbg(&pdev->dev,
1419 "cannot get DMA RX channel\n");
1420 status = -ENODEV;
1421 break;
1422 }
Charulatha V1a5d8192011-02-02 17:52:14 +05301423
Matt Porter74f3aaa2013-06-22 23:07:38 +05301424 mcspi->dma_channels[i].dma_rx_sync_dev =
1425 dma_res->start;
Charulatha V1a5d8192011-02-02 17:52:14 +05301426 }
Matt Porter74f3aaa2013-06-22 23:07:38 +05301427 sprintf(dma_tx_ch_name, "tx%d", i);
1428 if (!pdev->dev.of_node) {
1429 dma_res =
1430 platform_get_resource_byname(pdev,
1431 IORESOURCE_DMA,
1432 dma_tx_ch_name);
1433 if (!dma_res) {
1434 dev_dbg(&pdev->dev,
1435 "cannot get DMA TX channel\n");
1436 status = -ENODEV;
1437 break;
1438 }
Charulatha V1a5d8192011-02-02 17:52:14 +05301439
Matt Porter74f3aaa2013-06-22 23:07:38 +05301440 mcspi->dma_channels[i].dma_tx_sync_dev =
1441 dma_res->start;
1442 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001443 }
1444
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301445 if (status < 0)
Axel Lina6f936d2014-03-29 21:37:44 +08001446 goto free_master;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301447
Shubhrajyoti D27b52842012-03-26 17:04:22 +05301448 pm_runtime_use_autosuspend(&pdev->dev);
1449 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301450 pm_runtime_enable(&pdev->dev);
1451
Wei Yongjun142e07b2013-04-18 11:14:59 +08001452 status = omap2_mcspi_master_setup(mcspi);
1453 if (status < 0)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301454 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001455
Jingoo Hanb95e02b2013-09-24 13:40:29 +09001456 status = devm_spi_register_master(&pdev->dev, master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001457 if (status < 0)
Shubhrajyoti D37a2d842012-08-02 16:41:25 +05301458 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001459
1460 return status;
1461
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301462disable_pm:
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301463 pm_runtime_disable(&pdev->dev);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301464free_master:
Shubhrajyoti D37a2d842012-08-02 16:41:25 +05301465 spi_master_put(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001466 return status;
1467}
1468
Grant Likelyfd4a3192012-12-07 16:57:14 +00001469static int omap2_mcspi_remove(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001470{
Axel Lina6f936d2014-03-29 21:37:44 +08001471 struct spi_master *master = platform_get_drvdata(pdev);
1472 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001473
Shubhrajyoti Da93a2022012-08-22 11:35:14 +05301474 pm_runtime_put_sync(mcspi->dev);
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301475 pm_runtime_disable(&pdev->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001476
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001477 return 0;
1478}
1479
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001480/* work with hotplug and coldplug */
1481MODULE_ALIAS("platform:omap2_mcspi");
1482
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001483#ifdef CONFIG_SUSPEND
1484/*
1485 * When SPI wake up from off-mode, CS is in activate state. If it was in
1486 * unactive state when driver was suspend, then force it to unactive state at
1487 * wake up.
1488 */
1489static int omap2_mcspi_resume(struct device *dev)
1490{
1491 struct spi_master *master = dev_get_drvdata(dev);
1492 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301493 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1494 struct omap2_mcspi_cs *cs;
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001495
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301496 pm_runtime_get_sync(mcspi->dev);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301497 list_for_each_entry(cs, &ctx->cs, node) {
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001498 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001499 /*
1500 * We need to toggle CS state for OMAP take this
1501 * change in account.
1502 */
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +05301503 cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
Victor Kamensky21b2ce52013-11-16 02:01:16 +02001504 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +05301505 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
Victor Kamensky21b2ce52013-11-16 02:01:16 +02001506 writel_relaxed(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001507 }
1508 }
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301509 pm_runtime_mark_last_busy(mcspi->dev);
1510 pm_runtime_put_autosuspend(mcspi->dev);
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001511 return 0;
1512}
1513#else
1514#define omap2_mcspi_resume NULL
1515#endif
1516
1517static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1518 .resume = omap2_mcspi_resume,
Govindraj.R1f1a4382011-02-02 17:52:15 +05301519 .runtime_resume = omap_mcspi_runtime_resume,
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001520};
1521
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001522static struct platform_driver omap2_mcspi_driver = {
1523 .driver = {
1524 .name = "omap2_mcspi",
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");