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>
42 #include <linux/slab.h>
44 #include <sysdev/fsl_soc.h>
49 /* CPM1 and CPM2 are mutually exclusive. */
52 #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_CH_SPI, 0)
55 #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK, 0, 0)
58 /* SPI Controller registers */
59 struct mpc8xxx_spi_reg
{
69 /* SPI Parameter RAM */
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 */
91 /* SPI Controller mode register definitions */
92 #define SPMODE_LOOP (1 << 30)
93 #define SPMODE_CI_INACTIVEHIGH (1 << 29)
94 #define SPMODE_CP_BEGIN_EDGECLK (1 << 28)
95 #define SPMODE_DIV16 (1 << 27)
96 #define SPMODE_REV (1 << 26)
97 #define SPMODE_MS (1 << 25)
98 #define SPMODE_ENABLE (1 << 24)
99 #define SPMODE_LEN(x) ((x) << 20)
100 #define SPMODE_PM(x) ((x) << 16)
101 #define SPMODE_OP (1 << 14)
102 #define SPMODE_CG(x) ((x) << 7)
105 * Default for SPI Mode:
106 * SPI MODE 0 (inactive low, phase middle, MSB, 8-bit length, slow clk
108 #define SPMODE_INIT_VAL (SPMODE_CI_INACTIVEHIGH | SPMODE_DIV16 | SPMODE_REV | \
109 SPMODE_MS | SPMODE_LEN(7) | SPMODE_PM(0xf))
111 /* SPIE register values */
112 #define SPIE_NE 0x00000200 /* Not empty */
113 #define SPIE_NF 0x00000100 /* Not full */
115 /* SPIM register values */
116 #define SPIM_NE 0x00000200 /* Not empty */
117 #define SPIM_NF 0x00000100 /* Not full */
119 #define SPIE_TXB 0x00000200 /* Last char is written to tx fifo */
120 #define SPIE_RXB 0x00000100 /* Last char is written to rx buf */
122 /* SPCOM register values */
123 #define SPCOM_STR (1 << 23) /* Start transmit */
125 #define SPI_PRAM_SIZE 0x100
126 #define SPI_MRBLR ((unsigned int)PAGE_SIZE)
128 /* SPI Controller driver's private data. */
131 struct mpc8xxx_spi_reg __iomem
*base
;
133 /* rx & tx bufs from the spi_transfer */
138 struct spi_pram __iomem
*pram
;
139 struct cpm_buf_desc __iomem
*tx_bd
;
140 struct cpm_buf_desc __iomem
*rx_bd
;
142 struct spi_transfer
*xfer_in_progress
;
144 /* dma addresses for CPM transfers */
150 dma_addr_t dma_dummy_tx
;
151 dma_addr_t dma_dummy_rx
;
153 /* functions to deal with different sized buffers */
154 void (*get_rx
) (u32 rx_data
, struct mpc8xxx_spi
*);
155 u32(*get_tx
) (struct mpc8xxx_spi
*);
160 unsigned nsecs
; /* (clock cycle time)/2 */
162 u32 spibrg
; /* SPIBRG input clock */
163 u32 rx_shift
; /* RX data reg shift when in qe mode */
164 u32 tx_shift
; /* TX data reg shift when in qe mode */
168 struct workqueue_struct
*workqueue
;
169 struct work_struct work
;
171 struct list_head queue
;
174 struct completion done
;
177 static void *mpc8xxx_dummy_rx
;
178 static DEFINE_MUTEX(mpc8xxx_dummy_rx_lock
);
179 static int mpc8xxx_dummy_rx_refcnt
;
181 struct spi_mpc8xxx_cs
{
182 /* functions to deal with different sized buffers */
183 void (*get_rx
) (u32 rx_data
, struct mpc8xxx_spi
*);
184 u32 (*get_tx
) (struct mpc8xxx_spi
*);
185 u32 rx_shift
; /* RX data reg shift when in qe mode */
186 u32 tx_shift
; /* TX data reg shift when in qe mode */
187 u32 hw_mode
; /* Holds HW mode register settings */
190 static inline void mpc8xxx_spi_write_reg(__be32 __iomem
*reg
, u32 val
)
195 static inline u32
mpc8xxx_spi_read_reg(__be32 __iomem
*reg
)
200 #define MPC83XX_SPI_RX_BUF(type) \
202 void mpc8xxx_spi_rx_buf_##type(u32 data, struct mpc8xxx_spi *mpc8xxx_spi) \
204 type *rx = mpc8xxx_spi->rx; \
205 *rx++ = (type)(data >> mpc8xxx_spi->rx_shift); \
206 mpc8xxx_spi->rx = rx; \
209 #define MPC83XX_SPI_TX_BUF(type) \
211 u32 mpc8xxx_spi_tx_buf_##type(struct mpc8xxx_spi *mpc8xxx_spi) \
214 const type *tx = mpc8xxx_spi->tx; \
217 data = *tx++ << mpc8xxx_spi->tx_shift; \
218 mpc8xxx_spi->tx = tx; \
222 MPC83XX_SPI_RX_BUF(u8
)
223 MPC83XX_SPI_RX_BUF(u16
)
224 MPC83XX_SPI_RX_BUF(u32
)
225 MPC83XX_SPI_TX_BUF(u8
)
226 MPC83XX_SPI_TX_BUF(u16
)
227 MPC83XX_SPI_TX_BUF(u32
)
229 static void mpc8xxx_spi_change_mode(struct spi_device
*spi
)
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
;
236 if (cs
->hw_mode
== mpc8xxx_spi_read_reg(mode
))
239 /* Turn off IRQs locally to minimize time that SPI is disabled. */
240 local_irq_save(flags
);
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
);
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);
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
));
262 local_irq_restore(flags
);
265 static void mpc8xxx_spi_chipselect(struct spi_device
*spi
, int value
)
267 struct mpc8xxx_spi
*mpc8xxx_spi
= spi_master_get_devdata(spi
->master
);
268 struct fsl_spi_platform_data
*pdata
= spi
->dev
.parent
->platform_data
;
269 bool pol
= spi
->mode
& SPI_CS_HIGH
;
270 struct spi_mpc8xxx_cs
*cs
= spi
->controller_state
;
272 if (value
== BITBANG_CS_INACTIVE
) {
273 if (pdata
->cs_control
)
274 pdata
->cs_control(spi
, !pol
);
277 if (value
== BITBANG_CS_ACTIVE
) {
278 mpc8xxx_spi
->rx_shift
= cs
->rx_shift
;
279 mpc8xxx_spi
->tx_shift
= cs
->tx_shift
;
280 mpc8xxx_spi
->get_rx
= cs
->get_rx
;
281 mpc8xxx_spi
->get_tx
= cs
->get_tx
;
283 mpc8xxx_spi_change_mode(spi
);
285 if (pdata
->cs_control
)
286 pdata
->cs_control(spi
, pol
);
291 int mpc8xxx_spi_setup_transfer(struct spi_device
*spi
, struct spi_transfer
*t
)
293 struct mpc8xxx_spi
*mpc8xxx_spi
;
294 u8 bits_per_word
, pm
;
296 struct spi_mpc8xxx_cs
*cs
= spi
->controller_state
;
298 mpc8xxx_spi
= spi_master_get_devdata(spi
->master
);
301 bits_per_word
= t
->bits_per_word
;
308 /* spi_transfer level calls that work per-word */
310 bits_per_word
= spi
->bits_per_word
;
312 /* Make sure its a bit width we support [4..16, 32] */
313 if ((bits_per_word
< 4)
314 || ((bits_per_word
> 16) && (bits_per_word
!= 32)))
318 hz
= spi
->max_speed_hz
;
322 if (bits_per_word
<= 8) {
323 cs
->get_rx
= mpc8xxx_spi_rx_buf_u8
;
324 cs
->get_tx
= mpc8xxx_spi_tx_buf_u8
;
325 if (mpc8xxx_spi
->flags
& SPI_QE_CPU_MODE
) {
329 } else if (bits_per_word
<= 16) {
330 cs
->get_rx
= mpc8xxx_spi_rx_buf_u16
;
331 cs
->get_tx
= mpc8xxx_spi_tx_buf_u16
;
332 if (mpc8xxx_spi
->flags
& SPI_QE_CPU_MODE
) {
336 } else if (bits_per_word
<= 32) {
337 cs
->get_rx
= mpc8xxx_spi_rx_buf_u32
;
338 cs
->get_tx
= mpc8xxx_spi_tx_buf_u32
;
342 if (mpc8xxx_spi
->flags
& SPI_QE_CPU_MODE
&&
343 spi
->mode
& SPI_LSB_FIRST
) {
345 if (bits_per_word
<= 8)
351 mpc8xxx_spi
->rx_shift
= cs
->rx_shift
;
352 mpc8xxx_spi
->tx_shift
= cs
->tx_shift
;
353 mpc8xxx_spi
->get_rx
= cs
->get_rx
;
354 mpc8xxx_spi
->get_tx
= cs
->get_tx
;
356 if (bits_per_word
== 32)
359 bits_per_word
= bits_per_word
- 1;
361 /* mask out bits we are going to set */
362 cs
->hw_mode
&= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
365 cs
->hw_mode
|= SPMODE_LEN(bits_per_word
);
367 if ((mpc8xxx_spi
->spibrg
/ hz
) > 64) {
368 cs
->hw_mode
|= SPMODE_DIV16
;
369 pm
= (mpc8xxx_spi
->spibrg
- 1) / (hz
* 64) + 1;
371 WARN_ONCE(pm
> 16, "%s: Requested speed is too low: %d Hz. "
372 "Will use %d Hz instead.\n", dev_name(&spi
->dev
),
373 hz
, mpc8xxx_spi
->spibrg
/ 1024);
377 pm
= (mpc8xxx_spi
->spibrg
- 1) / (hz
* 4) + 1;
381 cs
->hw_mode
|= SPMODE_PM(pm
);
383 mpc8xxx_spi_change_mode(spi
);
387 static void mpc8xxx_spi_cpm_bufs_start(struct mpc8xxx_spi
*mspi
)
389 struct cpm_buf_desc __iomem
*tx_bd
= mspi
->tx_bd
;
390 struct cpm_buf_desc __iomem
*rx_bd
= mspi
->rx_bd
;
391 unsigned int xfer_len
= min(mspi
->count
, SPI_MRBLR
);
392 unsigned int xfer_ofs
;
394 xfer_ofs
= mspi
->xfer_in_progress
->len
- mspi
->count
;
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
);
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
|
406 mpc8xxx_spi_write_reg(&mspi
->base
->command
, SPCOM_STR
);
409 static int mpc8xxx_spi_cpm_bufs(struct mpc8xxx_spi
*mspi
,
410 struct spi_transfer
*t
, bool is_dma_mapped
)
412 struct device
*dev
= mspi
->dev
;
415 mspi
->map_tx_dma
= 0;
416 mspi
->map_rx_dma
= 0;
418 mspi
->map_tx_dma
= 1;
419 mspi
->map_rx_dma
= 1;
423 mspi
->tx_dma
= mspi
->dma_dummy_tx
;
424 mspi
->map_tx_dma
= 0;
428 mspi
->rx_dma
= mspi
->dma_dummy_rx
;
429 mspi
->map_rx_dma
= 0;
432 if (mspi
->map_tx_dma
) {
433 void *nonconst_tx
= (void *)mspi
->tx
; /* shut up gcc */
435 mspi
->tx_dma
= dma_map_single(dev
, nonconst_tx
, t
->len
,
437 if (dma_mapping_error(dev
, mspi
->tx_dma
)) {
438 dev_err(dev
, "unable to map tx dma\n");
442 mspi
->tx_dma
= t
->tx_dma
;
445 if (mspi
->map_rx_dma
) {
446 mspi
->rx_dma
= dma_map_single(dev
, mspi
->rx
, t
->len
,
448 if (dma_mapping_error(dev
, mspi
->rx_dma
)) {
449 dev_err(dev
, "unable to map rx dma\n");
453 mspi
->rx_dma
= t
->rx_dma
;
457 mpc8xxx_spi_write_reg(&mspi
->base
->mask
, SPIE_RXB
);
459 mspi
->xfer_in_progress
= t
;
460 mspi
->count
= t
->len
;
462 /* start CPM transfers */
463 mpc8xxx_spi_cpm_bufs_start(mspi
);
468 if (mspi
->map_tx_dma
)
469 dma_unmap_single(dev
, mspi
->tx_dma
, t
->len
, DMA_TO_DEVICE
);
473 static void mpc8xxx_spi_cpm_bufs_complete(struct mpc8xxx_spi
*mspi
)
475 struct device
*dev
= mspi
->dev
;
476 struct spi_transfer
*t
= mspi
->xfer_in_progress
;
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
;
485 static int mpc8xxx_spi_cpu_bufs(struct mpc8xxx_spi
*mspi
,
486 struct spi_transfer
*t
, unsigned int len
)
493 mpc8xxx_spi_write_reg(&mspi
->base
->mask
, SPIM_NE
);
496 word
= mspi
->get_tx(mspi
);
497 mpc8xxx_spi_write_reg(&mspi
->base
->transmit
, word
);
502 static int mpc8xxx_spi_bufs(struct spi_device
*spi
, struct spi_transfer
*t
,
505 struct mpc8xxx_spi
*mpc8xxx_spi
= spi_master_get_devdata(spi
->master
);
506 unsigned int len
= t
->len
;
510 bits_per_word
= spi
->bits_per_word
;
511 if (t
->bits_per_word
)
512 bits_per_word
= t
->bits_per_word
;
514 if (bits_per_word
> 8) {
515 /* invalid length? */
520 if (bits_per_word
> 16) {
521 /* invalid length? */
527 mpc8xxx_spi
->tx
= t
->tx_buf
;
528 mpc8xxx_spi
->rx
= t
->rx_buf
;
530 INIT_COMPLETION(mpc8xxx_spi
->done
);
532 if (mpc8xxx_spi
->flags
& SPI_CPM_MODE
)
533 ret
= mpc8xxx_spi_cpm_bufs(mpc8xxx_spi
, t
, is_dma_mapped
);
535 ret
= mpc8xxx_spi_cpu_bufs(mpc8xxx_spi
, t
, len
);
539 wait_for_completion(&mpc8xxx_spi
->done
);
541 /* disable rx ints */
542 mpc8xxx_spi_write_reg(&mpc8xxx_spi
->base
->mask
, 0);
544 if (mpc8xxx_spi
->flags
& SPI_CPM_MODE
)
545 mpc8xxx_spi_cpm_bufs_complete(mpc8xxx_spi
);
547 return mpc8xxx_spi
->count
;
550 static void mpc8xxx_spi_do_one_msg(struct spi_message
*m
)
552 struct spi_device
*spi
= m
->spi
;
553 struct spi_transfer
*t
;
554 unsigned int cs_change
;
555 const int nsecs
= 50;
560 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
561 if (t
->bits_per_word
|| t
->speed_hz
) {
562 /* Don't allow changes if CS is active */
566 status
= mpc8xxx_spi_setup_transfer(spi
, t
);
572 mpc8xxx_spi_chipselect(spi
, BITBANG_CS_ACTIVE
);
575 cs_change
= t
->cs_change
;
577 status
= mpc8xxx_spi_bufs(spi
, t
, m
->is_dma_mapped
);
582 m
->actual_length
+= t
->len
;
585 udelay(t
->delay_usecs
);
589 mpc8xxx_spi_chipselect(spi
, BITBANG_CS_INACTIVE
);
595 m
->complete(m
->context
);
597 if (status
|| !cs_change
) {
599 mpc8xxx_spi_chipselect(spi
, BITBANG_CS_INACTIVE
);
602 mpc8xxx_spi_setup_transfer(spi
, NULL
);
605 static void mpc8xxx_spi_work(struct work_struct
*work
)
607 struct mpc8xxx_spi
*mpc8xxx_spi
= container_of(work
, struct mpc8xxx_spi
,
610 spin_lock_irq(&mpc8xxx_spi
->lock
);
611 while (!list_empty(&mpc8xxx_spi
->queue
)) {
612 struct spi_message
*m
= container_of(mpc8xxx_spi
->queue
.next
,
613 struct spi_message
, queue
);
615 list_del_init(&m
->queue
);
616 spin_unlock_irq(&mpc8xxx_spi
->lock
);
618 mpc8xxx_spi_do_one_msg(m
);
620 spin_lock_irq(&mpc8xxx_spi
->lock
);
622 spin_unlock_irq(&mpc8xxx_spi
->lock
);
625 static int mpc8xxx_spi_setup(struct spi_device
*spi
)
627 struct mpc8xxx_spi
*mpc8xxx_spi
;
630 struct spi_mpc8xxx_cs
*cs
= spi
->controller_state
;
632 if (!spi
->max_speed_hz
)
636 cs
= kzalloc(sizeof *cs
, GFP_KERNEL
);
639 spi
->controller_state
= cs
;
641 mpc8xxx_spi
= spi_master_get_devdata(spi
->master
);
643 hw_mode
= cs
->hw_mode
; /* Save orginal settings */
644 cs
->hw_mode
= mpc8xxx_spi_read_reg(&mpc8xxx_spi
->base
->mode
);
645 /* mask out bits we are going to set */
646 cs
->hw_mode
&= ~(SPMODE_CP_BEGIN_EDGECLK
| SPMODE_CI_INACTIVEHIGH
647 | SPMODE_REV
| SPMODE_LOOP
);
649 if (spi
->mode
& SPI_CPHA
)
650 cs
->hw_mode
|= SPMODE_CP_BEGIN_EDGECLK
;
651 if (spi
->mode
& SPI_CPOL
)
652 cs
->hw_mode
|= SPMODE_CI_INACTIVEHIGH
;
653 if (!(spi
->mode
& SPI_LSB_FIRST
))
654 cs
->hw_mode
|= SPMODE_REV
;
655 if (spi
->mode
& SPI_LOOP
)
656 cs
->hw_mode
|= SPMODE_LOOP
;
658 retval
= mpc8xxx_spi_setup_transfer(spi
, NULL
);
660 cs
->hw_mode
= hw_mode
; /* Restore settings */
666 static void mpc8xxx_spi_cpm_irq(struct mpc8xxx_spi
*mspi
, u32 events
)
670 dev_dbg(mspi
->dev
, "%s: bd datlen %d, count %d\n", __func__
,
671 in_be16(&mspi
->rx_bd
->cbd_datlen
), mspi
->count
);
673 len
= in_be16(&mspi
->rx_bd
->cbd_datlen
);
674 if (len
> mspi
->count
) {
679 /* Clear the events */
680 mpc8xxx_spi_write_reg(&mspi
->base
->event
, events
);
684 mpc8xxx_spi_cpm_bufs_start(mspi
);
686 complete(&mspi
->done
);
689 static void mpc8xxx_spi_cpu_irq(struct mpc8xxx_spi
*mspi
, u32 events
)
691 /* We need handle RX first */
692 if (events
& SPIE_NE
) {
693 u32 rx_data
= mpc8xxx_spi_read_reg(&mspi
->base
->receive
);
696 mspi
->get_rx(rx_data
, mspi
);
699 if ((events
& SPIE_NF
) == 0)
700 /* spin until TX is done */
702 mpc8xxx_spi_read_reg(&mspi
->base
->event
)) &
706 /* Clear the events */
707 mpc8xxx_spi_write_reg(&mspi
->base
->event
, events
);
711 u32 word
= mspi
->get_tx(mspi
);
713 mpc8xxx_spi_write_reg(&mspi
->base
->transmit
, word
);
715 complete(&mspi
->done
);
719 static irqreturn_t
mpc8xxx_spi_irq(s32 irq
, void *context_data
)
721 struct mpc8xxx_spi
*mspi
= context_data
;
722 irqreturn_t ret
= IRQ_NONE
;
725 /* Get interrupt events(tx/rx) */
726 events
= mpc8xxx_spi_read_reg(&mspi
->base
->event
);
730 dev_dbg(mspi
->dev
, "%s: events %x\n", __func__
, events
);
732 if (mspi
->flags
& SPI_CPM_MODE
)
733 mpc8xxx_spi_cpm_irq(mspi
, events
);
735 mpc8xxx_spi_cpu_irq(mspi
, events
);
740 static int mpc8xxx_spi_transfer(struct spi_device
*spi
,
741 struct spi_message
*m
)
743 struct mpc8xxx_spi
*mpc8xxx_spi
= spi_master_get_devdata(spi
->master
);
746 m
->actual_length
= 0;
747 m
->status
= -EINPROGRESS
;
749 spin_lock_irqsave(&mpc8xxx_spi
->lock
, flags
);
750 list_add_tail(&m
->queue
, &mpc8xxx_spi
->queue
);
751 queue_work(mpc8xxx_spi
->workqueue
, &mpc8xxx_spi
->work
);
752 spin_unlock_irqrestore(&mpc8xxx_spi
->lock
, flags
);
758 static void mpc8xxx_spi_cleanup(struct spi_device
*spi
)
760 kfree(spi
->controller_state
);
763 static void *mpc8xxx_spi_alloc_dummy_rx(void)
765 mutex_lock(&mpc8xxx_dummy_rx_lock
);
767 if (!mpc8xxx_dummy_rx
)
768 mpc8xxx_dummy_rx
= kmalloc(SPI_MRBLR
, GFP_KERNEL
);
769 if (mpc8xxx_dummy_rx
)
770 mpc8xxx_dummy_rx_refcnt
++;
772 mutex_unlock(&mpc8xxx_dummy_rx_lock
);
774 return mpc8xxx_dummy_rx
;
777 static void mpc8xxx_spi_free_dummy_rx(void)
779 mutex_lock(&mpc8xxx_dummy_rx_lock
);
781 switch (mpc8xxx_dummy_rx_refcnt
) {
786 kfree(mpc8xxx_dummy_rx
);
787 mpc8xxx_dummy_rx
= NULL
;
790 mpc8xxx_dummy_rx_refcnt
--;
794 mutex_unlock(&mpc8xxx_dummy_rx_lock
);
797 static unsigned long mpc8xxx_spi_cpm_get_pram(struct mpc8xxx_spi
*mspi
)
799 struct device
*dev
= mspi
->dev
;
800 struct device_node
*np
= dev_archdata_get_node(&dev
->archdata
);
803 unsigned long spi_base_ofs
;
804 unsigned long pram_ofs
= -ENOMEM
;
806 /* Can't use of_address_to_resource(), QE muram isn't at 0. */
807 iprop
= of_get_property(np
, "reg", &size
);
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
);
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
);
821 /* CPM1 and CPM2 pram must be at a fixed addr. */
822 if (!iprop
|| size
!= sizeof(*iprop
) * 4)
825 spi_base_ofs
= cpm_muram_alloc_fixed(iprop
[2], 2);
826 if (IS_ERR_VALUE(spi_base_ofs
))
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
);
834 out_be16(spi_base
, pram_ofs
);
837 struct spi_pram __iomem
*pram
= cpm_muram_addr(spi_base_ofs
);
838 u16 rpbase
= in_be16(&pram
->rpbase
);
840 /* Microcode relocation patch applied? */
847 cpm_muram_free(spi_base_ofs
);
851 static int mpc8xxx_spi_cpm_init(struct mpc8xxx_spi
*mspi
)
853 struct device
*dev
= mspi
->dev
;
854 struct device_node
*np
= dev_archdata_get_node(&dev
->archdata
);
857 unsigned long pram_ofs
;
858 unsigned long bds_ofs
;
860 if (!(mspi
->flags
& SPI_CPM_MODE
))
863 if (!mpc8xxx_spi_alloc_dummy_rx())
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
;
871 switch (mspi
->subblock
) {
873 dev_warn(dev
, "cell-index unspecified, assuming SPI1");
876 mspi
->subblock
= QE_CR_SUBBLOCK_SPI1
;
879 mspi
->subblock
= QE_CR_SUBBLOCK_SPI2
;
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");
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");
897 mspi
->dma_dummy_tx
= dma_map_single(dev
, empty_zero_page
, PAGE_SIZE
,
899 if (dma_mapping_error(dev
, mspi
->dma_dummy_tx
)) {
900 dev_err(dev
, "unable to map dummy tx buffer\n");
904 mspi
->dma_dummy_rx
= dma_map_single(dev
, mpc8xxx_dummy_rx
, SPI_MRBLR
,
906 if (dma_mapping_error(dev
, mspi
->dma_dummy_rx
)) {
907 dev_err(dev
, "unable to map dummy rx buffer\n");
911 mspi
->pram
= cpm_muram_addr(pram_ofs
);
913 mspi
->tx_bd
= cpm_muram_addr(bds_ofs
);
914 mspi
->rx_bd
= cpm_muram_addr(bds_ofs
+ sizeof(*mspi
->tx_bd
));
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);
936 dma_unmap_single(dev
, mspi
->dma_dummy_tx
, PAGE_SIZE
, DMA_TO_DEVICE
);
938 cpm_muram_free(bds_ofs
);
940 cpm_muram_free(pram_ofs
);
942 mpc8xxx_spi_free_dummy_rx();
946 static void mpc8xxx_spi_cpm_free(struct mpc8xxx_spi
*mspi
)
948 struct device
*dev
= mspi
->dev
;
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();
957 static const char *mpc8xxx_spi_strmode(unsigned int flags
)
959 if (flags
& SPI_QE_CPU_MODE
) {
961 } else if (flags
& SPI_CPM_MODE
) {
964 else if (flags
& SPI_CPM2
)
972 static struct spi_master
* __devinit
973 mpc8xxx_spi_probe(struct device
*dev
, struct resource
*mem
, unsigned int irq
)
975 struct fsl_spi_platform_data
*pdata
= dev
->platform_data
;
976 struct spi_master
*master
;
977 struct mpc8xxx_spi
*mpc8xxx_spi
;
981 master
= spi_alloc_master(dev
, sizeof(struct mpc8xxx_spi
));
982 if (master
== NULL
) {
987 dev_set_drvdata(dev
, master
);
989 /* the spi->mode bits understood by this driver: */
990 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
991 | SPI_LSB_FIRST
| SPI_LOOP
;
993 master
->setup
= mpc8xxx_spi_setup
;
994 master
->transfer
= mpc8xxx_spi_transfer
;
995 master
->cleanup
= mpc8xxx_spi_cleanup
;
997 mpc8xxx_spi
= spi_master_get_devdata(master
);
998 mpc8xxx_spi
->dev
= dev
;
999 mpc8xxx_spi
->get_rx
= mpc8xxx_spi_rx_buf_u8
;
1000 mpc8xxx_spi
->get_tx
= mpc8xxx_spi_tx_buf_u8
;
1001 mpc8xxx_spi
->flags
= pdata
->flags
;
1002 mpc8xxx_spi
->spibrg
= pdata
->sysclk
;
1004 ret
= mpc8xxx_spi_cpm_init(mpc8xxx_spi
);
1008 mpc8xxx_spi
->rx_shift
= 0;
1009 mpc8xxx_spi
->tx_shift
= 0;
1010 if (mpc8xxx_spi
->flags
& SPI_QE_CPU_MODE
) {
1011 mpc8xxx_spi
->rx_shift
= 16;
1012 mpc8xxx_spi
->tx_shift
= 24;
1015 init_completion(&mpc8xxx_spi
->done
);
1017 mpc8xxx_spi
->base
= ioremap(mem
->start
, resource_size(mem
));
1018 if (mpc8xxx_spi
->base
== NULL
) {
1023 mpc8xxx_spi
->irq
= irq
;
1025 /* Register for SPI Interrupt */
1026 ret
= request_irq(mpc8xxx_spi
->irq
, mpc8xxx_spi_irq
,
1027 0, "mpc8xxx_spi", mpc8xxx_spi
);
1032 master
->bus_num
= pdata
->bus_num
;
1033 master
->num_chipselect
= pdata
->max_chipselect
;
1035 /* SPI controller initializations */
1036 mpc8xxx_spi_write_reg(&mpc8xxx_spi
->base
->mode
, 0);
1037 mpc8xxx_spi_write_reg(&mpc8xxx_spi
->base
->mask
, 0);
1038 mpc8xxx_spi_write_reg(&mpc8xxx_spi
->base
->command
, 0);
1039 mpc8xxx_spi_write_reg(&mpc8xxx_spi
->base
->event
, 0xffffffff);
1041 /* Enable SPI interface */
1042 regval
= pdata
->initial_spmode
| SPMODE_INIT_VAL
| SPMODE_ENABLE
;
1043 if (mpc8xxx_spi
->flags
& SPI_QE_CPU_MODE
)
1044 regval
|= SPMODE_OP
;
1046 mpc8xxx_spi_write_reg(&mpc8xxx_spi
->base
->mode
, regval
);
1047 spin_lock_init(&mpc8xxx_spi
->lock
);
1048 init_completion(&mpc8xxx_spi
->done
);
1049 INIT_WORK(&mpc8xxx_spi
->work
, mpc8xxx_spi_work
);
1050 INIT_LIST_HEAD(&mpc8xxx_spi
->queue
);
1052 mpc8xxx_spi
->workqueue
= create_singlethread_workqueue(
1053 dev_name(master
->dev
.parent
));
1054 if (mpc8xxx_spi
->workqueue
== NULL
) {
1059 ret
= spi_register_master(master
);
1063 dev_info(dev
, "at 0x%p (irq = %d), %s mode\n", mpc8xxx_spi
->base
,
1064 mpc8xxx_spi
->irq
, mpc8xxx_spi_strmode(mpc8xxx_spi
->flags
));
1069 destroy_workqueue(mpc8xxx_spi
->workqueue
);
1071 free_irq(mpc8xxx_spi
->irq
, mpc8xxx_spi
);
1073 iounmap(mpc8xxx_spi
->base
);
1075 mpc8xxx_spi_cpm_free(mpc8xxx_spi
);
1077 spi_master_put(master
);
1079 return ERR_PTR(ret
);
1082 static int __devexit
mpc8xxx_spi_remove(struct device
*dev
)
1084 struct mpc8xxx_spi
*mpc8xxx_spi
;
1085 struct spi_master
*master
;
1087 master
= dev_get_drvdata(dev
);
1088 mpc8xxx_spi
= spi_master_get_devdata(master
);
1090 flush_workqueue(mpc8xxx_spi
->workqueue
);
1091 destroy_workqueue(mpc8xxx_spi
->workqueue
);
1092 spi_unregister_master(master
);
1094 free_irq(mpc8xxx_spi
->irq
, mpc8xxx_spi
);
1095 iounmap(mpc8xxx_spi
->base
);
1096 mpc8xxx_spi_cpm_free(mpc8xxx_spi
);
1101 struct mpc8xxx_spi_probe_info
{
1102 struct fsl_spi_platform_data pdata
;
1107 static struct mpc8xxx_spi_probe_info
*
1108 to_of_pinfo(struct fsl_spi_platform_data
*pdata
)
1110 return container_of(pdata
, struct mpc8xxx_spi_probe_info
, pdata
);
1113 static void mpc8xxx_spi_cs_control(struct spi_device
*spi
, bool on
)
1115 struct device
*dev
= spi
->dev
.parent
;
1116 struct mpc8xxx_spi_probe_info
*pinfo
= to_of_pinfo(dev
->platform_data
);
1117 u16 cs
= spi
->chip_select
;
1118 int gpio
= pinfo
->gpios
[cs
];
1119 bool alow
= pinfo
->alow_flags
[cs
];
1121 gpio_set_value(gpio
, on
^ alow
);
1124 static int of_mpc8xxx_spi_get_chipselects(struct device
*dev
)
1126 struct device_node
*np
= dev_archdata_get_node(&dev
->archdata
);
1127 struct fsl_spi_platform_data
*pdata
= dev
->platform_data
;
1128 struct mpc8xxx_spi_probe_info
*pinfo
= to_of_pinfo(pdata
);
1129 unsigned int ngpios
;
1133 ngpios
= of_gpio_count(np
);
1136 * SPI w/o chip-select line. One SPI device is still permitted
1139 pdata
->max_chipselect
= 1;
1143 pinfo
->gpios
= kmalloc(ngpios
* sizeof(*pinfo
->gpios
), GFP_KERNEL
);
1146 memset(pinfo
->gpios
, -1, ngpios
* sizeof(*pinfo
->gpios
));
1148 pinfo
->alow_flags
= kzalloc(ngpios
* sizeof(*pinfo
->alow_flags
),
1150 if (!pinfo
->alow_flags
) {
1152 goto err_alloc_flags
;
1155 for (; i
< ngpios
; i
++) {
1157 enum of_gpio_flags flags
;
1159 gpio
= of_get_gpio_flags(np
, i
, &flags
);
1160 if (!gpio_is_valid(gpio
)) {
1161 dev_err(dev
, "invalid gpio #%d: %d\n", i
, gpio
);
1166 ret
= gpio_request(gpio
, dev_name(dev
));
1168 dev_err(dev
, "can't request gpio #%d: %d\n", i
, ret
);
1172 pinfo
->gpios
[i
] = gpio
;
1173 pinfo
->alow_flags
[i
] = flags
& OF_GPIO_ACTIVE_LOW
;
1175 ret
= gpio_direction_output(pinfo
->gpios
[i
],
1176 pinfo
->alow_flags
[i
]);
1178 dev_err(dev
, "can't set output direction for gpio "
1179 "#%d: %d\n", i
, ret
);
1184 pdata
->max_chipselect
= ngpios
;
1185 pdata
->cs_control
= mpc8xxx_spi_cs_control
;
1191 if (gpio_is_valid(pinfo
->gpios
[i
]))
1192 gpio_free(pinfo
->gpios
[i
]);
1196 kfree(pinfo
->alow_flags
);
1197 pinfo
->alow_flags
= NULL
;
1199 kfree(pinfo
->gpios
);
1200 pinfo
->gpios
= NULL
;
1204 static int of_mpc8xxx_spi_free_chipselects(struct device
*dev
)
1206 struct fsl_spi_platform_data
*pdata
= dev
->platform_data
;
1207 struct mpc8xxx_spi_probe_info
*pinfo
= to_of_pinfo(pdata
);
1213 for (i
= 0; i
< pdata
->max_chipselect
; i
++) {
1214 if (gpio_is_valid(pinfo
->gpios
[i
]))
1215 gpio_free(pinfo
->gpios
[i
]);
1218 kfree(pinfo
->gpios
);
1219 kfree(pinfo
->alow_flags
);
1223 static int __devinit
of_mpc8xxx_spi_probe(struct of_device
*ofdev
,
1224 const struct of_device_id
*ofid
)
1226 struct device
*dev
= &ofdev
->dev
;
1227 struct device_node
*np
= ofdev
->node
;
1228 struct mpc8xxx_spi_probe_info
*pinfo
;
1229 struct fsl_spi_platform_data
*pdata
;
1230 struct spi_master
*master
;
1231 struct resource mem
;
1232 struct resource irq
;
1236 pinfo
= kzalloc(sizeof(*pinfo
), GFP_KERNEL
);
1240 pdata
= &pinfo
->pdata
;
1241 dev
->platform_data
= pdata
;
1243 /* Allocate bus num dynamically. */
1244 pdata
->bus_num
= -1;
1246 /* SPI controller is either clocked from QE or SoC clock. */
1247 pdata
->sysclk
= get_brgfreq();
1248 if (pdata
->sysclk
== -1) {
1249 pdata
->sysclk
= fsl_get_sys_freq();
1250 if (pdata
->sysclk
== -1) {
1256 prop
= of_get_property(np
, "mode", NULL
);
1257 if (prop
&& !strcmp(prop
, "cpu-qe"))
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
;
1266 ret
= of_mpc8xxx_spi_get_chipselects(dev
);
1270 ret
= of_address_to_resource(np
, 0, &mem
);
1274 ret
= of_irq_to_resource(np
, 0, &irq
);
1280 master
= mpc8xxx_spi_probe(dev
, &mem
, irq
.start
);
1281 if (IS_ERR(master
)) {
1282 ret
= PTR_ERR(master
);
1286 of_register_spi_devices(master
, np
);
1291 of_mpc8xxx_spi_free_chipselects(dev
);
1297 static int __devexit
of_mpc8xxx_spi_remove(struct of_device
*ofdev
)
1301 ret
= mpc8xxx_spi_remove(&ofdev
->dev
);
1304 of_mpc8xxx_spi_free_chipselects(&ofdev
->dev
);
1308 static const struct of_device_id of_mpc8xxx_spi_match
[] = {
1309 { .compatible
= "fsl,spi" },
1312 MODULE_DEVICE_TABLE(of
, of_mpc8xxx_spi_match
);
1314 static struct of_platform_driver of_mpc8xxx_spi_driver
= {
1315 .name
= "mpc8xxx_spi",
1316 .match_table
= of_mpc8xxx_spi_match
,
1317 .probe
= of_mpc8xxx_spi_probe
,
1318 .remove
= __devexit_p(of_mpc8xxx_spi_remove
),
1321 #ifdef CONFIG_MPC832x_RDB
1324 * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
1325 * only. The driver should go away soon, since newer MPC8323E-RDB's device
1326 * tree can work with OpenFirmware driver. But for now we support old trees
1329 static int __devinit
plat_mpc8xxx_spi_probe(struct platform_device
*pdev
)
1331 struct resource
*mem
;
1333 struct spi_master
*master
;
1335 if (!pdev
->dev
.platform_data
)
1338 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1342 irq
= platform_get_irq(pdev
, 0);
1346 master
= mpc8xxx_spi_probe(&pdev
->dev
, mem
, irq
);
1348 return PTR_ERR(master
);
1352 static int __devexit
plat_mpc8xxx_spi_remove(struct platform_device
*pdev
)
1354 return mpc8xxx_spi_remove(&pdev
->dev
);
1357 MODULE_ALIAS("platform:mpc8xxx_spi");
1358 static struct platform_driver mpc8xxx_spi_driver
= {
1359 .probe
= plat_mpc8xxx_spi_probe
,
1360 .remove
= __devexit_p(plat_mpc8xxx_spi_remove
),
1362 .name
= "mpc8xxx_spi",
1363 .owner
= THIS_MODULE
,
1367 static bool legacy_driver_failed
;
1369 static void __init
legacy_driver_register(void)
1371 legacy_driver_failed
= platform_driver_register(&mpc8xxx_spi_driver
);
1374 static void __exit
legacy_driver_unregister(void)
1376 if (legacy_driver_failed
)
1378 platform_driver_unregister(&mpc8xxx_spi_driver
);
1381 static void __init
legacy_driver_register(void) {}
1382 static void __exit
legacy_driver_unregister(void) {}
1383 #endif /* CONFIG_MPC832x_RDB */
1385 static int __init
mpc8xxx_spi_init(void)
1387 legacy_driver_register();
1388 return of_register_platform_driver(&of_mpc8xxx_spi_driver
);
1391 static void __exit
mpc8xxx_spi_exit(void)
1393 of_unregister_platform_driver(&of_mpc8xxx_spi_driver
);
1394 legacy_driver_unregister();
1397 module_init(mpc8xxx_spi_init
);
1398 module_exit(mpc8xxx_spi_exit
);
1400 MODULE_AUTHOR("Kumar Gala");
1401 MODULE_DESCRIPTION("Simple MPC8xxx SPI Driver");
1402 MODULE_LICENSE("GPL");