1 From 771f4741de2cd8a103d96cd1e8281926448ff56d Mon Sep 17 00:00:00 2001
2 From: Kurt Mahan <kmahan@freescale.com>
3 Date: Tue, 15 Jul 2008 11:44:08 -0600
4 Subject: [PATCH] Fix DSPI driver.
6 Update to latest Matt Coldfire driver. Fix timing issues with
7 M547x/M548x END-OF-QUEUE notification.
9 LTIBName: mcfv4e-dspi-update
10 Signed-off-by: Kurt Mahan <kmahan@freescale.com>
12 arch/m68k/coldfire/m547x_8x-devices.c | 35 +-
13 drivers/spi/Kconfig | 6 +
14 drivers/spi/Makefile | 2 +-
15 drivers/spi/dspi_mcf.c | 1217 +++++++++++++++++++++++++++++++++
16 include/asm-m68k/mcfdspi.h | 48 ++
17 5 files changed, 1296 insertions(+), 12 deletions(-)
18 create mode 100644 drivers/spi/dspi_mcf.c
19 create mode 100644 include/asm-m68k/mcfdspi.h
21 --- a/arch/m68k/coldfire/m547x_8x-devices.c
22 +++ b/arch/m68k/coldfire/m547x_8x-devices.c
26 /* number of supported SPI selects */
27 -#define SPI_NUM_CHIPSELECTS 4
28 +#define SPI_NUM_CHIPSELECTS 8
30 void coldfire_spi_cs_control(u8 cs, u8 command)
32 /* nothing special required */
35 +#if defined(CONFIG_SPI_SPIDEV) || defined(CONFIG_SPI_SPIDEV_MODULE)
36 +static struct coldfire_spi_chip spidev_chip_info = {
41 static struct spi_board_info spi_board_info[] = {
43 +#if defined(CONFIG_SPI_SPIDEV) || defined(CONFIG_SPI_SPIDEV_MODULE)
45 + .modalias = "spidev",
46 + .max_speed_hz = 16000000, /* max clk (SCK) speed in HZ */
48 + .chip_select = 0, /* CS0 */
49 + .controller_data = &spidev_chip_info,
54 static int spi_irq_list[] = {
55 - /* IRQ, ICR Offset, ICR Val, Mask */
56 - 64 + ISC_DSPI_OVRFW, 0, 0, 0,
57 - 64 + ISC_DSPI_RFOF, 0, 0, 0,
58 - 64 + ISC_DSPI_RFDF, 0, 0, 0,
59 - 64 + ISC_DSPI_TFUF, 0, 0, 0,
60 - 64 + ISC_DSPI_TCF, 0, 0, 0,
61 - 64 + ISC_DSPI_TFFF, 0, 0, 0,
62 - 64 + ISC_DSPI_EOQF, 0, 0, 0,
63 + /* IRQ, ICR Offset, ICR Val,Mask */
64 + 64 + ISC_DSPI_OVRFW, ISC_DSPI_OVRFW, 0x18, 0,
65 + 64 + ISC_DSPI_RFOF, ISC_DSPI_RFOF, 0x18, 0,
66 + 64 + ISC_DSPI_RFDF, ISC_DSPI_RFDF, 0x18, 0,
67 + 64 + ISC_DSPI_TFUF, ISC_DSPI_TFUF, 0x18, 0,
68 + 64 + ISC_DSPI_TCF, ISC_DSPI_TCF, 0x18, 0,
69 + 64 + ISC_DSPI_TFFF, ISC_DSPI_TFFF, 0x18, 0,
70 + 64 + ISC_DSPI_EOQF, ISC_DSPI_EOQF, 0x18, 0,
74 @@ -120,7 +134,6 @@ static int __init m547x_8x_spi_init(void
76 retval = platform_device_register(&coldfire_spi);
78 - printk(KERN_ERR "SPI-m547x_8x: platform_device_register failed with code=%d\n", retval);
82 --- a/drivers/spi/Kconfig
83 +++ b/drivers/spi/Kconfig
84 @@ -130,6 +130,12 @@ config SPI_COLDFIRE
85 Tested with the 5282 processor, but should also work with other
89 + tristate "Coldfire DSPI"
90 + depends on SPI_MASTER && COLDFIRE
92 + SPI driver for Coldfire DSPI driver only.
94 config SPI_COLDFIRE_DSPI_EDMA
95 boolean "Coldfire DSPI master driver uses eDMA"
96 depends on SPI_MASTER && COLDFIRE && SPI_COLDFIRE && EXPERIMENTAL && COLDFIRE_EDMA
97 --- a/drivers/spi/Makefile
98 +++ b/drivers/spi/Makefile
99 @@ -19,7 +19,7 @@ obj-$(CONFIG_SPI_BITBANG) += spi_bitban
100 obj-$(CONFIG_SPI_AU1550) += au1550_spi.o
101 obj-$(CONFIG_SPI_BUTTERFLY) += spi_butterfly.o
102 # obj-$(CONFIG_SPI_COLDFIRE) += spi_coldfire.o spi-m5445x.o
103 -obj-$(CONFIG_SPI_COLDFIRE) += spi_coldfire.o
104 +obj-$(CONFIG_SPI_DSPI) += dspi_mcf.o
105 obj-$(CONFIG_SPI_GPIO) += spi_gpio.o
106 obj-$(CONFIG_SPI_IMX) += spi_imx.o
107 obj-$(CONFIG_SPI_LM70_LLP) += spi_lm70llp.o
109 +++ b/drivers/spi/dspi_mcf.c
112 + * dspi_mcf.c - DSPI controller for ColdFire processors
115 + * Matt Waddel Matt.Waddel@freescale.com
116 + * Copyright Freescale Semiconductor, Inc. 2008
118 + * M547x/M548x changes by Kurt Mahan kmahan@freescale.com
119 + * Copyright Freescale Semiconductor, Inc. 2008
121 + * Based on spi_coldfire.c done by:
123 + * Yaroslav Vinogradov
124 + * Copyright Freescale Semiconductor, Inc. 2006-2007
125 + * Mike Lavender (mike@steroidmicros)
126 + * (C) Copyright 2005, Intec Automation,
128 + * This program is free software; you can redistribute it and/or modify it
129 + * under the terms of the GNU General Public License as published by the
130 + * Free Software Foundation; either version 2 of the License, or (at your
131 + * option) any later version.
133 + * This program is distributed in the hope that it will be useful,
134 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
135 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
136 + * GNU General Public License for more details.
138 + * You should have received a copy of the GNU General Public License
139 + * along with this program; if not, write to the Free Software
140 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
142 + ***************************************************************************
144 + * v0.002 M547x/M548x support.
145 + * v0.001 Initial version. Coldfire DSPI master driver.
146 + ****************************************************************************/
152 +#include <linux/autoconf.h>
153 +#include <linux/init.h>
154 +#include <linux/module.h>
155 +#include <linux/device.h>
156 +#include <linux/interrupt.h>
157 +#include <linux/platform_device.h>
158 +#include <linux/spi/spi.h>
159 +#include <linux/workqueue.h>
160 +#include <linux/delay.h>
161 +#include <asm/mcfsim.h>
162 +#include <asm/mcfqspi.h>
163 +#include <asm/coldfire.h>
164 +#include <linux/io.h>
165 +#include <asm/mcfdspi.h>
166 +#include <linux/dma-mapping.h>
168 +#undef DSPI_COLDFIRE_DEBUG
170 +#ifdef DSPI_COLDFIRE_DEBUG
171 +#define DBG(fmt, args...) \
172 + printk(KERN_INFO "[%s] " fmt , __FUNCTION__, ## args)
174 +#define DBG(fmt, args...) do {} while (0)
177 +#if defined(CONFIG_M54455)
178 +#include <asm/mcf5445x_dspi.h>
179 +#if defined(CONFIG_SPI_COLDFIRE_DSPI_EDMA)
180 + #include <asm/mcf5445x_edma.h>
184 +#if defined(CONFIG_M547X_8X)
185 +#include <asm/virtconvert.h>
186 +#include <asm/m5485dspi.h>
189 +#if defined(CONFIG_SPI_COLDFIRE_DSPI_EDMA)
190 +#include <asm/mcf_edma.h>
191 +#define SPI_DSPI_EDMA
192 +#define EDMA_BUFSIZE_KMALLOC (DSPI_FIFO_SIZE*4)
193 +#define DSPI_DMA_RX_TCD MCF_EDMA_CHAN_DSPI_RX
194 +#define DSPI_DMA_TX_TCD MCF_EDMA_CHAN_DSPI_TX
197 +#define DSPI_BITS MCF_DSPI_DCTAR_FMSZ(0xF)
198 +#define DSPI_BITS_16 MCF_DSPI_DCTAR_FMSZ(0xF)
199 +#define DSPI_BITS_8 MCF_DSPI_DCTAR_FMSZ(0x7)
200 +#define DSPI_FIFO_SIZE 16
202 +#define DRIVER_NAME "Coldfire DSPI"
204 +/****************************************************************************/
207 + * Local constants and macros
210 +#define START_STATE ((void *)0)
211 +#define RUNNING_STATE ((void *)1)
212 +#define DONE_STATE ((void *)2)
213 +#define ERROR_STATE ((void *)-1)
215 +#define QUEUE_RUNNING 0
216 +#define QUEUE_STOPPED 1
218 +/****************************************************************************/
221 + * Local Data Structures
226 + unsigned cont_scke:1;
233 + unsigned reserved15:1;
236 + unsigned dis_rxf:1;
238 + unsigned clr_rxf:1;
239 + unsigned smpl_pt:2;
240 + unsigned reserved71:7;
264 + struct DSPI_MCR mcr;
268 + struct DSPI_CTAR ctar;
270 + u16 void_write_data;
274 +struct driver_data {
275 + /* Driver model hookup */
276 + struct platform_device *pdev;
278 + /* SPI framework hookup */
279 + struct spi_master *master;
281 + /* Driver message queue */
282 + struct workqueue_struct *workqueue;
283 + struct work_struct pump_messages;
284 + spinlock_t lock; /* lock */
285 + struct list_head queue;
289 + /* Message Transfer pump */
290 + struct tasklet_struct pump_transfers;
292 + /* Current message transfer state info */
293 + struct spi_message *cur_msg;
294 + struct spi_transfer *cur_transfer;
295 + struct chip_data *cur_chip;
302 +#define TRAN_STATE_RX_VOID 0x01
303 +#define TRAN_STATE_TX_VOID 0x02
304 +#define TRAN_STATE_WORD_ODD_NUM 0x04
306 + u16 void_write_data;
307 + unsigned cs_change:1;
312 + u32 *mcr; /* DSPI MCR register */
313 + u32 *ctar; /* DSPI CTAR register */
314 + u32 *dspi_dtfr; /* DSPI DTFR register */
315 + u32 *dspi_drfr; /* DSPI DRFR register */
316 + u32 *dspi_rser; /* DSPI RSER register */
317 + u32 *dspi_sr; /* DSPI status register */
319 +#if defined(SPI_DSPI_EDMA)
324 +#if defined(CONFIG_M532x) || defined(CONFIG_M537x)
325 + u16 *par; /* Pin assignment register */
327 + u8 *par; /* Pin assignment register */
329 + u8 *int_icr; /* Interrupt level and priority register */
330 + u32 *int_mr; /* Interrupt mask register */
331 + void (*cs_control)(u8 cs, u8 command);
334 +#define DSPI_CS(cs) ((1<<(cs))<<16)
336 +/****************************************************************************/
339 + * SPI local functions
342 +static void *next_transfer(struct driver_data *drv_data)
344 + struct spi_message *msg = drv_data->cur_msg;
345 + struct spi_transfer *trans = drv_data->cur_transfer;
348 + /* Move to next transfer */
349 + if (trans->transfer_list.next != &msg->transfers) {
350 + drv_data->cur_transfer = list_entry(trans->transfer_list.next,
351 + struct spi_transfer,
354 + if (drv_data->cur_transfer->transfer_list.next
355 + == &msg->transfers) /* last transfer */
356 + drv_data->cur_transfer->cs_change = 1;
358 + return RUNNING_STATE;
364 +static inline int is_word_transfer(struct driver_data *drv_data)
366 + return ((*(drv_data->ctar+drv_data->cs) & DSPI_BITS_16) ==
367 + DSPI_BITS_8) ? 0 : 1;
370 +static inline void set_8bit_transfer_mode(struct driver_data *drv_data)
373 + *(drv_data->ctar+drv_data->cs) =
374 + (*(drv_data->ctar + drv_data->cs) & ~DSPI_BITS) | DSPI_BITS_8;
377 +static inline void set_16bit_transfer_mode(struct driver_data *drv_data)
380 + *(drv_data->ctar+drv_data->cs) =
381 + (*(drv_data->ctar + drv_data->cs) & ~DSPI_BITS) | DSPI_BITS_16;
384 +static unsigned char hz_to_spi_baud(int pbr, int dbr, int speed_hz)
386 + int pbr_tbl[4] = {2, 3, 5, 7}; /* Valid baud rate pre-scaler values */
387 + int brs[16] = { 2, 4, 6, 8,
389 + 256, 512, 1024, 2048,
390 + 4096, 8192, 16384, 32768 };
391 + int temp, index = 0;
393 + if ((pbr < 0) || (pbr > 3) ||
394 + (dbr < 0) || (dbr > 1))
395 + return 15; /* table indexes out of range, go slow */
397 + temp = ((((MCF_CLK / 2) / pbr_tbl[pbr]) * (1 + dbr)) / speed_hz);
399 + while (temp >= brs[index])
403 + DBG("baud rate scaler = 0x%x - %d\n", index, brs[index]);
407 +static int write(struct driver_data *drv_data)
410 + int tx_word = is_word_transfer(drv_data);
413 + u32 dspi_pushr = 0;
415 +#if defined(SPI_DSPI_EDMA)
416 + u32 *edma_wr = (u32 *)(drv_data->edma_tx_buf);
419 + /* If we are in word mode, but only have a single byte to transfer
420 + * then switch to byte mode temporarily. Will switch back at the
421 + * end of the transfer. */
422 + if (tx_word && ((drv_data->tx_end - drv_data->tx) == 1)) {
423 + drv_data->flags |= TRAN_STATE_WORD_ODD_NUM;
424 + set_8bit_transfer_mode(drv_data);
428 + while ((drv_data->tx < drv_data->tx_end) && (tx_count < DSPI_FIFO_SIZE)) {
431 + if ((drv_data->tx_end - drv_data->tx) == 1)
434 + if (!(drv_data->flags & TRAN_STATE_TX_VOID))
435 + d16 = *(u16 *)drv_data->tx;
437 + d16 = drv_data->void_write_data;
439 + dspi_pushr = MCF_DSPI_DTFR_TXDATA(d16) |
440 + DSPI_CS(drv_data->cs) |
441 + MCF_DSPI_DTFR_CTAS(drv_data->cs) |
442 + MCF_DSPI_DTFR_CONT;
446 + if (!(drv_data->flags & TRAN_STATE_TX_VOID))
447 + d8 = *(u8 *)drv_data->tx;
449 + d8 = *(u8 *)&drv_data->void_write_data;
451 + dspi_pushr = MCF_DSPI_DTFR_TXDATA(d8) |
452 + DSPI_CS(drv_data->cs) |
453 + MCF_DSPI_DTFR_CTAS(drv_data->cs) |
454 + MCF_DSPI_DTFR_CONT;
459 + if (drv_data->tx == drv_data->tx_end
460 + || tx_count == DSPI_FIFO_SIZE-1) {
461 + /* last transfer in the queue */
462 + dspi_pushr |= MCF_DSPI_DTFR_EOQ;
463 + if ((drv_data->cs_change)
464 + && (drv_data->tx == drv_data->tx_end))
465 + dspi_pushr &= ~MCF_DSPI_DTFR_CONT;
466 +#ifdef CONFIG_M547X_8X
467 + /* EOQ gets missed if we don't delay */
470 + } else if (tx_word && ((drv_data->tx_end - drv_data->tx) == 1))
471 + dspi_pushr |= MCF_DSPI_DTFR_EOQ;
475 + dspi_pushr |= MCF_DSPI_DTFR_CTCNT; /* clear counter */
477 +#if defined(SPI_DSPI_EDMA)
478 + *edma_wr = dspi_pushr;
481 + *drv_data->dspi_dtfr = dspi_pushr;
486 +#if defined(SPI_DSPI_EDMA)
487 + if (tx_count > 0) {
489 + mcf_edma_set_tcd_params(DSPI_DMA_TX_TCD,
490 + virt_to_phys(drv_data->edma_tx_buf),
491 + (u32)drv_data->dspi_dtfr,
492 + MCF_EDMA_TCD_ATTR_SSIZE_32BIT
493 + | MCF_EDMA_TCD_ATTR_DSIZE_32BIT,
495 + 4 * tx_count, /* nbytes */
502 + 1); /* disable_req */
504 + mcf_edma_set_tcd_params(DSPI_DMA_RX_TCD,
505 + (u32)drv_data->dspi_drfr,
506 + virt_to_phys(drv_data->edma_rx_buf),
507 + MCF_EDMA_TCD_ATTR_SSIZE_32BIT
508 + | MCF_EDMA_TCD_ATTR_DSIZE_32BIT,
510 + 4 * tx_count, /* nbytes */
517 + 1); /* disable_req */
519 + mcf_edma_start_transfer(DSPI_DMA_TX_TCD);
522 + return (tx_count * (tx_word + 1));
525 +static int read(struct driver_data *drv_data)
528 + int rx_word = is_word_transfer(drv_data);
530 +#if defined(SPI_DSPI_EDMA)
531 + u32 *rx_edma = (u32 *) drv_data->edma_rx_buf;
533 + /* receive SPI data */
534 + mcf_edma_start_transfer(DSPI_DMA_RX_TCD);
536 + while ((drv_data->rx < drv_data->rx_end)
537 + && (rx_count < DSPI_FIFO_SIZE)) {
540 + if ((drv_data->rx_end - drv_data->rx) == 1)
542 +#if defined(SPI_DSPI_EDMA)
543 + d = MCF_DSPI_DRFR_RXDATA(*rx_edma);
546 + d = MCF_DSPI_DRFR_RXDATA(*drv_data->dspi_drfr);
548 + if (!(drv_data->flags & TRAN_STATE_RX_VOID))
549 + *(u16 *)drv_data->rx = d;
553 +#if defined(SPI_DSPI_EDMA)
554 + d = MCF_DSPI_DRFR_RXDATA(*rx_edma);
557 + d = MCF_DSPI_DRFR_RXDATA(*drv_data->dspi_drfr);
559 + if (!(drv_data->flags & TRAN_STATE_RX_VOID))
560 + *(u8 *)drv_data->rx = d;
564 + DBG("rxd=0x%x\n", d);
570 +static inline void dspi_setup_chip(struct driver_data *drv_data)
572 + struct chip_data *chip = drv_data->cur_chip;
575 + *drv_data->mcr = chip->mcr_val;
576 + *(drv_data->ctar+drv_data->cs) = chip->ctar_val;
577 + *drv_data->dspi_rser = MCF_DSPI_DRSER_EOQFE;
580 +#if defined(SPI_DSPI_EDMA)
581 +static int edma_tx_handler(int channel, void *dev)
584 + if (channel == DSPI_DMA_TX_TCD)
585 + mcf_edma_stop_transfer(DSPI_DMA_TX_TCD);
586 + return IRQ_HANDLED;
589 +static int edma_rx_handler(int channel, void *dev)
592 + if (channel == DSPI_DMA_RX_TCD)
593 + mcf_edma_stop_transfer(DSPI_DMA_RX_TCD);
594 + return IRQ_HANDLED;
598 +static irqreturn_t dspi_interrupt(int irq, void *dev_id)
600 + struct driver_data *drv_data = (struct driver_data *)dev_id;
601 + struct spi_message *msg = drv_data->cur_msg;
603 + /* Clear all flags immediately */
604 + *drv_data->dspi_sr = MCF_DSPI_DSR_EOQF;
606 + if (!drv_data->cur_msg || !drv_data->cur_msg->state) {
607 +#if !defined(SPI_DSPI_EDMA)
608 + u32 irq_status = *drv_data->dspi_sr;
609 + /* if eDMA is used it happens some time (at least once)*/
610 + printk(KERN_ERR "Bad message or transfer state handler. \
611 + IRQ status = %x\n", irq_status);
618 + * Read the data into the buffer and reload and start
619 + * queue with new data if not finished. If finished
620 + * then setup the next transfer
624 + if (drv_data->rx == drv_data->rx_end) {
626 + * Finished now - fall through and schedule next
629 + if (drv_data->flags & TRAN_STATE_WORD_ODD_NUM)
630 + set_16bit_transfer_mode(drv_data);
632 + msg->state = next_transfer(drv_data);
634 + /* not finished yet - keep going */
635 + msg->actual_length += write(drv_data);
636 + return IRQ_HANDLED;
639 + tasklet_schedule(&drv_data->pump_transfers);
641 + return IRQ_HANDLED;
644 +/* caller already set message->status; dma and pio irqs are blocked */
645 +static void giveback(struct driver_data *drv_data)
647 + struct spi_transfer *last_transfer;
648 + unsigned long flags;
649 + struct spi_message *msg;
652 + spin_lock_irqsave(&drv_data->lock, flags);
653 + msg = drv_data->cur_msg;
654 + drv_data->cur_msg = NULL;
655 + drv_data->cur_transfer = NULL;
656 + drv_data->cur_chip = NULL;
657 + queue_work(drv_data->workqueue, &drv_data->pump_messages);
658 + spin_unlock_irqrestore(&drv_data->lock, flags);
660 + last_transfer = list_entry(msg->transfers.prev,
661 + struct spi_transfer, transfer_list);
663 + if (!last_transfer->cs_change)
664 + drv_data->cs_control(drv_data->cs, QSPI_CS_DROP);
668 + msg->complete(msg->context);
672 +static void pump_transfers(unsigned long data)
674 + struct driver_data *drv_data = (struct driver_data *)data;
675 + struct spi_message *message = NULL;
676 + struct spi_transfer *transfer = NULL;
677 + struct spi_transfer *previous = NULL;
678 + struct chip_data *chip = NULL;
679 + unsigned long flags;
681 + /* Get current state information */
682 + message = drv_data->cur_msg;
683 + transfer = drv_data->cur_transfer;
684 + chip = drv_data->cur_chip;
686 + /* Handle for abort */
687 + if (message->state == ERROR_STATE) {
688 + message->status = -EIO;
689 + giveback(drv_data);
693 + /* Handle end of message */
694 + if (message->state == DONE_STATE) {
695 + message->status = 0;
696 + giveback(drv_data);
700 + if (message->state == START_STATE) {
701 + dspi_setup_chip(drv_data);
703 + if (drv_data->cs_control)
704 + drv_data->cs_control(message->spi->chip_select,
708 + /* Delay if requested at end of transfer*/
709 + if (message->state == RUNNING_STATE) {
710 + previous = list_entry(transfer->transfer_list.prev,
711 + struct spi_transfer,
714 + if (drv_data->cs_control && transfer->cs_change)
715 + drv_data->cs_control(message->spi->chip_select,
718 + if (previous->delay_usecs)
719 + udelay(previous->delay_usecs);
721 + if (drv_data->cs_control && transfer->cs_change)
722 + drv_data->cs_control(message->spi->chip_select,
726 + drv_data->flags = 0;
727 + drv_data->tx = (void *)transfer->tx_buf;
728 + drv_data->tx_end = drv_data->tx + transfer->len;
729 + drv_data->rx = transfer->rx_buf;
730 + drv_data->rx_end = drv_data->rx + transfer->len;
733 + drv_data->flags |= TRAN_STATE_RX_VOID;
735 + drv_data->flags |= TRAN_STATE_TX_VOID;
736 + drv_data->cs = message->spi->chip_select;
737 + drv_data->cs_change = transfer->cs_change;
738 + drv_data->void_write_data = chip->void_write_data;
739 + if (transfer->speed_hz) {
740 + *(drv_data->ctar + drv_data->cs) = \
741 + ((chip->ctar_val & ~0xF) | \
742 + hz_to_spi_baud(chip->ctar.pbr, \
744 + transfer->speed_hz));
747 + message->state = RUNNING_STATE;
749 + local_irq_save(flags);
750 + message->actual_length += write(drv_data);
751 + local_irq_restore(flags);
755 +static void pump_messages(struct work_struct *work)
757 + struct driver_data *drv_data;
758 + unsigned long flags;
761 + drv_data = container_of(work, struct driver_data, pump_messages);
763 + /* Lock queue and check for queue work */
764 + spin_lock_irqsave(&drv_data->lock, flags);
765 + if (list_empty(&drv_data->queue)
766 + || drv_data->run == QUEUE_STOPPED) {
767 + drv_data->busy = 0;
768 + spin_unlock_irqrestore(&drv_data->lock, flags);
772 + /* Make sure we are not already running a message */
773 + if (drv_data->cur_msg) {
774 + spin_unlock_irqrestore(&drv_data->lock, flags);
778 + /* Extract head of queue */
779 + drv_data->cur_msg = list_entry(drv_data->queue.next,
780 + struct spi_message, queue);
781 + list_del_init(&drv_data->cur_msg->queue);
783 + /* Initial message state*/
784 + drv_data->cur_msg->state = START_STATE;
785 + drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
786 + struct spi_transfer,
789 + if (drv_data->cur_transfer->transfer_list.next
790 + == &drv_data->cur_msg->transfers)
791 + drv_data->cur_transfer->cs_change = 1; /* last */
794 + /* Setup the SPI Registers using the per chip configuration */
795 + drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
797 + /* Mark as busy and launch transfers */
798 + tasklet_schedule(&drv_data->pump_transfers);
800 + drv_data->busy = 1;
801 + spin_unlock_irqrestore(&drv_data->lock, flags);
804 +/****************************************************************************/
807 + * SPI master implementation
810 +static int transfer(struct spi_device *spi, struct spi_message *msg)
812 + struct driver_data *drv_data = spi_master_get_devdata(spi->master);
813 + unsigned long flags;
816 + spin_lock_irqsave(&drv_data->lock, flags);
818 + if (drv_data->run == QUEUE_STOPPED) {
819 + spin_unlock_irqrestore(&drv_data->lock, flags);
823 + msg->actual_length = 0;
824 + msg->status = -EINPROGRESS;
825 + msg->state = START_STATE;
827 + list_add_tail(&msg->queue, &drv_data->queue);
829 + if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
830 + queue_work(drv_data->workqueue, &drv_data->pump_messages);
832 + spin_unlock_irqrestore(&drv_data->lock, flags);
838 +static int setup(struct spi_device *spi)
841 + struct chip_data *chip;
842 + struct coldfire_dspi_chip *chip_info
843 + = (struct coldfire_dspi_chip *)spi->controller_data;
846 + /* Only alloc on first setup */
847 + chip = spi_get_ctldata(spi);
848 + if (chip == NULL) {
849 + chip = kcalloc(1, sizeof(struct chip_data), GFP_KERNEL);
852 + spi->mode = chip_info->mode;
853 + spi->bits_per_word = chip_info->bits_per_word;
856 + chip->mcr.master = 1;
857 + chip->mcr.cont_scke = 0;
858 + chip->mcr.dconf = 0;
860 + chip->mcr.mtfe = 0;
861 + chip->mcr.pcsse = 0;
862 + chip->mcr.rooe = 0;
863 + chip->mcr.pcsis = 0xFF;
864 + chip->mcr.reserved15 = 0;
865 + chip->mcr.mdis = 0;
866 + chip->mcr.dis_tx = 0;
867 + chip->mcr.dis_rxf = 0;
868 + chip->mcr.clr_tx = 1;
869 + chip->mcr.clr_rxf = 1;
870 + chip->mcr.smpl_pt = 0;
871 + chip->mcr.reserved71 = 0;
872 + chip->mcr.halt = 0;
874 + if ((spi->bits_per_word >= 4) && (spi->bits_per_word <= 16)) {
875 + chip->ctar.fmsz = spi->bits_per_word-1;
877 + printk(KERN_ERR "Invalid wordsize\n");
882 + chip->void_write_data = chip_info->void_write_data;
884 + if (spi->max_speed_hz != 0)
885 + chip_info->br = hz_to_spi_baud(chip_info->pbr, chip_info->dbr, \
886 + spi->max_speed_hz);
888 + chip->ctar.cpha = (spi->mode & SPI_CPHA) ? 1 : 0;
889 + chip->ctar.cpol = (spi->mode & SPI_CPOL) ? 1 : 0;
890 + chip->ctar.lsbfe = (spi->mode & SPI_LSB_FIRST) ? 1 : 0;
891 + chip->ctar.dbr = chip_info->dbr;
892 + chip->ctar.pbr = chip_info->pbr;
893 + chip->ctar.br = chip_info->br;
894 + chip->ctar.pcssck = chip_info->pcssck;
895 + chip->ctar.pasc = chip_info->pasc;
896 + chip->ctar.pdt = chip_info->pdt;
897 + chip->ctar.cssck = chip_info->cssck;
898 + chip->ctar.asc = chip_info->asc;
899 + chip->ctar.dt = chip_info->dt;
901 + spi_set_ctldata(spi, chip);
906 +static int init_queue(struct driver_data *drv_data)
908 + INIT_LIST_HEAD(&drv_data->queue);
909 + spin_lock_init(&drv_data->lock);
911 + drv_data->run = QUEUE_STOPPED;
912 + drv_data->busy = 0;
914 + tasklet_init(&drv_data->pump_transfers,
915 + pump_transfers, (unsigned long)drv_data);
917 + INIT_WORK(&drv_data->pump_messages, pump_messages);
919 + drv_data->workqueue = create_singlethread_workqueue(
920 + drv_data->master->dev.parent->bus_id);
921 + if (drv_data->workqueue == NULL)
927 +static int start_queue(struct driver_data *drv_data)
929 + unsigned long flags;
931 + spin_lock_irqsave(&drv_data->lock, flags);
933 + if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
934 + spin_unlock_irqrestore(&drv_data->lock, flags);
938 + drv_data->run = QUEUE_RUNNING;
939 + drv_data->cur_msg = NULL;
940 + drv_data->cur_transfer = NULL;
941 + drv_data->cur_chip = NULL;
942 + spin_unlock_irqrestore(&drv_data->lock, flags);
944 + queue_work(drv_data->workqueue, &drv_data->pump_messages);
949 +static int stop_queue(struct driver_data *drv_data)
951 + unsigned long flags;
952 + unsigned limit = 500;
955 + spin_lock_irqsave(&drv_data->lock, flags);
957 + /* This is a bit lame, but is optimized for the common execution path.
958 + * A wait_queue on the drv_data->busy could be used, but then the common
959 + * execution path (pump_messages) would be required to call wake_up or
960 + * friends on every SPI message. Do this instead */
961 + drv_data->run = QUEUE_STOPPED;
962 + while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
963 + spin_unlock_irqrestore(&drv_data->lock, flags);
965 + spin_lock_irqsave(&drv_data->lock, flags);
968 + if (!list_empty(&drv_data->queue) || drv_data->busy)
971 + spin_unlock_irqrestore(&drv_data->lock, flags);
976 +static int destroy_queue(struct driver_data *drv_data)
980 + status = stop_queue(drv_data);
984 + destroy_workqueue(drv_data->workqueue);
990 +static void cleanup(struct spi_device *spi)
992 + struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
994 + dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
995 + spi->master->bus_num, spi->chip_select);
1001 +/****************************************************************************/
1004 + * Generic Device driver routines and interface implementation
1007 +static int coldfire_spi_probe(struct platform_device *pdev)
1009 + struct device *dev = &pdev->dev;
1010 + struct coldfire_spi_master *platform_info;
1011 + struct spi_master *master;
1012 + struct driver_data *drv_data = 0;
1013 + struct resource *memory_resource;
1017 +#if defined(SPI_DSPI_EDMA)
1018 + dma_addr_t dma_handle;
1021 + platform_info = (struct coldfire_spi_master *)dev->platform_data;
1023 + master = spi_alloc_master(dev, sizeof(struct driver_data));
1027 + drv_data = spi_master_get_devdata(master);
1028 + drv_data->master = master;
1030 + INIT_LIST_HEAD(&drv_data->queue);
1031 + spin_lock_init(&drv_data->lock);
1033 + master->bus_num = platform_info->bus_num;
1034 + master->num_chipselect = platform_info->num_chipselect;
1035 + master->cleanup = cleanup;
1036 + master->setup = setup;
1037 + master->transfer = transfer;
1039 + drv_data->cs_control = platform_info->cs_control;
1040 + if (drv_data->cs_control)
1041 + for (i = 0; i < master->num_chipselect; i++)
1042 + drv_data->cs_control(i, QSPI_CS_INIT | QSPI_CS_DROP);
1044 + /* Setup register addresses */
1045 + memory_resource = platform_get_resource_byname(pdev,
1046 + IORESOURCE_MEM, "spi-module");
1047 + if (!memory_resource) {
1048 + dev_dbg(dev, "can not find platform module memory\n");
1049 + goto out_error_master_alloc;
1052 +#if defined(SPI_DSPI_EDMA)
1053 + drv_data->edma_tx_buf = dma_alloc_coherent(NULL, EDMA_BUFSIZE_KMALLOC, \
1054 + &dma_handle, GFP_DMA);
1055 + if (!drv_data->edma_tx_buf) {
1056 + dev_dbg(dev, "cannot allocate eDMA TX memory\n");
1057 + goto out_error_master_alloc;
1059 + drv_data->edma_rx_buf = dma_alloc_coherent(NULL, EDMA_BUFSIZE_KMALLOC, \
1060 + &dma_handle, GFP_DMA);
1061 + if (!drv_data->edma_rx_buf) {
1062 + dma_free_coherent(NULL, EDMA_BUFSIZE_KMALLOC, \
1063 + drv_data->edma_tx_buf, dma_handle);
1064 + kfree(drv_data->edma_tx_buf);
1065 + dev_dbg(dev, "cannot allocate eDMA RX memory\n");
1066 + goto out_error_master_alloc;
1070 + drv_data->mcr = (void *)&MCF_DSPI_DMCR;
1071 + drv_data->ctar = (void *)&MCF_DSPI_DCTAR0;
1072 + drv_data->dspi_sr = (void *)&MCF_DSPI_DSR;
1073 + drv_data->dspi_rser = (void *)&MCF_DSPI_DRSER;
1074 + drv_data->dspi_dtfr = (void *)&MCF_DSPI_DTFR;
1075 + drv_data->dspi_drfr = (void *)&MCF_DSPI_DRFR;
1077 + memory_resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1079 + if (!memory_resource) {
1080 + dev_dbg(dev, "No spi-par memory\n");
1081 + goto out_error_master_alloc;
1083 + drv_data->par = (void *)memory_resource->start;
1085 + memory_resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1087 + if (!memory_resource) {
1088 + dev_dbg(dev, "No spi-int-level memory\n");
1089 + goto out_error_master_alloc;
1091 + drv_data->int_icr = (void *)memory_resource->start;
1093 + memory_resource = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1095 + if (!memory_resource) {
1096 + dev_dbg(dev, "No spi-int-mask memory\n");
1097 + goto out_error_master_alloc;
1099 + drv_data->int_mr = (void *)memory_resource->start;
1101 + if (platform_info->irq_list) {
1102 + /* multiple IRQs */
1103 + int *irqlist = platform_info->irq_list;
1104 + while ((irq = *irqlist++)) {
1105 + int off = *irqlist++;
1106 + int lvl = *irqlist++;
1107 + int msk = *irqlist++;
1108 + status = request_irq(irq, dspi_interrupt, IRQF_DISABLED,
1109 + dev->bus_id, drv_data);
1111 + dev_err(&pdev->dev,
1112 + "Unable to attach ColdFire DSPI interrupt\n");
1113 + goto out_error_master_alloc;
1117 + *(drv_data->int_icr + off) = lvl;
1120 + *drv_data->int_mr &= ~msk;
1124 + irq = platform_info->irq_vector;
1126 + status = request_irq(platform_info->irq_vector, dspi_interrupt,
1127 + IRQF_DISABLED, dev->bus_id, drv_data);
1129 + dev_err(&pdev->dev, "Unable to attach ColdFire DSPI interrupt\n");
1130 + goto out_error_master_alloc;
1133 + *drv_data->int_icr = platform_info->irq_lp;
1134 + *drv_data->int_mr &= ~platform_info->irq_mask;
1137 + /* Now that we have all the addresses etc. Let's set it up */
1138 + if (platform_info->par_val)
1139 + *drv_data->par = platform_info->par_val;
1141 + /* Initial and start queue */
1142 + status = init_queue(drv_data);
1143 + if (status != 0) {
1144 + dev_err(&pdev->dev, "Problem initializing DSPI queue\n");
1145 + goto out_error_irq_alloc;
1147 + status = start_queue(drv_data);
1148 + if (status != 0) {
1149 + dev_err(&pdev->dev, "Problem starting DSPI queue\n");
1150 + goto out_error_irq_alloc;
1153 + /* Register with the SPI framework */
1154 + platform_set_drvdata(pdev, drv_data);
1155 + status = spi_register_master(master);
1156 + if (status != 0) {
1157 + dev_err(&pdev->dev, "Problem registering DSPI master\n");
1159 + goto out_error_queue_alloc;
1162 +#if defined(SPI_DSPI_EDMA)
1163 + if (mcf_edma_request_channel(DSPI_DMA_TX_TCD,
1168 + NULL, /* spinlock */
1169 + DRIVER_NAME) < 0){
1170 + dev_err(&pdev->dev, "eDMA transmit channel request\n");
1172 + goto out_error_queue_alloc;
1175 + if (mcf_edma_request_channel(DSPI_DMA_RX_TCD,
1180 + NULL, /* spinlock */
1181 + DRIVER_NAME) < 0){
1182 + dev_err(&pdev->dev, "eDAM receive channel request\n");
1184 + mcf_edma_free_channel(DSPI_DMA_TX_TCD, pdev);
1185 + goto out_error_queue_alloc;
1189 + printk(KERN_INFO "DSPI: Coldfire master initialized\n");
1192 +out_error_queue_alloc:
1193 + destroy_queue(drv_data);
1195 +out_error_irq_alloc:
1196 + free_irq(platform_info->irq_vector, drv_data);
1198 +out_error_master_alloc:
1199 + spi_master_put(master);
1204 +static int coldfire_spi_remove(struct platform_device *pdev)
1206 + struct driver_data *drv_data = platform_get_drvdata(pdev);
1213 +#if defined(SPI_DSPI_EDMA)
1214 + mcf_edma_free_channel(DSPI_DMA_TX_TCD, pdev);
1215 + mcf_edma_free_channel(DSPI_DMA_RX_TCD, pdev);
1218 + /* Remove the queue */
1219 + status = destroy_queue(drv_data);
1224 + irq = platform_get_irq(pdev, 0);
1226 + free_irq(irq, drv_data);
1228 + /* Disconnect from the SPI framework */
1229 + spi_unregister_master(drv_data->master);
1231 + /* Prevent double remove */
1232 + platform_set_drvdata(pdev, NULL);
1237 +static void coldfire_spi_shutdown(struct platform_device *pdev)
1239 + int status = coldfire_spi_remove(pdev);
1242 + dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1247 +static int suspend_devices(struct device *dev, void *pm_message)
1249 + pm_message_t *state = pm_message;
1251 + if (dev->power.power_state.event != state->event) {
1252 + dev_warn(dev, "pm state does not match request\n");
1259 +static int coldfire_spi_suspend(struct platform_device *pdev,
1260 + pm_message_t state)
1262 + struct driver_data *drv_data = platform_get_drvdata(pdev);
1265 + /* Check all childern for current power state */
1266 + if (device_for_each_child(&pdev->dev,
1267 + &state, suspend_devices) != 0) {
1268 + dev_warn(&pdev->dev, "suspend aborted\n");
1272 + status = stop_queue(drv_data);
1279 +static int coldfire_spi_resume(struct platform_device *pdev)
1281 + struct driver_data *drv_data = platform_get_drvdata(pdev);
1284 + /* Start the queue running */
1285 + status = start_queue(drv_data);
1286 + if (status != 0) {
1287 + dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1294 +#define coldfire_spi_suspend NULL
1295 +#define coldfire_spi_resume NULL
1296 +#endif /* CONFIG_PM */
1298 +static struct platform_driver driver = {
1300 + .name = "spi_coldfire",
1301 + .bus = &platform_bus_type,
1302 + .owner = THIS_MODULE,
1304 + .probe = coldfire_spi_probe,
1305 + .remove = __devexit_p(coldfire_spi_remove),
1306 + .shutdown = coldfire_spi_shutdown,
1307 + .suspend = coldfire_spi_suspend,
1308 + .resume = coldfire_spi_resume,
1311 +static int __init coldfire_spi_init(void)
1313 + platform_driver_register(&driver);
1317 +module_init(coldfire_spi_init);
1319 +static void __exit coldfire_spi_exit(void)
1321 + platform_driver_unregister(&driver);
1323 +module_exit(coldfire_spi_exit);
1325 +MODULE_AUTHOR("Matt Waddel");
1326 +MODULE_DESCRIPTION("ColdFire DSPI Contoller");
1327 +MODULE_LICENSE("GPL");
1329 +++ b/include/asm-m68k/mcfdspi.h
1332 + * mcfdspi.h-DSPI controller for the ColdFire processors.
1335 + * Copyright Freescale Semiconductor, Inc. 2008
1337 + * This file is based on mcfqspi.h
1339 + * This program is free software; you can redistribute it and/or modify it
1340 + * under the terms of the GNU General Public License as published by the
1341 + * Free Software Foundation; either version 2 of the License, or (at your
1342 + * option) any later version.
1344 + * This program is distributed in the hope that it will be useful,
1345 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1346 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1347 + * GNU General Public License for more details.
1349 + * You should have received a copy of the GNU General Public License
1350 + * along with this program; if not, write to the Free Software
1351 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1353 + ***************************************************************************
1355 + * v0.001 25 March 2008 Andrey Butok
1356 + * Initial Release - developed on uClinux with 2.6.23 kernel.
1363 +struct coldfire_dspi_chip {
1366 + u16 void_write_data;
1367 + /* Only used in master mode */
1368 + u8 dbr; /* Double baud rate */
1369 + u8 pbr; /* Baud rate prescaler */
1370 + u8 br; /* Baud rate scaler */
1371 + u8 pcssck; /* PCS to SCK delay prescaler */
1372 + u8 pasc; /* After SCK delay prescaler */
1373 + u8 pdt; /* Delay after transfer prescaler */
1374 + u8 cssck; /* PCS to SCK delay scaler */
1375 + u8 asc; /* After SCK delay scaler */
1376 + u8 dt; /* Delay after transfer scaler */
1378 +#endif /*MCFDSPI_H_*/