aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/spi
diff options
context:
space:
mode:
authorAnton Vorontsov <avorontsov@ru.mvista.com>2009-10-12 12:49:27 -0400
committerKumar Gala <galak@kernel.crashing.org>2009-11-11 22:43:26 -0500
commit4c1fba442960cfa2fd6333b9fec7d5b85c5fa29f (patch)
treebd0e8eacfdfc81032d81080b14c6c5283f2483b9 /drivers/spi
parent87ec0e98cfdd8b68da6a7f9e70142ffc0e404fbb (diff)
spi_mpc8xxx: Add support for QE DMA mode and CPM1/CPM2 chips
This patch adds QE buffer descriptors mode support for the spi_mpc8xxx driver, and as a side effect we now support CPM1 and CPM2 SPI controllers. That means that today we support almost all MPC SPI controllers: - MPC834x-style controllers (support PIO mode only); - CPM1 and CPM2 controllers (support DMA mode only); - QE SPI controllers in CPU mode (PIO mode with shift quirks); - QE SPI controllers in buffer descriptors (DMA) mode; The only controller we don't currently support is a newer eSPI (with a dedicated chip selects and a bit different registers map). Signed-off-by: Anton Vorontsov <avorontsov@ru.mvista.com> Acked-by: David Brownell <dbrownell@users.sourceforge.net> Signed-off-by: Kumar Gala <galak@kernel.crashing.org>
Diffstat (limited to 'drivers/spi')
-rw-r--r--drivers/spi/Kconfig3
-rw-r--r--drivers/spi/spi_mpc8xxx.c540
2 files changed, 500 insertions, 43 deletions
diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index 4b6f7cba3b3d..94058c62620a 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -147,9 +147,6 @@ config SPI_MPC8xxx
147 This enables using the Freescale MPC8xxx SPI controllers in master 147 This enables using the Freescale MPC8xxx SPI controllers in master
148 mode. 148 mode.
149 149
150 This driver uses a simple set of shift registers for data (opposed
151 to the CPM based descriptor model).
152
153config SPI_OMAP_UWIRE 150config SPI_OMAP_UWIRE
154 tristate "OMAP1 MicroWire" 151 tristate "OMAP1 MicroWire"
155 depends on ARCH_OMAP1 152 depends on ARCH_OMAP1
diff --git a/drivers/spi/spi_mpc8xxx.c b/drivers/spi/spi_mpc8xxx.c
index 80374dfa9708..394b6581e17f 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,6 +31,9 @@
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>
@@ -34,8 +41,19 @@
34#include <linux/of_spi.h> 41#include <linux/of_spi.h>
35 42
36#include <sysdev/fsl_soc.h> 43#include <sysdev/fsl_soc.h>
44#include <asm/cpm.h>
45#include <asm/qe.h>
37#include <asm/irq.h> 46#include <asm/irq.h>
38 47
48/* CPM1 and CPM2 are mutually exclusive. */
49#ifdef CONFIG_CPM1
50#include <asm/cpm1.h>
51#define CPM_SPI_CMD mk_cr_cmd(CPM_CR_CH_SPI, 0)
52#else
53#include <asm/cpm2.h>
54#define CPM_SPI_CMD mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK, 0, 0)
55#endif
56
39/* SPI Controller registers */ 57/* SPI Controller registers */
40struct mpc8xxx_spi_reg { 58struct mpc8xxx_spi_reg {
41 u8 res1[0x20]; 59 u8 res1[0x20];
@@ -47,6 +65,28 @@ struct mpc8xxx_spi_reg {
47 __be32 receive; 65 __be32 receive;
48}; 66};
49 67
68/* SPI Parameter RAM */
69struct spi_pram {
70 __be16 rbase; /* Rx Buffer descriptor base address */
71 __be16 tbase; /* Tx Buffer descriptor base address */
72 u8 rfcr; /* Rx function code */
73 u8 tfcr; /* Tx function code */
74 __be16 mrblr; /* Max receive buffer length */
75 __be32 rstate; /* Internal */
76 __be32 rdp; /* Internal */
77 __be16 rbptr; /* Internal */
78 __be16 rbc; /* Internal */
79 __be32 rxtmp; /* Internal */
80 __be32 tstate; /* Internal */
81 __be32 tdp; /* Internal */
82 __be16 tbptr; /* Internal */
83 __be16 tbc; /* Internal */
84 __be32 txtmp; /* Internal */
85 __be32 res; /* Tx temp. */
86 __be16 rpbase; /* Relocation pointer (CPM1 only) */
87 __be16 res1; /* Reserved */
88};
89
50/* SPI Controller mode register definitions */ 90/* SPI Controller mode register definitions */
51#define SPMODE_LOOP (1 << 30) 91#define SPMODE_LOOP (1 << 30)
52#define SPMODE_CI_INACTIVEHIGH (1 << 29) 92#define SPMODE_CI_INACTIVEHIGH (1 << 29)
@@ -75,14 +115,40 @@ struct mpc8xxx_spi_reg {
75#define SPIM_NE 0x00000200 /* Not empty */ 115#define SPIM_NE 0x00000200 /* Not empty */
76#define SPIM_NF 0x00000100 /* Not full */ 116#define SPIM_NF 0x00000100 /* Not full */
77 117
118#define SPIE_TXB 0x00000200 /* Last char is written to tx fifo */
119#define SPIE_RXB 0x00000100 /* Last char is written to rx buf */
120
121/* SPCOM register values */
122#define SPCOM_STR (1 << 23) /* Start transmit */
123
124#define SPI_PRAM_SIZE 0x100
125#define SPI_MRBLR ((unsigned int)PAGE_SIZE)
126
78/* SPI Controller driver's private data. */ 127/* SPI Controller driver's private data. */
79struct mpc8xxx_spi { 128struct mpc8xxx_spi {
129 struct device *dev;
80 struct mpc8xxx_spi_reg __iomem *base; 130 struct mpc8xxx_spi_reg __iomem *base;
81 131
82 /* rx & tx bufs from the spi_transfer */ 132 /* rx & tx bufs from the spi_transfer */
83 const void *tx; 133 const void *tx;
84 void *rx; 134 void *rx;
85 135
136 int subblock;
137 struct spi_pram __iomem *pram;
138 struct cpm_buf_desc __iomem *tx_bd;
139 struct cpm_buf_desc __iomem *rx_bd;
140
141 struct spi_transfer *xfer_in_progress;
142
143 /* dma addresses for CPM transfers */
144 dma_addr_t tx_dma;
145 dma_addr_t rx_dma;
146 bool map_tx_dma;
147 bool map_rx_dma;
148
149 dma_addr_t dma_dummy_tx;
150 dma_addr_t dma_dummy_rx;
151
86 /* functions to deal with different sized buffers */ 152 /* functions to deal with different sized buffers */
87 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *); 153 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
88 u32(*get_tx) (struct mpc8xxx_spi *); 154 u32(*get_tx) (struct mpc8xxx_spi *);
@@ -98,6 +164,10 @@ struct mpc8xxx_spi {
98 164
99 unsigned int flags; 165 unsigned int flags;
100#define SPI_QE_CPU_MODE (1 << 0) /* QE CPU ("PIO") mode */ 166#define SPI_QE_CPU_MODE (1 << 0) /* QE CPU ("PIO") mode */
167#define SPI_CPM_MODE (1 << 1) /* CPM/QE ("DMA") mode */
168#define SPI_CPM1 (1 << 2) /* SPI unit is in CPM1 block */
169#define SPI_CPM2 (1 << 3) /* SPI unit is in CPM2 block */
170#define SPI_QE (1 << 4) /* SPI unit is in QE block */
101 171
102 struct workqueue_struct *workqueue; 172 struct workqueue_struct *workqueue;
103 struct work_struct work; 173 struct work_struct work;
@@ -108,6 +178,10 @@ struct mpc8xxx_spi {
108 struct completion done; 178 struct completion done;
109}; 179};
110 180
181static void *mpc8xxx_dummy_rx;
182static DEFINE_MUTEX(mpc8xxx_dummy_rx_lock);
183static int mpc8xxx_dummy_rx_refcnt;
184
111struct spi_mpc8xxx_cs { 185struct spi_mpc8xxx_cs {
112 /* functions to deal with different sized buffers */ 186 /* functions to deal with different sized buffers */
113 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *); 187 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
@@ -173,6 +247,22 @@ static void mpc8xxx_spi_change_mode(struct spi_device *spi)
173 mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE); 247 mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
174 mpc8xxx_spi_write_reg(mode, cs->hw_mode); 248 mpc8xxx_spi_write_reg(mode, cs->hw_mode);
175 249
250 /* When in CPM mode, we need to reinit tx and rx. */
251 if (mspi->flags & SPI_CPM_MODE) {
252 if (mspi->flags & SPI_QE) {
253 qe_issue_cmd(QE_INIT_TX_RX, mspi->subblock,
254 QE_CR_PROTOCOL_UNSPECIFIED, 0);
255 } else {
256 cpm_command(CPM_SPI_CMD, CPM_CR_INIT_TRX);
257 if (mspi->flags & SPI_CPM1) {
258 out_be16(&mspi->pram->rbptr,
259 in_be16(&mspi->pram->rbase));
260 out_be16(&mspi->pram->tbptr,
261 in_be16(&mspi->pram->tbase));
262 }
263 }
264 }
265
176 local_irq_restore(flags); 266 local_irq_restore(flags);
177} 267}
178 268
@@ -298,19 +388,133 @@ int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
298 return 0; 388 return 0;
299} 389}
300 390
301static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t) 391static void mpc8xxx_spi_cpm_bufs_start(struct mpc8xxx_spi *mspi)
302{ 392{
303 struct mpc8xxx_spi *mpc8xxx_spi; 393 struct cpm_buf_desc __iomem *tx_bd = mspi->tx_bd;
304 u32 word, len, bits_per_word; 394 struct cpm_buf_desc __iomem *rx_bd = mspi->rx_bd;
395 unsigned int xfer_len = min(mspi->count, SPI_MRBLR);
396 unsigned int xfer_ofs;
305 397
306 mpc8xxx_spi = spi_master_get_devdata(spi->master); 398 xfer_ofs = mspi->xfer_in_progress->len - mspi->count;
399
400 out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma + xfer_ofs);
401 out_be16(&rx_bd->cbd_datlen, 0);
402 out_be16(&rx_bd->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT | BD_SC_WRAP);
403
404 out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma + xfer_ofs);
405 out_be16(&tx_bd->cbd_datlen, xfer_len);
406 out_be16(&tx_bd->cbd_sc, BD_SC_READY | BD_SC_INTRPT | BD_SC_WRAP |
407 BD_SC_LAST);
408
409 /* start transfer */
410 mpc8xxx_spi_write_reg(&mspi->base->command, SPCOM_STR);
411}
412
413static int mpc8xxx_spi_cpm_bufs(struct mpc8xxx_spi *mspi,
414 struct spi_transfer *t, bool is_dma_mapped)
415{
416 struct device *dev = mspi->dev;
417
418 if (is_dma_mapped) {
419 mspi->map_tx_dma = 0;
420 mspi->map_rx_dma = 0;
421 } else {
422 mspi->map_tx_dma = 1;
423 mspi->map_rx_dma = 1;
424 }
425
426 if (!t->tx_buf) {
427 mspi->tx_dma = mspi->dma_dummy_tx;
428 mspi->map_tx_dma = 0;
429 }
430
431 if (!t->rx_buf) {
432 mspi->rx_dma = mspi->dma_dummy_rx;
433 mspi->map_rx_dma = 0;
434 }
435
436 if (mspi->map_tx_dma) {
437 void *nonconst_tx = (void *)mspi->tx; /* shut up gcc */
438
439 mspi->tx_dma = dma_map_single(dev, nonconst_tx, t->len,
440 DMA_TO_DEVICE);
441 if (dma_mapping_error(dev, mspi->tx_dma)) {
442 dev_err(dev, "unable to map tx dma\n");
443 return -ENOMEM;
444 }
445 } else {
446 mspi->tx_dma = t->tx_dma;
447 }
448
449 if (mspi->map_rx_dma) {
450 mspi->rx_dma = dma_map_single(dev, mspi->rx, t->len,
451 DMA_FROM_DEVICE);
452 if (dma_mapping_error(dev, mspi->rx_dma)) {
453 dev_err(dev, "unable to map rx dma\n");
454 goto err_rx_dma;
455 }
456 } else {
457 mspi->rx_dma = t->rx_dma;
458 }
459
460 /* enable rx ints */
461 mpc8xxx_spi_write_reg(&mspi->base->mask, SPIE_RXB);
462
463 mspi->xfer_in_progress = t;
464 mspi->count = t->len;
465
466 /* start CPM transfers */
467 mpc8xxx_spi_cpm_bufs_start(mspi);
468
469 return 0;
470
471err_rx_dma:
472 if (mspi->map_tx_dma)
473 dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
474 return -ENOMEM;
475}
476
477static void mpc8xxx_spi_cpm_bufs_complete(struct mpc8xxx_spi *mspi)
478{
479 struct device *dev = mspi->dev;
480 struct spi_transfer *t = mspi->xfer_in_progress;
481
482 if (mspi->map_tx_dma)
483 dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
484 if (mspi->map_tx_dma)
485 dma_unmap_single(dev, mspi->rx_dma, t->len, DMA_FROM_DEVICE);
486 mspi->xfer_in_progress = NULL;
487}
488
489static int mpc8xxx_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
490 struct spi_transfer *t, unsigned int len)
491{
492 u32 word;
493
494 mspi->count = len;
495
496 /* enable rx ints */
497 mpc8xxx_spi_write_reg(&mspi->base->mask, SPIM_NE);
498
499 /* transmit word */
500 word = mspi->get_tx(mspi);
501 mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
502
503 return 0;
504}
505
506static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
507 bool is_dma_mapped)
508{
509 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
510 unsigned int len = t->len;
511 u8 bits_per_word;
512 int ret;
307 513
308 mpc8xxx_spi->tx = t->tx_buf;
309 mpc8xxx_spi->rx = t->rx_buf;
310 bits_per_word = spi->bits_per_word; 514 bits_per_word = spi->bits_per_word;
311 if (t->bits_per_word) 515 if (t->bits_per_word)
312 bits_per_word = t->bits_per_word; 516 bits_per_word = t->bits_per_word;
313 len = t->len; 517
314 if (bits_per_word > 8) { 518 if (bits_per_word > 8) {
315 /* invalid length? */ 519 /* invalid length? */
316 if (len & 1) 520 if (len & 1)
@@ -323,22 +527,27 @@ static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t)
323 return -EINVAL; 527 return -EINVAL;
324 len /= 2; 528 len /= 2;
325 } 529 }
326 mpc8xxx_spi->count = len;
327 530
328 INIT_COMPLETION(mpc8xxx_spi->done); 531 mpc8xxx_spi->tx = t->tx_buf;
532 mpc8xxx_spi->rx = t->rx_buf;
329 533
330 /* enable rx ints */ 534 INIT_COMPLETION(mpc8xxx_spi->done);
331 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, SPIM_NE);
332 535
333 /* transmit word */ 536 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
334 word = mpc8xxx_spi->get_tx(mpc8xxx_spi); 537 ret = mpc8xxx_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
335 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->transmit, word); 538 else
539 ret = mpc8xxx_spi_cpu_bufs(mpc8xxx_spi, t, len);
540 if (ret)
541 return ret;
336 542
337 wait_for_completion(&mpc8xxx_spi->done); 543 wait_for_completion(&mpc8xxx_spi->done);
338 544
339 /* disable rx ints */ 545 /* disable rx ints */
340 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0); 546 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
341 547
548 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
549 mpc8xxx_spi_cpm_bufs_complete(mpc8xxx_spi);
550
342 return mpc8xxx_spi->count; 551 return mpc8xxx_spi->count;
343} 552}
344 553
@@ -369,7 +578,7 @@ static void mpc8xxx_spi_do_one_msg(struct spi_message *m)
369 } 578 }
370 cs_change = t->cs_change; 579 cs_change = t->cs_change;
371 if (t->len) 580 if (t->len)
372 status = mpc8xxx_spi_bufs(spi, t); 581 status = mpc8xxx_spi_bufs(spi, t, m->is_dma_mapped);
373 if (status) { 582 if (status) {
374 status = -EMSGSIZE; 583 status = -EMSGSIZE;
375 break; 584 break;
@@ -458,45 +667,80 @@ static int mpc8xxx_spi_setup(struct spi_device *spi)
458 return 0; 667 return 0;
459} 668}
460 669
461static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data) 670static void mpc8xxx_spi_cpm_irq(struct mpc8xxx_spi *mspi, u32 events)
462{ 671{
463 struct mpc8xxx_spi *mpc8xxx_spi = context_data; 672 u16 len;
464 u32 event;
465 irqreturn_t ret = IRQ_NONE;
466 673
467 /* Get interrupt events(tx/rx) */ 674 dev_dbg(mspi->dev, "%s: bd datlen %d, count %d\n", __func__,
468 event = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->event); 675 in_be16(&mspi->rx_bd->cbd_datlen), mspi->count);
469 676
470 /* We need handle RX first */ 677 len = in_be16(&mspi->rx_bd->cbd_datlen);
471 if (event & SPIE_NE) { 678 if (len > mspi->count) {
472 u32 rx_data = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->receive); 679 WARN_ON(1);
680 len = mspi->count;
681 }
473 682
474 if (mpc8xxx_spi->rx) 683 /* Clear the events */
475 mpc8xxx_spi->get_rx(rx_data, mpc8xxx_spi); 684 mpc8xxx_spi_write_reg(&mspi->base->event, events);
476 685
477 ret = IRQ_HANDLED; 686 mspi->count -= len;
687 if (mspi->count)
688 mpc8xxx_spi_cpm_bufs_start(mspi);
689 else
690 complete(&mspi->done);
691}
692
693static void mpc8xxx_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
694{
695 /* We need handle RX first */
696 if (events & SPIE_NE) {
697 u32 rx_data = mpc8xxx_spi_read_reg(&mspi->base->receive);
698
699 if (mspi->rx)
700 mspi->get_rx(rx_data, mspi);
478 } 701 }
479 702
480 if ((event & SPIE_NF) == 0) 703 if ((events & SPIE_NF) == 0)
481 /* spin until TX is done */ 704 /* spin until TX is done */
482 while (((event = 705 while (((events =
483 mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->event)) & 706 mpc8xxx_spi_read_reg(&mspi->base->event)) &
484 SPIE_NF) == 0) 707 SPIE_NF) == 0)
485 cpu_relax(); 708 cpu_relax();
486 709
487 mpc8xxx_spi->count -= 1; 710 /* Clear the events */
488 if (mpc8xxx_spi->count) { 711 mpc8xxx_spi_write_reg(&mspi->base->event, events);
489 u32 word = mpc8xxx_spi->get_tx(mpc8xxx_spi); 712
490 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->transmit, word); 713 mspi->count -= 1;
714 if (mspi->count) {
715 u32 word = mspi->get_tx(mspi);
716
717 mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
491 } else { 718 } else {
492 complete(&mpc8xxx_spi->done); 719 complete(&mspi->done);
493 } 720 }
721}
494 722
495 /* Clear the events */ 723static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data)
496 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, event); 724{
725 struct mpc8xxx_spi *mspi = context_data;
726 irqreturn_t ret = IRQ_NONE;
727 u32 events;
728
729 /* Get interrupt events(tx/rx) */
730 events = mpc8xxx_spi_read_reg(&mspi->base->event);
731 if (events)
732 ret = IRQ_HANDLED;
733
734 dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
735
736 if (mspi->flags & SPI_CPM_MODE)
737 mpc8xxx_spi_cpm_irq(mspi, events);
738 else
739 mpc8xxx_spi_cpu_irq(mspi, events);
497 740
498 return ret; 741 return ret;
499} 742}
743
500static int mpc8xxx_spi_transfer(struct spi_device *spi, 744static int mpc8xxx_spi_transfer(struct spi_device *spi,
501 struct spi_message *m) 745 struct spi_message *m)
502{ 746{
@@ -520,10 +764,212 @@ static void mpc8xxx_spi_cleanup(struct spi_device *spi)
520 kfree(spi->controller_state); 764 kfree(spi->controller_state);
521} 765}
522 766
767static void *mpc8xxx_spi_alloc_dummy_rx(void)
768{
769 mutex_lock(&mpc8xxx_dummy_rx_lock);
770
771 if (!mpc8xxx_dummy_rx)
772 mpc8xxx_dummy_rx = kmalloc(SPI_MRBLR, GFP_KERNEL);
773 if (mpc8xxx_dummy_rx)
774 mpc8xxx_dummy_rx_refcnt++;
775
776 mutex_unlock(&mpc8xxx_dummy_rx_lock);
777
778 return mpc8xxx_dummy_rx;
779}
780
781static void mpc8xxx_spi_free_dummy_rx(void)
782{
783 mutex_lock(&mpc8xxx_dummy_rx_lock);
784
785 switch (mpc8xxx_dummy_rx_refcnt) {
786 case 0:
787 WARN_ON(1);
788 break;
789 case 1:
790 kfree(mpc8xxx_dummy_rx);
791 mpc8xxx_dummy_rx = NULL;
792 /* fall through */
793 default:
794 mpc8xxx_dummy_rx_refcnt--;
795 break;
796 }
797
798 mutex_unlock(&mpc8xxx_dummy_rx_lock);
799}
800
801static unsigned long mpc8xxx_spi_cpm_get_pram(struct mpc8xxx_spi *mspi)
802{
803 struct device *dev = mspi->dev;
804 struct device_node *np = dev_archdata_get_node(&dev->archdata);
805 const u32 *iprop;
806 int size;
807 unsigned long spi_base_ofs;
808 unsigned long pram_ofs = -ENOMEM;
809
810 /* Can't use of_address_to_resource(), QE muram isn't at 0. */
811 iprop = of_get_property(np, "reg", &size);
812
813 /* QE with a fixed pram location? */
814 if (mspi->flags & SPI_QE && iprop && size == sizeof(*iprop) * 4)
815 return cpm_muram_alloc_fixed(iprop[2], SPI_PRAM_SIZE);
816
817 /* QE but with a dynamic pram location? */
818 if (mspi->flags & SPI_QE) {
819 pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
820 qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, mspi->subblock,
821 QE_CR_PROTOCOL_UNSPECIFIED, pram_ofs);
822 return pram_ofs;
823 }
824
825 /* CPM1 and CPM2 pram must be at a fixed addr. */
826 if (!iprop || size != sizeof(*iprop) * 4)
827 return -ENOMEM;
828
829 spi_base_ofs = cpm_muram_alloc_fixed(iprop[2], 2);
830 if (IS_ERR_VALUE(spi_base_ofs))
831 return -ENOMEM;
832
833 if (mspi->flags & SPI_CPM2) {
834 pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
835 if (!IS_ERR_VALUE(pram_ofs)) {
836 u16 __iomem *spi_base = cpm_muram_addr(spi_base_ofs);
837
838 out_be16(spi_base, pram_ofs);
839 }
840 } else {
841 struct spi_pram __iomem *pram = cpm_muram_addr(spi_base_ofs);
842 u16 rpbase = in_be16(&pram->rpbase);
843
844 /* Microcode relocation patch applied? */
845 if (rpbase)
846 pram_ofs = rpbase;
847 else
848 return spi_base_ofs;
849 }
850
851 cpm_muram_free(spi_base_ofs);
852 return pram_ofs;
853}
854
855static int mpc8xxx_spi_cpm_init(struct mpc8xxx_spi *mspi)
856{
857 struct device *dev = mspi->dev;
858 struct device_node *np = dev_archdata_get_node(&dev->archdata);
859 const u32 *iprop;
860 int size;
861 unsigned long pram_ofs;
862 unsigned long bds_ofs;
863
864 if (!(mspi->flags & SPI_CPM_MODE))
865 return 0;
866
867 if (!mpc8xxx_spi_alloc_dummy_rx())
868 return -ENOMEM;
869
870 if (mspi->flags & SPI_QE) {
871 iprop = of_get_property(np, "cell-index", &size);
872 if (iprop && size == sizeof(*iprop))
873 mspi->subblock = *iprop;
874
875 switch (mspi->subblock) {
876 default:
877 dev_warn(dev, "cell-index unspecified, assuming SPI1");
878 /* fall through */
879 case 0:
880 mspi->subblock = QE_CR_SUBBLOCK_SPI1;
881 break;
882 case 1:
883 mspi->subblock = QE_CR_SUBBLOCK_SPI2;
884 break;
885 }
886 }
887
888 pram_ofs = mpc8xxx_spi_cpm_get_pram(mspi);
889 if (IS_ERR_VALUE(pram_ofs)) {
890 dev_err(dev, "can't allocate spi parameter ram\n");
891 goto err_pram;
892 }
893
894 bds_ofs = cpm_muram_alloc(sizeof(*mspi->tx_bd) +
895 sizeof(*mspi->rx_bd), 8);
896 if (IS_ERR_VALUE(bds_ofs)) {
897 dev_err(dev, "can't allocate bds\n");
898 goto err_bds;
899 }
900
901 mspi->dma_dummy_tx = dma_map_single(dev, empty_zero_page, PAGE_SIZE,
902 DMA_TO_DEVICE);
903 if (dma_mapping_error(dev, mspi->dma_dummy_tx)) {
904 dev_err(dev, "unable to map dummy tx buffer\n");
905 goto err_dummy_tx;
906 }
907
908 mspi->dma_dummy_rx = dma_map_single(dev, mpc8xxx_dummy_rx, SPI_MRBLR,
909 DMA_FROM_DEVICE);
910 if (dma_mapping_error(dev, mspi->dma_dummy_rx)) {
911 dev_err(dev, "unable to map dummy rx buffer\n");
912 goto err_dummy_rx;
913 }
914
915 mspi->pram = cpm_muram_addr(pram_ofs);
916
917 mspi->tx_bd = cpm_muram_addr(bds_ofs);
918 mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd));
919
920 /* Initialize parameter ram. */
921 out_be16(&mspi->pram->tbase, cpm_muram_offset(mspi->tx_bd));
922 out_be16(&mspi->pram->rbase, cpm_muram_offset(mspi->rx_bd));
923 out_8(&mspi->pram->tfcr, CPMFCR_EB | CPMFCR_GBL);
924 out_8(&mspi->pram->rfcr, CPMFCR_EB | CPMFCR_GBL);
925 out_be16(&mspi->pram->mrblr, SPI_MRBLR);
926 out_be32(&mspi->pram->rstate, 0);
927 out_be32(&mspi->pram->rdp, 0);
928 out_be16(&mspi->pram->rbptr, 0);
929 out_be16(&mspi->pram->rbc, 0);
930 out_be32(&mspi->pram->rxtmp, 0);
931 out_be32(&mspi->pram->tstate, 0);
932 out_be32(&mspi->pram->tdp, 0);
933 out_be16(&mspi->pram->tbptr, 0);
934 out_be16(&mspi->pram->tbc, 0);
935 out_be32(&mspi->pram->txtmp, 0);
936
937 return 0;
938
939err_dummy_rx:
940 dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
941err_dummy_tx:
942 cpm_muram_free(bds_ofs);
943err_bds:
944 cpm_muram_free(pram_ofs);
945err_pram:
946 mpc8xxx_spi_free_dummy_rx();
947 return -ENOMEM;
948}
949
950static void mpc8xxx_spi_cpm_free(struct mpc8xxx_spi *mspi)
951{
952 struct device *dev = mspi->dev;
953
954 dma_unmap_single(dev, mspi->dma_dummy_rx, SPI_MRBLR, DMA_FROM_DEVICE);
955 dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
956 cpm_muram_free(cpm_muram_offset(mspi->tx_bd));
957 cpm_muram_free(cpm_muram_offset(mspi->pram));
958 mpc8xxx_spi_free_dummy_rx();
959}
960
523static const char *mpc8xxx_spi_strmode(unsigned int flags) 961static const char *mpc8xxx_spi_strmode(unsigned int flags)
524{ 962{
525 if (flags & SPI_QE_CPU_MODE) 963 if (flags & SPI_QE_CPU_MODE) {
526 return "QE CPU"; 964 return "QE CPU";
965 } else if (flags & SPI_CPM_MODE) {
966 if (flags & SPI_QE)
967 return "QE";
968 else if (flags & SPI_CPM2)
969 return "CPM2";
970 else
971 return "CPM1";
972 }
527 return "CPU"; 973 return "CPU";
528} 974}
529 975
@@ -553,11 +999,16 @@ mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
553 master->cleanup = mpc8xxx_spi_cleanup; 999 master->cleanup = mpc8xxx_spi_cleanup;
554 1000
555 mpc8xxx_spi = spi_master_get_devdata(master); 1001 mpc8xxx_spi = spi_master_get_devdata(master);
1002 mpc8xxx_spi->dev = dev;
556 mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8; 1003 mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
557 mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8; 1004 mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
558 mpc8xxx_spi->flags = pdata->flags; 1005 mpc8xxx_spi->flags = pdata->flags;
559 mpc8xxx_spi->spibrg = pdata->sysclk; 1006 mpc8xxx_spi->spibrg = pdata->sysclk;
560 1007
1008 ret = mpc8xxx_spi_cpm_init(mpc8xxx_spi);
1009 if (ret)
1010 goto err_cpm_init;
1011
561 mpc8xxx_spi->rx_shift = 0; 1012 mpc8xxx_spi->rx_shift = 0;
562 mpc8xxx_spi->tx_shift = 0; 1013 mpc8xxx_spi->tx_shift = 0;
563 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) { 1014 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
@@ -570,7 +1021,7 @@ mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
570 mpc8xxx_spi->base = ioremap(mem->start, mem->end - mem->start + 1); 1021 mpc8xxx_spi->base = ioremap(mem->start, mem->end - mem->start + 1);
571 if (mpc8xxx_spi->base == NULL) { 1022 if (mpc8xxx_spi->base == NULL) {
572 ret = -ENOMEM; 1023 ret = -ENOMEM;
573 goto put_master; 1024 goto err_ioremap;
574 } 1025 }
575 1026
576 mpc8xxx_spi->irq = irq; 1027 mpc8xxx_spi->irq = irq;
@@ -624,7 +1075,9 @@ free_irq:
624 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi); 1075 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
625unmap_io: 1076unmap_io:
626 iounmap(mpc8xxx_spi->base); 1077 iounmap(mpc8xxx_spi->base);
627put_master: 1078err_ioremap:
1079 mpc8xxx_spi_cpm_free(mpc8xxx_spi);
1080err_cpm_init:
628 spi_master_put(master); 1081 spi_master_put(master);
629err: 1082err:
630 return ERR_PTR(ret); 1083 return ERR_PTR(ret);
@@ -644,6 +1097,7 @@ static int __devexit mpc8xxx_spi_remove(struct device *dev)
644 1097
645 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi); 1098 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
646 iounmap(mpc8xxx_spi->base); 1099 iounmap(mpc8xxx_spi->base);
1100 mpc8xxx_spi_cpm_free(mpc8xxx_spi);
647 1101
648 return 0; 1102 return 0;
649} 1103}
@@ -806,6 +1260,12 @@ static int __devinit of_mpc8xxx_spi_probe(struct of_device *ofdev,
806 prop = of_get_property(np, "mode", NULL); 1260 prop = of_get_property(np, "mode", NULL);
807 if (prop && !strcmp(prop, "cpu-qe")) 1261 if (prop && !strcmp(prop, "cpu-qe"))
808 pdata->flags = SPI_QE_CPU_MODE; 1262 pdata->flags = SPI_QE_CPU_MODE;
1263 else if (prop && !strcmp(prop, "qe"))
1264 pdata->flags = SPI_CPM_MODE | SPI_QE;
1265 else if (of_device_is_compatible(np, "fsl,cpm2-spi"))
1266 pdata->flags = SPI_CPM_MODE | SPI_CPM2;
1267 else if (of_device_is_compatible(np, "fsl,cpm1-spi"))
1268 pdata->flags = SPI_CPM_MODE | SPI_CPM1;
809 1269
810 ret = of_mpc8xxx_spi_get_chipselects(dev); 1270 ret = of_mpc8xxx_spi_get_chipselects(dev);
811 if (ret) 1271 if (ret)