2 * File: drivers/spi/bfin5xx_spi.c
4 * Author: Luke Yang (Analog Devices Inc.)
6 * Created: March. 10th 2006
7 * Description: SPI controller driver for Blackfin 5xx
8 * Bugs: Enter bugs at http://blackfin.uclinux.org/
11 * March 10, 2006 bfin5xx_spi.c Created. (Luke Yang)
12 * August 7, 2006 added full duplex mode (Axel Weiss & Luke Yang)
14 * Copyright 2004-2006 Analog Devices Inc.
16 * This program is free software ; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation ; either version 2, or (at your option)
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY ; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program ; see the file COPYING.
28 * If not, write to the Free Software Foundation,
29 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #include <linux/init.h>
33 #include <linux/module.h>
34 #include <linux/device.h>
35 #include <linux/ioport.h>
36 #include <linux/errno.h>
37 #include <linux/interrupt.h>
38 #include <linux/platform_device.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/spi/spi.h>
41 #include <linux/workqueue.h>
42 #include <linux/delay.h>
46 #include <asm/delay.h>
49 #include <asm/bfin5xx_spi.h>
51 MODULE_AUTHOR("Luke Yang");
52 MODULE_DESCRIPTION("Blackfin 5xx SPI Contoller");
53 MODULE_LICENSE("GPL");
55 #define IS_DMA_ALIGNED(x) (((u32)(x)&0x07)==0)
57 #define DEFINE_SPI_REG(reg, off) \
58 static inline u16 read_##reg(void) \
59 { return *(volatile unsigned short*)(SPI0_REGBASE + off); } \
60 static inline void write_##reg(u16 v) \
61 {*(volatile unsigned short*)(SPI0_REGBASE + off) = v;\
64 DEFINE_SPI_REG(CTRL
, 0x00)
65 DEFINE_SPI_REG(FLAG
, 0x04)
66 DEFINE_SPI_REG(STAT
, 0x08)
67 DEFINE_SPI_REG(TDBR
, 0x0C)
68 DEFINE_SPI_REG(RDBR
, 0x10)
69 DEFINE_SPI_REG(BAUD
, 0x14)
70 DEFINE_SPI_REG(SHAW
, 0x18)
71 #define START_STATE ((void*)0)
72 #define RUNNING_STATE ((void*)1)
73 #define DONE_STATE ((void*)2)
74 #define ERROR_STATE ((void*)-1)
75 #define QUEUE_RUNNING 0
76 #define QUEUE_STOPPED 1
80 /* Driver model hookup */
81 struct platform_device
*pdev
;
83 /* SPI framework hookup */
84 struct spi_master
*master
;
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
;
116 void (*write
) (struct driver_data
*);
117 void (*read
) (struct driver_data
*);
118 void (*duplex
) (struct driver_data
*);
128 u8 width
; /* 0 or 1 */
130 u8 bits_per_word
; /* 8 or 16 */
131 u8 cs_change_per_word
;
133 void (*write
) (struct driver_data
*);
134 void (*read
) (struct driver_data
*);
135 void (*duplex
) (struct driver_data
*);
138 static void bfin_spi_enable(struct driver_data
*drv_data
)
143 write_CTRL(cr
| BIT_CTL_ENABLE
);
147 static void bfin_spi_disable(struct driver_data
*drv_data
)
152 write_CTRL(cr
& (~BIT_CTL_ENABLE
));
156 /* Caculate the SPI_BAUD register value based on input HZ */
157 static u16
hz_to_spi_baud(u32 speed_hz
)
159 u_long sclk
= get_sclk();
160 u16 spi_baud
= (sclk
/ (2 * speed_hz
));
162 if ((sclk
% (2 * speed_hz
)) > 0)
168 static int flush(struct driver_data
*drv_data
)
170 unsigned long limit
= loops_per_jiffy
<< 1;
172 /* wait for stop and clear stat */
173 while (!(read_STAT() & BIT_STAT_SPIF
) && limit
--)
176 write_STAT(BIT_STAT_CLR
);
181 /* stop controller and re-config current chip*/
182 static void restore_state(struct driver_data
*drv_data
)
184 struct chip_data
*chip
= drv_data
->cur_chip
;
186 /* Clear status and disable clock */
187 write_STAT(BIT_STAT_CLR
);
188 bfin_spi_disable(drv_data
);
189 dev_dbg(&drv_data
->pdev
->dev
, "restoring spi ctl state\n");
191 #if defined(CONFIG_BF534) || defined(CONFIG_BF536) || defined(CONFIG_BF537)
192 dev_dbg(&drv_data
->pdev
->dev
,
193 "chip select number is %d\n", chip
->chip_select_num
);
195 switch (chip
->chip_select_num
) {
197 bfin_write_PORTF_FER(bfin_read_PORTF_FER() | 0x3c00);
203 bfin_write_PORT_MUX(bfin_read_PORT_MUX() | PJSE_SPI
);
205 bfin_write_PORTF_FER(bfin_read_PORTF_FER() | 0x3800);
210 bfin_write_PORT_MUX(bfin_read_PORT_MUX() | PFS4E_SPI
);
212 bfin_write_PORTF_FER(bfin_read_PORTF_FER() | 0x3840);
217 bfin_write_PORT_MUX(bfin_read_PORT_MUX() | PFS5E_SPI
);
219 bfin_write_PORTF_FER(bfin_read_PORTF_FER() | 0x3820);
224 bfin_write_PORT_MUX(bfin_read_PORT_MUX() | PFS6E_SPI
);
226 bfin_write_PORTF_FER(bfin_read_PORTF_FER() | 0x3810);
231 bfin_write_PORT_MUX(bfin_read_PORT_MUX() | PJCE_SPI
);
233 bfin_write_PORTF_FER(bfin_read_PORTF_FER() | 0x3800);
239 /* Load the registers */
240 write_CTRL(chip
->ctl_reg
);
241 write_BAUD(chip
->baud
);
242 write_FLAG(chip
->flag
);
245 /* used to kick off transfer in rx mode */
246 static unsigned short dummy_read(void)
253 static void null_writer(struct driver_data
*drv_data
)
255 u8 n_bytes
= drv_data
->n_bytes
;
257 while (drv_data
->tx
< drv_data
->tx_end
) {
259 while ((read_STAT() & BIT_STAT_TXS
))
261 drv_data
->tx
+= n_bytes
;
265 static void null_reader(struct driver_data
*drv_data
)
267 u8 n_bytes
= drv_data
->n_bytes
;
270 while (drv_data
->rx
< drv_data
->rx_end
) {
271 while (!(read_STAT() & BIT_STAT_RXS
))
274 drv_data
->rx
+= n_bytes
;
278 static void u8_writer(struct driver_data
*drv_data
)
280 dev_dbg(&drv_data
->pdev
->dev
,
281 "cr8-s is 0x%x\n", read_STAT());
282 while (drv_data
->tx
< drv_data
->tx_end
) {
283 write_TDBR(*(u8
*) (drv_data
->tx
));
284 while (read_STAT() & BIT_STAT_TXS
)
289 /* poll for SPI completion before returning */
290 while (!(read_STAT() & BIT_STAT_SPIF
))
294 static void u8_cs_chg_writer(struct driver_data
*drv_data
)
296 struct chip_data
*chip
= drv_data
->cur_chip
;
298 while (drv_data
->tx
< drv_data
->tx_end
) {
299 write_FLAG(chip
->flag
);
302 write_TDBR(*(u8
*) (drv_data
->tx
));
303 while (read_STAT() & BIT_STAT_TXS
)
305 while (!(read_STAT() & BIT_STAT_SPIF
))
307 write_FLAG(0xFF00 | chip
->flag
);
309 if (chip
->cs_chg_udelay
)
310 udelay(chip
->cs_chg_udelay
);
317 static void u8_reader(struct driver_data
*drv_data
)
319 dev_dbg(&drv_data
->pdev
->dev
,
320 "cr-8 is 0x%x\n", read_STAT());
322 /* clear TDBR buffer before read(else it will be shifted out) */
327 while (drv_data
->rx
< drv_data
->rx_end
- 1) {
328 while (!(read_STAT() & BIT_STAT_RXS
))
330 *(u8
*) (drv_data
->rx
) = read_RDBR();
334 while (!(read_STAT() & BIT_STAT_RXS
))
336 *(u8
*) (drv_data
->rx
) = read_SHAW();
340 static void u8_cs_chg_reader(struct driver_data
*drv_data
)
342 struct chip_data
*chip
= drv_data
->cur_chip
;
344 while (drv_data
->rx
< drv_data
->rx_end
) {
345 write_FLAG(chip
->flag
);
348 read_RDBR(); /* kick off */
349 while (!(read_STAT() & BIT_STAT_RXS
))
351 while (!(read_STAT() & BIT_STAT_SPIF
))
353 *(u8
*) (drv_data
->rx
) = read_SHAW();
354 write_FLAG(0xFF00 | chip
->flag
);
356 if (chip
->cs_chg_udelay
)
357 udelay(chip
->cs_chg_udelay
);
364 static void u8_duplex(struct driver_data
*drv_data
)
366 /* in duplex mode, clk is triggered by writing of TDBR */
367 while (drv_data
->rx
< drv_data
->rx_end
) {
368 write_TDBR(*(u8
*) (drv_data
->tx
));
369 while (!(read_STAT() & BIT_STAT_SPIF
))
371 while (!(read_STAT() & BIT_STAT_RXS
))
373 *(u8
*) (drv_data
->rx
) = read_RDBR();
379 static void u8_cs_chg_duplex(struct driver_data
*drv_data
)
381 struct chip_data
*chip
= drv_data
->cur_chip
;
383 while (drv_data
->rx
< drv_data
->rx_end
) {
384 write_FLAG(chip
->flag
);
387 write_TDBR(*(u8
*) (drv_data
->tx
));
388 while (!(read_STAT() & BIT_STAT_SPIF
))
390 while (!(read_STAT() & BIT_STAT_RXS
))
392 *(u8
*) (drv_data
->rx
) = read_RDBR();
393 write_FLAG(0xFF00 | chip
->flag
);
395 if (chip
->cs_chg_udelay
)
396 udelay(chip
->cs_chg_udelay
);
404 static void u16_writer(struct driver_data
*drv_data
)
406 dev_dbg(&drv_data
->pdev
->dev
,
407 "cr16 is 0x%x\n", read_STAT());
409 while (drv_data
->tx
< drv_data
->tx_end
) {
410 write_TDBR(*(u16
*) (drv_data
->tx
));
411 while ((read_STAT() & BIT_STAT_TXS
))
416 /* poll for SPI completion before returning */
417 while (!(read_STAT() & BIT_STAT_SPIF
))
421 static void u16_cs_chg_writer(struct driver_data
*drv_data
)
423 struct chip_data
*chip
= drv_data
->cur_chip
;
425 while (drv_data
->tx
< drv_data
->tx_end
) {
426 write_FLAG(chip
->flag
);
429 write_TDBR(*(u16
*) (drv_data
->tx
));
430 while ((read_STAT() & BIT_STAT_TXS
))
432 while (!(read_STAT() & BIT_STAT_SPIF
))
434 write_FLAG(0xFF00 | chip
->flag
);
436 if (chip
->cs_chg_udelay
)
437 udelay(chip
->cs_chg_udelay
);
444 static void u16_reader(struct driver_data
*drv_data
)
446 dev_dbg(&drv_data
->pdev
->dev
,
447 "cr-16 is 0x%x\n", read_STAT());
450 while (drv_data
->rx
< (drv_data
->rx_end
- 2)) {
451 while (!(read_STAT() & BIT_STAT_RXS
))
453 *(u16
*) (drv_data
->rx
) = read_RDBR();
457 while (!(read_STAT() & BIT_STAT_RXS
))
459 *(u16
*) (drv_data
->rx
) = read_SHAW();
463 static void u16_cs_chg_reader(struct driver_data
*drv_data
)
465 struct chip_data
*chip
= drv_data
->cur_chip
;
467 while (drv_data
->rx
< drv_data
->rx_end
) {
468 write_FLAG(chip
->flag
);
471 read_RDBR(); /* kick off */
472 while (!(read_STAT() & BIT_STAT_RXS
))
474 while (!(read_STAT() & BIT_STAT_SPIF
))
476 *(u16
*) (drv_data
->rx
) = read_SHAW();
477 write_FLAG(0xFF00 | chip
->flag
);
479 if (chip
->cs_chg_udelay
)
480 udelay(chip
->cs_chg_udelay
);
487 static void u16_duplex(struct driver_data
*drv_data
)
489 /* in duplex mode, clk is triggered by writing of TDBR */
490 while (drv_data
->tx
< drv_data
->tx_end
) {
491 write_TDBR(*(u16
*) (drv_data
->tx
));
492 while (!(read_STAT() & BIT_STAT_SPIF
))
494 while (!(read_STAT() & BIT_STAT_RXS
))
496 *(u16
*) (drv_data
->rx
) = read_RDBR();
502 static void u16_cs_chg_duplex(struct driver_data
*drv_data
)
504 struct chip_data
*chip
= drv_data
->cur_chip
;
506 while (drv_data
->tx
< drv_data
->tx_end
) {
507 write_FLAG(chip
->flag
);
510 write_TDBR(*(u16
*) (drv_data
->tx
));
511 while (!(read_STAT() & BIT_STAT_SPIF
))
513 while (!(read_STAT() & BIT_STAT_RXS
))
515 *(u16
*) (drv_data
->rx
) = read_RDBR();
516 write_FLAG(0xFF00 | chip
->flag
);
518 if (chip
->cs_chg_udelay
)
519 udelay(chip
->cs_chg_udelay
);
527 /* test if ther is more transfer to be done */
528 static void *next_transfer(struct driver_data
*drv_data
)
530 struct spi_message
*msg
= drv_data
->cur_msg
;
531 struct spi_transfer
*trans
= drv_data
->cur_transfer
;
533 /* Move to next transfer */
534 if (trans
->transfer_list
.next
!= &msg
->transfers
) {
535 drv_data
->cur_transfer
=
536 list_entry(trans
->transfer_list
.next
,
537 struct spi_transfer
, transfer_list
);
538 return RUNNING_STATE
;
544 * caller already set message->status;
545 * dma and pio irqs are blocked give finished message back
547 static void giveback(struct driver_data
*drv_data
)
549 struct spi_transfer
*last_transfer
;
551 struct spi_message
*msg
;
553 spin_lock_irqsave(&drv_data
->lock
, flags
);
554 msg
= drv_data
->cur_msg
;
555 drv_data
->cur_msg
= NULL
;
556 drv_data
->cur_transfer
= NULL
;
557 drv_data
->cur_chip
= NULL
;
558 queue_work(drv_data
->workqueue
, &drv_data
->pump_messages
);
559 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
561 last_transfer
= list_entry(msg
->transfers
.prev
,
562 struct spi_transfer
, transfer_list
);
566 /* disable chip select signal. And not stop spi in autobuffer mode */
567 if (drv_data
->tx_dma
!= 0xFFFF) {
569 bfin_spi_disable(drv_data
);
573 msg
->complete(msg
->context
);
576 static irqreturn_t
dma_irq_handler(int irq
, void *dev_id
)
578 struct driver_data
*drv_data
= (struct driver_data
*)dev_id
;
579 struct spi_message
*msg
= drv_data
->cur_msg
;
581 dev_dbg(&drv_data
->pdev
->dev
, "in dma_irq_handler\n");
582 clear_dma_irqstat(CH_SPI
);
584 /* Wait for DMA to complete */
585 while (get_dma_curr_irqstat(CH_SPI
) & DMA_RUN
)
589 * wait for the last transaction shifted out. HRM states:
590 * at this point there may still be data in the SPI DMA FIFO waiting
591 * to be transmitted ... software needs to poll TXS in the SPI_STAT
592 * register until it goes low for 2 successive reads
594 if (drv_data
->tx
!= NULL
) {
595 while ((bfin_read_SPI_STAT() & TXS
) ||
596 (bfin_read_SPI_STAT() & TXS
))
600 while (!(bfin_read_SPI_STAT() & SPIF
))
603 bfin_spi_disable(drv_data
);
605 msg
->actual_length
+= drv_data
->len_in_bytes
;
607 /* Move to next transfer */
608 msg
->state
= next_transfer(drv_data
);
610 /* Schedule transfer tasklet */
611 tasklet_schedule(&drv_data
->pump_transfers
);
613 /* free the irq handler before next transfer */
614 dev_dbg(&drv_data
->pdev
->dev
,
615 "disable dma channel irq%d\n",
617 dma_disable_irq(CH_SPI
);
622 static void pump_transfers(unsigned long data
)
624 struct driver_data
*drv_data
= (struct driver_data
*)data
;
625 struct spi_message
*message
= NULL
;
626 struct spi_transfer
*transfer
= NULL
;
627 struct spi_transfer
*previous
= NULL
;
628 struct chip_data
*chip
= NULL
;
630 u16 cr
, dma_width
, dma_config
;
631 u32 tranf_success
= 1;
633 /* Get current state information */
634 message
= drv_data
->cur_msg
;
635 transfer
= drv_data
->cur_transfer
;
636 chip
= drv_data
->cur_chip
;
639 * if msg is error or done, report it back using complete() callback
642 /* Handle for abort */
643 if (message
->state
== ERROR_STATE
) {
644 message
->status
= -EIO
;
649 /* Handle end of message */
650 if (message
->state
== DONE_STATE
) {
656 /* Delay if requested at end of transfer */
657 if (message
->state
== RUNNING_STATE
) {
658 previous
= list_entry(transfer
->transfer_list
.prev
,
659 struct spi_transfer
, transfer_list
);
660 if (previous
->delay_usecs
)
661 udelay(previous
->delay_usecs
);
664 /* Setup the transfer state based on the type of transfer */
665 if (flush(drv_data
) == 0) {
666 dev_err(&drv_data
->pdev
->dev
, "pump_transfers: flush failed\n");
667 message
->status
= -EIO
;
672 if (transfer
->tx_buf
!= NULL
) {
673 drv_data
->tx
= (void *)transfer
->tx_buf
;
674 drv_data
->tx_end
= drv_data
->tx
+ transfer
->len
;
675 dev_dbg(&drv_data
->pdev
->dev
, "tx_buf is %p, tx_end is %p\n",
676 transfer
->tx_buf
, drv_data
->tx_end
);
681 if (transfer
->rx_buf
!= NULL
) {
682 drv_data
->rx
= transfer
->rx_buf
;
683 drv_data
->rx_end
= drv_data
->rx
+ transfer
->len
;
684 dev_dbg(&drv_data
->pdev
->dev
, "rx_buf is %p, rx_end is %p\n",
685 transfer
->rx_buf
, drv_data
->rx_end
);
690 drv_data
->rx_dma
= transfer
->rx_dma
;
691 drv_data
->tx_dma
= transfer
->tx_dma
;
692 drv_data
->len_in_bytes
= transfer
->len
;
695 if (width
== CFG_SPI_WORDSIZE16
) {
696 drv_data
->len
= (transfer
->len
) >> 1;
698 drv_data
->len
= transfer
->len
;
700 drv_data
->write
= drv_data
->tx
? chip
->write
: null_writer
;
701 drv_data
->read
= drv_data
->rx
? chip
->read
: null_reader
;
702 drv_data
->duplex
= chip
->duplex
? chip
->duplex
: null_writer
;
703 dev_dbg(&drv_data
->pdev
->dev
,
704 "transfer: drv_data->write is %p, chip->write is %p, null_wr is %p\n",
705 drv_data
->write
, chip
->write
, null_writer
);
707 /* speed and width has been set on per message */
708 message
->state
= RUNNING_STATE
;
711 /* restore spi status for each spi transfer */
712 if (transfer
->speed_hz
) {
713 write_BAUD(hz_to_spi_baud(transfer
->speed_hz
));
715 write_BAUD(chip
->baud
);
717 write_FLAG(chip
->flag
);
719 dev_dbg(&drv_data
->pdev
->dev
,
720 "now pumping a transfer: width is %d, len is %d\n",
721 width
, transfer
->len
);
724 * Try to map dma buffer and do a dma transfer if
725 * successful use different way to r/w according to
726 * drv_data->cur_chip->enable_dma
728 if (drv_data
->cur_chip
->enable_dma
&& drv_data
->len
> 6) {
730 write_STAT(BIT_STAT_CLR
);
732 clear_dma_irqstat(CH_SPI
);
733 bfin_spi_disable(drv_data
);
735 /* config dma channel */
736 dev_dbg(&drv_data
->pdev
->dev
, "doing dma transfer\n");
737 if (width
== CFG_SPI_WORDSIZE16
) {
738 set_dma_x_count(CH_SPI
, drv_data
->len
);
739 set_dma_x_modify(CH_SPI
, 2);
740 dma_width
= WDSIZE_16
;
742 set_dma_x_count(CH_SPI
, drv_data
->len
);
743 set_dma_x_modify(CH_SPI
, 1);
744 dma_width
= WDSIZE_8
;
747 /* set transfer width,direction. And enable spi */
748 cr
= (read_CTRL() & (~BIT_CTL_TIMOD
));
750 /* dirty hack for autobuffer DMA mode */
751 if (drv_data
->tx_dma
== 0xFFFF) {
752 dev_dbg(&drv_data
->pdev
->dev
,
753 "doing autobuffer DMA out.\n");
755 /* no irq in autobuffer mode */
757 (DMAFLOW_AUTO
| RESTART
| dma_width
| DI_EN
);
758 set_dma_config(CH_SPI
, dma_config
);
759 set_dma_start_addr(CH_SPI
, (unsigned long)drv_data
->tx
);
761 write_CTRL(cr
| CFG_SPI_DMAWRITE
| (width
<< 8) |
762 (CFG_SPI_ENABLE
<< 14));
764 /* just return here, there can only be one transfer in this mode */
770 /* In dma mode, rx or tx must be NULL in one transfer */
771 if (drv_data
->rx
!= NULL
) {
772 /* set transfer mode, and enable SPI */
773 dev_dbg(&drv_data
->pdev
->dev
, "doing DMA in.\n");
775 /* disable SPI before write to TDBR */
776 write_CTRL(cr
& ~BIT_CTL_ENABLE
);
778 /* clear tx reg soformer data is not shifted out */
781 set_dma_x_count(CH_SPI
, drv_data
->len
);
784 dma_enable_irq(CH_SPI
);
785 dma_config
= (WNR
| RESTART
| dma_width
| DI_EN
);
786 set_dma_config(CH_SPI
, dma_config
);
787 set_dma_start_addr(CH_SPI
, (unsigned long)drv_data
->rx
);
791 CFG_SPI_DMAREAD
| (width
<< 8) | (CFG_SPI_ENABLE
<<
793 /* set transfer mode, and enable SPI */
795 } else if (drv_data
->tx
!= NULL
) {
796 dev_dbg(&drv_data
->pdev
->dev
, "doing DMA out.\n");
799 dma_enable_irq(CH_SPI
);
800 dma_config
= (RESTART
| dma_width
| DI_EN
);
801 set_dma_config(CH_SPI
, dma_config
);
802 set_dma_start_addr(CH_SPI
, (unsigned long)drv_data
->tx
);
805 write_CTRL(cr
| CFG_SPI_DMAWRITE
| (width
<< 8) |
806 (CFG_SPI_ENABLE
<< 14));
810 /* IO mode write then read */
811 dev_dbg(&drv_data
->pdev
->dev
, "doing IO transfer\n");
813 write_STAT(BIT_STAT_CLR
);
815 if (drv_data
->tx
!= NULL
&& drv_data
->rx
!= NULL
) {
816 /* full duplex mode */
817 BUG_ON((drv_data
->tx_end
- drv_data
->tx
) !=
818 (drv_data
->rx_end
- drv_data
->rx
));
819 cr
= (read_CTRL() & (~BIT_CTL_TIMOD
));
820 cr
|= CFG_SPI_WRITE
| (width
<< 8) |
821 (CFG_SPI_ENABLE
<< 14);
822 dev_dbg(&drv_data
->pdev
->dev
,
823 "IO duplex: cr is 0x%x\n", cr
);
828 drv_data
->duplex(drv_data
);
830 if (drv_data
->tx
!= drv_data
->tx_end
)
832 } else if (drv_data
->tx
!= NULL
) {
833 /* write only half duplex */
834 cr
= (read_CTRL() & (~BIT_CTL_TIMOD
));
835 cr
|= CFG_SPI_WRITE
| (width
<< 8) |
836 (CFG_SPI_ENABLE
<< 14);
837 dev_dbg(&drv_data
->pdev
->dev
,
838 "IO write: cr is 0x%x\n", cr
);
843 drv_data
->write(drv_data
);
845 if (drv_data
->tx
!= drv_data
->tx_end
)
847 } else if (drv_data
->rx
!= NULL
) {
848 /* read only half duplex */
849 cr
= (read_CTRL() & (~BIT_CTL_TIMOD
));
850 cr
|= CFG_SPI_READ
| (width
<< 8) |
851 (CFG_SPI_ENABLE
<< 14);
852 dev_dbg(&drv_data
->pdev
->dev
,
853 "IO read: cr is 0x%x\n", cr
);
858 drv_data
->read(drv_data
);
859 if (drv_data
->rx
!= drv_data
->rx_end
)
863 if (!tranf_success
) {
864 dev_dbg(&drv_data
->pdev
->dev
,
865 "IO write error!\n");
866 message
->state
= ERROR_STATE
;
868 /* Update total byte transfered */
869 message
->actual_length
+= drv_data
->len
;
871 /* Move to next transfer of this msg */
872 message
->state
= next_transfer(drv_data
);
875 /* Schedule next transfer tasklet */
876 tasklet_schedule(&drv_data
->pump_transfers
);
881 /* pop a msg from queue and kick off real transfer */
882 static void pump_messages(struct work_struct
*work
)
884 struct driver_data
*drv_data
= container_of(work
, struct driver_data
, pump_messages
);
887 /* Lock queue and check for queue work */
888 spin_lock_irqsave(&drv_data
->lock
, flags
);
889 if (list_empty(&drv_data
->queue
) || drv_data
->run
== QUEUE_STOPPED
) {
890 /* pumper kicked off but no work to do */
892 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
896 /* Make sure we are not already running a message */
897 if (drv_data
->cur_msg
) {
898 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
902 /* Extract head of queue */
903 drv_data
->cur_msg
= list_entry(drv_data
->queue
.next
,
904 struct spi_message
, queue
);
905 list_del_init(&drv_data
->cur_msg
->queue
);
907 /* Initial message state */
908 drv_data
->cur_msg
->state
= START_STATE
;
909 drv_data
->cur_transfer
= list_entry(drv_data
->cur_msg
->transfers
.next
,
910 struct spi_transfer
, transfer_list
);
912 /* Setup the SSP using the per chip configuration */
913 drv_data
->cur_chip
= spi_get_ctldata(drv_data
->cur_msg
->spi
);
914 restore_state(drv_data
);
915 dev_dbg(&drv_data
->pdev
->dev
,
916 "got a message to pump, state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
917 drv_data
->cur_chip
->baud
, drv_data
->cur_chip
->flag
,
918 drv_data
->cur_chip
->ctl_reg
);
920 dev_dbg(&drv_data
->pdev
->dev
,
921 "the first transfer len is %d\n",
922 drv_data
->cur_transfer
->len
);
924 /* Mark as busy and launch transfers */
925 tasklet_schedule(&drv_data
->pump_transfers
);
928 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
932 * got a msg to transfer, queue it in drv_data->queue.
933 * And kick off message pumper
935 static int transfer(struct spi_device
*spi
, struct spi_message
*msg
)
937 struct driver_data
*drv_data
= spi_master_get_devdata(spi
->master
);
940 spin_lock_irqsave(&drv_data
->lock
, flags
);
942 if (drv_data
->run
== QUEUE_STOPPED
) {
943 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
947 msg
->actual_length
= 0;
948 msg
->status
= -EINPROGRESS
;
949 msg
->state
= START_STATE
;
951 dev_dbg(&spi
->dev
, "adding an msg in transfer() \n");
952 list_add_tail(&msg
->queue
, &drv_data
->queue
);
954 if (drv_data
->run
== QUEUE_RUNNING
&& !drv_data
->busy
)
955 queue_work(drv_data
->workqueue
, &drv_data
->pump_messages
);
957 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
962 /* first setup for new devices */
963 static int setup(struct spi_device
*spi
)
965 struct bfin5xx_spi_chip
*chip_info
= NULL
;
966 struct chip_data
*chip
;
967 struct driver_data
*drv_data
= spi_master_get_devdata(spi
->master
);
970 /* Abort device setup if requested features are not supported */
971 if (spi
->mode
& ~(SPI_CPOL
| SPI_CPHA
| SPI_LSB_FIRST
)) {
972 dev_err(&spi
->dev
, "requested mode not fully supported\n");
976 /* Zero (the default) here means 8 bits */
977 if (!spi
->bits_per_word
)
978 spi
->bits_per_word
= 8;
980 if (spi
->bits_per_word
!= 8 && spi
->bits_per_word
!= 16)
983 /* Only alloc (or use chip_info) on first setup */
984 chip
= spi_get_ctldata(spi
);
986 chip
= kzalloc(sizeof(struct chip_data
), GFP_KERNEL
);
990 chip
->enable_dma
= 0;
991 chip_info
= spi
->controller_data
;
994 /* chip_info isn't always needed */
996 chip
->enable_dma
= chip_info
->enable_dma
!= 0
997 && drv_data
->master_info
->enable_dma
;
998 chip
->ctl_reg
= chip_info
->ctl_reg
;
999 chip
->bits_per_word
= chip_info
->bits_per_word
;
1000 chip
->cs_change_per_word
= chip_info
->cs_change_per_word
;
1001 chip
->cs_chg_udelay
= chip_info
->cs_chg_udelay
;
1004 /* translate common spi framework into our register */
1005 if (spi
->mode
& SPI_CPOL
)
1006 chip
->ctl_reg
|= CPOL
;
1007 if (spi
->mode
& SPI_CPHA
)
1008 chip
->ctl_reg
|= CPHA
;
1009 if (spi
->mode
& SPI_LSB_FIRST
)
1010 chip
->ctl_reg
|= LSBF
;
1011 /* we dont support running in slave mode (yet?) */
1012 chip
->ctl_reg
|= MSTR
;
1015 * if any one SPI chip is registered and wants DMA, request the
1016 * DMA channel for it
1018 if (chip
->enable_dma
&& !dma_requested
) {
1019 /* register dma irq handler */
1020 if (request_dma(CH_SPI
, "BF53x_SPI_DMA") < 0) {
1022 "Unable to request BlackFin SPI DMA channel\n");
1025 if (set_dma_callback(CH_SPI
, (void *)dma_irq_handler
, drv_data
)
1027 dev_dbg(&spi
->dev
, "Unable to set dma callback\n");
1030 dma_disable_irq(CH_SPI
);
1035 * Notice: for blackfin, the speed_hz is the value of register
1036 * SPI_BAUD, not the real baudrate
1038 chip
->baud
= hz_to_spi_baud(spi
->max_speed_hz
);
1039 spi_flg
= ~(1 << (spi
->chip_select
));
1040 chip
->flag
= ((u16
) spi_flg
<< 8) | (1 << (spi
->chip_select
));
1041 chip
->chip_select_num
= spi
->chip_select
;
1043 switch (chip
->bits_per_word
) {
1046 chip
->width
= CFG_SPI_WORDSIZE8
;
1047 chip
->read
= chip
->cs_change_per_word
?
1048 u8_cs_chg_reader
: u8_reader
;
1049 chip
->write
= chip
->cs_change_per_word
?
1050 u8_cs_chg_writer
: u8_writer
;
1051 chip
->duplex
= chip
->cs_change_per_word
?
1052 u8_cs_chg_duplex
: u8_duplex
;
1057 chip
->width
= CFG_SPI_WORDSIZE16
;
1058 chip
->read
= chip
->cs_change_per_word
?
1059 u16_cs_chg_reader
: u16_reader
;
1060 chip
->write
= chip
->cs_change_per_word
?
1061 u16_cs_chg_writer
: u16_writer
;
1062 chip
->duplex
= chip
->cs_change_per_word
?
1063 u16_cs_chg_duplex
: u16_duplex
;
1067 dev_err(&spi
->dev
, "%d bits_per_word is not supported\n",
1068 chip
->bits_per_word
);
1073 dev_dbg(&spi
->dev
, "setup spi chip %s, width is %d, dma is %d\n",
1074 spi
->modalias
, chip
->width
, chip
->enable_dma
);
1075 dev_dbg(&spi
->dev
, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
1076 chip
->ctl_reg
, chip
->flag
);
1078 spi_set_ctldata(spi
, chip
);
1084 * callback for spi framework.
1085 * clean driver specific data
1087 static void cleanup(struct spi_device
*spi
)
1089 struct chip_data
*chip
= spi_get_ctldata(spi
);
1094 static inline int init_queue(struct driver_data
*drv_data
)
1096 INIT_LIST_HEAD(&drv_data
->queue
);
1097 spin_lock_init(&drv_data
->lock
);
1099 drv_data
->run
= QUEUE_STOPPED
;
1102 /* init transfer tasklet */
1103 tasklet_init(&drv_data
->pump_transfers
,
1104 pump_transfers
, (unsigned long)drv_data
);
1106 /* init messages workqueue */
1107 INIT_WORK(&drv_data
->pump_messages
, pump_messages
);
1108 drv_data
->workqueue
=
1109 create_singlethread_workqueue(drv_data
->master
->dev
.parent
->bus_id
);
1110 if (drv_data
->workqueue
== NULL
)
1116 static inline int start_queue(struct driver_data
*drv_data
)
1118 unsigned long flags
;
1120 spin_lock_irqsave(&drv_data
->lock
, flags
);
1122 if (drv_data
->run
== QUEUE_RUNNING
|| drv_data
->busy
) {
1123 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1127 drv_data
->run
= QUEUE_RUNNING
;
1128 drv_data
->cur_msg
= NULL
;
1129 drv_data
->cur_transfer
= NULL
;
1130 drv_data
->cur_chip
= NULL
;
1131 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1133 queue_work(drv_data
->workqueue
, &drv_data
->pump_messages
);
1138 static inline int stop_queue(struct driver_data
*drv_data
)
1140 unsigned long flags
;
1141 unsigned limit
= 500;
1144 spin_lock_irqsave(&drv_data
->lock
, flags
);
1147 * This is a bit lame, but is optimized for the common execution path.
1148 * A wait_queue on the drv_data->busy could be used, but then the common
1149 * execution path (pump_messages) would be required to call wake_up or
1150 * friends on every SPI message. Do this instead
1152 drv_data
->run
= QUEUE_STOPPED
;
1153 while (!list_empty(&drv_data
->queue
) && drv_data
->busy
&& limit
--) {
1154 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1156 spin_lock_irqsave(&drv_data
->lock
, flags
);
1159 if (!list_empty(&drv_data
->queue
) || drv_data
->busy
)
1162 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1167 static inline int destroy_queue(struct driver_data
*drv_data
)
1171 status
= stop_queue(drv_data
);
1175 destroy_workqueue(drv_data
->workqueue
);
1180 static int __init
bfin5xx_spi_probe(struct platform_device
*pdev
)
1182 struct device
*dev
= &pdev
->dev
;
1183 struct bfin5xx_spi_master
*platform_info
;
1184 struct spi_master
*master
;
1185 struct driver_data
*drv_data
= 0;
1188 platform_info
= dev
->platform_data
;
1190 /* Allocate master with space for drv_data */
1191 master
= spi_alloc_master(dev
, sizeof(struct driver_data
) + 16);
1193 dev_err(&pdev
->dev
, "can not alloc spi_master\n");
1196 drv_data
= spi_master_get_devdata(master
);
1197 drv_data
->master
= master
;
1198 drv_data
->master_info
= platform_info
;
1199 drv_data
->pdev
= pdev
;
1201 master
->bus_num
= pdev
->id
;
1202 master
->num_chipselect
= platform_info
->num_chipselect
;
1203 master
->cleanup
= cleanup
;
1204 master
->setup
= setup
;
1205 master
->transfer
= transfer
;
1207 /* Initial and start queue */
1208 status
= init_queue(drv_data
);
1210 dev_err(&pdev
->dev
, "problem initializing queue\n");
1211 goto out_error_queue_alloc
;
1213 status
= start_queue(drv_data
);
1215 dev_err(&pdev
->dev
, "problem starting queue\n");
1216 goto out_error_queue_alloc
;
1219 /* Register with the SPI framework */
1220 platform_set_drvdata(pdev
, drv_data
);
1221 status
= spi_register_master(master
);
1223 dev_err(&pdev
->dev
, "problem registering spi master\n");
1224 goto out_error_queue_alloc
;
1226 dev_dbg(&pdev
->dev
, "controller probe successfully\n");
1229 out_error_queue_alloc
:
1230 destroy_queue(drv_data
);
1231 spi_master_put(master
);
1235 /* stop hardware and remove the driver */
1236 static int __devexit
bfin5xx_spi_remove(struct platform_device
*pdev
)
1238 struct driver_data
*drv_data
= platform_get_drvdata(pdev
);
1244 /* Remove the queue */
1245 status
= destroy_queue(drv_data
);
1249 /* Disable the SSP at the peripheral and SOC level */
1250 bfin_spi_disable(drv_data
);
1253 if (drv_data
->master_info
->enable_dma
) {
1254 if (dma_channel_active(CH_SPI
))
1258 /* Disconnect from the SPI framework */
1259 spi_unregister_master(drv_data
->master
);
1261 /* Prevent double remove */
1262 platform_set_drvdata(pdev
, NULL
);
1268 static int bfin5xx_spi_suspend(struct platform_device
*pdev
, pm_message_t state
)
1270 struct driver_data
*drv_data
= platform_get_drvdata(pdev
);
1273 status
= stop_queue(drv_data
);
1278 bfin_spi_disable(drv_data
);
1283 static int bfin5xx_spi_resume(struct platform_device
*pdev
)
1285 struct driver_data
*drv_data
= platform_get_drvdata(pdev
);
1288 /* Enable the SPI interface */
1289 bfin_spi_enable(drv_data
);
1291 /* Start the queue running */
1292 status
= start_queue(drv_data
);
1294 dev_err(&pdev
->dev
, "problem starting queue (%d)\n", status
);
1301 #define bfin5xx_spi_suspend NULL
1302 #define bfin5xx_spi_resume NULL
1303 #endif /* CONFIG_PM */
1305 MODULE_ALIAS("bfin-spi-master"); /* for platform bus hotplug */
1306 static struct platform_driver bfin5xx_spi_driver
= {
1308 .name
= "bfin-spi-master",
1309 .owner
= THIS_MODULE
,
1311 .suspend
= bfin5xx_spi_suspend
,
1312 .resume
= bfin5xx_spi_resume
,
1313 .remove
= __devexit_p(bfin5xx_spi_remove
),
1316 static int __init
bfin5xx_spi_init(void)
1318 return platform_driver_probe(&bfin5xx_spi_driver
, bfin5xx_spi_probe
);
1320 module_init(bfin5xx_spi_init
);
1322 static void __exit
bfin5xx_spi_exit(void)
1324 platform_driver_unregister(&bfin5xx_spi_driver
);
1326 module_exit(bfin5xx_spi_exit
);