blob: 33f54cd07fe00093f922383686371914a8b8521b [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
Hemanth Va41ae1a2009-09-22 16:46:16 -070048/* OMAP2 has 3 SPI controllers, while OMAP3 has 4 */
49#define OMAP2_MCSPI_MAX_CTRL 4
50
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070051#define OMAP2_MCSPI_REVISION 0x00
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070052#define OMAP2_MCSPI_SYSSTATUS 0x14
53#define OMAP2_MCSPI_IRQSTATUS 0x18
54#define OMAP2_MCSPI_IRQENABLE 0x1c
55#define OMAP2_MCSPI_WAKEUPENABLE 0x20
56#define OMAP2_MCSPI_SYST 0x24
57#define OMAP2_MCSPI_MODULCTRL 0x28
58
59/* per-channel banks, 0x14 bytes each, first is: */
60#define OMAP2_MCSPI_CHCONF0 0x2c
61#define OMAP2_MCSPI_CHSTAT0 0x30
62#define OMAP2_MCSPI_CHCTRL0 0x34
63#define OMAP2_MCSPI_TX0 0x38
64#define OMAP2_MCSPI_RX0 0x3c
65
66/* per-register bitmasks: */
67
Jouni Hogander7a8fa722009-09-22 16:45:58 -070068#define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
69#define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
70#define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070071
Jouni Hogander7a8fa722009-09-22 16:45:58 -070072#define OMAP2_MCSPI_CHCONF_PHA BIT(0)
73#define OMAP2_MCSPI_CHCONF_POL BIT(1)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070074#define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070075#define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070076#define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070077#define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
78#define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070079#define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070080#define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
81#define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
82#define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
83#define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
84#define OMAP2_MCSPI_CHCONF_IS BIT(18)
85#define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
86#define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070087
Jouni Hogander7a8fa722009-09-22 16:45:58 -070088#define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
89#define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
90#define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070091
Jouni Hogander7a8fa722009-09-22 16:45:58 -070092#define OMAP2_MCSPI_CHCTRL_EN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070093
Jouni Hogander7a8fa722009-09-22 16:45:58 -070094#define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070095
96/* We have 2 DMA channels per CS, one for RX and one for TX */
97struct omap2_mcspi_dma {
98 int dma_tx_channel;
99 int dma_rx_channel;
100
101 int dma_tx_sync_dev;
102 int dma_rx_sync_dev;
103
104 struct completion dma_tx_completion;
105 struct completion dma_rx_completion;
106};
107
108/* use PIO for small transfers, avoiding DMA setup/teardown overhead and
109 * cache operations; better heuristics consider wordsize and bitrate.
110 */
Roman Tereshonkov8b66c132010-04-12 09:07:54 +0000111#define DMA_MIN_BYTES 160
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700112
113
114struct omap2_mcspi {
115 struct work_struct work;
116 /* lock protects queue and registers */
117 spinlock_t lock;
118 struct list_head msg_queue;
119 struct spi_master *master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700120 /* Virtual base address of the controller */
121 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100122 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700123 /* SPI1 has 4 channels, while SPI2 has 2 */
124 struct omap2_mcspi_dma *dma_channels;
Govindraj.R1f1a4382011-02-02 17:52:15 +0530125 struct device *dev;
Shubhrajyoti D2856ac12011-10-28 17:14:17 +0530126 struct workqueue_struct *wq;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700127};
128
129struct omap2_mcspi_cs {
130 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100131 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700132 int word_len;
Tero Kristo89c05372009-09-22 16:46:17 -0700133 struct list_head node;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700134 /* Context save and restore shadow register */
135 u32 chconf0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700136};
137
Hemanth Va41ae1a2009-09-22 16:46:16 -0700138/* used for context save and restore, structure members to be updated whenever
139 * corresponding registers are modified.
140 */
141struct omap2_mcspi_regs {
Hemanth Va41ae1a2009-09-22 16:46:16 -0700142 u32 modulctrl;
143 u32 wakeupenable;
Tero Kristo89c05372009-09-22 16:46:17 -0700144 struct list_head cs;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700145};
146
147static struct omap2_mcspi_regs omap2_mcspi_ctx[OMAP2_MCSPI_MAX_CTRL];
148
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700149#define MOD_REG_BIT(val, mask, set) do { \
150 if (set) \
151 val |= mask; \
152 else \
153 val &= ~mask; \
154} while (0)
155
156static inline void mcspi_write_reg(struct spi_master *master,
157 int idx, u32 val)
158{
159 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
160
161 __raw_writel(val, mcspi->base + idx);
162}
163
164static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
165{
166 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
167
168 return __raw_readl(mcspi->base + idx);
169}
170
171static inline void mcspi_write_cs_reg(const struct spi_device *spi,
172 int idx, u32 val)
173{
174 struct omap2_mcspi_cs *cs = spi->controller_state;
175
176 __raw_writel(val, cs->base + idx);
177}
178
179static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
180{
181 struct omap2_mcspi_cs *cs = spi->controller_state;
182
183 return __raw_readl(cs->base + idx);
184}
185
Hemanth Va41ae1a2009-09-22 16:46:16 -0700186static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
187{
188 struct omap2_mcspi_cs *cs = spi->controller_state;
189
190 return cs->chconf0;
191}
192
193static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
194{
195 struct omap2_mcspi_cs *cs = spi->controller_state;
196
197 cs->chconf0 = val;
198 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
Roman Tereshonkova330ce22010-03-15 09:06:28 +0000199 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700200}
201
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700202static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
203 int is_read, int enable)
204{
205 u32 l, rw;
206
Hemanth Va41ae1a2009-09-22 16:46:16 -0700207 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700208
209 if (is_read) /* 1 is read, 0 write */
210 rw = OMAP2_MCSPI_CHCONF_DMAR;
211 else
212 rw = OMAP2_MCSPI_CHCONF_DMAW;
213
214 MOD_REG_BIT(l, rw, enable);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700215 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700216}
217
218static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
219{
220 u32 l;
221
222 l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
223 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000224 /* Flash post-writes */
225 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700226}
227
228static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
229{
230 u32 l;
231
Hemanth Va41ae1a2009-09-22 16:46:16 -0700232 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700233 MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700234 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700235}
236
237static void omap2_mcspi_set_master_mode(struct spi_master *master)
238{
239 u32 l;
240
241 /* setup when switching from (reset default) slave mode
242 * 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
250 omap2_mcspi_ctx[master->bus_num - 1].modulctrl = l;
251}
252
253static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
254{
255 struct spi_master *spi_cntrl;
Tero Kristo89c05372009-09-22 16:46:17 -0700256 struct omap2_mcspi_cs *cs;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700257 spi_cntrl = mcspi->master;
258
259 /* McSPI: context restore */
260 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL,
261 omap2_mcspi_ctx[spi_cntrl->bus_num - 1].modulctrl);
262
Hemanth Va41ae1a2009-09-22 16:46:16 -0700263 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE,
264 omap2_mcspi_ctx[spi_cntrl->bus_num - 1].wakeupenable);
Tero Kristo89c05372009-09-22 16:46:17 -0700265
266 list_for_each_entry(cs, &omap2_mcspi_ctx[spi_cntrl->bus_num - 1].cs,
267 node)
268 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700269}
270static void omap2_mcspi_disable_clocks(struct omap2_mcspi *mcspi)
271{
Govindraj.R1f1a4382011-02-02 17:52:15 +0530272 pm_runtime_put_sync(mcspi->dev);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700273}
274
275static int omap2_mcspi_enable_clocks(struct omap2_mcspi *mcspi)
276{
Govindraj.R1f1a4382011-02-02 17:52:15 +0530277 return pm_runtime_get_sync(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700278}
279
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300280static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
281{
282 unsigned long timeout;
283
284 timeout = jiffies + msecs_to_jiffies(1000);
285 while (!(__raw_readl(reg) & bit)) {
286 if (time_after(jiffies, timeout))
287 return -1;
288 cpu_relax();
289 }
290 return 0;
291}
292
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700293static unsigned
294omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
295{
296 struct omap2_mcspi *mcspi;
297 struct omap2_mcspi_cs *cs = spi->controller_state;
298 struct omap2_mcspi_dma *mcspi_dma;
299 unsigned int count, c;
300 unsigned long base, tx_reg, rx_reg;
301 int word_len, data_type, element_count;
Govindraj.R8b20c8c2011-06-01 11:31:24 +0530302 int elements = 0;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000303 u32 l;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700304 u8 * rx;
305 const u8 * tx;
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300306 void __iomem *chstat_reg;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700307
308 mcspi = spi_master_get_devdata(spi->master);
309 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000310 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700311
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300312 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
313
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700314 count = xfer->len;
315 c = count;
316 word_len = cs->word_len;
317
Russell Kinge5480b732008-09-01 21:51:50 +0100318 base = cs->phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700319 tx_reg = base + OMAP2_MCSPI_TX0;
320 rx_reg = base + OMAP2_MCSPI_RX0;
321 rx = xfer->rx_buf;
322 tx = xfer->tx_buf;
323
324 if (word_len <= 8) {
325 data_type = OMAP_DMA_DATA_TYPE_S8;
326 element_count = count;
327 } else if (word_len <= 16) {
328 data_type = OMAP_DMA_DATA_TYPE_S16;
329 element_count = count >> 1;
330 } else /* word_len <= 32 */ {
331 data_type = OMAP_DMA_DATA_TYPE_S32;
332 element_count = count >> 2;
333 }
334
335 if (tx != NULL) {
336 omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
337 data_type, element_count, 1,
338 OMAP_DMA_SYNC_ELEMENT,
339 mcspi_dma->dma_tx_sync_dev, 0);
340
341 omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
342 OMAP_DMA_AMODE_CONSTANT,
343 tx_reg, 0, 0);
344
345 omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
346 OMAP_DMA_AMODE_POST_INC,
347 xfer->tx_dma, 0, 0);
348 }
349
350 if (rx != NULL) {
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000351 elements = element_count - 1;
352 if (l & OMAP2_MCSPI_CHCONF_TURBO)
353 elements--;
354
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700355 omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000356 data_type, elements, 1,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700357 OMAP_DMA_SYNC_ELEMENT,
358 mcspi_dma->dma_rx_sync_dev, 1);
359
360 omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
361 OMAP_DMA_AMODE_CONSTANT,
362 rx_reg, 0, 0);
363
364 omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
365 OMAP_DMA_AMODE_POST_INC,
366 xfer->rx_dma, 0, 0);
367 }
368
369 if (tx != NULL) {
370 omap_start_dma(mcspi_dma->dma_tx_channel);
371 omap2_mcspi_set_dma_req(spi, 0, 1);
372 }
373
374 if (rx != NULL) {
375 omap_start_dma(mcspi_dma->dma_rx_channel);
376 omap2_mcspi_set_dma_req(spi, 1, 1);
377 }
378
379 if (tx != NULL) {
380 wait_for_completion(&mcspi_dma->dma_tx_completion);
Russell King - ARM Linux07fe0352011-01-07 15:49:20 +0000381 dma_unmap_single(&spi->dev, xfer->tx_dma, count, DMA_TO_DEVICE);
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300382
383 /* for TX_ONLY mode, be sure all words have shifted out */
384 if (rx == NULL) {
385 if (mcspi_wait_for_reg_bit(chstat_reg,
386 OMAP2_MCSPI_CHSTAT_TXS) < 0)
387 dev_err(&spi->dev, "TXS timed out\n");
388 else if (mcspi_wait_for_reg_bit(chstat_reg,
389 OMAP2_MCSPI_CHSTAT_EOT) < 0)
390 dev_err(&spi->dev, "EOT timed out\n");
391 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700392 }
393
394 if (rx != NULL) {
395 wait_for_completion(&mcspi_dma->dma_rx_completion);
Russell King - ARM Linux07fe0352011-01-07 15:49:20 +0000396 dma_unmap_single(&spi->dev, xfer->rx_dma, count, DMA_FROM_DEVICE);
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700397 omap2_mcspi_set_enable(spi, 0);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000398
399 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
400
401 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
402 & OMAP2_MCSPI_CHSTAT_RXS)) {
403 u32 w;
404
405 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
406 if (word_len <= 8)
407 ((u8 *)xfer->rx_buf)[elements++] = w;
408 else if (word_len <= 16)
409 ((u16 *)xfer->rx_buf)[elements++] = w;
410 else /* word_len <= 32 */
411 ((u32 *)xfer->rx_buf)[elements++] = w;
412 } else {
413 dev_err(&spi->dev,
414 "DMA RX penultimate word empty");
415 count -= (word_len <= 8) ? 2 :
416 (word_len <= 16) ? 4 :
417 /* word_len <= 32 */ 8;
418 omap2_mcspi_set_enable(spi, 1);
419 return count;
420 }
421 }
422
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700423 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
424 & OMAP2_MCSPI_CHSTAT_RXS)) {
425 u32 w;
426
427 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
428 if (word_len <= 8)
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000429 ((u8 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700430 else if (word_len <= 16)
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000431 ((u16 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700432 else /* word_len <= 32 */
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000433 ((u32 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700434 } else {
435 dev_err(&spi->dev, "DMA RX last word empty");
436 count -= (word_len <= 8) ? 1 :
437 (word_len <= 16) ? 2 :
438 /* word_len <= 32 */ 4;
439 }
440 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700441 }
442 return count;
443}
444
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700445static unsigned
446omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
447{
448 struct omap2_mcspi *mcspi;
449 struct omap2_mcspi_cs *cs = spi->controller_state;
450 unsigned int count, c;
451 u32 l;
452 void __iomem *base = cs->base;
453 void __iomem *tx_reg;
454 void __iomem *rx_reg;
455 void __iomem *chstat_reg;
456 int word_len;
457
458 mcspi = spi_master_get_devdata(spi->master);
459 count = xfer->len;
460 c = count;
461 word_len = cs->word_len;
462
Hemanth Va41ae1a2009-09-22 16:46:16 -0700463 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700464
465 /* We store the pre-calculated register addresses on stack to speed
466 * up the transfer loop. */
467 tx_reg = base + OMAP2_MCSPI_TX0;
468 rx_reg = base + OMAP2_MCSPI_RX0;
469 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
470
Michael Jonesadef6582011-02-25 16:55:11 +0100471 if (c < (word_len>>3))
472 return 0;
473
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700474 if (word_len <= 8) {
475 u8 *rx;
476 const u8 *tx;
477
478 rx = xfer->rx_buf;
479 tx = xfer->tx_buf;
480
481 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800482 c -= 1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700483 if (tx != NULL) {
484 if (mcspi_wait_for_reg_bit(chstat_reg,
485 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
486 dev_err(&spi->dev, "TXS timed out\n");
487 goto out;
488 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900489 dev_vdbg(&spi->dev, "write-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700490 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700491 __raw_writel(*tx++, tx_reg);
492 }
493 if (rx != NULL) {
494 if (mcspi_wait_for_reg_bit(chstat_reg,
495 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
496 dev_err(&spi->dev, "RXS timed out\n");
497 goto out;
498 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000499
500 if (c == 1 && tx == NULL &&
501 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
502 omap2_mcspi_set_enable(spi, 0);
503 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900504 dev_vdbg(&spi->dev, "read-%d %02x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000505 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000506 if (mcspi_wait_for_reg_bit(chstat_reg,
507 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
508 dev_err(&spi->dev,
509 "RXS timed out\n");
510 goto out;
511 }
512 c = 0;
513 } else if (c == 0 && tx == NULL) {
514 omap2_mcspi_set_enable(spi, 0);
515 }
516
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700517 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900518 dev_vdbg(&spi->dev, "read-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700519 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700520 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200521 } while (c);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700522 } else if (word_len <= 16) {
523 u16 *rx;
524 const u16 *tx;
525
526 rx = xfer->rx_buf;
527 tx = xfer->tx_buf;
528 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800529 c -= 2;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700530 if (tx != NULL) {
531 if (mcspi_wait_for_reg_bit(chstat_reg,
532 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
533 dev_err(&spi->dev, "TXS timed out\n");
534 goto out;
535 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900536 dev_vdbg(&spi->dev, "write-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700537 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700538 __raw_writel(*tx++, tx_reg);
539 }
540 if (rx != NULL) {
541 if (mcspi_wait_for_reg_bit(chstat_reg,
542 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
543 dev_err(&spi->dev, "RXS timed out\n");
544 goto out;
545 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000546
547 if (c == 2 && tx == NULL &&
548 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
549 omap2_mcspi_set_enable(spi, 0);
550 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900551 dev_vdbg(&spi->dev, "read-%d %04x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000552 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000553 if (mcspi_wait_for_reg_bit(chstat_reg,
554 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
555 dev_err(&spi->dev,
556 "RXS timed out\n");
557 goto out;
558 }
559 c = 0;
560 } else if (c == 0 && tx == NULL) {
561 omap2_mcspi_set_enable(spi, 0);
562 }
563
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700564 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900565 dev_vdbg(&spi->dev, "read-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700566 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700567 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200568 } while (c >= 2);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700569 } else if (word_len <= 32) {
570 u32 *rx;
571 const u32 *tx;
572
573 rx = xfer->rx_buf;
574 tx = xfer->tx_buf;
575 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800576 c -= 4;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700577 if (tx != NULL) {
578 if (mcspi_wait_for_reg_bit(chstat_reg,
579 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
580 dev_err(&spi->dev, "TXS timed out\n");
581 goto out;
582 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900583 dev_vdbg(&spi->dev, "write-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700584 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700585 __raw_writel(*tx++, tx_reg);
586 }
587 if (rx != NULL) {
588 if (mcspi_wait_for_reg_bit(chstat_reg,
589 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
590 dev_err(&spi->dev, "RXS timed out\n");
591 goto out;
592 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000593
594 if (c == 4 && tx == NULL &&
595 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
596 omap2_mcspi_set_enable(spi, 0);
597 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900598 dev_vdbg(&spi->dev, "read-%d %08x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000599 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000600 if (mcspi_wait_for_reg_bit(chstat_reg,
601 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
602 dev_err(&spi->dev,
603 "RXS timed out\n");
604 goto out;
605 }
606 c = 0;
607 } else if (c == 0 && tx == NULL) {
608 omap2_mcspi_set_enable(spi, 0);
609 }
610
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700611 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900612 dev_vdbg(&spi->dev, "read-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700613 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700614 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200615 } while (c >= 4);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700616 }
617
618 /* for TX_ONLY mode, be sure all words have shifted out */
619 if (xfer->rx_buf == NULL) {
620 if (mcspi_wait_for_reg_bit(chstat_reg,
621 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
622 dev_err(&spi->dev, "TXS timed out\n");
623 } else if (mcspi_wait_for_reg_bit(chstat_reg,
624 OMAP2_MCSPI_CHSTAT_EOT) < 0)
625 dev_err(&spi->dev, "EOT timed out\n");
Jason Wange1993ed2010-10-19 18:03:27 +0800626
627 /* disable chan to purge rx datas received in TX_ONLY transfer,
628 * otherwise these rx datas will affect the direct following
629 * RX_ONLY transfer.
630 */
631 omap2_mcspi_set_enable(spi, 0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700632 }
633out:
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000634 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700635 return count - c;
636}
637
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200638static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
639{
640 u32 div;
641
642 for (div = 0; div < 15; div++)
643 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
644 return div;
645
646 return 15;
647}
648
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700649/* called only when no transfer is active to this device */
650static int omap2_mcspi_setup_transfer(struct spi_device *spi,
651 struct spi_transfer *t)
652{
653 struct omap2_mcspi_cs *cs = spi->controller_state;
654 struct omap2_mcspi *mcspi;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700655 struct spi_master *spi_cntrl;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700656 u32 l = 0, div = 0;
657 u8 word_len = spi->bits_per_word;
Scott Ellis9bd45172010-03-10 14:23:13 -0700658 u32 speed_hz = spi->max_speed_hz;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700659
660 mcspi = spi_master_get_devdata(spi->master);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700661 spi_cntrl = mcspi->master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700662
663 if (t != NULL && t->bits_per_word)
664 word_len = t->bits_per_word;
665
666 cs->word_len = word_len;
667
Scott Ellis9bd45172010-03-10 14:23:13 -0700668 if (t && t->speed_hz)
669 speed_hz = t->speed_hz;
670
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200671 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
672 div = omap2_mcspi_calc_divisor(speed_hz);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700673
Hemanth Va41ae1a2009-09-22 16:46:16 -0700674 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700675
676 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
677 * REVISIT: this controller could support SPI_3WIRE mode.
678 */
679 l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
680 l |= OMAP2_MCSPI_CHCONF_DPE0;
681
682 /* wordlength */
683 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
684 l |= (word_len - 1) << 7;
685
686 /* set chipselect polarity; manage with FORCE */
687 if (!(spi->mode & SPI_CS_HIGH))
688 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
689 else
690 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
691
692 /* set clock divisor */
693 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
694 l |= div << 2;
695
696 /* set SPI mode 0..3 */
697 if (spi->mode & SPI_CPOL)
698 l |= OMAP2_MCSPI_CHCONF_POL;
699 else
700 l &= ~OMAP2_MCSPI_CHCONF_POL;
701 if (spi->mode & SPI_CPHA)
702 l |= OMAP2_MCSPI_CHCONF_PHA;
703 else
704 l &= ~OMAP2_MCSPI_CHCONF_PHA;
705
Hemanth Va41ae1a2009-09-22 16:46:16 -0700706 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700707
708 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200709 OMAP2_MCSPI_MAX_FREQ >> div,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700710 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
711 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
712
713 return 0;
714}
715
716static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
717{
718 struct spi_device *spi = data;
719 struct omap2_mcspi *mcspi;
720 struct omap2_mcspi_dma *mcspi_dma;
721
722 mcspi = spi_master_get_devdata(spi->master);
723 mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
724
725 complete(&mcspi_dma->dma_rx_completion);
726
727 /* We must disable the DMA RX request */
728 omap2_mcspi_set_dma_req(spi, 1, 0);
729}
730
731static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
732{
733 struct spi_device *spi = data;
734 struct omap2_mcspi *mcspi;
735 struct omap2_mcspi_dma *mcspi_dma;
736
737 mcspi = spi_master_get_devdata(spi->master);
738 mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
739
740 complete(&mcspi_dma->dma_tx_completion);
741
742 /* We must disable the DMA TX request */
743 omap2_mcspi_set_dma_req(spi, 0, 0);
744}
745
746static int omap2_mcspi_request_dma(struct spi_device *spi)
747{
748 struct spi_master *master = spi->master;
749 struct omap2_mcspi *mcspi;
750 struct omap2_mcspi_dma *mcspi_dma;
751
752 mcspi = spi_master_get_devdata(master);
753 mcspi_dma = mcspi->dma_channels + spi->chip_select;
754
755 if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
756 omap2_mcspi_dma_rx_callback, spi,
757 &mcspi_dma->dma_rx_channel)) {
758 dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
759 return -EAGAIN;
760 }
761
762 if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
763 omap2_mcspi_dma_tx_callback, spi,
764 &mcspi_dma->dma_tx_channel)) {
765 omap_free_dma(mcspi_dma->dma_rx_channel);
766 mcspi_dma->dma_rx_channel = -1;
767 dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
768 return -EAGAIN;
769 }
770
771 init_completion(&mcspi_dma->dma_rx_completion);
772 init_completion(&mcspi_dma->dma_tx_completion);
773
774 return 0;
775}
776
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700777static int omap2_mcspi_setup(struct spi_device *spi)
778{
779 int ret;
780 struct omap2_mcspi *mcspi;
781 struct omap2_mcspi_dma *mcspi_dma;
782 struct omap2_mcspi_cs *cs = spi->controller_state;
783
David Brownell7d077192009-06-17 16:26:03 -0700784 if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700785 dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
786 spi->bits_per_word);
787 return -EINVAL;
788 }
789
790 mcspi = spi_master_get_devdata(spi->master);
791 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
792
793 if (!cs) {
Shubhrajyoti D1a77b122012-03-17 12:44:01 +0530794 cs = devm_kzalloc(&spi->dev , sizeof *cs, GFP_KERNEL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700795 if (!cs)
796 return -ENOMEM;
797 cs->base = mcspi->base + spi->chip_select * 0x14;
Russell Kinge5480b732008-09-01 21:51:50 +0100798 cs->phys = mcspi->phys + spi->chip_select * 0x14;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700799 cs->chconf0 = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700800 spi->controller_state = cs;
Tero Kristo89c05372009-09-22 16:46:17 -0700801 /* Link this to context save list */
802 list_add_tail(&cs->node,
803 &omap2_mcspi_ctx[mcspi->master->bus_num - 1].cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700804 }
805
806 if (mcspi_dma->dma_rx_channel == -1
807 || mcspi_dma->dma_tx_channel == -1) {
808 ret = omap2_mcspi_request_dma(spi);
809 if (ret < 0)
810 return ret;
811 }
812
Govindraj.R1f1a4382011-02-02 17:52:15 +0530813 ret = omap2_mcspi_enable_clocks(mcspi);
814 if (ret < 0)
815 return ret;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700816
Kyungmin Park86eeb6f2007-10-16 01:27:45 -0700817 ret = omap2_mcspi_setup_transfer(spi, NULL);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700818 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700819
820 return ret;
821}
822
823static void omap2_mcspi_cleanup(struct spi_device *spi)
824{
825 struct omap2_mcspi *mcspi;
826 struct omap2_mcspi_dma *mcspi_dma;
Tero Kristo89c05372009-09-22 16:46:17 -0700827 struct omap2_mcspi_cs *cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700828
829 mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700830
Scott Ellis5e774942010-03-10 14:22:45 -0700831 if (spi->controller_state) {
832 /* Unlink controller state from context save list */
833 cs = spi->controller_state;
834 list_del(&cs->node);
Tero Kristo89c05372009-09-22 16:46:17 -0700835
Scott Ellis5e774942010-03-10 14:22:45 -0700836 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700837
Scott Ellis99f1a432010-05-24 14:20:27 +0000838 if (spi->chip_select < spi->master->num_chipselect) {
839 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
840
841 if (mcspi_dma->dma_rx_channel != -1) {
842 omap_free_dma(mcspi_dma->dma_rx_channel);
843 mcspi_dma->dma_rx_channel = -1;
844 }
845 if (mcspi_dma->dma_tx_channel != -1) {
846 omap_free_dma(mcspi_dma->dma_tx_channel);
847 mcspi_dma->dma_tx_channel = -1;
848 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700849 }
850}
851
852static void omap2_mcspi_work(struct work_struct *work)
853{
854 struct omap2_mcspi *mcspi;
855
856 mcspi = container_of(work, struct omap2_mcspi, work);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700857
Govindraj.R1f1a4382011-02-02 17:52:15 +0530858 if (omap2_mcspi_enable_clocks(mcspi) < 0)
859 return;
860
861 spin_lock_irq(&mcspi->lock);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700862
863 /* We only enable one channel at a time -- the one whose message is
864 * at the head of the queue -- although this controller would gladly
865 * arbitrate among multiple channels. This corresponds to "single
866 * channel" master mode. As a side effect, we need to manage the
867 * chipselect with the FORCE bit ... CS != channel enable.
868 */
869 while (!list_empty(&mcspi->msg_queue)) {
870 struct spi_message *m;
871 struct spi_device *spi;
872 struct spi_transfer *t = NULL;
873 int cs_active = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700874 struct omap2_mcspi_cs *cs;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000875 struct omap2_mcspi_device_config *cd;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700876 int par_override = 0;
877 int status = 0;
878 u32 chconf;
879
880 m = container_of(mcspi->msg_queue.next, struct spi_message,
881 queue);
882
883 list_del_init(&m->queue);
884 spin_unlock_irq(&mcspi->lock);
885
886 spi = m->spi;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700887 cs = spi->controller_state;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000888 cd = spi->controller_data;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700889
890 omap2_mcspi_set_enable(spi, 1);
891 list_for_each_entry(t, &m->transfers, transfer_list) {
892 if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
893 status = -EINVAL;
894 break;
895 }
896 if (par_override || t->speed_hz || t->bits_per_word) {
897 par_override = 1;
898 status = omap2_mcspi_setup_transfer(spi, t);
899 if (status < 0)
900 break;
901 if (!t->speed_hz && !t->bits_per_word)
902 par_override = 0;
903 }
904
905 if (!cs_active) {
906 omap2_mcspi_force_cs(spi, 1);
907 cs_active = 1;
908 }
909
Hemanth Va41ae1a2009-09-22 16:46:16 -0700910 chconf = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700911 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000912 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
913
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700914 if (t->tx_buf == NULL)
915 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
916 else if (t->rx_buf == NULL)
917 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000918
919 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
920 /* Turbo mode is for more than one word */
921 if (t->len > ((cs->word_len + 7) >> 3))
922 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
923 }
924
Hemanth Va41ae1a2009-09-22 16:46:16 -0700925 mcspi_write_chconf0(spi, chconf);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700926
927 if (t->len) {
928 unsigned count;
929
930 /* RX_ONLY mode needs dummy data in TX reg */
931 if (t->tx_buf == NULL)
932 __raw_writel(0, cs->base
933 + OMAP2_MCSPI_TX0);
934
935 if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
936 count = omap2_mcspi_txrx_dma(spi, t);
937 else
938 count = omap2_mcspi_txrx_pio(spi, t);
939 m->actual_length += count;
940
941 if (count != t->len) {
942 status = -EIO;
943 break;
944 }
945 }
946
947 if (t->delay_usecs)
948 udelay(t->delay_usecs);
949
950 /* ignore the "leave it on after last xfer" hint */
951 if (t->cs_change) {
952 omap2_mcspi_force_cs(spi, 0);
953 cs_active = 0;
954 }
955 }
956
957 /* Restore defaults if they were overriden */
958 if (par_override) {
959 par_override = 0;
960 status = omap2_mcspi_setup_transfer(spi, NULL);
961 }
962
963 if (cs_active)
964 omap2_mcspi_force_cs(spi, 0);
965
966 omap2_mcspi_set_enable(spi, 0);
967
968 m->status = status;
969 m->complete(m->context);
970
971 spin_lock_irq(&mcspi->lock);
972 }
973
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700974 spin_unlock_irq(&mcspi->lock);
Govindraj.R1f1a4382011-02-02 17:52:15 +0530975
976 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700977}
978
979static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
980{
981 struct omap2_mcspi *mcspi;
982 unsigned long flags;
983 struct spi_transfer *t;
984
985 m->actual_length = 0;
986 m->status = 0;
987
988 /* reject invalid messages and transfers */
989 if (list_empty(&m->transfers) || !m->complete)
990 return -EINVAL;
991 list_for_each_entry(t, &m->transfers, transfer_list) {
992 const void *tx_buf = t->tx_buf;
993 void *rx_buf = t->rx_buf;
994 unsigned len = t->len;
995
996 if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
997 || (len && !(rx_buf || tx_buf))
998 || (t->bits_per_word &&
999 ( t->bits_per_word < 4
1000 || t->bits_per_word > 32))) {
1001 dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
1002 t->speed_hz,
1003 len,
1004 tx_buf ? "tx" : "",
1005 rx_buf ? "rx" : "",
1006 t->bits_per_word);
1007 return -EINVAL;
1008 }
Hannu Heikkinen57d9c102011-02-24 21:31:33 +02001009 if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
1010 dev_dbg(&spi->dev, "speed_hz %d below minimum %d Hz\n",
1011 t->speed_hz,
1012 OMAP2_MCSPI_MAX_FREQ >> 15);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001013 return -EINVAL;
1014 }
1015
1016 if (m->is_dma_mapped || len < DMA_MIN_BYTES)
1017 continue;
1018
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001019 if (tx_buf != NULL) {
1020 t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
1021 len, DMA_TO_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001022 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001023 dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
1024 'T', len);
1025 return -EINVAL;
1026 }
1027 }
1028 if (rx_buf != NULL) {
1029 t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
1030 DMA_FROM_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001031 if (dma_mapping_error(&spi->dev, t->rx_dma)) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001032 dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
1033 'R', len);
1034 if (tx_buf != NULL)
Russell King - ARM Linux07fe0352011-01-07 15:49:20 +00001035 dma_unmap_single(&spi->dev, t->tx_dma,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001036 len, DMA_TO_DEVICE);
1037 return -EINVAL;
1038 }
1039 }
1040 }
1041
1042 mcspi = spi_master_get_devdata(spi->master);
1043
1044 spin_lock_irqsave(&mcspi->lock, flags);
1045 list_add_tail(&m->queue, &mcspi->msg_queue);
Shubhrajyoti D2856ac12011-10-28 17:14:17 +05301046 queue_work(mcspi->wq, &mcspi->work);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001047 spin_unlock_irqrestore(&mcspi->lock, flags);
1048
1049 return 0;
1050}
1051
Govindraj.R1f1a4382011-02-02 17:52:15 +05301052static int __init omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001053{
1054 struct spi_master *master = mcspi->master;
1055 u32 tmp;
Govindraj.R1f1a4382011-02-02 17:52:15 +05301056 int ret = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001057
Govindraj.R1f1a4382011-02-02 17:52:15 +05301058 ret = omap2_mcspi_enable_clocks(mcspi);
1059 if (ret < 0)
1060 return ret;
Jouni Hoganderddb22192009-07-29 15:02:11 -07001061
Hemanth Va41ae1a2009-09-22 16:46:16 -07001062 tmp = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
1063 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, tmp);
1064 omap2_mcspi_ctx[master->bus_num - 1].wakeupenable = tmp;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001065
1066 omap2_mcspi_set_master_mode(master);
Hemanth Va41ae1a2009-09-22 16:46:16 -07001067 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001068 return 0;
1069}
1070
Govindraj.R1f1a4382011-02-02 17:52:15 +05301071static int omap_mcspi_runtime_resume(struct device *dev)
1072{
1073 struct omap2_mcspi *mcspi;
1074 struct spi_master *master;
1075
1076 master = dev_get_drvdata(dev);
1077 mcspi = spi_master_get_devdata(master);
1078 omap2_mcspi_restore_ctx(mcspi);
1079
1080 return 0;
1081}
1082
Benoit Coussond5a80032012-02-15 18:37:34 +01001083static struct omap2_mcspi_platform_config omap2_pdata = {
1084 .regs_offset = 0,
1085};
1086
1087static struct omap2_mcspi_platform_config omap4_pdata = {
1088 .regs_offset = OMAP4_MCSPI_REG_OFFSET,
1089};
1090
1091static const struct of_device_id omap_mcspi_of_match[] = {
1092 {
1093 .compatible = "ti,omap2-mcspi",
1094 .data = &omap2_pdata,
1095 },
1096 {
1097 .compatible = "ti,omap4-mcspi",
1098 .data = &omap4_pdata,
1099 },
1100 { },
1101};
1102MODULE_DEVICE_TABLE(of, omap_mcspi_of_match);
Girishccc7bae2008-02-06 01:38:16 -08001103
Felipe Balbi7d6b6d82012-03-14 11:18:30 +02001104static int __devinit omap2_mcspi_probe(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001105{
1106 struct spi_master *master;
Benoit Coussond5a80032012-02-15 18:37:34 +01001107 struct omap2_mcspi_platform_config *pdata;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001108 struct omap2_mcspi *mcspi;
1109 struct resource *r;
1110 int status = 0, i;
Shubhrajyoti D2856ac12011-10-28 17:14:17 +05301111 char wq_name[20];
Benoit Coussond5a80032012-02-15 18:37:34 +01001112 u32 regs_offset = 0;
1113 static int bus_num = 1;
1114 struct device_node *node = pdev->dev.of_node;
1115 const struct of_device_id *match;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001116
1117 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1118 if (master == NULL) {
1119 dev_dbg(&pdev->dev, "master allocation failed\n");
1120 return -ENOMEM;
1121 }
1122
David Brownelle7db06b2009-06-17 16:26:04 -07001123 /* the spi->mode bits understood by this driver: */
1124 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1125
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001126 master->setup = omap2_mcspi_setup;
1127 master->transfer = omap2_mcspi_transfer;
1128 master->cleanup = omap2_mcspi_cleanup;
Benoit Coussond5a80032012-02-15 18:37:34 +01001129 master->dev.of_node = node;
1130
1131 match = of_match_device(omap_mcspi_of_match, &pdev->dev);
1132 if (match) {
1133 u32 num_cs = 1; /* default number of chipselect */
1134 pdata = match->data;
1135
1136 of_property_read_u32(node, "ti,spi-num-cs", &num_cs);
1137 master->num_chipselect = num_cs;
1138 master->bus_num = bus_num++;
1139 } else {
1140 pdata = pdev->dev.platform_data;
1141 master->num_chipselect = pdata->num_cs;
1142 if (pdev->id != -1)
1143 master->bus_num = pdev->id;
1144 }
1145 regs_offset = pdata->regs_offset;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001146
1147 dev_set_drvdata(&pdev->dev, master);
1148
1149 mcspi = spi_master_get_devdata(master);
1150 mcspi->master = master;
1151
Shubhrajyoti D2856ac12011-10-28 17:14:17 +05301152 sprintf(wq_name, "omap2_mcspi/%d", master->bus_num);
1153 mcspi->wq = alloc_workqueue(wq_name, WQ_MEM_RECLAIM, 1);
1154 if (mcspi->wq == NULL) {
1155 status = -ENOMEM;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301156 goto free_master;
Shubhrajyoti D2856ac12011-10-28 17:14:17 +05301157 }
1158
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001159 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1160 if (r == NULL) {
1161 status = -ENODEV;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301162 goto free_master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001163 }
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301164
Benoit Coussond5a80032012-02-15 18:37:34 +01001165 r->start += regs_offset;
1166 r->end += regs_offset;
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301167 mcspi->phys = r->start;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001168
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301169 mcspi->base = devm_request_and_ioremap(&pdev->dev, r);
Russell King55c381e2008-09-04 14:07:22 +01001170 if (!mcspi->base) {
1171 dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
1172 status = -ENOMEM;
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301173 goto free_master;
Russell King55c381e2008-09-04 14:07:22 +01001174 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001175
Govindraj.R1f1a4382011-02-02 17:52:15 +05301176 mcspi->dev = &pdev->dev;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001177 INIT_WORK(&mcspi->work, omap2_mcspi_work);
1178
1179 spin_lock_init(&mcspi->lock);
1180 INIT_LIST_HEAD(&mcspi->msg_queue);
Tero Kristo89c05372009-09-22 16:46:17 -07001181 INIT_LIST_HEAD(&omap2_mcspi_ctx[master->bus_num - 1].cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001182
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001183 mcspi->dma_channels = kcalloc(master->num_chipselect,
1184 sizeof(struct omap2_mcspi_dma),
1185 GFP_KERNEL);
1186
1187 if (mcspi->dma_channels == NULL)
Shubhrajyoti D1a77b122012-03-17 12:44:01 +05301188 goto free_master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001189
Charulatha V1a5d8192011-02-02 17:52:14 +05301190 for (i = 0; i < master->num_chipselect; i++) {
1191 char dma_ch_name[14];
1192 struct resource *dma_res;
1193
1194 sprintf(dma_ch_name, "rx%d", i);
1195 dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1196 dma_ch_name);
1197 if (!dma_res) {
1198 dev_dbg(&pdev->dev, "cannot get DMA RX channel\n");
1199 status = -ENODEV;
1200 break;
1201 }
1202
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001203 mcspi->dma_channels[i].dma_rx_channel = -1;
Charulatha V1a5d8192011-02-02 17:52:14 +05301204 mcspi->dma_channels[i].dma_rx_sync_dev = dma_res->start;
1205 sprintf(dma_ch_name, "tx%d", i);
1206 dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1207 dma_ch_name);
1208 if (!dma_res) {
1209 dev_dbg(&pdev->dev, "cannot get DMA TX channel\n");
1210 status = -ENODEV;
1211 break;
1212 }
1213
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001214 mcspi->dma_channels[i].dma_tx_channel = -1;
Charulatha V1a5d8192011-02-02 17:52:14 +05301215 mcspi->dma_channels[i].dma_tx_sync_dev = dma_res->start;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001216 }
1217
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301218 if (status < 0)
1219 goto dma_chnl_free;
1220
Govindraj.R1f1a4382011-02-02 17:52:15 +05301221 pm_runtime_enable(&pdev->dev);
1222
1223 if (status || omap2_mcspi_master_setup(mcspi) < 0)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301224 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001225
1226 status = spi_register_master(master);
1227 if (status < 0)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301228 goto err_spi_register;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001229
1230 return status;
1231
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301232err_spi_register:
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001233 spi_master_put(master);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301234disable_pm:
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301235 pm_runtime_disable(&pdev->dev);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301236dma_chnl_free:
Govindraj.R1f1a4382011-02-02 17:52:15 +05301237 kfree(mcspi->dma_channels);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301238free_master:
1239 kfree(master);
1240 platform_set_drvdata(pdev, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001241 return status;
1242}
1243
Felipe Balbi7d6b6d82012-03-14 11:18:30 +02001244static int __devexit omap2_mcspi_remove(struct platform_device *pdev)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001245{
1246 struct spi_master *master;
1247 struct omap2_mcspi *mcspi;
1248 struct omap2_mcspi_dma *dma_channels;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001249
1250 master = dev_get_drvdata(&pdev->dev);
1251 mcspi = spi_master_get_devdata(master);
1252 dma_channels = mcspi->dma_channels;
1253
Govindraj.R1f1a4382011-02-02 17:52:15 +05301254 omap2_mcspi_disable_clocks(mcspi);
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301255 pm_runtime_disable(&pdev->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001256
1257 spi_unregister_master(master);
1258 kfree(dma_channels);
Shubhrajyoti D2856ac12011-10-28 17:14:17 +05301259 destroy_workqueue(mcspi->wq);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301260 platform_set_drvdata(pdev, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001261
1262 return 0;
1263}
1264
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001265/* work with hotplug and coldplug */
1266MODULE_ALIAS("platform:omap2_mcspi");
1267
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001268#ifdef CONFIG_SUSPEND
1269/*
1270 * When SPI wake up from off-mode, CS is in activate state. If it was in
1271 * unactive state when driver was suspend, then force it to unactive state at
1272 * wake up.
1273 */
1274static int omap2_mcspi_resume(struct device *dev)
1275{
1276 struct spi_master *master = dev_get_drvdata(dev);
1277 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1278 struct omap2_mcspi_cs *cs;
1279
1280 omap2_mcspi_enable_clocks(mcspi);
1281 list_for_each_entry(cs, &omap2_mcspi_ctx[master->bus_num - 1].cs,
1282 node) {
1283 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
1284
1285 /*
1286 * We need to toggle CS state for OMAP take this
1287 * change in account.
1288 */
1289 MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 1);
1290 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1291 MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 0);
1292 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1293 }
1294 }
1295 omap2_mcspi_disable_clocks(mcspi);
1296 return 0;
1297}
1298#else
1299#define omap2_mcspi_resume NULL
1300#endif
1301
1302static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1303 .resume = omap2_mcspi_resume,
Govindraj.R1f1a4382011-02-02 17:52:15 +05301304 .runtime_resume = omap_mcspi_runtime_resume,
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001305};
1306
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001307static struct platform_driver omap2_mcspi_driver = {
1308 .driver = {
1309 .name = "omap2_mcspi",
1310 .owner = THIS_MODULE,
Benoit Coussond5a80032012-02-15 18:37:34 +01001311 .pm = &omap2_mcspi_pm_ops,
1312 .of_match_table = omap_mcspi_of_match,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001313 },
Felipe Balbi7d6b6d82012-03-14 11:18:30 +02001314 .probe = omap2_mcspi_probe,
1315 .remove = __devexit_p(omap2_mcspi_remove),
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001316};
1317
Felipe Balbi9fdca9d2012-03-14 11:18:31 +02001318module_platform_driver(omap2_mcspi_driver);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001319MODULE_LICENSE("GPL");