blob: d80f81b6c84454fbce31dae6ac82484011f5a294 [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>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070041
42#include <linux/spi/spi.h>
43
Tony Lindgrence491cf2009-10-20 09:40:47 -070044#include <plat/clock.h>
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +000045#include <plat/mcspi.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070046
47#define OMAP2_MCSPI_MAX_FREQ 48000000
Shubhrajyoti D27b52842012-03-26 17:04:22 +053048#define SPI_AUTOSUSPEND_TIMEOUT 2000
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070049
50#define OMAP2_MCSPI_REVISION 0x00
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070051#define OMAP2_MCSPI_SYSSTATUS 0x14
52#define OMAP2_MCSPI_IRQSTATUS 0x18
53#define OMAP2_MCSPI_IRQENABLE 0x1c
54#define OMAP2_MCSPI_WAKEUPENABLE 0x20
55#define OMAP2_MCSPI_SYST 0x24
56#define OMAP2_MCSPI_MODULCTRL 0x28
57
58/* per-channel banks, 0x14 bytes each, first is: */
59#define OMAP2_MCSPI_CHCONF0 0x2c
60#define OMAP2_MCSPI_CHSTAT0 0x30
61#define OMAP2_MCSPI_CHCTRL0 0x34
62#define OMAP2_MCSPI_TX0 0x38
63#define OMAP2_MCSPI_RX0 0x3c
64
65/* per-register bitmasks: */
66
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)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070086
Jouni Hogander7a8fa722009-09-22 16:45:58 -070087#define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
88#define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
89#define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070090
Jouni Hogander7a8fa722009-09-22 16:45:58 -070091#define OMAP2_MCSPI_CHCTRL_EN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070092
Jouni Hogander7a8fa722009-09-22 16:45:58 -070093#define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070094
95/* We have 2 DMA channels per CS, one for RX and one for TX */
96struct omap2_mcspi_dma {
Russell King53741ed2012-04-23 13:51:48 +010097 struct dma_chan *dma_tx;
98 struct dma_chan *dma_rx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070099
100 int dma_tx_sync_dev;
101 int dma_rx_sync_dev;
102
103 struct completion dma_tx_completion;
104 struct completion dma_rx_completion;
105};
106
107/* use PIO for small transfers, avoiding DMA setup/teardown overhead and
108 * cache operations; better heuristics consider wordsize and bitrate.
109 */
Roman Tereshonkov8b66c132010-04-12 09:07:54 +0000110#define DMA_MIN_BYTES 160
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700111
112
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530113/*
114 * Used for context save and restore, structure members to be updated whenever
115 * corresponding registers are modified.
116 */
117struct omap2_mcspi_regs {
118 u32 modulctrl;
119 u32 wakeupenable;
120 struct list_head cs;
121};
122
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700123struct omap2_mcspi {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700124 struct spi_master *master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700125 /* Virtual base address of the controller */
126 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100127 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700128 /* SPI1 has 4 channels, while SPI2 has 2 */
129 struct omap2_mcspi_dma *dma_channels;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530130 struct device *dev;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530131 struct omap2_mcspi_regs ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700132};
133
134struct omap2_mcspi_cs {
135 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100136 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700137 int word_len;
Tero Kristo89c05372009-09-22 16:46:17 -0700138 struct list_head node;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700139 /* Context save and restore shadow register */
140 u32 chconf0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700141};
142
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700143#define MOD_REG_BIT(val, mask, set) do { \
144 if (set) \
145 val |= mask; \
146 else \
147 val &= ~mask; \
148} while (0)
149
150static inline void mcspi_write_reg(struct spi_master *master,
151 int idx, u32 val)
152{
153 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
154
155 __raw_writel(val, mcspi->base + idx);
156}
157
158static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
159{
160 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
161
162 return __raw_readl(mcspi->base + idx);
163}
164
165static inline void mcspi_write_cs_reg(const struct spi_device *spi,
166 int idx, u32 val)
167{
168 struct omap2_mcspi_cs *cs = spi->controller_state;
169
170 __raw_writel(val, cs->base + idx);
171}
172
173static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
174{
175 struct omap2_mcspi_cs *cs = spi->controller_state;
176
177 return __raw_readl(cs->base + idx);
178}
179
Hemanth Va41ae1a2009-09-22 16:46:16 -0700180static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
181{
182 struct omap2_mcspi_cs *cs = spi->controller_state;
183
184 return cs->chconf0;
185}
186
187static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
188{
189 struct omap2_mcspi_cs *cs = spi->controller_state;
190
191 cs->chconf0 = val;
192 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
Roman Tereshonkova330ce22010-03-15 09:06:28 +0000193 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700194}
195
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700196static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
197 int is_read, int enable)
198{
199 u32 l, rw;
200
Hemanth Va41ae1a2009-09-22 16:46:16 -0700201 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700202
203 if (is_read) /* 1 is read, 0 write */
204 rw = OMAP2_MCSPI_CHCONF_DMAR;
205 else
206 rw = OMAP2_MCSPI_CHCONF_DMAW;
207
208 MOD_REG_BIT(l, rw, enable);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700209 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700210}
211
212static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
213{
214 u32 l;
215
216 l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
217 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000218 /* Flash post-writes */
219 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700220}
221
222static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
223{
224 u32 l;
225
Hemanth Va41ae1a2009-09-22 16:46:16 -0700226 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700227 MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
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_master_mode(struct spi_master *master)
232{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530233 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
234 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700235 u32 l;
236
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530237 /*
238 * Setup when switching from (reset default) slave mode
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700239 * to single-channel master mode
240 */
241 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
242 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
243 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
244 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
245 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700246
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530247 ctx->modulctrl = l;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700248}
249
250static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
251{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530252 struct spi_master *spi_cntrl = mcspi->master;
253 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
254 struct omap2_mcspi_cs *cs;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700255
256 /* McSPI: context restore */
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530257 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
258 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700259
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530260 list_for_each_entry(cs, &ctx->cs, node)
Tero Kristo89c05372009-09-22 16:46:17 -0700261 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700262}
263static void omap2_mcspi_disable_clocks(struct omap2_mcspi *mcspi)
264{
Shubhrajyoti D27b52842012-03-26 17:04:22 +0530265 pm_runtime_mark_last_busy(mcspi->dev);
266 pm_runtime_put_autosuspend(mcspi->dev);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700267}
268
269static int omap2_mcspi_enable_clocks(struct omap2_mcspi *mcspi)
270{
Govindraj.R1f1a4382011-02-02 17:52:15 +0530271 return pm_runtime_get_sync(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700272}
273
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +0530274static int omap2_prepare_transfer(struct spi_master *master)
275{
276 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
277
278 pm_runtime_get_sync(mcspi->dev);
279 return 0;
280}
281
282static int omap2_unprepare_transfer(struct spi_master *master)
283{
284 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
285
286 pm_runtime_mark_last_busy(mcspi->dev);
287 pm_runtime_put_autosuspend(mcspi->dev);
288 return 0;
289}
290
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300291static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
292{
293 unsigned long timeout;
294
295 timeout = jiffies + msecs_to_jiffies(1000);
296 while (!(__raw_readl(reg) & bit)) {
297 if (time_after(jiffies, timeout))
298 return -1;
299 cpu_relax();
300 }
301 return 0;
302}
303
Russell King53741ed2012-04-23 13:51:48 +0100304static void omap2_mcspi_rx_callback(void *data)
305{
306 struct spi_device *spi = data;
307 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
308 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
309
310 complete(&mcspi_dma->dma_rx_completion);
311
312 /* We must disable the DMA RX request */
313 omap2_mcspi_set_dma_req(spi, 1, 0);
314}
315
316static void omap2_mcspi_tx_callback(void *data)
317{
318 struct spi_device *spi = data;
319 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
320 struct omap2_mcspi_dma *mcspi_dma = &mcspi->dma_channels[spi->chip_select];
321
322 complete(&mcspi_dma->dma_tx_completion);
323
324 /* We must disable the DMA TX request */
325 omap2_mcspi_set_dma_req(spi, 0, 0);
326}
327
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700328static unsigned
329omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
330{
331 struct omap2_mcspi *mcspi;
332 struct omap2_mcspi_cs *cs = spi->controller_state;
333 struct omap2_mcspi_dma *mcspi_dma;
Russell King8c7494a2012-04-23 13:56:25 +0100334 unsigned int count;
335 int word_len, element_count;
Govindraj.R8b20c8c2011-06-01 11:31:24 +0530336 int elements = 0;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000337 u32 l;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700338 u8 * rx;
339 const u8 * tx;
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300340 void __iomem *chstat_reg;
Russell King53741ed2012-04-23 13:51:48 +0100341 struct dma_slave_config cfg;
342 enum dma_slave_buswidth width;
343 unsigned es;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700344
345 mcspi = spi_master_get_devdata(spi->master);
346 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000347 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700348
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300349 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
350
Russell King53741ed2012-04-23 13:51:48 +0100351 if (cs->word_len <= 8) {
352 width = DMA_SLAVE_BUSWIDTH_1_BYTE;
353 es = 1;
354 } else if (cs->word_len <= 16) {
355 width = DMA_SLAVE_BUSWIDTH_2_BYTES;
356 es = 2;
357 } else {
358 width = DMA_SLAVE_BUSWIDTH_4_BYTES;
359 es = 4;
360 }
361
362 memset(&cfg, 0, sizeof(cfg));
363 cfg.src_addr = cs->phys + OMAP2_MCSPI_RX0;
364 cfg.dst_addr = cs->phys + OMAP2_MCSPI_TX0;
365 cfg.src_addr_width = width;
366 cfg.dst_addr_width = width;
367 cfg.src_maxburst = 1;
368 cfg.dst_maxburst = 1;
369
370 if (xfer->tx_buf && mcspi_dma->dma_tx) {
371 struct dma_async_tx_descriptor *tx;
372 struct scatterlist sg;
373
374 dmaengine_slave_config(mcspi_dma->dma_tx, &cfg);
375
376 sg_init_table(&sg, 1);
377 sg_dma_address(&sg) = xfer->tx_dma;
378 sg_dma_len(&sg) = xfer->len;
379
380 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_tx, &sg, 1,
381 DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
382 if (tx) {
383 tx->callback = omap2_mcspi_tx_callback;
384 tx->callback_param = spi;
385 dmaengine_submit(tx);
386 } else {
387 /* FIXME: fall back to PIO? */
388 }
389 }
390
391 if (xfer->rx_buf && mcspi_dma->dma_rx) {
392 struct dma_async_tx_descriptor *tx;
393 struct scatterlist sg;
394 size_t len = xfer->len - es;
395
396 dmaengine_slave_config(mcspi_dma->dma_rx, &cfg);
397
398 if (l & OMAP2_MCSPI_CHCONF_TURBO)
399 len -= es;
400
401 sg_init_table(&sg, 1);
402 sg_dma_address(&sg) = xfer->rx_dma;
403 sg_dma_len(&sg) = len;
404
405 tx = dmaengine_prep_slave_sg(mcspi_dma->dma_rx, &sg, 1,
406 DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
407 if (tx) {
408 tx->callback = omap2_mcspi_rx_callback;
409 tx->callback_param = spi;
410 dmaengine_submit(tx);
411 } else {
412 /* FIXME: fall back to PIO? */
413 }
414 }
415
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700416 count = xfer->len;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700417 word_len = cs->word_len;
418
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700419 rx = xfer->rx_buf;
420 tx = xfer->tx_buf;
421
422 if (word_len <= 8) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700423 element_count = count;
424 } else if (word_len <= 16) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700425 element_count = count >> 1;
426 } else /* word_len <= 32 */ {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700427 element_count = count >> 2;
428 }
429
430 if (tx != NULL) {
Russell King8c7494a2012-04-23 13:56:25 +0100431 dma_async_issue_pending(mcspi_dma->dma_tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700432 omap2_mcspi_set_dma_req(spi, 0, 1);
433 }
434
435 if (rx != NULL) {
Russell King8c7494a2012-04-23 13:56:25 +0100436 dma_async_issue_pending(mcspi_dma->dma_rx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700437 omap2_mcspi_set_dma_req(spi, 1, 1);
438 }
439
440 if (tx != NULL) {
441 wait_for_completion(&mcspi_dma->dma_tx_completion);
Shubhrajyoti Da3ce9a82012-07-19 23:16:52 +0530442 dma_unmap_single(mcspi->dev, xfer->tx_dma, count,
443 DMA_TO_DEVICE);
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300444
445 /* for TX_ONLY mode, be sure all words have shifted out */
446 if (rx == NULL) {
447 if (mcspi_wait_for_reg_bit(chstat_reg,
448 OMAP2_MCSPI_CHSTAT_TXS) < 0)
449 dev_err(&spi->dev, "TXS timed out\n");
450 else if (mcspi_wait_for_reg_bit(chstat_reg,
451 OMAP2_MCSPI_CHSTAT_EOT) < 0)
452 dev_err(&spi->dev, "EOT timed out\n");
453 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700454 }
455
456 if (rx != NULL) {
457 wait_for_completion(&mcspi_dma->dma_rx_completion);
Shubhrajyoti Da3ce9a82012-07-19 23:16:52 +0530458 dma_unmap_single(mcspi->dev, xfer->rx_dma, count,
459 DMA_FROM_DEVICE);
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700460 omap2_mcspi_set_enable(spi, 0);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000461
Russell King53741ed2012-04-23 13:51:48 +0100462 elements = element_count - 1;
463
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000464 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
Russell King53741ed2012-04-23 13:51:48 +0100465 elements--;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000466
467 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
468 & OMAP2_MCSPI_CHSTAT_RXS)) {
469 u32 w;
470
471 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
472 if (word_len <= 8)
473 ((u8 *)xfer->rx_buf)[elements++] = w;
474 else if (word_len <= 16)
475 ((u16 *)xfer->rx_buf)[elements++] = w;
476 else /* word_len <= 32 */
477 ((u32 *)xfer->rx_buf)[elements++] = w;
478 } else {
479 dev_err(&spi->dev,
480 "DMA RX penultimate word empty");
481 count -= (word_len <= 8) ? 2 :
482 (word_len <= 16) ? 4 :
483 /* word_len <= 32 */ 8;
484 omap2_mcspi_set_enable(spi, 1);
485 return count;
486 }
487 }
488
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700489 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
490 & OMAP2_MCSPI_CHSTAT_RXS)) {
491 u32 w;
492
493 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
494 if (word_len <= 8)
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000495 ((u8 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700496 else if (word_len <= 16)
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000497 ((u16 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700498 else /* word_len <= 32 */
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000499 ((u32 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700500 } else {
501 dev_err(&spi->dev, "DMA RX last word empty");
502 count -= (word_len <= 8) ? 1 :
503 (word_len <= 16) ? 2 :
504 /* word_len <= 32 */ 4;
505 }
506 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700507 }
508 return count;
509}
510
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700511static unsigned
512omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
513{
514 struct omap2_mcspi *mcspi;
515 struct omap2_mcspi_cs *cs = spi->controller_state;
516 unsigned int count, c;
517 u32 l;
518 void __iomem *base = cs->base;
519 void __iomem *tx_reg;
520 void __iomem *rx_reg;
521 void __iomem *chstat_reg;
522 int word_len;
523
524 mcspi = spi_master_get_devdata(spi->master);
525 count = xfer->len;
526 c = count;
527 word_len = cs->word_len;
528
Hemanth Va41ae1a2009-09-22 16:46:16 -0700529 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700530
531 /* We store the pre-calculated register addresses on stack to speed
532 * up the transfer loop. */
533 tx_reg = base + OMAP2_MCSPI_TX0;
534 rx_reg = base + OMAP2_MCSPI_RX0;
535 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
536
Michael Jonesadef6582011-02-25 16:55:11 +0100537 if (c < (word_len>>3))
538 return 0;
539
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700540 if (word_len <= 8) {
541 u8 *rx;
542 const u8 *tx;
543
544 rx = xfer->rx_buf;
545 tx = xfer->tx_buf;
546
547 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800548 c -= 1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700549 if (tx != NULL) {
550 if (mcspi_wait_for_reg_bit(chstat_reg,
551 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
552 dev_err(&spi->dev, "TXS timed out\n");
553 goto out;
554 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900555 dev_vdbg(&spi->dev, "write-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700556 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700557 __raw_writel(*tx++, tx_reg);
558 }
559 if (rx != NULL) {
560 if (mcspi_wait_for_reg_bit(chstat_reg,
561 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
562 dev_err(&spi->dev, "RXS timed out\n");
563 goto out;
564 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000565
566 if (c == 1 && tx == NULL &&
567 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
568 omap2_mcspi_set_enable(spi, 0);
569 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900570 dev_vdbg(&spi->dev, "read-%d %02x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000571 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000572 if (mcspi_wait_for_reg_bit(chstat_reg,
573 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
574 dev_err(&spi->dev,
575 "RXS timed out\n");
576 goto out;
577 }
578 c = 0;
579 } else if (c == 0 && tx == NULL) {
580 omap2_mcspi_set_enable(spi, 0);
581 }
582
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700583 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900584 dev_vdbg(&spi->dev, "read-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700585 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700586 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200587 } while (c);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700588 } else if (word_len <= 16) {
589 u16 *rx;
590 const u16 *tx;
591
592 rx = xfer->rx_buf;
593 tx = xfer->tx_buf;
594 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800595 c -= 2;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700596 if (tx != NULL) {
597 if (mcspi_wait_for_reg_bit(chstat_reg,
598 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
599 dev_err(&spi->dev, "TXS timed out\n");
600 goto out;
601 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900602 dev_vdbg(&spi->dev, "write-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700603 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700604 __raw_writel(*tx++, tx_reg);
605 }
606 if (rx != NULL) {
607 if (mcspi_wait_for_reg_bit(chstat_reg,
608 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
609 dev_err(&spi->dev, "RXS timed out\n");
610 goto out;
611 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000612
613 if (c == 2 && tx == NULL &&
614 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
615 omap2_mcspi_set_enable(spi, 0);
616 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900617 dev_vdbg(&spi->dev, "read-%d %04x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000618 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000619 if (mcspi_wait_for_reg_bit(chstat_reg,
620 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
621 dev_err(&spi->dev,
622 "RXS timed out\n");
623 goto out;
624 }
625 c = 0;
626 } else if (c == 0 && tx == NULL) {
627 omap2_mcspi_set_enable(spi, 0);
628 }
629
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700630 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900631 dev_vdbg(&spi->dev, "read-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700632 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700633 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200634 } while (c >= 2);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700635 } else if (word_len <= 32) {
636 u32 *rx;
637 const u32 *tx;
638
639 rx = xfer->rx_buf;
640 tx = xfer->tx_buf;
641 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800642 c -= 4;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700643 if (tx != NULL) {
644 if (mcspi_wait_for_reg_bit(chstat_reg,
645 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
646 dev_err(&spi->dev, "TXS timed out\n");
647 goto out;
648 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900649 dev_vdbg(&spi->dev, "write-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700650 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700651 __raw_writel(*tx++, tx_reg);
652 }
653 if (rx != NULL) {
654 if (mcspi_wait_for_reg_bit(chstat_reg,
655 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
656 dev_err(&spi->dev, "RXS timed out\n");
657 goto out;
658 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000659
660 if (c == 4 && tx == NULL &&
661 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
662 omap2_mcspi_set_enable(spi, 0);
663 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900664 dev_vdbg(&spi->dev, "read-%d %08x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000665 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000666 if (mcspi_wait_for_reg_bit(chstat_reg,
667 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
668 dev_err(&spi->dev,
669 "RXS timed out\n");
670 goto out;
671 }
672 c = 0;
673 } else if (c == 0 && tx == NULL) {
674 omap2_mcspi_set_enable(spi, 0);
675 }
676
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700677 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900678 dev_vdbg(&spi->dev, "read-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700679 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700680 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200681 } while (c >= 4);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700682 }
683
684 /* for TX_ONLY mode, be sure all words have shifted out */
685 if (xfer->rx_buf == NULL) {
686 if (mcspi_wait_for_reg_bit(chstat_reg,
687 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
688 dev_err(&spi->dev, "TXS timed out\n");
689 } else if (mcspi_wait_for_reg_bit(chstat_reg,
690 OMAP2_MCSPI_CHSTAT_EOT) < 0)
691 dev_err(&spi->dev, "EOT timed out\n");
Jason Wange1993ed2010-10-19 18:03:27 +0800692
693 /* disable chan to purge rx datas received in TX_ONLY transfer,
694 * otherwise these rx datas will affect the direct following
695 * RX_ONLY transfer.
696 */
697 omap2_mcspi_set_enable(spi, 0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700698 }
699out:
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000700 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700701 return count - c;
702}
703
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200704static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
705{
706 u32 div;
707
708 for (div = 0; div < 15; div++)
709 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
710 return div;
711
712 return 15;
713}
714
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700715/* called only when no transfer is active to this device */
716static int omap2_mcspi_setup_transfer(struct spi_device *spi,
717 struct spi_transfer *t)
718{
719 struct omap2_mcspi_cs *cs = spi->controller_state;
720 struct omap2_mcspi *mcspi;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700721 struct spi_master *spi_cntrl;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700722 u32 l = 0, div = 0;
723 u8 word_len = spi->bits_per_word;
Scott Ellis9bd45172010-03-10 14:23:13 -0700724 u32 speed_hz = spi->max_speed_hz;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700725
726 mcspi = spi_master_get_devdata(spi->master);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700727 spi_cntrl = mcspi->master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700728
729 if (t != NULL && t->bits_per_word)
730 word_len = t->bits_per_word;
731
732 cs->word_len = word_len;
733
Scott Ellis9bd45172010-03-10 14:23:13 -0700734 if (t && t->speed_hz)
735 speed_hz = t->speed_hz;
736
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200737 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
738 div = omap2_mcspi_calc_divisor(speed_hz);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700739
Hemanth Va41ae1a2009-09-22 16:46:16 -0700740 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700741
742 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
743 * REVISIT: this controller could support SPI_3WIRE mode.
744 */
745 l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
746 l |= OMAP2_MCSPI_CHCONF_DPE0;
747
748 /* wordlength */
749 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
750 l |= (word_len - 1) << 7;
751
752 /* set chipselect polarity; manage with FORCE */
753 if (!(spi->mode & SPI_CS_HIGH))
754 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
755 else
756 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
757
758 /* set clock divisor */
759 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
760 l |= div << 2;
761
762 /* set SPI mode 0..3 */
763 if (spi->mode & SPI_CPOL)
764 l |= OMAP2_MCSPI_CHCONF_POL;
765 else
766 l &= ~OMAP2_MCSPI_CHCONF_POL;
767 if (spi->mode & SPI_CPHA)
768 l |= OMAP2_MCSPI_CHCONF_PHA;
769 else
770 l &= ~OMAP2_MCSPI_CHCONF_PHA;
771
Hemanth Va41ae1a2009-09-22 16:46:16 -0700772 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700773
774 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200775 OMAP2_MCSPI_MAX_FREQ >> div,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700776 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
777 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
778
779 return 0;
780}
781
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700782static int omap2_mcspi_request_dma(struct spi_device *spi)
783{
784 struct spi_master *master = spi->master;
785 struct omap2_mcspi *mcspi;
786 struct omap2_mcspi_dma *mcspi_dma;
Russell King53741ed2012-04-23 13:51:48 +0100787 dma_cap_mask_t mask;
788 unsigned sig;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700789
790 mcspi = spi_master_get_devdata(master);
791 mcspi_dma = mcspi->dma_channels + spi->chip_select;
792
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700793 init_completion(&mcspi_dma->dma_rx_completion);
794 init_completion(&mcspi_dma->dma_tx_completion);
795
Russell King53741ed2012-04-23 13:51:48 +0100796 dma_cap_zero(mask);
797 dma_cap_set(DMA_SLAVE, mask);
Russell King53741ed2012-04-23 13:51:48 +0100798 sig = mcspi_dma->dma_rx_sync_dev;
799 mcspi_dma->dma_rx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
800 if (!mcspi_dma->dma_rx) {
801 dev_err(&spi->dev, "no RX DMA engine channel for McSPI\n");
802 return -EAGAIN;
803 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700804
Russell King53741ed2012-04-23 13:51:48 +0100805 sig = mcspi_dma->dma_tx_sync_dev;
806 mcspi_dma->dma_tx = dma_request_channel(mask, omap_dma_filter_fn, &sig);
807 if (!mcspi_dma->dma_tx) {
808 dev_err(&spi->dev, "no TX DMA engine channel for McSPI\n");
809 dma_release_channel(mcspi_dma->dma_rx);
810 mcspi_dma->dma_rx = NULL;
811 return -EAGAIN;
812 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700813
814 return 0;
815}
816
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700817static int omap2_mcspi_setup(struct spi_device *spi)
818{
819 int ret;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530820 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
821 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700822 struct omap2_mcspi_dma *mcspi_dma;
823 struct omap2_mcspi_cs *cs = spi->controller_state;
824
David Brownell7d077192009-06-17 16:26:03 -0700825 if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700826 dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
827 spi->bits_per_word);
828 return -EINVAL;
829 }
830
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700831 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
832
833 if (!cs) {
Russell King10aa5a32012-06-18 11:27:04 +0100834 cs = kzalloc(sizeof *cs, GFP_KERNEL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700835 if (!cs)
836 return -ENOMEM;
837 cs->base = mcspi->base + spi->chip_select * 0x14;
Russell Kinge5480b732008-09-01 21:51:50 +0100838 cs->phys = mcspi->phys + spi->chip_select * 0x14;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700839 cs->chconf0 = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700840 spi->controller_state = cs;
Tero Kristo89c05372009-09-22 16:46:17 -0700841 /* Link this to context save list */
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530842 list_add_tail(&cs->node, &ctx->cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700843 }
844
Russell King8c7494a2012-04-23 13:56:25 +0100845 if (!mcspi_dma->dma_rx || !mcspi_dma->dma_tx) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700846 ret = omap2_mcspi_request_dma(spi);
847 if (ret < 0)
848 return ret;
849 }
850
Govindraj.R1f1a4382011-02-02 17:52:15 +0530851 ret = omap2_mcspi_enable_clocks(mcspi);
852 if (ret < 0)
853 return ret;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700854
Kyungmin Park86eeb6f2007-10-16 01:27:45 -0700855 ret = omap2_mcspi_setup_transfer(spi, NULL);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700856 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700857
858 return ret;
859}
860
861static void omap2_mcspi_cleanup(struct spi_device *spi)
862{
863 struct omap2_mcspi *mcspi;
864 struct omap2_mcspi_dma *mcspi_dma;
Tero Kristo89c05372009-09-22 16:46:17 -0700865 struct omap2_mcspi_cs *cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700866
867 mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700868
Scott Ellis5e774942010-03-10 14:22:45 -0700869 if (spi->controller_state) {
870 /* Unlink controller state from context save list */
871 cs = spi->controller_state;
872 list_del(&cs->node);
Tero Kristo89c05372009-09-22 16:46:17 -0700873
Russell King10aa5a32012-06-18 11:27:04 +0100874 kfree(cs);
Scott Ellis5e774942010-03-10 14:22:45 -0700875 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700876
Scott Ellis99f1a432010-05-24 14:20:27 +0000877 if (spi->chip_select < spi->master->num_chipselect) {
878 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
879
Russell King53741ed2012-04-23 13:51:48 +0100880 if (mcspi_dma->dma_rx) {
881 dma_release_channel(mcspi_dma->dma_rx);
882 mcspi_dma->dma_rx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +0000883 }
Russell King53741ed2012-04-23 13:51:48 +0100884 if (mcspi_dma->dma_tx) {
885 dma_release_channel(mcspi_dma->dma_tx);
886 mcspi_dma->dma_tx = NULL;
Scott Ellis99f1a432010-05-24 14:20:27 +0000887 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700888 }
889}
890
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +0530891static void omap2_mcspi_work(struct omap2_mcspi *mcspi, struct spi_message *m)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700892{
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700893
894 /* We only enable one channel at a time -- the one whose message is
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +0530895 * -- although this controller would gladly
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700896 * arbitrate among multiple channels. This corresponds to "single
897 * channel" master mode. As a side effect, we need to manage the
898 * chipselect with the FORCE bit ... CS != channel enable.
899 */
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700900
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +0530901 struct spi_device *spi;
902 struct spi_transfer *t = NULL;
903 int cs_active = 0;
904 struct omap2_mcspi_cs *cs;
905 struct omap2_mcspi_device_config *cd;
906 int par_override = 0;
907 int status = 0;
908 u32 chconf;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700909
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +0530910 spi = m->spi;
911 cs = spi->controller_state;
912 cd = spi->controller_data;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700913
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +0530914 omap2_mcspi_set_enable(spi, 1);
915 list_for_each_entry(t, &m->transfers, transfer_list) {
916 if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
917 status = -EINVAL;
918 break;
919 }
920 if (par_override || t->speed_hz || t->bits_per_word) {
921 par_override = 1;
922 status = omap2_mcspi_setup_transfer(spi, t);
923 if (status < 0)
924 break;
925 if (!t->speed_hz && !t->bits_per_word)
926 par_override = 0;
927 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700928
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +0530929 if (!cs_active) {
930 omap2_mcspi_force_cs(spi, 1);
931 cs_active = 1;
932 }
933
934 chconf = mcspi_cached_chconf0(spi);
935 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
936 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
937
938 if (t->tx_buf == NULL)
939 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
940 else if (t->rx_buf == NULL)
941 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
942
943 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
944 /* Turbo mode is for more than one word */
945 if (t->len > ((cs->word_len + 7) >> 3))
946 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
947 }
948
949 mcspi_write_chconf0(spi, chconf);
950
951 if (t->len) {
952 unsigned count;
953
954 /* RX_ONLY mode needs dummy data in TX reg */
955 if (t->tx_buf == NULL)
956 __raw_writel(0, cs->base
957 + OMAP2_MCSPI_TX0);
958
959 if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
960 count = omap2_mcspi_txrx_dma(spi, t);
961 else
962 count = omap2_mcspi_txrx_pio(spi, t);
963 m->actual_length += count;
964
965 if (count != t->len) {
966 status = -EIO;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700967 break;
968 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700969 }
970
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +0530971 if (t->delay_usecs)
972 udelay(t->delay_usecs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700973
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +0530974 /* ignore the "leave it on after last xfer" hint */
975 if (t->cs_change) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700976 omap2_mcspi_force_cs(spi, 0);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +0530977 cs_active = 0;
978 }
979 }
980 /* Restore defaults if they were overriden */
981 if (par_override) {
982 par_override = 0;
983 status = omap2_mcspi_setup_transfer(spi, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700984 }
985
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +0530986 if (cs_active)
987 omap2_mcspi_force_cs(spi, 0);
Govindraj.R1f1a4382011-02-02 17:52:15 +0530988
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +0530989 omap2_mcspi_set_enable(spi, 0);
990
991 m->status = status;
992
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700993}
994
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +0530995static int omap2_mcspi_transfer_one_message(struct spi_master *master,
996 struct spi_message *m)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700997{
998 struct omap2_mcspi *mcspi;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700999 struct spi_transfer *t;
1000
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301001 mcspi = spi_master_get_devdata(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001002 m->actual_length = 0;
1003 m->status = 0;
1004
1005 /* reject invalid messages and transfers */
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301006 if (list_empty(&m->transfers))
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001007 return -EINVAL;
1008 list_for_each_entry(t, &m->transfers, transfer_list) {
1009 const void *tx_buf = t->tx_buf;
1010 void *rx_buf = t->rx_buf;
1011 unsigned len = t->len;
1012
1013 if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
1014 || (len && !(rx_buf || tx_buf))
1015 || (t->bits_per_word &&
1016 ( t->bits_per_word < 4
1017 || t->bits_per_word > 32))) {
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301018 dev_dbg(mcspi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001019 t->speed_hz,
1020 len,
1021 tx_buf ? "tx" : "",
1022 rx_buf ? "rx" : "",
1023 t->bits_per_word);
1024 return -EINVAL;
1025 }
Hannu Heikkinen57d9c102011-02-24 21:31:33 +02001026 if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301027 dev_dbg(mcspi->dev, "speed_hz %d below minimum %d Hz\n",
Hannu Heikkinen57d9c102011-02-24 21:31:33 +02001028 t->speed_hz,
1029 OMAP2_MCSPI_MAX_FREQ >> 15);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001030 return -EINVAL;
1031 }
1032
1033 if (m->is_dma_mapped || len < DMA_MIN_BYTES)
1034 continue;
1035
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001036 if (tx_buf != NULL) {
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301037 t->tx_dma = dma_map_single(mcspi->dev, (void *) tx_buf,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001038 len, DMA_TO_DEVICE);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301039 if (dma_mapping_error(mcspi->dev, t->tx_dma)) {
1040 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001041 'T', len);
1042 return -EINVAL;
1043 }
1044 }
1045 if (rx_buf != NULL) {
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301046 t->rx_dma = dma_map_single(mcspi->dev, rx_buf, t->len,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001047 DMA_FROM_DEVICE);
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301048 if (dma_mapping_error(mcspi->dev, t->rx_dma)) {
1049 dev_dbg(mcspi->dev, "dma %cX %d bytes error\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001050 'R', len);
1051 if (tx_buf != NULL)
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301052 dma_unmap_single(mcspi->dev, t->tx_dma,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001053 len, DMA_TO_DEVICE);
1054 return -EINVAL;
1055 }
1056 }
1057 }
1058
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301059 omap2_mcspi_work(mcspi, m);
1060 spi_finalize_current_message(master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001061 return 0;
1062}
1063
Arnd Bergmann24ab32752012-07-20 16:01:43 +05301064static int __devinit omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001065{
1066 struct spi_master *master = mcspi->master;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301067 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301068 int ret = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001069
Govindraj.R1f1a4382011-02-02 17:52:15 +05301070 ret = omap2_mcspi_enable_clocks(mcspi);
1071 if (ret < 0)
1072 return ret;
Jouni Hoganderddb22192009-07-29 15:02:11 -07001073
Shubhrajyoti D39f80522012-03-29 22:11:07 +05301074 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE,
1075 OMAP2_MCSPI_WAKEUPENABLE_WKEN);
1076 ctx->wakeupenable = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001077
1078 omap2_mcspi_set_master_mode(master);
Hemanth Va41ae1a2009-09-22 16:46:16 -07001079 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001080 return 0;
1081}
1082
Govindraj.R1f1a4382011-02-02 17:52:15 +05301083static int omap_mcspi_runtime_resume(struct device *dev)
1084{
1085 struct omap2_mcspi *mcspi;
1086 struct spi_master *master;
1087
1088 master = dev_get_drvdata(dev);
1089 mcspi = spi_master_get_devdata(master);
1090 omap2_mcspi_restore_ctx(mcspi);
1091
1092 return 0;
1093}
1094
Benoit Coussond5a80032012-02-15 18:37:34 +01001095static struct omap2_mcspi_platform_config omap2_pdata = {
1096 .regs_offset = 0,
1097};
1098
1099static struct omap2_mcspi_platform_config omap4_pdata = {
1100 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1101};
1102
1103static const struct of_device_id omap_mcspi_of_match[] = {
1104 {
1105 .compatible = "ti,omap2-mcspi",
1106 .data = &omap2_pdata,
1107 },
1108 {
1109 .compatible = "ti,omap4-mcspi",
1110 .data = &omap4_pdata,
1111 },
1112 { },
1113};
1114MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
Girishccc7bae2008-02-06 01:38:16 -08001115
Felipe Balbi7d6b6d82012-03-14 11:18:30 +02001116static int __devinit omap2_mcspi_probe(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001117{
1118 struct spi_master *master;
Uwe Kleine-König83a01e72012-05-21 21:57:39 +02001119 const struct omap2_mcspi_platform_config *pdata;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001120 struct omap2_mcspi *mcspi;
1121 struct resource *r;
1122 int status = 0, i;
Benoit Coussond5a80032012-02-15 18:37:34 +01001123 u32 regs_offset = 0;
1124 static int bus_num = 1;
1125 struct device_node *node = pdev->dev.of_node;
1126 const struct of_device_id *match;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001127
1128 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1129 if (master == NULL) {
1130 dev_dbg(&pdev->dev, "master allocation failed\n");
1131 return -ENOMEM;
1132 }
1133
David Brownelle7db06b2009-06-17 16:26:04 -07001134 /* the spi->mode bits understood by this driver: */
1135 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1136
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001137 master->setup = omap2_mcspi_setup;
Shubhrajyoti D5fda88f2012-05-10 18:27:45 +05301138 master->prepare_transfer_hardware = omap2_prepare_transfer;
1139 master->unprepare_transfer_hardware = omap2_unprepare_transfer;
1140 master->transfer_one_message = omap2_mcspi_transfer_one_message;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001141 master->cleanup = omap2_mcspi_cleanup;
Benoit Coussond5a80032012-02-15 18:37:34 +01001142 master->dev.of_node = node;
1143
1144 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1145 if (match) {
1146 u32 num_cs = 1; /* default number of chipselect */
1147 pdata = match->data;
1148
1149 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1150 master->num_chipselect = num_cs;
1151 master->bus_num = bus_num++;
1152 } else {
1153 pdata = pdev->dev.platform_data;
1154 master->num_chipselect = pdata->num_cs;
1155 if (pdev->id != -1)
1156 master->bus_num = pdev->id;
1157 }
1158 regs_offset = pdata->regs_offset;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001159
1160 dev_set_drvdata(&pdev->dev, master);
1161
1162 mcspi = spi_master_get_devdata(master);
1163 mcspi->master = master;
1164
1165 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1166 if (r == NULL) {
1167 status = -ENODEV;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301168 goto free_master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001169 }
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301170
Benoit Coussond5a80032012-02-15 18:37:34 +01001171 r->start += regs_offset;
1172 r->end += regs_offset;
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301173 mcspi->phys = r->start;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001174
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301175 mcspi->base = devm_request_and_ioremap(&pdev->dev, r);
Russell King55c381e2008-09-04 14:07:22 +01001176 if (!mcspi->base) {
1177 dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
1178 status = -ENOMEM;
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301179 goto free_master;
Russell King55c381e2008-09-04 14:07:22 +01001180 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001181
Govindraj.R1f1a4382011-02-02 17:52:15 +05301182 mcspi->dev = &pdev->dev;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001183
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301184 INIT_LIST_HEAD(&mcspi->ctx.cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001185
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001186 mcspi->dma_channels = kcalloc(master->num_chipselect,
1187 sizeof(struct omap2_mcspi_dma),
1188 GFP_KERNEL);
1189
1190 if (mcspi->dma_channels == NULL)
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301191 goto free_master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001192
Charulatha V1a5d8192011-02-02 17:52:14 +05301193 for (i = 0; i < master->num_chipselect; i++) {
1194 char dma_ch_name[14];
1195 struct resource *dma_res;
1196
1197 sprintf(dma_ch_name, "rx%d", i);
1198 dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1199 dma_ch_name);
1200 if (!dma_res) {
1201 dev_dbg(&pdev->dev, "cannot get DMA RX channel\n");
1202 status = -ENODEV;
1203 break;
1204 }
1205
Charulatha V1a5d8192011-02-02 17:52:14 +05301206 mcspi->dma_channels[i].dma_rx_sync_dev = dma_res->start;
1207 sprintf(dma_ch_name, "tx%d", i);
1208 dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1209 dma_ch_name);
1210 if (!dma_res) {
1211 dev_dbg(&pdev->dev, "cannot get DMA TX channel\n");
1212 status = -ENODEV;
1213 break;
1214 }
1215
Charulatha V1a5d8192011-02-02 17:52:14 +05301216 mcspi->dma_channels[i].dma_tx_sync_dev = dma_res->start;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001217 }
1218
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301219 if (status < 0)
1220 goto dma_chnl_free;
1221
Shubhrajyoti D27b52842012-03-26 17:04:22 +05301222 pm_runtime_use_autosuspend(&pdev->dev);
1223 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
Govindraj.R1f1a4382011-02-02 17:52:15 +05301224 pm_runtime_enable(&pdev->dev);
1225
1226 if (status || omap2_mcspi_master_setup(mcspi) < 0)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301227 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001228
1229 status = spi_register_master(master);
1230 if (status < 0)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301231 goto err_spi_register;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001232
1233 return status;
1234
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301235err_spi_register:
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001236 spi_master_put(master);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301237disable_pm:
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301238 pm_runtime_disable(&pdev->dev);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301239dma_chnl_free:
Govindraj.R1f1a4382011-02-02 17:52:15 +05301240 kfree(mcspi->dma_channels);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301241free_master:
1242 kfree(master);
1243 platform_set_drvdata(pdev, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001244 return status;
1245}
1246
Felipe Balbi7d6b6d82012-03-14 11:18:30 +02001247static int __devexit omap2_mcspi_remove(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001248{
1249 struct spi_master *master;
1250 struct omap2_mcspi *mcspi;
1251 struct omap2_mcspi_dma *dma_channels;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001252
1253 master = dev_get_drvdata(&pdev->dev);
1254 mcspi = spi_master_get_devdata(master);
1255 dma_channels = mcspi->dma_channels;
1256
Govindraj.R1f1a4382011-02-02 17:52:15 +05301257 omap2_mcspi_disable_clocks(mcspi);
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301258 pm_runtime_disable(&pdev->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001259
1260 spi_unregister_master(master);
1261 kfree(dma_channels);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301262 platform_set_drvdata(pdev, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001263
1264 return 0;
1265}
1266
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001267/* work with hotplug and coldplug */
1268MODULE_ALIAS("platform:omap2_mcspi");
1269
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001270#ifdef CONFIG_SUSPEND
1271/*
1272 * When SPI wake up from off-mode, CS is in activate state. If it was in
1273 * unactive state when driver was suspend, then force it to unactive state at
1274 * wake up.
1275 */
1276static int omap2_mcspi_resume(struct device *dev)
1277{
1278 struct spi_master *master = dev_get_drvdata(dev);
1279 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301280 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1281 struct omap2_mcspi_cs *cs;
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001282
1283 omap2_mcspi_enable_clocks(mcspi);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301284 list_for_each_entry(cs, &ctx->cs, node) {
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001285 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001286 /*
1287 * We need to toggle CS state for OMAP take this
1288 * change in account.
1289 */
1290 MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 1);
1291 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1292 MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 0);
1293 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1294 }
1295 }
1296 omap2_mcspi_disable_clocks(mcspi);
1297 return 0;
1298}
1299#else
1300#define omap2_mcspi_resume NULL
1301#endif
1302
1303static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1304 .resume = omap2_mcspi_resume,
Govindraj.R1f1a4382011-02-02 17:52:15 +05301305 .runtime_resume = omap_mcspi_runtime_resume,
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001306};
1307
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001308static struct platform_driver omap2_mcspi_driver = {
1309 .driver = {
1310 .name = "omap2_mcspi",
1311 .owner = THIS_MODULE,
Benoit Coussond5a80032012-02-15 18:37:34 +01001312 .pm = &omap2_mcspi_pm_ops,
1313 .of_match_table = omap_mcspi_of_match,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001314 },
Felipe Balbi7d6b6d82012-03-14 11:18:30 +02001315 .probe = omap2_mcspi_probe,
1316 .remove = __devexit_p(omap2_mcspi_remove),
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001317};
1318
Felipe Balbi9fdca9d2012-03-14 11:18:31 +02001319module_platform_driver(omap2_mcspi_driver);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001320MODULE_LICENSE("GPL");