blob: 27a3fa1f3bf460c45b7ef43ce2a9b9990a4ce779 [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
299static unsigned
300omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
301{
302 struct omap2_mcspi *mcspi;
303 struct omap2_mcspi_cs *cs = spi->controller_state;
304 struct omap2_mcspi_dma *mcspi_dma;
305 unsigned int count, c;
306 unsigned long base, tx_reg, rx_reg;
307 int word_len, data_type, element_count;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000308 int elements;
309 u32 l;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700310 u8 * rx;
311 const u8 * tx;
312
313 mcspi = spi_master_get_devdata(spi->master);
314 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000315 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700316
317 count = xfer->len;
318 c = count;
319 word_len = cs->word_len;
320
Russell Kinge5480b732008-09-01 21:51:50 +0100321 base = cs->phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700322 tx_reg = base + OMAP2_MCSPI_TX0;
323 rx_reg = base + OMAP2_MCSPI_RX0;
324 rx = xfer->rx_buf;
325 tx = xfer->tx_buf;
326
327 if (word_len <= 8) {
328 data_type = OMAP_DMA_DATA_TYPE_S8;
329 element_count = count;
330 } else if (word_len <= 16) {
331 data_type = OMAP_DMA_DATA_TYPE_S16;
332 element_count = count >> 1;
333 } else /* word_len <= 32 */ {
334 data_type = OMAP_DMA_DATA_TYPE_S32;
335 element_count = count >> 2;
336 }
337
338 if (tx != NULL) {
339 omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
340 data_type, element_count, 1,
341 OMAP_DMA_SYNC_ELEMENT,
342 mcspi_dma->dma_tx_sync_dev, 0);
343
344 omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
345 OMAP_DMA_AMODE_CONSTANT,
346 tx_reg, 0, 0);
347
348 omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
349 OMAP_DMA_AMODE_POST_INC,
350 xfer->tx_dma, 0, 0);
351 }
352
353 if (rx != NULL) {
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000354 elements = element_count - 1;
355 if (l & OMAP2_MCSPI_CHCONF_TURBO)
356 elements--;
357
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700358 omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000359 data_type, elements, 1,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700360 OMAP_DMA_SYNC_ELEMENT,
361 mcspi_dma->dma_rx_sync_dev, 1);
362
363 omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
364 OMAP_DMA_AMODE_CONSTANT,
365 rx_reg, 0, 0);
366
367 omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
368 OMAP_DMA_AMODE_POST_INC,
369 xfer->rx_dma, 0, 0);
370 }
371
372 if (tx != NULL) {
373 omap_start_dma(mcspi_dma->dma_tx_channel);
374 omap2_mcspi_set_dma_req(spi, 0, 1);
375 }
376
377 if (rx != NULL) {
378 omap_start_dma(mcspi_dma->dma_rx_channel);
379 omap2_mcspi_set_dma_req(spi, 1, 1);
380 }
381
382 if (tx != NULL) {
383 wait_for_completion(&mcspi_dma->dma_tx_completion);
384 dma_unmap_single(NULL, xfer->tx_dma, count, DMA_TO_DEVICE);
385 }
386
387 if (rx != NULL) {
388 wait_for_completion(&mcspi_dma->dma_rx_completion);
389 dma_unmap_single(NULL, xfer->rx_dma, count, DMA_FROM_DEVICE);
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700390 omap2_mcspi_set_enable(spi, 0);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000391
392 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
393
394 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
395 & OMAP2_MCSPI_CHSTAT_RXS)) {
396 u32 w;
397
398 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
399 if (word_len <= 8)
400 ((u8 *)xfer->rx_buf)[elements++] = w;
401 else if (word_len <= 16)
402 ((u16 *)xfer->rx_buf)[elements++] = w;
403 else /* word_len <= 32 */
404 ((u32 *)xfer->rx_buf)[elements++] = w;
405 } else {
406 dev_err(&spi->dev,
407 "DMA RX penultimate word empty");
408 count -= (word_len <= 8) ? 2 :
409 (word_len <= 16) ? 4 :
410 /* word_len <= 32 */ 8;
411 omap2_mcspi_set_enable(spi, 1);
412 return count;
413 }
414 }
415
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700416 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
417 & OMAP2_MCSPI_CHSTAT_RXS)) {
418 u32 w;
419
420 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
421 if (word_len <= 8)
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000422 ((u8 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700423 else if (word_len <= 16)
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000424 ((u16 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700425 else /* word_len <= 32 */
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000426 ((u32 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700427 } else {
428 dev_err(&spi->dev, "DMA RX last word empty");
429 count -= (word_len <= 8) ? 1 :
430 (word_len <= 16) ? 2 :
431 /* word_len <= 32 */ 4;
432 }
433 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700434 }
435 return count;
436}
437
438static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
439{
440 unsigned long timeout;
441
442 timeout = jiffies + msecs_to_jiffies(1000);
443 while (!(__raw_readl(reg) & bit)) {
444 if (time_after(jiffies, timeout))
445 return -1;
446 cpu_relax();
447 }
448 return 0;
449}
450
451static unsigned
452omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
453{
454 struct omap2_mcspi *mcspi;
455 struct omap2_mcspi_cs *cs = spi->controller_state;
456 unsigned int count, c;
457 u32 l;
458 void __iomem *base = cs->base;
459 void __iomem *tx_reg;
460 void __iomem *rx_reg;
461 void __iomem *chstat_reg;
462 int word_len;
463
464 mcspi = spi_master_get_devdata(spi->master);
465 count = xfer->len;
466 c = count;
467 word_len = cs->word_len;
468
Hemanth Va41ae1a2009-09-22 16:46:16 -0700469 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700470
471 /* We store the pre-calculated register addresses on stack to speed
472 * up the transfer loop. */
473 tx_reg = base + OMAP2_MCSPI_TX0;
474 rx_reg = base + OMAP2_MCSPI_RX0;
475 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
476
477 if (word_len <= 8) {
478 u8 *rx;
479 const u8 *tx;
480
481 rx = xfer->rx_buf;
482 tx = xfer->tx_buf;
483
484 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800485 c -= 1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700486 if (tx != NULL) {
487 if (mcspi_wait_for_reg_bit(chstat_reg,
488 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
489 dev_err(&spi->dev, "TXS timed out\n");
490 goto out;
491 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900492 dev_vdbg(&spi->dev, "write-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700493 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700494 __raw_writel(*tx++, tx_reg);
495 }
496 if (rx != NULL) {
497 if (mcspi_wait_for_reg_bit(chstat_reg,
498 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
499 dev_err(&spi->dev, "RXS timed out\n");
500 goto out;
501 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000502
503 if (c == 1 && tx == NULL &&
504 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
505 omap2_mcspi_set_enable(spi, 0);
506 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900507 dev_vdbg(&spi->dev, "read-%d %02x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000508 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000509 if (mcspi_wait_for_reg_bit(chstat_reg,
510 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
511 dev_err(&spi->dev,
512 "RXS timed out\n");
513 goto out;
514 }
515 c = 0;
516 } else if (c == 0 && tx == NULL) {
517 omap2_mcspi_set_enable(spi, 0);
518 }
519
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700520 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900521 dev_vdbg(&spi->dev, "read-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700522 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700523 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700524 } while (c);
525 } else if (word_len <= 16) {
526 u16 *rx;
527 const u16 *tx;
528
529 rx = xfer->rx_buf;
530 tx = xfer->tx_buf;
531 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800532 c -= 2;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700533 if (tx != NULL) {
534 if (mcspi_wait_for_reg_bit(chstat_reg,
535 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
536 dev_err(&spi->dev, "TXS timed out\n");
537 goto out;
538 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900539 dev_vdbg(&spi->dev, "write-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700540 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700541 __raw_writel(*tx++, tx_reg);
542 }
543 if (rx != NULL) {
544 if (mcspi_wait_for_reg_bit(chstat_reg,
545 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
546 dev_err(&spi->dev, "RXS timed out\n");
547 goto out;
548 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000549
550 if (c == 2 && tx == NULL &&
551 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
552 omap2_mcspi_set_enable(spi, 0);
553 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900554 dev_vdbg(&spi->dev, "read-%d %04x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000555 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000556 if (mcspi_wait_for_reg_bit(chstat_reg,
557 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
558 dev_err(&spi->dev,
559 "RXS timed out\n");
560 goto out;
561 }
562 c = 0;
563 } else if (c == 0 && tx == NULL) {
564 omap2_mcspi_set_enable(spi, 0);
565 }
566
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700567 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900568 dev_vdbg(&spi->dev, "read-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700569 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700570 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700571 } while (c);
572 } else if (word_len <= 32) {
573 u32 *rx;
574 const u32 *tx;
575
576 rx = xfer->rx_buf;
577 tx = xfer->tx_buf;
578 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800579 c -= 4;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700580 if (tx != NULL) {
581 if (mcspi_wait_for_reg_bit(chstat_reg,
582 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
583 dev_err(&spi->dev, "TXS timed out\n");
584 goto out;
585 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900586 dev_vdbg(&spi->dev, "write-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700587 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700588 __raw_writel(*tx++, tx_reg);
589 }
590 if (rx != NULL) {
591 if (mcspi_wait_for_reg_bit(chstat_reg,
592 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
593 dev_err(&spi->dev, "RXS timed out\n");
594 goto out;
595 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000596
597 if (c == 4 && tx == NULL &&
598 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
599 omap2_mcspi_set_enable(spi, 0);
600 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900601 dev_vdbg(&spi->dev, "read-%d %08x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000602 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000603 if (mcspi_wait_for_reg_bit(chstat_reg,
604 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
605 dev_err(&spi->dev,
606 "RXS timed out\n");
607 goto out;
608 }
609 c = 0;
610 } else if (c == 0 && tx == NULL) {
611 omap2_mcspi_set_enable(spi, 0);
612 }
613
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700614 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900615 dev_vdbg(&spi->dev, "read-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700616 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700617 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700618 } while (c);
619 }
620
621 /* for TX_ONLY mode, be sure all words have shifted out */
622 if (xfer->rx_buf == NULL) {
623 if (mcspi_wait_for_reg_bit(chstat_reg,
624 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
625 dev_err(&spi->dev, "TXS timed out\n");
626 } else if (mcspi_wait_for_reg_bit(chstat_reg,
627 OMAP2_MCSPI_CHSTAT_EOT) < 0)
628 dev_err(&spi->dev, "EOT timed out\n");
Jason Wange1993ed2010-10-19 18:03:27 +0800629
630 /* disable chan to purge rx datas received in TX_ONLY transfer,
631 * otherwise these rx datas will affect the direct following
632 * RX_ONLY transfer.
633 */
634 omap2_mcspi_set_enable(spi, 0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700635 }
636out:
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000637 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700638 return count - c;
639}
640
641/* called only when no transfer is active to this device */
642static int omap2_mcspi_setup_transfer(struct spi_device *spi,
643 struct spi_transfer *t)
644{
645 struct omap2_mcspi_cs *cs = spi->controller_state;
646 struct omap2_mcspi *mcspi;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700647 struct spi_master *spi_cntrl;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700648 u32 l = 0, div = 0;
649 u8 word_len = spi->bits_per_word;
Scott Ellis9bd45172010-03-10 14:23:13 -0700650 u32 speed_hz = spi->max_speed_hz;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700651
652 mcspi = spi_master_get_devdata(spi->master);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700653 spi_cntrl = mcspi->master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700654
655 if (t != NULL && t->bits_per_word)
656 word_len = t->bits_per_word;
657
658 cs->word_len = word_len;
659
Scott Ellis9bd45172010-03-10 14:23:13 -0700660 if (t && t->speed_hz)
661 speed_hz = t->speed_hz;
662
663 if (speed_hz) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700664 while (div <= 15 && (OMAP2_MCSPI_MAX_FREQ / (1 << div))
Scott Ellis9bd45172010-03-10 14:23:13 -0700665 > speed_hz)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700666 div++;
667 } else
668 div = 15;
669
Hemanth Va41ae1a2009-09-22 16:46:16 -0700670 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700671
672 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
673 * REVISIT: this controller could support SPI_3WIRE mode.
674 */
675 l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
676 l |= OMAP2_MCSPI_CHCONF_DPE0;
677
678 /* wordlength */
679 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
680 l |= (word_len - 1) << 7;
681
682 /* set chipselect polarity; manage with FORCE */
683 if (!(spi->mode & SPI_CS_HIGH))
684 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
685 else
686 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
687
688 /* set clock divisor */
689 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
690 l |= div << 2;
691
692 /* set SPI mode 0..3 */
693 if (spi->mode & SPI_CPOL)
694 l |= OMAP2_MCSPI_CHCONF_POL;
695 else
696 l &= ~OMAP2_MCSPI_CHCONF_POL;
697 if (spi->mode & SPI_CPHA)
698 l |= OMAP2_MCSPI_CHCONF_PHA;
699 else
700 l &= ~OMAP2_MCSPI_CHCONF_PHA;
701
Hemanth Va41ae1a2009-09-22 16:46:16 -0700702 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700703
704 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
705 OMAP2_MCSPI_MAX_FREQ / (1 << div),
706 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
707 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
708
709 return 0;
710}
711
712static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
713{
714 struct spi_device *spi = data;
715 struct omap2_mcspi *mcspi;
716 struct omap2_mcspi_dma *mcspi_dma;
717
718 mcspi = spi_master_get_devdata(spi->master);
719 mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
720
721 complete(&mcspi_dma->dma_rx_completion);
722
723 /* We must disable the DMA RX request */
724 omap2_mcspi_set_dma_req(spi, 1, 0);
725}
726
727static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
728{
729 struct spi_device *spi = data;
730 struct omap2_mcspi *mcspi;
731 struct omap2_mcspi_dma *mcspi_dma;
732
733 mcspi = spi_master_get_devdata(spi->master);
734 mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
735
736 complete(&mcspi_dma->dma_tx_completion);
737
738 /* We must disable the DMA TX request */
739 omap2_mcspi_set_dma_req(spi, 0, 0);
740}
741
742static int omap2_mcspi_request_dma(struct spi_device *spi)
743{
744 struct spi_master *master = spi->master;
745 struct omap2_mcspi *mcspi;
746 struct omap2_mcspi_dma *mcspi_dma;
747
748 mcspi = spi_master_get_devdata(master);
749 mcspi_dma = mcspi->dma_channels + spi->chip_select;
750
751 if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
752 omap2_mcspi_dma_rx_callback, spi,
753 &mcspi_dma->dma_rx_channel)) {
754 dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
755 return -EAGAIN;
756 }
757
758 if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
759 omap2_mcspi_dma_tx_callback, spi,
760 &mcspi_dma->dma_tx_channel)) {
761 omap_free_dma(mcspi_dma->dma_rx_channel);
762 mcspi_dma->dma_rx_channel = -1;
763 dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
764 return -EAGAIN;
765 }
766
767 init_completion(&mcspi_dma->dma_rx_completion);
768 init_completion(&mcspi_dma->dma_tx_completion);
769
770 return 0;
771}
772
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700773static int omap2_mcspi_setup(struct spi_device *spi)
774{
775 int ret;
776 struct omap2_mcspi *mcspi;
777 struct omap2_mcspi_dma *mcspi_dma;
778 struct omap2_mcspi_cs *cs = spi->controller_state;
779
David Brownell7d077192009-06-17 16:26:03 -0700780 if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700781 dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
782 spi->bits_per_word);
783 return -EINVAL;
784 }
785
786 mcspi = spi_master_get_devdata(spi->master);
787 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
788
789 if (!cs) {
790 cs = kzalloc(sizeof *cs, GFP_KERNEL);
791 if (!cs)
792 return -ENOMEM;
793 cs->base = mcspi->base + spi->chip_select * 0x14;
Russell Kinge5480b732008-09-01 21:51:50 +0100794 cs->phys = mcspi->phys + spi->chip_select * 0x14;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700795 cs->chconf0 = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700796 spi->controller_state = cs;
Tero Kristo89c05372009-09-22 16:46:17 -0700797 /* Link this to context save list */
798 list_add_tail(&cs->node,
799 &omap2_mcspi_ctx[mcspi->master->bus_num - 1].cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700800 }
801
802 if (mcspi_dma->dma_rx_channel == -1
803 || mcspi_dma->dma_tx_channel == -1) {
804 ret = omap2_mcspi_request_dma(spi);
805 if (ret < 0)
806 return ret;
807 }
808
Hemanth Va41ae1a2009-09-22 16:46:16 -0700809 if (omap2_mcspi_enable_clocks(mcspi))
810 return -ENODEV;
811
Kyungmin Park86eeb6f2007-10-16 01:27:45 -0700812 ret = omap2_mcspi_setup_transfer(spi, NULL);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700813 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700814
815 return ret;
816}
817
818static void omap2_mcspi_cleanup(struct spi_device *spi)
819{
820 struct omap2_mcspi *mcspi;
821 struct omap2_mcspi_dma *mcspi_dma;
Tero Kristo89c05372009-09-22 16:46:17 -0700822 struct omap2_mcspi_cs *cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700823
824 mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700825
Scott Ellis5e774942010-03-10 14:22:45 -0700826 if (spi->controller_state) {
827 /* Unlink controller state from context save list */
828 cs = spi->controller_state;
829 list_del(&cs->node);
Tero Kristo89c05372009-09-22 16:46:17 -0700830
Scott Ellis5e774942010-03-10 14:22:45 -0700831 kfree(spi->controller_state);
832 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700833
Scott Ellis99f1a432010-05-24 14:20:27 +0000834 if (spi->chip_select < spi->master->num_chipselect) {
835 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
836
837 if (mcspi_dma->dma_rx_channel != -1) {
838 omap_free_dma(mcspi_dma->dma_rx_channel);
839 mcspi_dma->dma_rx_channel = -1;
840 }
841 if (mcspi_dma->dma_tx_channel != -1) {
842 omap_free_dma(mcspi_dma->dma_tx_channel);
843 mcspi_dma->dma_tx_channel = -1;
844 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700845 }
846}
847
848static void omap2_mcspi_work(struct work_struct *work)
849{
850 struct omap2_mcspi *mcspi;
851
852 mcspi = container_of(work, struct omap2_mcspi, work);
853 spin_lock_irq(&mcspi->lock);
854
Hemanth Va41ae1a2009-09-22 16:46:16 -0700855 if (omap2_mcspi_enable_clocks(mcspi))
856 goto out;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700857
858 /* We only enable one channel at a time -- the one whose message is
859 * at the head of the queue -- although this controller would gladly
860 * arbitrate among multiple channels. This corresponds to "single
861 * channel" master mode. As a side effect, we need to manage the
862 * chipselect with the FORCE bit ... CS != channel enable.
863 */
864 while (!list_empty(&mcspi->msg_queue)) {
865 struct spi_message *m;
866 struct spi_device *spi;
867 struct spi_transfer *t = NULL;
868 int cs_active = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700869 struct omap2_mcspi_cs *cs;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000870 struct omap2_mcspi_device_config *cd;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700871 int par_override = 0;
872 int status = 0;
873 u32 chconf;
874
875 m = container_of(mcspi->msg_queue.next, struct spi_message,
876 queue);
877
878 list_del_init(&m->queue);
879 spin_unlock_irq(&mcspi->lock);
880
881 spi = m->spi;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700882 cs = spi->controller_state;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000883 cd = spi->controller_data;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700884
885 omap2_mcspi_set_enable(spi, 1);
886 list_for_each_entry(t, &m->transfers, transfer_list) {
887 if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
888 status = -EINVAL;
889 break;
890 }
891 if (par_override || t->speed_hz || t->bits_per_word) {
892 par_override = 1;
893 status = omap2_mcspi_setup_transfer(spi, t);
894 if (status < 0)
895 break;
896 if (!t->speed_hz && !t->bits_per_word)
897 par_override = 0;
898 }
899
900 if (!cs_active) {
901 omap2_mcspi_force_cs(spi, 1);
902 cs_active = 1;
903 }
904
Hemanth Va41ae1a2009-09-22 16:46:16 -0700905 chconf = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700906 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000907 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
908
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700909 if (t->tx_buf == NULL)
910 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
911 else if (t->rx_buf == NULL)
912 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000913
914 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
915 /* Turbo mode is for more than one word */
916 if (t->len > ((cs->word_len + 7) >> 3))
917 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
918 }
919
Hemanth Va41ae1a2009-09-22 16:46:16 -0700920 mcspi_write_chconf0(spi, chconf);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700921
922 if (t->len) {
923 unsigned count;
924
925 /* RX_ONLY mode needs dummy data in TX reg */
926 if (t->tx_buf == NULL)
927 __raw_writel(0, cs->base
928 + OMAP2_MCSPI_TX0);
929
930 if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
931 count = omap2_mcspi_txrx_dma(spi, t);
932 else
933 count = omap2_mcspi_txrx_pio(spi, t);
934 m->actual_length += count;
935
936 if (count != t->len) {
937 status = -EIO;
938 break;
939 }
940 }
941
942 if (t->delay_usecs)
943 udelay(t->delay_usecs);
944
945 /* ignore the "leave it on after last xfer" hint */
946 if (t->cs_change) {
947 omap2_mcspi_force_cs(spi, 0);
948 cs_active = 0;
949 }
950 }
951
952 /* Restore defaults if they were overriden */
953 if (par_override) {
954 par_override = 0;
955 status = omap2_mcspi_setup_transfer(spi, NULL);
956 }
957
958 if (cs_active)
959 omap2_mcspi_force_cs(spi, 0);
960
961 omap2_mcspi_set_enable(spi, 0);
962
963 m->status = status;
964 m->complete(m->context);
965
966 spin_lock_irq(&mcspi->lock);
967 }
968
Hemanth Va41ae1a2009-09-22 16:46:16 -0700969 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700970
Hemanth Va41ae1a2009-09-22 16:46:16 -0700971out:
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700972 spin_unlock_irq(&mcspi->lock);
973}
974
975static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
976{
977 struct omap2_mcspi *mcspi;
978 unsigned long flags;
979 struct spi_transfer *t;
980
981 m->actual_length = 0;
982 m->status = 0;
983
984 /* reject invalid messages and transfers */
985 if (list_empty(&m->transfers) || !m->complete)
986 return -EINVAL;
987 list_for_each_entry(t, &m->transfers, transfer_list) {
988 const void *tx_buf = t->tx_buf;
989 void *rx_buf = t->rx_buf;
990 unsigned len = t->len;
991
992 if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
993 || (len && !(rx_buf || tx_buf))
994 || (t->bits_per_word &&
995 ( t->bits_per_word < 4
996 || t->bits_per_word > 32))) {
997 dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
998 t->speed_hz,
999 len,
1000 tx_buf ? "tx" : "",
1001 rx_buf ? "rx" : "",
1002 t->bits_per_word);
1003 return -EINVAL;
1004 }
1005 if (t->speed_hz && t->speed_hz < OMAP2_MCSPI_MAX_FREQ/(1<<16)) {
1006 dev_dbg(&spi->dev, "%d Hz max exceeds %d\n",
1007 t->speed_hz,
1008 OMAP2_MCSPI_MAX_FREQ/(1<<16));
1009 return -EINVAL;
1010 }
1011
1012 if (m->is_dma_mapped || len < DMA_MIN_BYTES)
1013 continue;
1014
1015 /* Do DMA mapping "early" for better error reporting and
1016 * dcache use. Note that if dma_unmap_single() ever starts
1017 * to do real work on ARM, we'd need to clean up mappings
1018 * for previous transfers on *ALL* exits of this loop...
1019 */
1020 if (tx_buf != NULL) {
1021 t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
1022 len, DMA_TO_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001023 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001024 dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
1025 'T', len);
1026 return -EINVAL;
1027 }
1028 }
1029 if (rx_buf != NULL) {
1030 t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
1031 DMA_FROM_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001032 if (dma_mapping_error(&spi->dev, t->rx_dma)) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001033 dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
1034 'R', len);
1035 if (tx_buf != NULL)
1036 dma_unmap_single(NULL, t->tx_dma,
1037 len, DMA_TO_DEVICE);
1038 return -EINVAL;
1039 }
1040 }
1041 }
1042
1043 mcspi = spi_master_get_devdata(spi->master);
1044
1045 spin_lock_irqsave(&mcspi->lock, flags);
1046 list_add_tail(&m->queue, &mcspi->msg_queue);
1047 queue_work(omap2_mcspi_wq, &mcspi->work);
1048 spin_unlock_irqrestore(&mcspi->lock, flags);
1049
1050 return 0;
1051}
1052
1053static int __init omap2_mcspi_reset(struct omap2_mcspi *mcspi)
1054{
1055 struct spi_master *master = mcspi->master;
1056 u32 tmp;
1057
Hemanth Va41ae1a2009-09-22 16:46:16 -07001058 if (omap2_mcspi_enable_clocks(mcspi))
1059 return -1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001060
1061 mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG,
1062 OMAP2_MCSPI_SYSCONFIG_SOFTRESET);
1063 do {
1064 tmp = mcspi_read_reg(master, OMAP2_MCSPI_SYSSTATUS);
1065 } while (!(tmp & OMAP2_MCSPI_SYSSTATUS_RESETDONE));
1066
Hemanth Va41ae1a2009-09-22 16:46:16 -07001067 tmp = OMAP2_MCSPI_SYSCONFIG_AUTOIDLE |
1068 OMAP2_MCSPI_SYSCONFIG_ENAWAKEUP |
1069 OMAP2_MCSPI_SYSCONFIG_SMARTIDLE;
1070 mcspi_write_reg(master, OMAP2_MCSPI_SYSCONFIG, tmp);
1071 omap2_mcspi_ctx[master->bus_num - 1].sysconfig = tmp;
Jouni Hoganderddb22192009-07-29 15:02:11 -07001072
Hemanth Va41ae1a2009-09-22 16:46:16 -07001073 tmp = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
1074 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, tmp);
1075 omap2_mcspi_ctx[master->bus_num - 1].wakeupenable = tmp;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001076
1077 omap2_mcspi_set_master_mode(master);
Hemanth Va41ae1a2009-09-22 16:46:16 -07001078 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001079 return 0;
1080}
1081
1082static u8 __initdata spi1_rxdma_id [] = {
1083 OMAP24XX_DMA_SPI1_RX0,
1084 OMAP24XX_DMA_SPI1_RX1,
1085 OMAP24XX_DMA_SPI1_RX2,
1086 OMAP24XX_DMA_SPI1_RX3,
1087};
1088
1089static u8 __initdata spi1_txdma_id [] = {
1090 OMAP24XX_DMA_SPI1_TX0,
1091 OMAP24XX_DMA_SPI1_TX1,
1092 OMAP24XX_DMA_SPI1_TX2,
1093 OMAP24XX_DMA_SPI1_TX3,
1094};
1095
1096static u8 __initdata spi2_rxdma_id[] = {
1097 OMAP24XX_DMA_SPI2_RX0,
1098 OMAP24XX_DMA_SPI2_RX1,
1099};
1100
1101static u8 __initdata spi2_txdma_id[] = {
1102 OMAP24XX_DMA_SPI2_TX0,
1103 OMAP24XX_DMA_SPI2_TX1,
1104};
1105
Tony Lindgrena8eb7ca2010-02-12 12:26:48 -08001106#if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
Syed Rafiuddin7869c0b2009-09-22 16:46:18 -07001107 || defined(CONFIG_ARCH_OMAP4)
Girishccc7bae2008-02-06 01:38:16 -08001108static u8 __initdata spi3_rxdma_id[] = {
1109 OMAP24XX_DMA_SPI3_RX0,
1110 OMAP24XX_DMA_SPI3_RX1,
1111};
1112
1113static u8 __initdata spi3_txdma_id[] = {
1114 OMAP24XX_DMA_SPI3_TX0,
1115 OMAP24XX_DMA_SPI3_TX1,
1116};
1117#endif
1118
Syed Rafiuddin7869c0b2009-09-22 16:46:18 -07001119#if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
Girishccc7bae2008-02-06 01:38:16 -08001120static u8 __initdata spi4_rxdma_id[] = {
1121 OMAP34XX_DMA_SPI4_RX0,
1122};
1123
1124static u8 __initdata spi4_txdma_id[] = {
1125 OMAP34XX_DMA_SPI4_TX0,
1126};
1127#endif
1128
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001129static int __init omap2_mcspi_probe(struct platform_device *pdev)
1130{
1131 struct spi_master *master;
1132 struct omap2_mcspi *mcspi;
1133 struct resource *r;
1134 int status = 0, i;
1135 const u8 *rxdma_id, *txdma_id;
1136 unsigned num_chipselect;
1137
1138 switch (pdev->id) {
1139 case 1:
1140 rxdma_id = spi1_rxdma_id;
1141 txdma_id = spi1_txdma_id;
1142 num_chipselect = 4;
1143 break;
1144 case 2:
1145 rxdma_id = spi2_rxdma_id;
1146 txdma_id = spi2_txdma_id;
1147 num_chipselect = 2;
1148 break;
Syed Rafiuddin7869c0b2009-09-22 16:46:18 -07001149#if defined(CONFIG_ARCH_OMAP2430) || defined(CONFIG_ARCH_OMAP3) \
1150 || defined(CONFIG_ARCH_OMAP4)
Girishccc7bae2008-02-06 01:38:16 -08001151 case 3:
1152 rxdma_id = spi3_rxdma_id;
1153 txdma_id = spi3_txdma_id;
1154 num_chipselect = 2;
1155 break;
1156#endif
Syed Rafiuddin7869c0b2009-09-22 16:46:18 -07001157#if defined(CONFIG_ARCH_OMAP3) || defined(CONFIG_ARCH_OMAP4)
Girishccc7bae2008-02-06 01:38:16 -08001158 case 4:
1159 rxdma_id = spi4_rxdma_id;
1160 txdma_id = spi4_txdma_id;
1161 num_chipselect = 1;
1162 break;
1163#endif
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001164 default:
1165 return -EINVAL;
1166 }
1167
1168 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1169 if (master == NULL) {
1170 dev_dbg(&pdev->dev, "master allocation failed\n");
1171 return -ENOMEM;
1172 }
1173
David Brownelle7db06b2009-06-17 16:26:04 -07001174 /* the spi->mode bits understood by this driver: */
1175 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1176
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001177 if (pdev->id != -1)
1178 master->bus_num = pdev->id;
1179
1180 master->setup = omap2_mcspi_setup;
1181 master->transfer = omap2_mcspi_transfer;
1182 master->cleanup = omap2_mcspi_cleanup;
1183 master->num_chipselect = num_chipselect;
1184
1185 dev_set_drvdata(&pdev->dev, master);
1186
1187 mcspi = spi_master_get_devdata(master);
1188 mcspi->master = master;
1189
1190 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1191 if (r == NULL) {
1192 status = -ENODEV;
1193 goto err1;
1194 }
1195 if (!request_mem_region(r->start, (r->end - r->start) + 1,
Kay Sievers6c7377a2009-03-24 16:38:21 -07001196 dev_name(&pdev->dev))) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001197 status = -EBUSY;
1198 goto err1;
1199 }
1200
Russell Kinge5480b732008-09-01 21:51:50 +01001201 mcspi->phys = r->start;
Russell King55c381e2008-09-04 14:07:22 +01001202 mcspi->base = ioremap(r->start, r->end - r->start + 1);
1203 if (!mcspi->base) {
1204 dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
1205 status = -ENOMEM;
1206 goto err1aa;
1207 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001208
1209 INIT_WORK(&mcspi->work, omap2_mcspi_work);
1210
1211 spin_lock_init(&mcspi->lock);
1212 INIT_LIST_HEAD(&mcspi->msg_queue);
Tero Kristo89c05372009-09-22 16:46:17 -07001213 INIT_LIST_HEAD(&omap2_mcspi_ctx[master->bus_num - 1].cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001214
Russell King1b5715e2009-01-19 20:49:37 +00001215 mcspi->ick = clk_get(&pdev->dev, "ick");
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001216 if (IS_ERR(mcspi->ick)) {
1217 dev_dbg(&pdev->dev, "can't get mcspi_ick\n");
1218 status = PTR_ERR(mcspi->ick);
1219 goto err1a;
1220 }
Russell King1b5715e2009-01-19 20:49:37 +00001221 mcspi->fck = clk_get(&pdev->dev, "fck");
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001222 if (IS_ERR(mcspi->fck)) {
1223 dev_dbg(&pdev->dev, "can't get mcspi_fck\n");
1224 status = PTR_ERR(mcspi->fck);
1225 goto err2;
1226 }
1227
1228 mcspi->dma_channels = kcalloc(master->num_chipselect,
1229 sizeof(struct omap2_mcspi_dma),
1230 GFP_KERNEL);
1231
1232 if (mcspi->dma_channels == NULL)
1233 goto err3;
1234
1235 for (i = 0; i < num_chipselect; i++) {
1236 mcspi->dma_channels[i].dma_rx_channel = -1;
1237 mcspi->dma_channels[i].dma_rx_sync_dev = rxdma_id[i];
1238 mcspi->dma_channels[i].dma_tx_channel = -1;
1239 mcspi->dma_channels[i].dma_tx_sync_dev = txdma_id[i];
1240 }
1241
1242 if (omap2_mcspi_reset(mcspi) < 0)
1243 goto err4;
1244
1245 status = spi_register_master(master);
1246 if (status < 0)
1247 goto err4;
1248
1249 return status;
1250
1251err4:
1252 kfree(mcspi->dma_channels);
1253err3:
1254 clk_put(mcspi->fck);
1255err2:
1256 clk_put(mcspi->ick);
1257err1a:
Russell King55c381e2008-09-04 14:07:22 +01001258 iounmap(mcspi->base);
1259err1aa:
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001260 release_mem_region(r->start, (r->end - r->start) + 1);
1261err1:
1262 spi_master_put(master);
1263 return status;
1264}
1265
1266static int __exit omap2_mcspi_remove(struct platform_device *pdev)
1267{
1268 struct spi_master *master;
1269 struct omap2_mcspi *mcspi;
1270 struct omap2_mcspi_dma *dma_channels;
1271 struct resource *r;
Russell King55c381e2008-09-04 14:07:22 +01001272 void __iomem *base;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001273
1274 master = dev_get_drvdata(&pdev->dev);
1275 mcspi = spi_master_get_devdata(master);
1276 dma_channels = mcspi->dma_channels;
1277
1278 clk_put(mcspi->fck);
1279 clk_put(mcspi->ick);
1280
1281 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1282 release_mem_region(r->start, (r->end - r->start) + 1);
1283
Russell King55c381e2008-09-04 14:07:22 +01001284 base = mcspi->base;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001285 spi_unregister_master(master);
Russell King55c381e2008-09-04 14:07:22 +01001286 iounmap(base);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001287 kfree(dma_channels);
1288
1289 return 0;
1290}
1291
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001292/* work with hotplug and coldplug */
1293MODULE_ALIAS("platform:omap2_mcspi");
1294
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001295static struct platform_driver omap2_mcspi_driver = {
1296 .driver = {
1297 .name = "omap2_mcspi",
1298 .owner = THIS_MODULE,
1299 },
1300 .remove = __exit_p(omap2_mcspi_remove),
1301};
1302
1303
1304static int __init omap2_mcspi_init(void)
1305{
1306 omap2_mcspi_wq = create_singlethread_workqueue(
1307 omap2_mcspi_driver.driver.name);
1308 if (omap2_mcspi_wq == NULL)
1309 return -1;
1310 return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
1311}
1312subsys_initcall(omap2_mcspi_init);
1313
1314static void __exit omap2_mcspi_exit(void)
1315{
1316 platform_driver_unregister(&omap2_mcspi_driver);
1317
1318 destroy_workqueue(omap2_mcspi_wq);
1319}
1320module_exit(omap2_mcspi_exit);
1321
1322MODULE_LICENSE("GPL");