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
);
245 /* When in CPM mode, we need to reinit tx and rx. */
246 if (mspi
->flags
& SPI_CPM_MODE
) {
247 if (mspi
->flags
& SPI_QE
) {
248 qe_issue_cmd(QE_INIT_TX_RX
, mspi
->subblock
,
249 QE_CR_PROTOCOL_UNSPECIFIED
, 0);
251 cpm_command(CPM_SPI_CMD
, CPM_CR_INIT_TRX
);
252 if (mspi
->flags
& SPI_CPM1
) {
253 out_be16(&mspi
->pram
->rbptr
,
254 in_be16(&mspi
->pram
->rbase
));
255 out_be16(&mspi
->pram
->tbptr
,
256 in_be16(&mspi
->pram
->tbase
));
260 mpc8xxx_spi_write_reg(mode
, cs
->hw_mode
);
261 local_irq_restore(flags
);
264 static void mpc8xxx_spi_chipselect(struct spi_device
*spi
, int value
)
266 struct mpc8xxx_spi
*mpc8xxx_spi
= spi_master_get_devdata(spi
->master
);
267 struct fsl_spi_platform_data
*pdata
= spi
->dev
.parent
->platform_data
;
268 bool pol
= spi
->mode
& SPI_CS_HIGH
;
269 struct spi_mpc8xxx_cs
*cs
= spi
->controller_state
;
271 if (value
== BITBANG_CS_INACTIVE
) {
272 if (pdata
->cs_control
)
273 pdata
->cs_control(spi
, !pol
);
276 if (value
== BITBANG_CS_ACTIVE
) {
277 mpc8xxx_spi
->rx_shift
= cs
->rx_shift
;
278 mpc8xxx_spi
->tx_shift
= cs
->tx_shift
;
279 mpc8xxx_spi
->get_rx
= cs
->get_rx
;
280 mpc8xxx_spi
->get_tx
= cs
->get_tx
;
282 mpc8xxx_spi_change_mode(spi
);
284 if (pdata
->cs_control
)
285 pdata
->cs_control(spi
, pol
);
290 mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs
*cs
,
291 struct spi_device
*spi
,
292 struct mpc8xxx_spi
*mpc8xxx_spi
,
297 if (bits_per_word
<= 8) {
298 cs
->get_rx
= mpc8xxx_spi_rx_buf_u8
;
299 cs
->get_tx
= mpc8xxx_spi_tx_buf_u8
;
300 if (mpc8xxx_spi
->flags
& SPI_QE_CPU_MODE
) {
304 } else if (bits_per_word
<= 16) {
305 cs
->get_rx
= mpc8xxx_spi_rx_buf_u16
;
306 cs
->get_tx
= mpc8xxx_spi_tx_buf_u16
;
307 if (mpc8xxx_spi
->flags
& SPI_QE_CPU_MODE
) {
311 } else if (bits_per_word
<= 32) {
312 cs
->get_rx
= mpc8xxx_spi_rx_buf_u32
;
313 cs
->get_tx
= mpc8xxx_spi_tx_buf_u32
;
317 if (mpc8xxx_spi
->flags
& SPI_QE_CPU_MODE
&&
318 spi
->mode
& SPI_LSB_FIRST
) {
320 if (bits_per_word
<= 8)
325 mpc8xxx_spi
->rx_shift
= cs
->rx_shift
;
326 mpc8xxx_spi
->tx_shift
= cs
->tx_shift
;
327 mpc8xxx_spi
->get_rx
= cs
->get_rx
;
328 mpc8xxx_spi
->get_tx
= cs
->get_tx
;
330 return bits_per_word
;
334 mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs
*cs
,
335 struct spi_device
*spi
,
338 /* QE uses Little Endian for words > 8
339 * so transform all words > 8 into 8 bits
340 * Unfortnatly that doesn't work for LSB so
341 * reject these for now */
342 /* Note: 32 bits word, LSB works iff
343 * tfcr/rfcr is set to CPMFCR_GBL */
344 if (spi
->mode
& SPI_LSB_FIRST
&&
347 if (bits_per_word
> 8)
348 return 8; /* pretend its 8 bits */
349 return bits_per_word
;
353 int mpc8xxx_spi_setup_transfer(struct spi_device
*spi
, struct spi_transfer
*t
)
355 struct mpc8xxx_spi
*mpc8xxx_spi
;
359 struct spi_mpc8xxx_cs
*cs
= spi
->controller_state
;
361 mpc8xxx_spi
= spi_master_get_devdata(spi
->master
);
364 bits_per_word
= t
->bits_per_word
;
371 /* spi_transfer level calls that work per-word */
373 bits_per_word
= spi
->bits_per_word
;
375 /* Make sure its a bit width we support [4..16, 32] */
376 if ((bits_per_word
< 4)
377 || ((bits_per_word
> 16) && (bits_per_word
!= 32)))
381 hz
= spi
->max_speed_hz
;
383 if (!(mpc8xxx_spi
->flags
& SPI_CPM_MODE
))
384 bits_per_word
= mspi_apply_cpu_mode_quirks(cs
, spi
,
387 else if (mpc8xxx_spi
->flags
& SPI_QE
)
388 bits_per_word
= mspi_apply_qe_mode_quirks(cs
, spi
,
391 if (bits_per_word
< 0)
392 return bits_per_word
;
394 if (bits_per_word
== 32)
397 bits_per_word
= bits_per_word
- 1;
399 /* mask out bits we are going to set */
400 cs
->hw_mode
&= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
403 cs
->hw_mode
|= SPMODE_LEN(bits_per_word
);
405 if ((mpc8xxx_spi
->spibrg
/ hz
) > 64) {
406 cs
->hw_mode
|= SPMODE_DIV16
;
407 pm
= (mpc8xxx_spi
->spibrg
- 1) / (hz
* 64) + 1;
409 WARN_ONCE(pm
> 16, "%s: Requested speed is too low: %d Hz. "
410 "Will use %d Hz instead.\n", dev_name(&spi
->dev
),
411 hz
, mpc8xxx_spi
->spibrg
/ 1024);
415 pm
= (mpc8xxx_spi
->spibrg
- 1) / (hz
* 4) + 1;
419 cs
->hw_mode
|= SPMODE_PM(pm
);
421 mpc8xxx_spi_change_mode(spi
);
425 static void mpc8xxx_spi_cpm_bufs_start(struct mpc8xxx_spi
*mspi
)
427 struct cpm_buf_desc __iomem
*tx_bd
= mspi
->tx_bd
;
428 struct cpm_buf_desc __iomem
*rx_bd
= mspi
->rx_bd
;
429 unsigned int xfer_len
= min(mspi
->count
, SPI_MRBLR
);
430 unsigned int xfer_ofs
;
432 xfer_ofs
= mspi
->xfer_in_progress
->len
- mspi
->count
;
434 out_be32(&rx_bd
->cbd_bufaddr
, mspi
->rx_dma
+ xfer_ofs
);
435 out_be16(&rx_bd
->cbd_datlen
, 0);
436 out_be16(&rx_bd
->cbd_sc
, BD_SC_EMPTY
| BD_SC_INTRPT
| BD_SC_WRAP
);
438 out_be32(&tx_bd
->cbd_bufaddr
, mspi
->tx_dma
+ xfer_ofs
);
439 out_be16(&tx_bd
->cbd_datlen
, xfer_len
);
440 out_be16(&tx_bd
->cbd_sc
, BD_SC_READY
| BD_SC_INTRPT
| BD_SC_WRAP
|
444 mpc8xxx_spi_write_reg(&mspi
->base
->command
, SPCOM_STR
);
447 static int mpc8xxx_spi_cpm_bufs(struct mpc8xxx_spi
*mspi
,
448 struct spi_transfer
*t
, bool is_dma_mapped
)
450 struct device
*dev
= mspi
->dev
;
453 mspi
->map_tx_dma
= 0;
454 mspi
->map_rx_dma
= 0;
456 mspi
->map_tx_dma
= 1;
457 mspi
->map_rx_dma
= 1;
461 mspi
->tx_dma
= mspi
->dma_dummy_tx
;
462 mspi
->map_tx_dma
= 0;
466 mspi
->rx_dma
= mspi
->dma_dummy_rx
;
467 mspi
->map_rx_dma
= 0;
470 if (mspi
->map_tx_dma
) {
471 void *nonconst_tx
= (void *)mspi
->tx
; /* shut up gcc */
473 mspi
->tx_dma
= dma_map_single(dev
, nonconst_tx
, t
->len
,
475 if (dma_mapping_error(dev
, mspi
->tx_dma
)) {
476 dev_err(dev
, "unable to map tx dma\n");
479 } else if (t
->tx_buf
) {
480 mspi
->tx_dma
= t
->tx_dma
;
483 if (mspi
->map_rx_dma
) {
484 mspi
->rx_dma
= dma_map_single(dev
, mspi
->rx
, t
->len
,
486 if (dma_mapping_error(dev
, mspi
->rx_dma
)) {
487 dev_err(dev
, "unable to map rx dma\n");
490 } else if (t
->rx_buf
) {
491 mspi
->rx_dma
= t
->rx_dma
;
495 mpc8xxx_spi_write_reg(&mspi
->base
->mask
, SPIE_RXB
);
497 mspi
->xfer_in_progress
= t
;
498 mspi
->count
= t
->len
;
500 /* start CPM transfers */
501 mpc8xxx_spi_cpm_bufs_start(mspi
);
506 if (mspi
->map_tx_dma
)
507 dma_unmap_single(dev
, mspi
->tx_dma
, t
->len
, DMA_TO_DEVICE
);
511 static void mpc8xxx_spi_cpm_bufs_complete(struct mpc8xxx_spi
*mspi
)
513 struct device
*dev
= mspi
->dev
;
514 struct spi_transfer
*t
= mspi
->xfer_in_progress
;
516 if (mspi
->map_tx_dma
)
517 dma_unmap_single(dev
, mspi
->tx_dma
, t
->len
, DMA_TO_DEVICE
);
518 if (mspi
->map_rx_dma
)
519 dma_unmap_single(dev
, mspi
->rx_dma
, t
->len
, DMA_FROM_DEVICE
);
520 mspi
->xfer_in_progress
= NULL
;
523 static int mpc8xxx_spi_cpu_bufs(struct mpc8xxx_spi
*mspi
,
524 struct spi_transfer
*t
, unsigned int len
)
531 mpc8xxx_spi_write_reg(&mspi
->base
->mask
, SPIM_NE
);
534 word
= mspi
->get_tx(mspi
);
535 mpc8xxx_spi_write_reg(&mspi
->base
->transmit
, word
);
540 static int mpc8xxx_spi_bufs(struct spi_device
*spi
, struct spi_transfer
*t
,
543 struct mpc8xxx_spi
*mpc8xxx_spi
= spi_master_get_devdata(spi
->master
);
544 unsigned int len
= t
->len
;
548 bits_per_word
= spi
->bits_per_word
;
549 if (t
->bits_per_word
)
550 bits_per_word
= t
->bits_per_word
;
552 if (bits_per_word
> 8) {
553 /* invalid length? */
558 if (bits_per_word
> 16) {
559 /* invalid length? */
565 mpc8xxx_spi
->tx
= t
->tx_buf
;
566 mpc8xxx_spi
->rx
= t
->rx_buf
;
568 INIT_COMPLETION(mpc8xxx_spi
->done
);
570 if (mpc8xxx_spi
->flags
& SPI_CPM_MODE
)
571 ret
= mpc8xxx_spi_cpm_bufs(mpc8xxx_spi
, t
, is_dma_mapped
);
573 ret
= mpc8xxx_spi_cpu_bufs(mpc8xxx_spi
, t
, len
);
577 wait_for_completion(&mpc8xxx_spi
->done
);
579 /* disable rx ints */
580 mpc8xxx_spi_write_reg(&mpc8xxx_spi
->base
->mask
, 0);
582 if (mpc8xxx_spi
->flags
& SPI_CPM_MODE
)
583 mpc8xxx_spi_cpm_bufs_complete(mpc8xxx_spi
);
585 return mpc8xxx_spi
->count
;
588 static void mpc8xxx_spi_do_one_msg(struct spi_message
*m
)
590 struct spi_device
*spi
= m
->spi
;
591 struct spi_transfer
*t
;
592 unsigned int cs_change
;
593 const int nsecs
= 50;
598 list_for_each_entry(t
, &m
->transfers
, transfer_list
) {
599 if (t
->bits_per_word
|| t
->speed_hz
) {
600 /* Don't allow changes if CS is active */
604 status
= mpc8xxx_spi_setup_transfer(spi
, t
);
610 mpc8xxx_spi_chipselect(spi
, BITBANG_CS_ACTIVE
);
613 cs_change
= t
->cs_change
;
615 status
= mpc8xxx_spi_bufs(spi
, t
, m
->is_dma_mapped
);
620 m
->actual_length
+= t
->len
;
623 udelay(t
->delay_usecs
);
627 mpc8xxx_spi_chipselect(spi
, BITBANG_CS_INACTIVE
);
633 m
->complete(m
->context
);
635 if (status
|| !cs_change
) {
637 mpc8xxx_spi_chipselect(spi
, BITBANG_CS_INACTIVE
);
640 mpc8xxx_spi_setup_transfer(spi
, NULL
);
643 static void mpc8xxx_spi_work(struct work_struct
*work
)
645 struct mpc8xxx_spi
*mpc8xxx_spi
= container_of(work
, struct mpc8xxx_spi
,
648 spin_lock_irq(&mpc8xxx_spi
->lock
);
649 while (!list_empty(&mpc8xxx_spi
->queue
)) {
650 struct spi_message
*m
= container_of(mpc8xxx_spi
->queue
.next
,
651 struct spi_message
, queue
);
653 list_del_init(&m
->queue
);
654 spin_unlock_irq(&mpc8xxx_spi
->lock
);
656 mpc8xxx_spi_do_one_msg(m
);
658 spin_lock_irq(&mpc8xxx_spi
->lock
);
660 spin_unlock_irq(&mpc8xxx_spi
->lock
);
663 static int mpc8xxx_spi_setup(struct spi_device
*spi
)
665 struct mpc8xxx_spi
*mpc8xxx_spi
;
668 struct spi_mpc8xxx_cs
*cs
= spi
->controller_state
;
670 if (!spi
->max_speed_hz
)
674 cs
= kzalloc(sizeof *cs
, GFP_KERNEL
);
677 spi
->controller_state
= cs
;
679 mpc8xxx_spi
= spi_master_get_devdata(spi
->master
);
681 hw_mode
= cs
->hw_mode
; /* Save original settings */
682 cs
->hw_mode
= mpc8xxx_spi_read_reg(&mpc8xxx_spi
->base
->mode
);
683 /* mask out bits we are going to set */
684 cs
->hw_mode
&= ~(SPMODE_CP_BEGIN_EDGECLK
| SPMODE_CI_INACTIVEHIGH
685 | SPMODE_REV
| SPMODE_LOOP
);
687 if (spi
->mode
& SPI_CPHA
)
688 cs
->hw_mode
|= SPMODE_CP_BEGIN_EDGECLK
;
689 if (spi
->mode
& SPI_CPOL
)
690 cs
->hw_mode
|= SPMODE_CI_INACTIVEHIGH
;
691 if (!(spi
->mode
& SPI_LSB_FIRST
))
692 cs
->hw_mode
|= SPMODE_REV
;
693 if (spi
->mode
& SPI_LOOP
)
694 cs
->hw_mode
|= SPMODE_LOOP
;
696 retval
= mpc8xxx_spi_setup_transfer(spi
, NULL
);
698 cs
->hw_mode
= hw_mode
; /* Restore settings */
704 static void mpc8xxx_spi_cpm_irq(struct mpc8xxx_spi
*mspi
, u32 events
)
708 dev_dbg(mspi
->dev
, "%s: bd datlen %d, count %d\n", __func__
,
709 in_be16(&mspi
->rx_bd
->cbd_datlen
), mspi
->count
);
711 len
= in_be16(&mspi
->rx_bd
->cbd_datlen
);
712 if (len
> mspi
->count
) {
717 /* Clear the events */
718 mpc8xxx_spi_write_reg(&mspi
->base
->event
, events
);
722 mpc8xxx_spi_cpm_bufs_start(mspi
);
724 complete(&mspi
->done
);
727 static void mpc8xxx_spi_cpu_irq(struct mpc8xxx_spi
*mspi
, u32 events
)
729 /* We need handle RX first */
730 if (events
& SPIE_NE
) {
731 u32 rx_data
= mpc8xxx_spi_read_reg(&mspi
->base
->receive
);
734 mspi
->get_rx(rx_data
, mspi
);
737 if ((events
& SPIE_NF
) == 0)
738 /* spin until TX is done */
740 mpc8xxx_spi_read_reg(&mspi
->base
->event
)) &
744 /* Clear the events */
745 mpc8xxx_spi_write_reg(&mspi
->base
->event
, events
);
749 u32 word
= mspi
->get_tx(mspi
);
751 mpc8xxx_spi_write_reg(&mspi
->base
->transmit
, word
);
753 complete(&mspi
->done
);
757 static irqreturn_t
mpc8xxx_spi_irq(s32 irq
, void *context_data
)
759 struct mpc8xxx_spi
*mspi
= context_data
;
760 irqreturn_t ret
= IRQ_NONE
;
763 /* Get interrupt events(tx/rx) */
764 events
= mpc8xxx_spi_read_reg(&mspi
->base
->event
);
768 dev_dbg(mspi
->dev
, "%s: events %x\n", __func__
, events
);
770 if (mspi
->flags
& SPI_CPM_MODE
)
771 mpc8xxx_spi_cpm_irq(mspi
, events
);
773 mpc8xxx_spi_cpu_irq(mspi
, events
);
778 static int mpc8xxx_spi_transfer(struct spi_device
*spi
,
779 struct spi_message
*m
)
781 struct mpc8xxx_spi
*mpc8xxx_spi
= spi_master_get_devdata(spi
->master
);
784 m
->actual_length
= 0;
785 m
->status
= -EINPROGRESS
;
787 spin_lock_irqsave(&mpc8xxx_spi
->lock
, flags
);
788 list_add_tail(&m
->queue
, &mpc8xxx_spi
->queue
);
789 queue_work(mpc8xxx_spi
->workqueue
, &mpc8xxx_spi
->work
);
790 spin_unlock_irqrestore(&mpc8xxx_spi
->lock
, flags
);
796 static void mpc8xxx_spi_cleanup(struct spi_device
*spi
)
798 kfree(spi
->controller_state
);
801 static void *mpc8xxx_spi_alloc_dummy_rx(void)
803 mutex_lock(&mpc8xxx_dummy_rx_lock
);
805 if (!mpc8xxx_dummy_rx
)
806 mpc8xxx_dummy_rx
= kmalloc(SPI_MRBLR
, GFP_KERNEL
);
807 if (mpc8xxx_dummy_rx
)
808 mpc8xxx_dummy_rx_refcnt
++;
810 mutex_unlock(&mpc8xxx_dummy_rx_lock
);
812 return mpc8xxx_dummy_rx
;
815 static void mpc8xxx_spi_free_dummy_rx(void)
817 mutex_lock(&mpc8xxx_dummy_rx_lock
);
819 switch (mpc8xxx_dummy_rx_refcnt
) {
824 kfree(mpc8xxx_dummy_rx
);
825 mpc8xxx_dummy_rx
= NULL
;
828 mpc8xxx_dummy_rx_refcnt
--;
832 mutex_unlock(&mpc8xxx_dummy_rx_lock
);
835 static unsigned long mpc8xxx_spi_cpm_get_pram(struct mpc8xxx_spi
*mspi
)
837 struct device
*dev
= mspi
->dev
;
838 struct device_node
*np
= dev
->of_node
;
841 unsigned long spi_base_ofs
;
842 unsigned long pram_ofs
= -ENOMEM
;
844 /* Can't use of_address_to_resource(), QE muram isn't at 0. */
845 iprop
= of_get_property(np
, "reg", &size
);
847 /* QE with a fixed pram location? */
848 if (mspi
->flags
& SPI_QE
&& iprop
&& size
== sizeof(*iprop
) * 4)
849 return cpm_muram_alloc_fixed(iprop
[2], SPI_PRAM_SIZE
);
851 /* QE but with a dynamic pram location? */
852 if (mspi
->flags
& SPI_QE
) {
853 pram_ofs
= cpm_muram_alloc(SPI_PRAM_SIZE
, 64);
854 qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE
, mspi
->subblock
,
855 QE_CR_PROTOCOL_UNSPECIFIED
, pram_ofs
);
859 /* CPM1 and CPM2 pram must be at a fixed addr. */
860 if (!iprop
|| size
!= sizeof(*iprop
) * 4)
863 spi_base_ofs
= cpm_muram_alloc_fixed(iprop
[2], 2);
864 if (IS_ERR_VALUE(spi_base_ofs
))
867 if (mspi
->flags
& SPI_CPM2
) {
868 pram_ofs
= cpm_muram_alloc(SPI_PRAM_SIZE
, 64);
869 if (!IS_ERR_VALUE(pram_ofs
)) {
870 u16 __iomem
*spi_base
= cpm_muram_addr(spi_base_ofs
);
872 out_be16(spi_base
, pram_ofs
);
875 struct spi_pram __iomem
*pram
= cpm_muram_addr(spi_base_ofs
);
876 u16 rpbase
= in_be16(&pram
->rpbase
);
878 /* Microcode relocation patch applied? */
885 cpm_muram_free(spi_base_ofs
);
889 static int mpc8xxx_spi_cpm_init(struct mpc8xxx_spi
*mspi
)
891 struct device
*dev
= mspi
->dev
;
892 struct device_node
*np
= dev
->of_node
;
895 unsigned long pram_ofs
;
896 unsigned long bds_ofs
;
898 if (!(mspi
->flags
& SPI_CPM_MODE
))
901 if (!mpc8xxx_spi_alloc_dummy_rx())
904 if (mspi
->flags
& SPI_QE
) {
905 iprop
= of_get_property(np
, "cell-index", &size
);
906 if (iprop
&& size
== sizeof(*iprop
))
907 mspi
->subblock
= *iprop
;
909 switch (mspi
->subblock
) {
911 dev_warn(dev
, "cell-index unspecified, assuming SPI1");
914 mspi
->subblock
= QE_CR_SUBBLOCK_SPI1
;
917 mspi
->subblock
= QE_CR_SUBBLOCK_SPI2
;
922 pram_ofs
= mpc8xxx_spi_cpm_get_pram(mspi
);
923 if (IS_ERR_VALUE(pram_ofs
)) {
924 dev_err(dev
, "can't allocate spi parameter ram\n");
928 bds_ofs
= cpm_muram_alloc(sizeof(*mspi
->tx_bd
) +
929 sizeof(*mspi
->rx_bd
), 8);
930 if (IS_ERR_VALUE(bds_ofs
)) {
931 dev_err(dev
, "can't allocate bds\n");
935 mspi
->dma_dummy_tx
= dma_map_single(dev
, empty_zero_page
, PAGE_SIZE
,
937 if (dma_mapping_error(dev
, mspi
->dma_dummy_tx
)) {
938 dev_err(dev
, "unable to map dummy tx buffer\n");
942 mspi
->dma_dummy_rx
= dma_map_single(dev
, mpc8xxx_dummy_rx
, SPI_MRBLR
,
944 if (dma_mapping_error(dev
, mspi
->dma_dummy_rx
)) {
945 dev_err(dev
, "unable to map dummy rx buffer\n");
949 mspi
->pram
= cpm_muram_addr(pram_ofs
);
951 mspi
->tx_bd
= cpm_muram_addr(bds_ofs
);
952 mspi
->rx_bd
= cpm_muram_addr(bds_ofs
+ sizeof(*mspi
->tx_bd
));
954 /* Initialize parameter ram. */
955 out_be16(&mspi
->pram
->tbase
, cpm_muram_offset(mspi
->tx_bd
));
956 out_be16(&mspi
->pram
->rbase
, cpm_muram_offset(mspi
->rx_bd
));
957 out_8(&mspi
->pram
->tfcr
, CPMFCR_EB
| CPMFCR_GBL
);
958 out_8(&mspi
->pram
->rfcr
, CPMFCR_EB
| CPMFCR_GBL
);
959 out_be16(&mspi
->pram
->mrblr
, SPI_MRBLR
);
960 out_be32(&mspi
->pram
->rstate
, 0);
961 out_be32(&mspi
->pram
->rdp
, 0);
962 out_be16(&mspi
->pram
->rbptr
, 0);
963 out_be16(&mspi
->pram
->rbc
, 0);
964 out_be32(&mspi
->pram
->rxtmp
, 0);
965 out_be32(&mspi
->pram
->tstate
, 0);
966 out_be32(&mspi
->pram
->tdp
, 0);
967 out_be16(&mspi
->pram
->tbptr
, 0);
968 out_be16(&mspi
->pram
->tbc
, 0);
969 out_be32(&mspi
->pram
->txtmp
, 0);
974 dma_unmap_single(dev
, mspi
->dma_dummy_tx
, PAGE_SIZE
, DMA_TO_DEVICE
);
976 cpm_muram_free(bds_ofs
);
978 cpm_muram_free(pram_ofs
);
980 mpc8xxx_spi_free_dummy_rx();
984 static void mpc8xxx_spi_cpm_free(struct mpc8xxx_spi
*mspi
)
986 struct device
*dev
= mspi
->dev
;
988 dma_unmap_single(dev
, mspi
->dma_dummy_rx
, SPI_MRBLR
, DMA_FROM_DEVICE
);
989 dma_unmap_single(dev
, mspi
->dma_dummy_tx
, PAGE_SIZE
, DMA_TO_DEVICE
);
990 cpm_muram_free(cpm_muram_offset(mspi
->tx_bd
));
991 cpm_muram_free(cpm_muram_offset(mspi
->pram
));
992 mpc8xxx_spi_free_dummy_rx();
995 static const char *mpc8xxx_spi_strmode(unsigned int flags
)
997 if (flags
& SPI_QE_CPU_MODE
) {
999 } else if (flags
& SPI_CPM_MODE
) {
1002 else if (flags
& SPI_CPM2
)
1010 static struct spi_master
* __devinit
1011 mpc8xxx_spi_probe(struct device
*dev
, struct resource
*mem
, unsigned int irq
)
1013 struct fsl_spi_platform_data
*pdata
= dev
->platform_data
;
1014 struct spi_master
*master
;
1015 struct mpc8xxx_spi
*mpc8xxx_spi
;
1019 master
= spi_alloc_master(dev
, sizeof(struct mpc8xxx_spi
));
1020 if (master
== NULL
) {
1025 dev_set_drvdata(dev
, master
);
1027 /* the spi->mode bits understood by this driver: */
1028 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_CS_HIGH
1029 | SPI_LSB_FIRST
| SPI_LOOP
;
1031 master
->setup
= mpc8xxx_spi_setup
;
1032 master
->transfer
= mpc8xxx_spi_transfer
;
1033 master
->cleanup
= mpc8xxx_spi_cleanup
;
1035 mpc8xxx_spi
= spi_master_get_devdata(master
);
1036 mpc8xxx_spi
->dev
= dev
;
1037 mpc8xxx_spi
->get_rx
= mpc8xxx_spi_rx_buf_u8
;
1038 mpc8xxx_spi
->get_tx
= mpc8xxx_spi_tx_buf_u8
;
1039 mpc8xxx_spi
->flags
= pdata
->flags
;
1040 mpc8xxx_spi
->spibrg
= pdata
->sysclk
;
1042 ret
= mpc8xxx_spi_cpm_init(mpc8xxx_spi
);
1046 mpc8xxx_spi
->rx_shift
= 0;
1047 mpc8xxx_spi
->tx_shift
= 0;
1048 if (mpc8xxx_spi
->flags
& SPI_QE_CPU_MODE
) {
1049 mpc8xxx_spi
->rx_shift
= 16;
1050 mpc8xxx_spi
->tx_shift
= 24;
1053 init_completion(&mpc8xxx_spi
->done
);
1055 mpc8xxx_spi
->base
= ioremap(mem
->start
, resource_size(mem
));
1056 if (mpc8xxx_spi
->base
== NULL
) {
1061 mpc8xxx_spi
->irq
= irq
;
1063 /* Register for SPI Interrupt */
1064 ret
= request_irq(mpc8xxx_spi
->irq
, mpc8xxx_spi_irq
,
1065 0, "mpc8xxx_spi", mpc8xxx_spi
);
1070 master
->bus_num
= pdata
->bus_num
;
1071 master
->num_chipselect
= pdata
->max_chipselect
;
1073 /* SPI controller initializations */
1074 mpc8xxx_spi_write_reg(&mpc8xxx_spi
->base
->mode
, 0);
1075 mpc8xxx_spi_write_reg(&mpc8xxx_spi
->base
->mask
, 0);
1076 mpc8xxx_spi_write_reg(&mpc8xxx_spi
->base
->command
, 0);
1077 mpc8xxx_spi_write_reg(&mpc8xxx_spi
->base
->event
, 0xffffffff);
1079 /* Enable SPI interface */
1080 regval
= pdata
->initial_spmode
| SPMODE_INIT_VAL
| SPMODE_ENABLE
;
1081 if (mpc8xxx_spi
->flags
& SPI_QE_CPU_MODE
)
1082 regval
|= SPMODE_OP
;
1084 mpc8xxx_spi_write_reg(&mpc8xxx_spi
->base
->mode
, regval
);
1085 spin_lock_init(&mpc8xxx_spi
->lock
);
1086 init_completion(&mpc8xxx_spi
->done
);
1087 INIT_WORK(&mpc8xxx_spi
->work
, mpc8xxx_spi_work
);
1088 INIT_LIST_HEAD(&mpc8xxx_spi
->queue
);
1090 mpc8xxx_spi
->workqueue
= create_singlethread_workqueue(
1091 dev_name(master
->dev
.parent
));
1092 if (mpc8xxx_spi
->workqueue
== NULL
) {
1097 ret
= spi_register_master(master
);
1101 dev_info(dev
, "at 0x%p (irq = %d), %s mode\n", mpc8xxx_spi
->base
,
1102 mpc8xxx_spi
->irq
, mpc8xxx_spi_strmode(mpc8xxx_spi
->flags
));
1107 destroy_workqueue(mpc8xxx_spi
->workqueue
);
1109 free_irq(mpc8xxx_spi
->irq
, mpc8xxx_spi
);
1111 iounmap(mpc8xxx_spi
->base
);
1113 mpc8xxx_spi_cpm_free(mpc8xxx_spi
);
1115 spi_master_put(master
);
1117 return ERR_PTR(ret
);
1120 static int __devexit
mpc8xxx_spi_remove(struct device
*dev
)
1122 struct mpc8xxx_spi
*mpc8xxx_spi
;
1123 struct spi_master
*master
;
1125 master
= dev_get_drvdata(dev
);
1126 mpc8xxx_spi
= spi_master_get_devdata(master
);
1128 flush_workqueue(mpc8xxx_spi
->workqueue
);
1129 destroy_workqueue(mpc8xxx_spi
->workqueue
);
1130 spi_unregister_master(master
);
1132 free_irq(mpc8xxx_spi
->irq
, mpc8xxx_spi
);
1133 iounmap(mpc8xxx_spi
->base
);
1134 mpc8xxx_spi_cpm_free(mpc8xxx_spi
);
1139 struct mpc8xxx_spi_probe_info
{
1140 struct fsl_spi_platform_data pdata
;
1145 static struct mpc8xxx_spi_probe_info
*
1146 to_of_pinfo(struct fsl_spi_platform_data
*pdata
)
1148 return container_of(pdata
, struct mpc8xxx_spi_probe_info
, pdata
);
1151 static void mpc8xxx_spi_cs_control(struct spi_device
*spi
, bool on
)
1153 struct device
*dev
= spi
->dev
.parent
;
1154 struct mpc8xxx_spi_probe_info
*pinfo
= to_of_pinfo(dev
->platform_data
);
1155 u16 cs
= spi
->chip_select
;
1156 int gpio
= pinfo
->gpios
[cs
];
1157 bool alow
= pinfo
->alow_flags
[cs
];
1159 gpio_set_value(gpio
, on
^ alow
);
1162 static int of_mpc8xxx_spi_get_chipselects(struct device
*dev
)
1164 struct device_node
*np
= dev
->of_node
;
1165 struct fsl_spi_platform_data
*pdata
= dev
->platform_data
;
1166 struct mpc8xxx_spi_probe_info
*pinfo
= to_of_pinfo(pdata
);
1167 unsigned int ngpios
;
1171 ngpios
= of_gpio_count(np
);
1174 * SPI w/o chip-select line. One SPI device is still permitted
1177 pdata
->max_chipselect
= 1;
1181 pinfo
->gpios
= kmalloc(ngpios
* sizeof(*pinfo
->gpios
), GFP_KERNEL
);
1184 memset(pinfo
->gpios
, -1, ngpios
* sizeof(*pinfo
->gpios
));
1186 pinfo
->alow_flags
= kzalloc(ngpios
* sizeof(*pinfo
->alow_flags
),
1188 if (!pinfo
->alow_flags
) {
1190 goto err_alloc_flags
;
1193 for (; i
< ngpios
; i
++) {
1195 enum of_gpio_flags flags
;
1197 gpio
= of_get_gpio_flags(np
, i
, &flags
);
1198 if (!gpio_is_valid(gpio
)) {
1199 dev_err(dev
, "invalid gpio #%d: %d\n", i
, gpio
);
1204 ret
= gpio_request(gpio
, dev_name(dev
));
1206 dev_err(dev
, "can't request gpio #%d: %d\n", i
, ret
);
1210 pinfo
->gpios
[i
] = gpio
;
1211 pinfo
->alow_flags
[i
] = flags
& OF_GPIO_ACTIVE_LOW
;
1213 ret
= gpio_direction_output(pinfo
->gpios
[i
],
1214 pinfo
->alow_flags
[i
]);
1216 dev_err(dev
, "can't set output direction for gpio "
1217 "#%d: %d\n", i
, ret
);
1222 pdata
->max_chipselect
= ngpios
;
1223 pdata
->cs_control
= mpc8xxx_spi_cs_control
;
1229 if (gpio_is_valid(pinfo
->gpios
[i
]))
1230 gpio_free(pinfo
->gpios
[i
]);
1234 kfree(pinfo
->alow_flags
);
1235 pinfo
->alow_flags
= NULL
;
1237 kfree(pinfo
->gpios
);
1238 pinfo
->gpios
= NULL
;
1242 static int of_mpc8xxx_spi_free_chipselects(struct device
*dev
)
1244 struct fsl_spi_platform_data
*pdata
= dev
->platform_data
;
1245 struct mpc8xxx_spi_probe_info
*pinfo
= to_of_pinfo(pdata
);
1251 for (i
= 0; i
< pdata
->max_chipselect
; i
++) {
1252 if (gpio_is_valid(pinfo
->gpios
[i
]))
1253 gpio_free(pinfo
->gpios
[i
]);
1256 kfree(pinfo
->gpios
);
1257 kfree(pinfo
->alow_flags
);
1261 static int __devinit
of_mpc8xxx_spi_probe(struct of_device
*ofdev
,
1262 const struct of_device_id
*ofid
)
1264 struct device
*dev
= &ofdev
->dev
;
1265 struct device_node
*np
= ofdev
->dev
.of_node
;
1266 struct mpc8xxx_spi_probe_info
*pinfo
;
1267 struct fsl_spi_platform_data
*pdata
;
1268 struct spi_master
*master
;
1269 struct resource mem
;
1270 struct resource irq
;
1274 pinfo
= kzalloc(sizeof(*pinfo
), GFP_KERNEL
);
1278 pdata
= &pinfo
->pdata
;
1279 dev
->platform_data
= pdata
;
1281 /* Allocate bus num dynamically. */
1282 pdata
->bus_num
= -1;
1284 /* SPI controller is either clocked from QE or SoC clock. */
1285 pdata
->sysclk
= get_brgfreq();
1286 if (pdata
->sysclk
== -1) {
1287 pdata
->sysclk
= fsl_get_sys_freq();
1288 if (pdata
->sysclk
== -1) {
1294 prop
= of_get_property(np
, "mode", NULL
);
1295 if (prop
&& !strcmp(prop
, "cpu-qe"))
1296 pdata
->flags
= SPI_QE_CPU_MODE
;
1297 else if (prop
&& !strcmp(prop
, "qe"))
1298 pdata
->flags
= SPI_CPM_MODE
| SPI_QE
;
1299 else if (of_device_is_compatible(np
, "fsl,cpm2-spi"))
1300 pdata
->flags
= SPI_CPM_MODE
| SPI_CPM2
;
1301 else if (of_device_is_compatible(np
, "fsl,cpm1-spi"))
1302 pdata
->flags
= SPI_CPM_MODE
| SPI_CPM1
;
1304 ret
= of_mpc8xxx_spi_get_chipselects(dev
);
1308 ret
= of_address_to_resource(np
, 0, &mem
);
1312 ret
= of_irq_to_resource(np
, 0, &irq
);
1318 master
= mpc8xxx_spi_probe(dev
, &mem
, irq
.start
);
1319 if (IS_ERR(master
)) {
1320 ret
= PTR_ERR(master
);
1324 of_register_spi_devices(master
, np
);
1329 of_mpc8xxx_spi_free_chipselects(dev
);
1335 static int __devexit
of_mpc8xxx_spi_remove(struct of_device
*ofdev
)
1339 ret
= mpc8xxx_spi_remove(&ofdev
->dev
);
1342 of_mpc8xxx_spi_free_chipselects(&ofdev
->dev
);
1346 static const struct of_device_id of_mpc8xxx_spi_match
[] = {
1347 { .compatible
= "fsl,spi" },
1350 MODULE_DEVICE_TABLE(of
, of_mpc8xxx_spi_match
);
1352 static struct of_platform_driver of_mpc8xxx_spi_driver
= {
1354 .name
= "mpc8xxx_spi",
1355 .owner
= THIS_MODULE
,
1356 .of_match_table
= of_mpc8xxx_spi_match
,
1358 .probe
= of_mpc8xxx_spi_probe
,
1359 .remove
= __devexit_p(of_mpc8xxx_spi_remove
),
1362 #ifdef CONFIG_MPC832x_RDB
1365 * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
1366 * only. The driver should go away soon, since newer MPC8323E-RDB's device
1367 * tree can work with OpenFirmware driver. But for now we support old trees
1370 static int __devinit
plat_mpc8xxx_spi_probe(struct platform_device
*pdev
)
1372 struct resource
*mem
;
1374 struct spi_master
*master
;
1376 if (!pdev
->dev
.platform_data
)
1379 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1383 irq
= platform_get_irq(pdev
, 0);
1387 master
= mpc8xxx_spi_probe(&pdev
->dev
, mem
, irq
);
1389 return PTR_ERR(master
);
1393 static int __devexit
plat_mpc8xxx_spi_remove(struct platform_device
*pdev
)
1395 return mpc8xxx_spi_remove(&pdev
->dev
);
1398 MODULE_ALIAS("platform:mpc8xxx_spi");
1399 static struct platform_driver mpc8xxx_spi_driver
= {
1400 .probe
= plat_mpc8xxx_spi_probe
,
1401 .remove
= __devexit_p(plat_mpc8xxx_spi_remove
),
1403 .name
= "mpc8xxx_spi",
1404 .owner
= THIS_MODULE
,
1408 static bool legacy_driver_failed
;
1410 static void __init
legacy_driver_register(void)
1412 legacy_driver_failed
= platform_driver_register(&mpc8xxx_spi_driver
);
1415 static void __exit
legacy_driver_unregister(void)
1417 if (legacy_driver_failed
)
1419 platform_driver_unregister(&mpc8xxx_spi_driver
);
1422 static void __init
legacy_driver_register(void) {}
1423 static void __exit
legacy_driver_unregister(void) {}
1424 #endif /* CONFIG_MPC832x_RDB */
1426 static int __init
mpc8xxx_spi_init(void)
1428 legacy_driver_register();
1429 return of_register_platform_driver(&of_mpc8xxx_spi_driver
);
1432 static void __exit
mpc8xxx_spi_exit(void)
1434 of_unregister_platform_driver(&of_mpc8xxx_spi_driver
);
1435 legacy_driver_unregister();
1438 module_init(mpc8xxx_spi_init
);
1439 module_exit(mpc8xxx_spi_exit
);
1441 MODULE_AUTHOR("Kumar Gala");
1442 MODULE_DESCRIPTION("Simple MPC8xxx SPI Driver");
1443 MODULE_LICENSE("GPL");