2 * MPC8xxx SPI controller driver.
4 * Maintainer: Kumar Gala
6 * Copyright (C) 2006 Polycom, Inc.
8 * CPM SPI and QE buffer descriptors mode support:
9 * Copyright (c) 2009 MontaVista Software, Inc.
10 * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version.
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/bug.h>
22 #include <linux/errno.h>
23 #include <linux/err.h>
25 #include <linux/completion.h>
26 #include <linux/interrupt.h>
27 #include <linux/delay.h>
28 #include <linux/irq.h>
29 #include <linux/device.h>
30 #include <linux/spi/spi.h>
31 #include <linux/spi/spi_bitbang.h>
32 #include <linux/platform_device.h>
33 #include <linux/fsl_devices.h>
34 #include <linux/dma-mapping.h>
36 #include <linux/mutex.h>
38 #include <linux/of_platform.h>
39 #include <linux/gpio.h>
40 #include <linux/of_gpio.h>
41 #include <linux/of_spi.h>
43 #include <sysdev/fsl_soc.h>
48 /* CPM1 and CPM2 are mutually exclusive. */
51 #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_CH_SPI, 0)
54 #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK, 0, 0)
57 /* SPI Controller registers */
58 struct mpc8xxx_spi_reg {
68 /* SPI Parameter RAM */
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 */
90 /* SPI Controller mode register definitions */
91 #define SPMODE_LOOP (1 << 30)
92 #define SPMODE_CI_INACTIVEHIGH (1 << 29)
93 #define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
94 #define SPMODE_DIV16 (1 << 27)
95 #define SPMODE_REV (1 << 26)
96 #define SPMODE_MS (1 << 25)
97 #define SPMODE_ENABLE (1 << 24)
98 #define SPMODE_LEN(x) ((x) << 20)
99 #define SPMODE_PM(x) ((x) << 16)
100 #define SPMODE_OP (1 << 14)
101 #define SPMODE_CG(x) ((x) << 7)
104 * Default for SPI Mode:
105 * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
107 #define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
108 SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
110 /* SPIE register values */
111 #define SPIE_NE 0x00000200 /* Not empty */
112 #define SPIE_NF 0x00000100 /* Not full */
114 /* SPIM register values */
115 #define SPIM_NE 0x00000200 /* Not empty */
116 #define SPIM_NF 0x00000100 /* Not full */
118 #define SPIE_TXB 0x00000200 /* Last char is written to tx fifo */
119 #define SPIE_RXB 0x00000100 /* Last char is written to rx buf */
121 /* SPCOM register values */
122 #define SPCOM_STR (1 << 23) /* Start transmit */
124 #define SPI_PRAM_SIZE 0x100
125 #define SPI_MRBLR ((unsigned int)PAGE_SIZE)
127 /* SPI Controller driver's private data. */
130 struct mpc8xxx_spi_reg __iomem *base;
132 /* rx & tx bufs from the spi_transfer */
137 struct spi_pram __iomem *pram;
138 struct cpm_buf_desc __iomem *tx_bd;
139 struct cpm_buf_desc __iomem *rx_bd;
141 struct spi_transfer *xfer_in_progress;
143 /* dma addresses for CPM transfers */
149 dma_addr_t dma_dummy_tx;
150 dma_addr_t dma_dummy_rx;
152 /* functions to deal with different sized buffers */
153 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
154 u32(*get_tx) (struct mpc8xxx_spi *);
159 unsigned nsecs; /* (clock cycle time)/2 */
161 u32 spibrg; /* SPIBRG input clock */
162 u32 rx_shift; /* RX data reg shift when in qe mode */
163 u32 tx_shift; /* TX data reg shift when in qe 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 */
172 struct workqueue_struct *workqueue;
173 struct work_struct work;
175 struct list_head queue;
178 struct completion done;
181 static void *mpc8xxx_dummy_rx;
182 static DEFINE_MUTEX(mpc8xxx_dummy_rx_lock);
183 static int mpc8xxx_dummy_rx_refcnt;
185 struct spi_mpc8xxx_cs {
186 /* functions to deal with different sized buffers */
187 void (*get_rx) (u32 rx_data, struct mpc8xxx_spi *);
188 u32 (*get_tx) (struct mpc8xxx_spi *);
189 u32 rx_shift; /* RX data reg shift when in qe mode */
190 u32 tx_shift; /* TX data reg shift when in qe mode */
191 u32 hw_mode; /* Holds HW mode register settings */
194 static inline void mpc8xxx_spi_write_reg(__be32 __iomem *reg, u32 val)
199 static inline u32 mpc8xxx_spi_read_reg(__be32 __iomem *reg)
204 #define MPC83XX_SPI_RX_BUF(type) \
206 void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
208 type *rx = mpc8xxx_spi->rx; \
209 *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
210 mpc8xxx_spi->rx = rx; \
213 #define MPC83XX_SPI_TX_BUF(type) \
215 u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
218 const type *tx = mpc8xxx_spi->tx; \
221 data = *tx++ << mpc8xxx_spi->tx_shift; \
222 mpc8xxx_spi->tx = tx; \
226 MPC83XX_SPI_RX_BUF(u8)
227 MPC83XX_SPI_RX_BUF(u16)
228 MPC83XX_SPI_RX_BUF(u32)
229 MPC83XX_SPI_TX_BUF(u8)
230 MPC83XX_SPI_TX_BUF(u16)
231 MPC83XX_SPI_TX_BUF(u32)
233 static void mpc8xxx_spi_change_mode(struct spi_device *spi)
235 struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
236 struct spi_mpc8xxx_cs *cs = spi->controller_state;
237 __be32 __iomem *mode = &mspi->base->mode;
240 if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
243 /* Turn off IRQs locally to minimize time that SPI is disabled. */
244 local_irq_save(flags);
246 /* Turn off SPI unit prior changing mode */
247 mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
248 mpc8xxx_spi_write_reg(mode, cs->hw_mode);
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);
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));
266 local_irq_restore(flags);
269 static void mpc8xxx_spi_chipselect(struct spi_device *spi, int value)
271 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
272 struct fsl_spi_platform_data *pdata = spi->dev.parent->platform_data;
273 bool pol = spi->mode & SPI_CS_HIGH;
274 struct spi_mpc8xxx_cs *cs = spi->controller_state;
276 if (value == BITBANG_CS_INACTIVE) {
277 if (pdata->cs_control)
278 pdata->cs_control(spi, !pol);
281 if (value == BITBANG_CS_ACTIVE) {
282 mpc8xxx_spi->rx_shift = cs->rx_shift;
283 mpc8xxx_spi->tx_shift = cs->tx_shift;
284 mpc8xxx_spi->get_rx = cs->get_rx;
285 mpc8xxx_spi->get_tx = cs->get_tx;
287 mpc8xxx_spi_change_mode(spi);
289 if (pdata->cs_control)
290 pdata->cs_control(spi, pol);
295 int mpc8xxx_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
297 struct mpc8xxx_spi *mpc8xxx_spi;
298 u8 bits_per_word, pm;
300 struct spi_mpc8xxx_cs *cs = spi->controller_state;
302 mpc8xxx_spi = spi_master_get_devdata(spi->master);
305 bits_per_word = t->bits_per_word;
312 /* spi_transfer level calls that work per-word */
314 bits_per_word = spi->bits_per_word;
316 /* Make sure its a bit width we support [4..16, 32] */
317 if ((bits_per_word < 4)
318 || ((bits_per_word > 16) && (bits_per_word != 32)))
322 hz = spi->max_speed_hz;
326 if (bits_per_word <= 8) {
327 cs->get_rx = mpc8xxx_spi_rx_buf_u8;
328 cs->get_tx = mpc8xxx_spi_tx_buf_u8;
329 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
333 } else if (bits_per_word <= 16) {
334 cs->get_rx = mpc8xxx_spi_rx_buf_u16;
335 cs->get_tx = mpc8xxx_spi_tx_buf_u16;
336 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
340 } else if (bits_per_word <= 32) {
341 cs->get_rx = mpc8xxx_spi_rx_buf_u32;
342 cs->get_tx = mpc8xxx_spi_tx_buf_u32;
346 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE &&
347 spi->mode & SPI_LSB_FIRST) {
349 if (bits_per_word <= 8)
355 mpc8xxx_spi->rx_shift = cs->rx_shift;
356 mpc8xxx_spi->tx_shift = cs->tx_shift;
357 mpc8xxx_spi->get_rx = cs->get_rx;
358 mpc8xxx_spi->get_tx = cs->get_tx;
360 if (bits_per_word == 32)
363 bits_per_word = bits_per_word - 1;
365 /* mask out bits we are going to set */
366 cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
369 cs->hw_mode |= SPMODE_LEN(bits_per_word);
371 if ((mpc8xxx_spi->spibrg / hz) > 64) {
372 cs->hw_mode |= SPMODE_DIV16;
373 pm = mpc8xxx_spi->spibrg / (hz * 64);
375 WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
376 "Will use %d Hz instead.\n", dev_name(&spi->dev),
377 hz, mpc8xxx_spi->spibrg / 1024);
381 pm = mpc8xxx_spi->spibrg / (hz * 4);
385 cs->hw_mode |= SPMODE_PM(pm);
387 mpc8xxx_spi_change_mode(spi);
391 static void mpc8xxx_spi_cpm_bufs_start(struct mpc8xxx_spi *mspi)
393 struct cpm_buf_desc __iomem *tx_bd = mspi->tx_bd;
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;
398 xfer_ofs = mspi->xfer_in_progress->len - mspi->count;
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);
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 |
410 mpc8xxx_spi_write_reg(&mspi->base->command, SPCOM_STR);
413 static int mpc8xxx_spi_cpm_bufs(struct mpc8xxx_spi *mspi,
414 struct spi_transfer *t, bool is_dma_mapped)
416 struct device *dev = mspi->dev;
419 mspi->map_tx_dma = 0;
420 mspi->map_rx_dma = 0;
422 mspi->map_tx_dma = 1;
423 mspi->map_rx_dma = 1;
427 mspi->tx_dma = mspi->dma_dummy_tx;
428 mspi->map_tx_dma = 0;
432 mspi->rx_dma = mspi->dma_dummy_rx;
433 mspi->map_rx_dma = 0;
436 if (mspi->map_tx_dma) {
437 void *nonconst_tx = (void *)mspi->tx; /* shut up gcc */
439 mspi->tx_dma = dma_map_single(dev, nonconst_tx, t->len,
441 if (dma_mapping_error(dev, mspi->tx_dma)) {
442 dev_err(dev, "unable to map tx dma\n");
446 mspi->tx_dma = t->tx_dma;
449 if (mspi->map_rx_dma) {
450 mspi->rx_dma = dma_map_single(dev, mspi->rx, t->len,
452 if (dma_mapping_error(dev, mspi->rx_dma)) {
453 dev_err(dev, "unable to map rx dma\n");
457 mspi->rx_dma = t->rx_dma;
461 mpc8xxx_spi_write_reg(&mspi->base->mask, SPIE_RXB);
463 mspi->xfer_in_progress = t;
464 mspi->count = t->len;
466 /* start CPM transfers */
467 mpc8xxx_spi_cpm_bufs_start(mspi);
472 if (mspi->map_tx_dma)
473 dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
477 static void mpc8xxx_spi_cpm_bufs_complete(struct mpc8xxx_spi *mspi)
479 struct device *dev = mspi->dev;
480 struct spi_transfer *t = mspi->xfer_in_progress;
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;
489 static int mpc8xxx_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
490 struct spi_transfer *t, unsigned int len)
497 mpc8xxx_spi_write_reg(&mspi->base->mask, SPIM_NE);
500 word = mspi->get_tx(mspi);
501 mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
506 static int mpc8xxx_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
509 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
510 unsigned int len = t->len;
514 bits_per_word = spi->bits_per_word;
515 if (t->bits_per_word)
516 bits_per_word = t->bits_per_word;
518 if (bits_per_word > 8) {
519 /* invalid length? */
524 if (bits_per_word > 16) {
525 /* invalid length? */
531 mpc8xxx_spi->tx = t->tx_buf;
532 mpc8xxx_spi->rx = t->rx_buf;
534 INIT_COMPLETION(mpc8xxx_spi->done);
536 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
537 ret = mpc8xxx_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
539 ret = mpc8xxx_spi_cpu_bufs(mpc8xxx_spi, t, len);
543 wait_for_completion(&mpc8xxx_spi->done);
545 /* disable rx ints */
546 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
548 if (mpc8xxx_spi->flags & SPI_CPM_MODE)
549 mpc8xxx_spi_cpm_bufs_complete(mpc8xxx_spi);
551 return mpc8xxx_spi->count;
554 static void mpc8xxx_spi_do_one_msg(struct spi_message *m)
556 struct spi_device *spi = m->spi;
557 struct spi_transfer *t;
558 unsigned int cs_change;
559 const int nsecs = 50;
564 list_for_each_entry(t, &m->transfers, transfer_list) {
565 if (t->bits_per_word || t->speed_hz) {
566 /* Don't allow changes if CS is active */
570 status = mpc8xxx_spi_setup_transfer(spi, t);
576 mpc8xxx_spi_chipselect(spi, BITBANG_CS_ACTIVE);
579 cs_change = t->cs_change;
581 status = mpc8xxx_spi_bufs(spi, t, m->is_dma_mapped);
586 m->actual_length += t->len;
589 udelay(t->delay_usecs);
593 mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
599 m->complete(m->context);
601 if (status || !cs_change) {
603 mpc8xxx_spi_chipselect(spi, BITBANG_CS_INACTIVE);
606 mpc8xxx_spi_setup_transfer(spi, NULL);
609 static void mpc8xxx_spi_work(struct work_struct *work)
611 struct mpc8xxx_spi *mpc8xxx_spi = container_of(work, struct mpc8xxx_spi,
614 spin_lock_irq(&mpc8xxx_spi->lock);
615 while (!list_empty(&mpc8xxx_spi->queue)) {
616 struct spi_message *m = container_of(mpc8xxx_spi->queue.next,
617 struct spi_message, queue);
619 list_del_init(&m->queue);
620 spin_unlock_irq(&mpc8xxx_spi->lock);
622 mpc8xxx_spi_do_one_msg(m);
624 spin_lock_irq(&mpc8xxx_spi->lock);
626 spin_unlock_irq(&mpc8xxx_spi->lock);
629 static int mpc8xxx_spi_setup(struct spi_device *spi)
631 struct mpc8xxx_spi *mpc8xxx_spi;
634 struct spi_mpc8xxx_cs *cs = spi->controller_state;
636 if (!spi->max_speed_hz)
640 cs = kzalloc(sizeof *cs, GFP_KERNEL);
643 spi->controller_state = cs;
645 mpc8xxx_spi = spi_master_get_devdata(spi->master);
647 hw_mode = cs->hw_mode; /* Save orginal settings */
648 cs->hw_mode = mpc8xxx_spi_read_reg(&mpc8xxx_spi->base->mode);
649 /* mask out bits we are going to set */
650 cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
651 | SPMODE_REV | SPMODE_LOOP);
653 if (spi->mode & SPI_CPHA)
654 cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
655 if (spi->mode & SPI_CPOL)
656 cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
657 if (!(spi->mode & SPI_LSB_FIRST))
658 cs->hw_mode |= SPMODE_REV;
659 if (spi->mode & SPI_LOOP)
660 cs->hw_mode |= SPMODE_LOOP;
662 retval = mpc8xxx_spi_setup_transfer(spi, NULL);
664 cs->hw_mode = hw_mode; /* Restore settings */
670 static void mpc8xxx_spi_cpm_irq(struct mpc8xxx_spi *mspi, u32 events)
674 dev_dbg(mspi->dev, "%s: bd datlen %d, count %d\n", __func__,
675 in_be16(&mspi->rx_bd->cbd_datlen), mspi->count);
677 len = in_be16(&mspi->rx_bd->cbd_datlen);
678 if (len > mspi->count) {
683 /* Clear the events */
684 mpc8xxx_spi_write_reg(&mspi->base->event, events);
688 mpc8xxx_spi_cpm_bufs_start(mspi);
690 complete(&mspi->done);
693 static void mpc8xxx_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
695 /* We need handle RX first */
696 if (events & SPIE_NE) {
697 u32 rx_data = mpc8xxx_spi_read_reg(&mspi->base->receive);
700 mspi->get_rx(rx_data, mspi);
703 if ((events & SPIE_NF) == 0)
704 /* spin until TX is done */
706 mpc8xxx_spi_read_reg(&mspi->base->event)) &
710 /* Clear the events */
711 mpc8xxx_spi_write_reg(&mspi->base->event, events);
715 u32 word = mspi->get_tx(mspi);
717 mpc8xxx_spi_write_reg(&mspi->base->transmit, word);
719 complete(&mspi->done);
723 static irqreturn_t mpc8xxx_spi_irq(s32 irq, void *context_data)
725 struct mpc8xxx_spi *mspi = context_data;
726 irqreturn_t ret = IRQ_NONE;
729 /* Get interrupt events(tx/rx) */
730 events = mpc8xxx_spi_read_reg(&mspi->base->event);
734 dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
736 if (mspi->flags & SPI_CPM_MODE)
737 mpc8xxx_spi_cpm_irq(mspi, events);
739 mpc8xxx_spi_cpu_irq(mspi, events);
744 static int mpc8xxx_spi_transfer(struct spi_device *spi,
745 struct spi_message *m)
747 struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
750 m->actual_length = 0;
751 m->status = -EINPROGRESS;
753 spin_lock_irqsave(&mpc8xxx_spi->lock, flags);
754 list_add_tail(&m->queue, &mpc8xxx_spi->queue);
755 queue_work(mpc8xxx_spi->workqueue, &mpc8xxx_spi->work);
756 spin_unlock_irqrestore(&mpc8xxx_spi->lock, flags);
762 static void mpc8xxx_spi_cleanup(struct spi_device *spi)
764 kfree(spi->controller_state);
767 static void *mpc8xxx_spi_alloc_dummy_rx(void)
769 mutex_lock(&mpc8xxx_dummy_rx_lock);
771 if (!mpc8xxx_dummy_rx)
772 mpc8xxx_dummy_rx = kmalloc(SPI_MRBLR, GFP_KERNEL);
773 if (mpc8xxx_dummy_rx)
774 mpc8xxx_dummy_rx_refcnt++;
776 mutex_unlock(&mpc8xxx_dummy_rx_lock);
778 return mpc8xxx_dummy_rx;
781 static void mpc8xxx_spi_free_dummy_rx(void)
783 mutex_lock(&mpc8xxx_dummy_rx_lock);
785 switch (mpc8xxx_dummy_rx_refcnt) {
790 kfree(mpc8xxx_dummy_rx);
791 mpc8xxx_dummy_rx = NULL;
794 mpc8xxx_dummy_rx_refcnt--;
798 mutex_unlock(&mpc8xxx_dummy_rx_lock);
801 static unsigned long mpc8xxx_spi_cpm_get_pram(struct mpc8xxx_spi *mspi)
803 struct device *dev = mspi->dev;
804 struct device_node *np = dev_archdata_get_node(&dev->archdata);
807 unsigned long spi_base_ofs;
808 unsigned long pram_ofs = -ENOMEM;
810 /* Can't use of_address_to_resource(), QE muram isn't at 0. */
811 iprop = of_get_property(np, "reg", &size);
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);
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);
825 /* CPM1 and CPM2 pram must be at a fixed addr. */
826 if (!iprop || size != sizeof(*iprop) * 4)
829 spi_base_ofs = cpm_muram_alloc_fixed(iprop[2], 2);
830 if (IS_ERR_VALUE(spi_base_ofs))
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);
838 out_be16(spi_base, pram_ofs);
841 struct spi_pram __iomem *pram = cpm_muram_addr(spi_base_ofs);
842 u16 rpbase = in_be16(&pram->rpbase);
844 /* Microcode relocation patch applied? */
851 cpm_muram_free(spi_base_ofs);
855 static int mpc8xxx_spi_cpm_init(struct mpc8xxx_spi *mspi)
857 struct device *dev = mspi->dev;
858 struct device_node *np = dev_archdata_get_node(&dev->archdata);
861 unsigned long pram_ofs;
862 unsigned long bds_ofs;
864 if (!(mspi->flags & SPI_CPM_MODE))
867 if (!mpc8xxx_spi_alloc_dummy_rx())
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;
875 switch (mspi->subblock) {
877 dev_warn(dev, "cell-index unspecified, assuming SPI1");
880 mspi->subblock = QE_CR_SUBBLOCK_SPI1;
883 mspi->subblock = QE_CR_SUBBLOCK_SPI2;
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");
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");
901 mspi->dma_dummy_tx = dma_map_single(dev, empty_zero_page, PAGE_SIZE,
903 if (dma_mapping_error(dev, mspi->dma_dummy_tx)) {
904 dev_err(dev, "unable to map dummy tx buffer\n");
908 mspi->dma_dummy_rx = dma_map_single(dev, mpc8xxx_dummy_rx, SPI_MRBLR,
910 if (dma_mapping_error(dev, mspi->dma_dummy_rx)) {
911 dev_err(dev, "unable to map dummy rx buffer\n");
915 mspi->pram = cpm_muram_addr(pram_ofs);
917 mspi->tx_bd = cpm_muram_addr(bds_ofs);
918 mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd));
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);
940 dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
942 cpm_muram_free(bds_ofs);
944 cpm_muram_free(pram_ofs);
946 mpc8xxx_spi_free_dummy_rx();
950 static void mpc8xxx_spi_cpm_free(struct mpc8xxx_spi *mspi)
952 struct device *dev = mspi->dev;
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();
961 static const char *mpc8xxx_spi_strmode(unsigned int flags)
963 if (flags & SPI_QE_CPU_MODE) {
965 } else if (flags & SPI_CPM_MODE) {
968 else if (flags & SPI_CPM2)
976 static struct spi_master * __devinit
977 mpc8xxx_spi_probe(struct device *dev, struct resource *mem, unsigned int irq)
979 struct fsl_spi_platform_data *pdata = dev->platform_data;
980 struct spi_master *master;
981 struct mpc8xxx_spi *mpc8xxx_spi;
985 master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
986 if (master == NULL) {
991 dev_set_drvdata(dev, master);
993 /* the spi->mode bits understood by this driver: */
994 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH
995 | SPI_LSB_FIRST | SPI_LOOP;
997 master->setup = mpc8xxx_spi_setup;
998 master->transfer = mpc8xxx_spi_transfer;
999 master->cleanup = mpc8xxx_spi_cleanup;
1001 mpc8xxx_spi = spi_master_get_devdata(master);
1002 mpc8xxx_spi->dev = dev;
1003 mpc8xxx_spi->get_rx = mpc8xxx_spi_rx_buf_u8;
1004 mpc8xxx_spi->get_tx = mpc8xxx_spi_tx_buf_u8;
1005 mpc8xxx_spi->flags = pdata->flags;
1006 mpc8xxx_spi->spibrg = pdata->sysclk;
1008 ret = mpc8xxx_spi_cpm_init(mpc8xxx_spi);
1012 mpc8xxx_spi->rx_shift = 0;
1013 mpc8xxx_spi->tx_shift = 0;
1014 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
1015 mpc8xxx_spi->rx_shift = 16;
1016 mpc8xxx_spi->tx_shift = 24;
1019 init_completion(&mpc8xxx_spi->done);
1021 mpc8xxx_spi->base = ioremap(mem->start, mem->end - mem->start + 1);
1022 if (mpc8xxx_spi->base == NULL) {
1027 mpc8xxx_spi->irq = irq;
1029 /* Register for SPI Interrupt */
1030 ret = request_irq(mpc8xxx_spi->irq, mpc8xxx_spi_irq,
1031 0, "mpc8xxx_spi", mpc8xxx_spi);
1036 master->bus_num = pdata->bus_num;
1037 master->num_chipselect = pdata->max_chipselect;
1039 /* SPI controller initializations */
1040 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, 0);
1041 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mask, 0);
1042 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->command, 0);
1043 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->event, 0xffffffff);
1045 /* Enable SPI interface */
1046 regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
1047 if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
1048 regval |= SPMODE_OP;
1050 mpc8xxx_spi_write_reg(&mpc8xxx_spi->base->mode, regval);
1051 spin_lock_init(&mpc8xxx_spi->lock);
1052 init_completion(&mpc8xxx_spi->done);
1053 INIT_WORK(&mpc8xxx_spi->work, mpc8xxx_spi_work);
1054 INIT_LIST_HEAD(&mpc8xxx_spi->queue);
1056 mpc8xxx_spi->workqueue = create_singlethread_workqueue(
1057 dev_name(master->dev.parent));
1058 if (mpc8xxx_spi->workqueue == NULL) {
1063 ret = spi_register_master(master);
1067 dev_info(dev, "at 0x%p (irq = %d), %s mode\n", mpc8xxx_spi->base,
1068 mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
1073 destroy_workqueue(mpc8xxx_spi->workqueue);
1075 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
1077 iounmap(mpc8xxx_spi->base);
1079 mpc8xxx_spi_cpm_free(mpc8xxx_spi);
1081 spi_master_put(master);
1083 return ERR_PTR(ret);
1086 static int __devexit mpc8xxx_spi_remove(struct device *dev)
1088 struct mpc8xxx_spi *mpc8xxx_spi;
1089 struct spi_master *master;
1091 master = dev_get_drvdata(dev);
1092 mpc8xxx_spi = spi_master_get_devdata(master);
1094 flush_workqueue(mpc8xxx_spi->workqueue);
1095 destroy_workqueue(mpc8xxx_spi->workqueue);
1096 spi_unregister_master(master);
1098 free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
1099 iounmap(mpc8xxx_spi->base);
1100 mpc8xxx_spi_cpm_free(mpc8xxx_spi);
1105 struct mpc8xxx_spi_probe_info {
1106 struct fsl_spi_platform_data pdata;
1111 static struct mpc8xxx_spi_probe_info *
1112 to_of_pinfo(struct fsl_spi_platform_data *pdata)
1114 return container_of(pdata, struct mpc8xxx_spi_probe_info, pdata);
1117 static void mpc8xxx_spi_cs_control(struct spi_device *spi, bool on)
1119 struct device *dev = spi->dev.parent;
1120 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
1121 u16 cs = spi->chip_select;
1122 int gpio = pinfo->gpios[cs];
1123 bool alow = pinfo->alow_flags[cs];
1125 gpio_set_value(gpio, on ^ alow);
1128 static int of_mpc8xxx_spi_get_chipselects(struct device *dev)
1130 struct device_node *np = dev_archdata_get_node(&dev->archdata);
1131 struct fsl_spi_platform_data *pdata = dev->platform_data;
1132 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
1133 unsigned int ngpios;
1137 ngpios = of_gpio_count(np);
1140 * SPI w/o chip-select line. One SPI device is still permitted
1143 pdata->max_chipselect = 1;
1147 pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
1150 memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
1152 pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
1154 if (!pinfo->alow_flags) {
1156 goto err_alloc_flags;
1159 for (; i < ngpios; i++) {
1161 enum of_gpio_flags flags;
1163 gpio = of_get_gpio_flags(np, i, &flags);
1164 if (!gpio_is_valid(gpio)) {
1165 dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
1170 ret = gpio_request(gpio, dev_name(dev));
1172 dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
1176 pinfo->gpios[i] = gpio;
1177 pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
1179 ret = gpio_direction_output(pinfo->gpios[i],
1180 pinfo->alow_flags[i]);
1182 dev_err(dev, "can't set output direction for gpio "
1183 "#%d: %d\n", i, ret);
1188 pdata->max_chipselect = ngpios;
1189 pdata->cs_control = mpc8xxx_spi_cs_control;
1195 if (gpio_is_valid(pinfo->gpios[i]))
1196 gpio_free(pinfo->gpios[i]);
1200 kfree(pinfo->alow_flags);
1201 pinfo->alow_flags = NULL;
1203 kfree(pinfo->gpios);
1204 pinfo->gpios = NULL;
1208 static int of_mpc8xxx_spi_free_chipselects(struct device *dev)
1210 struct fsl_spi_platform_data *pdata = dev->platform_data;
1211 struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
1217 for (i = 0; i < pdata->max_chipselect; i++) {
1218 if (gpio_is_valid(pinfo->gpios[i]))
1219 gpio_free(pinfo->gpios[i]);
1222 kfree(pinfo->gpios);
1223 kfree(pinfo->alow_flags);
1227 static int __devinit of_mpc8xxx_spi_probe(struct of_device *ofdev,
1228 const struct of_device_id *ofid)
1230 struct device *dev = &ofdev->dev;
1231 struct device_node *np = ofdev->node;
1232 struct mpc8xxx_spi_probe_info *pinfo;
1233 struct fsl_spi_platform_data *pdata;
1234 struct spi_master *master;
1235 struct resource mem;
1236 struct resource irq;
1240 pinfo = kzalloc(sizeof(*pinfo), GFP_KERNEL);
1244 pdata = &pinfo->pdata;
1245 dev->platform_data = pdata;
1247 /* Allocate bus num dynamically. */
1248 pdata->bus_num = -1;
1250 /* SPI controller is either clocked from QE or SoC clock. */
1251 pdata->sysclk = get_brgfreq();
1252 if (pdata->sysclk == -1) {
1253 pdata->sysclk = fsl_get_sys_freq();
1254 if (pdata->sysclk == -1) {
1260 prop = of_get_property(np, "mode", NULL);
1261 if (prop && !strcmp(prop, "cpu-qe"))
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;
1270 ret = of_mpc8xxx_spi_get_chipselects(dev);
1274 ret = of_address_to_resource(np, 0, &mem);
1278 ret = of_irq_to_resource(np, 0, &irq);
1284 master = mpc8xxx_spi_probe(dev, &mem, irq.start);
1285 if (IS_ERR(master)) {
1286 ret = PTR_ERR(master);
1290 of_register_spi_devices(master, np);
1295 of_mpc8xxx_spi_free_chipselects(dev);
1301 static int __devexit of_mpc8xxx_spi_remove(struct of_device *ofdev)
1305 ret = mpc8xxx_spi_remove(&ofdev->dev);
1308 of_mpc8xxx_spi_free_chipselects(&ofdev->dev);
1312 static const struct of_device_id of_mpc8xxx_spi_match[] = {
1313 { .compatible = "fsl,spi" },
1316 MODULE_DEVICE_TABLE(of, of_mpc8xxx_spi_match);
1318 static struct of_platform_driver of_mpc8xxx_spi_driver = {
1319 .name = "mpc8xxx_spi",
1320 .match_table = of_mpc8xxx_spi_match,
1321 .probe = of_mpc8xxx_spi_probe,
1322 .remove = __devexit_p(of_mpc8xxx_spi_remove),
1325 #ifdef CONFIG_MPC832x_RDB
1328 * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
1329 * only. The driver should go away soon, since newer MPC8323E-RDB's device
1330 * tree can work with OpenFirmware driver. But for now we support old trees
1333 static int __devinit plat_mpc8xxx_spi_probe(struct platform_device *pdev)
1335 struct resource *mem;
1337 struct spi_master *master;
1339 if (!pdev->dev.platform_data)
1342 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1346 irq = platform_get_irq(pdev, 0);
1350 master = mpc8xxx_spi_probe(&pdev->dev, mem, irq);
1352 return PTR_ERR(master);
1356 static int __devexit plat_mpc8xxx_spi_remove(struct platform_device *pdev)
1358 return mpc8xxx_spi_remove(&pdev->dev);
1361 MODULE_ALIAS("platform:mpc8xxx_spi");
1362 static struct platform_driver mpc8xxx_spi_driver = {
1363 .probe = plat_mpc8xxx_spi_probe,
1364 .remove = __exit_p(plat_mpc8xxx_spi_remove),
1366 .name = "mpc8xxx_spi",
1367 .owner = THIS_MODULE,
1371 static bool legacy_driver_failed;
1373 static void __init legacy_driver_register(void)
1375 legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
1378 static void __exit legacy_driver_unregister(void)
1380 if (legacy_driver_failed)
1382 platform_driver_unregister(&mpc8xxx_spi_driver);
1385 static void __init legacy_driver_register(void) {}
1386 static void __exit legacy_driver_unregister(void) {}
1387 #endif /* CONFIG_MPC832x_RDB */
1389 static int __init mpc8xxx_spi_init(void)
1391 legacy_driver_register();
1392 return of_register_platform_driver(&of_mpc8xxx_spi_driver);
1395 static void __exit mpc8xxx_spi_exit(void)
1397 of_unregister_platform_driver(&of_mpc8xxx_spi_driver);
1398 legacy_driver_unregister();
1401 module_init(mpc8xxx_spi_init);
1402 module_exit(mpc8xxx_spi_exit);
1404 MODULE_AUTHOR("Kumar Gala");
1405 MODULE_DESCRIPTION("Simple MPC8xxx SPI Driver");
1406 MODULE_LICENSE("GPL");