blob: 1907ed2e2958944597e80db2b41bd4668c86a0ac [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>
31#include <linux/platform_device.h>
32#include <linux/err.h>
33#include <linux/clk.h>
34#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Govindraj.R1f1a4382011-02-02 17:52:15 +053036#include <linux/pm_runtime.h>
Benoit Coussond5a80032012-02-15 18:37:34 +010037#include <linux/of.h>
38#include <linux/of_device.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070039
40#include <linux/spi/spi.h>
41
Tony Lindgrence491cf2009-10-20 09:40:47 -070042#include <plat/dma.h>
43#include <plat/clock.h>
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +000044#include <plat/mcspi.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070045
46#define OMAP2_MCSPI_MAX_FREQ 48000000
47
48#define OMAP2_MCSPI_REVISION 0x00
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070049#define OMAP2_MCSPI_SYSSTATUS 0x14
50#define OMAP2_MCSPI_IRQSTATUS 0x18
51#define OMAP2_MCSPI_IRQENABLE 0x1c
52#define OMAP2_MCSPI_WAKEUPENABLE 0x20
53#define OMAP2_MCSPI_SYST 0x24
54#define OMAP2_MCSPI_MODULCTRL 0x28
55
56/* per-channel banks, 0x14 bytes each, first is: */
57#define OMAP2_MCSPI_CHCONF0 0x2c
58#define OMAP2_MCSPI_CHSTAT0 0x30
59#define OMAP2_MCSPI_CHCTRL0 0x34
60#define OMAP2_MCSPI_TX0 0x38
61#define OMAP2_MCSPI_RX0 0x3c
62
63/* per-register bitmasks: */
64
Jouni Hogander7a8fa722009-09-22 16:45:58 -070065#define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
66#define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
67#define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070068
Jouni Hogander7a8fa722009-09-22 16:45:58 -070069#define OMAP2_MCSPI_CHCONF_PHA BIT(0)
70#define OMAP2_MCSPI_CHCONF_POL BIT(1)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070071#define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070072#define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070073#define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070074#define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
75#define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070076#define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070077#define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
78#define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
79#define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
80#define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
81#define OMAP2_MCSPI_CHCONF_IS BIT(18)
82#define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
83#define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070084
Jouni Hogander7a8fa722009-09-22 16:45:58 -070085#define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
86#define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
87#define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070088
Jouni Hogander7a8fa722009-09-22 16:45:58 -070089#define OMAP2_MCSPI_CHCTRL_EN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070090
Jouni Hogander7a8fa722009-09-22 16:45:58 -070091#define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070092
93/* We have 2 DMA channels per CS, one for RX and one for TX */
94struct omap2_mcspi_dma {
95 int dma_tx_channel;
96 int dma_rx_channel;
97
98 int dma_tx_sync_dev;
99 int dma_rx_sync_dev;
100
101 struct completion dma_tx_completion;
102 struct completion dma_rx_completion;
103};
104
105/* use PIO for small transfers, avoiding DMA setup/teardown overhead and
106 * cache operations; better heuristics consider wordsize and bitrate.
107 */
Roman Tereshonkov8b66c132010-04-12 09:07:54 +0000108#define DMA_MIN_BYTES 160
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700109
110
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530111/*
112 * Used for context save and restore, structure members to be updated whenever
113 * corresponding registers are modified.
114 */
115struct omap2_mcspi_regs {
116 u32 modulctrl;
117 u32 wakeupenable;
118 struct list_head cs;
119};
120
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700121struct omap2_mcspi {
122 struct work_struct work;
123 /* lock protects queue and registers */
124 spinlock_t lock;
125 struct list_head msg_queue;
126 struct spi_master *master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700127 /* Virtual base address of the controller */
128 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100129 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700130 /* SPI1 has 4 channels, while SPI2 has 2 */
131 struct omap2_mcspi_dma *dma_channels;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530132 struct device *dev;
Shubhrajyoti D2856ac12011-10-28 17:14:17 +0530133 struct workqueue_struct *wq;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530134 struct omap2_mcspi_regs ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700135};
136
137struct omap2_mcspi_cs {
138 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100139 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700140 int word_len;
Tero Kristo89c05372009-09-22 16:46:17 -0700141 struct list_head node;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700142 /* Context save and restore shadow register */
143 u32 chconf0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700144};
145
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700146#define MOD_REG_BIT(val, mask, set) do { \
147 if (set) \
148 val |= mask; \
149 else \
150 val &= ~mask; \
151} while (0)
152
153static inline void mcspi_write_reg(struct spi_master *master,
154 int idx, u32 val)
155{
156 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
157
158 __raw_writel(val, mcspi->base + idx);
159}
160
161static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
162{
163 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
164
165 return __raw_readl(mcspi->base + idx);
166}
167
168static inline void mcspi_write_cs_reg(const struct spi_device *spi,
169 int idx, u32 val)
170{
171 struct omap2_mcspi_cs *cs = spi->controller_state;
172
173 __raw_writel(val, cs->base + idx);
174}
175
176static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
177{
178 struct omap2_mcspi_cs *cs = spi->controller_state;
179
180 return __raw_readl(cs->base + idx);
181}
182
Hemanth Va41ae1a2009-09-22 16:46:16 -0700183static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
184{
185 struct omap2_mcspi_cs *cs = spi->controller_state;
186
187 return cs->chconf0;
188}
189
190static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
191{
192 struct omap2_mcspi_cs *cs = spi->controller_state;
193
194 cs->chconf0 = val;
195 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
Roman Tereshonkova330ce22010-03-15 09:06:28 +0000196 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700197}
198
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700199static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
200 int is_read, int enable)
201{
202 u32 l, rw;
203
Hemanth Va41ae1a2009-09-22 16:46:16 -0700204 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700205
206 if (is_read) /* 1 is read, 0 write */
207 rw = OMAP2_MCSPI_CHCONF_DMAR;
208 else
209 rw = OMAP2_MCSPI_CHCONF_DMAW;
210
211 MOD_REG_BIT(l, rw, enable);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700212 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700213}
214
215static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
216{
217 u32 l;
218
219 l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
220 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000221 /* Flash post-writes */
222 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700223}
224
225static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
226{
227 u32 l;
228
Hemanth Va41ae1a2009-09-22 16:46:16 -0700229 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700230 MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700231 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700232}
233
234static void omap2_mcspi_set_master_mode(struct spi_master *master)
235{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530236 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
237 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700238 u32 l;
239
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530240 /*
241 * Setup when switching from (reset default) slave mode
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700242 * to single-channel master mode
243 */
244 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
245 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
246 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
247 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
248 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700249
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530250 ctx->modulctrl = l;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700251}
252
253static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
254{
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530255 struct spi_master *spi_cntrl = mcspi->master;
256 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
257 struct omap2_mcspi_cs *cs;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700258
259 /* McSPI: context restore */
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530260 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL, ctx->modulctrl);
261 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE, ctx->wakeupenable);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700262
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530263 list_for_each_entry(cs, &ctx->cs, node)
Tero Kristo89c05372009-09-22 16:46:17 -0700264 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700265}
266static void omap2_mcspi_disable_clocks(struct omap2_mcspi *mcspi)
267{
Govindraj.R1f1a4382011-02-02 17:52:15 +0530268 pm_runtime_put_sync(mcspi->dev);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700269}
270
271static int omap2_mcspi_enable_clocks(struct omap2_mcspi *mcspi)
272{
Govindraj.R1f1a4382011-02-02 17:52:15 +0530273 return pm_runtime_get_sync(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700274}
275
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300276static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
277{
278 unsigned long timeout;
279
280 timeout = jiffies + msecs_to_jiffies(1000);
281 while (!(__raw_readl(reg) & bit)) {
282 if (time_after(jiffies, timeout))
283 return -1;
284 cpu_relax();
285 }
286 return 0;
287}
288
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700289static unsigned
290omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
291{
292 struct omap2_mcspi *mcspi;
293 struct omap2_mcspi_cs *cs = spi->controller_state;
294 struct omap2_mcspi_dma *mcspi_dma;
295 unsigned int count, c;
296 unsigned long base, tx_reg, rx_reg;
297 int word_len, data_type, element_count;
Govindraj.R8b20c8c2011-06-01 11:31:24 +0530298 int elements = 0;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000299 u32 l;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700300 u8 * rx;
301 const u8 * tx;
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300302 void __iomem *chstat_reg;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700303
304 mcspi = spi_master_get_devdata(spi->master);
305 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000306 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700307
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300308 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
309
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700310 count = xfer->len;
311 c = count;
312 word_len = cs->word_len;
313
Russell Kinge5480b732008-09-01 21:51:50 +0100314 base = cs->phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700315 tx_reg = base + OMAP2_MCSPI_TX0;
316 rx_reg = base + OMAP2_MCSPI_RX0;
317 rx = xfer->rx_buf;
318 tx = xfer->tx_buf;
319
320 if (word_len <= 8) {
321 data_type = OMAP_DMA_DATA_TYPE_S8;
322 element_count = count;
323 } else if (word_len <= 16) {
324 data_type = OMAP_DMA_DATA_TYPE_S16;
325 element_count = count >> 1;
326 } else /* word_len <= 32 */ {
327 data_type = OMAP_DMA_DATA_TYPE_S32;
328 element_count = count >> 2;
329 }
330
331 if (tx != NULL) {
332 omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
333 data_type, element_count, 1,
334 OMAP_DMA_SYNC_ELEMENT,
335 mcspi_dma->dma_tx_sync_dev, 0);
336
337 omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
338 OMAP_DMA_AMODE_CONSTANT,
339 tx_reg, 0, 0);
340
341 omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
342 OMAP_DMA_AMODE_POST_INC,
343 xfer->tx_dma, 0, 0);
344 }
345
346 if (rx != NULL) {
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000347 elements = element_count - 1;
348 if (l & OMAP2_MCSPI_CHCONF_TURBO)
349 elements--;
350
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700351 omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000352 data_type, elements, 1,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700353 OMAP_DMA_SYNC_ELEMENT,
354 mcspi_dma->dma_rx_sync_dev, 1);
355
356 omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
357 OMAP_DMA_AMODE_CONSTANT,
358 rx_reg, 0, 0);
359
360 omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
361 OMAP_DMA_AMODE_POST_INC,
362 xfer->rx_dma, 0, 0);
363 }
364
365 if (tx != NULL) {
366 omap_start_dma(mcspi_dma->dma_tx_channel);
367 omap2_mcspi_set_dma_req(spi, 0, 1);
368 }
369
370 if (rx != NULL) {
371 omap_start_dma(mcspi_dma->dma_rx_channel);
372 omap2_mcspi_set_dma_req(spi, 1, 1);
373 }
374
375 if (tx != NULL) {
376 wait_for_completion(&mcspi_dma->dma_tx_completion);
Russell King - ARM Linux07fe0352011-01-07 15:49:20 +0000377 dma_unmap_single(&spi->dev, xfer->tx_dma, count, DMA_TO_DEVICE);
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300378
379 /* for TX_ONLY mode, be sure all words have shifted out */
380 if (rx == NULL) {
381 if (mcspi_wait_for_reg_bit(chstat_reg,
382 OMAP2_MCSPI_CHSTAT_TXS) < 0)
383 dev_err(&spi->dev, "TXS timed out\n");
384 else if (mcspi_wait_for_reg_bit(chstat_reg,
385 OMAP2_MCSPI_CHSTAT_EOT) < 0)
386 dev_err(&spi->dev, "EOT timed out\n");
387 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700388 }
389
390 if (rx != NULL) {
391 wait_for_completion(&mcspi_dma->dma_rx_completion);
Russell King - ARM Linux07fe0352011-01-07 15:49:20 +0000392 dma_unmap_single(&spi->dev, xfer->rx_dma, count, DMA_FROM_DEVICE);
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700393 omap2_mcspi_set_enable(spi, 0);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000394
395 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
396
397 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
398 & OMAP2_MCSPI_CHSTAT_RXS)) {
399 u32 w;
400
401 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
402 if (word_len <= 8)
403 ((u8 *)xfer->rx_buf)[elements++] = w;
404 else if (word_len <= 16)
405 ((u16 *)xfer->rx_buf)[elements++] = w;
406 else /* word_len <= 32 */
407 ((u32 *)xfer->rx_buf)[elements++] = w;
408 } else {
409 dev_err(&spi->dev,
410 "DMA RX penultimate word empty");
411 count -= (word_len <= 8) ? 2 :
412 (word_len <= 16) ? 4 :
413 /* word_len <= 32 */ 8;
414 omap2_mcspi_set_enable(spi, 1);
415 return count;
416 }
417 }
418
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700419 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
420 & OMAP2_MCSPI_CHSTAT_RXS)) {
421 u32 w;
422
423 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
424 if (word_len <= 8)
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000425 ((u8 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700426 else if (word_len <= 16)
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000427 ((u16 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700428 else /* word_len <= 32 */
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000429 ((u32 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700430 } else {
431 dev_err(&spi->dev, "DMA RX last word empty");
432 count -= (word_len <= 8) ? 1 :
433 (word_len <= 16) ? 2 :
434 /* word_len <= 32 */ 4;
435 }
436 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700437 }
438 return count;
439}
440
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700441static unsigned
442omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
443{
444 struct omap2_mcspi *mcspi;
445 struct omap2_mcspi_cs *cs = spi->controller_state;
446 unsigned int count, c;
447 u32 l;
448 void __iomem *base = cs->base;
449 void __iomem *tx_reg;
450 void __iomem *rx_reg;
451 void __iomem *chstat_reg;
452 int word_len;
453
454 mcspi = spi_master_get_devdata(spi->master);
455 count = xfer->len;
456 c = count;
457 word_len = cs->word_len;
458
Hemanth Va41ae1a2009-09-22 16:46:16 -0700459 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700460
461 /* We store the pre-calculated register addresses on stack to speed
462 * up the transfer loop. */
463 tx_reg = base + OMAP2_MCSPI_TX0;
464 rx_reg = base + OMAP2_MCSPI_RX0;
465 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
466
Michael Jonesadef6582011-02-25 16:55:11 +0100467 if (c < (word_len>>3))
468 return 0;
469
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700470 if (word_len <= 8) {
471 u8 *rx;
472 const u8 *tx;
473
474 rx = xfer->rx_buf;
475 tx = xfer->tx_buf;
476
477 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800478 c -= 1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700479 if (tx != NULL) {
480 if (mcspi_wait_for_reg_bit(chstat_reg,
481 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
482 dev_err(&spi->dev, "TXS timed out\n");
483 goto out;
484 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900485 dev_vdbg(&spi->dev, "write-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700486 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700487 __raw_writel(*tx++, tx_reg);
488 }
489 if (rx != NULL) {
490 if (mcspi_wait_for_reg_bit(chstat_reg,
491 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
492 dev_err(&spi->dev, "RXS timed out\n");
493 goto out;
494 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000495
496 if (c == 1 && tx == NULL &&
497 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
498 omap2_mcspi_set_enable(spi, 0);
499 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900500 dev_vdbg(&spi->dev, "read-%d %02x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000501 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000502 if (mcspi_wait_for_reg_bit(chstat_reg,
503 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
504 dev_err(&spi->dev,
505 "RXS timed out\n");
506 goto out;
507 }
508 c = 0;
509 } else if (c == 0 && tx == NULL) {
510 omap2_mcspi_set_enable(spi, 0);
511 }
512
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700513 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900514 dev_vdbg(&spi->dev, "read-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700515 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700516 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200517 } while (c);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700518 } else if (word_len <= 16) {
519 u16 *rx;
520 const u16 *tx;
521
522 rx = xfer->rx_buf;
523 tx = xfer->tx_buf;
524 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800525 c -= 2;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700526 if (tx != NULL) {
527 if (mcspi_wait_for_reg_bit(chstat_reg,
528 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
529 dev_err(&spi->dev, "TXS timed out\n");
530 goto out;
531 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900532 dev_vdbg(&spi->dev, "write-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700533 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700534 __raw_writel(*tx++, tx_reg);
535 }
536 if (rx != NULL) {
537 if (mcspi_wait_for_reg_bit(chstat_reg,
538 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
539 dev_err(&spi->dev, "RXS timed out\n");
540 goto out;
541 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000542
543 if (c == 2 && tx == NULL &&
544 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
545 omap2_mcspi_set_enable(spi, 0);
546 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900547 dev_vdbg(&spi->dev, "read-%d %04x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000548 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000549 if (mcspi_wait_for_reg_bit(chstat_reg,
550 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
551 dev_err(&spi->dev,
552 "RXS timed out\n");
553 goto out;
554 }
555 c = 0;
556 } else if (c == 0 && tx == NULL) {
557 omap2_mcspi_set_enable(spi, 0);
558 }
559
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700560 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900561 dev_vdbg(&spi->dev, "read-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700562 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700563 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200564 } while (c >= 2);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700565 } else if (word_len <= 32) {
566 u32 *rx;
567 const u32 *tx;
568
569 rx = xfer->rx_buf;
570 tx = xfer->tx_buf;
571 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800572 c -= 4;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700573 if (tx != NULL) {
574 if (mcspi_wait_for_reg_bit(chstat_reg,
575 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
576 dev_err(&spi->dev, "TXS timed out\n");
577 goto out;
578 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900579 dev_vdbg(&spi->dev, "write-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700580 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700581 __raw_writel(*tx++, tx_reg);
582 }
583 if (rx != NULL) {
584 if (mcspi_wait_for_reg_bit(chstat_reg,
585 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
586 dev_err(&spi->dev, "RXS timed out\n");
587 goto out;
588 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000589
590 if (c == 4 && tx == NULL &&
591 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
592 omap2_mcspi_set_enable(spi, 0);
593 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900594 dev_vdbg(&spi->dev, "read-%d %08x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000595 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000596 if (mcspi_wait_for_reg_bit(chstat_reg,
597 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
598 dev_err(&spi->dev,
599 "RXS timed out\n");
600 goto out;
601 }
602 c = 0;
603 } else if (c == 0 && tx == NULL) {
604 omap2_mcspi_set_enable(spi, 0);
605 }
606
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700607 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900608 dev_vdbg(&spi->dev, "read-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700609 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700610 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200611 } while (c >= 4);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700612 }
613
614 /* for TX_ONLY mode, be sure all words have shifted out */
615 if (xfer->rx_buf == NULL) {
616 if (mcspi_wait_for_reg_bit(chstat_reg,
617 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
618 dev_err(&spi->dev, "TXS timed out\n");
619 } else if (mcspi_wait_for_reg_bit(chstat_reg,
620 OMAP2_MCSPI_CHSTAT_EOT) < 0)
621 dev_err(&spi->dev, "EOT timed out\n");
Jason Wange1993ed2010-10-19 18:03:27 +0800622
623 /* disable chan to purge rx datas received in TX_ONLY transfer,
624 * otherwise these rx datas will affect the direct following
625 * RX_ONLY transfer.
626 */
627 omap2_mcspi_set_enable(spi, 0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700628 }
629out:
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000630 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700631 return count - c;
632}
633
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200634static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
635{
636 u32 div;
637
638 for (div = 0; div < 15; div++)
639 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
640 return div;
641
642 return 15;
643}
644
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700645/* called only when no transfer is active to this device */
646static int omap2_mcspi_setup_transfer(struct spi_device *spi,
647 struct spi_transfer *t)
648{
649 struct omap2_mcspi_cs *cs = spi->controller_state;
650 struct omap2_mcspi *mcspi;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700651 struct spi_master *spi_cntrl;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700652 u32 l = 0, div = 0;
653 u8 word_len = spi->bits_per_word;
Scott Ellis9bd45172010-03-10 14:23:13 -0700654 u32 speed_hz = spi->max_speed_hz;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700655
656 mcspi = spi_master_get_devdata(spi->master);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700657 spi_cntrl = mcspi->master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700658
659 if (t != NULL && t->bits_per_word)
660 word_len = t->bits_per_word;
661
662 cs->word_len = word_len;
663
Scott Ellis9bd45172010-03-10 14:23:13 -0700664 if (t && t->speed_hz)
665 speed_hz = t->speed_hz;
666
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200667 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
668 div = omap2_mcspi_calc_divisor(speed_hz);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700669
Hemanth Va41ae1a2009-09-22 16:46:16 -0700670 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700671
672 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
673 * REVISIT: this controller could support SPI_3WIRE mode.
674 */
675 l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
676 l |= OMAP2_MCSPI_CHCONF_DPE0;
677
678 /* wordlength */
679 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
680 l |= (word_len - 1) << 7;
681
682 /* set chipselect polarity; manage with FORCE */
683 if (!(spi->mode & SPI_CS_HIGH))
684 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
685 else
686 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
687
688 /* set clock divisor */
689 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
690 l |= div << 2;
691
692 /* set SPI mode 0..3 */
693 if (spi->mode & SPI_CPOL)
694 l |= OMAP2_MCSPI_CHCONF_POL;
695 else
696 l &= ~OMAP2_MCSPI_CHCONF_POL;
697 if (spi->mode & SPI_CPHA)
698 l |= OMAP2_MCSPI_CHCONF_PHA;
699 else
700 l &= ~OMAP2_MCSPI_CHCONF_PHA;
701
Hemanth Va41ae1a2009-09-22 16:46:16 -0700702 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700703
704 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200705 OMAP2_MCSPI_MAX_FREQ >> div,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700706 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
707 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
708
709 return 0;
710}
711
712static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
713{
714 struct spi_device *spi = data;
715 struct omap2_mcspi *mcspi;
716 struct omap2_mcspi_dma *mcspi_dma;
717
718 mcspi = spi_master_get_devdata(spi->master);
719 mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
720
721 complete(&mcspi_dma->dma_rx_completion);
722
723 /* We must disable the DMA RX request */
724 omap2_mcspi_set_dma_req(spi, 1, 0);
725}
726
727static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
728{
729 struct spi_device *spi = data;
730 struct omap2_mcspi *mcspi;
731 struct omap2_mcspi_dma *mcspi_dma;
732
733 mcspi = spi_master_get_devdata(spi->master);
734 mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
735
736 complete(&mcspi_dma->dma_tx_completion);
737
738 /* We must disable the DMA TX request */
739 omap2_mcspi_set_dma_req(spi, 0, 0);
740}
741
742static int omap2_mcspi_request_dma(struct spi_device *spi)
743{
744 struct spi_master *master = spi->master;
745 struct omap2_mcspi *mcspi;
746 struct omap2_mcspi_dma *mcspi_dma;
747
748 mcspi = spi_master_get_devdata(master);
749 mcspi_dma = mcspi->dma_channels + spi->chip_select;
750
751 if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
752 omap2_mcspi_dma_rx_callback, spi,
753 &mcspi_dma->dma_rx_channel)) {
754 dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
755 return -EAGAIN;
756 }
757
758 if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
759 omap2_mcspi_dma_tx_callback, spi,
760 &mcspi_dma->dma_tx_channel)) {
761 omap_free_dma(mcspi_dma->dma_rx_channel);
762 mcspi_dma->dma_rx_channel = -1;
763 dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
764 return -EAGAIN;
765 }
766
767 init_completion(&mcspi_dma->dma_rx_completion);
768 init_completion(&mcspi_dma->dma_tx_completion);
769
770 return 0;
771}
772
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700773static int omap2_mcspi_setup(struct spi_device *spi)
774{
775 int ret;
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530776 struct omap2_mcspi *mcspi = spi_master_get_devdata(spi->master);
777 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700778 struct omap2_mcspi_dma *mcspi_dma;
779 struct omap2_mcspi_cs *cs = spi->controller_state;
780
David Brownell7d077192009-06-17 16:26:03 -0700781 if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700782 dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
783 spi->bits_per_word);
784 return -EINVAL;
785 }
786
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700787 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
788
789 if (!cs) {
Shubhrajyoti D1a77b122012-03-17 12:44:01 +0530790 cs = devm_kzalloc(&spi->dev , sizeof *cs, GFP_KERNEL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700791 if (!cs)
792 return -ENOMEM;
793 cs->base = mcspi->base + spi->chip_select * 0x14;
Russell Kinge5480b732008-09-01 21:51:50 +0100794 cs->phys = mcspi->phys + spi->chip_select * 0x14;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700795 cs->chconf0 = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700796 spi->controller_state = cs;
Tero Kristo89c05372009-09-22 16:46:17 -0700797 /* Link this to context save list */
Benoit Cousson1bd897f82012-03-26 15:32:33 +0530798 list_add_tail(&cs->node, &ctx->cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700799 }
800
801 if (mcspi_dma->dma_rx_channel == -1
802 || mcspi_dma->dma_tx_channel == -1) {
803 ret = omap2_mcspi_request_dma(spi);
804 if (ret < 0)
805 return ret;
806 }
807
Govindraj.R1f1a4382011-02-02 17:52:15 +0530808 ret = omap2_mcspi_enable_clocks(mcspi);
809 if (ret < 0)
810 return ret;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700811
Kyungmin Park86eeb6f2007-10-16 01:27:45 -0700812 ret = omap2_mcspi_setup_transfer(spi, NULL);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700813 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700814
815 return ret;
816}
817
818static void omap2_mcspi_cleanup(struct spi_device *spi)
819{
820 struct omap2_mcspi *mcspi;
821 struct omap2_mcspi_dma *mcspi_dma;
Tero Kristo89c05372009-09-22 16:46:17 -0700822 struct omap2_mcspi_cs *cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700823
824 mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700825
Scott Ellis5e774942010-03-10 14:22:45 -0700826 if (spi->controller_state) {
827 /* Unlink controller state from context save list */
828 cs = spi->controller_state;
829 list_del(&cs->node);
Tero Kristo89c05372009-09-22 16:46:17 -0700830
Scott Ellis5e774942010-03-10 14:22:45 -0700831 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700832
Scott Ellis99f1a432010-05-24 14:20:27 +0000833 if (spi->chip_select < spi->master->num_chipselect) {
834 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
835
836 if (mcspi_dma->dma_rx_channel != -1) {
837 omap_free_dma(mcspi_dma->dma_rx_channel);
838 mcspi_dma->dma_rx_channel = -1;
839 }
840 if (mcspi_dma->dma_tx_channel != -1) {
841 omap_free_dma(mcspi_dma->dma_tx_channel);
842 mcspi_dma->dma_tx_channel = -1;
843 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700844 }
845}
846
847static void omap2_mcspi_work(struct work_struct *work)
848{
849 struct omap2_mcspi *mcspi;
850
851 mcspi = container_of(work, struct omap2_mcspi, work);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700852
Govindraj.R1f1a4382011-02-02 17:52:15 +0530853 if (omap2_mcspi_enable_clocks(mcspi) < 0)
854 return;
855
856 spin_lock_irq(&mcspi->lock);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700857
858 /* We only enable one channel at a time -- the one whose message is
859 * at the head of the queue -- although this controller would gladly
860 * arbitrate among multiple channels. This corresponds to "single
861 * channel" master mode. As a side effect, we need to manage the
862 * chipselect with the FORCE bit ... CS != channel enable.
863 */
864 while (!list_empty(&mcspi->msg_queue)) {
865 struct spi_message *m;
866 struct spi_device *spi;
867 struct spi_transfer *t = NULL;
868 int cs_active = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700869 struct omap2_mcspi_cs *cs;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000870 struct omap2_mcspi_device_config *cd;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700871 int par_override = 0;
872 int status = 0;
873 u32 chconf;
874
875 m = container_of(mcspi->msg_queue.next, struct spi_message,
876 queue);
877
878 list_del_init(&m->queue);
879 spin_unlock_irq(&mcspi->lock);
880
881 spi = m->spi;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700882 cs = spi->controller_state;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000883 cd = spi->controller_data;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700884
885 omap2_mcspi_set_enable(spi, 1);
886 list_for_each_entry(t, &m->transfers, transfer_list) {
887 if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
888 status = -EINVAL;
889 break;
890 }
891 if (par_override || t->speed_hz || t->bits_per_word) {
892 par_override = 1;
893 status = omap2_mcspi_setup_transfer(spi, t);
894 if (status < 0)
895 break;
896 if (!t->speed_hz && !t->bits_per_word)
897 par_override = 0;
898 }
899
900 if (!cs_active) {
901 omap2_mcspi_force_cs(spi, 1);
902 cs_active = 1;
903 }
904
Hemanth Va41ae1a2009-09-22 16:46:16 -0700905 chconf = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700906 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000907 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
908
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700909 if (t->tx_buf == NULL)
910 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
911 else if (t->rx_buf == NULL)
912 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000913
914 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
915 /* Turbo mode is for more than one word */
916 if (t->len > ((cs->word_len + 7) >> 3))
917 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
918 }
919
Hemanth Va41ae1a2009-09-22 16:46:16 -0700920 mcspi_write_chconf0(spi, chconf);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700921
922 if (t->len) {
923 unsigned count;
924
925 /* RX_ONLY mode needs dummy data in TX reg */
926 if (t->tx_buf == NULL)
927 __raw_writel(0, cs->base
928 + OMAP2_MCSPI_TX0);
929
930 if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
931 count = omap2_mcspi_txrx_dma(spi, t);
932 else
933 count = omap2_mcspi_txrx_pio(spi, t);
934 m->actual_length += count;
935
936 if (count != t->len) {
937 status = -EIO;
938 break;
939 }
940 }
941
942 if (t->delay_usecs)
943 udelay(t->delay_usecs);
944
945 /* ignore the "leave it on after last xfer" hint */
946 if (t->cs_change) {
947 omap2_mcspi_force_cs(spi, 0);
948 cs_active = 0;
949 }
950 }
951
952 /* Restore defaults if they were overriden */
953 if (par_override) {
954 par_override = 0;
955 status = omap2_mcspi_setup_transfer(spi, NULL);
956 }
957
958 if (cs_active)
959 omap2_mcspi_force_cs(spi, 0);
960
961 omap2_mcspi_set_enable(spi, 0);
962
963 m->status = status;
964 m->complete(m->context);
965
966 spin_lock_irq(&mcspi->lock);
967 }
968
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700969 spin_unlock_irq(&mcspi->lock);
Govindraj.R1f1a4382011-02-02 17:52:15 +0530970
971 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700972}
973
974static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
975{
976 struct omap2_mcspi *mcspi;
977 unsigned long flags;
978 struct spi_transfer *t;
979
980 m->actual_length = 0;
981 m->status = 0;
982
983 /* reject invalid messages and transfers */
984 if (list_empty(&m->transfers) || !m->complete)
985 return -EINVAL;
986 list_for_each_entry(t, &m->transfers, transfer_list) {
987 const void *tx_buf = t->tx_buf;
988 void *rx_buf = t->rx_buf;
989 unsigned len = t->len;
990
991 if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
992 || (len && !(rx_buf || tx_buf))
993 || (t->bits_per_word &&
994 ( t->bits_per_word < 4
995 || t->bits_per_word > 32))) {
996 dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
997 t->speed_hz,
998 len,
999 tx_buf ? "tx" : "",
1000 rx_buf ? "rx" : "",
1001 t->bits_per_word);
1002 return -EINVAL;
1003 }
Hannu Heikkinen57d9c102011-02-24 21:31:33 +02001004 if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
1005 dev_dbg(&spi->dev, "speed_hz %d below minimum %d Hz\n",
1006 t->speed_hz,
1007 OMAP2_MCSPI_MAX_FREQ >> 15);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001008 return -EINVAL;
1009 }
1010
1011 if (m->is_dma_mapped || len < DMA_MIN_BYTES)
1012 continue;
1013
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001014 if (tx_buf != NULL) {
1015 t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
1016 len, DMA_TO_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001017 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001018 dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
1019 'T', len);
1020 return -EINVAL;
1021 }
1022 }
1023 if (rx_buf != NULL) {
1024 t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
1025 DMA_FROM_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001026 if (dma_mapping_error(&spi->dev, t->rx_dma)) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001027 dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
1028 'R', len);
1029 if (tx_buf != NULL)
Russell King - ARM Linux07fe0352011-01-07 15:49:20 +00001030 dma_unmap_single(&spi->dev, t->tx_dma,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001031 len, DMA_TO_DEVICE);
1032 return -EINVAL;
1033 }
1034 }
1035 }
1036
1037 mcspi = spi_master_get_devdata(spi->master);
1038
1039 spin_lock_irqsave(&mcspi->lock, flags);
1040 list_add_tail(&m->queue, &mcspi->msg_queue);
Shubhrajyoti D2856ac12011-10-28 17:14:17 +05301041 queue_work(mcspi->wq, &mcspi->work);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001042 spin_unlock_irqrestore(&mcspi->lock, flags);
1043
1044 return 0;
1045}
1046
Govindraj.R1f1a4382011-02-02 17:52:15 +05301047static int __init omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001048{
1049 struct spi_master *master = mcspi->master;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301050 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001051 u32 tmp;
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301052 int ret = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001053
Govindraj.R1f1a4382011-02-02 17:52:15 +05301054 ret = omap2_mcspi_enable_clocks(mcspi);
1055 if (ret < 0)
1056 return ret;
Jouni Hoganderddb22192009-07-29 15:02:11 -07001057
Hemanth Va41ae1a2009-09-22 16:46:16 -07001058 tmp = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
1059 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, tmp);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301060 ctx->wakeupenable = tmp;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001061
1062 omap2_mcspi_set_master_mode(master);
Hemanth Va41ae1a2009-09-22 16:46:16 -07001063 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001064 return 0;
1065}
1066
Govindraj.R1f1a4382011-02-02 17:52:15 +05301067static int omap_mcspi_runtime_resume(struct device *dev)
1068{
1069 struct omap2_mcspi *mcspi;
1070 struct spi_master *master;
1071
1072 master = dev_get_drvdata(dev);
1073 mcspi = spi_master_get_devdata(master);
1074 omap2_mcspi_restore_ctx(mcspi);
1075
1076 return 0;
1077}
1078
Benoit Coussond5a80032012-02-15 18:37:34 +01001079static struct omap2_mcspi_platform_config omap2_pdata = {
1080 .regs_offset = 0,
1081};
1082
1083static struct omap2_mcspi_platform_config omap4_pdata = {
1084 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1085};
1086
1087static const struct of_device_id omap_mcspi_of_match[] = {
1088 {
1089 .compatible = "ti,omap2-mcspi",
1090 .data = &omap2_pdata,
1091 },
1092 {
1093 .compatible = "ti,omap4-mcspi",
1094 .data = &omap4_pdata,
1095 },
1096 { },
1097};
1098MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
Girishccc7bae2008-02-06 01:38:16 -08001099
Felipe Balbi7d6b6d82012-03-14 11:18:30 +02001100static int __devinit omap2_mcspi_probe(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001101{
1102 struct spi_master *master;
Benoit Coussond5a80032012-02-15 18:37:34 +01001103 struct omap2_mcspi_platform_config *pdata;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001104 struct omap2_mcspi *mcspi;
1105 struct resource *r;
1106 int status = 0, i;
Benoit Coussond5a80032012-02-15 18:37:34 +01001107 u32 regs_offset = 0;
1108 static int bus_num = 1;
1109 struct device_node *node = pdev->dev.of_node;
1110 const struct of_device_id *match;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001111
1112 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1113 if (master == NULL) {
1114 dev_dbg(&pdev->dev, "master allocation failed\n");
1115 return -ENOMEM;
1116 }
1117
David Brownelle7db06b2009-06-17 16:26:04 -07001118 /* the spi->mode bits understood by this driver: */
1119 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1120
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001121 master->setup = omap2_mcspi_setup;
1122 master->transfer = omap2_mcspi_transfer;
1123 master->cleanup = omap2_mcspi_cleanup;
Benoit Coussond5a80032012-02-15 18:37:34 +01001124 master->dev.of_node = node;
1125
1126 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1127 if (match) {
1128 u32 num_cs = 1; /* default number of chipselect */
1129 pdata = match->data;
1130
1131 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1132 master->num_chipselect = num_cs;
1133 master->bus_num = bus_num++;
1134 } else {
1135 pdata = pdev->dev.platform_data;
1136 master->num_chipselect = pdata->num_cs;
1137 if (pdev->id != -1)
1138 master->bus_num = pdev->id;
1139 }
1140 regs_offset = pdata->regs_offset;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001141
1142 dev_set_drvdata(&pdev->dev, master);
1143
1144 mcspi = spi_master_get_devdata(master);
1145 mcspi->master = master;
1146
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301147 mcspi->wq = alloc_workqueue(dev_name(&pdev->dev), WQ_MEM_RECLAIM, 1);
Shubhrajyoti D2856ac12011-10-28 17:14:17 +05301148 if (mcspi->wq == NULL) {
1149 status = -ENOMEM;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301150 goto free_master;
Shubhrajyoti D2856ac12011-10-28 17:14:17 +05301151 }
1152
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001153 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1154 if (r == NULL) {
1155 status = -ENODEV;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301156 goto free_master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001157 }
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301158
Benoit Coussond5a80032012-02-15 18:37:34 +01001159 r->start += regs_offset;
1160 r->end += regs_offset;
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301161 mcspi->phys = r->start;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001162
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301163 mcspi->base = devm_request_and_ioremap(&pdev->dev, r);
Russell King55c381e2008-09-04 14:07:22 +01001164 if (!mcspi->base) {
1165 dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
1166 status = -ENOMEM;
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301167 goto free_master;
Russell King55c381e2008-09-04 14:07:22 +01001168 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001169
Govindraj.R1f1a4382011-02-02 17:52:15 +05301170 mcspi->dev = &pdev->dev;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001171 INIT_WORK(&mcspi->work, omap2_mcspi_work);
1172
1173 spin_lock_init(&mcspi->lock);
1174 INIT_LIST_HEAD(&mcspi->msg_queue);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301175 INIT_LIST_HEAD(&mcspi->ctx.cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001176
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001177 mcspi->dma_channels = kcalloc(master->num_chipselect,
1178 sizeof(struct omap2_mcspi_dma),
1179 GFP_KERNEL);
1180
1181 if (mcspi->dma_channels == NULL)
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301182 goto free_master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001183
Charulatha V1a5d8192011-02-02 17:52:14 +05301184 for (i = 0; i < master->num_chipselect; i++) {
1185 char dma_ch_name[14];
1186 struct resource *dma_res;
1187
1188 sprintf(dma_ch_name, "rx%d", i);
1189 dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1190 dma_ch_name);
1191 if (!dma_res) {
1192 dev_dbg(&pdev->dev, "cannot get DMA RX channel\n");
1193 status = -ENODEV;
1194 break;
1195 }
1196
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001197 mcspi->dma_channels[i].dma_rx_channel = -1;
Charulatha V1a5d8192011-02-02 17:52:14 +05301198 mcspi->dma_channels[i].dma_rx_sync_dev = dma_res->start;
1199 sprintf(dma_ch_name, "tx%d", i);
1200 dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1201 dma_ch_name);
1202 if (!dma_res) {
1203 dev_dbg(&pdev->dev, "cannot get DMA TX channel\n");
1204 status = -ENODEV;
1205 break;
1206 }
1207
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001208 mcspi->dma_channels[i].dma_tx_channel = -1;
Charulatha V1a5d8192011-02-02 17:52:14 +05301209 mcspi->dma_channels[i].dma_tx_sync_dev = dma_res->start;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001210 }
1211
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301212 if (status < 0)
1213 goto dma_chnl_free;
1214
Govindraj.R1f1a4382011-02-02 17:52:15 +05301215 pm_runtime_enable(&pdev->dev);
1216
1217 if (status || omap2_mcspi_master_setup(mcspi) < 0)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301218 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001219
1220 status = spi_register_master(master);
1221 if (status < 0)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301222 goto err_spi_register;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001223
1224 return status;
1225
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301226err_spi_register:
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001227 spi_master_put(master);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301228disable_pm:
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301229 pm_runtime_disable(&pdev->dev);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301230dma_chnl_free:
Govindraj.R1f1a4382011-02-02 17:52:15 +05301231 kfree(mcspi->dma_channels);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301232free_master:
1233 kfree(master);
1234 platform_set_drvdata(pdev, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001235 return status;
1236}
1237
Felipe Balbi7d6b6d82012-03-14 11:18:30 +02001238static int __devexit omap2_mcspi_remove(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001239{
1240 struct spi_master *master;
1241 struct omap2_mcspi *mcspi;
1242 struct omap2_mcspi_dma *dma_channels;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001243
1244 master = dev_get_drvdata(&pdev->dev);
1245 mcspi = spi_master_get_devdata(master);
1246 dma_channels = mcspi->dma_channels;
1247
Govindraj.R1f1a4382011-02-02 17:52:15 +05301248 omap2_mcspi_disable_clocks(mcspi);
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301249 pm_runtime_disable(&pdev->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001250
1251 spi_unregister_master(master);
1252 kfree(dma_channels);
Shubhrajyoti D2856ac12011-10-28 17:14:17 +05301253 destroy_workqueue(mcspi->wq);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301254 platform_set_drvdata(pdev, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001255
1256 return 0;
1257}
1258
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001259/* work with hotplug and coldplug */
1260MODULE_ALIAS("platform:omap2_mcspi");
1261
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001262#ifdef CONFIG_SUSPEND
1263/*
1264 * When SPI wake up from off-mode, CS is in activate state. If it was in
1265 * unactive state when driver was suspend, then force it to unactive state at
1266 * wake up.
1267 */
1268static int omap2_mcspi_resume(struct device *dev)
1269{
1270 struct spi_master *master = dev_get_drvdata(dev);
1271 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301272 struct omap2_mcspi_regs *ctx = &mcspi->ctx;
1273 struct omap2_mcspi_cs *cs;
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001274
1275 omap2_mcspi_enable_clocks(mcspi);
Benoit Cousson1bd897f82012-03-26 15:32:33 +05301276 list_for_each_entry(cs, &ctx->cs, node) {
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001277 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001278 /*
1279 * We need to toggle CS state for OMAP take this
1280 * change in account.
1281 */
1282 MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 1);
1283 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1284 MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 0);
1285 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1286 }
1287 }
1288 omap2_mcspi_disable_clocks(mcspi);
1289 return 0;
1290}
1291#else
1292#define omap2_mcspi_resume NULL
1293#endif
1294
1295static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1296 .resume = omap2_mcspi_resume,
Govindraj.R1f1a4382011-02-02 17:52:15 +05301297 .runtime_resume = omap_mcspi_runtime_resume,
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001298};
1299
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001300static struct platform_driver omap2_mcspi_driver = {
1301 .driver = {
1302 .name = "omap2_mcspi",
1303 .owner = THIS_MODULE,
Benoit Coussond5a80032012-02-15 18:37:34 +01001304 .pm = &omap2_mcspi_pm_ops,
1305 .of_match_table = omap_mcspi_of_match,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001306 },
Felipe Balbi7d6b6d82012-03-14 11:18:30 +02001307 .probe = omap2_mcspi_probe,
1308 .remove = __devexit_p(omap2_mcspi_remove),
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001309};
1310
Felipe Balbi9fdca9d2012-03-14 11:18:31 +02001311module_platform_driver(omap2_mcspi_driver);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001312MODULE_LICENSE("GPL");