aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/spi/spi_mpc8xxx.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/spi/spi_mpc8xxx.c')
-rw-r--r--drivers/spi/spi_mpc8xxx.c631
1 files changed, 544 insertions, 87 deletions
diff --git a/drivers/spi/spi_mpc8xxx.c b/drivers/spi/spi_mpc8xxx.c
index 0fd0ec4d3a7d..14d052316502 100644
--- a/drivers/spi/spi_mpc8xxx.c
+++ b/drivers/spi/spi_mpc8xxx.c
@@ -5,6 +5,10 @@
5 * 5 *
6 * Copyright (C) 2006 Polycom, Inc. 6 * Copyright (C) 2006 Polycom, Inc.
7 * 7 *
8 * CPM SPI and QE buffer descriptors mode support:
9 * Copyright (c) 2009 MontaVista Software, Inc.
10 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
11 *
8 * This program is free software; you can redistribute it and/or modify it 12 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the 13 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your 14 * Free Software Foundation; either version 2 of the License, or (at your
@@ -27,15 +31,30 @@
27#include <linux/spi/spi_bitbang.h> 31#include <linux/spi/spi_bitbang.h>
28#include <linux/platform_device.h> 32#include <linux/platform_device.h>
29#include <linux/fsl_devices.h> 33#include <linux/fsl_devices.h>
34#include <linux/dma-mapping.h>
35#include <linux/mm.h>
36#include <linux/mutex.h>
30#include <linux/of.h> 37#include <linux/of.h>
31#include <linux/of_platform.h> 38#include <linux/of_platform.h>
32#include <linux/gpio.h> 39#include <linux/gpio.h>
33#include <linux/of_gpio.h> 40#include <linux/of_gpio.h>
34#include <linux/of_spi.h> 41#include <linux/of_spi.h>
42#include <linux/slab.h>
35 43
36#include <sysdev/fsl_soc.h> 44#include <sysdev/fsl_soc.h>
45#include <asm/cpm.h>
46#include <asm/qe.h>
37#include <asm/irq.h> 47#include <asm/irq.h>
38 48
49/* CPM1 and CPM2 are mutually exclusive. */
50#ifdef CONFIG_CPM1
51#include <asm/cpm1.h>
52#define CPM_SPI_CMD mk_cr_cmd(CPM_CR_CH_SPI, 0)
53#else
54#include <asm/cpm2.h>
55#define CPM_SPI_CMD mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK, 0, 0)
56#endif
57
39/* SPI Controller registers */ 58/* SPI Controller registers */
40struct mpc8xxx_spi_reg { 59struct mpc8xxx_spi_reg {
41 u8 res1[0x20]; 60 u8 res1[0x20];
@@ -47,6 +66,28 @@ struct mpc8xxx_spi_reg {
47 __be32 receive; 66 __be32 receive;
48}; 67};
49 68
69/* SPI Parameter RAM */
70struct spi_pram {
71 __be16 rbase; /* Rx Buffer descriptor base address */
72 __be16 tbase; /* Tx Buffer descriptor base address */
73 u8 rfcr; /* Rx function code */
74 u8 tfcr; /* Tx function code */
75 __be16 mrblr; /* Max receive buffer length */
76 __be32 rstate; /* Internal */
77 __be32 rdp; /* Internal */
78 __be16 rbptr; /* Internal */
79 __be16 rbc; /* Internal */
80 __be32 rxtmp; /* Internal */
81 __be32 tstate; /* Internal */
82 __be32 tdp; /* Internal */
83 __be16 tbptr; /* Internal */
84 __be16 tbc; /* Internal */
85 __be32 txtmp; /* Internal */
86 __be32 res; /* Tx temp. */
87 __be16 rpbase; /* Relocation pointer (CPM1 only) */
88 __be16 res1; /* Reserved */
89};
90
50/* SPI Controller mode register definitions */ 91/* SPI Controller mode register definitions */
51#define SPMODE_LOOP (1 << 30) 92#define SPMODE_LOOP (1 << 30)
52#define SPMODE_CI_INACTIVEHIGH (1 << 29) 93#define SPMODE_CI_INACTIVEHIGH (1 << 29)
@@ -75,14 +116,40 @@ struct mpc8xxx_spi_reg {
75#define SPIM_NE 0x00000200 /* Not empty */ 116#define SPIM_NE 0x00000200 /* Not empty */
76#define SPIM_NF 0x00000100 /* Not full */ 117#define SPIM_NF 0x00000100 /* Not full */
77 118
119#define SPIE_TXB 0x00000200 /* Last char is written to tx fifo */
120#define SPIE_RXB 0x00000100 /* Last char is written to rx buf */
121
122/* SPCOM register values */
123#define SPCOM_STR (1 << 23) /* Start transmit */
124
125#define SPI_PRAM_SIZE 0x100
126#define SPI_MRBLR ((unsigned int)PAGE_SIZE)
127
78/* SPI Controller driver's private data. */ 128/* SPI Controller driver's private data. */
79struct mpc8xxx_spi { 129struct mpc8xxx_spi {
130 struct device *dev;
80 struct mpc8xxx_spi_reg __iomem *base; 131 struct mpc8xxx_spi_reg __iomem *base;
81 132
82 /* rx & tx bufs from the spi_transfer */ 133 /* rx & tx bufs from the spi_transfer */
83 const void *tx; 134 const void *tx;
84 void *rx; 135 void *rx;
85 136
137 int subblock;
138 struct spi_pram __iomem *pram;
139 struct cpm_buf_desc __iomem *tx_bd;
140 struct cpm_buf_desc __iomem *rx_bd;
141
142 struct spi_transfer *xfer_in_progress;
143
144 /* dma addresses for CPM transfers */
145 dma_addr_t tx_dma;
146 dma_addr_t rx_dma;
147 bool map_tx_dma;
148 bool map_rx_dma;
149
150 dma_addr_t dma_dummy_tx;
151 dma_addr_t dma_dummy_rx;
152
86 /* functions to deal with different sized buffers */ 153 /* functions to deal with different sized buffers */
87 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *); 154 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
88 u32(*get_tx) (struct mpc8xxx_spi *); 155 u32(*get_tx) (struct mpc8xxx_spi *);
@@ -96,7 +163,7 @@ struct mpc8xxx_spi {
96 u32 rx_shift; /* RX data reg shift when in qe mode */ 163 u32 rx_shift; /* RX data reg shift when in qe mode */
97 u32 tx_shift; /* TX data reg shift when in qe mode */ 164 u32 tx_shift; /* TX data reg shift when in qe mode */
98 165
99 bool qe_mode; 166 unsigned int flags;
100 167
101 struct workqueue_struct *workqueue; 168 struct workqueue_struct *workqueue;
102 struct work_struct work; 169 struct work_struct work;
@@ -107,6 +174,10 @@ struct mpc8xxx_spi {
107 struct completion done; 174 struct completion done;
108}; 175};
109 176
177static void *mpc8xxx_dummy_rx;
178static DEFINE_MUTEX(mpc8xxx_dummy_rx_lock);
179static int mpc8xxx_dummy_rx_refcnt;
180
110struct spi_mpc8xxx_cs { 181struct spi_mpc8xxx_cs {
111 /* functions to deal with different sized buffers */ 182 /* functions to deal with different sized buffers */
112 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *); 183 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
@@ -155,6 +226,42 @@ MPC83XX_SPI_TX_BUF(u8)
155MPC83XX_SPI_TX_BUF(u16) 226MPC83XX_SPI_TX_BUF(u16)
156MPC83XX_SPI_TX_BUF(u32) 227MPC83XX_SPI_TX_BUF(u32)
157 228
229static void mpc8xxx_spi_change_mode(struct spi_device *spi)
230{
231 struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
232 struct spi_mpc8xxx_cs *cs = spi->controller_state;
233 __be32 __iomem *mode = &mspi->base->mode;
234 unsigned long flags;
235
236 if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
237 return;
238
239 /* Turn off IRQs locally to minimize time that SPI is disabled. */
240 local_irq_save(flags);
241
242 /* Turn off SPI unit prior changing mode */
243 mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
244 mpc8xxx_spi_write_reg(mode, cs->hw_mode);
245
246 /* When in CPM mode, we need to reinit tx and rx. */
247 if (mspi->flags & SPI_CPM_MODE) {
248 if (mspi->flags & SPI_QE) {
249 qe_issue_cmd(QE_INIT_TX_RX, mspi->subblock,
250 QE_CR_PROTOCOL_UNSPECIFIED, 0);
251 } else {
252 cpm_command(CPM_SPI_CMD, CPM_CR_INIT_TRX);
253 if (mspi->flags & SPI_CPM1) {
254 out_be16(&mspi->pram->rbptr,
255 in_be16(&mspi->pram->rbase));
256 out_be16(&mspi->pram->tbptr,
257 in_be16(&mspi->pram->tbase));
258 }
259 }
260 }
261
262 local_irq_restore(flags);
263}
264
158static void mpc8xxx_spi_chipselect(struct spi_device *spi, int value) 265static void mpc8xxx_spi_chipselect(struct spi_device *spi, int value)
159{ 266{
160 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master); 267 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
@@ -168,27 +275,13 @@ static void mpc8xxx_spi_chipselect(struct spi_device *spi, int value)
168 } 275 }
169 276
170 if (value == BITBANG_CS_ACTIVE) { 277 if (value == BITBANG_CS_ACTIVE) {
171 u32 regval = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
172
173 mpc8xxx_spi->rx_shift = cs->rx_shift; 278 mpc8xxx_spi->rx_shift = cs->rx_shift;
174 mpc8xxx_spi->tx_shift = cs->tx_shift; 279 mpc8xxx_spi->tx_shift = cs->tx_shift;
175 mpc8xxx_spi->get_rx = cs->get_rx; 280 mpc8xxx_spi->get_rx = cs->get_rx;
176 mpc8xxx_spi->get_tx = cs->get_tx; 281 mpc8xxx_spi->get_tx = cs->get_tx;
177 282
178 if (cs->hw_mode != regval) { 283 mpc8xxx_spi_change_mode(spi);
179 unsigned long flags; 284
180 __be32 __iomem *mode = &mpc8xxx_spi->base->mode;
181
182 regval = cs->hw_mode;
183 /* Turn off IRQs locally to minimize time that
184 * SPI is disabled
185 */
186 local_irq_save(flags);
187 /* Turn off SPI unit prior changing mode */
188 mpc8xxx_spi_write_reg(mode, regval & ~SPMODE_ENABLE);
189 mpc8xxx_spi_write_reg(mode, regval);
190 local_irq_restore(flags);
191 }
192 if (pdata->cs_control) 285 if (pdata->cs_control)
193 pdata->cs_control(spi, pol); 286 pdata->cs_control(spi, pol);
194 } 287 }
@@ -198,7 +291,6 @@ static
198int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t) 291int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
199{ 292{
200 struct mpc8xxx_spi *mpc8xxx_spi; 293 struct mpc8xxx_spi *mpc8xxx_spi;
201 u32 regval;
202 u8 bits_per_word, pm; 294 u8 bits_per_word, pm;
203 u32 hz; 295 u32 hz;
204 struct spi_mpc8xxx_cs *cs = spi->controller_state; 296 struct spi_mpc8xxx_cs *cs = spi->controller_state;
@@ -230,14 +322,14 @@ int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
230 if (bits_per_word <= 8) { 322 if (bits_per_word <= 8) {
231 cs->get_rx = mpc8xxx_spi_rx_buf_u8; 323 cs->get_rx = mpc8xxx_spi_rx_buf_u8;
232 cs->get_tx = mpc8xxx_spi_tx_buf_u8; 324 cs->get_tx = mpc8xxx_spi_tx_buf_u8;
233 if (mpc8xxx_spi->qe_mode) { 325 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
234 cs->rx_shift = 16; 326 cs->rx_shift = 16;
235 cs->tx_shift = 24; 327 cs->tx_shift = 24;
236 } 328 }
237 } else if (bits_per_word <= 16) { 329 } else if (bits_per_word <= 16) {
238 cs->get_rx = mpc8xxx_spi_rx_buf_u16; 330 cs->get_rx = mpc8xxx_spi_rx_buf_u16;
239 cs->get_tx = mpc8xxx_spi_tx_buf_u16; 331 cs->get_tx = mpc8xxx_spi_tx_buf_u16;
240 if (mpc8xxx_spi->qe_mode) { 332 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
241 cs->rx_shift = 16; 333 cs->rx_shift = 16;
242 cs->tx_shift = 16; 334 cs->tx_shift = 16;
243 } 335 }
@@ -247,7 +339,8 @@ int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
247 } else 339 } else
248 return -EINVAL; 340 return -EINVAL;
249 341
250 if (mpc8xxx_spi->qe_mode && spi->mode & SPI_LSB_FIRST) { 342 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE &&
343 spi->mode & SPI_LSB_FIRST) {
251 cs->tx_shift = 0; 344 cs->tx_shift = 0;
252 if (bits_per_word <= 8) 345 if (bits_per_word <= 8)
253 cs->rx_shift = 8; 346 cs->rx_shift = 8;
@@ -273,7 +366,7 @@ int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
273 366
274 if ((mpc8xxx_spi->spibrg / hz) > 64) { 367 if ((mpc8xxx_spi->spibrg / hz) > 64) {
275 cs->hw_mode |= SPMODE_DIV16; 368 cs->hw_mode |= SPMODE_DIV16;
276 pm = mpc8xxx_spi->spibrg / (hz * 64); 369 pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1;
277 370
278 WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. " 371 WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
279 "Will use %d Hz instead.\n", dev_name(&spi->dev), 372 "Will use %d Hz instead.\n", dev_name(&spi->dev),
@@ -281,42 +374,143 @@ int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
281 if (pm > 16) 374 if (pm > 16)
282 pm = 16; 375 pm = 16;
283 } else 376 } else
284 pm = mpc8xxx_spi->spibrg / (hz * 4); 377 pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1;
285 if (pm) 378 if (pm)
286 pm--; 379 pm--;
287 380
288 cs->hw_mode |= SPMODE_PM(pm); 381 cs->hw_mode |= SPMODE_PM(pm);
289 regval = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode); 382
290 if (cs->hw_mode != regval) { 383 mpc8xxx_spi_change_mode(spi);
291 unsigned long flags; 384 return 0;
292 __be32 __iomem *mode = &mpc8xxx_spi->base->mode; 385}
293 386
294 regval = cs->hw_mode; 387static void mpc8xxx_spi_cpm_bufs_start(struct mpc8xxx_spi *mspi)
295 /* Turn off IRQs locally to minimize time 388{
296 * that SPI is disabled 389 struct cpm_buf_desc __iomem *tx_bd = mspi->tx_bd;
297 */ 390 struct cpm_buf_desc __iomem *rx_bd = mspi->rx_bd;
298 local_irq_save(flags); 391 unsigned int xfer_len = min(mspi->count, SPI_MRBLR);
299 /* Turn off SPI unit prior changing mode */ 392 unsigned int xfer_ofs;
300 mpc8xxx_spi_write_reg(mode, regval & ~SPMODE_ENABLE); 393
301 mpc8xxx_spi_write_reg(mode, regval); 394 xfer_ofs = mspi->xfer_in_progress->len - mspi->count;
302 local_irq_restore(flags); 395
396 out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma + xfer_ofs);
397 out_be16(&rx_bd->cbd_datlen, 0);
398 out_be16(&rx_bd->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT | BD_SC_WRAP);
399
400 out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma + xfer_ofs);
401 out_be16(&tx_bd->cbd_datlen, xfer_len);
402 out_be16(&tx_bd->cbd_sc, BD_SC_READY | BD_SC_INTRPT | BD_SC_WRAP |
403 BD_SC_LAST);
404
405 /* start transfer */
406 mpc8xxx_spi_write_reg(&mspi->base->command, SPCOM_STR);
407}
408
409static int mpc8xxx_spi_cpm_bufs(struct mpc8xxx_spi *mspi,
410 struct spi_transfer *t, bool is_dma_mapped)
411{
412 struct device *dev = mspi->dev;
413
414 if (is_dma_mapped) {
415 mspi->map_tx_dma = 0;
416 mspi->map_rx_dma = 0;
417 } else {
418 mspi->map_tx_dma = 1;
419 mspi->map_rx_dma = 1;
420 }
421
422 if (!t->tx_buf) {
423 mspi->tx_dma = mspi->dma_dummy_tx;
424 mspi->map_tx_dma = 0;
425 }
426
427 if (!t->rx_buf) {
428 mspi->rx_dma = mspi->dma_dummy_rx;
429 mspi->map_rx_dma = 0;
303 } 430 }
431
432 if (mspi->map_tx_dma) {
433 void *nonconst_tx = (void *)mspi->tx; /* shut up gcc */
434
435 mspi->tx_dma = dma_map_single(dev, nonconst_tx, t->len,
436 DMA_TO_DEVICE);
437 if (dma_mapping_error(dev, mspi->tx_dma)) {
438 dev_err(dev, "unable to map tx dma\n");
439 return -ENOMEM;
440 }
441 } else {
442 mspi->tx_dma = t->tx_dma;
443 }
444
445 if (mspi->map_rx_dma) {
446 mspi->rx_dma = dma_map_single(dev, mspi->rx, t->len,
447 DMA_FROM_DEVICE);
448 if (dma_mapping_error(dev, mspi->rx_dma)) {
449 dev_err(dev, "unable to map rx dma\n");
450 goto err_rx_dma;
451 }
452 } else {
453 mspi->rx_dma = t->rx_dma;
454 }
455
456 /* enable rx ints */
457 mpc8xxx_spi_write_reg(&mspi->base->mask, SPIE_RXB);
458
459 mspi->xfer_in_progress = t;
460 mspi->count = t->len;
461
462 /* start CPM transfers */
463 mpc8xxx_spi_cpm_bufs_start(mspi);
464
304 return 0; 465 return 0;
466
467err_rx_dma:
468 if (mspi->map_tx_dma)
469 dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
470 return -ENOMEM;
305} 471}
306 472
307static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t) 473static void mpc8xxx_spi_cpm_bufs_complete(struct mpc8xxx_spi *mspi)
308{ 474{
309 struct mpc8xxx_spi *mpc8xxx_spi; 475 struct device *dev = mspi->dev;
310 u32 word, len, bits_per_word; 476 struct spi_transfer *t = mspi->xfer_in_progress;
477
478 if (mspi->map_tx_dma)
479 dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
480 if (mspi->map_tx_dma)
481 dma_unmap_single(dev, mspi->rx_dma, t->len, DMA_FROM_DEVICE);
482 mspi->xfer_in_progress = NULL;
483}
311 484
312 mpc8xxx_spi = spi_master_get_devdata(spi->master); 485static int mpc8xxx_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
486 struct spi_transfer *t, unsigned int len)
487{
488 u32 word;
489
490 mspi->count = len;
491
492 /* enable rx ints */
493 mpc8xxx_spi_write_reg(&mspi->base->mask, SPIM_NE);
494
495 /* transmit word */
496 word = mspi->get_tx(mspi);
497 mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
498
499 return 0;
500}
501
502static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
503 bool is_dma_mapped)
504{
505 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
506 unsigned int len = t->len;
507 u8 bits_per_word;
508 int ret;
313 509
314 mpc8xxx_spi->tx = t->tx_buf;
315 mpc8xxx_spi->rx = t->rx_buf;
316 bits_per_word = spi->bits_per_word; 510 bits_per_word = spi->bits_per_word;
317 if (t->bits_per_word) 511 if (t->bits_per_word)
318 bits_per_word = t->bits_per_word; 512 bits_per_word = t->bits_per_word;
319 len = t->len; 513
320 if (bits_per_word > 8) { 514 if (bits_per_word > 8) {
321 /* invalid length? */ 515 /* invalid length? */
322 if (len & 1) 516 if (len & 1)
@@ -329,22 +523,27 @@ static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
329 return -EINVAL; 523 return -EINVAL;
330 len /= 2; 524 len /= 2;
331 } 525 }
332 mpc8xxx_spi->count = len;
333 526
334 INIT_COMPLETION(mpc8xxx_spi->done); 527 mpc8xxx_spi->tx = t->tx_buf;
528 mpc8xxx_spi->rx = t->rx_buf;
335 529
336 /* enable rx ints */ 530 INIT_COMPLETION(mpc8xxx_spi->done);
337 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, SPIM_NE);
338 531
339 /* transmit word */ 532 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
340 word = mpc8xxx_spi->get_tx(mpc8xxx_spi); 533 ret = mpc8xxx_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
341 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->transmit, word); 534 else
535 ret = mpc8xxx_spi_cpu_bufs(mpc8xxx_spi, t, len);
536 if (ret)
537 return ret;
342 538
343 wait_for_completion(&mpc8xxx_spi->done); 539 wait_for_completion(&mpc8xxx_spi->done);
344 540
345 /* disable rx ints */ 541 /* disable rx ints */
346 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0); 542 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
347 543
544 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
545 mpc8xxx_spi_cpm_bufs_complete(mpc8xxx_spi);
546
348 return mpc8xxx_spi->count; 547 return mpc8xxx_spi->count;
349} 548}
350 549
@@ -375,7 +574,7 @@ static void mpc8xxx_spi_do_one_msg(struct spi_message *m)
375 } 574 }
376 cs_change = t->cs_change; 575 cs_change = t->cs_change;
377 if (t->len) 576 if (t->len)
378 status = mpc8xxx_spi_bufs(spi, t); 577 status = mpc8xxx_spi_bufs(spi, t, m->is_dma_mapped);
379 if (status) { 578 if (status) {
380 status = -EMSGSIZE; 579 status = -EMSGSIZE;
381 break; 580 break;
@@ -464,45 +663,80 @@ static int mpc8xxx_spi_setup(struct spi_device *spi)
464 return 0; 663 return 0;
465} 664}
466 665
467static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data) 666static void mpc8xxx_spi_cpm_irq(struct mpc8xxx_spi *mspi, u32 events)
468{ 667{
469 struct mpc8xxx_spi *mpc8xxx_spi = context_data; 668 u16 len;
470 u32 event;
471 irqreturn_t ret = IRQ_NONE;
472 669
473 /* Get interrupt events(tx/rx) */ 670 dev_dbg(mspi->dev, "%s: bd datlen %d, count %d\n", __func__,
474 event = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->event); 671 in_be16(&mspi->rx_bd->cbd_datlen), mspi->count);
475 672
476 /* We need handle RX first */ 673 len = in_be16(&mspi->rx_bd->cbd_datlen);
477 if (event & SPIE_NE) { 674 if (len > mspi->count) {
478 u32 rx_data = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->receive); 675 WARN_ON(1);
676 len = mspi->count;
677 }
479 678
480 if (mpc8xxx_spi->rx) 679 /* Clear the events */
481 mpc8xxx_spi->get_rx(rx_data, mpc8xxx_spi); 680 mpc8xxx_spi_write_reg(&mspi->base->event, events);
482 681
483 ret = IRQ_HANDLED; 682 mspi->count -= len;
683 if (mspi->count)
684 mpc8xxx_spi_cpm_bufs_start(mspi);
685 else
686 complete(&mspi->done);
687}
688
689static void mpc8xxx_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
690{
691 /* We need handle RX first */
692 if (events & SPIE_NE) {
693 u32 rx_data = mpc8xxx_spi_read_reg(&mspi->base->receive);
694
695 if (mspi->rx)
696 mspi->get_rx(rx_data, mspi);
484 } 697 }
485 698
486 if ((event & SPIE_NF) == 0) 699 if ((events & SPIE_NF) == 0)
487 /* spin until TX is done */ 700 /* spin until TX is done */
488 while (((event = 701 while (((events =
489 mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->event)) & 702 mpc8xxx_spi_read_reg(&mspi->base->event)) &
490 SPIE_NF) == 0) 703 SPIE_NF) == 0)
491 cpu_relax(); 704 cpu_relax();
492 705
493 mpc8xxx_spi->count -= 1; 706 /* Clear the events */
494 if (mpc8xxx_spi->count) { 707 mpc8xxx_spi_write_reg(&mspi->base->event, events);
495 u32 word = mpc8xxx_spi->get_tx(mpc8xxx_spi); 708
496 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->transmit, word); 709 mspi->count -= 1;
710 if (mspi->count) {
711 u32 word = mspi->get_tx(mspi);
712
713 mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
497 } else { 714 } else {
498 complete(&mpc8xxx_spi->done); 715 complete(&mspi->done);
499 } 716 }
717}
500 718
501 /* Clear the events */ 719static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data)
502 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, event); 720{
721 struct mpc8xxx_spi *mspi = context_data;
722 irqreturn_t ret = IRQ_NONE;
723 u32 events;
724
725 /* Get interrupt events(tx/rx) */
726 events = mpc8xxx_spi_read_reg(&mspi->base->event);
727 if (events)
728 ret = IRQ_HANDLED;
729
730 dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
731
732 if (mspi->flags & SPI_CPM_MODE)
733 mpc8xxx_spi_cpm_irq(mspi, events);
734 else
735 mpc8xxx_spi_cpu_irq(mspi, events);
503 736
504 return ret; 737 return ret;
505} 738}
739
506static int mpc8xxx_spi_transfer(struct spi_device *spi, 740static int mpc8xxx_spi_transfer(struct spi_device *spi,
507 struct spi_message *m) 741 struct spi_message *m)
508{ 742{
@@ -526,6 +760,215 @@ static void mpc8xxx_spi_cleanup(struct spi_device *spi)
526 kfree(spi->controller_state); 760 kfree(spi->controller_state);
527} 761}
528 762
763static void *mpc8xxx_spi_alloc_dummy_rx(void)
764{
765 mutex_lock(&mpc8xxx_dummy_rx_lock);
766
767 if (!mpc8xxx_dummy_rx)
768 mpc8xxx_dummy_rx = kmalloc(SPI_MRBLR, GFP_KERNEL);
769 if (mpc8xxx_dummy_rx)
770 mpc8xxx_dummy_rx_refcnt++;
771
772 mutex_unlock(&mpc8xxx_dummy_rx_lock);
773
774 return mpc8xxx_dummy_rx;
775}
776
777static void mpc8xxx_spi_free_dummy_rx(void)
778{
779 mutex_lock(&mpc8xxx_dummy_rx_lock);
780
781 switch (mpc8xxx_dummy_rx_refcnt) {
782 case 0:
783 WARN_ON(1);
784 break;
785 case 1:
786 kfree(mpc8xxx_dummy_rx);
787 mpc8xxx_dummy_rx = NULL;
788 /* fall through */
789 default:
790 mpc8xxx_dummy_rx_refcnt--;
791 break;
792 }
793
794 mutex_unlock(&mpc8xxx_dummy_rx_lock);
795}
796
797static unsigned long mpc8xxx_spi_cpm_get_pram(struct mpc8xxx_spi *mspi)
798{
799 struct device *dev = mspi->dev;
800 struct device_node *np = dev_archdata_get_node(&dev->archdata);
801 const u32 *iprop;
802 int size;
803 unsigned long spi_base_ofs;
804 unsigned long pram_ofs = -ENOMEM;
805
806 /* Can't use of_address_to_resource(), QE muram isn't at 0. */
807 iprop = of_get_property(np, "reg", &size);
808
809 /* QE with a fixed pram location? */
810 if (mspi->flags & SPI_QE && iprop && size == sizeof(*iprop) * 4)
811 return cpm_muram_alloc_fixed(iprop[2], SPI_PRAM_SIZE);
812
813 /* QE but with a dynamic pram location? */
814 if (mspi->flags & SPI_QE) {
815 pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
816 qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, mspi->subblock,
817 QE_CR_PROTOCOL_UNSPECIFIED, pram_ofs);
818 return pram_ofs;
819 }
820
821 /* CPM1 and CPM2 pram must be at a fixed addr. */
822 if (!iprop || size != sizeof(*iprop) * 4)
823 return -ENOMEM;
824
825 spi_base_ofs = cpm_muram_alloc_fixed(iprop[2], 2);
826 if (IS_ERR_VALUE(spi_base_ofs))
827 return -ENOMEM;
828
829 if (mspi->flags & SPI_CPM2) {
830 pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
831 if (!IS_ERR_VALUE(pram_ofs)) {
832 u16 __iomem *spi_base = cpm_muram_addr(spi_base_ofs);
833
834 out_be16(spi_base, pram_ofs);
835 }
836 } else {
837 struct spi_pram __iomem *pram = cpm_muram_addr(spi_base_ofs);
838 u16 rpbase = in_be16(&pram->rpbase);
839
840 /* Microcode relocation patch applied? */
841 if (rpbase)
842 pram_ofs = rpbase;
843 else
844 return spi_base_ofs;
845 }
846
847 cpm_muram_free(spi_base_ofs);
848 return pram_ofs;
849}
850
851static int mpc8xxx_spi_cpm_init(struct mpc8xxx_spi *mspi)
852{
853 struct device *dev = mspi->dev;
854 struct device_node *np = dev_archdata_get_node(&dev->archdata);
855 const u32 *iprop;
856 int size;
857 unsigned long pram_ofs;
858 unsigned long bds_ofs;
859
860 if (!(mspi->flags & SPI_CPM_MODE))
861 return 0;
862
863 if (!mpc8xxx_spi_alloc_dummy_rx())
864 return -ENOMEM;
865
866 if (mspi->flags & SPI_QE) {
867 iprop = of_get_property(np, "cell-index", &size);
868 if (iprop && size == sizeof(*iprop))
869 mspi->subblock = *iprop;
870
871 switch (mspi->subblock) {
872 default:
873 dev_warn(dev, "cell-index unspecified, assuming SPI1");
874 /* fall through */
875 case 0:
876 mspi->subblock = QE_CR_SUBBLOCK_SPI1;
877 break;
878 case 1:
879 mspi->subblock = QE_CR_SUBBLOCK_SPI2;
880 break;
881 }
882 }
883
884 pram_ofs = mpc8xxx_spi_cpm_get_pram(mspi);
885 if (IS_ERR_VALUE(pram_ofs)) {
886 dev_err(dev, "can't allocate spi parameter ram\n");
887 goto err_pram;
888 }
889
890 bds_ofs = cpm_muram_alloc(sizeof(*mspi->tx_bd) +
891 sizeof(*mspi->rx_bd), 8);
892 if (IS_ERR_VALUE(bds_ofs)) {
893 dev_err(dev, "can't allocate bds\n");
894 goto err_bds;
895 }
896
897 mspi->dma_dummy_tx = dma_map_single(dev, empty_zero_page, PAGE_SIZE,
898 DMA_TO_DEVICE);
899 if (dma_mapping_error(dev, mspi->dma_dummy_tx)) {
900 dev_err(dev, "unable to map dummy tx buffer\n");
901 goto err_dummy_tx;
902 }
903
904 mspi->dma_dummy_rx = dma_map_single(dev, mpc8xxx_dummy_rx, SPI_MRBLR,
905 DMA_FROM_DEVICE);
906 if (dma_mapping_error(dev, mspi->dma_dummy_rx)) {
907 dev_err(dev, "unable to map dummy rx buffer\n");
908 goto err_dummy_rx;
909 }
910
911 mspi->pram = cpm_muram_addr(pram_ofs);
912
913 mspi->tx_bd = cpm_muram_addr(bds_ofs);
914 mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd));
915
916 /* Initialize parameter ram. */
917 out_be16(&mspi->pram->tbase, cpm_muram_offset(mspi->tx_bd));
918 out_be16(&mspi->pram->rbase, cpm_muram_offset(mspi->rx_bd));
919 out_8(&mspi->pram->tfcr, CPMFCR_EB | CPMFCR_GBL);
920 out_8(&mspi->pram->rfcr, CPMFCR_EB | CPMFCR_GBL);
921 out_be16(&mspi->pram->mrblr, SPI_MRBLR);
922 out_be32(&mspi->pram->rstate, 0);
923 out_be32(&mspi->pram->rdp, 0);
924 out_be16(&mspi->pram->rbptr, 0);
925 out_be16(&mspi->pram->rbc, 0);
926 out_be32(&mspi->pram->rxtmp, 0);
927 out_be32(&mspi->pram->tstate, 0);
928 out_be32(&mspi->pram->tdp, 0);
929 out_be16(&mspi->pram->tbptr, 0);
930 out_be16(&mspi->pram->tbc, 0);
931 out_be32(&mspi->pram->txtmp, 0);
932
933 return 0;
934
935err_dummy_rx:
936 dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
937err_dummy_tx:
938 cpm_muram_free(bds_ofs);
939err_bds:
940 cpm_muram_free(pram_ofs);
941err_pram:
942 mpc8xxx_spi_free_dummy_rx();
943 return -ENOMEM;
944}
945
946static void mpc8xxx_spi_cpm_free(struct mpc8xxx_spi *mspi)
947{
948 struct device *dev = mspi->dev;
949
950 dma_unmap_single(dev, mspi->dma_dummy_rx, SPI_MRBLR, DMA_FROM_DEVICE);
951 dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
952 cpm_muram_free(cpm_muram_offset(mspi->tx_bd));
953 cpm_muram_free(cpm_muram_offset(mspi->pram));
954 mpc8xxx_spi_free_dummy_rx();
955}
956
957static const char *mpc8xxx_spi_strmode(unsigned int flags)
958{
959 if (flags & SPI_QE_CPU_MODE) {
960 return "QE CPU";
961 } else if (flags & SPI_CPM_MODE) {
962 if (flags & SPI_QE)
963 return "QE";
964 else if (flags & SPI_CPM2)
965 return "CPM2";
966 else
967 return "CPM1";
968 }
969 return "CPU";
970}
971
529static struct spi_master * __devinit 972static struct spi_master * __devinit
530mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq) 973mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
531{ 974{
@@ -552,24 +995,29 @@ mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
552 master->cleanup = mpc8xxx_spi_cleanup; 995 master->cleanup = mpc8xxx_spi_cleanup;
553 996
554 mpc8xxx_spi = spi_master_get_devdata(master); 997 mpc8xxx_spi = spi_master_get_devdata(master);
555 mpc8xxx_spi->qe_mode = pdata->qe_mode; 998 mpc8xxx_spi->dev = dev;
556 mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8; 999 mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
557 mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8; 1000 mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
1001 mpc8xxx_spi->flags = pdata->flags;
558 mpc8xxx_spi->spibrg = pdata->sysclk; 1002 mpc8xxx_spi->spibrg = pdata->sysclk;
559 1003
1004 ret = mpc8xxx_spi_cpm_init(mpc8xxx_spi);
1005 if (ret)
1006 goto err_cpm_init;
1007
560 mpc8xxx_spi->rx_shift = 0; 1008 mpc8xxx_spi->rx_shift = 0;
561 mpc8xxx_spi->tx_shift = 0; 1009 mpc8xxx_spi->tx_shift = 0;
562 if (mpc8xxx_spi->qe_mode) { 1010 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
563 mpc8xxx_spi->rx_shift = 16; 1011 mpc8xxx_spi->rx_shift = 16;
564 mpc8xxx_spi->tx_shift = 24; 1012 mpc8xxx_spi->tx_shift = 24;
565 } 1013 }
566 1014
567 init_completion(&mpc8xxx_spi->done); 1015 init_completion(&mpc8xxx_spi->done);
568 1016
569 mpc8xxx_spi->base = ioremap(mem->start, mem->end - mem->start + 1); 1017 mpc8xxx_spi->base = ioremap(mem->start, resource_size(mem));
570 if (mpc8xxx_spi->base == NULL) { 1018 if (mpc8xxx_spi->base == NULL) {
571 ret = -ENOMEM; 1019 ret = -ENOMEM;
572 goto put_master; 1020 goto err_ioremap;
573 } 1021 }
574 1022
575 mpc8xxx_spi->irq = irq; 1023 mpc8xxx_spi->irq = irq;
@@ -592,7 +1040,7 @@ mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
592 1040
593 /* Enable SPI interface */ 1041 /* Enable SPI interface */
594 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE; 1042 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
595 if (pdata->qe_mode) 1043 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
596 regval |= SPMODE_OP; 1044 regval |= SPMODE_OP;
597 1045
598 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, regval); 1046 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, regval);
@@ -612,9 +1060,8 @@ mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
612 if (ret < 0) 1060 if (ret < 0)
613 goto unreg_master; 1061 goto unreg_master;
614 1062
615 printk(KERN_INFO 1063 dev_info(dev, "at 0x%p (irq = %d), %s mode\n", mpc8xxx_spi->base,
616 "%s: MPC8xxx SPI Controller driver at 0x%p (irq = %d)\n", 1064 mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
617 dev_name(dev), mpc8xxx_spi->base, mpc8xxx_spi->irq);
618 1065
619 return master; 1066 return master;
620 1067
@@ -624,7 +1071,9 @@ free_irq:
624 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi); 1071 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
625unmap_io: 1072unmap_io:
626 iounmap(mpc8xxx_spi->base); 1073 iounmap(mpc8xxx_spi->base);
627put_master: 1074err_ioremap:
1075 mpc8xxx_spi_cpm_free(mpc8xxx_spi);
1076err_cpm_init:
628 spi_master_put(master); 1077 spi_master_put(master);
629err: 1078err:
630 return ERR_PTR(ret); 1079 return ERR_PTR(ret);
@@ -644,6 +1093,7 @@ static int __devexit mpc8xxx_spi_remove(struct device *dev)
644 1093
645 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi); 1094 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
646 iounmap(mpc8xxx_spi->base); 1095 iounmap(mpc8xxx_spi->base);
1096 mpc8xxx_spi_cpm_free(mpc8xxx_spi);
647 1097
648 return 0; 1098 return 0;
649} 1099}
@@ -709,6 +1159,7 @@ static int of_mpc8xxx_spi_get_chipselects(struct device *dev)
709 gpio = of_get_gpio_flags(np, i, &flags); 1159 gpio = of_get_gpio_flags(np, i, &flags);
710 if (!gpio_is_valid(gpio)) { 1160 if (!gpio_is_valid(gpio)) {
711 dev_err(dev, "invalid gpio #%d: %d\n", i, gpio); 1161 dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
1162 ret = gpio;
712 goto err_loop; 1163 goto err_loop;
713 } 1164 }
714 1165
@@ -804,7 +1255,13 @@ static int __devinit of_mpc8xxx_spi_probe(struct of_device *ofdev,
804 1255
805 prop = of_get_property(np, "mode", NULL); 1256 prop = of_get_property(np, "mode", NULL);
806 if (prop && !strcmp(prop, "cpu-qe")) 1257 if (prop && !strcmp(prop, "cpu-qe"))
807 pdata->qe_mode = 1; 1258 pdata->flags = SPI_QE_CPU_MODE;
1259 else if (prop && !strcmp(prop, "qe"))
1260 pdata->flags = SPI_CPM_MODE | SPI_QE;
1261 else if (of_device_is_compatible(np, "fsl,cpm2-spi"))
1262 pdata->flags = SPI_CPM_MODE | SPI_CPM2;
1263 else if (of_device_is_compatible(np, "fsl,cpm1-spi"))
1264 pdata->flags = SPI_CPM_MODE | SPI_CPM1;
808 1265
809 ret = of_mpc8xxx_spi_get_chipselects(dev); 1266 ret = of_mpc8xxx_spi_get_chipselects(dev);
810 if (ret) 1267 if (ret)
@@ -872,7 +1329,7 @@ static struct of_platform_driver of_mpc8xxx_spi_driver = {
872static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev) 1329static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev)
873{ 1330{
874 struct resource *mem; 1331 struct resource *mem;
875 unsigned int irq; 1332 int irq;
876 struct spi_master *master; 1333 struct spi_master *master;
877 1334
878 if (!pdev->dev.platform_data) 1335 if (!pdev->dev.platform_data)
@@ -883,7 +1340,7 @@ static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev)
883 return -EINVAL; 1340 return -EINVAL;
884 1341
885 irq = platform_get_irq(pdev, 0); 1342 irq = platform_get_irq(pdev, 0);
886 if (!irq) 1343 if (irq <= 0)
887 return -EINVAL; 1344 return -EINVAL;
888 1345
889 master = mpc8xxx_spi_probe(&pdev->dev, mem, irq); 1346 master = mpc8xxx_spi_probe(&pdev->dev, mem, irq);
@@ -900,7 +1357,7 @@ static int __devexit plat_mpc8xxx_spi_remove(struct platform_device *pdev)
900MODULE_ALIAS("platform:mpc8xxx_spi"); 1357MODULE_ALIAS("platform:mpc8xxx_spi");
901static struct platform_driver mpc8xxx_spi_driver = { 1358static struct platform_driver mpc8xxx_spi_driver = {
902 .probe = plat_mpc8xxx_spi_probe, 1359 .probe = plat_mpc8xxx_spi_probe,
903 .remove = __exit_p(plat_mpc8xxx_spi_remove), 1360 .remove = __devexit_p(plat_mpc8xxx_spi_remove),
904 .driver = { 1361 .driver = {
905 .name = "mpc8xxx_spi", 1362 .name = "mpc8xxx_spi",
906 .owner = THIS_MODULE, 1363 .owner = THIS_MODULE,