blob: 1a2d0b8bdd6a6b3f2fa9d5b24df12fce264d195a [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
6 * Juha Yrjölä <juha.yrjola@nokia.com>
7 *
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>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070036
37#include <linux/spi/spi.h>
38
Tony Lindgrence491cf2009-10-20 09:40:47 -070039#include <plat/dma.h>
40#include <plat/clock.h>
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +000041#include <plat/mcspi.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070042
43#define OMAP2_MCSPI_MAX_FREQ 48000000
44
Hemanth Va41ae1a2009-09-22 16:46:16 -070045/* OMAP2 has 3 SPI controllers, while OMAP3 has 4 */
46#define OMAP2_MCSPI_MAX_CTRL 4
47
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070048#define OMAP2_MCSPI_REVISION 0x00
49#define OMAP2_MCSPI_SYSCONFIG 0x10
50#define OMAP2_MCSPI_SYSSTATUS 0x14
51#define OMAP2_MCSPI_IRQSTATUS 0x18
52#define OMAP2_MCSPI_IRQENABLE 0x1c
53#define OMAP2_MCSPI_WAKEUPENABLE 0x20
54#define OMAP2_MCSPI_SYST 0x24
55#define OMAP2_MCSPI_MODULCTRL 0x28
56
57/* per-channel banks, 0x14 bytes each, first is: */
58#define OMAP2_MCSPI_CHCONF0 0x2c
59#define OMAP2_MCSPI_CHSTAT0 0x30
60#define OMAP2_MCSPI_CHCTRL0 0x34
61#define OMAP2_MCSPI_TX0 0x38
62#define OMAP2_MCSPI_RX0 0x3c
63
64/* per-register bitmasks: */
65
Jouni Hogander7a8fa722009-09-22 16:45:58 -070066#define OMAP2_MCSPI_SYSCONFIG_SMARTIDLE BIT(4)
67#define OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP BIT(2)
68#define OMAP2_MCSPI_SYSCONFIG_AUTOIDLE BIT(0)
69#define OMAP2_MCSPI_SYSCONFIG_SOFTRESET BIT(1)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070070
Jouni Hogander7a8fa722009-09-22 16:45:58 -070071#define OMAP2_MCSPI_SYSSTATUS_RESETDONE BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070072
Jouni Hogander7a8fa722009-09-22 16:45:58 -070073#define OMAP2_MCSPI_MODULCTRL_SINGLE BIT(0)
74#define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
75#define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070076
Jouni Hogander7a8fa722009-09-22 16:45:58 -070077#define OMAP2_MCSPI_CHCONF_PHA BIT(0)
78#define OMAP2_MCSPI_CHCONF_POL BIT(1)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070079#define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070080#define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070081#define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070082#define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
83#define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070084#define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070085#define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
86#define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
87#define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
88#define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
89#define OMAP2_MCSPI_CHCONF_IS BIT(18)
90#define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
91#define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070092
Jouni Hogander7a8fa722009-09-22 16:45:58 -070093#define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
94#define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
95#define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070096
Jouni Hogander7a8fa722009-09-22 16:45:58 -070097#define OMAP2_MCSPI_CHCTRL_EN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070098
Jouni Hogander7a8fa722009-09-22 16:45:58 -070099#define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700100
101/* We have 2 DMA channels per CS, one for RX and one for TX */
102struct omap2_mcspi_dma {
103 int dma_tx_channel;
104 int dma_rx_channel;
105
106 int dma_tx_sync_dev;
107 int dma_rx_sync_dev;
108
109 struct completion dma_tx_completion;
110 struct completion dma_rx_completion;
111};
112
113/* use PIO for small transfers, avoiding DMA setup/teardown overhead and
114 * cache operations; better heuristics consider wordsize and bitrate.
115 */
Roman Tereshonkov8b66c132010-04-12 09:07:54 +0000116#define DMA_MIN_BYTES 160
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700117
118
119struct omap2_mcspi {
120 struct work_struct work;
121 /* lock protects queue and registers */
122 spinlock_t lock;
123 struct list_head msg_queue;
124 struct spi_master *master;
125 struct clk *ick;
126 struct clk *fck;
127 /* 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;
132};
133
134struct omap2_mcspi_cs {
135 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100136 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700137 int word_len;
Tero Kristo89c05372009-09-22 16:46:17 -0700138 struct list_head node;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700139 /* Context save and restore shadow register */
140 u32 chconf0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700141};
142
Hemanth Va41ae1a2009-09-22 16:46:16 -0700143/* used for context save and restore, structure members to be updated whenever
144 * corresponding registers are modified.
145 */
146struct omap2_mcspi_regs {
147 u32 sysconfig;
148 u32 modulctrl;
149 u32 wakeupenable;
Tero Kristo89c05372009-09-22 16:46:17 -0700150 struct list_head cs;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700151};
152
153static struct omap2_mcspi_regs omap2_mcspi_ctx[OMAP2_MCSPI_MAX_CTRL];
154
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700155static struct workqueue_struct *omap2_mcspi_wq;
156
157#define MOD_REG_BIT(val, mask, set) do { \
158 if (set) \
159 val |= mask; \
160 else \
161 val &= ~mask; \
162} while (0)
163
164static inline void mcspi_write_reg(struct spi_master *master,
165 int idx, u32 val)
166{
167 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
168
169 __raw_writel(val, mcspi->base + idx);
170}
171
172static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
173{
174 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
175
176 return __raw_readl(mcspi->base + idx);
177}
178
179static inline void mcspi_write_cs_reg(const struct spi_device *spi,
180 int idx, u32 val)
181{
182 struct omap2_mcspi_cs *cs = spi->controller_state;
183
184 __raw_writel(val, cs->base + idx);
185}
186
187static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
188{
189 struct omap2_mcspi_cs *cs = spi->controller_state;
190
191 return __raw_readl(cs->base + idx);
192}
193
Hemanth Va41ae1a2009-09-22 16:46:16 -0700194static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
195{
196 struct omap2_mcspi_cs *cs = spi->controller_state;
197
198 return cs->chconf0;
199}
200
201static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
202{
203 struct omap2_mcspi_cs *cs = spi->controller_state;
204
205 cs->chconf0 = val;
206 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
Roman Tereshonkova330ce22010-03-15 09:06:28 +0000207 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700208}
209
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700210static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
211 int is_read, int enable)
212{
213 u32 l, rw;
214
Hemanth Va41ae1a2009-09-22 16:46:16 -0700215 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700216
217 if (is_read) /* 1 is read, 0 write */
218 rw = OMAP2_MCSPI_CHCONF_DMAR;
219 else
220 rw = OMAP2_MCSPI_CHCONF_DMAW;
221
222 MOD_REG_BIT(l, rw, enable);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700223 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700224}
225
226static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
227{
228 u32 l;
229
230 l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
231 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000232 /* Flash post-writes */
233 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700234}
235
236static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
237{
238 u32 l;
239
Hemanth Va41ae1a2009-09-22 16:46:16 -0700240 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700241 MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700242 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700243}
244
245static void omap2_mcspi_set_master_mode(struct spi_master *master)
246{
247 u32 l;
248
249 /* setup when switching from (reset default) slave mode
250 * to single-channel master mode
251 */
252 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
253 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
254 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
255 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
256 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700257
258 omap2_mcspi_ctx[master->bus_num - 1].modulctrl = l;
259}
260
261static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
262{
263 struct spi_master *spi_cntrl;
Tero Kristo89c05372009-09-22 16:46:17 -0700264 struct omap2_mcspi_cs *cs;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700265 spi_cntrl = mcspi->master;
266
267 /* McSPI: context restore */
268 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL,
269 omap2_mcspi_ctx[spi_cntrl->bus_num - 1].modulctrl);
270
271 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_SYSCONFIG,
272 omap2_mcspi_ctx[spi_cntrl->bus_num - 1].sysconfig);
273
274 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE,
275 omap2_mcspi_ctx[spi_cntrl->bus_num - 1].wakeupenable);
Tero Kristo89c05372009-09-22 16:46:17 -0700276
277 list_for_each_entry(cs, &omap2_mcspi_ctx[spi_cntrl->bus_num - 1].cs,
278 node)
279 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700280}
281static void omap2_mcspi_disable_clocks(struct omap2_mcspi *mcspi)
282{
283 clk_disable(mcspi->ick);
284 clk_disable(mcspi->fck);
285}
286
287static int omap2_mcspi_enable_clocks(struct omap2_mcspi *mcspi)
288{
289 if (clk_enable(mcspi->ick))
290 return -ENODEV;
291 if (clk_enable(mcspi->fck))
292 return -ENODEV;
293
294 omap2_mcspi_restore_ctx(mcspi);
295
296 return 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700297}
298
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300299static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
300{
301 unsigned long timeout;
302
303 timeout = jiffies + msecs_to_jiffies(1000);
304 while (!(__raw_readl(reg) & bit)) {
305 if (time_after(jiffies, timeout))
306 return -1;
307 cpu_relax();
308 }
309 return 0;
310}
311
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700312static unsigned
313omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
314{
315 struct omap2_mcspi *mcspi;
316 struct omap2_mcspi_cs *cs = spi->controller_state;
317 struct omap2_mcspi_dma *mcspi_dma;
318 unsigned int count, c;
319 unsigned long base, tx_reg, rx_reg;
320 int word_len, data_type, element_count;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000321 int elements;
322 u32 l;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700323 u8 * rx;
324 const u8 * tx;
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300325 void __iomem *chstat_reg;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700326
327 mcspi = spi_master_get_devdata(spi->master);
328 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000329 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700330
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300331 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
332
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700333 count = xfer->len;
334 c = count;
335 word_len = cs->word_len;
336
Russell Kinge5480b732008-09-01 21:51:50 +0100337 base = cs->phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700338 tx_reg = base + OMAP2_MCSPI_TX0;
339 rx_reg = base + OMAP2_MCSPI_RX0;
340 rx = xfer->rx_buf;
341 tx = xfer->tx_buf;
342
343 if (word_len <= 8) {
344 data_type = OMAP_DMA_DATA_TYPE_S8;
345 element_count = count;
346 } else if (word_len <= 16) {
347 data_type = OMAP_DMA_DATA_TYPE_S16;
348 element_count = count >> 1;
349 } else /* word_len <= 32 */ {
350 data_type = OMAP_DMA_DATA_TYPE_S32;
351 element_count = count >> 2;
352 }
353
354 if (tx != NULL) {
355 omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
356 data_type, element_count, 1,
357 OMAP_DMA_SYNC_ELEMENT,
358 mcspi_dma->dma_tx_sync_dev, 0);
359
360 omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
361 OMAP_DMA_AMODE_CONSTANT,
362 tx_reg, 0, 0);
363
364 omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
365 OMAP_DMA_AMODE_POST_INC,
366 xfer->tx_dma, 0, 0);
367 }
368
369 if (rx != NULL) {
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000370 elements = element_count - 1;
371 if (l & OMAP2_MCSPI_CHCONF_TURBO)
372 elements--;
373
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700374 omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000375 data_type, elements, 1,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700376 OMAP_DMA_SYNC_ELEMENT,
377 mcspi_dma->dma_rx_sync_dev, 1);
378
379 omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
380 OMAP_DMA_AMODE_CONSTANT,
381 rx_reg, 0, 0);
382
383 omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
384 OMAP_DMA_AMODE_POST_INC,
385 xfer->rx_dma, 0, 0);
386 }
387
388 if (tx != NULL) {
389 omap_start_dma(mcspi_dma->dma_tx_channel);
390 omap2_mcspi_set_dma_req(spi, 0, 1);
391 }
392
393 if (rx != NULL) {
394 omap_start_dma(mcspi_dma->dma_rx_channel);
395 omap2_mcspi_set_dma_req(spi, 1, 1);
396 }
397
398 if (tx != NULL) {
399 wait_for_completion(&mcspi_dma->dma_tx_completion);
Russell King - ARM Linux07fe0352011-01-07 15:49:20 +0000400 dma_unmap_single(&spi->dev, xfer->tx_dma, count, DMA_TO_DEVICE);
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300401
402 /* for TX_ONLY mode, be sure all words have shifted out */
403 if (rx == NULL) {
404 if (mcspi_wait_for_reg_bit(chstat_reg,
405 OMAP2_MCSPI_CHSTAT_TXS) < 0)
406 dev_err(&spi->dev, "TXS timed out\n");
407 else if (mcspi_wait_for_reg_bit(chstat_reg,
408 OMAP2_MCSPI_CHSTAT_EOT) < 0)
409 dev_err(&spi->dev, "EOT timed out\n");
410 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700411 }
412
413 if (rx != NULL) {
414 wait_for_completion(&mcspi_dma->dma_rx_completion);
Russell King - ARM Linux07fe0352011-01-07 15:49:20 +0000415 dma_unmap_single(&spi->dev, xfer->rx_dma, count, DMA_FROM_DEVICE);
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700416 omap2_mcspi_set_enable(spi, 0);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000417
418 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
419
420 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
421 & OMAP2_MCSPI_CHSTAT_RXS)) {
422 u32 w;
423
424 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
425 if (word_len <= 8)
426 ((u8 *)xfer->rx_buf)[elements++] = w;
427 else if (word_len <= 16)
428 ((u16 *)xfer->rx_buf)[elements++] = w;
429 else /* word_len <= 32 */
430 ((u32 *)xfer->rx_buf)[elements++] = w;
431 } else {
432 dev_err(&spi->dev,
433 "DMA RX penultimate word empty");
434 count -= (word_len <= 8) ? 2 :
435 (word_len <= 16) ? 4 :
436 /* word_len <= 32 */ 8;
437 omap2_mcspi_set_enable(spi, 1);
438 return count;
439 }
440 }
441
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700442 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
443 & OMAP2_MCSPI_CHSTAT_RXS)) {
444 u32 w;
445
446 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
447 if (word_len <= 8)
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000448 ((u8 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700449 else if (word_len <= 16)
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000450 ((u16 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700451 else /* word_len <= 32 */
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000452 ((u32 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700453 } else {
454 dev_err(&spi->dev, "DMA RX last word empty");
455 count -= (word_len <= 8) ? 1 :
456 (word_len <= 16) ? 2 :
457 /* word_len <= 32 */ 4;
458 }
459 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700460 }
461 return count;
462}
463
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700464static unsigned
465omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
466{
467 struct omap2_mcspi *mcspi;
468 struct omap2_mcspi_cs *cs = spi->controller_state;
469 unsigned int count, c;
470 u32 l;
471 void __iomem *base = cs->base;
472 void __iomem *tx_reg;
473 void __iomem *rx_reg;
474 void __iomem *chstat_reg;
475 int word_len;
476
477 mcspi = spi_master_get_devdata(spi->master);
478 count = xfer->len;
479 c = count;
480 word_len = cs->word_len;
481
Hemanth Va41ae1a2009-09-22 16:46:16 -0700482 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700483
484 /* We store the pre-calculated register addresses on stack to speed
485 * up the transfer loop. */
486 tx_reg = base + OMAP2_MCSPI_TX0;
487 rx_reg = base + OMAP2_MCSPI_RX0;
488 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
489
490 if (word_len <= 8) {
491 u8 *rx;
492 const u8 *tx;
493
494 rx = xfer->rx_buf;
495 tx = xfer->tx_buf;
496
497 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800498 c -= 1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700499 if (tx != NULL) {
500 if (mcspi_wait_for_reg_bit(chstat_reg,
501 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
502 dev_err(&spi->dev, "TXS timed out\n");
503 goto out;
504 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900505 dev_vdbg(&spi->dev, "write-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700506 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700507 __raw_writel(*tx++, tx_reg);
508 }
509 if (rx != NULL) {
510 if (mcspi_wait_for_reg_bit(chstat_reg,
511 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
512 dev_err(&spi->dev, "RXS timed out\n");
513 goto out;
514 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000515
516 if (c == 1 && tx == NULL &&
517 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
518 omap2_mcspi_set_enable(spi, 0);
519 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900520 dev_vdbg(&spi->dev, "read-%d %02x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000521 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000522 if (mcspi_wait_for_reg_bit(chstat_reg,
523 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
524 dev_err(&spi->dev,
525 "RXS timed out\n");
526 goto out;
527 }
528 c = 0;
529 } else if (c == 0 && tx == NULL) {
530 omap2_mcspi_set_enable(spi, 0);
531 }
532
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700533 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900534 dev_vdbg(&spi->dev, "read-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700535 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700536 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700537 } while (c);
538 } else if (word_len <= 16) {
539 u16 *rx;
540 const u16 *tx;
541
542 rx = xfer->rx_buf;
543 tx = xfer->tx_buf;
544 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800545 c -= 2;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700546 if (tx != NULL) {
547 if (mcspi_wait_for_reg_bit(chstat_reg,
548 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
549 dev_err(&spi->dev, "TXS timed out\n");
550 goto out;
551 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900552 dev_vdbg(&spi->dev, "write-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700553 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700554 __raw_writel(*tx++, tx_reg);
555 }
556 if (rx != NULL) {
557 if (mcspi_wait_for_reg_bit(chstat_reg,
558 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
559 dev_err(&spi->dev, "RXS timed out\n");
560 goto out;
561 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000562
563 if (c == 2 && tx == NULL &&
564 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
565 omap2_mcspi_set_enable(spi, 0);
566 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900567 dev_vdbg(&spi->dev, "read-%d %04x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000568 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000569 if (mcspi_wait_for_reg_bit(chstat_reg,
570 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
571 dev_err(&spi->dev,
572 "RXS timed out\n");
573 goto out;
574 }
575 c = 0;
576 } else if (c == 0 && tx == NULL) {
577 omap2_mcspi_set_enable(spi, 0);
578 }
579
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700580 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900581 dev_vdbg(&spi->dev, "read-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700582 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700583 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700584 } while (c);
585 } else if (word_len <= 32) {
586 u32 *rx;
587 const u32 *tx;
588
589 rx = xfer->rx_buf;
590 tx = xfer->tx_buf;
591 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800592 c -= 4;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700593 if (tx != NULL) {
594 if (mcspi_wait_for_reg_bit(chstat_reg,
595 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
596 dev_err(&spi->dev, "TXS timed out\n");
597 goto out;
598 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900599 dev_vdbg(&spi->dev, "write-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700600 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700601 __raw_writel(*tx++, tx_reg);
602 }
603 if (rx != NULL) {
604 if (mcspi_wait_for_reg_bit(chstat_reg,
605 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
606 dev_err(&spi->dev, "RXS timed out\n");
607 goto out;
608 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000609
610 if (c == 4 && tx == NULL &&
611 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
612 omap2_mcspi_set_enable(spi, 0);
613 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900614 dev_vdbg(&spi->dev, "read-%d %08x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000615 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000616 if (mcspi_wait_for_reg_bit(chstat_reg,
617 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
618 dev_err(&spi->dev,
619 "RXS timed out\n");
620 goto out;
621 }
622 c = 0;
623 } else if (c == 0 && tx == NULL) {
624 omap2_mcspi_set_enable(spi, 0);
625 }
626
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700627 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900628 dev_vdbg(&spi->dev, "read-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700629 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700630 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700631 } while (c);
632 }
633
634 /* for TX_ONLY mode, be sure all words have shifted out */
635 if (xfer->rx_buf == NULL) {
636 if (mcspi_wait_for_reg_bit(chstat_reg,
637 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
638 dev_err(&spi->dev, "TXS timed out\n");
639 } else if (mcspi_wait_for_reg_bit(chstat_reg,
640 OMAP2_MCSPI_CHSTAT_EOT) < 0)
641 dev_err(&spi->dev, "EOT timed out\n");
Jason Wange1993ed2010-10-19 18:03:27 +0800642
643 /* disable chan to purge rx datas received in TX_ONLY transfer,
644 * otherwise these rx datas will affect the direct following
645 * RX_ONLY transfer.
646 */
647 omap2_mcspi_set_enable(spi, 0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700648 }
649out:
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000650 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700651 return count - c;
652}
653
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200654static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
655{
656 u32 div;
657
658 for (div = 0; div < 15; div++)
659 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
660 return div;
661
662 return 15;
663}
664
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700665/* called only when no transfer is active to this device */
666static int omap2_mcspi_setup_transfer(struct spi_device *spi,
667 struct spi_transfer *t)
668{
669 struct omap2_mcspi_cs *cs = spi->controller_state;
670 struct omap2_mcspi *mcspi;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700671 struct spi_master *spi_cntrl;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700672 u32 l = 0, div = 0;
673 u8 word_len = spi->bits_per_word;
Scott Ellis9bd45172010-03-10 14:23:13 -0700674 u32 speed_hz = spi->max_speed_hz;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700675
676 mcspi = spi_master_get_devdata(spi->master);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700677 spi_cntrl = mcspi->master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700678
679 if (t != NULL && t->bits_per_word)
680 word_len = t->bits_per_word;
681
682 cs->word_len = word_len;
683
Scott Ellis9bd45172010-03-10 14:23:13 -0700684 if (t && t->speed_hz)
685 speed_hz = t->speed_hz;
686
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200687 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
688 div = omap2_mcspi_calc_divisor(speed_hz);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700689
Hemanth Va41ae1a2009-09-22 16:46:16 -0700690 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700691
692 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
693 * REVISIT: this controller could support SPI_3WIRE mode.
694 */
695 l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
696 l |= OMAP2_MCSPI_CHCONF_DPE0;
697
698 /* wordlength */
699 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
700 l |= (word_len - 1) << 7;
701
702 /* set chipselect polarity; manage with FORCE */
703 if (!(spi->mode & SPI_CS_HIGH))
704 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
705 else
706 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
707
708 /* set clock divisor */
709 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
710 l |= div << 2;
711
712 /* set SPI mode 0..3 */
713 if (spi->mode & SPI_CPOL)
714 l |= OMAP2_MCSPI_CHCONF_POL;
715 else
716 l &= ~OMAP2_MCSPI_CHCONF_POL;
717 if (spi->mode & SPI_CPHA)
718 l |= OMAP2_MCSPI_CHCONF_PHA;
719 else
720 l &= ~OMAP2_MCSPI_CHCONF_PHA;
721
Hemanth Va41ae1a2009-09-22 16:46:16 -0700722 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700723
724 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200725 OMAP2_MCSPI_MAX_FREQ >> div,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700726 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
727 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
728
729 return 0;
730}
731
732static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
733{
734 struct spi_device *spi = data;
735 struct omap2_mcspi *mcspi;
736 struct omap2_mcspi_dma *mcspi_dma;
737
738 mcspi = spi_master_get_devdata(spi->master);
739 mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
740
741 complete(&mcspi_dma->dma_rx_completion);
742
743 /* We must disable the DMA RX request */
744 omap2_mcspi_set_dma_req(spi, 1, 0);
745}
746
747static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
748{
749 struct spi_device *spi = data;
750 struct omap2_mcspi *mcspi;
751 struct omap2_mcspi_dma *mcspi_dma;
752
753 mcspi = spi_master_get_devdata(spi->master);
754 mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
755
756 complete(&mcspi_dma->dma_tx_completion);
757
758 /* We must disable the DMA TX request */
759 omap2_mcspi_set_dma_req(spi, 0, 0);
760}
761
762static int omap2_mcspi_request_dma(struct spi_device *spi)
763{
764 struct spi_master *master = spi->master;
765 struct omap2_mcspi *mcspi;
766 struct omap2_mcspi_dma *mcspi_dma;
767
768 mcspi = spi_master_get_devdata(master);
769 mcspi_dma = mcspi->dma_channels + spi->chip_select;
770
771 if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
772 omap2_mcspi_dma_rx_callback, spi,
773 &mcspi_dma->dma_rx_channel)) {
774 dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
775 return -EAGAIN;
776 }
777
778 if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
779 omap2_mcspi_dma_tx_callback, spi,
780 &mcspi_dma->dma_tx_channel)) {
781 omap_free_dma(mcspi_dma->dma_rx_channel);
782 mcspi_dma->dma_rx_channel = -1;
783 dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
784 return -EAGAIN;
785 }
786
787 init_completion(&mcspi_dma->dma_rx_completion);
788 init_completion(&mcspi_dma->dma_tx_completion);
789
790 return 0;
791}
792
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700793static int omap2_mcspi_setup(struct spi_device *spi)
794{
795 int ret;
796 struct omap2_mcspi *mcspi;
797 struct omap2_mcspi_dma *mcspi_dma;
798 struct omap2_mcspi_cs *cs = spi->controller_state;
799
David Brownell7d077192009-06-17 16:26:03 -0700800 if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700801 dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
802 spi->bits_per_word);
803 return -EINVAL;
804 }
805
806 mcspi = spi_master_get_devdata(spi->master);
807 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
808
809 if (!cs) {
810 cs = kzalloc(sizeof *cs, GFP_KERNEL);
811 if (!cs)
812 return -ENOMEM;
813 cs->base = mcspi->base + spi->chip_select * 0x14;
Russell Kinge5480b732008-09-01 21:51:50 +0100814 cs->phys = mcspi->phys + spi->chip_select * 0x14;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700815 cs->chconf0 = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700816 spi->controller_state = cs;
Tero Kristo89c05372009-09-22 16:46:17 -0700817 /* Link this to context save list */
818 list_add_tail(&cs->node,
819 &omap2_mcspi_ctx[mcspi->master->bus_num - 1].cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700820 }
821
822 if (mcspi_dma->dma_rx_channel == -1
823 || mcspi_dma->dma_tx_channel == -1) {
824 ret = omap2_mcspi_request_dma(spi);
825 if (ret < 0)
826 return ret;
827 }
828
Hemanth Va41ae1a2009-09-22 16:46:16 -0700829 if (omap2_mcspi_enable_clocks(mcspi))
830 return -ENODEV;
831
Kyungmin Park86eeb6f2007-10-16 01:27:45 -0700832 ret = omap2_mcspi_setup_transfer(spi, NULL);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700833 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700834
835 return ret;
836}
837
838static void omap2_mcspi_cleanup(struct spi_device *spi)
839{
840 struct omap2_mcspi *mcspi;
841 struct omap2_mcspi_dma *mcspi_dma;
Tero Kristo89c05372009-09-22 16:46:17 -0700842 struct omap2_mcspi_cs *cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700843
844 mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700845
Scott Ellis5e774942010-03-10 14:22:45 -0700846 if (spi->controller_state) {
847 /* Unlink controller state from context save list */
848 cs = spi->controller_state;
849 list_del(&cs->node);
Tero Kristo89c05372009-09-22 16:46:17 -0700850
Scott Ellis5e774942010-03-10 14:22:45 -0700851 kfree(spi->controller_state);
852 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700853
Scott Ellis99f1a432010-05-24 14:20:27 +0000854 if (spi->chip_select < spi->master->num_chipselect) {
855 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
856
857 if (mcspi_dma->dma_rx_channel != -1) {
858 omap_free_dma(mcspi_dma->dma_rx_channel);
859 mcspi_dma->dma_rx_channel = -1;
860 }
861 if (mcspi_dma->dma_tx_channel != -1) {
862 omap_free_dma(mcspi_dma->dma_tx_channel);
863 mcspi_dma->dma_tx_channel = -1;
864 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700865 }
866}
867
868static void omap2_mcspi_work(struct work_struct *work)
869{
870 struct omap2_mcspi *mcspi;
871
872 mcspi = container_of(work, struct omap2_mcspi, work);
873 spin_lock_irq(&mcspi->lock);
874
Hemanth Va41ae1a2009-09-22 16:46:16 -0700875 if (omap2_mcspi_enable_clocks(mcspi))
876 goto out;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700877
878 /* We only enable one channel at a time -- the one whose message is
879 * at the head of the queue -- although this controller would gladly
880 * arbitrate among multiple channels. This corresponds to "single
881 * channel" master mode. As a side effect, we need to manage the
882 * chipselect with the FORCE bit ... CS != channel enable.
883 */
884 while (!list_empty(&mcspi->msg_queue)) {
885 struct spi_message *m;
886 struct spi_device *spi;
887 struct spi_transfer *t = NULL;
888 int cs_active = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700889 struct omap2_mcspi_cs *cs;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000890 struct omap2_mcspi_device_config *cd;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700891 int par_override = 0;
892 int status = 0;
893 u32 chconf;
894
895 m = container_of(mcspi->msg_queue.next, struct spi_message,
896 queue);
897
898 list_del_init(&m->queue);
899 spin_unlock_irq(&mcspi->lock);
900
901 spi = m->spi;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700902 cs = spi->controller_state;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000903 cd = spi->controller_data;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700904
905 omap2_mcspi_set_enable(spi, 1);
906 list_for_each_entry(t, &m->transfers, transfer_list) {
907 if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
908 status = -EINVAL;
909 break;
910 }
911 if (par_override || t->speed_hz || t->bits_per_word) {
912 par_override = 1;
913 status = omap2_mcspi_setup_transfer(spi, t);
914 if (status < 0)
915 break;
916 if (!t->speed_hz && !t->bits_per_word)
917 par_override = 0;
918 }
919
920 if (!cs_active) {
921 omap2_mcspi_force_cs(spi, 1);
922 cs_active = 1;
923 }
924
Hemanth Va41ae1a2009-09-22 16:46:16 -0700925 chconf = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700926 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000927 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
928
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700929 if (t->tx_buf == NULL)
930 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
931 else if (t->rx_buf == NULL)
932 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000933
934 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
935 /* Turbo mode is for more than one word */
936 if (t->len > ((cs->word_len + 7) >> 3))
937 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
938 }
939
Hemanth Va41ae1a2009-09-22 16:46:16 -0700940 mcspi_write_chconf0(spi, chconf);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700941
942 if (t->len) {
943 unsigned count;
944
945 /* RX_ONLY mode needs dummy data in TX reg */
946 if (t->tx_buf == NULL)
947 __raw_writel(0, cs->base
948 + OMAP2_MCSPI_TX0);
949
950 if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
951 count = omap2_mcspi_txrx_dma(spi, t);
952 else
953 count = omap2_mcspi_txrx_pio(spi, t);
954 m->actual_length += count;
955
956 if (count != t->len) {
957 status = -EIO;
958 break;
959 }
960 }
961
962 if (t->delay_usecs)
963 udelay(t->delay_usecs);
964
965 /* ignore the "leave it on after last xfer" hint */
966 if (t->cs_change) {
967 omap2_mcspi_force_cs(spi, 0);
968 cs_active = 0;
969 }
970 }
971
972 /* Restore defaults if they were overriden */
973 if (par_override) {
974 par_override = 0;
975 status = omap2_mcspi_setup_transfer(spi, NULL);
976 }
977
978 if (cs_active)
979 omap2_mcspi_force_cs(spi, 0);
980
981 omap2_mcspi_set_enable(spi, 0);
982
983 m->status = status;
984 m->complete(m->context);
985
986 spin_lock_irq(&mcspi->lock);
987 }
988
Hemanth Va41ae1a2009-09-22 16:46:16 -0700989 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700990
Hemanth Va41ae1a2009-09-22 16:46:16 -0700991out:
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700992 spin_unlock_irq(&mcspi->lock);
993}
994
995static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
996{
997 struct omap2_mcspi *mcspi;
998 unsigned long flags;
999 struct spi_transfer *t;
1000
1001 m->actual_length = 0;
1002 m->status = 0;
1003
1004 /* reject invalid messages and transfers */
1005 if (list_empty(&m->transfers) || !m->complete)
1006 return -EINVAL;
1007 list_for_each_entry(t, &m->transfers, transfer_list) {
1008 const void *tx_buf = t->tx_buf;
1009 void *rx_buf = t->rx_buf;
1010 unsigned len = t->len;
1011
1012 if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
1013 || (len && !(rx_buf || tx_buf))
1014 || (t->bits_per_word &&
1015 ( t->bits_per_word < 4
1016 || t->bits_per_word > 32))) {
1017 dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
1018 t->speed_hz,
1019 len,
1020 tx_buf ? "tx" : "",
1021 rx_buf ? "rx" : "",
1022 t->bits_per_word);
1023 return -EINVAL;
1024 }
Hannu Heikkinen57d9c102011-02-24 21:31:33 +02001025 if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
1026 dev_dbg(&spi->dev, "speed_hz %d below minimum %d Hz\n",
1027 t->speed_hz,
1028 OMAP2_MCSPI_MAX_FREQ >> 15);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001029 return -EINVAL;
1030 }
1031
1032 if (m->is_dma_mapped || len < DMA_MIN_BYTES)
1033 continue;
1034
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001035 if (tx_buf != NULL) {
1036 t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
1037 len, DMA_TO_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001038 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001039 dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
1040 'T', len);
1041 return -EINVAL;
1042 }
1043 }
1044 if (rx_buf != NULL) {
1045 t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
1046 DMA_FROM_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001047 if (dma_mapping_error(&spi->dev, t->rx_dma)) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001048 dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
1049 'R', len);
1050 if (tx_buf != NULL)
Russell King - ARM Linux07fe0352011-01-07 15:49:20 +00001051 dma_unmap_single(&spi->dev, t->tx_dma,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001052 len, DMA_TO_DEVICE);
1053 return -EINVAL;
1054 }
1055 }
1056 }
1057
1058 mcspi = spi_master_get_devdata(spi->master);
1059
1060 spin_lock_irqsave(&mcspi->lock, flags);
1061 list_add_tail(&m->queue, &mcspi->msg_queue);
1062 queue_work(omap2_mcspi_wq, &mcspi->work);
1063 spin_unlock_irqrestore(&mcspi->lock, flags);
1064
1065 return 0;
1066}
1067
1068static int __init omap2_mcspi_reset(struct omap2_mcspi *mcspi)
1069{
1070 struct spi_master *master = mcspi->master;
1071 u32 tmp;
1072
Hemanth Va41ae1a2009-09-22 16:46:16 -07001073 if (omap2_mcspi_enable_clocks(mcspi))
1074 return -1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001075
1076 mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
1077 OMAP2_MCSPI_SYSCONFIG_SOFTRESET);
1078 do {
1079 tmp = mcspi_read_reg(master, OMAP2_MCSPI_SYSSTATUS);
1080 } while (!(tmp & OMAP2_MCSPI_SYSSTATUS_RESETDONE));
1081
Hemanth Va41ae1a2009-09-22 16:46:16 -07001082 tmp = OMAP2_MCSPI_SYSCONFIG_AUTOIDLE |
1083 OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP |
1084 OMAP2_MCSPI_SYSCONFIG_SMARTIDLE;
1085 mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG, tmp);
1086 omap2_mcspi_ctx[master->bus_num - 1].sysconfig = tmp;
Jouni Hoganderddb22192009-07-29 15:02:11 -07001087
Hemanth Va41ae1a2009-09-22 16:46:16 -07001088 tmp = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
1089 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, tmp);
1090 omap2_mcspi_ctx[master->bus_num - 1].wakeupenable = tmp;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001091
1092 omap2_mcspi_set_master_mode(master);
Hemanth Va41ae1a2009-09-22 16:46:16 -07001093 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001094 return 0;
1095}
1096
1097static u8 __initdata spi1_rxdma_id [] = {
1098 OMAP24XX_DMA_SPI1_RX0,
1099 OMAP24XX_DMA_SPI1_RX1,
1100 OMAP24XX_DMA_SPI1_RX2,
1101 OMAP24XX_DMA_SPI1_RX3,
1102};
1103
1104static u8 __initdata spi1_txdma_id [] = {
1105 OMAP24XX_DMA_SPI1_TX0,
1106 OMAP24XX_DMA_SPI1_TX1,
1107 OMAP24XX_DMA_SPI1_TX2,
1108 OMAP24XX_DMA_SPI1_TX3,
1109};
1110
1111static u8 __initdata spi2_rxdma_id[] = {
1112 OMAP24XX_DMA_SPI2_RX0,
1113 OMAP24XX_DMA_SPI2_RX1,
1114};
1115
1116static u8 __initdata spi2_txdma_id[] = {
1117 OMAP24XX_DMA_SPI2_TX0,
1118 OMAP24XX_DMA_SPI2_TX1,
1119};
1120
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001121#if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
Syed Rafiuddin7869c0b2009-09-22 16:46:18 -07001122 || defined(CONFIG_ARCH_OMAP4)
Girishccc7bae2008-02-06 01:38:16 -08001123static u8 __initdata spi3_rxdma_id[] = {
1124 OMAP24XX_DMA_SPI3_RX0,
1125 OMAP24XX_DMA_SPI3_RX1,
1126};
1127
1128static u8 __initdata spi3_txdma_id[] = {
1129 OMAP24XX_DMA_SPI3_TX0,
1130 OMAP24XX_DMA_SPI3_TX1,
1131};
1132#endif
1133
Syed Rafiuddin7869c0b2009-09-22 16:46:18 -07001134#if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
Girishccc7bae2008-02-06 01:38:16 -08001135static u8 __initdata spi4_rxdma_id[] = {
1136 OMAP34XX_DMA_SPI4_RX0,
1137};
1138
1139static u8 __initdata spi4_txdma_id[] = {
1140 OMAP34XX_DMA_SPI4_TX0,
1141};
1142#endif
1143
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001144static int __init omap2_mcspi_probe(struct platform_device *pdev)
1145{
1146 struct spi_master *master;
1147 struct omap2_mcspi *mcspi;
1148 struct resource *r;
1149 int status = 0, i;
1150 const u8 *rxdma_id, *txdma_id;
1151 unsigned num_chipselect;
1152
1153 switch (pdev->id) {
1154 case 1:
1155 rxdma_id = spi1_rxdma_id;
1156 txdma_id = spi1_txdma_id;
1157 num_chipselect = 4;
1158 break;
1159 case 2:
1160 rxdma_id = spi2_rxdma_id;
1161 txdma_id = spi2_txdma_id;
1162 num_chipselect = 2;
1163 break;
Syed Rafiuddin7869c0b2009-09-22 16:46:18 -07001164#if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
1165 || defined(CONFIG_ARCH_OMAP4)
Girishccc7bae2008-02-06 01:38:16 -08001166 case 3:
1167 rxdma_id = spi3_rxdma_id;
1168 txdma_id = spi3_txdma_id;
1169 num_chipselect = 2;
1170 break;
1171#endif
Syed Rafiuddin7869c0b2009-09-22 16:46:18 -07001172#if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
Girishccc7bae2008-02-06 01:38:16 -08001173 case 4:
1174 rxdma_id = spi4_rxdma_id;
1175 txdma_id = spi4_txdma_id;
1176 num_chipselect = 1;
1177 break;
1178#endif
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001179 default:
1180 return -EINVAL;
1181 }
1182
1183 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1184 if (master == NULL) {
1185 dev_dbg(&pdev->dev, "master allocation failed\n");
1186 return -ENOMEM;
1187 }
1188
David Brownelle7db06b2009-06-17 16:26:04 -07001189 /* the spi->mode bits understood by this driver: */
1190 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1191
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001192 if (pdev->id != -1)
1193 master->bus_num = pdev->id;
1194
1195 master->setup = omap2_mcspi_setup;
1196 master->transfer = omap2_mcspi_transfer;
1197 master->cleanup = omap2_mcspi_cleanup;
1198 master->num_chipselect = num_chipselect;
1199
1200 dev_set_drvdata(&pdev->dev, master);
1201
1202 mcspi = spi_master_get_devdata(master);
1203 mcspi->master = master;
1204
1205 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1206 if (r == NULL) {
1207 status = -ENODEV;
1208 goto err1;
1209 }
1210 if (!request_mem_region(r->start, (r->end - r->start) + 1,
Kay Sievers6c7377a2009-03-24 16:38:21 -07001211 dev_name(&pdev->dev))) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001212 status = -EBUSY;
1213 goto err1;
1214 }
1215
Russell Kinge5480b732008-09-01 21:51:50 +01001216 mcspi->phys = r->start;
Russell King55c381e2008-09-04 14:07:22 +01001217 mcspi->base = ioremap(r->start, r->end - r->start + 1);
1218 if (!mcspi->base) {
1219 dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
1220 status = -ENOMEM;
1221 goto err1aa;
1222 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001223
1224 INIT_WORK(&mcspi->work, omap2_mcspi_work);
1225
1226 spin_lock_init(&mcspi->lock);
1227 INIT_LIST_HEAD(&mcspi->msg_queue);
Tero Kristo89c05372009-09-22 16:46:17 -07001228 INIT_LIST_HEAD(&omap2_mcspi_ctx[master->bus_num - 1].cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001229
Russell King1b5715e2009-01-19 20:49:37 +00001230 mcspi->ick = clk_get(&pdev->dev, "ick");
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001231 if (IS_ERR(mcspi->ick)) {
1232 dev_dbg(&pdev->dev, "can't get mcspi_ick\n");
1233 status = PTR_ERR(mcspi->ick);
1234 goto err1a;
1235 }
Russell King1b5715e2009-01-19 20:49:37 +00001236 mcspi->fck = clk_get(&pdev->dev, "fck");
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001237 if (IS_ERR(mcspi->fck)) {
1238 dev_dbg(&pdev->dev, "can't get mcspi_fck\n");
1239 status = PTR_ERR(mcspi->fck);
1240 goto err2;
1241 }
1242
1243 mcspi->dma_channels = kcalloc(master->num_chipselect,
1244 sizeof(struct omap2_mcspi_dma),
1245 GFP_KERNEL);
1246
1247 if (mcspi->dma_channels == NULL)
1248 goto err3;
1249
1250 for (i = 0; i < num_chipselect; i++) {
1251 mcspi->dma_channels[i].dma_rx_channel = -1;
1252 mcspi->dma_channels[i].dma_rx_sync_dev = rxdma_id[i];
1253 mcspi->dma_channels[i].dma_tx_channel = -1;
1254 mcspi->dma_channels[i].dma_tx_sync_dev = txdma_id[i];
1255 }
1256
1257 if (omap2_mcspi_reset(mcspi) < 0)
1258 goto err4;
1259
1260 status = spi_register_master(master);
1261 if (status < 0)
1262 goto err4;
1263
1264 return status;
1265
1266err4:
1267 kfree(mcspi->dma_channels);
1268err3:
1269 clk_put(mcspi->fck);
1270err2:
1271 clk_put(mcspi->ick);
1272err1a:
Russell King55c381e2008-09-04 14:07:22 +01001273 iounmap(mcspi->base);
1274err1aa:
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001275 release_mem_region(r->start, (r->end - r->start) + 1);
1276err1:
1277 spi_master_put(master);
1278 return status;
1279}
1280
1281static int __exit omap2_mcspi_remove(struct platform_device *pdev)
1282{
1283 struct spi_master *master;
1284 struct omap2_mcspi *mcspi;
1285 struct omap2_mcspi_dma *dma_channels;
1286 struct resource *r;
Russell King55c381e2008-09-04 14:07:22 +01001287 void __iomem *base;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001288
1289 master = dev_get_drvdata(&pdev->dev);
1290 mcspi = spi_master_get_devdata(master);
1291 dma_channels = mcspi->dma_channels;
1292
1293 clk_put(mcspi->fck);
1294 clk_put(mcspi->ick);
1295
1296 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1297 release_mem_region(r->start, (r->end - r->start) + 1);
1298
Russell King55c381e2008-09-04 14:07:22 +01001299 base = mcspi->base;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001300 spi_unregister_master(master);
Russell King55c381e2008-09-04 14:07:22 +01001301 iounmap(base);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001302 kfree(dma_channels);
1303
1304 return 0;
1305}
1306
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001307/* work with hotplug and coldplug */
1308MODULE_ALIAS("platform:omap2_mcspi");
1309
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001310#ifdef CONFIG_SUSPEND
1311/*
1312 * When SPI wake up from off-mode, CS is in activate state. If it was in
1313 * unactive state when driver was suspend, then force it to unactive state at
1314 * wake up.
1315 */
1316static int omap2_mcspi_resume(struct device *dev)
1317{
1318 struct spi_master *master = dev_get_drvdata(dev);
1319 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1320 struct omap2_mcspi_cs *cs;
1321
1322 omap2_mcspi_enable_clocks(mcspi);
1323 list_for_each_entry(cs, &omap2_mcspi_ctx[master->bus_num - 1].cs,
1324 node) {
1325 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
1326
1327 /*
1328 * We need to toggle CS state for OMAP take this
1329 * change in account.
1330 */
1331 MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 1);
1332 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1333 MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 0);
1334 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1335 }
1336 }
1337 omap2_mcspi_disable_clocks(mcspi);
1338 return 0;
1339}
1340#else
1341#define omap2_mcspi_resume NULL
1342#endif
1343
1344static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1345 .resume = omap2_mcspi_resume,
1346};
1347
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001348static struct platform_driver omap2_mcspi_driver = {
1349 .driver = {
1350 .name = "omap2_mcspi",
1351 .owner = THIS_MODULE,
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001352 .pm = &omap2_mcspi_pm_ops
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001353 },
1354 .remove = __exit_p(omap2_mcspi_remove),
1355};
1356
1357
1358static int __init omap2_mcspi_init(void)
1359{
1360 omap2_mcspi_wq = create_singlethread_workqueue(
1361 omap2_mcspi_driver.driver.name);
1362 if (omap2_mcspi_wq == NULL)
1363 return -1;
1364 return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
1365}
1366subsys_initcall(omap2_mcspi_init);
1367
1368static void __exit omap2_mcspi_exit(void)
1369{
1370 platform_driver_unregister(&omap2_mcspi_driver);
1371
1372 destroy_workqueue(omap2_mcspi_wq);
1373}
1374module_exit(omap2_mcspi_exit);
1375
1376MODULE_LICENSE("GPL");