blob: 15d32042a7db3fcc961905849141d54653ff8338 [file] [log] [blame]
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001/*
2 * OMAP2 McSPI controller driver
3 *
4 * Copyright (C) 2005, 2006 Nokia Corporation
5 * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
Charulatha V1a5d8192011-02-02 17:52:14 +05306 * Juha Yrj�l� <juha.yrjola@nokia.com>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <linux/kernel.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/module.h>
28#include <linux/device.h>
29#include <linux/delay.h>
30#include <linux/dma-mapping.h>
Russell King53741ed2012-04-23 13:51:48 +010031#include <linux/dmaengine.h>
32#include <linux/omap-dma.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070033#include <linux/platform_device.h>
34#include <linux/err.h>
35#include <linux/clk.h>
36#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Govindraj.R1f1a4382011-02-02 17:52:15 +053038#include <linux/pm_runtime.h>
Benoit Coussond5a80032012-02-15 18:37:34 +010039#include <linux/of.h>
40#include <linux/of_device.h>
Illia Smyrnovd33f4732013-06-17 16:31:06 +030041#include <linux/gcd.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070042
43#include <linux/spi/spi.h>
44
Arnd Bergmann22037472012-08-24 15:21:06 +020045#include <linux/platform_data/spi-omap2-mcspi.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070046
47#define OMAP2_MCSPI_MAX_FREQ 48000000
Illia Smyrnovd33f4732013-06-17 16:31:06 +030048#define OMAP2_MCSPI_MAX_FIFODEPTH 64
49#define OMAP2_MCSPI_MAX_FIFOWCNT 0xFFFF
Shubhrajyoti D27b52842012-03-26 17:04:22 +053050#define SPI_AUTOSUSPEND_TIMEOUT 2000
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070051
52#define OMAP2_MCSPI_REVISION 0x00
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070053#define OMAP2_MCSPI_SYSSTATUS 0x14
54#define OMAP2_MCSPI_IRQSTATUS 0x18
55#define OMAP2_MCSPI_IRQENABLE 0x1c
56#define OMAP2_MCSPI_WAKEUPENABLE 0x20
57#define OMAP2_MCSPI_SYST 0x24
58#define OMAP2_MCSPI_MODULCTRL 0x28
Illia Smyrnovd33f4732013-06-17 16:31:06 +030059#define OMAP2_MCSPI_XFERLEVEL 0x7c
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070060
61/* per-channel banks, 0x14 bytes each, first is: */
62#define OMAP2_MCSPI_CHCONF0 0x2c
63#define OMAP2_MCSPI_CHSTAT0 0x30
64#define OMAP2_MCSPI_CHCTRL0 0x34
65#define OMAP2_MCSPI_TX0 0x38
66#define OMAP2_MCSPI_RX0 0x3c
67
68/* per-register bitmasks: */
Illia Smyrnovd33f4732013-06-17 16:31:06 +030069#define OMAP2_MCSPI_IRQSTATUS_EOW BIT(17)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070070
Jouni Hogander7a8fa722009-09-22 16:45:58 -070071#define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
72#define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
73#define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070074
Jouni Hogander7a8fa722009-09-22 16:45:58 -070075#define OMAP2_MCSPI_CHCONF_PHA BIT(0)
76#define OMAP2_MCSPI_CHCONF_POL BIT(1)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070077#define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070078#define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070079#define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070080#define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
81#define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070082#define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070083#define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
84#define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
85#define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
86#define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
87#define OMAP2_MCSPI_CHCONF_IS BIT(18)
88#define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
89#define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
Illia Smyrnovd33f4732013-06-17 16:31:06 +030090#define OMAP2_MCSPI_CHCONF_FFET BIT(27)
91#define OMAP2_MCSPI_CHCONF_FFER BIT(28)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070092
Jouni Hogander7a8fa722009-09-22 16:45:58 -070093#define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
94#define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
95#define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
Illia Smyrnovd33f4732013-06-17 16:31:06 +030096#define OMAP2_MCSPI_CHSTAT_TXFFE BIT(3)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070097
Jouni Hogander7a8fa722009-09-22 16:45:58 -070098#define OMAP2_MCSPI_CHCTRL_EN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070099
Jouni Hogander7a8fa722009-09-22 16:45:58 -0700100#define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700101
102/* We have 2 DMA channels per CS, one for RX and one for TX */
103struct omap2_mcspi_dma {
Russell King53741ed2012-04-23 13:51:48 +0100104 struct dma_chan *dma_tx;
105 struct dma_chan *dma_rx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700106
107 int dma_tx_sync_dev;
108 int dma_rx_sync_dev;
109
110 struct completion dma_tx_completion;
111 struct completion dma_rx_completion;
Matt Porter74f3aaa2013-06-22 23:07:38 +0530112
113 char dma_rx_ch_name[14];
114 char dma_tx_ch_name[14];
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700115};
116
117/* use PIO for small transfers, avoiding DMA setup/teardown overhead and
118 * cache operations; better heuristics consider wordsize and bitrate.
119 */
Roman Tereshonkov8b66c132010-04-12 09:07:54 +0000120#define DMA_MIN_BYTES 160
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700121
122
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530123/*
124 * Used for context save and restore, structure members to be updated whenever
125 * corresponding registers are modified.
126 */
127struct omap2_mcspi_regs {
128 u32 modulctrl;
129 u32 wakeupenable;
130 struct list_head cs;
131};
132
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700133struct omap2_mcspi {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700134 struct spi_master *master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700135 /* Virtual base address of the controller */
136 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100137 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700138 /* SPI1 has 4 channels, while SPI2 has 2 */
139 struct omap2_mcspi_dma *dma_channels;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530140 struct device *dev;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530141 struct omap2_mcspi_regs ctx;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300142 int fifo_depth;
Daniel Mack0384e902012-10-07 18:19:44 +0200143 unsigned int pin_dir:1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700144};
145
146struct omap2_mcspi_cs {
147 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100148 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700149 int word_len;
Tero Kristo89c05372009-09-22 16:46:17 -0700150 struct list_head node;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700151 /* Context save and restore shadow register */
152 u32 chconf0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700153};
154
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700155static inline void mcspi_write_reg(struct spi_master *master,
156 int idx, u32 val)
157{
158 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
159
160 __raw_writel(val, mcspi->base + idx);
161}
162
163static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
164{
165 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
166
167 return __raw_readl(mcspi->base + idx);
168}
169
170static inline void mcspi_write_cs_reg(const struct spi_device *spi,
171 int idx, u32 val)
172{
173 struct omap2_mcspi_cs *cs = spi->controller_state;
174
175 __raw_writel(val, cs->base + idx);
176}
177
178static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
179{
180 struct omap2_mcspi_cs *cs = spi->controller_state;
181
182 return __raw_readl(cs->base + idx);
183}
184
Hemanth Va41ae1a2009-09-22 16:46:16 -0700185static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
186{
187 struct omap2_mcspi_cs *cs = spi->controller_state;
188
189 return cs->chconf0;
190}
191
192static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
193{
194 struct omap2_mcspi_cs *cs = spi->controller_state;
195
196 cs->chconf0 = val;
197 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
Roman Tereshonkova330ce22010-03-15 09:06:28 +0000198 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700199}
200
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300201static inline int mcspi_bytes_per_word(int word_len)
202{
203 if (word_len <= 8)
204 return 1;
205 else if (word_len <= 16)
206 return 2;
207 else /* word_len <= 32 */
208 return 4;
209}
210
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700211static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
212 int is_read, int enable)
213{
214 u32 l, rw;
215
Hemanth Va41ae1a2009-09-22 16:46:16 -0700216 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700217
218 if (is_read) /* 1 is read, 0 write */
219 rw = OMAP2_MCSPI_CHCONF_DMAR;
220 else
221 rw = OMAP2_MCSPI_CHCONF_DMAW;
222
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530223 if (enable)
224 l |= rw;
225 else
226 l &= ~rw;
227
Hemanth Va41ae1a2009-09-22 16:46:16 -0700228 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700229}
230
231static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
232{
233 u32 l;
234
235 l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
236 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000237 /* Flash post-writes */
238 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700239}
240
241static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
242{
243 u32 l;
244
Hemanth Va41ae1a2009-09-22 16:46:16 -0700245 l = mcspi_cached_chconf0(spi);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530246 if (cs_active)
247 l |= OMAP2_MCSPI_CHCONF_FORCE;
248 else
249 l &= ~OMAP2_MCSPI_CHCONF_FORCE;
250
Hemanth Va41ae1a2009-09-22 16:46:16 -0700251 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700252}
253
254static void omap2_mcspi_set_master_mode(struct spi_master *master)
255{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530256 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
257 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700258 u32 l;
259
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530260 /*
261 * Setup when switching from (reset default) slave mode
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700262 * to single-channel master mode
263 */
264 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +0530265 l &= ~(OMAP2_MCSPI_MODULCTRL_STEST | OMAP2_MCSPI_MODULCTRL_MS);
266 l |= OMAP2_MCSPI_MODULCTRL_SINGLE;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700267 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700268
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530269 ctx->modulctrl = l;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700270}
271
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300272static void omap2_mcspi_set_fifo(const struct spi_device *spi,
273 struct spi_transfer *t, int enable)
274{
275 struct spi_master *master = spi->master;
276 struct omap2_mcspi_cs *cs = spi->controller_state;
277 struct omap2_mcspi *mcspi;
278 unsigned int wcnt;
279 int fifo_depth, bytes_per_word;
280 u32 chconf, xferlevel;
281
282 mcspi = spi_master_get_devdata(master);
283
284 chconf = mcspi_cached_chconf0(spi);
285 if (enable) {
286 bytes_per_word = mcspi_bytes_per_word(cs->word_len);
287 if (t->len % bytes_per_word != 0)
288 goto disable_fifo;
289
290 fifo_depth = gcd(t->len, OMAP2_MCSPI_MAX_FIFODEPTH);
291 if (fifo_depth < 2 || fifo_depth % bytes_per_word != 0)
292 goto disable_fifo;
293
294 wcnt = t->len / bytes_per_word;
295 if (wcnt > OMAP2_MCSPI_MAX_FIFOWCNT)
296 goto disable_fifo;
297
298 xferlevel = wcnt << 16;
299 if (t->rx_buf != NULL) {
300 chconf |= OMAP2_MCSPI_CHCONF_FFER;
301 xferlevel |= (fifo_depth - 1) << 8;
302 } else {
303 chconf |= OMAP2_MCSPI_CHCONF_FFET;
304 xferlevel |= fifo_depth - 1;
305 }
306
307 mcspi_write_reg(master, OMAP2_MCSPI_XFERLEVEL, xferlevel);
308 mcspi_write_chconf0(spi, chconf);
309 mcspi->fifo_depth = fifo_depth;
310
311 return;
312 }
313
314disable_fifo:
315 if (t->rx_buf != NULL)
316 chconf &= ~OMAP2_MCSPI_CHCONF_FFER;
317 else
318 chconf &= ~OMAP2_MCSPI_CHCONF_FFET;
319
320 mcspi_write_chconf0(spi, chconf);
321 mcspi->fifo_depth = 0;
322}
323
Hemanth Va41ae1a2009-09-22 16:46:16 -0700324static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
325{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530326 struct spi_master *spi_cntrl = mcspi->master;
327 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
328 struct omap2_mcspi_cs *cs;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700329
330 /* McSPI: context restore */
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530331 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
332 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700333
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530334 list_for_each_entry(cs, &ctx->cs, node)
Tero Kristo89c05372009-09-22 16:46:17 -0700335 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700336}
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700337
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300338static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
339{
340 unsigned long timeout;
341
342 timeout = jiffies + msecs_to_jiffies(1000);
343 while (!(__raw_readl(reg) & bit)) {
Sebastian Andrzej Siewiorff23fa32013-03-21 13:22:48 +0100344 if (time_after(jiffies, timeout)) {
345 if (!(__raw_readl(reg) & bit))
346 return -ETIMEDOUT;
347 else
348 return 0;
349 }
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300350 cpu_relax();
351 }
352 return 0;
353}
354
Russell King53741ed2012-04-23 13:51:48 +0100355static void omap2_mcspi_rx_callback(void *data)
356{
357 struct spi_device *spi = data;
358 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
359 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
360
Russell King53741ed2012-04-23 13:51:48 +0100361 /* We must disable the DMA RX request */
362 omap2_mcspi_set_dma_req(spi, 1, 0);
Felipe Balbi830379e2012-12-12 10:45:59 +0200363
364 complete(&mcspi_dma->dma_rx_completion);
Russell King53741ed2012-04-23 13:51:48 +0100365}
366
367static void omap2_mcspi_tx_callback(void *data)
368{
369 struct spi_device *spi = data;
370 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
371 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
372
Russell King53741ed2012-04-23 13:51:48 +0100373 /* We must disable the DMA TX request */
374 omap2_mcspi_set_dma_req(spi, 0, 0);
Felipe Balbi830379e2012-12-12 10:45:59 +0200375
376 complete(&mcspi_dma->dma_tx_completion);
Russell King53741ed2012-04-23 13:51:48 +0100377}
378
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530379static void omap2_mcspi_tx_dma(struct spi_device *spi,
380 struct spi_transfer *xfer,
381 struct dma_slave_config cfg)
382{
383 struct omap2_mcspi *mcspi;
384 struct omap2_mcspi_dma *mcspi_dma;
385 unsigned int count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530386
387 mcspi = spi_master_get_devdata(spi->master);
388 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
389 count = xfer->len;
390
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530391 if (mcspi_dma->dma_tx) {
392 struct dma_async_tx_descriptor *tx;
393 struct scatterlist sg;
394
395 dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
396
397 sg_init_table(&sg, 1);
398 sg_dma_address(&sg) = xfer->tx_dma;
399 sg_dma_len(&sg) = xfer->len;
400
401 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
402 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
403 if (tx) {
404 tx->callback = omap2_mcspi_tx_callback;
405 tx->callback_param = spi;
406 dmaengine_submit(tx);
407 } else {
408 /* FIXME: fall back to PIO? */
409 }
410 }
411 dma_async_issue_pending(mcspi_dma->dma_tx);
412 omap2_mcspi_set_dma_req(spi, 0, 1);
413
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530414}
415
416static unsigned
417omap2_mcspi_rx_dma(struct spi_device *spi, struct spi_transfer *xfer,
418 struct dma_slave_config cfg,
419 unsigned es)
420{
421 struct omap2_mcspi *mcspi;
422 struct omap2_mcspi_dma *mcspi_dma;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300423 unsigned int count, dma_count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530424 u32 l;
425 int elements = 0;
426 int word_len, element_count;
427 struct omap2_mcspi_cs *cs = spi->controller_state;
428 mcspi = spi_master_get_devdata(spi->master);
429 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
430 count = xfer->len;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300431 dma_count = xfer->len;
432
433 if (mcspi->fifo_depth == 0)
434 dma_count -= es;
435
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530436 word_len = cs->word_len;
437 l = mcspi_cached_chconf0(spi);
438
439 if (word_len <= 8)
440 element_count = count;
441 else if (word_len <= 16)
442 element_count = count >> 1;
443 else /* word_len <= 32 */
444 element_count = count >> 2;
445
446 if (mcspi_dma->dma_rx) {
447 struct dma_async_tx_descriptor *tx;
448 struct scatterlist sg;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530449
450 dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
451
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300452 if ((l & OMAP2_MCSPI_CHCONF_TURBO) && mcspi->fifo_depth == 0)
453 dma_count -= es;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530454
455 sg_init_table(&sg, 1);
456 sg_dma_address(&sg) = xfer->rx_dma;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300457 sg_dma_len(&sg) = dma_count;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530458
459 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
460 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT |
461 DMA_CTRL_ACK);
462 if (tx) {
463 tx->callback = omap2_mcspi_rx_callback;
464 tx->callback_param = spi;
465 dmaengine_submit(tx);
466 } else {
467 /* FIXME: fall back to PIO? */
468 }
469 }
470
471 dma_async_issue_pending(mcspi_dma->dma_rx);
472 omap2_mcspi_set_dma_req(spi, 1, 1);
473
474 wait_for_completion(&mcspi_dma->dma_rx_completion);
475 dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
476 DMA_FROM_DEVICE);
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300477
478 if (mcspi->fifo_depth > 0)
479 return count;
480
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530481 omap2_mcspi_set_enable(spi, 0);
482
483 elements = element_count - 1;
484
485 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
486 elements--;
487
488 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
489 & OMAP2_MCSPI_CHSTAT_RXS)) {
490 u32 w;
491
492 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
493 if (word_len <= 8)
494 ((u8 *)xfer->rx_buf)[elements++] = w;
495 else if (word_len <= 16)
496 ((u16 *)xfer->rx_buf)[elements++] = w;
497 else /* word_len <= 32 */
498 ((u32 *)xfer->rx_buf)[elements++] = w;
499 } else {
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300500 int bytes_per_word = mcspi_bytes_per_word(word_len);
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300501 dev_err(&spi->dev, "DMA RX penultimate word empty\n");
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300502 count -= (bytes_per_word << 1);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530503 omap2_mcspi_set_enable(spi, 1);
504 return count;
505 }
506 }
507 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
508 & OMAP2_MCSPI_CHSTAT_RXS)) {
509 u32 w;
510
511 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
512 if (word_len <= 8)
513 ((u8 *)xfer->rx_buf)[elements] = w;
514 else if (word_len <= 16)
515 ((u16 *)xfer->rx_buf)[elements] = w;
516 else /* word_len <= 32 */
517 ((u32 *)xfer->rx_buf)[elements] = w;
518 } else {
Jarkko Nikulaa1829d22013-10-11 13:53:59 +0300519 dev_err(&spi->dev, "DMA RX last word empty\n");
Illia Smyrnov56cd5c12013-06-14 19:12:07 +0300520 count -= mcspi_bytes_per_word(word_len);
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530521 }
522 omap2_mcspi_set_enable(spi, 1);
523 return count;
524}
525
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700526static unsigned
527omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
528{
529 struct omap2_mcspi *mcspi;
530 struct omap2_mcspi_cs *cs = spi->controller_state;
531 struct omap2_mcspi_dma *mcspi_dma;
Russell King8c7494a2012-04-23 13:56:25 +0100532 unsigned int count;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000533 u32 l;
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530534 u8 *rx;
535 const u8 *tx;
Russell King53741ed2012-04-23 13:51:48 +0100536 struct dma_slave_config cfg;
537 enum dma_slave_buswidth width;
538 unsigned es;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300539 u32 burst;
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530540 void __iomem *chstat_reg;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300541 void __iomem *irqstat_reg;
542 int wait_res;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700543
544 mcspi = spi_master_get_devdata(spi->master);
545 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000546 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700547
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300548
Russell King53741ed2012-04-23 13:51:48 +0100549 if (cs->word_len <= 8) {
550 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
551 es = 1;
552 } else if (cs->word_len <= 16) {
553 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
554 es = 2;
555 } else {
556 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
557 es = 4;
558 }
559
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300560 count = xfer->len;
561 burst = 1;
562
563 if (mcspi->fifo_depth > 0) {
564 if (count > mcspi->fifo_depth)
565 burst = mcspi->fifo_depth / es;
566 else
567 burst = count / es;
568 }
569
Russell King53741ed2012-04-23 13:51:48 +0100570 memset(&cfg, 0, sizeof(cfg));
571 cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
572 cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
573 cfg.src_addr_width = width;
574 cfg.dst_addr_width = width;
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300575 cfg.src_maxburst = burst;
576 cfg.dst_maxburst = burst;
Russell King53741ed2012-04-23 13:51:48 +0100577
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700578 rx = xfer->rx_buf;
579 tx = xfer->tx_buf;
580
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530581 if (tx != NULL)
582 omap2_mcspi_tx_dma(spi, xfer, cfg);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700583
Shubhrajyoti Dd7b43942012-09-11 12:13:20 +0530584 if (rx != NULL)
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530585 count = omap2_mcspi_rx_dma(spi, xfer, cfg, es);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700586
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530587 if (tx != NULL) {
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530588 wait_for_completion(&mcspi_dma->dma_tx_completion);
589 dma_unmap_single(mcspi->dev, xfer->tx_dma, xfer->len,
590 DMA_TO_DEVICE);
591
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300592 if (mcspi->fifo_depth > 0) {
593 irqstat_reg = mcspi->base + OMAP2_MCSPI_IRQSTATUS;
594
595 if (mcspi_wait_for_reg_bit(irqstat_reg,
596 OMAP2_MCSPI_IRQSTATUS_EOW) < 0)
597 dev_err(&spi->dev, "EOW timed out\n");
598
599 mcspi_write_reg(mcspi->master, OMAP2_MCSPI_IRQSTATUS,
600 OMAP2_MCSPI_IRQSTATUS_EOW);
601 }
602
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530603 /* for TX_ONLY mode, be sure all words have shifted out */
604 if (rx == NULL) {
Illia Smyrnovd33f4732013-06-17 16:31:06 +0300605 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
606 if (mcspi->fifo_depth > 0) {
607 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
608 OMAP2_MCSPI_CHSTAT_TXFFE);
609 if (wait_res < 0)
610 dev_err(&spi->dev, "TXFFE timed out\n");
611 } else {
612 wait_res = mcspi_wait_for_reg_bit(chstat_reg,
613 OMAP2_MCSPI_CHSTAT_TXS);
614 if (wait_res < 0)
615 dev_err(&spi->dev, "TXS timed out\n");
616 }
617 if (wait_res >= 0 &&
618 (mcspi_wait_for_reg_bit(chstat_reg,
619 OMAP2_MCSPI_CHSTAT_EOT) < 0))
Shubhrajyoti De47a6822012-11-06 14:30:19 +0530620 dev_err(&spi->dev, "EOT timed out\n");
621 }
622 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700623 return count;
624}
625
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700626static unsigned
627omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
628{
629 struct omap2_mcspi *mcspi;
630 struct omap2_mcspi_cs *cs = spi->controller_state;
631 unsigned int count, c;
632 u32 l;
633 void __iomem *base = cs->base;
634 void __iomem *tx_reg;
635 void __iomem *rx_reg;
636 void __iomem *chstat_reg;
637 int word_len;
638
639 mcspi = spi_master_get_devdata(spi->master);
640 count = xfer->len;
641 c = count;
642 word_len = cs->word_len;
643
Hemanth Va41ae1a2009-09-22 16:46:16 -0700644 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700645
646 /* We store the pre-calculated register addresses on stack to speed
647 * up the transfer loop. */
648 tx_reg = base + OMAP2_MCSPI_TX0;
649 rx_reg = base + OMAP2_MCSPI_RX0;
650 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
651
Michael Jonesadef6582011-02-25 16:55:11 +0100652 if (c < (word_len>>3))
653 return 0;
654
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700655 if (word_len <= 8) {
656 u8 *rx;
657 const u8 *tx;
658
659 rx = xfer->rx_buf;
660 tx = xfer->tx_buf;
661
662 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800663 c -= 1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700664 if (tx != NULL) {
665 if (mcspi_wait_for_reg_bit(chstat_reg,
666 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
667 dev_err(&spi->dev, "TXS timed out\n");
668 goto out;
669 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900670 dev_vdbg(&spi->dev, "write-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700671 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700672 __raw_writel(*tx++, tx_reg);
673 }
674 if (rx != NULL) {
675 if (mcspi_wait_for_reg_bit(chstat_reg,
676 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
677 dev_err(&spi->dev, "RXS timed out\n");
678 goto out;
679 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000680
681 if (c == 1 && tx == NULL &&
682 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
683 omap2_mcspi_set_enable(spi, 0);
684 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900685 dev_vdbg(&spi->dev, "read-%d %02x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000686 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000687 if (mcspi_wait_for_reg_bit(chstat_reg,
688 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
689 dev_err(&spi->dev,
690 "RXS timed out\n");
691 goto out;
692 }
693 c = 0;
694 } else if (c == 0 && tx == NULL) {
695 omap2_mcspi_set_enable(spi, 0);
696 }
697
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700698 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900699 dev_vdbg(&spi->dev, "read-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700700 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700701 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200702 } while (c);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700703 } else if (word_len <= 16) {
704 u16 *rx;
705 const u16 *tx;
706
707 rx = xfer->rx_buf;
708 tx = xfer->tx_buf;
709 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800710 c -= 2;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700711 if (tx != NULL) {
712 if (mcspi_wait_for_reg_bit(chstat_reg,
713 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
714 dev_err(&spi->dev, "TXS timed out\n");
715 goto out;
716 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900717 dev_vdbg(&spi->dev, "write-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700718 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700719 __raw_writel(*tx++, tx_reg);
720 }
721 if (rx != NULL) {
722 if (mcspi_wait_for_reg_bit(chstat_reg,
723 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
724 dev_err(&spi->dev, "RXS timed out\n");
725 goto out;
726 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000727
728 if (c == 2 && tx == NULL &&
729 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
730 omap2_mcspi_set_enable(spi, 0);
731 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900732 dev_vdbg(&spi->dev, "read-%d %04x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000733 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000734 if (mcspi_wait_for_reg_bit(chstat_reg,
735 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
736 dev_err(&spi->dev,
737 "RXS timed out\n");
738 goto out;
739 }
740 c = 0;
741 } else if (c == 0 && tx == NULL) {
742 omap2_mcspi_set_enable(spi, 0);
743 }
744
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700745 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900746 dev_vdbg(&spi->dev, "read-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700747 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700748 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200749 } while (c >= 2);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700750 } else if (word_len <= 32) {
751 u32 *rx;
752 const u32 *tx;
753
754 rx = xfer->rx_buf;
755 tx = xfer->tx_buf;
756 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800757 c -= 4;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700758 if (tx != NULL) {
759 if (mcspi_wait_for_reg_bit(chstat_reg,
760 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
761 dev_err(&spi->dev, "TXS timed out\n");
762 goto out;
763 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900764 dev_vdbg(&spi->dev, "write-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700765 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700766 __raw_writel(*tx++, tx_reg);
767 }
768 if (rx != NULL) {
769 if (mcspi_wait_for_reg_bit(chstat_reg,
770 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
771 dev_err(&spi->dev, "RXS timed out\n");
772 goto out;
773 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000774
775 if (c == 4 && tx == NULL &&
776 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
777 omap2_mcspi_set_enable(spi, 0);
778 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900779 dev_vdbg(&spi->dev, "read-%d %08x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000780 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000781 if (mcspi_wait_for_reg_bit(chstat_reg,
782 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
783 dev_err(&spi->dev,
784 "RXS timed out\n");
785 goto out;
786 }
787 c = 0;
788 } else if (c == 0 && tx == NULL) {
789 omap2_mcspi_set_enable(spi, 0);
790 }
791
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700792 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900793 dev_vdbg(&spi->dev, "read-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700794 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700795 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200796 } while (c >= 4);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700797 }
798
799 /* for TX_ONLY mode, be sure all words have shifted out */
800 if (xfer->rx_buf == NULL) {
801 if (mcspi_wait_for_reg_bit(chstat_reg,
802 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
803 dev_err(&spi->dev, "TXS timed out\n");
804 } else if (mcspi_wait_for_reg_bit(chstat_reg,
805 OMAP2_MCSPI_CHSTAT_EOT) < 0)
806 dev_err(&spi->dev, "EOT timed out\n");
Jason Wange1993ed2010-10-19 18:03:27 +0800807
808 /* disable chan to purge rx datas received in TX_ONLY transfer,
809 * otherwise these rx datas will affect the direct following
810 * RX_ONLY transfer.
811 */
812 omap2_mcspi_set_enable(spi, 0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700813 }
814out:
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000815 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700816 return count - c;
817}
818
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200819static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
820{
821 u32 div;
822
823 for (div = 0; div < 15; div++)
824 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
825 return div;
826
827 return 15;
828}
829
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700830/* called only when no transfer is active to this device */
831static int omap2_mcspi_setup_transfer(struct spi_device *spi,
832 struct spi_transfer *t)
833{
834 struct omap2_mcspi_cs *cs = spi->controller_state;
835 struct omap2_mcspi *mcspi;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700836 struct spi_master *spi_cntrl;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700837 u32 l = 0, div = 0;
838 u8 word_len = spi->bits_per_word;
Scott Ellis9bd45172010-03-10 14:23:13 -0700839 u32 speed_hz = spi->max_speed_hz;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700840
841 mcspi = spi_master_get_devdata(spi->master);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700842 spi_cntrl = mcspi->master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700843
844 if (t != NULL && t->bits_per_word)
845 word_len = t->bits_per_word;
846
847 cs->word_len = word_len;
848
Scott Ellis9bd45172010-03-10 14:23:13 -0700849 if (t && t->speed_hz)
850 speed_hz = t->speed_hz;
851
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200852 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
853 div = omap2_mcspi_calc_divisor(speed_hz);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700854
Hemanth Va41ae1a2009-09-22 16:46:16 -0700855 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700856
857 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
858 * REVISIT: this controller could support SPI_3WIRE mode.
859 */
Daniel Mack2cd45172012-11-14 11:14:26 +0800860 if (mcspi->pin_dir == MCSPI_PINDIR_D0_IN_D1_OUT) {
Daniel Mack0384e902012-10-07 18:19:44 +0200861 l &= ~OMAP2_MCSPI_CHCONF_IS;
862 l &= ~OMAP2_MCSPI_CHCONF_DPE1;
863 l |= OMAP2_MCSPI_CHCONF_DPE0;
864 } else {
865 l |= OMAP2_MCSPI_CHCONF_IS;
866 l |= OMAP2_MCSPI_CHCONF_DPE1;
867 l &= ~OMAP2_MCSPI_CHCONF_DPE0;
868 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700869
870 /* wordlength */
871 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
872 l |= (word_len - 1) << 7;
873
874 /* set chipselect polarity; manage with FORCE */
875 if (!(spi->mode & SPI_CS_HIGH))
876 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
877 else
878 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
879
880 /* set clock divisor */
881 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
882 l |= div << 2;
883
884 /* set SPI mode 0..3 */
885 if (spi->mode & SPI_CPOL)
886 l |= OMAP2_MCSPI_CHCONF_POL;
887 else
888 l &= ~OMAP2_MCSPI_CHCONF_POL;
889 if (spi->mode & SPI_CPHA)
890 l |= OMAP2_MCSPI_CHCONF_PHA;
891 else
892 l &= ~OMAP2_MCSPI_CHCONF_PHA;
893
Hemanth Va41ae1a2009-09-22 16:46:16 -0700894 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700895
896 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200897 OMAP2_MCSPI_MAX_FREQ >> div,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700898 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
899 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
900
901 return 0;
902}
903
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700904/*
905 * Note that we currently allow DMA only if we get a channel
906 * for both rx and tx. Otherwise we'll do PIO for both rx and tx.
907 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700908static int omap2_mcspi_request_dma(struct spi_device *spi)
909{
910 struct spi_master *master = spi->master;
911 struct omap2_mcspi *mcspi;
912 struct omap2_mcspi_dma *mcspi_dma;
Russell King53741ed2012-04-23 13:51:48 +0100913 dma_cap_mask_t mask;
914 unsigned sig;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700915
916 mcspi = spi_master_get_devdata(master);
917 mcspi_dma = mcspi->dma_channels + spi->chip_select;
918
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700919 init_completion(&mcspi_dma->dma_rx_completion);
920 init_completion(&mcspi_dma->dma_tx_completion);
921
Russell King53741ed2012-04-23 13:51:48 +0100922 dma_cap_zero(mask);
923 dma_cap_set(DMA_SLAVE, mask);
Russell King53741ed2012-04-23 13:51:48 +0100924 sig = mcspi_dma->dma_rx_sync_dev;
Matt Porter74f3aaa2013-06-22 23:07:38 +0530925
926 mcspi_dma->dma_rx =
927 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
928 &sig, &master->dev,
929 mcspi_dma->dma_rx_ch_name);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700930 if (!mcspi_dma->dma_rx)
931 goto no_dma;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700932
Russell King53741ed2012-04-23 13:51:48 +0100933 sig = mcspi_dma->dma_tx_sync_dev;
Matt Porter74f3aaa2013-06-22 23:07:38 +0530934 mcspi_dma->dma_tx =
935 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
936 &sig, &master->dev,
937 mcspi_dma->dma_tx_ch_name);
938
Russell King53741ed2012-04-23 13:51:48 +0100939 if (!mcspi_dma->dma_tx) {
Russell King53741ed2012-04-23 13:51:48 +0100940 dma_release_channel(mcspi_dma->dma_rx);
941 mcspi_dma->dma_rx = NULL;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700942 goto no_dma;
Russell King53741ed2012-04-23 13:51:48 +0100943 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700944
945 return 0;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700946
947no_dma:
948 dev_warn(&spi->dev, "not using DMA for McSPI\n");
949 return -EAGAIN;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700950}
951
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700952static int omap2_mcspi_setup(struct spi_device *spi)
953{
954 int ret;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530955 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
956 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700957 struct omap2_mcspi_dma *mcspi_dma;
958 struct omap2_mcspi_cs *cs = spi->controller_state;
959
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700960 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
961
962 if (!cs) {
Russell King10aa5a32012-06-18 11:27:04 +0100963 cs = kzalloc(sizeof *cs, GFP_KERNEL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700964 if (!cs)
965 return -ENOMEM;
966 cs->base = mcspi->base + spi->chip_select * 0x14;
Russell Kinge5480b732008-09-01 21:51:50 +0100967 cs->phys = mcspi->phys + spi->chip_select * 0x14;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700968 cs->chconf0 = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700969 spi->controller_state = cs;
Tero Kristo89c05372009-09-22 16:46:17 -0700970 /* Link this to context save list */
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530971 list_add_tail(&cs->node, &ctx->cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700972 }
973
Russell King8c7494a2012-04-23 13:56:25 +0100974 if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700975 ret = omap2_mcspi_request_dma(spi);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -0700976 if (ret < 0 && ret != -EAGAIN)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700977 return ret;
978 }
979
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +0530980 ret = pm_runtime_get_sync(mcspi->dev);
Govindraj.R1f1a4382011-02-02 17:52:15 +0530981 if (ret < 0)
982 return ret;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700983
Kyungmin Park86eeb6f2007-10-16 01:27:45 -0700984 ret = omap2_mcspi_setup_transfer(spi, NULL);
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +0530985 pm_runtime_mark_last_busy(mcspi->dev);
986 pm_runtime_put_autosuspend(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700987
988 return ret;
989}
990
991static void omap2_mcspi_cleanup(struct spi_device *spi)
992{
993 struct omap2_mcspi *mcspi;
994 struct omap2_mcspi_dma *mcspi_dma;
Tero Kristo89c05372009-09-22 16:46:17 -0700995 struct omap2_mcspi_cs *cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700996
997 mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700998
Scott Ellis5e774942010-03-10 14:22:45 -0700999 if (spi->controller_state) {
1000 /* Unlink controller state from context save list */
1001 cs = spi->controller_state;
1002 list_del(&cs->node);
Tero Kristo89c05372009-09-22 16:46:17 -07001003
Russell King10aa5a32012-06-18 11:27:04 +01001004 kfree(cs);
Scott Ellis5e774942010-03-10 14:22:45 -07001005 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001006
Scott Ellis99f1a432010-05-24 14:20:27 +00001007 if (spi->chip_select < spi->master->num_chipselect) {
1008 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
1009
Russell King53741ed2012-04-23 13:51:48 +01001010 if (mcspi_dma->dma_rx) {
1011 dma_release_channel(mcspi_dma->dma_rx);
1012 mcspi_dma->dma_rx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +00001013 }
Russell King53741ed2012-04-23 13:51:48 +01001014 if (mcspi_dma->dma_tx) {
1015 dma_release_channel(mcspi_dma->dma_tx);
1016 mcspi_dma->dma_tx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +00001017 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001018 }
1019}
1020
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301021static void omap2_mcspi_work(struct omap2_mcspi *mcspi, struct spi_message *m)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001022{
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001023
1024 /* We only enable one channel at a time -- the one whose message is
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301025 * -- although this controller would gladly
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001026 * arbitrate among multiple channels. This corresponds to "single
1027 * channel" master mode. As a side effect, we need to manage the
1028 * chipselect with the FORCE bit ... CS != channel enable.
1029 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001030
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301031 struct spi_device *spi;
1032 struct spi_transfer *t = NULL;
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001033 struct spi_master *master;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001034 struct omap2_mcspi_dma *mcspi_dma;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301035 int cs_active = 0;
1036 struct omap2_mcspi_cs *cs;
1037 struct omap2_mcspi_device_config *cd;
1038 int par_override = 0;
1039 int status = 0;
1040 u32 chconf;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001041
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301042 spi = m->spi;
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001043 master = spi->master;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001044 mcspi_dma = mcspi->dma_channels + spi->chip_select;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301045 cs = spi->controller_state;
1046 cd = spi->controller_data;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001047
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001048 omap2_mcspi_set_enable(spi, 0);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301049 list_for_each_entry(t, &m->transfers, transfer_list) {
1050 if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
1051 status = -EINVAL;
1052 break;
1053 }
1054 if (par_override || t->speed_hz || t->bits_per_word) {
1055 par_override = 1;
1056 status = omap2_mcspi_setup_transfer(spi, t);
1057 if (status < 0)
1058 break;
1059 if (!t->speed_hz && !t->bits_per_word)
1060 par_override = 0;
1061 }
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001062 if (cd && cd->cs_per_word) {
1063 chconf = mcspi->ctx.modulctrl;
1064 chconf &= ~OMAP2_MCSPI_MODULCTRL_SINGLE;
1065 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1066 mcspi->ctx.modulctrl =
1067 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1068 }
1069
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001070
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301071 if (!cs_active) {
1072 omap2_mcspi_force_cs(spi, 1);
1073 cs_active = 1;
1074 }
1075
1076 chconf = mcspi_cached_chconf0(spi);
1077 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
1078 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
1079
1080 if (t->tx_buf == NULL)
1081 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
1082 else if (t->rx_buf == NULL)
1083 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
1084
1085 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
1086 /* Turbo mode is for more than one word */
1087 if (t->len > ((cs->word_len + 7) >> 3))
1088 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
1089 }
1090
1091 mcspi_write_chconf0(spi, chconf);
1092
1093 if (t->len) {
1094 unsigned count;
1095
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001096 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1097 (m->is_dma_mapped || t->len >= DMA_MIN_BYTES))
1098 omap2_mcspi_set_fifo(spi, t, 1);
1099
1100 omap2_mcspi_set_enable(spi, 1);
1101
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301102 /* RX_ONLY mode needs dummy data in TX reg */
1103 if (t->tx_buf == NULL)
1104 __raw_writel(0, cs->base
1105 + OMAP2_MCSPI_TX0);
1106
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001107 if ((mcspi_dma->dma_rx && mcspi_dma->dma_tx) &&
1108 (m->is_dma_mapped || t->len >= DMA_MIN_BYTES))
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301109 count = omap2_mcspi_txrx_dma(spi, t);
1110 else
1111 count = omap2_mcspi_txrx_pio(spi, t);
1112 m->actual_length += count;
1113
1114 if (count != t->len) {
1115 status = -EIO;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001116 break;
1117 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001118 }
1119
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301120 if (t->delay_usecs)
1121 udelay(t->delay_usecs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001122
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301123 /* ignore the "leave it on after last xfer" hint */
1124 if (t->cs_change) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001125 omap2_mcspi_force_cs(spi, 0);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301126 cs_active = 0;
1127 }
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001128
1129 omap2_mcspi_set_enable(spi, 0);
1130
1131 if (mcspi->fifo_depth > 0)
1132 omap2_mcspi_set_fifo(spi, t, 0);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301133 }
1134 /* Restore defaults if they were overriden */
1135 if (par_override) {
1136 par_override = 0;
1137 status = omap2_mcspi_setup_transfer(spi, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001138 }
1139
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301140 if (cs_active)
1141 omap2_mcspi_force_cs(spi, 0);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301142
Matthias Brugger5cbc7ca2013-01-24 13:40:41 +01001143 if (cd && cd->cs_per_word) {
1144 chconf = mcspi->ctx.modulctrl;
1145 chconf |= OMAP2_MCSPI_MODULCTRL_SINGLE;
1146 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, chconf);
1147 mcspi->ctx.modulctrl =
1148 mcspi_read_cs_reg(spi, OMAP2_MCSPI_MODULCTRL);
1149 }
1150
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301151 omap2_mcspi_set_enable(spi, 0);
1152
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001153 if (mcspi->fifo_depth > 0 && t)
1154 omap2_mcspi_set_fifo(spi, t, 0);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301155
Illia Smyrnovd33f4732013-06-17 16:31:06 +03001156 m->status = status;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001157}
1158
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301159static int omap2_mcspi_transfer_one_message(struct spi_master *master,
Matthias Brugger18dd6192013-01-24 13:28:58 +01001160 struct spi_message *m)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001161{
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001162 struct spi_device *spi;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001163 struct omap2_mcspi *mcspi;
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001164 struct omap2_mcspi_dma *mcspi_dma;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001165 struct spi_transfer *t;
1166
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001167 spi = m->spi;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301168 mcspi = spi_master_get_devdata(master);
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001169 mcspi_dma = mcspi->dma_channels + spi->chip_select;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001170 m->actual_length = 0;
1171 m->status = 0;
1172
1173 /* reject invalid messages and transfers */
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301174 if (list_empty(&m->transfers))
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001175 return -EINVAL;
1176 list_for_each_entry(t, &m->transfers, transfer_list) {
1177 const void *tx_buf = t->tx_buf;
1178 void *rx_buf = t->rx_buf;
1179 unsigned len = t->len;
1180
1181 if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
Stephen Warren24778be2013-05-21 20:36:35 -06001182 || (len && !(rx_buf || tx_buf))) {
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301183 dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001184 t->speed_hz,
1185 len,
1186 tx_buf ? "tx" : "",
1187 rx_buf ? "rx" : "",
1188 t->bits_per_word);
1189 return -EINVAL;
1190 }
Hannu Heikkinen57d9c102011-02-24 21:31:33 +02001191 if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301192 dev_dbg(mcspi->dev, "speed_hz %d below minimum %d Hz\n",
Matthias Brugger18dd6192013-01-24 13:28:58 +01001193 t->speed_hz,
1194 OMAP2_MCSPI_MAX_FREQ >> 15);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001195 return -EINVAL;
1196 }
1197
1198 if (m->is_dma_mapped || len < DMA_MIN_BYTES)
1199 continue;
1200
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001201 if (mcspi_dma->dma_tx && tx_buf != NULL) {
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301202 t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001203 len, DMA_TO_DEVICE);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301204 if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
1205 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001206 'T', len);
1207 return -EINVAL;
1208 }
1209 }
Tony Lindgrenddc5cdf2013-04-12 17:25:07 -07001210 if (mcspi_dma->dma_rx && rx_buf != NULL) {
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301211 t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001212 DMA_FROM_DEVICE);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301213 if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
1214 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001215 'R', len);
1216 if (tx_buf != NULL)
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301217 dma_unmap_single(mcspi->dev, t->tx_dma,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001218 len, DMA_TO_DEVICE);
1219 return -EINVAL;
1220 }
1221 }
1222 }
1223
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301224 omap2_mcspi_work(mcspi, m);
1225 spi_finalize_current_message(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001226 return 0;
1227}
1228
Grant Likelyfd4a3192012-12-07 16:57:14 +00001229static int omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001230{
1231 struct spi_master *master = mcspi->master;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301232 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301233 int ret = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001234
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301235 ret = pm_runtime_get_sync(mcspi->dev);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301236 if (ret < 0)
1237 return ret;
Jouni Hoganderddb22192009-07-29 15:02:11 -07001238
Shubhrajyoti D39f80522012-03-29 22:11:07 +05301239 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
Matthias Brugger18dd6192013-01-24 13:28:58 +01001240 OMAP2_MCSPI_WAKEUPENABLE_WKEN);
Shubhrajyoti D39f80522012-03-29 22:11:07 +05301241 ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001242
1243 omap2_mcspi_set_master_mode(master);
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301244 pm_runtime_mark_last_busy(mcspi->dev);
1245 pm_runtime_put_autosuspend(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001246 return 0;
1247}
1248
Govindraj.R1f1a4382011-02-02 17:52:15 +05301249static int omap_mcspi_runtime_resume(struct device *dev)
1250{
1251 struct omap2_mcspi *mcspi;
1252 struct spi_master *master;
1253
1254 master = dev_get_drvdata(dev);
1255 mcspi = spi_master_get_devdata(master);
1256 omap2_mcspi_restore_ctx(mcspi);
1257
1258 return 0;
1259}
1260
Benoit Coussond5a80032012-02-15 18:37:34 +01001261static struct omap2_mcspi_platform_config omap2_pdata = {
1262 .regs_offset = 0,
1263};
1264
1265static struct omap2_mcspi_platform_config omap4_pdata = {
1266 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1267};
1268
1269static const struct of_device_id omap_mcspi_of_match[] = {
1270 {
1271 .compatible = "ti,omap2-mcspi",
1272 .data = &omap2_pdata,
1273 },
1274 {
1275 .compatible = "ti,omap4-mcspi",
1276 .data = &omap4_pdata,
1277 },
1278 { },
1279};
1280MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
Girishccc7bae2008-02-06 01:38:16 -08001281
Grant Likelyfd4a3192012-12-07 16:57:14 +00001282static int omap2_mcspi_probe(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001283{
1284 struct spi_master *master;
Uwe Kleine-König83a01e72012-05-21 21:57:39 +02001285 const struct omap2_mcspi_platform_config *pdata;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001286 struct omap2_mcspi *mcspi;
1287 struct resource *r;
1288 int status = 0, i;
Benoit Coussond5a80032012-02-15 18:37:34 +01001289 u32 regs_offset = 0;
1290 static int bus_num = 1;
1291 struct device_node *node = pdev->dev.of_node;
1292 const struct of_device_id *match;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001293
1294 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1295 if (master == NULL) {
1296 dev_dbg(&pdev->dev, "master allocation failed\n");
1297 return -ENOMEM;
1298 }
1299
David Brownelle7db06b2009-06-17 16:26:04 -07001300 /* the spi->mode bits understood by this driver: */
1301 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
Stephen Warren24778be2013-05-21 20:36:35 -06001302 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001303 master->setup = omap2_mcspi_setup;
Mark Brownf0278a12013-07-28 15:34:37 +01001304 master->auto_runtime_pm = true;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301305 master->transfer_one_message = omap2_mcspi_transfer_one_message;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001306 master->cleanup = omap2_mcspi_cleanup;
Benoit Coussond5a80032012-02-15 18:37:34 +01001307 master->dev.of_node = node;
1308
Jingoo Han24b5a822013-05-23 19:20:40 +09001309 platform_set_drvdata(pdev, master);
Daniel Mack0384e902012-10-07 18:19:44 +02001310
1311 mcspi = spi_master_get_devdata(master);
1312 mcspi->master = master;
1313
Benoit Coussond5a80032012-02-15 18:37:34 +01001314 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1315 if (match) {
1316 u32 num_cs = 1; /* default number of chipselect */
1317 pdata = match->data;
1318
1319 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1320 master->num_chipselect = num_cs;
1321 master->bus_num = bus_num++;
Daniel Mack2cd45172012-11-14 11:14:26 +08001322 if (of_get_property(node, "ti,pindir-d0-out-d1-in", NULL))
1323 mcspi->pin_dir = MCSPI_PINDIR_D0_OUT_D1_IN;
Benoit Coussond5a80032012-02-15 18:37:34 +01001324 } else {
Jingoo Han8074cf02013-07-30 16:58:59 +09001325 pdata = dev_get_platdata(&pdev->dev);
Benoit Coussond5a80032012-02-15 18:37:34 +01001326 master->num_chipselect = pdata->num_cs;
1327 if (pdev->id != -1)
1328 master->bus_num = pdev->id;
Daniel Mack0384e902012-10-07 18:19:44 +02001329 mcspi->pin_dir = pdata->pin_dir;
Benoit Coussond5a80032012-02-15 18:37:34 +01001330 }
1331 regs_offset = pdata->regs_offset;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001332
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001333 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1334 if (r == NULL) {
1335 status = -ENODEV;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301336 goto free_master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001337 }
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301338
Benoit Coussond5a80032012-02-15 18:37:34 +01001339 r->start += regs_offset;
1340 r->end += regs_offset;
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301341 mcspi->phys = r->start;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001342
Thierry Redingb0ee5602013-01-21 11:09:18 +01001343 mcspi->base = devm_ioremap_resource(&pdev->dev, r);
1344 if (IS_ERR(mcspi->base)) {
1345 status = PTR_ERR(mcspi->base);
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301346 goto free_master;
Russell King55c381e2008-09-04 14:07:22 +01001347 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001348
Govindraj.R1f1a4382011-02-02 17:52:15 +05301349 mcspi->dev = &pdev->dev;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001350
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301351 INIT_LIST_HEAD(&mcspi->ctx.cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001352
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001353 mcspi->dma_channels = kcalloc(master->num_chipselect,
1354 sizeof(struct omap2_mcspi_dma),
1355 GFP_KERNEL);
1356
1357 if (mcspi->dma_channels == NULL)
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301358 goto free_master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001359
Charulatha V1a5d8192011-02-02 17:52:14 +05301360 for (i = 0; i < master->num_chipselect; i++) {
Matt Porter74f3aaa2013-06-22 23:07:38 +05301361 char *dma_rx_ch_name = mcspi->dma_channels[i].dma_rx_ch_name;
1362 char *dma_tx_ch_name = mcspi->dma_channels[i].dma_tx_ch_name;
Charulatha V1a5d8192011-02-02 17:52:14 +05301363 struct resource *dma_res;
1364
Matt Porter74f3aaa2013-06-22 23:07:38 +05301365 sprintf(dma_rx_ch_name, "rx%d", i);
1366 if (!pdev->dev.of_node) {
1367 dma_res =
1368 platform_get_resource_byname(pdev,
1369 IORESOURCE_DMA,
1370 dma_rx_ch_name);
1371 if (!dma_res) {
1372 dev_dbg(&pdev->dev,
1373 "cannot get DMA RX channel\n");
1374 status = -ENODEV;
1375 break;
1376 }
Charulatha V1a5d8192011-02-02 17:52:14 +05301377
Matt Porter74f3aaa2013-06-22 23:07:38 +05301378 mcspi->dma_channels[i].dma_rx_sync_dev =
1379 dma_res->start;
Charulatha V1a5d8192011-02-02 17:52:14 +05301380 }
Matt Porter74f3aaa2013-06-22 23:07:38 +05301381 sprintf(dma_tx_ch_name, "tx%d", i);
1382 if (!pdev->dev.of_node) {
1383 dma_res =
1384 platform_get_resource_byname(pdev,
1385 IORESOURCE_DMA,
1386 dma_tx_ch_name);
1387 if (!dma_res) {
1388 dev_dbg(&pdev->dev,
1389 "cannot get DMA TX channel\n");
1390 status = -ENODEV;
1391 break;
1392 }
Charulatha V1a5d8192011-02-02 17:52:14 +05301393
Matt Porter74f3aaa2013-06-22 23:07:38 +05301394 mcspi->dma_channels[i].dma_tx_sync_dev =
1395 dma_res->start;
1396 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001397 }
1398
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301399 if (status < 0)
1400 goto dma_chnl_free;
1401
Shubhrajyoti D27b52842012-03-26 17:04:22 +05301402 pm_runtime_use_autosuspend(&pdev->dev);
1403 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301404 pm_runtime_enable(&pdev->dev);
1405
Wei Yongjun142e07b2013-04-18 11:14:59 +08001406 status = omap2_mcspi_master_setup(mcspi);
1407 if (status < 0)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301408 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001409
1410 status = spi_register_master(master);
1411 if (status < 0)
Shubhrajyoti D37a2d842012-08-02 16:41:25 +05301412 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001413
1414 return status;
1415
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301416disable_pm:
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301417 pm_runtime_disable(&pdev->dev);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301418dma_chnl_free:
Govindraj.R1f1a4382011-02-02 17:52:15 +05301419 kfree(mcspi->dma_channels);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301420free_master:
Shubhrajyoti D37a2d842012-08-02 16:41:25 +05301421 spi_master_put(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001422 return status;
1423}
1424
Grant Likelyfd4a3192012-12-07 16:57:14 +00001425static int omap2_mcspi_remove(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001426{
1427 struct spi_master *master;
1428 struct omap2_mcspi *mcspi;
1429 struct omap2_mcspi_dma *dma_channels;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001430
Jingoo Han24b5a822013-05-23 19:20:40 +09001431 master = platform_get_drvdata(pdev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001432 mcspi = spi_master_get_devdata(master);
1433 dma_channels = mcspi->dma_channels;
1434
Shubhrajyoti Da93a2022012-08-22 11:35:14 +05301435 pm_runtime_put_sync(mcspi->dev);
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301436 pm_runtime_disable(&pdev->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001437
1438 spi_unregister_master(master);
1439 kfree(dma_channels);
1440
1441 return 0;
1442}
1443
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001444/* work with hotplug and coldplug */
1445MODULE_ALIAS("platform:omap2_mcspi");
1446
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001447#ifdef CONFIG_SUSPEND
1448/*
1449 * When SPI wake up from off-mode, CS is in activate state. If it was in
1450 * unactive state when driver was suspend, then force it to unactive state at
1451 * wake up.
1452 */
1453static int omap2_mcspi_resume(struct device *dev)
1454{
1455 struct spi_master *master = dev_get_drvdata(dev);
1456 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301457 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1458 struct omap2_mcspi_cs *cs;
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001459
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301460 pm_runtime_get_sync(mcspi->dev);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301461 list_for_each_entry(cs, &ctx->cs, node) {
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001462 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001463 /*
1464 * We need to toggle CS state for OMAP take this
1465 * change in account.
1466 */
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +05301467 cs->chconf0 |= OMAP2_MCSPI_CHCONF_FORCE;
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001468 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Shubhrajyoti Daf4e9442012-08-22 11:35:13 +05301469 cs->chconf0 &= ~OMAP2_MCSPI_CHCONF_FORCE;
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001470 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1471 }
1472 }
Shubhrajyoti D034d3dc2012-08-22 11:35:12 +05301473 pm_runtime_mark_last_busy(mcspi->dev);
1474 pm_runtime_put_autosuspend(mcspi->dev);
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001475 return 0;
1476}
1477#else
1478#define omap2_mcspi_resume NULL
1479#endif
1480
1481static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1482 .resume = omap2_mcspi_resume,
Govindraj.R1f1a4382011-02-02 17:52:15 +05301483 .runtime_resume = omap_mcspi_runtime_resume,
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001484};
1485
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001486static struct platform_driver omap2_mcspi_driver = {
1487 .driver = {
1488 .name = "omap2_mcspi",
1489 .owner = THIS_MODULE,
Benoit Coussond5a80032012-02-15 18:37:34 +01001490 .pm = &omap2_mcspi_pm_ops,
1491 .of_match_table = omap_mcspi_of_match,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001492 },
Felipe Balbi7d6b6d82012-03-14 11:18:30 +02001493 .probe = omap2_mcspi_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +00001494 .remove = omap2_mcspi_remove,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001495};
1496
Felipe Balbi9fdca9d2012-03-14 11:18:31 +02001497module_platform_driver(omap2_mcspi_driver);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001498MODULE_LICENSE("GPL");