2 * Blackfin On-Chip SPI Driver
4 * Copyright 2004-2007 Analog Devices Inc.
6 * Enter bugs at http://blackfin.uclinux.org/
8 * Licensed under the GPL-2 or later.
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/delay.h>
14 #include <linux/device.h>
16 #include <linux/ioport.h>
17 #include <linux/irq.h>
18 #include <linux/errno.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/spi/spi.h>
23 #include <linux/workqueue.h>
26 #include <asm/portmux.h>
27 #include <asm/bfin5xx_spi.h>
28 #include <asm/cacheflush.h>
30 #define DRV_NAME "bfin-spi"
31 #define DRV_AUTHOR "Bryan Wu, Luke Yang"
32 #define DRV_DESC "Blackfin BF5xx on-chip SPI Controller Driver"
33 #define DRV_VERSION "1.0"
35 MODULE_AUTHOR(DRV_AUTHOR
);
36 MODULE_DESCRIPTION(DRV_DESC
);
37 MODULE_LICENSE("GPL");
39 #define IS_DMA_ALIGNED(x) (((u32)(x)&0x07) == 0)
41 #define START_STATE ((void *)0)
42 #define RUNNING_STATE ((void *)1)
43 #define DONE_STATE ((void *)2)
44 #define ERROR_STATE ((void *)-1)
45 #define QUEUE_RUNNING 0
46 #define QUEUE_STOPPED 1
49 /* Driver model hookup */
50 struct platform_device
*pdev
;
52 /* SPI framework hookup */
53 struct spi_master
*master
;
55 /* Regs base of SPI controller */
56 void __iomem
*regs_base
;
58 /* Pin request list */
62 struct bfin5xx_spi_master
*master_info
;
64 /* Driver message queue */
65 struct workqueue_struct
*workqueue
;
66 struct work_struct pump_messages
;
68 struct list_head queue
;
72 /* Message Transfer pump */
73 struct tasklet_struct pump_transfers
;
75 /* Current message transfer state info */
76 struct spi_message
*cur_msg
;
77 struct spi_transfer
*cur_transfer
;
78 struct chip_data
*cur_chip
;
97 void (*write
) (struct driver_data
*);
98 void (*read
) (struct driver_data
*);
99 void (*duplex
) (struct driver_data
*);
109 u8 width
; /* 0 or 1 */
111 u8 bits_per_word
; /* 8 or 16 */
112 u8 cs_change_per_word
;
113 u16 cs_chg_udelay
; /* Some devices require > 255usec delay */
114 void (*write
) (struct driver_data
*);
115 void (*read
) (struct driver_data
*);
116 void (*duplex
) (struct driver_data
*);
119 #define DEFINE_SPI_REG(reg, off) \
120 static inline u16 read_##reg(struct driver_data *drv_data) \
121 { return bfin_read16(drv_data->regs_base + off); } \
122 static inline void write_##reg(struct driver_data *drv_data, u16 v) \
123 { bfin_write16(drv_data->regs_base + off, v); }
125 DEFINE_SPI_REG(CTRL
, 0x00)
126 DEFINE_SPI_REG(FLAG
, 0x04)
127 DEFINE_SPI_REG(STAT
, 0x08)
128 DEFINE_SPI_REG(TDBR
, 0x0C)
129 DEFINE_SPI_REG(RDBR
, 0x10)
130 DEFINE_SPI_REG(BAUD
, 0x14)
131 DEFINE_SPI_REG(SHAW
, 0x18)
133 static void bfin_spi_enable(struct driver_data
*drv_data
)
137 cr
= read_CTRL(drv_data
);
138 write_CTRL(drv_data
, (cr
| BIT_CTL_ENABLE
));
141 static void bfin_spi_disable(struct driver_data
*drv_data
)
145 cr
= read_CTRL(drv_data
);
146 write_CTRL(drv_data
, (cr
& (~BIT_CTL_ENABLE
)));
149 /* Caculate the SPI_BAUD register value based on input HZ */
150 static u16
hz_to_spi_baud(u32 speed_hz
)
152 u_long sclk
= get_sclk();
153 u16 spi_baud
= (sclk
/ (2 * speed_hz
));
155 if ((sclk
% (2 * speed_hz
)) > 0)
158 if (spi_baud
< MIN_SPI_BAUD_VAL
)
159 spi_baud
= MIN_SPI_BAUD_VAL
;
164 static int flush(struct driver_data
*drv_data
)
166 unsigned long limit
= loops_per_jiffy
<< 1;
168 /* wait for stop and clear stat */
169 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
) && limit
--)
172 write_STAT(drv_data
, BIT_STAT_CLR
);
177 /* Chip select operation functions for cs_change flag */
178 static void cs_active(struct driver_data
*drv_data
, struct chip_data
*chip
)
180 u16 flag
= read_FLAG(drv_data
);
183 flag
&= ~(chip
->flag
<< 8);
185 write_FLAG(drv_data
, flag
);
188 static void cs_deactive(struct driver_data
*drv_data
, struct chip_data
*chip
)
190 u16 flag
= read_FLAG(drv_data
);
192 flag
|= (chip
->flag
<< 8);
194 write_FLAG(drv_data
, flag
);
196 /* Move delay here for consistency */
197 if (chip
->cs_chg_udelay
)
198 udelay(chip
->cs_chg_udelay
);
201 #define MAX_SPI_SSEL 7
203 /* stop controller and re-config current chip*/
204 static void restore_state(struct driver_data
*drv_data
)
206 struct chip_data
*chip
= drv_data
->cur_chip
;
208 /* Clear status and disable clock */
209 write_STAT(drv_data
, BIT_STAT_CLR
);
210 bfin_spi_disable(drv_data
);
211 dev_dbg(&drv_data
->pdev
->dev
, "restoring spi ctl state\n");
213 /* Load the registers */
214 write_CTRL(drv_data
, chip
->ctl_reg
);
215 write_BAUD(drv_data
, chip
->baud
);
217 bfin_spi_enable(drv_data
);
218 cs_active(drv_data
, chip
);
221 /* used to kick off transfer in rx mode */
222 static unsigned short dummy_read(struct driver_data
*drv_data
)
225 tmp
= read_RDBR(drv_data
);
229 static void null_writer(struct driver_data
*drv_data
)
231 u8 n_bytes
= drv_data
->n_bytes
;
233 while (drv_data
->tx
< drv_data
->tx_end
) {
234 write_TDBR(drv_data
, 0);
235 while ((read_STAT(drv_data
) & BIT_STAT_TXS
))
237 drv_data
->tx
+= n_bytes
;
241 static void null_reader(struct driver_data
*drv_data
)
243 u8 n_bytes
= drv_data
->n_bytes
;
244 dummy_read(drv_data
);
246 while (drv_data
->rx
< drv_data
->rx_end
) {
247 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
249 dummy_read(drv_data
);
250 drv_data
->rx
+= n_bytes
;
254 static void u8_writer(struct driver_data
*drv_data
)
256 dev_dbg(&drv_data
->pdev
->dev
,
257 "cr8-s is 0x%x\n", read_STAT(drv_data
));
259 while (drv_data
->tx
< drv_data
->tx_end
) {
260 write_TDBR(drv_data
, (*(u8
*) (drv_data
->tx
)));
261 while (read_STAT(drv_data
) & BIT_STAT_TXS
)
266 /* poll for SPI completion before return */
267 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
271 static void u8_cs_chg_writer(struct driver_data
*drv_data
)
273 struct chip_data
*chip
= drv_data
->cur_chip
;
275 while (drv_data
->tx
< drv_data
->tx_end
) {
276 cs_active(drv_data
, chip
);
278 write_TDBR(drv_data
, (*(u8
*) (drv_data
->tx
)));
279 while (read_STAT(drv_data
) & BIT_STAT_TXS
)
281 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
284 cs_deactive(drv_data
, chip
);
290 static void u8_reader(struct driver_data
*drv_data
)
292 dev_dbg(&drv_data
->pdev
->dev
,
293 "cr-8 is 0x%x\n", read_STAT(drv_data
));
295 /* poll for SPI completion before start */
296 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
299 /* clear TDBR buffer before read(else it will be shifted out) */
300 write_TDBR(drv_data
, 0xFFFF);
302 dummy_read(drv_data
);
304 while (drv_data
->rx
< drv_data
->rx_end
- 1) {
305 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
307 *(u8
*) (drv_data
->rx
) = read_RDBR(drv_data
);
311 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
313 *(u8
*) (drv_data
->rx
) = read_SHAW(drv_data
);
317 static void u8_cs_chg_reader(struct driver_data
*drv_data
)
319 struct chip_data
*chip
= drv_data
->cur_chip
;
321 while (drv_data
->rx
< drv_data
->rx_end
) {
322 cs_active(drv_data
, chip
);
323 read_RDBR(drv_data
); /* kick off */
325 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
327 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
330 *(u8
*) (drv_data
->rx
) = read_SHAW(drv_data
);
331 cs_deactive(drv_data
, chip
);
337 static void u8_duplex(struct driver_data
*drv_data
)
339 /* in duplex mode, clk is triggered by writing of TDBR */
340 while (drv_data
->rx
< drv_data
->rx_end
) {
341 write_TDBR(drv_data
, (*(u8
*) (drv_data
->tx
)));
342 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
344 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
346 *(u8
*) (drv_data
->rx
) = read_RDBR(drv_data
);
352 static void u8_cs_chg_duplex(struct driver_data
*drv_data
)
354 struct chip_data
*chip
= drv_data
->cur_chip
;
356 while (drv_data
->rx
< drv_data
->rx_end
) {
357 cs_active(drv_data
, chip
);
359 write_TDBR(drv_data
, (*(u8
*) (drv_data
->tx
)));
361 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
363 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
365 *(u8
*) (drv_data
->rx
) = read_RDBR(drv_data
);
367 cs_deactive(drv_data
, chip
);
374 static void u16_writer(struct driver_data
*drv_data
)
376 dev_dbg(&drv_data
->pdev
->dev
,
377 "cr16 is 0x%x\n", read_STAT(drv_data
));
379 while (drv_data
->tx
< drv_data
->tx_end
) {
380 write_TDBR(drv_data
, (*(u16
*) (drv_data
->tx
)));
381 while ((read_STAT(drv_data
) & BIT_STAT_TXS
))
386 /* poll for SPI completion before return */
387 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
391 static void u16_cs_chg_writer(struct driver_data
*drv_data
)
393 struct chip_data
*chip
= drv_data
->cur_chip
;
395 while (drv_data
->tx
< drv_data
->tx_end
) {
396 cs_active(drv_data
, chip
);
398 write_TDBR(drv_data
, (*(u16
*) (drv_data
->tx
)));
399 while ((read_STAT(drv_data
) & BIT_STAT_TXS
))
401 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
404 cs_deactive(drv_data
, chip
);
410 static void u16_reader(struct driver_data
*drv_data
)
412 dev_dbg(&drv_data
->pdev
->dev
,
413 "cr-16 is 0x%x\n", read_STAT(drv_data
));
415 /* poll for SPI completion before start */
416 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
419 /* clear TDBR buffer before read(else it will be shifted out) */
420 write_TDBR(drv_data
, 0xFFFF);
422 dummy_read(drv_data
);
424 while (drv_data
->rx
< (drv_data
->rx_end
- 2)) {
425 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
427 *(u16
*) (drv_data
->rx
) = read_RDBR(drv_data
);
431 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
433 *(u16
*) (drv_data
->rx
) = read_SHAW(drv_data
);
437 static void u16_cs_chg_reader(struct driver_data
*drv_data
)
439 struct chip_data
*chip
= drv_data
->cur_chip
;
441 /* poll for SPI completion before start */
442 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
445 /* clear TDBR buffer before read(else it will be shifted out) */
446 write_TDBR(drv_data
, 0xFFFF);
448 cs_active(drv_data
, chip
);
449 dummy_read(drv_data
);
451 while (drv_data
->rx
< drv_data
->rx_end
- 2) {
452 cs_deactive(drv_data
, chip
);
454 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
456 cs_active(drv_data
, chip
);
457 *(u16
*) (drv_data
->rx
) = read_RDBR(drv_data
);
460 cs_deactive(drv_data
, chip
);
462 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
464 *(u16
*) (drv_data
->rx
) = read_SHAW(drv_data
);
468 static void u16_duplex(struct driver_data
*drv_data
)
470 /* in duplex mode, clk is triggered by writing of TDBR */
471 while (drv_data
->tx
< drv_data
->tx_end
) {
472 write_TDBR(drv_data
, (*(u16
*) (drv_data
->tx
)));
473 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
475 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
477 *(u16
*) (drv_data
->rx
) = read_RDBR(drv_data
);
483 static void u16_cs_chg_duplex(struct driver_data
*drv_data
)
485 struct chip_data
*chip
= drv_data
->cur_chip
;
487 while (drv_data
->tx
< drv_data
->tx_end
) {
488 cs_active(drv_data
, chip
);
490 write_TDBR(drv_data
, (*(u16
*) (drv_data
->tx
)));
491 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
493 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
495 *(u16
*) (drv_data
->rx
) = read_RDBR(drv_data
);
497 cs_deactive(drv_data
, chip
);
504 /* test if ther is more transfer to be done */
505 static void *next_transfer(struct driver_data
*drv_data
)
507 struct spi_message
*msg
= drv_data
->cur_msg
;
508 struct spi_transfer
*trans
= drv_data
->cur_transfer
;
510 /* Move to next transfer */
511 if (trans
->transfer_list
.next
!= &msg
->transfers
) {
512 drv_data
->cur_transfer
=
513 list_entry(trans
->transfer_list
.next
,
514 struct spi_transfer
, transfer_list
);
515 return RUNNING_STATE
;
521 * caller already set message->status;
522 * dma and pio irqs are blocked give finished message back
524 static void giveback(struct driver_data
*drv_data
)
526 struct chip_data
*chip
= drv_data
->cur_chip
;
527 struct spi_transfer
*last_transfer
;
529 struct spi_message
*msg
;
531 spin_lock_irqsave(&drv_data
->lock
, flags
);
532 msg
= drv_data
->cur_msg
;
533 drv_data
->cur_msg
= NULL
;
534 drv_data
->cur_transfer
= NULL
;
535 drv_data
->cur_chip
= NULL
;
536 queue_work(drv_data
->workqueue
, &drv_data
->pump_messages
);
537 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
539 last_transfer
= list_entry(msg
->transfers
.prev
,
540 struct spi_transfer
, transfer_list
);
544 /* disable chip select signal. And not stop spi in autobuffer mode */
545 if (drv_data
->tx_dma
!= 0xFFFF) {
546 cs_deactive(drv_data
, chip
);
547 bfin_spi_disable(drv_data
);
550 if (!drv_data
->cs_change
)
551 cs_deactive(drv_data
, chip
);
554 msg
->complete(msg
->context
);
557 static irqreturn_t
dma_irq_handler(int irq
, void *dev_id
)
559 struct driver_data
*drv_data
= dev_id
;
560 struct chip_data
*chip
= drv_data
->cur_chip
;
561 struct spi_message
*msg
= drv_data
->cur_msg
;
562 u16 spistat
= read_STAT(drv_data
);
564 dev_dbg(&drv_data
->pdev
->dev
, "in dma_irq_handler\n");
565 clear_dma_irqstat(drv_data
->dma_channel
);
567 /* Wait for DMA to complete */
568 while (get_dma_curr_irqstat(drv_data
->dma_channel
) & DMA_RUN
)
572 * wait for the last transaction shifted out. HRM states:
573 * at this point there may still be data in the SPI DMA FIFO waiting
574 * to be transmitted ... software needs to poll TXS in the SPI_STAT
575 * register until it goes low for 2 successive reads
577 if (drv_data
->tx
!= NULL
) {
578 while ((read_STAT(drv_data
) & TXS
) ||
579 (read_STAT(drv_data
) & TXS
))
583 while (!(read_STAT(drv_data
) & SPIF
))
586 if (spistat
& RBSY
) {
587 msg
->state
= ERROR_STATE
;
588 dev_err(&drv_data
->pdev
->dev
, "dma receive: fifo/buffer overflow\n");
590 msg
->actual_length
+= drv_data
->len_in_bytes
;
592 if (drv_data
->cs_change
)
593 cs_deactive(drv_data
, chip
);
595 /* Move to next transfer */
596 msg
->state
= next_transfer(drv_data
);
599 /* Schedule transfer tasklet */
600 tasklet_schedule(&drv_data
->pump_transfers
);
602 /* free the irq handler before next transfer */
603 dev_dbg(&drv_data
->pdev
->dev
,
604 "disable dma channel irq%d\n",
605 drv_data
->dma_channel
);
606 dma_disable_irq(drv_data
->dma_channel
);
611 static void pump_transfers(unsigned long data
)
613 struct driver_data
*drv_data
= (struct driver_data
*)data
;
614 struct spi_message
*message
= NULL
;
615 struct spi_transfer
*transfer
= NULL
;
616 struct spi_transfer
*previous
= NULL
;
617 struct chip_data
*chip
= NULL
;
619 u16 cr
, dma_width
, dma_config
;
620 u32 tranf_success
= 1;
623 /* Get current state information */
624 message
= drv_data
->cur_msg
;
625 transfer
= drv_data
->cur_transfer
;
626 chip
= drv_data
->cur_chip
;
629 * if msg is error or done, report it back using complete() callback
632 /* Handle for abort */
633 if (message
->state
== ERROR_STATE
) {
634 message
->status
= -EIO
;
639 /* Handle end of message */
640 if (message
->state
== DONE_STATE
) {
646 /* Delay if requested at end of transfer */
647 if (message
->state
== RUNNING_STATE
) {
648 previous
= list_entry(transfer
->transfer_list
.prev
,
649 struct spi_transfer
, transfer_list
);
650 if (previous
->delay_usecs
)
651 udelay(previous
->delay_usecs
);
654 /* Setup the transfer state based on the type of transfer */
655 if (flush(drv_data
) == 0) {
656 dev_err(&drv_data
->pdev
->dev
, "pump_transfers: flush failed\n");
657 message
->status
= -EIO
;
662 if (transfer
->tx_buf
!= NULL
) {
663 drv_data
->tx
= (void *)transfer
->tx_buf
;
664 drv_data
->tx_end
= drv_data
->tx
+ transfer
->len
;
665 dev_dbg(&drv_data
->pdev
->dev
, "tx_buf is %p, tx_end is %p\n",
666 transfer
->tx_buf
, drv_data
->tx_end
);
671 if (transfer
->rx_buf
!= NULL
) {
672 full_duplex
= transfer
->tx_buf
!= NULL
;
673 drv_data
->rx
= transfer
->rx_buf
;
674 drv_data
->rx_end
= drv_data
->rx
+ transfer
->len
;
675 dev_dbg(&drv_data
->pdev
->dev
, "rx_buf is %p, rx_end is %p\n",
676 transfer
->rx_buf
, drv_data
->rx_end
);
681 drv_data
->rx_dma
= transfer
->rx_dma
;
682 drv_data
->tx_dma
= transfer
->tx_dma
;
683 drv_data
->len_in_bytes
= transfer
->len
;
684 drv_data
->cs_change
= transfer
->cs_change
;
686 /* Bits per word setup */
687 switch (transfer
->bits_per_word
) {
689 drv_data
->n_bytes
= 1;
690 width
= CFG_SPI_WORDSIZE8
;
691 drv_data
->read
= chip
->cs_change_per_word
?
692 u8_cs_chg_reader
: u8_reader
;
693 drv_data
->write
= chip
->cs_change_per_word
?
694 u8_cs_chg_writer
: u8_writer
;
695 drv_data
->duplex
= chip
->cs_change_per_word
?
696 u8_cs_chg_duplex
: u8_duplex
;
700 drv_data
->n_bytes
= 2;
701 width
= CFG_SPI_WORDSIZE16
;
702 drv_data
->read
= chip
->cs_change_per_word
?
703 u16_cs_chg_reader
: u16_reader
;
704 drv_data
->write
= chip
->cs_change_per_word
?
705 u16_cs_chg_writer
: u16_writer
;
706 drv_data
->duplex
= chip
->cs_change_per_word
?
707 u16_cs_chg_duplex
: u16_duplex
;
711 /* No change, the same as default setting */
712 drv_data
->n_bytes
= chip
->n_bytes
;
714 drv_data
->write
= drv_data
->tx
? chip
->write
: null_writer
;
715 drv_data
->read
= drv_data
->rx
? chip
->read
: null_reader
;
716 drv_data
->duplex
= chip
->duplex
? chip
->duplex
: null_writer
;
719 cr
= (read_CTRL(drv_data
) & (~BIT_CTL_TIMOD
));
721 write_CTRL(drv_data
, cr
);
723 if (width
== CFG_SPI_WORDSIZE16
) {
724 drv_data
->len
= (transfer
->len
) >> 1;
726 drv_data
->len
= transfer
->len
;
728 dev_dbg(&drv_data
->pdev
->dev
,
729 "transfer: drv_data->write is %p, chip->write is %p, null_wr is %p\n",
730 drv_data
->write
, chip
->write
, null_writer
);
732 /* speed and width has been set on per message */
733 message
->state
= RUNNING_STATE
;
736 /* Speed setup (surely valid because already checked) */
737 if (transfer
->speed_hz
)
738 write_BAUD(drv_data
, hz_to_spi_baud(transfer
->speed_hz
));
740 write_BAUD(drv_data
, chip
->baud
);
742 write_STAT(drv_data
, BIT_STAT_CLR
);
743 cr
= (read_CTRL(drv_data
) & (~BIT_CTL_TIMOD
));
744 cs_active(drv_data
, chip
);
746 dev_dbg(&drv_data
->pdev
->dev
,
747 "now pumping a transfer: width is %d, len is %d\n",
748 width
, transfer
->len
);
751 * Try to map dma buffer and do a dma transfer. If successful use,
752 * different way to r/w according to the enable_dma settings and if
753 * we are not doing a full duplex transfer (since the hardware does
754 * not support full duplex DMA transfers).
756 if (!full_duplex
&& drv_data
->cur_chip
->enable_dma
757 && drv_data
->len
> 6) {
759 unsigned long dma_start_addr
;
761 disable_dma(drv_data
->dma_channel
);
762 clear_dma_irqstat(drv_data
->dma_channel
);
763 bfin_spi_disable(drv_data
);
765 /* config dma channel */
766 dev_dbg(&drv_data
->pdev
->dev
, "doing dma transfer\n");
767 set_dma_x_count(drv_data
->dma_channel
, drv_data
->len
);
768 if (width
== CFG_SPI_WORDSIZE16
) {
769 set_dma_x_modify(drv_data
->dma_channel
, 2);
770 dma_width
= WDSIZE_16
;
772 set_dma_x_modify(drv_data
->dma_channel
, 1);
773 dma_width
= WDSIZE_8
;
776 /* poll for SPI completion before start */
777 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
780 /* dirty hack for autobuffer DMA mode */
781 if (drv_data
->tx_dma
== 0xFFFF) {
782 dev_dbg(&drv_data
->pdev
->dev
,
783 "doing autobuffer DMA out.\n");
785 /* no irq in autobuffer mode */
787 (DMAFLOW_AUTO
| RESTART
| dma_width
| DI_EN
);
788 set_dma_config(drv_data
->dma_channel
, dma_config
);
789 set_dma_start_addr(drv_data
->dma_channel
,
790 (unsigned long)drv_data
->tx
);
791 enable_dma(drv_data
->dma_channel
);
793 /* start SPI transfer */
795 (cr
| CFG_SPI_DMAWRITE
| BIT_CTL_ENABLE
));
797 /* just return here, there can only be one transfer
805 /* In dma mode, rx or tx must be NULL in one transfer */
806 dma_config
= (RESTART
| dma_width
| DI_EN
);
807 if (drv_data
->rx
!= NULL
) {
808 /* set transfer mode, and enable SPI */
809 dev_dbg(&drv_data
->pdev
->dev
, "doing DMA in.\n");
811 /* invalidate caches, if needed */
812 if (bfin_addr_dcachable((unsigned long) drv_data
->rx
))
813 invalidate_dcache_range((unsigned long) drv_data
->rx
,
814 (unsigned long) (drv_data
->rx
+
815 drv_data
->len_in_bytes
));
817 /* clear tx reg soformer data is not shifted out */
818 write_TDBR(drv_data
, 0xFFFF);
821 dma_start_addr
= (unsigned long)drv_data
->rx
;
822 cr
|= CFG_SPI_DMAREAD
;
824 } else if (drv_data
->tx
!= NULL
) {
825 dev_dbg(&drv_data
->pdev
->dev
, "doing DMA out.\n");
827 /* flush caches, if needed */
828 if (bfin_addr_dcachable((unsigned long) drv_data
->tx
))
829 flush_dcache_range((unsigned long) drv_data
->tx
,
830 (unsigned long) (drv_data
->tx
+
831 drv_data
->len_in_bytes
));
833 dma_start_addr
= (unsigned long)drv_data
->tx
;
834 cr
|= CFG_SPI_DMAWRITE
;
840 dma_enable_irq(drv_data
->dma_channel
);
841 set_dma_config(drv_data
->dma_channel
, dma_config
);
842 set_dma_start_addr(drv_data
->dma_channel
, dma_start_addr
);
843 enable_dma(drv_data
->dma_channel
);
845 /* start SPI transfer */
846 write_CTRL(drv_data
, (cr
| BIT_CTL_ENABLE
));
849 /* IO mode write then read */
850 dev_dbg(&drv_data
->pdev
->dev
, "doing IO transfer\n");
853 /* full duplex mode */
854 BUG_ON((drv_data
->tx_end
- drv_data
->tx
) !=
855 (drv_data
->rx_end
- drv_data
->rx
));
856 dev_dbg(&drv_data
->pdev
->dev
,
857 "IO duplex: cr is 0x%x\n", cr
);
859 /* set SPI transfer mode */
860 write_CTRL(drv_data
, (cr
| CFG_SPI_WRITE
));
862 drv_data
->duplex(drv_data
);
864 if (drv_data
->tx
!= drv_data
->tx_end
)
866 } else if (drv_data
->tx
!= NULL
) {
867 /* write only half duplex */
868 dev_dbg(&drv_data
->pdev
->dev
,
869 "IO write: cr is 0x%x\n", cr
);
871 /* set SPI transfer mode */
872 write_CTRL(drv_data
, (cr
| CFG_SPI_WRITE
));
874 drv_data
->write(drv_data
);
876 if (drv_data
->tx
!= drv_data
->tx_end
)
878 } else if (drv_data
->rx
!= NULL
) {
879 /* read only half duplex */
880 dev_dbg(&drv_data
->pdev
->dev
,
881 "IO read: cr is 0x%x\n", cr
);
883 /* set SPI transfer mode */
884 write_CTRL(drv_data
, (cr
| CFG_SPI_READ
));
886 drv_data
->read(drv_data
);
887 if (drv_data
->rx
!= drv_data
->rx_end
)
891 if (!tranf_success
) {
892 dev_dbg(&drv_data
->pdev
->dev
,
893 "IO write error!\n");
894 message
->state
= ERROR_STATE
;
896 /* Update total byte transfered */
897 message
->actual_length
+= drv_data
->len_in_bytes
;
899 /* Move to next transfer of this msg */
900 message
->state
= next_transfer(drv_data
);
903 /* Schedule next transfer tasklet */
904 tasklet_schedule(&drv_data
->pump_transfers
);
909 /* pop a msg from queue and kick off real transfer */
910 static void pump_messages(struct work_struct
*work
)
912 struct driver_data
*drv_data
;
915 drv_data
= container_of(work
, struct driver_data
, pump_messages
);
917 /* Lock queue and check for queue work */
918 spin_lock_irqsave(&drv_data
->lock
, flags
);
919 if (list_empty(&drv_data
->queue
) || drv_data
->run
== QUEUE_STOPPED
) {
920 /* pumper kicked off but no work to do */
922 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
926 /* Make sure we are not already running a message */
927 if (drv_data
->cur_msg
) {
928 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
932 /* Extract head of queue */
933 drv_data
->cur_msg
= list_entry(drv_data
->queue
.next
,
934 struct spi_message
, queue
);
936 /* Setup the SSP using the per chip configuration */
937 drv_data
->cur_chip
= spi_get_ctldata(drv_data
->cur_msg
->spi
);
938 restore_state(drv_data
);
940 list_del_init(&drv_data
->cur_msg
->queue
);
942 /* Initial message state */
943 drv_data
->cur_msg
->state
= START_STATE
;
944 drv_data
->cur_transfer
= list_entry(drv_data
->cur_msg
->transfers
.next
,
945 struct spi_transfer
, transfer_list
);
947 dev_dbg(&drv_data
->pdev
->dev
, "got a message to pump, "
948 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
949 drv_data
->cur_chip
->baud
, drv_data
->cur_chip
->flag
,
950 drv_data
->cur_chip
->ctl_reg
);
952 dev_dbg(&drv_data
->pdev
->dev
,
953 "the first transfer len is %d\n",
954 drv_data
->cur_transfer
->len
);
956 /* Mark as busy and launch transfers */
957 tasklet_schedule(&drv_data
->pump_transfers
);
960 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
964 * got a msg to transfer, queue it in drv_data->queue.
965 * And kick off message pumper
967 static int transfer(struct spi_device
*spi
, struct spi_message
*msg
)
969 struct driver_data
*drv_data
= spi_master_get_devdata(spi
->master
);
972 spin_lock_irqsave(&drv_data
->lock
, flags
);
974 if (drv_data
->run
== QUEUE_STOPPED
) {
975 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
979 msg
->actual_length
= 0;
980 msg
->status
= -EINPROGRESS
;
981 msg
->state
= START_STATE
;
983 dev_dbg(&spi
->dev
, "adding an msg in transfer() \n");
984 list_add_tail(&msg
->queue
, &drv_data
->queue
);
986 if (drv_data
->run
== QUEUE_RUNNING
&& !drv_data
->busy
)
987 queue_work(drv_data
->workqueue
, &drv_data
->pump_messages
);
989 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
994 #define MAX_SPI_SSEL 7
996 static u16 ssel
[3][MAX_SPI_SSEL
] = {
997 {P_SPI0_SSEL1
, P_SPI0_SSEL2
, P_SPI0_SSEL3
,
998 P_SPI0_SSEL4
, P_SPI0_SSEL5
,
999 P_SPI0_SSEL6
, P_SPI0_SSEL7
},
1001 {P_SPI1_SSEL1
, P_SPI1_SSEL2
, P_SPI1_SSEL3
,
1002 P_SPI1_SSEL4
, P_SPI1_SSEL5
,
1003 P_SPI1_SSEL6
, P_SPI1_SSEL7
},
1005 {P_SPI2_SSEL1
, P_SPI2_SSEL2
, P_SPI2_SSEL3
,
1006 P_SPI2_SSEL4
, P_SPI2_SSEL5
,
1007 P_SPI2_SSEL6
, P_SPI2_SSEL7
},
1010 /* first setup for new devices */
1011 static int setup(struct spi_device
*spi
)
1013 struct bfin5xx_spi_chip
*chip_info
= NULL
;
1014 struct chip_data
*chip
;
1015 struct driver_data
*drv_data
= spi_master_get_devdata(spi
->master
);
1018 /* Abort device setup if requested features are not supported */
1019 if (spi
->mode
& ~(SPI_CPOL
| SPI_CPHA
| SPI_LSB_FIRST
)) {
1020 dev_err(&spi
->dev
, "requested mode not fully supported\n");
1024 /* Zero (the default) here means 8 bits */
1025 if (!spi
->bits_per_word
)
1026 spi
->bits_per_word
= 8;
1028 if (spi
->bits_per_word
!= 8 && spi
->bits_per_word
!= 16)
1031 /* Only alloc (or use chip_info) on first setup */
1032 chip
= spi_get_ctldata(spi
);
1034 chip
= kzalloc(sizeof(struct chip_data
), GFP_KERNEL
);
1038 chip
->enable_dma
= 0;
1039 chip_info
= spi
->controller_data
;
1042 /* chip_info isn't always needed */
1044 /* Make sure people stop trying to set fields via ctl_reg
1045 * when they should actually be using common SPI framework.
1046 * Currently we let through: WOM EMISO PSSE GM SZ TIMOD.
1047 * Not sure if a user actually needs/uses any of these,
1048 * but let's assume (for now) they do.
1050 if (chip_info
->ctl_reg
& (SPE
|MSTR
|CPOL
|CPHA
|LSBF
|SIZE
)) {
1051 dev_err(&spi
->dev
, "do not set bits in ctl_reg "
1052 "that the SPI framework manages\n");
1056 chip
->enable_dma
= chip_info
->enable_dma
!= 0
1057 && drv_data
->master_info
->enable_dma
;
1058 chip
->ctl_reg
= chip_info
->ctl_reg
;
1059 chip
->bits_per_word
= chip_info
->bits_per_word
;
1060 chip
->cs_change_per_word
= chip_info
->cs_change_per_word
;
1061 chip
->cs_chg_udelay
= chip_info
->cs_chg_udelay
;
1064 /* translate common spi framework into our register */
1065 if (spi
->mode
& SPI_CPOL
)
1066 chip
->ctl_reg
|= CPOL
;
1067 if (spi
->mode
& SPI_CPHA
)
1068 chip
->ctl_reg
|= CPHA
;
1069 if (spi
->mode
& SPI_LSB_FIRST
)
1070 chip
->ctl_reg
|= LSBF
;
1071 /* we dont support running in slave mode (yet?) */
1072 chip
->ctl_reg
|= MSTR
;
1075 * if any one SPI chip is registered and wants DMA, request the
1076 * DMA channel for it
1078 if (chip
->enable_dma
&& !drv_data
->dma_requested
) {
1079 /* register dma irq handler */
1080 if (request_dma(drv_data
->dma_channel
, "BF53x_SPI_DMA") < 0) {
1082 "Unable to request BlackFin SPI DMA channel\n");
1085 if (set_dma_callback(drv_data
->dma_channel
,
1086 (void *)dma_irq_handler
, drv_data
) < 0) {
1087 dev_dbg(&spi
->dev
, "Unable to set dma callback\n");
1090 dma_disable_irq(drv_data
->dma_channel
);
1091 drv_data
->dma_requested
= 1;
1095 * Notice: for blackfin, the speed_hz is the value of register
1096 * SPI_BAUD, not the real baudrate
1098 chip
->baud
= hz_to_spi_baud(spi
->max_speed_hz
);
1099 spi_flg
= ~(1 << (spi
->chip_select
));
1100 chip
->flag
= ((u16
) spi_flg
<< 8) | (1 << (spi
->chip_select
));
1101 chip
->chip_select_num
= spi
->chip_select
;
1103 switch (chip
->bits_per_word
) {
1106 chip
->width
= CFG_SPI_WORDSIZE8
;
1107 chip
->read
= chip
->cs_change_per_word
?
1108 u8_cs_chg_reader
: u8_reader
;
1109 chip
->write
= chip
->cs_change_per_word
?
1110 u8_cs_chg_writer
: u8_writer
;
1111 chip
->duplex
= chip
->cs_change_per_word
?
1112 u8_cs_chg_duplex
: u8_duplex
;
1117 chip
->width
= CFG_SPI_WORDSIZE16
;
1118 chip
->read
= chip
->cs_change_per_word
?
1119 u16_cs_chg_reader
: u16_reader
;
1120 chip
->write
= chip
->cs_change_per_word
?
1121 u16_cs_chg_writer
: u16_writer
;
1122 chip
->duplex
= chip
->cs_change_per_word
?
1123 u16_cs_chg_duplex
: u16_duplex
;
1127 dev_err(&spi
->dev
, "%d bits_per_word is not supported\n",
1128 chip
->bits_per_word
);
1133 dev_dbg(&spi
->dev
, "setup spi chip %s, width is %d, dma is %d\n",
1134 spi
->modalias
, chip
->width
, chip
->enable_dma
);
1135 dev_dbg(&spi
->dev
, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
1136 chip
->ctl_reg
, chip
->flag
);
1138 spi_set_ctldata(spi
, chip
);
1140 dev_dbg(&spi
->dev
, "chip select number is %d\n", chip
->chip_select_num
);
1141 if ((chip
->chip_select_num
> 0)
1142 && (chip
->chip_select_num
<= spi
->master
->num_chipselect
))
1143 peripheral_request(ssel
[spi
->master
->bus_num
]
1144 [chip
->chip_select_num
-1], spi
->modalias
);
1146 cs_deactive(drv_data
, chip
);
1152 * callback for spi framework.
1153 * clean driver specific data
1155 static void cleanup(struct spi_device
*spi
)
1157 struct chip_data
*chip
= spi_get_ctldata(spi
);
1159 if ((chip
->chip_select_num
> 0)
1160 && (chip
->chip_select_num
<= spi
->master
->num_chipselect
))
1161 peripheral_free(ssel
[spi
->master
->bus_num
]
1162 [chip
->chip_select_num
-1]);
1167 static inline int init_queue(struct driver_data
*drv_data
)
1169 INIT_LIST_HEAD(&drv_data
->queue
);
1170 spin_lock_init(&drv_data
->lock
);
1172 drv_data
->run
= QUEUE_STOPPED
;
1175 /* init transfer tasklet */
1176 tasklet_init(&drv_data
->pump_transfers
,
1177 pump_transfers
, (unsigned long)drv_data
);
1179 /* init messages workqueue */
1180 INIT_WORK(&drv_data
->pump_messages
, pump_messages
);
1181 drv_data
->workqueue
= create_singlethread_workqueue(
1182 dev_name(drv_data
->master
->dev
.parent
));
1183 if (drv_data
->workqueue
== NULL
)
1189 static inline int start_queue(struct driver_data
*drv_data
)
1191 unsigned long flags
;
1193 spin_lock_irqsave(&drv_data
->lock
, flags
);
1195 if (drv_data
->run
== QUEUE_RUNNING
|| drv_data
->busy
) {
1196 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1200 drv_data
->run
= QUEUE_RUNNING
;
1201 drv_data
->cur_msg
= NULL
;
1202 drv_data
->cur_transfer
= NULL
;
1203 drv_data
->cur_chip
= NULL
;
1204 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1206 queue_work(drv_data
->workqueue
, &drv_data
->pump_messages
);
1211 static inline int stop_queue(struct driver_data
*drv_data
)
1213 unsigned long flags
;
1214 unsigned limit
= 500;
1217 spin_lock_irqsave(&drv_data
->lock
, flags
);
1220 * This is a bit lame, but is optimized for the common execution path.
1221 * A wait_queue on the drv_data->busy could be used, but then the common
1222 * execution path (pump_messages) would be required to call wake_up or
1223 * friends on every SPI message. Do this instead
1225 drv_data
->run
= QUEUE_STOPPED
;
1226 while (!list_empty(&drv_data
->queue
) && drv_data
->busy
&& limit
--) {
1227 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1229 spin_lock_irqsave(&drv_data
->lock
, flags
);
1232 if (!list_empty(&drv_data
->queue
) || drv_data
->busy
)
1235 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1240 static inline int destroy_queue(struct driver_data
*drv_data
)
1244 status
= stop_queue(drv_data
);
1248 destroy_workqueue(drv_data
->workqueue
);
1253 static int __init
bfin5xx_spi_probe(struct platform_device
*pdev
)
1255 struct device
*dev
= &pdev
->dev
;
1256 struct bfin5xx_spi_master
*platform_info
;
1257 struct spi_master
*master
;
1258 struct driver_data
*drv_data
= 0;
1259 struct resource
*res
;
1262 platform_info
= dev
->platform_data
;
1264 /* Allocate master with space for drv_data */
1265 master
= spi_alloc_master(dev
, sizeof(struct driver_data
) + 16);
1267 dev_err(&pdev
->dev
, "can not alloc spi_master\n");
1271 drv_data
= spi_master_get_devdata(master
);
1272 drv_data
->master
= master
;
1273 drv_data
->master_info
= platform_info
;
1274 drv_data
->pdev
= pdev
;
1275 drv_data
->pin_req
= platform_info
->pin_req
;
1277 master
->bus_num
= pdev
->id
;
1278 master
->num_chipselect
= platform_info
->num_chipselect
;
1279 master
->cleanup
= cleanup
;
1280 master
->setup
= setup
;
1281 master
->transfer
= transfer
;
1283 /* Find and map our resources */
1284 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1286 dev_err(dev
, "Cannot get IORESOURCE_MEM\n");
1288 goto out_error_get_res
;
1291 drv_data
->regs_base
= ioremap(res
->start
, (res
->end
- res
->start
+ 1));
1292 if (drv_data
->regs_base
== NULL
) {
1293 dev_err(dev
, "Cannot map IO\n");
1295 goto out_error_ioremap
;
1298 drv_data
->dma_channel
= platform_get_irq(pdev
, 0);
1299 if (drv_data
->dma_channel
< 0) {
1300 dev_err(dev
, "No DMA channel specified\n");
1302 goto out_error_no_dma_ch
;
1305 /* Initial and start queue */
1306 status
= init_queue(drv_data
);
1308 dev_err(dev
, "problem initializing queue\n");
1309 goto out_error_queue_alloc
;
1312 status
= start_queue(drv_data
);
1314 dev_err(dev
, "problem starting queue\n");
1315 goto out_error_queue_alloc
;
1318 status
= peripheral_request_list(drv_data
->pin_req
, DRV_NAME
);
1320 dev_err(&pdev
->dev
, ": Requesting Peripherals failed\n");
1321 goto out_error_queue_alloc
;
1324 /* Register with the SPI framework */
1325 platform_set_drvdata(pdev
, drv_data
);
1326 status
= spi_register_master(master
);
1328 dev_err(dev
, "problem registering spi master\n");
1329 goto out_error_queue_alloc
;
1332 dev_info(dev
, "%s, Version %s, regs_base@%p, dma channel@%d\n",
1333 DRV_DESC
, DRV_VERSION
, drv_data
->regs_base
,
1334 drv_data
->dma_channel
);
1337 out_error_queue_alloc
:
1338 destroy_queue(drv_data
);
1339 out_error_no_dma_ch
:
1340 iounmap((void *) drv_data
->regs_base
);
1343 spi_master_put(master
);
1348 /* stop hardware and remove the driver */
1349 static int __devexit
bfin5xx_spi_remove(struct platform_device
*pdev
)
1351 struct driver_data
*drv_data
= platform_get_drvdata(pdev
);
1357 /* Remove the queue */
1358 status
= destroy_queue(drv_data
);
1362 /* Disable the SSP at the peripheral and SOC level */
1363 bfin_spi_disable(drv_data
);
1366 if (drv_data
->master_info
->enable_dma
) {
1367 if (dma_channel_active(drv_data
->dma_channel
))
1368 free_dma(drv_data
->dma_channel
);
1371 /* Disconnect from the SPI framework */
1372 spi_unregister_master(drv_data
->master
);
1374 peripheral_free_list(drv_data
->pin_req
);
1376 /* Prevent double remove */
1377 platform_set_drvdata(pdev
, NULL
);
1383 static int bfin5xx_spi_suspend(struct platform_device
*pdev
, pm_message_t state
)
1385 struct driver_data
*drv_data
= platform_get_drvdata(pdev
);
1388 status
= stop_queue(drv_data
);
1393 bfin_spi_disable(drv_data
);
1398 static int bfin5xx_spi_resume(struct platform_device
*pdev
)
1400 struct driver_data
*drv_data
= platform_get_drvdata(pdev
);
1403 /* Enable the SPI interface */
1404 bfin_spi_enable(drv_data
);
1406 /* Start the queue running */
1407 status
= start_queue(drv_data
);
1409 dev_err(&pdev
->dev
, "problem starting queue (%d)\n", status
);
1416 #define bfin5xx_spi_suspend NULL
1417 #define bfin5xx_spi_resume NULL
1418 #endif /* CONFIG_PM */
1420 MODULE_ALIAS("platform:bfin-spi");
1421 static struct platform_driver bfin5xx_spi_driver
= {
1424 .owner
= THIS_MODULE
,
1426 .suspend
= bfin5xx_spi_suspend
,
1427 .resume
= bfin5xx_spi_resume
,
1428 .remove
= __devexit_p(bfin5xx_spi_remove
),
1431 static int __init
bfin5xx_spi_init(void)
1433 return platform_driver_probe(&bfin5xx_spi_driver
, bfin5xx_spi_probe
);
1435 module_init(bfin5xx_spi_init
);
1437 static void __exit
bfin5xx_spi_exit(void)
1439 platform_driver_unregister(&bfin5xx_spi_driver
);
1441 module_exit(bfin5xx_spi_exit
);