2 * File: drivers/spi/bfin5xx_spi.c
4 * Bryan Wu <bryan.wu@analog.com>
6 * Luke Yang (Analog Devices Inc.)
8 * Created: March. 10th 2006
9 * Description: SPI controller driver for Blackfin BF5xx
10 * Bugs: Enter bugs at http://blackfin.uclinux.org/
13 * March 10, 2006 bfin5xx_spi.c Created. (Luke Yang)
14 * August 7, 2006 added full duplex mode (Axel Weiss & Luke Yang)
15 * July 17, 2007 add support for BF54x SPI0 controller (Bryan Wu)
16 * July 30, 2007 add platfrom_resource interface to support multi-port
17 * SPI controller (Bryan Wu)
19 * Copyright 2004-2007 Analog Devices Inc.
21 * This program is free software ; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation ; either version 2, or (at your option)
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program ; see the file COPYING.
33 * If not, write to the Free Software Foundation,
34 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
37 #include <linux/init.h>
38 #include <linux/module.h>
39 #include <linux/delay.h>
40 #include <linux/device.h>
42 #include <linux/ioport.h>
43 #include <linux/irq.h>
44 #include <linux/errno.h>
45 #include <linux/interrupt.h>
46 #include <linux/platform_device.h>
47 #include <linux/dma-mapping.h>
48 #include <linux/spi/spi.h>
49 #include <linux/workqueue.h>
52 #include <asm/portmux.h>
53 #include <asm/bfin5xx_spi.h>
55 #define DRV_NAME "bfin-spi"
56 #define DRV_AUTHOR "Bryan Wu, Luke Yang"
57 #define DRV_DESC "Blackfin BF5xx on-chip SPI Controller Driver"
58 #define DRV_VERSION "1.0"
60 MODULE_AUTHOR(DRV_AUTHOR);
61 MODULE_DESCRIPTION(DRV_DESC);
62 MODULE_LICENSE("GPL");
64 #define IS_DMA_ALIGNED(x) (((u32)(x)&0x07) == 0)
66 #define START_STATE ((void *)0)
67 #define RUNNING_STATE ((void *)1)
68 #define DONE_STATE ((void *)2)
69 #define ERROR_STATE ((void *)-1)
70 #define QUEUE_RUNNING 0
71 #define QUEUE_STOPPED 1
74 /* Driver model hookup */
75 struct platform_device *pdev;
77 /* SPI framework hookup */
78 struct spi_master *master;
80 /* Regs base of SPI controller */
81 void __iomem *regs_base;
83 /* Pin request list */
87 struct bfin5xx_spi_master *master_info;
89 /* Driver message queue */
90 struct workqueue_struct *workqueue;
91 struct work_struct pump_messages;
93 struct list_head queue;
97 /* Message Transfer pump */
98 struct tasklet_struct pump_transfers;
100 /* Current message transfer state info */
101 struct spi_message *cur_msg;
102 struct spi_transfer *cur_transfer;
103 struct chip_data *cur_chip;
122 void (*write) (struct driver_data *);
123 void (*read) (struct driver_data *);
124 void (*duplex) (struct driver_data *);
134 u8 width; /* 0 or 1 */
136 u8 bits_per_word; /* 8 or 16 */
137 u8 cs_change_per_word;
138 u16 cs_chg_udelay; /* Some devices require > 255usec delay */
139 void (*write) (struct driver_data *);
140 void (*read) (struct driver_data *);
141 void (*duplex) (struct driver_data *);
144 #define DEFINE_SPI_REG(reg, off) \
145 static inline u16 read_##reg(struct driver_data *drv_data) \
146 { return bfin_read16(drv_data->regs_base + off); } \
147 static inline void write_##reg(struct driver_data *drv_data, u16 v) \
148 { bfin_write16(drv_data->regs_base + off, v); }
150 DEFINE_SPI_REG(CTRL, 0x00)
151 DEFINE_SPI_REG(FLAG, 0x04)
152 DEFINE_SPI_REG(STAT, 0x08)
153 DEFINE_SPI_REG(TDBR, 0x0C)
154 DEFINE_SPI_REG(RDBR, 0x10)
155 DEFINE_SPI_REG(BAUD, 0x14)
156 DEFINE_SPI_REG(SHAW, 0x18)
158 static void bfin_spi_enable(struct driver_data *drv_data)
162 cr = read_CTRL(drv_data);
163 write_CTRL(drv_data, (cr | BIT_CTL_ENABLE));
166 static void bfin_spi_disable(struct driver_data *drv_data)
170 cr = read_CTRL(drv_data);
171 write_CTRL(drv_data, (cr & (~BIT_CTL_ENABLE)));
174 /* Caculate the SPI_BAUD register value based on input HZ */
175 static u16 hz_to_spi_baud(u32 speed_hz)
177 u_long sclk = get_sclk();
178 u16 spi_baud = (sclk / (2 * speed_hz));
180 if ((sclk % (2 * speed_hz)) > 0)
186 static int flush(struct driver_data *drv_data)
188 unsigned long limit = loops_per_jiffy << 1;
190 /* wait for stop and clear stat */
191 while (!(read_STAT(drv_data) & BIT_STAT_SPIF) && limit--)
194 write_STAT(drv_data, BIT_STAT_CLR);
199 /* Chip select operation functions for cs_change flag */
200 static void cs_active(struct driver_data *drv_data, struct chip_data *chip)
202 u16 flag = read_FLAG(drv_data);
205 flag &= ~(chip->flag << 8);
207 write_FLAG(drv_data, flag);
210 static void cs_deactive(struct driver_data *drv_data, struct chip_data *chip)
212 u16 flag = read_FLAG(drv_data);
214 flag |= (chip->flag << 8);
216 write_FLAG(drv_data, flag);
218 /* Move delay here for consistency */
219 if (chip->cs_chg_udelay)
220 udelay(chip->cs_chg_udelay);
223 #define MAX_SPI_SSEL 7
225 /* stop controller and re-config current chip*/
226 static int restore_state(struct driver_data *drv_data)
228 struct chip_data *chip = drv_data->cur_chip;
231 /* Clear status and disable clock */
232 write_STAT(drv_data, BIT_STAT_CLR);
233 bfin_spi_disable(drv_data);
234 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
236 /* Load the registers */
237 write_CTRL(drv_data, chip->ctl_reg);
238 write_BAUD(drv_data, chip->baud);
240 bfin_spi_enable(drv_data);
241 cs_active(drv_data, chip);
244 dev_dbg(&drv_data->pdev->dev,
245 ": request chip select number %d failed\n",
246 chip->chip_select_num);
251 /* used to kick off transfer in rx mode */
252 static unsigned short dummy_read(struct driver_data *drv_data)
255 tmp = read_RDBR(drv_data);
259 static void null_writer(struct driver_data *drv_data)
261 u8 n_bytes = drv_data->n_bytes;
263 while (drv_data->tx < drv_data->tx_end) {
264 write_TDBR(drv_data, 0);
265 while ((read_STAT(drv_data) & BIT_STAT_TXS))
267 drv_data->tx += n_bytes;
271 static void null_reader(struct driver_data *drv_data)
273 u8 n_bytes = drv_data->n_bytes;
274 dummy_read(drv_data);
276 while (drv_data->rx < drv_data->rx_end) {
277 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
279 dummy_read(drv_data);
280 drv_data->rx += n_bytes;
284 static void u8_writer(struct driver_data *drv_data)
286 dev_dbg(&drv_data->pdev->dev,
287 "cr8-s is 0x%x\n", read_STAT(drv_data));
289 /* poll for SPI completion before start */
290 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
293 while (drv_data->tx < drv_data->tx_end) {
294 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
295 while (read_STAT(drv_data) & BIT_STAT_TXS)
301 static void u8_cs_chg_writer(struct driver_data *drv_data)
303 struct chip_data *chip = drv_data->cur_chip;
305 /* poll for SPI completion before start */
306 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
309 while (drv_data->tx < drv_data->tx_end) {
310 cs_active(drv_data, chip);
312 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
313 while (read_STAT(drv_data) & BIT_STAT_TXS)
316 cs_deactive(drv_data, chip);
322 static void u8_reader(struct driver_data *drv_data)
324 dev_dbg(&drv_data->pdev->dev,
325 "cr-8 is 0x%x\n", read_STAT(drv_data));
327 /* poll for SPI completion before start */
328 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
331 /* clear TDBR buffer before read(else it will be shifted out) */
332 write_TDBR(drv_data, 0xFFFF);
334 dummy_read(drv_data);
336 while (drv_data->rx < drv_data->rx_end - 1) {
337 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
339 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
343 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
345 *(u8 *) (drv_data->rx) = read_SHAW(drv_data);
349 static void u8_cs_chg_reader(struct driver_data *drv_data)
351 struct chip_data *chip = drv_data->cur_chip;
353 /* poll for SPI completion before start */
354 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
357 /* clear TDBR buffer before read(else it will be shifted out) */
358 write_TDBR(drv_data, 0xFFFF);
360 cs_active(drv_data, chip);
361 dummy_read(drv_data);
363 while (drv_data->rx < drv_data->rx_end - 1) {
364 cs_deactive(drv_data, chip);
366 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
368 cs_active(drv_data, chip);
369 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
372 cs_deactive(drv_data, chip);
374 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
376 *(u8 *) (drv_data->rx) = read_SHAW(drv_data);
380 static void u8_duplex(struct driver_data *drv_data)
382 /* poll for SPI completion before start */
383 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
386 /* in duplex mode, clk is triggered by writing of TDBR */
387 while (drv_data->rx < drv_data->rx_end) {
388 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
389 while (read_STAT(drv_data) & BIT_STAT_TXS)
391 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
393 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
399 static void u8_cs_chg_duplex(struct driver_data *drv_data)
401 struct chip_data *chip = drv_data->cur_chip;
403 /* poll for SPI completion before start */
404 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
407 while (drv_data->rx < drv_data->rx_end) {
408 cs_active(drv_data, chip);
410 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
411 while (read_STAT(drv_data) & BIT_STAT_TXS)
413 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
415 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
417 cs_deactive(drv_data, chip);
424 static void u16_writer(struct driver_data *drv_data)
426 dev_dbg(&drv_data->pdev->dev,
427 "cr16 is 0x%x\n", read_STAT(drv_data));
429 /* poll for SPI completion before start */
430 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
433 while (drv_data->tx < drv_data->tx_end) {
434 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
435 while ((read_STAT(drv_data) & BIT_STAT_TXS))
441 static void u16_cs_chg_writer(struct driver_data *drv_data)
443 struct chip_data *chip = drv_data->cur_chip;
445 /* poll for SPI completion before start */
446 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
449 while (drv_data->tx < drv_data->tx_end) {
450 cs_active(drv_data, chip);
452 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
453 while ((read_STAT(drv_data) & BIT_STAT_TXS))
456 cs_deactive(drv_data, chip);
462 static void u16_reader(struct driver_data *drv_data)
464 dev_dbg(&drv_data->pdev->dev,
465 "cr-16 is 0x%x\n", read_STAT(drv_data));
467 /* poll for SPI completion before start */
468 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
471 /* clear TDBR buffer before read(else it will be shifted out) */
472 write_TDBR(drv_data, 0xFFFF);
474 dummy_read(drv_data);
476 while (drv_data->rx < (drv_data->rx_end - 2)) {
477 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
479 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
483 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
485 *(u16 *) (drv_data->rx) = read_SHAW(drv_data);
489 static void u16_cs_chg_reader(struct driver_data *drv_data)
491 struct chip_data *chip = drv_data->cur_chip;
493 /* poll for SPI completion before start */
494 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
497 /* clear TDBR buffer before read(else it will be shifted out) */
498 write_TDBR(drv_data, 0xFFFF);
500 cs_active(drv_data, chip);
501 dummy_read(drv_data);
503 while (drv_data->rx < drv_data->rx_end - 2) {
504 cs_deactive(drv_data, chip);
506 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
508 cs_active(drv_data, chip);
509 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
512 cs_deactive(drv_data, chip);
514 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
516 *(u16 *) (drv_data->rx) = read_SHAW(drv_data);
520 static void u16_duplex(struct driver_data *drv_data)
522 /* poll for SPI completion before start */
523 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
526 /* in duplex mode, clk is triggered by writing of TDBR */
527 while (drv_data->tx < drv_data->tx_end) {
528 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
529 while (read_STAT(drv_data) & BIT_STAT_TXS)
531 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
533 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
539 static void u16_cs_chg_duplex(struct driver_data *drv_data)
541 struct chip_data *chip = drv_data->cur_chip;
543 /* poll for SPI completion before start */
544 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
547 while (drv_data->tx < drv_data->tx_end) {
548 cs_active(drv_data, chip);
550 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
551 while (read_STAT(drv_data) & BIT_STAT_TXS)
553 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
555 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
557 cs_deactive(drv_data, chip);
564 /* test if ther is more transfer to be done */
565 static void *next_transfer(struct driver_data *drv_data)
567 struct spi_message *msg = drv_data->cur_msg;
568 struct spi_transfer *trans = drv_data->cur_transfer;
570 /* Move to next transfer */
571 if (trans->transfer_list.next != &msg->transfers) {
572 drv_data->cur_transfer =
573 list_entry(trans->transfer_list.next,
574 struct spi_transfer, transfer_list);
575 return RUNNING_STATE;
581 * caller already set message->status;
582 * dma and pio irqs are blocked give finished message back
584 static void giveback(struct driver_data *drv_data)
586 struct chip_data *chip = drv_data->cur_chip;
587 struct spi_transfer *last_transfer;
589 struct spi_message *msg;
591 spin_lock_irqsave(&drv_data->lock, flags);
592 msg = drv_data->cur_msg;
593 drv_data->cur_msg = NULL;
594 drv_data->cur_transfer = NULL;
595 drv_data->cur_chip = NULL;
596 queue_work(drv_data->workqueue, &drv_data->pump_messages);
597 spin_unlock_irqrestore(&drv_data->lock, flags);
599 last_transfer = list_entry(msg->transfers.prev,
600 struct spi_transfer, transfer_list);
604 /* disable chip select signal. And not stop spi in autobuffer mode */
605 if (drv_data->tx_dma != 0xFFFF) {
606 cs_deactive(drv_data, chip);
607 bfin_spi_disable(drv_data);
610 if (!drv_data->cs_change)
611 cs_deactive(drv_data, chip);
614 msg->complete(msg->context);
617 static irqreturn_t dma_irq_handler(int irq, void *dev_id)
619 struct driver_data *drv_data = (struct driver_data *)dev_id;
620 struct chip_data *chip = drv_data->cur_chip;
621 struct spi_message *msg = drv_data->cur_msg;
623 dev_dbg(&drv_data->pdev->dev, "in dma_irq_handler\n");
624 clear_dma_irqstat(drv_data->dma_channel);
626 /* Wait for DMA to complete */
627 while (get_dma_curr_irqstat(drv_data->dma_channel) & DMA_RUN)
631 * wait for the last transaction shifted out. HRM states:
632 * at this point there may still be data in the SPI DMA FIFO waiting
633 * to be transmitted ... software needs to poll TXS in the SPI_STAT
634 * register until it goes low for 2 successive reads
636 if (drv_data->tx != NULL) {
637 while ((read_STAT(drv_data) & TXS) ||
638 (read_STAT(drv_data) & TXS))
642 while (!(read_STAT(drv_data) & SPIF))
645 msg->actual_length += drv_data->len_in_bytes;
647 if (drv_data->cs_change)
648 cs_deactive(drv_data, chip);
650 /* Move to next transfer */
651 msg->state = next_transfer(drv_data);
653 /* Schedule transfer tasklet */
654 tasklet_schedule(&drv_data->pump_transfers);
656 /* free the irq handler before next transfer */
657 dev_dbg(&drv_data->pdev->dev,
658 "disable dma channel irq%d\n",
659 drv_data->dma_channel);
660 dma_disable_irq(drv_data->dma_channel);
665 static void pump_transfers(unsigned long data)
667 struct driver_data *drv_data = (struct driver_data *)data;
668 struct spi_message *message = NULL;
669 struct spi_transfer *transfer = NULL;
670 struct spi_transfer *previous = NULL;
671 struct chip_data *chip = NULL;
673 u16 cr, dma_width, dma_config;
674 u32 tranf_success = 1;
676 /* Get current state information */
677 message = drv_data->cur_msg;
678 transfer = drv_data->cur_transfer;
679 chip = drv_data->cur_chip;
682 * if msg is error or done, report it back using complete() callback
685 /* Handle for abort */
686 if (message->state == ERROR_STATE) {
687 message->status = -EIO;
692 /* Handle end of message */
693 if (message->state == DONE_STATE) {
699 /* Delay if requested at end of transfer */
700 if (message->state == RUNNING_STATE) {
701 previous = list_entry(transfer->transfer_list.prev,
702 struct spi_transfer, transfer_list);
703 if (previous->delay_usecs)
704 udelay(previous->delay_usecs);
707 /* Setup the transfer state based on the type of transfer */
708 if (flush(drv_data) == 0) {
709 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
710 message->status = -EIO;
715 if (transfer->tx_buf != NULL) {
716 drv_data->tx = (void *)transfer->tx_buf;
717 drv_data->tx_end = drv_data->tx + transfer->len;
718 dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n",
719 transfer->tx_buf, drv_data->tx_end);
724 if (transfer->rx_buf != NULL) {
725 drv_data->rx = transfer->rx_buf;
726 drv_data->rx_end = drv_data->rx + transfer->len;
727 dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n",
728 transfer->rx_buf, drv_data->rx_end);
733 drv_data->rx_dma = transfer->rx_dma;
734 drv_data->tx_dma = transfer->tx_dma;
735 drv_data->len_in_bytes = transfer->len;
736 drv_data->cs_change = transfer->cs_change;
738 /* Bits per word setup */
739 switch (transfer->bits_per_word) {
741 drv_data->n_bytes = 1;
742 width = CFG_SPI_WORDSIZE8;
743 drv_data->read = chip->cs_change_per_word ?
744 u8_cs_chg_reader : u8_reader;
745 drv_data->write = chip->cs_change_per_word ?
746 u8_cs_chg_writer : u8_writer;
747 drv_data->duplex = chip->cs_change_per_word ?
748 u8_cs_chg_duplex : u8_duplex;
752 drv_data->n_bytes = 2;
753 width = CFG_SPI_WORDSIZE16;
754 drv_data->read = chip->cs_change_per_word ?
755 u16_cs_chg_reader : u16_reader;
756 drv_data->write = chip->cs_change_per_word ?
757 u16_cs_chg_writer : u16_writer;
758 drv_data->duplex = chip->cs_change_per_word ?
759 u16_cs_chg_duplex : u16_duplex;
763 /* No change, the same as default setting */
764 drv_data->n_bytes = chip->n_bytes;
766 drv_data->write = drv_data->tx ? chip->write : null_writer;
767 drv_data->read = drv_data->rx ? chip->read : null_reader;
768 drv_data->duplex = chip->duplex ? chip->duplex : null_writer;
771 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
773 write_CTRL(drv_data, cr);
775 if (width == CFG_SPI_WORDSIZE16) {
776 drv_data->len = (transfer->len) >> 1;
778 drv_data->len = transfer->len;
780 dev_dbg(&drv_data->pdev->dev, "transfer: ",
781 "drv_data->write is %p, chip->write is %p, null_wr is %p\n",
782 drv_data->write, chip->write, null_writer);
784 /* speed and width has been set on per message */
785 message->state = RUNNING_STATE;
788 /* Speed setup (surely valid because already checked) */
789 if (transfer->speed_hz)
790 write_BAUD(drv_data, hz_to_spi_baud(transfer->speed_hz));
792 write_BAUD(drv_data, chip->baud);
794 write_STAT(drv_data, BIT_STAT_CLR);
795 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
796 cs_active(drv_data, chip);
798 dev_dbg(&drv_data->pdev->dev,
799 "now pumping a transfer: width is %d, len is %d\n",
800 width, transfer->len);
803 * Try to map dma buffer and do a dma transfer if
804 * successful use different way to r/w according to
805 * drv_data->cur_chip->enable_dma
807 if (drv_data->cur_chip->enable_dma && drv_data->len > 6) {
809 disable_dma(drv_data->dma_channel);
810 clear_dma_irqstat(drv_data->dma_channel);
811 bfin_spi_disable(drv_data);
813 /* config dma channel */
814 dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
815 if (width == CFG_SPI_WORDSIZE16) {
816 set_dma_x_count(drv_data->dma_channel, drv_data->len);
817 set_dma_x_modify(drv_data->dma_channel, 2);
818 dma_width = WDSIZE_16;
820 set_dma_x_count(drv_data->dma_channel, drv_data->len);
821 set_dma_x_modify(drv_data->dma_channel, 1);
822 dma_width = WDSIZE_8;
825 /* poll for SPI completion before start */
826 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
829 /* dirty hack for autobuffer DMA mode */
830 if (drv_data->tx_dma == 0xFFFF) {
831 dev_dbg(&drv_data->pdev->dev,
832 "doing autobuffer DMA out.\n");
834 /* no irq in autobuffer mode */
836 (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
837 set_dma_config(drv_data->dma_channel, dma_config);
838 set_dma_start_addr(drv_data->dma_channel,
839 (unsigned long)drv_data->tx);
840 enable_dma(drv_data->dma_channel);
842 /* start SPI transfer */
844 (cr | CFG_SPI_DMAWRITE | BIT_CTL_ENABLE));
846 /* just return here, there can only be one transfer
854 /* In dma mode, rx or tx must be NULL in one transfer */
855 if (drv_data->rx != NULL) {
856 /* set transfer mode, and enable SPI */
857 dev_dbg(&drv_data->pdev->dev, "doing DMA in.\n");
859 /* clear tx reg soformer data is not shifted out */
860 write_TDBR(drv_data, 0xFFFF);
862 set_dma_x_count(drv_data->dma_channel, drv_data->len);
865 dma_enable_irq(drv_data->dma_channel);
866 dma_config = (WNR | RESTART | dma_width | DI_EN);
867 set_dma_config(drv_data->dma_channel, dma_config);
868 set_dma_start_addr(drv_data->dma_channel,
869 (unsigned long)drv_data->rx);
870 enable_dma(drv_data->dma_channel);
872 /* start SPI transfer */
874 (cr | CFG_SPI_DMAREAD | BIT_CTL_ENABLE));
876 } else if (drv_data->tx != NULL) {
877 dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
880 dma_enable_irq(drv_data->dma_channel);
881 dma_config = (RESTART | dma_width | DI_EN);
882 set_dma_config(drv_data->dma_channel, dma_config);
883 set_dma_start_addr(drv_data->dma_channel,
884 (unsigned long)drv_data->tx);
885 enable_dma(drv_data->dma_channel);
887 /* start SPI transfer */
889 (cr | CFG_SPI_DMAWRITE | BIT_CTL_ENABLE));
892 /* IO mode write then read */
893 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
895 if (drv_data->tx != NULL && drv_data->rx != NULL) {
896 /* full duplex mode */
897 BUG_ON((drv_data->tx_end - drv_data->tx) !=
898 (drv_data->rx_end - drv_data->rx));
899 dev_dbg(&drv_data->pdev->dev,
900 "IO duplex: cr is 0x%x\n", cr);
902 /* set SPI transfer mode */
903 write_CTRL(drv_data, (cr | CFG_SPI_WRITE));
905 drv_data->duplex(drv_data);
907 if (drv_data->tx != drv_data->tx_end)
909 } else if (drv_data->tx != NULL) {
910 /* write only half duplex */
911 dev_dbg(&drv_data->pdev->dev,
912 "IO write: cr is 0x%x\n", cr);
914 /* set SPI transfer mode */
915 write_CTRL(drv_data, (cr | CFG_SPI_WRITE));
917 drv_data->write(drv_data);
919 if (drv_data->tx != drv_data->tx_end)
921 } else if (drv_data->rx != NULL) {
922 /* read only half duplex */
923 dev_dbg(&drv_data->pdev->dev,
924 "IO read: cr is 0x%x\n", cr);
926 /* set SPI transfer mode */
927 write_CTRL(drv_data, (cr | CFG_SPI_READ));
929 drv_data->read(drv_data);
930 if (drv_data->rx != drv_data->rx_end)
934 if (!tranf_success) {
935 dev_dbg(&drv_data->pdev->dev,
936 "IO write error!\n");
937 message->state = ERROR_STATE;
939 /* Update total byte transfered */
940 message->actual_length += drv_data->len;
942 /* Move to next transfer of this msg */
943 message->state = next_transfer(drv_data);
946 /* Schedule next transfer tasklet */
947 tasklet_schedule(&drv_data->pump_transfers);
952 /* pop a msg from queue and kick off real transfer */
953 static void pump_messages(struct work_struct *work)
955 struct driver_data *drv_data;
958 drv_data = container_of(work, struct driver_data, pump_messages);
960 /* Lock queue and check for queue work */
961 spin_lock_irqsave(&drv_data->lock, flags);
962 if (list_empty(&drv_data->queue) || drv_data->run == QUEUE_STOPPED) {
963 /* pumper kicked off but no work to do */
965 spin_unlock_irqrestore(&drv_data->lock, flags);
969 /* Make sure we are not already running a message */
970 if (drv_data->cur_msg) {
971 spin_unlock_irqrestore(&drv_data->lock, flags);
975 /* Extract head of queue */
976 drv_data->cur_msg = list_entry(drv_data->queue.next,
977 struct spi_message, queue);
979 /* Setup the SSP using the per chip configuration */
980 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
981 if (restore_state(drv_data)) {
982 spin_unlock_irqrestore(&drv_data->lock, flags);
986 list_del_init(&drv_data->cur_msg->queue);
988 /* Initial message state */
989 drv_data->cur_msg->state = START_STATE;
990 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
991 struct spi_transfer, transfer_list);
993 dev_dbg(&drv_data->pdev->dev, "got a message to pump, "
994 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
995 drv_data->cur_chip->baud, drv_data->cur_chip->flag,
996 drv_data->cur_chip->ctl_reg);
998 dev_dbg(&drv_data->pdev->dev,
999 "the first transfer len is %d\n",
1000 drv_data->cur_transfer->len);
1002 /* Mark as busy and launch transfers */
1003 tasklet_schedule(&drv_data->pump_transfers);
1006 spin_unlock_irqrestore(&drv_data->lock, flags);
1010 * got a msg to transfer, queue it in drv_data->queue.
1011 * And kick off message pumper
1013 static int transfer(struct spi_device *spi, struct spi_message *msg)
1015 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1016 unsigned long flags;
1018 spin_lock_irqsave(&drv_data->lock, flags);
1020 if (drv_data->run == QUEUE_STOPPED) {
1021 spin_unlock_irqrestore(&drv_data->lock, flags);
1025 msg->actual_length = 0;
1026 msg->status = -EINPROGRESS;
1027 msg->state = START_STATE;
1029 dev_dbg(&spi->dev, "adding an msg in transfer() \n");
1030 list_add_tail(&msg->queue, &drv_data->queue);
1032 if (drv_data->run == QUEUE_RUNNING && !drv_data->busy)
1033 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1035 spin_unlock_irqrestore(&drv_data->lock, flags);
1040 #define MAX_SPI_SSEL 7
1042 static u16 ssel[3][MAX_SPI_SSEL] = {
1043 {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
1044 P_SPI0_SSEL4, P_SPI0_SSEL5,
1045 P_SPI0_SSEL6, P_SPI0_SSEL7},
1047 {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
1048 P_SPI1_SSEL4, P_SPI1_SSEL5,
1049 P_SPI1_SSEL6, P_SPI1_SSEL7},
1051 {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
1052 P_SPI2_SSEL4, P_SPI2_SSEL5,
1053 P_SPI2_SSEL6, P_SPI2_SSEL7},
1056 /* first setup for new devices */
1057 static int setup(struct spi_device *spi)
1059 struct bfin5xx_spi_chip *chip_info = NULL;
1060 struct chip_data *chip;
1061 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1064 /* Abort device setup if requested features are not supported */
1065 if (spi->mode & ~(SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST)) {
1066 dev_err(&spi->dev, "requested mode not fully supported\n");
1070 /* Zero (the default) here means 8 bits */
1071 if (!spi->bits_per_word)
1072 spi->bits_per_word = 8;
1074 if (spi->bits_per_word != 8 && spi->bits_per_word != 16)
1077 /* Only alloc (or use chip_info) on first setup */
1078 chip = spi_get_ctldata(spi);
1080 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1084 chip->enable_dma = 0;
1085 chip_info = spi->controller_data;
1088 /* chip_info isn't always needed */
1090 /* Make sure people stop trying to set fields via ctl_reg
1091 * when they should actually be using common SPI framework.
1092 * Currently we let through: WOM EMISO PSSE GM SZ TIMOD.
1093 * Not sure if a user actually needs/uses any of these,
1094 * but let's assume (for now) they do.
1096 if (chip_info->ctl_reg & (SPE|MSTR|CPOL|CPHA|LSBF|SIZE)) {
1097 dev_err(&spi->dev, "do not set bits in ctl_reg "
1098 "that the SPI framework manages\n");
1102 chip->enable_dma = chip_info->enable_dma != 0
1103 && drv_data->master_info->enable_dma;
1104 chip->ctl_reg = chip_info->ctl_reg;
1105 chip->bits_per_word = chip_info->bits_per_word;
1106 chip->cs_change_per_word = chip_info->cs_change_per_word;
1107 chip->cs_chg_udelay = chip_info->cs_chg_udelay;
1110 /* translate common spi framework into our register */
1111 if (spi->mode & SPI_CPOL)
1112 chip->ctl_reg |= CPOL;
1113 if (spi->mode & SPI_CPHA)
1114 chip->ctl_reg |= CPHA;
1115 if (spi->mode & SPI_LSB_FIRST)
1116 chip->ctl_reg |= LSBF;
1117 /* we dont support running in slave mode (yet?) */
1118 chip->ctl_reg |= MSTR;
1121 * if any one SPI chip is registered and wants DMA, request the
1122 * DMA channel for it
1124 if (chip->enable_dma && !drv_data->dma_requested) {
1125 /* register dma irq handler */
1126 if (request_dma(drv_data->dma_channel, "BF53x_SPI_DMA") < 0) {
1128 "Unable to request BlackFin SPI DMA channel\n");
1131 if (set_dma_callback(drv_data->dma_channel,
1132 (void *)dma_irq_handler, drv_data) < 0) {
1133 dev_dbg(&spi->dev, "Unable to set dma callback\n");
1136 dma_disable_irq(drv_data->dma_channel);
1137 drv_data->dma_requested = 1;
1141 * Notice: for blackfin, the speed_hz is the value of register
1142 * SPI_BAUD, not the real baudrate
1144 chip->baud = hz_to_spi_baud(spi->max_speed_hz);
1145 spi_flg = ~(1 << (spi->chip_select));
1146 chip->flag = ((u16) spi_flg << 8) | (1 << (spi->chip_select));
1147 chip->chip_select_num = spi->chip_select;
1149 switch (chip->bits_per_word) {
1152 chip->width = CFG_SPI_WORDSIZE8;
1153 chip->read = chip->cs_change_per_word ?
1154 u8_cs_chg_reader : u8_reader;
1155 chip->write = chip->cs_change_per_word ?
1156 u8_cs_chg_writer : u8_writer;
1157 chip->duplex = chip->cs_change_per_word ?
1158 u8_cs_chg_duplex : u8_duplex;
1163 chip->width = CFG_SPI_WORDSIZE16;
1164 chip->read = chip->cs_change_per_word ?
1165 u16_cs_chg_reader : u16_reader;
1166 chip->write = chip->cs_change_per_word ?
1167 u16_cs_chg_writer : u16_writer;
1168 chip->duplex = chip->cs_change_per_word ?
1169 u16_cs_chg_duplex : u16_duplex;
1173 dev_err(&spi->dev, "%d bits_per_word is not supported\n",
1174 chip->bits_per_word);
1179 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
1180 spi->modalias, chip->width, chip->enable_dma);
1181 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
1182 chip->ctl_reg, chip->flag);
1184 spi_set_ctldata(spi, chip);
1186 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
1187 if ((chip->chip_select_num > 0)
1188 && (chip->chip_select_num <= spi->master->num_chipselect))
1189 peripheral_request(ssel[spi->master->bus_num]
1190 [chip->chip_select_num-1], DRV_NAME);
1192 cs_deactive(drv_data, chip);
1198 * callback for spi framework.
1199 * clean driver specific data
1201 static void cleanup(struct spi_device *spi)
1203 struct chip_data *chip = spi_get_ctldata(spi);
1205 if ((chip->chip_select_num > 0)
1206 && (chip->chip_select_num <= spi->master->num_chipselect))
1207 peripheral_free(ssel[spi->master->bus_num]
1208 [chip->chip_select_num-1]);
1213 static inline int init_queue(struct driver_data *drv_data)
1215 INIT_LIST_HEAD(&drv_data->queue);
1216 spin_lock_init(&drv_data->lock);
1218 drv_data->run = QUEUE_STOPPED;
1221 /* init transfer tasklet */
1222 tasklet_init(&drv_data->pump_transfers,
1223 pump_transfers, (unsigned long)drv_data);
1225 /* init messages workqueue */
1226 INIT_WORK(&drv_data->pump_messages, pump_messages);
1227 drv_data->workqueue =
1228 create_singlethread_workqueue(drv_data->master->dev.parent->bus_id);
1229 if (drv_data->workqueue == NULL)
1235 static inline int start_queue(struct driver_data *drv_data)
1237 unsigned long flags;
1239 spin_lock_irqsave(&drv_data->lock, flags);
1241 if (drv_data->run == QUEUE_RUNNING || drv_data->busy) {
1242 spin_unlock_irqrestore(&drv_data->lock, flags);
1246 drv_data->run = QUEUE_RUNNING;
1247 drv_data->cur_msg = NULL;
1248 drv_data->cur_transfer = NULL;
1249 drv_data->cur_chip = NULL;
1250 spin_unlock_irqrestore(&drv_data->lock, flags);
1252 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1257 static inline int stop_queue(struct driver_data *drv_data)
1259 unsigned long flags;
1260 unsigned limit = 500;
1263 spin_lock_irqsave(&drv_data->lock, flags);
1266 * This is a bit lame, but is optimized for the common execution path.
1267 * A wait_queue on the drv_data->busy could be used, but then the common
1268 * execution path (pump_messages) would be required to call wake_up or
1269 * friends on every SPI message. Do this instead
1271 drv_data->run = QUEUE_STOPPED;
1272 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1273 spin_unlock_irqrestore(&drv_data->lock, flags);
1275 spin_lock_irqsave(&drv_data->lock, flags);
1278 if (!list_empty(&drv_data->queue) || drv_data->busy)
1281 spin_unlock_irqrestore(&drv_data->lock, flags);
1286 static inline int destroy_queue(struct driver_data *drv_data)
1290 status = stop_queue(drv_data);
1294 destroy_workqueue(drv_data->workqueue);
1299 static int __init bfin5xx_spi_probe(struct platform_device *pdev)
1301 struct device *dev = &pdev->dev;
1302 struct bfin5xx_spi_master *platform_info;
1303 struct spi_master *master;
1304 struct driver_data *drv_data = 0;
1305 struct resource *res;
1308 platform_info = dev->platform_data;
1310 /* Allocate master with space for drv_data */
1311 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1313 dev_err(&pdev->dev, "can not alloc spi_master\n");
1317 drv_data = spi_master_get_devdata(master);
1318 drv_data->master = master;
1319 drv_data->master_info = platform_info;
1320 drv_data->pdev = pdev;
1321 drv_data->pin_req = platform_info->pin_req;
1323 master->bus_num = pdev->id;
1324 master->num_chipselect = platform_info->num_chipselect;
1325 master->cleanup = cleanup;
1326 master->setup = setup;
1327 master->transfer = transfer;
1329 /* Find and map our resources */
1330 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1332 dev_err(dev, "Cannot get IORESOURCE_MEM\n");
1334 goto out_error_get_res;
1337 drv_data->regs_base = ioremap(res->start, (res->end - res->start + 1));
1338 if (drv_data->regs_base == NULL) {
1339 dev_err(dev, "Cannot map IO\n");
1341 goto out_error_ioremap;
1344 drv_data->dma_channel = platform_get_irq(pdev, 0);
1345 if (drv_data->dma_channel < 0) {
1346 dev_err(dev, "No DMA channel specified\n");
1348 goto out_error_no_dma_ch;
1351 /* Initial and start queue */
1352 status = init_queue(drv_data);
1354 dev_err(dev, "problem initializing queue\n");
1355 goto out_error_queue_alloc;
1358 status = start_queue(drv_data);
1360 dev_err(dev, "problem starting queue\n");
1361 goto out_error_queue_alloc;
1364 /* Register with the SPI framework */
1365 platform_set_drvdata(pdev, drv_data);
1366 status = spi_register_master(master);
1368 dev_err(dev, "problem registering spi master\n");
1369 goto out_error_queue_alloc;
1372 status = peripheral_request_list(drv_data->pin_req, DRV_NAME);
1374 dev_err(&pdev->dev, ": Requesting Peripherals failed\n");
1378 dev_info(dev, "%s, Version %s, regs_base@%p, dma channel@%d\n",
1379 DRV_DESC, DRV_VERSION, drv_data->regs_base,
1380 drv_data->dma_channel);
1383 out_error_queue_alloc:
1384 destroy_queue(drv_data);
1385 out_error_no_dma_ch:
1386 iounmap((void *) drv_data->regs_base);
1390 spi_master_put(master);
1395 /* stop hardware and remove the driver */
1396 static int __devexit bfin5xx_spi_remove(struct platform_device *pdev)
1398 struct driver_data *drv_data = platform_get_drvdata(pdev);
1404 /* Remove the queue */
1405 status = destroy_queue(drv_data);
1409 /* Disable the SSP at the peripheral and SOC level */
1410 bfin_spi_disable(drv_data);
1413 if (drv_data->master_info->enable_dma) {
1414 if (dma_channel_active(drv_data->dma_channel))
1415 free_dma(drv_data->dma_channel);
1418 /* Disconnect from the SPI framework */
1419 spi_unregister_master(drv_data->master);
1421 peripheral_free_list(drv_data->pin_req);
1423 /* Prevent double remove */
1424 platform_set_drvdata(pdev, NULL);
1430 static int bfin5xx_spi_suspend(struct platform_device *pdev, pm_message_t state)
1432 struct driver_data *drv_data = platform_get_drvdata(pdev);
1435 status = stop_queue(drv_data);
1440 bfin_spi_disable(drv_data);
1445 static int bfin5xx_spi_resume(struct platform_device *pdev)
1447 struct driver_data *drv_data = platform_get_drvdata(pdev);
1450 /* Enable the SPI interface */
1451 bfin_spi_enable(drv_data);
1453 /* Start the queue running */
1454 status = start_queue(drv_data);
1456 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1463 #define bfin5xx_spi_suspend NULL
1464 #define bfin5xx_spi_resume NULL
1465 #endif /* CONFIG_PM */
1467 MODULE_ALIAS("bfin-spi-master"); /* for platform bus hotplug */
1468 static struct platform_driver bfin5xx_spi_driver = {
1471 .owner = THIS_MODULE,
1473 .suspend = bfin5xx_spi_suspend,
1474 .resume = bfin5xx_spi_resume,
1475 .remove = __devexit_p(bfin5xx_spi_remove),
1478 static int __init bfin5xx_spi_init(void)
1480 return platform_driver_probe(&bfin5xx_spi_driver, bfin5xx_spi_probe);
1482 module_init(bfin5xx_spi_init);
1484 static void __exit bfin5xx_spi_exit(void)
1486 platform_driver_unregister(&bfin5xx_spi_driver);
1488 module_exit(bfin5xx_spi_exit);