blob: 0b0dfb71c6406335dea1a283dfe6acfae3ecce9b [file] [log] [blame]
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001/*
2 * OMAP2 McSPI controller driver
3 *
4 * Copyright (C) 2005, 2006 Nokia Corporation
5 * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and
Charulatha V1a5d8192011-02-02 17:52:14 +05306 * Juha Yrj�l� <juha.yrjola@nokia.com>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <linux/kernel.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/module.h>
28#include <linux/device.h>
29#include <linux/delay.h>
30#include <linux/dma-mapping.h>
31#include <linux/platform_device.h>
32#include <linux/err.h>
33#include <linux/clk.h>
34#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Govindraj.R1f1a4382011-02-02 17:52:15 +053036#include <linux/pm_runtime.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070037
38#include <linux/spi/spi.h>
39
Tony Lindgrence491cf2009-10-20 09:40:47 -070040#include <plat/dma.h>
41#include <plat/clock.h>
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +000042#include <plat/mcspi.h>
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070043
44#define OMAP2_MCSPI_MAX_FREQ 48000000
45
Hemanth Va41ae1a2009-09-22 16:46:16 -070046/* OMAP2 has 3 SPI controllers, while OMAP3 has 4 */
47#define OMAP2_MCSPI_MAX_CTRL 4
48
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070049#define OMAP2_MCSPI_REVISION 0x00
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070050#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_MODULCTRL_SINGLE BIT(0)
67#define OMAP2_MCSPI_MODULCTRL_MS BIT(2)
68#define OMAP2_MCSPI_MODULCTRL_STEST BIT(3)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070069
Jouni Hogander7a8fa722009-09-22 16:45:58 -070070#define OMAP2_MCSPI_CHCONF_PHA BIT(0)
71#define OMAP2_MCSPI_CHCONF_POL BIT(1)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070072#define OMAP2_MCSPI_CHCONF_CLKD_MASK (0x0f << 2)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070073#define OMAP2_MCSPI_CHCONF_EPOL BIT(6)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070074#define OMAP2_MCSPI_CHCONF_WL_MASK (0x1f << 7)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070075#define OMAP2_MCSPI_CHCONF_TRM_RX_ONLY BIT(12)
76#define OMAP2_MCSPI_CHCONF_TRM_TX_ONLY BIT(13)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070077#define OMAP2_MCSPI_CHCONF_TRM_MASK (0x03 << 12)
Jouni Hogander7a8fa722009-09-22 16:45:58 -070078#define OMAP2_MCSPI_CHCONF_DMAW BIT(14)
79#define OMAP2_MCSPI_CHCONF_DMAR BIT(15)
80#define OMAP2_MCSPI_CHCONF_DPE0 BIT(16)
81#define OMAP2_MCSPI_CHCONF_DPE1 BIT(17)
82#define OMAP2_MCSPI_CHCONF_IS BIT(18)
83#define OMAP2_MCSPI_CHCONF_TURBO BIT(19)
84#define OMAP2_MCSPI_CHCONF_FORCE BIT(20)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070085
Jouni Hogander7a8fa722009-09-22 16:45:58 -070086#define OMAP2_MCSPI_CHSTAT_RXS BIT(0)
87#define OMAP2_MCSPI_CHSTAT_TXS BIT(1)
88#define OMAP2_MCSPI_CHSTAT_EOT BIT(2)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070089
Jouni Hogander7a8fa722009-09-22 16:45:58 -070090#define OMAP2_MCSPI_CHCTRL_EN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070091
Jouni Hogander7a8fa722009-09-22 16:45:58 -070092#define OMAP2_MCSPI_WAKEUPENABLE_WKEN BIT(0)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -070093
94/* We have 2 DMA channels per CS, one for RX and one for TX */
95struct omap2_mcspi_dma {
96 int dma_tx_channel;
97 int dma_rx_channel;
98
99 int dma_tx_sync_dev;
100 int dma_rx_sync_dev;
101
102 struct completion dma_tx_completion;
103 struct completion dma_rx_completion;
104};
105
106/* use PIO for small transfers, avoiding DMA setup/teardown overhead and
107 * cache operations; better heuristics consider wordsize and bitrate.
108 */
Roman Tereshonkov8b66c132010-04-12 09:07:54 +0000109#define DMA_MIN_BYTES 160
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700110
111
112struct omap2_mcspi {
113 struct work_struct work;
114 /* lock protects queue and registers */
115 spinlock_t lock;
116 struct list_head msg_queue;
117 struct spi_master *master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700118 /* Virtual base address of the controller */
119 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100120 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700121 /* SPI1 has 4 channels, while SPI2 has 2 */
122 struct omap2_mcspi_dma *dma_channels;
Govindraj.R1f1a4382011-02-02 17:52:15 +0530123 struct device *dev;
Shubhrajyoti D2856ac12011-10-28 17:14:17 +0530124 struct workqueue_struct *wq;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700125};
126
127struct omap2_mcspi_cs {
128 void __iomem *base;
Russell Kinge5480b732008-09-01 21:51:50 +0100129 unsigned long phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700130 int word_len;
Tero Kristo89c05372009-09-22 16:46:17 -0700131 struct list_head node;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700132 /* Context save and restore shadow register */
133 u32 chconf0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700134};
135
Hemanth Va41ae1a2009-09-22 16:46:16 -0700136/* used for context save and restore, structure members to be updated whenever
137 * corresponding registers are modified.
138 */
139struct omap2_mcspi_regs {
Hemanth Va41ae1a2009-09-22 16:46:16 -0700140 u32 modulctrl;
141 u32 wakeupenable;
Tero Kristo89c05372009-09-22 16:46:17 -0700142 struct list_head cs;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700143};
144
145static struct omap2_mcspi_regs omap2_mcspi_ctx[OMAP2_MCSPI_MAX_CTRL];
146
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700147#define MOD_REG_BIT(val, mask, set) do { \
148 if (set) \
149 val |= mask; \
150 else \
151 val &= ~mask; \
152} while (0)
153
154static inline void mcspi_write_reg(struct spi_master *master,
155 int idx, u32 val)
156{
157 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
158
159 __raw_writel(val, mcspi->base + idx);
160}
161
162static inline u32 mcspi_read_reg(struct spi_master *master, int idx)
163{
164 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
165
166 return __raw_readl(mcspi->base + idx);
167}
168
169static inline void mcspi_write_cs_reg(const struct spi_device *spi,
170 int idx, u32 val)
171{
172 struct omap2_mcspi_cs *cs = spi->controller_state;
173
174 __raw_writel(val, cs->base + idx);
175}
176
177static inline u32 mcspi_read_cs_reg(const struct spi_device *spi, int idx)
178{
179 struct omap2_mcspi_cs *cs = spi->controller_state;
180
181 return __raw_readl(cs->base + idx);
182}
183
Hemanth Va41ae1a2009-09-22 16:46:16 -0700184static inline u32 mcspi_cached_chconf0(const struct spi_device *spi)
185{
186 struct omap2_mcspi_cs *cs = spi->controller_state;
187
188 return cs->chconf0;
189}
190
191static inline void mcspi_write_chconf0(const struct spi_device *spi, u32 val)
192{
193 struct omap2_mcspi_cs *cs = spi->controller_state;
194
195 cs->chconf0 = val;
196 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCONF0, val);
Roman Tereshonkova330ce22010-03-15 09:06:28 +0000197 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700198}
199
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700200static void omap2_mcspi_set_dma_req(const struct spi_device *spi,
201 int is_read, int enable)
202{
203 u32 l, rw;
204
Hemanth Va41ae1a2009-09-22 16:46:16 -0700205 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700206
207 if (is_read) /* 1 is read, 0 write */
208 rw = OMAP2_MCSPI_CHCONF_DMAR;
209 else
210 rw = OMAP2_MCSPI_CHCONF_DMAW;
211
212 MOD_REG_BIT(l, rw, enable);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700213 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700214}
215
216static void omap2_mcspi_set_enable(const struct spi_device *spi, int enable)
217{
218 u32 l;
219
220 l = enable ? OMAP2_MCSPI_CHCTRL_EN : 0;
221 mcspi_write_cs_reg(spi, OMAP2_MCSPI_CHCTRL0, l);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000222 /* Flash post-writes */
223 mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHCTRL0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700224}
225
226static void omap2_mcspi_force_cs(struct spi_device *spi, int cs_active)
227{
228 u32 l;
229
Hemanth Va41ae1a2009-09-22 16:46:16 -0700230 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700231 MOD_REG_BIT(l, OMAP2_MCSPI_CHCONF_FORCE, cs_active);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700232 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700233}
234
235static void omap2_mcspi_set_master_mode(struct spi_master *master)
236{
237 u32 l;
238
239 /* setup when switching from (reset default) slave mode
240 * to single-channel master mode
241 */
242 l = mcspi_read_reg(master, OMAP2_MCSPI_MODULCTRL);
243 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_STEST, 0);
244 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_MS, 0);
245 MOD_REG_BIT(l, OMAP2_MCSPI_MODULCTRL_SINGLE, 1);
246 mcspi_write_reg(master, OMAP2_MCSPI_MODULCTRL, l);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700247
248 omap2_mcspi_ctx[master->bus_num - 1].modulctrl = l;
249}
250
251static void omap2_mcspi_restore_ctx(struct omap2_mcspi *mcspi)
252{
253 struct spi_master *spi_cntrl;
Tero Kristo89c05372009-09-22 16:46:17 -0700254 struct omap2_mcspi_cs *cs;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700255 spi_cntrl = mcspi->master;
256
257 /* McSPI: context restore */
258 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_MODULCTRL,
259 omap2_mcspi_ctx[spi_cntrl->bus_num - 1].modulctrl);
260
Hemanth Va41ae1a2009-09-22 16:46:16 -0700261 mcspi_write_reg(spi_cntrl, OMAP2_MCSPI_WAKEUPENABLE,
262 omap2_mcspi_ctx[spi_cntrl->bus_num - 1].wakeupenable);
Tero Kristo89c05372009-09-22 16:46:17 -0700263
264 list_for_each_entry(cs, &omap2_mcspi_ctx[spi_cntrl->bus_num - 1].cs,
265 node)
266 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700267}
268static void omap2_mcspi_disable_clocks(struct omap2_mcspi *mcspi)
269{
Govindraj.R1f1a4382011-02-02 17:52:15 +0530270 pm_runtime_put_sync(mcspi->dev);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700271}
272
273static int omap2_mcspi_enable_clocks(struct omap2_mcspi *mcspi)
274{
Govindraj.R1f1a4382011-02-02 17:52:15 +0530275 return pm_runtime_get_sync(mcspi->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700276}
277
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300278static int mcspi_wait_for_reg_bit(void __iomem *reg, unsigned long bit)
279{
280 unsigned long timeout;
281
282 timeout = jiffies + msecs_to_jiffies(1000);
283 while (!(__raw_readl(reg) & bit)) {
284 if (time_after(jiffies, timeout))
285 return -1;
286 cpu_relax();
287 }
288 return 0;
289}
290
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700291static unsigned
292omap2_mcspi_txrx_dma(struct spi_device *spi, struct spi_transfer *xfer)
293{
294 struct omap2_mcspi *mcspi;
295 struct omap2_mcspi_cs *cs = spi->controller_state;
296 struct omap2_mcspi_dma *mcspi_dma;
297 unsigned int count, c;
298 unsigned long base, tx_reg, rx_reg;
299 int word_len, data_type, element_count;
Govindraj.R8b20c8c2011-06-01 11:31:24 +0530300 int elements = 0;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000301 u32 l;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700302 u8 * rx;
303 const u8 * tx;
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300304 void __iomem *chstat_reg;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700305
306 mcspi = spi_master_get_devdata(spi->master);
307 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000308 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700309
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300310 chstat_reg = cs->base + OMAP2_MCSPI_CHSTAT0;
311
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700312 count = xfer->len;
313 c = count;
314 word_len = cs->word_len;
315
Russell Kinge5480b732008-09-01 21:51:50 +0100316 base = cs->phys;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700317 tx_reg = base + OMAP2_MCSPI_TX0;
318 rx_reg = base + OMAP2_MCSPI_RX0;
319 rx = xfer->rx_buf;
320 tx = xfer->tx_buf;
321
322 if (word_len <= 8) {
323 data_type = OMAP_DMA_DATA_TYPE_S8;
324 element_count = count;
325 } else if (word_len <= 16) {
326 data_type = OMAP_DMA_DATA_TYPE_S16;
327 element_count = count >> 1;
328 } else /* word_len <= 32 */ {
329 data_type = OMAP_DMA_DATA_TYPE_S32;
330 element_count = count >> 2;
331 }
332
333 if (tx != NULL) {
334 omap_set_dma_transfer_params(mcspi_dma->dma_tx_channel,
335 data_type, element_count, 1,
336 OMAP_DMA_SYNC_ELEMENT,
337 mcspi_dma->dma_tx_sync_dev, 0);
338
339 omap_set_dma_dest_params(mcspi_dma->dma_tx_channel, 0,
340 OMAP_DMA_AMODE_CONSTANT,
341 tx_reg, 0, 0);
342
343 omap_set_dma_src_params(mcspi_dma->dma_tx_channel, 0,
344 OMAP_DMA_AMODE_POST_INC,
345 xfer->tx_dma, 0, 0);
346 }
347
348 if (rx != NULL) {
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000349 elements = element_count - 1;
350 if (l & OMAP2_MCSPI_CHCONF_TURBO)
351 elements--;
352
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700353 omap_set_dma_transfer_params(mcspi_dma->dma_rx_channel,
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000354 data_type, elements, 1,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700355 OMAP_DMA_SYNC_ELEMENT,
356 mcspi_dma->dma_rx_sync_dev, 1);
357
358 omap_set_dma_src_params(mcspi_dma->dma_rx_channel, 0,
359 OMAP_DMA_AMODE_CONSTANT,
360 rx_reg, 0, 0);
361
362 omap_set_dma_dest_params(mcspi_dma->dma_rx_channel, 0,
363 OMAP_DMA_AMODE_POST_INC,
364 xfer->rx_dma, 0, 0);
365 }
366
367 if (tx != NULL) {
368 omap_start_dma(mcspi_dma->dma_tx_channel);
369 omap2_mcspi_set_dma_req(spi, 0, 1);
370 }
371
372 if (rx != NULL) {
373 omap_start_dma(mcspi_dma->dma_rx_channel);
374 omap2_mcspi_set_dma_req(spi, 1, 1);
375 }
376
377 if (tx != NULL) {
378 wait_for_completion(&mcspi_dma->dma_tx_completion);
Russell King - ARM Linux07fe0352011-01-07 15:49:20 +0000379 dma_unmap_single(&spi->dev, xfer->tx_dma, count, DMA_TO_DEVICE);
Ilkka Koskinen2764c502010-10-19 17:07:31 +0300380
381 /* for TX_ONLY mode, be sure all words have shifted out */
382 if (rx == NULL) {
383 if (mcspi_wait_for_reg_bit(chstat_reg,
384 OMAP2_MCSPI_CHSTAT_TXS) < 0)
385 dev_err(&spi->dev, "TXS timed out\n");
386 else if (mcspi_wait_for_reg_bit(chstat_reg,
387 OMAP2_MCSPI_CHSTAT_EOT) < 0)
388 dev_err(&spi->dev, "EOT timed out\n");
389 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700390 }
391
392 if (rx != NULL) {
393 wait_for_completion(&mcspi_dma->dma_rx_completion);
Russell King - ARM Linux07fe0352011-01-07 15:49:20 +0000394 dma_unmap_single(&spi->dev, xfer->rx_dma, count, DMA_FROM_DEVICE);
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700395 omap2_mcspi_set_enable(spi, 0);
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000396
397 if (l & OMAP2_MCSPI_CHCONF_TURBO) {
398
399 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
400 & OMAP2_MCSPI_CHSTAT_RXS)) {
401 u32 w;
402
403 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
404 if (word_len <= 8)
405 ((u8 *)xfer->rx_buf)[elements++] = w;
406 else if (word_len <= 16)
407 ((u16 *)xfer->rx_buf)[elements++] = w;
408 else /* word_len <= 32 */
409 ((u32 *)xfer->rx_buf)[elements++] = w;
410 } else {
411 dev_err(&spi->dev,
412 "DMA RX penultimate word empty");
413 count -= (word_len <= 8) ? 2 :
414 (word_len <= 16) ? 4 :
415 /* word_len <= 32 */ 8;
416 omap2_mcspi_set_enable(spi, 1);
417 return count;
418 }
419 }
420
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700421 if (likely(mcspi_read_cs_reg(spi, OMAP2_MCSPI_CHSTAT0)
422 & OMAP2_MCSPI_CHSTAT_RXS)) {
423 u32 w;
424
425 w = mcspi_read_cs_reg(spi, OMAP2_MCSPI_RX0);
426 if (word_len <= 8)
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000427 ((u8 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700428 else if (word_len <= 16)
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000429 ((u16 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700430 else /* word_len <= 32 */
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000431 ((u32 *)xfer->rx_buf)[elements] = w;
Eero Nurkkala57c5c28d2009-07-29 15:02:12 -0700432 } else {
433 dev_err(&spi->dev, "DMA RX last word empty");
434 count -= (word_len <= 8) ? 1 :
435 (word_len <= 16) ? 2 :
436 /* word_len <= 32 */ 4;
437 }
438 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700439 }
440 return count;
441}
442
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700443static unsigned
444omap2_mcspi_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer)
445{
446 struct omap2_mcspi *mcspi;
447 struct omap2_mcspi_cs *cs = spi->controller_state;
448 unsigned int count, c;
449 u32 l;
450 void __iomem *base = cs->base;
451 void __iomem *tx_reg;
452 void __iomem *rx_reg;
453 void __iomem *chstat_reg;
454 int word_len;
455
456 mcspi = spi_master_get_devdata(spi->master);
457 count = xfer->len;
458 c = count;
459 word_len = cs->word_len;
460
Hemanth Va41ae1a2009-09-22 16:46:16 -0700461 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700462
463 /* We store the pre-calculated register addresses on stack to speed
464 * up the transfer loop. */
465 tx_reg = base + OMAP2_MCSPI_TX0;
466 rx_reg = base + OMAP2_MCSPI_RX0;
467 chstat_reg = base + OMAP2_MCSPI_CHSTAT0;
468
Michael Jonesadef6582011-02-25 16:55:11 +0100469 if (c < (word_len>>3))
470 return 0;
471
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700472 if (word_len <= 8) {
473 u8 *rx;
474 const u8 *tx;
475
476 rx = xfer->rx_buf;
477 tx = xfer->tx_buf;
478
479 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800480 c -= 1;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700481 if (tx != NULL) {
482 if (mcspi_wait_for_reg_bit(chstat_reg,
483 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
484 dev_err(&spi->dev, "TXS timed out\n");
485 goto out;
486 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900487 dev_vdbg(&spi->dev, "write-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700488 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700489 __raw_writel(*tx++, tx_reg);
490 }
491 if (rx != NULL) {
492 if (mcspi_wait_for_reg_bit(chstat_reg,
493 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
494 dev_err(&spi->dev, "RXS timed out\n");
495 goto out;
496 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000497
498 if (c == 1 && tx == NULL &&
499 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
500 omap2_mcspi_set_enable(spi, 0);
501 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900502 dev_vdbg(&spi->dev, "read-%d %02x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000503 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000504 if (mcspi_wait_for_reg_bit(chstat_reg,
505 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
506 dev_err(&spi->dev,
507 "RXS timed out\n");
508 goto out;
509 }
510 c = 0;
511 } else if (c == 0 && tx == NULL) {
512 omap2_mcspi_set_enable(spi, 0);
513 }
514
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700515 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900516 dev_vdbg(&spi->dev, "read-%d %02x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700517 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700518 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200519 } while (c);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700520 } else if (word_len <= 16) {
521 u16 *rx;
522 const u16 *tx;
523
524 rx = xfer->rx_buf;
525 tx = xfer->tx_buf;
526 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800527 c -= 2;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700528 if (tx != NULL) {
529 if (mcspi_wait_for_reg_bit(chstat_reg,
530 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
531 dev_err(&spi->dev, "TXS timed out\n");
532 goto out;
533 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900534 dev_vdbg(&spi->dev, "write-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700535 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700536 __raw_writel(*tx++, tx_reg);
537 }
538 if (rx != NULL) {
539 if (mcspi_wait_for_reg_bit(chstat_reg,
540 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
541 dev_err(&spi->dev, "RXS timed out\n");
542 goto out;
543 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000544
545 if (c == 2 && tx == NULL &&
546 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
547 omap2_mcspi_set_enable(spi, 0);
548 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900549 dev_vdbg(&spi->dev, "read-%d %04x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000550 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000551 if (mcspi_wait_for_reg_bit(chstat_reg,
552 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
553 dev_err(&spi->dev,
554 "RXS timed out\n");
555 goto out;
556 }
557 c = 0;
558 } else if (c == 0 && tx == NULL) {
559 omap2_mcspi_set_enable(spi, 0);
560 }
561
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700562 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900563 dev_vdbg(&spi->dev, "read-%d %04x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700564 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700565 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200566 } while (c >= 2);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700567 } else if (word_len <= 32) {
568 u32 *rx;
569 const u32 *tx;
570
571 rx = xfer->rx_buf;
572 tx = xfer->tx_buf;
573 do {
Kalle Valofeed9ba2008-01-24 14:00:40 -0800574 c -= 4;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700575 if (tx != NULL) {
576 if (mcspi_wait_for_reg_bit(chstat_reg,
577 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
578 dev_err(&spi->dev, "TXS timed out\n");
579 goto out;
580 }
Felipe Balbi079a1762010-09-29 17:31:29 +0900581 dev_vdbg(&spi->dev, "write-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700582 word_len, *tx);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700583 __raw_writel(*tx++, tx_reg);
584 }
585 if (rx != NULL) {
586 if (mcspi_wait_for_reg_bit(chstat_reg,
587 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
588 dev_err(&spi->dev, "RXS timed out\n");
589 goto out;
590 }
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000591
592 if (c == 4 && tx == NULL &&
593 (l & OMAP2_MCSPI_CHCONF_TURBO)) {
594 omap2_mcspi_set_enable(spi, 0);
595 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900596 dev_vdbg(&spi->dev, "read-%d %08x\n",
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000597 word_len, *(rx - 1));
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000598 if (mcspi_wait_for_reg_bit(chstat_reg,
599 OMAP2_MCSPI_CHSTAT_RXS) < 0) {
600 dev_err(&spi->dev,
601 "RXS timed out\n");
602 goto out;
603 }
604 c = 0;
605 } else if (c == 0 && tx == NULL) {
606 omap2_mcspi_set_enable(spi, 0);
607 }
608
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700609 *rx++ = __raw_readl(rx_reg);
Felipe Balbi079a1762010-09-29 17:31:29 +0900610 dev_vdbg(&spi->dev, "read-%d %08x\n",
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700611 word_len, *(rx - 1));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700612 }
Jarkko Nikula95c5c3a2011-03-21 16:27:30 +0200613 } while (c >= 4);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700614 }
615
616 /* for TX_ONLY mode, be sure all words have shifted out */
617 if (xfer->rx_buf == NULL) {
618 if (mcspi_wait_for_reg_bit(chstat_reg,
619 OMAP2_MCSPI_CHSTAT_TXS) < 0) {
620 dev_err(&spi->dev, "TXS timed out\n");
621 } else if (mcspi_wait_for_reg_bit(chstat_reg,
622 OMAP2_MCSPI_CHSTAT_EOT) < 0)
623 dev_err(&spi->dev, "EOT timed out\n");
Jason Wange1993ed2010-10-19 18:03:27 +0800624
625 /* disable chan to purge rx datas received in TX_ONLY transfer,
626 * otherwise these rx datas will affect the direct following
627 * RX_ONLY transfer.
628 */
629 omap2_mcspi_set_enable(spi, 0);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700630 }
631out:
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000632 omap2_mcspi_set_enable(spi, 1);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700633 return count - c;
634}
635
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200636static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
637{
638 u32 div;
639
640 for (div = 0; div < 15; div++)
641 if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
642 return div;
643
644 return 15;
645}
646
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700647/* called only when no transfer is active to this device */
648static int omap2_mcspi_setup_transfer(struct spi_device *spi,
649 struct spi_transfer *t)
650{
651 struct omap2_mcspi_cs *cs = spi->controller_state;
652 struct omap2_mcspi *mcspi;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700653 struct spi_master *spi_cntrl;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700654 u32 l = 0, div = 0;
655 u8 word_len = spi->bits_per_word;
Scott Ellis9bd45172010-03-10 14:23:13 -0700656 u32 speed_hz = spi->max_speed_hz;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700657
658 mcspi = spi_master_get_devdata(spi->master);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700659 spi_cntrl = mcspi->master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700660
661 if (t != NULL && t->bits_per_word)
662 word_len = t->bits_per_word;
663
664 cs->word_len = word_len;
665
Scott Ellis9bd45172010-03-10 14:23:13 -0700666 if (t && t->speed_hz)
667 speed_hz = t->speed_hz;
668
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200669 speed_hz = min_t(u32, speed_hz, OMAP2_MCSPI_MAX_FREQ);
670 div = omap2_mcspi_calc_divisor(speed_hz);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700671
Hemanth Va41ae1a2009-09-22 16:46:16 -0700672 l = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700673
674 /* standard 4-wire master mode: SCK, MOSI/out, MISO/in, nCS
675 * REVISIT: this controller could support SPI_3WIRE mode.
676 */
677 l &= ~(OMAP2_MCSPI_CHCONF_IS|OMAP2_MCSPI_CHCONF_DPE1);
678 l |= OMAP2_MCSPI_CHCONF_DPE0;
679
680 /* wordlength */
681 l &= ~OMAP2_MCSPI_CHCONF_WL_MASK;
682 l |= (word_len - 1) << 7;
683
684 /* set chipselect polarity; manage with FORCE */
685 if (!(spi->mode & SPI_CS_HIGH))
686 l |= OMAP2_MCSPI_CHCONF_EPOL; /* active-low; normal */
687 else
688 l &= ~OMAP2_MCSPI_CHCONF_EPOL;
689
690 /* set clock divisor */
691 l &= ~OMAP2_MCSPI_CHCONF_CLKD_MASK;
692 l |= div << 2;
693
694 /* set SPI mode 0..3 */
695 if (spi->mode & SPI_CPOL)
696 l |= OMAP2_MCSPI_CHCONF_POL;
697 else
698 l &= ~OMAP2_MCSPI_CHCONF_POL;
699 if (spi->mode & SPI_CPHA)
700 l |= OMAP2_MCSPI_CHCONF_PHA;
701 else
702 l &= ~OMAP2_MCSPI_CHCONF_PHA;
703
Hemanth Va41ae1a2009-09-22 16:46:16 -0700704 mcspi_write_chconf0(spi, l);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700705
706 dev_dbg(&spi->dev, "setup: speed %d, sample %s edge, clk %s\n",
Hannu Heikkinen57d9c102011-02-24 21:31:33 +0200707 OMAP2_MCSPI_MAX_FREQ >> div,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700708 (spi->mode & SPI_CPHA) ? "trailing" : "leading",
709 (spi->mode & SPI_CPOL) ? "inverted" : "normal");
710
711 return 0;
712}
713
714static void omap2_mcspi_dma_rx_callback(int lch, u16 ch_status, void *data)
715{
716 struct spi_device *spi = data;
717 struct omap2_mcspi *mcspi;
718 struct omap2_mcspi_dma *mcspi_dma;
719
720 mcspi = spi_master_get_devdata(spi->master);
721 mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
722
723 complete(&mcspi_dma->dma_rx_completion);
724
725 /* We must disable the DMA RX request */
726 omap2_mcspi_set_dma_req(spi, 1, 0);
727}
728
729static void omap2_mcspi_dma_tx_callback(int lch, u16 ch_status, void *data)
730{
731 struct spi_device *spi = data;
732 struct omap2_mcspi *mcspi;
733 struct omap2_mcspi_dma *mcspi_dma;
734
735 mcspi = spi_master_get_devdata(spi->master);
736 mcspi_dma = &(mcspi->dma_channels[spi->chip_select]);
737
738 complete(&mcspi_dma->dma_tx_completion);
739
740 /* We must disable the DMA TX request */
741 omap2_mcspi_set_dma_req(spi, 0, 0);
742}
743
744static int omap2_mcspi_request_dma(struct spi_device *spi)
745{
746 struct spi_master *master = spi->master;
747 struct omap2_mcspi *mcspi;
748 struct omap2_mcspi_dma *mcspi_dma;
749
750 mcspi = spi_master_get_devdata(master);
751 mcspi_dma = mcspi->dma_channels + spi->chip_select;
752
753 if (omap_request_dma(mcspi_dma->dma_rx_sync_dev, "McSPI RX",
754 omap2_mcspi_dma_rx_callback, spi,
755 &mcspi_dma->dma_rx_channel)) {
756 dev_err(&spi->dev, "no RX DMA channel for McSPI\n");
757 return -EAGAIN;
758 }
759
760 if (omap_request_dma(mcspi_dma->dma_tx_sync_dev, "McSPI TX",
761 omap2_mcspi_dma_tx_callback, spi,
762 &mcspi_dma->dma_tx_channel)) {
763 omap_free_dma(mcspi_dma->dma_rx_channel);
764 mcspi_dma->dma_rx_channel = -1;
765 dev_err(&spi->dev, "no TX DMA channel for McSPI\n");
766 return -EAGAIN;
767 }
768
769 init_completion(&mcspi_dma->dma_rx_completion);
770 init_completion(&mcspi_dma->dma_tx_completion);
771
772 return 0;
773}
774
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700775static int omap2_mcspi_setup(struct spi_device *spi)
776{
777 int ret;
778 struct omap2_mcspi *mcspi;
779 struct omap2_mcspi_dma *mcspi_dma;
780 struct omap2_mcspi_cs *cs = spi->controller_state;
781
David Brownell7d077192009-06-17 16:26:03 -0700782 if (spi->bits_per_word < 4 || spi->bits_per_word > 32) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700783 dev_dbg(&spi->dev, "setup: unsupported %d bit words\n",
784 spi->bits_per_word);
785 return -EINVAL;
786 }
787
788 mcspi = spi_master_get_devdata(spi->master);
789 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
790
791 if (!cs) {
792 cs = kzalloc(sizeof *cs, GFP_KERNEL);
793 if (!cs)
794 return -ENOMEM;
795 cs->base = mcspi->base + spi->chip_select * 0x14;
Russell Kinge5480b732008-09-01 21:51:50 +0100796 cs->phys = mcspi->phys + spi->chip_select * 0x14;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700797 cs->chconf0 = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700798 spi->controller_state = cs;
Tero Kristo89c05372009-09-22 16:46:17 -0700799 /* Link this to context save list */
800 list_add_tail(&cs->node,
801 &omap2_mcspi_ctx[mcspi->master->bus_num - 1].cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700802 }
803
804 if (mcspi_dma->dma_rx_channel == -1
805 || mcspi_dma->dma_tx_channel == -1) {
806 ret = omap2_mcspi_request_dma(spi);
807 if (ret < 0)
808 return ret;
809 }
810
Govindraj.R1f1a4382011-02-02 17:52:15 +0530811 ret = omap2_mcspi_enable_clocks(mcspi);
812 if (ret < 0)
813 return ret;
Hemanth Va41ae1a2009-09-22 16:46:16 -0700814
Kyungmin Park86eeb6f2007-10-16 01:27:45 -0700815 ret = omap2_mcspi_setup_transfer(spi, NULL);
Hemanth Va41ae1a2009-09-22 16:46:16 -0700816 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700817
818 return ret;
819}
820
821static void omap2_mcspi_cleanup(struct spi_device *spi)
822{
823 struct omap2_mcspi *mcspi;
824 struct omap2_mcspi_dma *mcspi_dma;
Tero Kristo89c05372009-09-22 16:46:17 -0700825 struct omap2_mcspi_cs *cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700826
827 mcspi = spi_master_get_devdata(spi->master);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700828
Scott Ellis5e774942010-03-10 14:22:45 -0700829 if (spi->controller_state) {
830 /* Unlink controller state from context save list */
831 cs = spi->controller_state;
832 list_del(&cs->node);
Tero Kristo89c05372009-09-22 16:46:17 -0700833
Scott Ellis5e774942010-03-10 14:22:45 -0700834 kfree(spi->controller_state);
835 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700836
Scott Ellis99f1a432010-05-24 14:20:27 +0000837 if (spi->chip_select < spi->master->num_chipselect) {
838 mcspi_dma = &mcspi->dma_channels[spi->chip_select];
839
840 if (mcspi_dma->dma_rx_channel != -1) {
841 omap_free_dma(mcspi_dma->dma_rx_channel);
842 mcspi_dma->dma_rx_channel = -1;
843 }
844 if (mcspi_dma->dma_tx_channel != -1) {
845 omap_free_dma(mcspi_dma->dma_tx_channel);
846 mcspi_dma->dma_tx_channel = -1;
847 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700848 }
849}
850
851static void omap2_mcspi_work(struct work_struct *work)
852{
853 struct omap2_mcspi *mcspi;
854
855 mcspi = container_of(work, struct omap2_mcspi, work);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700856
Govindraj.R1f1a4382011-02-02 17:52:15 +0530857 if (omap2_mcspi_enable_clocks(mcspi) < 0)
858 return;
859
860 spin_lock_irq(&mcspi->lock);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700861
862 /* We only enable one channel at a time -- the one whose message is
863 * at the head of the queue -- although this controller would gladly
864 * arbitrate among multiple channels. This corresponds to "single
865 * channel" master mode. As a side effect, we need to manage the
866 * chipselect with the FORCE bit ... CS != channel enable.
867 */
868 while (!list_empty(&mcspi->msg_queue)) {
869 struct spi_message *m;
870 struct spi_device *spi;
871 struct spi_transfer *t = NULL;
872 int cs_active = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700873 struct omap2_mcspi_cs *cs;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000874 struct omap2_mcspi_device_config *cd;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700875 int par_override = 0;
876 int status = 0;
877 u32 chconf;
878
879 m = container_of(mcspi->msg_queue.next, struct spi_message,
880 queue);
881
882 list_del_init(&m->queue);
883 spin_unlock_irq(&mcspi->lock);
884
885 spi = m->spi;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700886 cs = spi->controller_state;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000887 cd = spi->controller_data;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700888
889 omap2_mcspi_set_enable(spi, 1);
890 list_for_each_entry(t, &m->transfers, transfer_list) {
891 if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) {
892 status = -EINVAL;
893 break;
894 }
895 if (par_override || t->speed_hz || t->bits_per_word) {
896 par_override = 1;
897 status = omap2_mcspi_setup_transfer(spi, t);
898 if (status < 0)
899 break;
900 if (!t->speed_hz && !t->bits_per_word)
901 par_override = 0;
902 }
903
904 if (!cs_active) {
905 omap2_mcspi_force_cs(spi, 1);
906 cs_active = 1;
907 }
908
Hemanth Va41ae1a2009-09-22 16:46:16 -0700909 chconf = mcspi_cached_chconf0(spi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700910 chconf &= ~OMAP2_MCSPI_CHCONF_TRM_MASK;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000911 chconf &= ~OMAP2_MCSPI_CHCONF_TURBO;
912
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700913 if (t->tx_buf == NULL)
914 chconf |= OMAP2_MCSPI_CHCONF_TRM_RX_ONLY;
915 else if (t->rx_buf == NULL)
916 chconf |= OMAP2_MCSPI_CHCONF_TRM_TX_ONLY;
Roman Tereshonkov4743a0f2010-04-13 10:41:51 +0000917
918 if (cd && cd->turbo_mode && t->tx_buf == NULL) {
919 /* Turbo mode is for more than one word */
920 if (t->len > ((cs->word_len + 7) >> 3))
921 chconf |= OMAP2_MCSPI_CHCONF_TURBO;
922 }
923
Hemanth Va41ae1a2009-09-22 16:46:16 -0700924 mcspi_write_chconf0(spi, chconf);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700925
926 if (t->len) {
927 unsigned count;
928
929 /* RX_ONLY mode needs dummy data in TX reg */
930 if (t->tx_buf == NULL)
931 __raw_writel(0, cs->base
932 + OMAP2_MCSPI_TX0);
933
934 if (m->is_dma_mapped || t->len >= DMA_MIN_BYTES)
935 count = omap2_mcspi_txrx_dma(spi, t);
936 else
937 count = omap2_mcspi_txrx_pio(spi, t);
938 m->actual_length += count;
939
940 if (count != t->len) {
941 status = -EIO;
942 break;
943 }
944 }
945
946 if (t->delay_usecs)
947 udelay(t->delay_usecs);
948
949 /* ignore the "leave it on after last xfer" hint */
950 if (t->cs_change) {
951 omap2_mcspi_force_cs(spi, 0);
952 cs_active = 0;
953 }
954 }
955
956 /* Restore defaults if they were overriden */
957 if (par_override) {
958 par_override = 0;
959 status = omap2_mcspi_setup_transfer(spi, NULL);
960 }
961
962 if (cs_active)
963 omap2_mcspi_force_cs(spi, 0);
964
965 omap2_mcspi_set_enable(spi, 0);
966
967 m->status = status;
968 m->complete(m->context);
969
970 spin_lock_irq(&mcspi->lock);
971 }
972
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700973 spin_unlock_irq(&mcspi->lock);
Govindraj.R1f1a4382011-02-02 17:52:15 +0530974
975 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -0700976}
977
978static int omap2_mcspi_transfer(struct spi_device *spi, struct spi_message *m)
979{
980 struct omap2_mcspi *mcspi;
981 unsigned long flags;
982 struct spi_transfer *t;
983
984 m->actual_length = 0;
985 m->status = 0;
986
987 /* reject invalid messages and transfers */
988 if (list_empty(&m->transfers) || !m->complete)
989 return -EINVAL;
990 list_for_each_entry(t, &m->transfers, transfer_list) {
991 const void *tx_buf = t->tx_buf;
992 void *rx_buf = t->rx_buf;
993 unsigned len = t->len;
994
995 if (t->speed_hz > OMAP2_MCSPI_MAX_FREQ
996 || (len && !(rx_buf || tx_buf))
997 || (t->bits_per_word &&
998 ( t->bits_per_word < 4
999 || t->bits_per_word > 32))) {
1000 dev_dbg(&spi->dev, "transfer: %d Hz, %d %s%s, %d bpw\n",
1001 t->speed_hz,
1002 len,
1003 tx_buf ? "tx" : "",
1004 rx_buf ? "rx" : "",
1005 t->bits_per_word);
1006 return -EINVAL;
1007 }
Hannu Heikkinen57d9c102011-02-24 21:31:33 +02001008 if (t->speed_hz && t->speed_hz < (OMAP2_MCSPI_MAX_FREQ >> 15)) {
1009 dev_dbg(&spi->dev, "speed_hz %d below minimum %d Hz\n",
1010 t->speed_hz,
1011 OMAP2_MCSPI_MAX_FREQ >> 15);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001012 return -EINVAL;
1013 }
1014
1015 if (m->is_dma_mapped || len < DMA_MIN_BYTES)
1016 continue;
1017
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001018 if (tx_buf != NULL) {
1019 t->tx_dma = dma_map_single(&spi->dev, (void *) tx_buf,
1020 len, DMA_TO_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001021 if (dma_mapping_error(&spi->dev, t->tx_dma)) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001022 dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
1023 'T', len);
1024 return -EINVAL;
1025 }
1026 }
1027 if (rx_buf != NULL) {
1028 t->rx_dma = dma_map_single(&spi->dev, rx_buf, t->len,
1029 DMA_FROM_DEVICE);
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -07001030 if (dma_mapping_error(&spi->dev, t->rx_dma)) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001031 dev_dbg(&spi->dev, "dma %cX %d bytes error\n",
1032 'R', len);
1033 if (tx_buf != NULL)
Russell King - ARM Linux07fe0352011-01-07 15:49:20 +00001034 dma_unmap_single(&spi->dev, t->tx_dma,
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001035 len, DMA_TO_DEVICE);
1036 return -EINVAL;
1037 }
1038 }
1039 }
1040
1041 mcspi = spi_master_get_devdata(spi->master);
1042
1043 spin_lock_irqsave(&mcspi->lock, flags);
1044 list_add_tail(&m->queue, &mcspi->msg_queue);
Shubhrajyoti D2856ac12011-10-28 17:14:17 +05301045 queue_work(mcspi->wq, &mcspi->work);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001046 spin_unlock_irqrestore(&mcspi->lock, flags);
1047
1048 return 0;
1049}
1050
Govindraj.R1f1a4382011-02-02 17:52:15 +05301051static int __init omap2_mcspi_master_setup(struct omap2_mcspi *mcspi)
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001052{
1053 struct spi_master *master = mcspi->master;
1054 u32 tmp;
Govindraj.R1f1a4382011-02-02 17:52:15 +05301055 int ret = 0;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001056
Govindraj.R1f1a4382011-02-02 17:52:15 +05301057 ret = omap2_mcspi_enable_clocks(mcspi);
1058 if (ret < 0)
1059 return ret;
Jouni Hoganderddb22192009-07-29 15:02:11 -07001060
Hemanth Va41ae1a2009-09-22 16:46:16 -07001061 tmp = OMAP2_MCSPI_WAKEUPENABLE_WKEN;
1062 mcspi_write_reg(master, OMAP2_MCSPI_WAKEUPENABLE, tmp);
1063 omap2_mcspi_ctx[master->bus_num - 1].wakeupenable = tmp;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001064
1065 omap2_mcspi_set_master_mode(master);
Hemanth Va41ae1a2009-09-22 16:46:16 -07001066 omap2_mcspi_disable_clocks(mcspi);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001067 return 0;
1068}
1069
Govindraj.R1f1a4382011-02-02 17:52:15 +05301070static int omap_mcspi_runtime_resume(struct device *dev)
1071{
1072 struct omap2_mcspi *mcspi;
1073 struct spi_master *master;
1074
1075 master = dev_get_drvdata(dev);
1076 mcspi = spi_master_get_devdata(master);
1077 omap2_mcspi_restore_ctx(mcspi);
1078
1079 return 0;
1080}
1081
Girishccc7bae2008-02-06 01:38:16 -08001082
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001083static int __init omap2_mcspi_probe(struct platform_device *pdev)
1084{
1085 struct spi_master *master;
Charulatha V1a5d8192011-02-02 17:52:14 +05301086 struct omap2_mcspi_platform_config *pdata = pdev->dev.platform_data;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001087 struct omap2_mcspi *mcspi;
1088 struct resource *r;
1089 int status = 0, i;
Shubhrajyoti D2856ac12011-10-28 17:14:17 +05301090 char wq_name[20];
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001091
1092 master = spi_alloc_master(&pdev->dev, sizeof *mcspi);
1093 if (master == NULL) {
1094 dev_dbg(&pdev->dev, "master allocation failed\n");
1095 return -ENOMEM;
1096 }
1097
David Brownelle7db06b2009-06-17 16:26:04 -07001098 /* the spi->mode bits understood by this driver: */
1099 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1100
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001101 if (pdev->id != -1)
1102 master->bus_num = pdev->id;
1103
1104 master->setup = omap2_mcspi_setup;
1105 master->transfer = omap2_mcspi_transfer;
1106 master->cleanup = omap2_mcspi_cleanup;
Charulatha V1a5d8192011-02-02 17:52:14 +05301107 master->num_chipselect = pdata->num_cs;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001108
1109 dev_set_drvdata(&pdev->dev, master);
1110
1111 mcspi = spi_master_get_devdata(master);
1112 mcspi->master = master;
1113
Shubhrajyoti D2856ac12011-10-28 17:14:17 +05301114 sprintf(wq_name, "omap2_mcspi/%d", master->bus_num);
1115 mcspi->wq = alloc_workqueue(wq_name, WQ_MEM_RECLAIM, 1);
1116 if (mcspi->wq == NULL) {
1117 status = -ENOMEM;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301118 goto free_master;
Shubhrajyoti D2856ac12011-10-28 17:14:17 +05301119 }
1120
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001121 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1122 if (r == NULL) {
1123 status = -ENODEV;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301124 goto free_master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001125 }
Shubhrajyoti D1458d162011-10-24 15:54:24 +05301126
1127 r->start += pdata->regs_offset;
1128 r->end += pdata->regs_offset;
1129 mcspi->phys = r->start;
Joe Perches8e2943c2011-06-10 18:11:25 -07001130 if (!request_mem_region(r->start, resource_size(r),
1131 dev_name(&pdev->dev))) {
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001132 status = -EBUSY;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301133 goto free_master;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001134 }
1135
Joe Perches8e2943c2011-06-10 18:11:25 -07001136 mcspi->base = ioremap(r->start, resource_size(r));
Russell King55c381e2008-09-04 14:07:22 +01001137 if (!mcspi->base) {
1138 dev_dbg(&pdev->dev, "can't ioremap MCSPI\n");
1139 status = -ENOMEM;
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301140 goto release_region;
Russell King55c381e2008-09-04 14:07:22 +01001141 }
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001142
Govindraj.R1f1a4382011-02-02 17:52:15 +05301143 mcspi->dev = &pdev->dev;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001144 INIT_WORK(&mcspi->work, omap2_mcspi_work);
1145
1146 spin_lock_init(&mcspi->lock);
1147 INIT_LIST_HEAD(&mcspi->msg_queue);
Tero Kristo89c05372009-09-22 16:46:17 -07001148 INIT_LIST_HEAD(&omap2_mcspi_ctx[master->bus_num - 1].cs);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001149
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001150 mcspi->dma_channels = kcalloc(master->num_chipselect,
1151 sizeof(struct omap2_mcspi_dma),
1152 GFP_KERNEL);
1153
1154 if (mcspi->dma_channels == NULL)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301155 goto unmap_io;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001156
Charulatha V1a5d8192011-02-02 17:52:14 +05301157 for (i = 0; i < master->num_chipselect; i++) {
1158 char dma_ch_name[14];
1159 struct resource *dma_res;
1160
1161 sprintf(dma_ch_name, "rx%d", i);
1162 dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1163 dma_ch_name);
1164 if (!dma_res) {
1165 dev_dbg(&pdev->dev, "cannot get DMA RX channel\n");
1166 status = -ENODEV;
1167 break;
1168 }
1169
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001170 mcspi->dma_channels[i].dma_rx_channel = -1;
Charulatha V1a5d8192011-02-02 17:52:14 +05301171 mcspi->dma_channels[i].dma_rx_sync_dev = dma_res->start;
1172 sprintf(dma_ch_name, "tx%d", i);
1173 dma_res = platform_get_resource_byname(pdev, IORESOURCE_DMA,
1174 dma_ch_name);
1175 if (!dma_res) {
1176 dev_dbg(&pdev->dev, "cannot get DMA TX channel\n");
1177 status = -ENODEV;
1178 break;
1179 }
1180
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001181 mcspi->dma_channels[i].dma_tx_channel = -1;
Charulatha V1a5d8192011-02-02 17:52:14 +05301182 mcspi->dma_channels[i].dma_tx_sync_dev = dma_res->start;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001183 }
1184
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301185 if (status < 0)
1186 goto dma_chnl_free;
1187
Govindraj.R1f1a4382011-02-02 17:52:15 +05301188 pm_runtime_enable(&pdev->dev);
1189
1190 if (status || omap2_mcspi_master_setup(mcspi) < 0)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301191 goto disable_pm;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001192
1193 status = spi_register_master(master);
1194 if (status < 0)
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301195 goto err_spi_register;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001196
1197 return status;
1198
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301199err_spi_register:
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001200 spi_master_put(master);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301201disable_pm:
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301202 pm_runtime_disable(&pdev->dev);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301203dma_chnl_free:
Govindraj.R1f1a4382011-02-02 17:52:15 +05301204 kfree(mcspi->dma_channels);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301205unmap_io:
Govindraj.R1f1a4382011-02-02 17:52:15 +05301206 iounmap(mcspi->base);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301207release_region:
1208 release_mem_region(r->start, resource_size(r));
1209free_master:
1210 kfree(master);
1211 platform_set_drvdata(pdev, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001212 return status;
1213}
1214
1215static int __exit omap2_mcspi_remove(struct platform_device *pdev)
1216{
1217 struct spi_master *master;
1218 struct omap2_mcspi *mcspi;
1219 struct omap2_mcspi_dma *dma_channels;
1220 struct resource *r;
Russell King55c381e2008-09-04 14:07:22 +01001221 void __iomem *base;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001222
1223 master = dev_get_drvdata(&pdev->dev);
1224 mcspi = spi_master_get_devdata(master);
1225 dma_channels = mcspi->dma_channels;
1226
Govindraj.R1f1a4382011-02-02 17:52:15 +05301227 omap2_mcspi_disable_clocks(mcspi);
Shubhrajyoti D751c9252011-10-28 17:14:18 +05301228 pm_runtime_disable(&pdev->dev);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001229 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Joe Perches8e2943c2011-06-10 18:11:25 -07001230 release_mem_region(r->start, resource_size(r));
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001231
Russell King55c381e2008-09-04 14:07:22 +01001232 base = mcspi->base;
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001233 spi_unregister_master(master);
Russell King55c381e2008-09-04 14:07:22 +01001234 iounmap(base);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001235 kfree(dma_channels);
Shubhrajyoti D2856ac12011-10-28 17:14:17 +05301236 destroy_workqueue(mcspi->wq);
Shubhrajyoti D39f1b562011-10-28 17:14:19 +05301237 platform_set_drvdata(pdev, NULL);
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001238
1239 return 0;
1240}
1241
Kay Sievers7e38c3c2008-04-10 21:29:20 -07001242/* work with hotplug and coldplug */
1243MODULE_ALIAS("platform:omap2_mcspi");
1244
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001245#ifdef CONFIG_SUSPEND
1246/*
1247 * When SPI wake up from off-mode, CS is in activate state. If it was in
1248 * unactive state when driver was suspend, then force it to unactive state at
1249 * wake up.
1250 */
1251static int omap2_mcspi_resume(struct device *dev)
1252{
1253 struct spi_master *master = dev_get_drvdata(dev);
1254 struct omap2_mcspi *mcspi = spi_master_get_devdata(master);
1255 struct omap2_mcspi_cs *cs;
1256
1257 omap2_mcspi_enable_clocks(mcspi);
1258 list_for_each_entry(cs, &omap2_mcspi_ctx[master->bus_num - 1].cs,
1259 node) {
1260 if ((cs->chconf0 & OMAP2_MCSPI_CHCONF_FORCE) == 0) {
1261
1262 /*
1263 * We need to toggle CS state for OMAP take this
1264 * change in account.
1265 */
1266 MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 1);
1267 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1268 MOD_REG_BIT(cs->chconf0, OMAP2_MCSPI_CHCONF_FORCE, 0);
1269 __raw_writel(cs->chconf0, cs->base + OMAP2_MCSPI_CHCONF0);
1270 }
1271 }
1272 omap2_mcspi_disable_clocks(mcspi);
1273 return 0;
1274}
1275#else
1276#define omap2_mcspi_resume NULL
1277#endif
1278
1279static const struct dev_pm_ops omap2_mcspi_pm_ops = {
1280 .resume = omap2_mcspi_resume,
Govindraj.R1f1a4382011-02-02 17:52:15 +05301281 .runtime_resume = omap_mcspi_runtime_resume,
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001282};
1283
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001284static struct platform_driver omap2_mcspi_driver = {
1285 .driver = {
1286 .name = "omap2_mcspi",
1287 .owner = THIS_MODULE,
Gregory CLEMENT42ce7fd2010-12-29 11:52:53 +01001288 .pm = &omap2_mcspi_pm_ops
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001289 },
1290 .remove = __exit_p(omap2_mcspi_remove),
1291};
1292
1293
1294static int __init omap2_mcspi_init(void)
1295{
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001296 return platform_driver_probe(&omap2_mcspi_driver, omap2_mcspi_probe);
1297}
1298subsys_initcall(omap2_mcspi_init);
1299
1300static void __exit omap2_mcspi_exit(void)
1301{
1302 platform_driver_unregister(&omap2_mcspi_driver);
1303
Samuel Ortizccdc7bf2007-07-17 04:04:13 -07001304}
1305module_exit(omap2_mcspi_exit);
1306
1307MODULE_LICENSE("GPL");