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 /* stop controller and re-config current chip*/
202 static void restore_state(struct driver_data
*drv_data
)
204 struct chip_data
*chip
= drv_data
->cur_chip
;
206 /* Clear status and disable clock */
207 write_STAT(drv_data
, BIT_STAT_CLR
);
208 bfin_spi_disable(drv_data
);
209 dev_dbg(&drv_data
->pdev
->dev
, "restoring spi ctl state\n");
211 /* Load the registers */
212 write_CTRL(drv_data
, chip
->ctl_reg
);
213 write_BAUD(drv_data
, chip
->baud
);
215 bfin_spi_enable(drv_data
);
216 cs_active(drv_data
, chip
);
219 /* used to kick off transfer in rx mode */
220 static unsigned short dummy_read(struct driver_data
*drv_data
)
223 tmp
= read_RDBR(drv_data
);
227 static void null_writer(struct driver_data
*drv_data
)
229 u8 n_bytes
= drv_data
->n_bytes
;
231 while (drv_data
->tx
< drv_data
->tx_end
) {
232 write_TDBR(drv_data
, 0);
233 while ((read_STAT(drv_data
) & BIT_STAT_TXS
))
235 drv_data
->tx
+= n_bytes
;
239 static void null_reader(struct driver_data
*drv_data
)
241 u8 n_bytes
= drv_data
->n_bytes
;
242 dummy_read(drv_data
);
244 while (drv_data
->rx
< drv_data
->rx_end
) {
245 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
247 dummy_read(drv_data
);
248 drv_data
->rx
+= n_bytes
;
252 static void u8_writer(struct driver_data
*drv_data
)
254 dev_dbg(&drv_data
->pdev
->dev
,
255 "cr8-s is 0x%x\n", read_STAT(drv_data
));
257 while (drv_data
->tx
< drv_data
->tx_end
) {
258 write_TDBR(drv_data
, (*(u8
*) (drv_data
->tx
)));
259 while (read_STAT(drv_data
) & BIT_STAT_TXS
)
264 /* poll for SPI completion before return */
265 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
269 static void u8_cs_chg_writer(struct driver_data
*drv_data
)
271 struct chip_data
*chip
= drv_data
->cur_chip
;
273 while (drv_data
->tx
< drv_data
->tx_end
) {
274 cs_active(drv_data
, chip
);
276 write_TDBR(drv_data
, (*(u8
*) (drv_data
->tx
)));
277 while (read_STAT(drv_data
) & BIT_STAT_TXS
)
279 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
282 cs_deactive(drv_data
, chip
);
288 static void u8_reader(struct driver_data
*drv_data
)
290 dev_dbg(&drv_data
->pdev
->dev
,
291 "cr-8 is 0x%x\n", read_STAT(drv_data
));
293 /* poll for SPI completion before start */
294 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
297 /* clear TDBR buffer before read(else it will be shifted out) */
298 write_TDBR(drv_data
, 0xFFFF);
300 dummy_read(drv_data
);
302 while (drv_data
->rx
< drv_data
->rx_end
- 1) {
303 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
305 *(u8
*) (drv_data
->rx
) = read_RDBR(drv_data
);
309 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
311 *(u8
*) (drv_data
->rx
) = read_SHAW(drv_data
);
315 static void u8_cs_chg_reader(struct driver_data
*drv_data
)
317 struct chip_data
*chip
= drv_data
->cur_chip
;
319 while (drv_data
->rx
< drv_data
->rx_end
) {
320 cs_active(drv_data
, chip
);
321 read_RDBR(drv_data
); /* kick off */
323 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
325 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
328 *(u8
*) (drv_data
->rx
) = read_SHAW(drv_data
);
329 cs_deactive(drv_data
, chip
);
335 static void u8_duplex(struct driver_data
*drv_data
)
337 /* in duplex mode, clk is triggered by writing of TDBR */
338 while (drv_data
->rx
< drv_data
->rx_end
) {
339 write_TDBR(drv_data
, (*(u8
*) (drv_data
->tx
)));
340 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
342 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
344 *(u8
*) (drv_data
->rx
) = read_RDBR(drv_data
);
350 static void u8_cs_chg_duplex(struct driver_data
*drv_data
)
352 struct chip_data
*chip
= drv_data
->cur_chip
;
354 while (drv_data
->rx
< drv_data
->rx_end
) {
355 cs_active(drv_data
, chip
);
357 write_TDBR(drv_data
, (*(u8
*) (drv_data
->tx
)));
359 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
361 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
363 *(u8
*) (drv_data
->rx
) = read_RDBR(drv_data
);
365 cs_deactive(drv_data
, chip
);
372 static void u16_writer(struct driver_data
*drv_data
)
374 dev_dbg(&drv_data
->pdev
->dev
,
375 "cr16 is 0x%x\n", read_STAT(drv_data
));
377 while (drv_data
->tx
< drv_data
->tx_end
) {
378 write_TDBR(drv_data
, (*(u16
*) (drv_data
->tx
)));
379 while ((read_STAT(drv_data
) & BIT_STAT_TXS
))
384 /* poll for SPI completion before return */
385 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
389 static void u16_cs_chg_writer(struct driver_data
*drv_data
)
391 struct chip_data
*chip
= drv_data
->cur_chip
;
393 while (drv_data
->tx
< drv_data
->tx_end
) {
394 cs_active(drv_data
, chip
);
396 write_TDBR(drv_data
, (*(u16
*) (drv_data
->tx
)));
397 while ((read_STAT(drv_data
) & BIT_STAT_TXS
))
399 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
402 cs_deactive(drv_data
, chip
);
408 static void u16_reader(struct driver_data
*drv_data
)
410 dev_dbg(&drv_data
->pdev
->dev
,
411 "cr-16 is 0x%x\n", read_STAT(drv_data
));
413 /* poll for SPI completion before start */
414 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
417 /* clear TDBR buffer before read(else it will be shifted out) */
418 write_TDBR(drv_data
, 0xFFFF);
420 dummy_read(drv_data
);
422 while (drv_data
->rx
< (drv_data
->rx_end
- 2)) {
423 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
425 *(u16
*) (drv_data
->rx
) = read_RDBR(drv_data
);
429 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
431 *(u16
*) (drv_data
->rx
) = read_SHAW(drv_data
);
435 static void u16_cs_chg_reader(struct driver_data
*drv_data
)
437 struct chip_data
*chip
= drv_data
->cur_chip
;
439 /* poll for SPI completion before start */
440 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
443 /* clear TDBR buffer before read(else it will be shifted out) */
444 write_TDBR(drv_data
, 0xFFFF);
446 cs_active(drv_data
, chip
);
447 dummy_read(drv_data
);
449 while (drv_data
->rx
< drv_data
->rx_end
- 2) {
450 cs_deactive(drv_data
, chip
);
452 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
454 cs_active(drv_data
, chip
);
455 *(u16
*) (drv_data
->rx
) = read_RDBR(drv_data
);
458 cs_deactive(drv_data
, chip
);
460 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
462 *(u16
*) (drv_data
->rx
) = read_SHAW(drv_data
);
466 static void u16_duplex(struct driver_data
*drv_data
)
468 /* in duplex mode, clk is triggered by writing of TDBR */
469 while (drv_data
->tx
< drv_data
->tx_end
) {
470 write_TDBR(drv_data
, (*(u16
*) (drv_data
->tx
)));
471 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
473 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
475 *(u16
*) (drv_data
->rx
) = read_RDBR(drv_data
);
481 static void u16_cs_chg_duplex(struct driver_data
*drv_data
)
483 struct chip_data
*chip
= drv_data
->cur_chip
;
485 while (drv_data
->tx
< drv_data
->tx_end
) {
486 cs_active(drv_data
, chip
);
488 write_TDBR(drv_data
, (*(u16
*) (drv_data
->tx
)));
489 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
491 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
493 *(u16
*) (drv_data
->rx
) = read_RDBR(drv_data
);
495 cs_deactive(drv_data
, chip
);
502 /* test if ther is more transfer to be done */
503 static void *next_transfer(struct driver_data
*drv_data
)
505 struct spi_message
*msg
= drv_data
->cur_msg
;
506 struct spi_transfer
*trans
= drv_data
->cur_transfer
;
508 /* Move to next transfer */
509 if (trans
->transfer_list
.next
!= &msg
->transfers
) {
510 drv_data
->cur_transfer
=
511 list_entry(trans
->transfer_list
.next
,
512 struct spi_transfer
, transfer_list
);
513 return RUNNING_STATE
;
519 * caller already set message->status;
520 * dma and pio irqs are blocked give finished message back
522 static void giveback(struct driver_data
*drv_data
)
524 struct chip_data
*chip
= drv_data
->cur_chip
;
525 struct spi_transfer
*last_transfer
;
527 struct spi_message
*msg
;
529 spin_lock_irqsave(&drv_data
->lock
, flags
);
530 msg
= drv_data
->cur_msg
;
531 drv_data
->cur_msg
= NULL
;
532 drv_data
->cur_transfer
= NULL
;
533 drv_data
->cur_chip
= NULL
;
534 queue_work(drv_data
->workqueue
, &drv_data
->pump_messages
);
535 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
537 last_transfer
= list_entry(msg
->transfers
.prev
,
538 struct spi_transfer
, transfer_list
);
542 /* disable chip select signal. And not stop spi in autobuffer mode */
543 if (drv_data
->tx_dma
!= 0xFFFF) {
544 cs_deactive(drv_data
, chip
);
545 bfin_spi_disable(drv_data
);
548 if (!drv_data
->cs_change
)
549 cs_deactive(drv_data
, chip
);
552 msg
->complete(msg
->context
);
555 static irqreturn_t
dma_irq_handler(int irq
, void *dev_id
)
557 struct driver_data
*drv_data
= dev_id
;
558 struct chip_data
*chip
= drv_data
->cur_chip
;
559 struct spi_message
*msg
= drv_data
->cur_msg
;
560 unsigned long timeout
;
561 unsigned short dmastat
= get_dma_curr_irqstat(drv_data
->dma_channel
);
562 u16 spistat
= read_STAT(drv_data
);
564 dev_dbg(&drv_data
->pdev
->dev
,
565 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
568 clear_dma_irqstat(drv_data
->dma_channel
);
570 /* Wait for DMA to complete */
571 while (get_dma_curr_irqstat(drv_data
->dma_channel
) & DMA_RUN
)
575 * wait for the last transaction shifted out. HRM states:
576 * at this point there may still be data in the SPI DMA FIFO waiting
577 * to be transmitted ... software needs to poll TXS in the SPI_STAT
578 * register until it goes low for 2 successive reads
580 if (drv_data
->tx
!= NULL
) {
581 while ((read_STAT(drv_data
) & TXS
) ||
582 (read_STAT(drv_data
) & TXS
))
586 dev_dbg(&drv_data
->pdev
->dev
,
587 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
588 dmastat
, read_STAT(drv_data
));
590 timeout
= jiffies
+ HZ
;
591 while (!(read_STAT(drv_data
) & SPIF
))
592 if (!time_before(jiffies
, timeout
)) {
593 dev_warn(&drv_data
->pdev
->dev
, "timeout waiting for SPIF");
598 if ((dmastat
& DMA_ERR
) && (spistat
& RBSY
)) {
599 msg
->state
= ERROR_STATE
;
600 dev_err(&drv_data
->pdev
->dev
, "dma receive: fifo/buffer overflow\n");
602 msg
->actual_length
+= drv_data
->len_in_bytes
;
604 if (drv_data
->cs_change
)
605 cs_deactive(drv_data
, chip
);
607 /* Move to next transfer */
608 msg
->state
= next_transfer(drv_data
);
611 /* Schedule transfer tasklet */
612 tasklet_schedule(&drv_data
->pump_transfers
);
614 /* free the irq handler before next transfer */
615 dev_dbg(&drv_data
->pdev
->dev
,
616 "disable dma channel irq%d\n",
617 drv_data
->dma_channel
);
618 dma_disable_irq(drv_data
->dma_channel
);
623 static void pump_transfers(unsigned long data
)
625 struct driver_data
*drv_data
= (struct driver_data
*)data
;
626 struct spi_message
*message
= NULL
;
627 struct spi_transfer
*transfer
= NULL
;
628 struct spi_transfer
*previous
= NULL
;
629 struct chip_data
*chip
= NULL
;
631 u16 cr
, dma_width
, dma_config
;
632 u32 tranf_success
= 1;
635 /* Get current state information */
636 message
= drv_data
->cur_msg
;
637 transfer
= drv_data
->cur_transfer
;
638 chip
= drv_data
->cur_chip
;
641 * if msg is error or done, report it back using complete() callback
644 /* Handle for abort */
645 if (message
->state
== ERROR_STATE
) {
646 dev_dbg(&drv_data
->pdev
->dev
, "transfer: we've hit an error\n");
647 message
->status
= -EIO
;
652 /* Handle end of message */
653 if (message
->state
== DONE_STATE
) {
654 dev_dbg(&drv_data
->pdev
->dev
, "transfer: all done!\n");
660 /* Delay if requested at end of transfer */
661 if (message
->state
== RUNNING_STATE
) {
662 dev_dbg(&drv_data
->pdev
->dev
, "transfer: still running ...\n");
663 previous
= list_entry(transfer
->transfer_list
.prev
,
664 struct spi_transfer
, transfer_list
);
665 if (previous
->delay_usecs
)
666 udelay(previous
->delay_usecs
);
669 /* Setup the transfer state based on the type of transfer */
670 if (flush(drv_data
) == 0) {
671 dev_err(&drv_data
->pdev
->dev
, "pump_transfers: flush failed\n");
672 message
->status
= -EIO
;
677 if (transfer
->tx_buf
!= NULL
) {
678 drv_data
->tx
= (void *)transfer
->tx_buf
;
679 drv_data
->tx_end
= drv_data
->tx
+ transfer
->len
;
680 dev_dbg(&drv_data
->pdev
->dev
, "tx_buf is %p, tx_end is %p\n",
681 transfer
->tx_buf
, drv_data
->tx_end
);
686 if (transfer
->rx_buf
!= NULL
) {
687 full_duplex
= transfer
->tx_buf
!= NULL
;
688 drv_data
->rx
= transfer
->rx_buf
;
689 drv_data
->rx_end
= drv_data
->rx
+ transfer
->len
;
690 dev_dbg(&drv_data
->pdev
->dev
, "rx_buf is %p, rx_end is %p\n",
691 transfer
->rx_buf
, drv_data
->rx_end
);
696 drv_data
->rx_dma
= transfer
->rx_dma
;
697 drv_data
->tx_dma
= transfer
->tx_dma
;
698 drv_data
->len_in_bytes
= transfer
->len
;
699 drv_data
->cs_change
= transfer
->cs_change
;
701 /* Bits per word setup */
702 switch (transfer
->bits_per_word
) {
704 drv_data
->n_bytes
= 1;
705 width
= CFG_SPI_WORDSIZE8
;
706 drv_data
->read
= chip
->cs_change_per_word
?
707 u8_cs_chg_reader
: u8_reader
;
708 drv_data
->write
= chip
->cs_change_per_word
?
709 u8_cs_chg_writer
: u8_writer
;
710 drv_data
->duplex
= chip
->cs_change_per_word
?
711 u8_cs_chg_duplex
: u8_duplex
;
715 drv_data
->n_bytes
= 2;
716 width
= CFG_SPI_WORDSIZE16
;
717 drv_data
->read
= chip
->cs_change_per_word
?
718 u16_cs_chg_reader
: u16_reader
;
719 drv_data
->write
= chip
->cs_change_per_word
?
720 u16_cs_chg_writer
: u16_writer
;
721 drv_data
->duplex
= chip
->cs_change_per_word
?
722 u16_cs_chg_duplex
: u16_duplex
;
726 /* No change, the same as default setting */
727 drv_data
->n_bytes
= chip
->n_bytes
;
729 drv_data
->write
= drv_data
->tx
? chip
->write
: null_writer
;
730 drv_data
->read
= drv_data
->rx
? chip
->read
: null_reader
;
731 drv_data
->duplex
= chip
->duplex
? chip
->duplex
: null_writer
;
734 cr
= (read_CTRL(drv_data
) & (~BIT_CTL_TIMOD
));
736 write_CTRL(drv_data
, cr
);
738 if (width
== CFG_SPI_WORDSIZE16
) {
739 drv_data
->len
= (transfer
->len
) >> 1;
741 drv_data
->len
= transfer
->len
;
743 dev_dbg(&drv_data
->pdev
->dev
,
744 "transfer: drv_data->write is %p, chip->write is %p, null_wr is %p\n",
745 drv_data
->write
, chip
->write
, null_writer
);
747 /* speed and width has been set on per message */
748 message
->state
= RUNNING_STATE
;
751 /* Speed setup (surely valid because already checked) */
752 if (transfer
->speed_hz
)
753 write_BAUD(drv_data
, hz_to_spi_baud(transfer
->speed_hz
));
755 write_BAUD(drv_data
, chip
->baud
);
757 write_STAT(drv_data
, BIT_STAT_CLR
);
758 cr
= (read_CTRL(drv_data
) & (~BIT_CTL_TIMOD
));
759 cs_active(drv_data
, chip
);
761 dev_dbg(&drv_data
->pdev
->dev
,
762 "now pumping a transfer: width is %d, len is %d\n",
763 width
, transfer
->len
);
766 * Try to map dma buffer and do a dma transfer. If successful use,
767 * different way to r/w according to the enable_dma settings and if
768 * we are not doing a full duplex transfer (since the hardware does
769 * not support full duplex DMA transfers).
771 if (!full_duplex
&& drv_data
->cur_chip
->enable_dma
772 && drv_data
->len
> 6) {
774 unsigned long dma_start_addr
, flags
;
776 disable_dma(drv_data
->dma_channel
);
777 clear_dma_irqstat(drv_data
->dma_channel
);
779 /* config dma channel */
780 dev_dbg(&drv_data
->pdev
->dev
, "doing dma transfer\n");
781 set_dma_x_count(drv_data
->dma_channel
, drv_data
->len
);
782 if (width
== CFG_SPI_WORDSIZE16
) {
783 set_dma_x_modify(drv_data
->dma_channel
, 2);
784 dma_width
= WDSIZE_16
;
786 set_dma_x_modify(drv_data
->dma_channel
, 1);
787 dma_width
= WDSIZE_8
;
790 /* poll for SPI completion before start */
791 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
794 /* dirty hack for autobuffer DMA mode */
795 if (drv_data
->tx_dma
== 0xFFFF) {
796 dev_dbg(&drv_data
->pdev
->dev
,
797 "doing autobuffer DMA out.\n");
799 /* no irq in autobuffer mode */
801 (DMAFLOW_AUTO
| RESTART
| dma_width
| DI_EN
);
802 set_dma_config(drv_data
->dma_channel
, dma_config
);
803 set_dma_start_addr(drv_data
->dma_channel
,
804 (unsigned long)drv_data
->tx
);
805 enable_dma(drv_data
->dma_channel
);
807 /* start SPI transfer */
808 write_CTRL(drv_data
, cr
| BIT_CTL_TIMOD_DMA_TX
);
810 /* just return here, there can only be one transfer
818 /* In dma mode, rx or tx must be NULL in one transfer */
819 dma_config
= (RESTART
| dma_width
| DI_EN
);
820 if (drv_data
->rx
!= NULL
) {
821 /* set transfer mode, and enable SPI */
822 dev_dbg(&drv_data
->pdev
->dev
, "doing DMA in to %p (size %zx)\n",
823 drv_data
->rx
, drv_data
->len_in_bytes
);
825 /* invalidate caches, if needed */
826 if (bfin_addr_dcachable((unsigned long) drv_data
->rx
))
827 invalidate_dcache_range((unsigned long) drv_data
->rx
,
828 (unsigned long) (drv_data
->rx
+
829 drv_data
->len_in_bytes
));
831 /* clear tx reg soformer data is not shifted out */
832 write_TDBR(drv_data
, 0xFFFF);
835 dma_start_addr
= (unsigned long)drv_data
->rx
;
836 cr
|= BIT_CTL_TIMOD_DMA_RX
| BIT_CTL_SENDOPT
;
838 } else if (drv_data
->tx
!= NULL
) {
839 dev_dbg(&drv_data
->pdev
->dev
, "doing DMA out.\n");
841 /* flush caches, if needed */
842 if (bfin_addr_dcachable((unsigned long) drv_data
->tx
))
843 flush_dcache_range((unsigned long) drv_data
->tx
,
844 (unsigned long) (drv_data
->tx
+
845 drv_data
->len_in_bytes
));
847 dma_start_addr
= (unsigned long)drv_data
->tx
;
848 cr
|= BIT_CTL_TIMOD_DMA_TX
;
853 /* oh man, here there be monsters ... and i dont mean the
854 * fluffy cute ones from pixar, i mean the kind that'll eat
855 * your data, kick your dog, and love it all. do *not* try
856 * and change these lines unless you (1) heavily test DMA
857 * with SPI flashes on a loaded system (e.g. ping floods),
858 * (2) know just how broken the DMA engine interaction with
859 * the SPI peripheral is, and (3) have someone else to blame
860 * when you screw it all up anyways.
862 set_dma_start_addr(drv_data
->dma_channel
, dma_start_addr
);
863 set_dma_config(drv_data
->dma_channel
, dma_config
);
864 local_irq_save(flags
);
865 enable_dma(drv_data
->dma_channel
);
866 write_CTRL(drv_data
, cr
);
867 dma_enable_irq(drv_data
->dma_channel
);
868 local_irq_restore(flags
);
871 /* IO mode write then read */
872 dev_dbg(&drv_data
->pdev
->dev
, "doing IO transfer\n");
875 /* full duplex mode */
876 BUG_ON((drv_data
->tx_end
- drv_data
->tx
) !=
877 (drv_data
->rx_end
- drv_data
->rx
));
878 dev_dbg(&drv_data
->pdev
->dev
,
879 "IO duplex: cr is 0x%x\n", cr
);
881 /* set SPI transfer mode */
882 write_CTRL(drv_data
, (cr
| CFG_SPI_WRITE
));
884 drv_data
->duplex(drv_data
);
886 if (drv_data
->tx
!= drv_data
->tx_end
)
888 } else if (drv_data
->tx
!= NULL
) {
889 /* write only half duplex */
890 dev_dbg(&drv_data
->pdev
->dev
,
891 "IO write: cr is 0x%x\n", cr
);
893 /* set SPI transfer mode */
894 write_CTRL(drv_data
, (cr
| CFG_SPI_WRITE
));
896 drv_data
->write(drv_data
);
898 if (drv_data
->tx
!= drv_data
->tx_end
)
900 } else if (drv_data
->rx
!= NULL
) {
901 /* read only half duplex */
902 dev_dbg(&drv_data
->pdev
->dev
,
903 "IO read: cr is 0x%x\n", cr
);
905 /* set SPI transfer mode */
906 write_CTRL(drv_data
, (cr
| CFG_SPI_READ
));
908 drv_data
->read(drv_data
);
909 if (drv_data
->rx
!= drv_data
->rx_end
)
913 if (!tranf_success
) {
914 dev_dbg(&drv_data
->pdev
->dev
,
915 "IO write error!\n");
916 message
->state
= ERROR_STATE
;
918 /* Update total byte transfered */
919 message
->actual_length
+= drv_data
->len_in_bytes
;
921 /* Move to next transfer of this msg */
922 message
->state
= next_transfer(drv_data
);
925 /* Schedule next transfer tasklet */
926 tasklet_schedule(&drv_data
->pump_transfers
);
931 /* pop a msg from queue and kick off real transfer */
932 static void pump_messages(struct work_struct
*work
)
934 struct driver_data
*drv_data
;
937 drv_data
= container_of(work
, struct driver_data
, pump_messages
);
939 /* Lock queue and check for queue work */
940 spin_lock_irqsave(&drv_data
->lock
, flags
);
941 if (list_empty(&drv_data
->queue
) || drv_data
->run
== QUEUE_STOPPED
) {
942 /* pumper kicked off but no work to do */
944 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
948 /* Make sure we are not already running a message */
949 if (drv_data
->cur_msg
) {
950 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
954 /* Extract head of queue */
955 drv_data
->cur_msg
= list_entry(drv_data
->queue
.next
,
956 struct spi_message
, queue
);
958 /* Setup the SSP using the per chip configuration */
959 drv_data
->cur_chip
= spi_get_ctldata(drv_data
->cur_msg
->spi
);
960 restore_state(drv_data
);
962 list_del_init(&drv_data
->cur_msg
->queue
);
964 /* Initial message state */
965 drv_data
->cur_msg
->state
= START_STATE
;
966 drv_data
->cur_transfer
= list_entry(drv_data
->cur_msg
->transfers
.next
,
967 struct spi_transfer
, transfer_list
);
969 dev_dbg(&drv_data
->pdev
->dev
, "got a message to pump, "
970 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
971 drv_data
->cur_chip
->baud
, drv_data
->cur_chip
->flag
,
972 drv_data
->cur_chip
->ctl_reg
);
974 dev_dbg(&drv_data
->pdev
->dev
,
975 "the first transfer len is %d\n",
976 drv_data
->cur_transfer
->len
);
978 /* Mark as busy and launch transfers */
979 tasklet_schedule(&drv_data
->pump_transfers
);
982 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
986 * got a msg to transfer, queue it in drv_data->queue.
987 * And kick off message pumper
989 static int transfer(struct spi_device
*spi
, struct spi_message
*msg
)
991 struct driver_data
*drv_data
= spi_master_get_devdata(spi
->master
);
994 spin_lock_irqsave(&drv_data
->lock
, flags
);
996 if (drv_data
->run
== QUEUE_STOPPED
) {
997 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1001 msg
->actual_length
= 0;
1002 msg
->status
= -EINPROGRESS
;
1003 msg
->state
= START_STATE
;
1005 dev_dbg(&spi
->dev
, "adding an msg in transfer() \n");
1006 list_add_tail(&msg
->queue
, &drv_data
->queue
);
1008 if (drv_data
->run
== QUEUE_RUNNING
&& !drv_data
->busy
)
1009 queue_work(drv_data
->workqueue
, &drv_data
->pump_messages
);
1011 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1016 #define MAX_SPI_SSEL 7
1018 static u16 ssel
[][MAX_SPI_SSEL
] = {
1019 {P_SPI0_SSEL1
, P_SPI0_SSEL2
, P_SPI0_SSEL3
,
1020 P_SPI0_SSEL4
, P_SPI0_SSEL5
,
1021 P_SPI0_SSEL6
, P_SPI0_SSEL7
},
1023 {P_SPI1_SSEL1
, P_SPI1_SSEL2
, P_SPI1_SSEL3
,
1024 P_SPI1_SSEL4
, P_SPI1_SSEL5
,
1025 P_SPI1_SSEL6
, P_SPI1_SSEL7
},
1027 {P_SPI2_SSEL1
, P_SPI2_SSEL2
, P_SPI2_SSEL3
,
1028 P_SPI2_SSEL4
, P_SPI2_SSEL5
,
1029 P_SPI2_SSEL6
, P_SPI2_SSEL7
},
1032 /* first setup for new devices */
1033 static int setup(struct spi_device
*spi
)
1035 struct bfin5xx_spi_chip
*chip_info
= NULL
;
1036 struct chip_data
*chip
;
1037 struct driver_data
*drv_data
= spi_master_get_devdata(spi
->master
);
1040 /* Abort device setup if requested features are not supported */
1041 if (spi
->mode
& ~(SPI_CPOL
| SPI_CPHA
| SPI_LSB_FIRST
)) {
1042 dev_err(&spi
->dev
, "requested mode not fully supported\n");
1046 /* Zero (the default) here means 8 bits */
1047 if (!spi
->bits_per_word
)
1048 spi
->bits_per_word
= 8;
1050 if (spi
->bits_per_word
!= 8 && spi
->bits_per_word
!= 16)
1053 /* Only alloc (or use chip_info) on first setup */
1054 chip
= spi_get_ctldata(spi
);
1056 chip
= kzalloc(sizeof(struct chip_data
), GFP_KERNEL
);
1060 chip
->enable_dma
= 0;
1061 chip_info
= spi
->controller_data
;
1064 /* chip_info isn't always needed */
1066 /* Make sure people stop trying to set fields via ctl_reg
1067 * when they should actually be using common SPI framework.
1068 * Currently we let through: WOM EMISO PSSE GM SZ TIMOD.
1069 * Not sure if a user actually needs/uses any of these,
1070 * but let's assume (for now) they do.
1072 if (chip_info
->ctl_reg
& (SPE
|MSTR
|CPOL
|CPHA
|LSBF
|SIZE
)) {
1073 dev_err(&spi
->dev
, "do not set bits in ctl_reg "
1074 "that the SPI framework manages\n");
1078 chip
->enable_dma
= chip_info
->enable_dma
!= 0
1079 && drv_data
->master_info
->enable_dma
;
1080 chip
->ctl_reg
= chip_info
->ctl_reg
;
1081 chip
->bits_per_word
= chip_info
->bits_per_word
;
1082 chip
->cs_change_per_word
= chip_info
->cs_change_per_word
;
1083 chip
->cs_chg_udelay
= chip_info
->cs_chg_udelay
;
1086 /* translate common spi framework into our register */
1087 if (spi
->mode
& SPI_CPOL
)
1088 chip
->ctl_reg
|= CPOL
;
1089 if (spi
->mode
& SPI_CPHA
)
1090 chip
->ctl_reg
|= CPHA
;
1091 if (spi
->mode
& SPI_LSB_FIRST
)
1092 chip
->ctl_reg
|= LSBF
;
1093 /* we dont support running in slave mode (yet?) */
1094 chip
->ctl_reg
|= MSTR
;
1097 * if any one SPI chip is registered and wants DMA, request the
1098 * DMA channel for it
1100 if (chip
->enable_dma
&& !drv_data
->dma_requested
) {
1101 /* register dma irq handler */
1102 if (request_dma(drv_data
->dma_channel
, "BFIN_SPI_DMA") < 0) {
1104 "Unable to request BlackFin SPI DMA channel\n");
1107 if (set_dma_callback(drv_data
->dma_channel
,
1108 dma_irq_handler
, drv_data
) < 0) {
1109 dev_dbg(&spi
->dev
, "Unable to set dma callback\n");
1112 dma_disable_irq(drv_data
->dma_channel
);
1113 drv_data
->dma_requested
= 1;
1117 * Notice: for blackfin, the speed_hz is the value of register
1118 * SPI_BAUD, not the real baudrate
1120 chip
->baud
= hz_to_spi_baud(spi
->max_speed_hz
);
1121 spi_flg
= ~(1 << (spi
->chip_select
));
1122 chip
->flag
= ((u16
) spi_flg
<< 8) | (1 << (spi
->chip_select
));
1123 chip
->chip_select_num
= spi
->chip_select
;
1125 switch (chip
->bits_per_word
) {
1128 chip
->width
= CFG_SPI_WORDSIZE8
;
1129 chip
->read
= chip
->cs_change_per_word
?
1130 u8_cs_chg_reader
: u8_reader
;
1131 chip
->write
= chip
->cs_change_per_word
?
1132 u8_cs_chg_writer
: u8_writer
;
1133 chip
->duplex
= chip
->cs_change_per_word
?
1134 u8_cs_chg_duplex
: u8_duplex
;
1139 chip
->width
= CFG_SPI_WORDSIZE16
;
1140 chip
->read
= chip
->cs_change_per_word
?
1141 u16_cs_chg_reader
: u16_reader
;
1142 chip
->write
= chip
->cs_change_per_word
?
1143 u16_cs_chg_writer
: u16_writer
;
1144 chip
->duplex
= chip
->cs_change_per_word
?
1145 u16_cs_chg_duplex
: u16_duplex
;
1149 dev_err(&spi
->dev
, "%d bits_per_word is not supported\n",
1150 chip
->bits_per_word
);
1155 dev_dbg(&spi
->dev
, "setup spi chip %s, width is %d, dma is %d\n",
1156 spi
->modalias
, chip
->width
, chip
->enable_dma
);
1157 dev_dbg(&spi
->dev
, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
1158 chip
->ctl_reg
, chip
->flag
);
1160 spi_set_ctldata(spi
, chip
);
1162 dev_dbg(&spi
->dev
, "chip select number is %d\n", chip
->chip_select_num
);
1163 if ((chip
->chip_select_num
> 0)
1164 && (chip
->chip_select_num
<= spi
->master
->num_chipselect
))
1165 peripheral_request(ssel
[spi
->master
->bus_num
]
1166 [chip
->chip_select_num
-1], spi
->modalias
);
1168 cs_deactive(drv_data
, chip
);
1174 * callback for spi framework.
1175 * clean driver specific data
1177 static void cleanup(struct spi_device
*spi
)
1179 struct chip_data
*chip
= spi_get_ctldata(spi
);
1181 if ((chip
->chip_select_num
> 0)
1182 && (chip
->chip_select_num
<= spi
->master
->num_chipselect
))
1183 peripheral_free(ssel
[spi
->master
->bus_num
]
1184 [chip
->chip_select_num
-1]);
1189 static inline int init_queue(struct driver_data
*drv_data
)
1191 INIT_LIST_HEAD(&drv_data
->queue
);
1192 spin_lock_init(&drv_data
->lock
);
1194 drv_data
->run
= QUEUE_STOPPED
;
1197 /* init transfer tasklet */
1198 tasklet_init(&drv_data
->pump_transfers
,
1199 pump_transfers
, (unsigned long)drv_data
);
1201 /* init messages workqueue */
1202 INIT_WORK(&drv_data
->pump_messages
, pump_messages
);
1203 drv_data
->workqueue
= create_singlethread_workqueue(
1204 dev_name(drv_data
->master
->dev
.parent
));
1205 if (drv_data
->workqueue
== NULL
)
1211 static inline int start_queue(struct driver_data
*drv_data
)
1213 unsigned long flags
;
1215 spin_lock_irqsave(&drv_data
->lock
, flags
);
1217 if (drv_data
->run
== QUEUE_RUNNING
|| drv_data
->busy
) {
1218 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1222 drv_data
->run
= QUEUE_RUNNING
;
1223 drv_data
->cur_msg
= NULL
;
1224 drv_data
->cur_transfer
= NULL
;
1225 drv_data
->cur_chip
= NULL
;
1226 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1228 queue_work(drv_data
->workqueue
, &drv_data
->pump_messages
);
1233 static inline int stop_queue(struct driver_data
*drv_data
)
1235 unsigned long flags
;
1236 unsigned limit
= 500;
1239 spin_lock_irqsave(&drv_data
->lock
, flags
);
1242 * This is a bit lame, but is optimized for the common execution path.
1243 * A wait_queue on the drv_data->busy could be used, but then the common
1244 * execution path (pump_messages) would be required to call wake_up or
1245 * friends on every SPI message. Do this instead
1247 drv_data
->run
= QUEUE_STOPPED
;
1248 while (!list_empty(&drv_data
->queue
) && drv_data
->busy
&& limit
--) {
1249 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1251 spin_lock_irqsave(&drv_data
->lock
, flags
);
1254 if (!list_empty(&drv_data
->queue
) || drv_data
->busy
)
1257 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1262 static inline int destroy_queue(struct driver_data
*drv_data
)
1266 status
= stop_queue(drv_data
);
1270 destroy_workqueue(drv_data
->workqueue
);
1275 static int __init
bfin5xx_spi_probe(struct platform_device
*pdev
)
1277 struct device
*dev
= &pdev
->dev
;
1278 struct bfin5xx_spi_master
*platform_info
;
1279 struct spi_master
*master
;
1280 struct driver_data
*drv_data
= 0;
1281 struct resource
*res
;
1284 platform_info
= dev
->platform_data
;
1286 /* Allocate master with space for drv_data */
1287 master
= spi_alloc_master(dev
, sizeof(struct driver_data
) + 16);
1289 dev_err(&pdev
->dev
, "can not alloc spi_master\n");
1293 drv_data
= spi_master_get_devdata(master
);
1294 drv_data
->master
= master
;
1295 drv_data
->master_info
= platform_info
;
1296 drv_data
->pdev
= pdev
;
1297 drv_data
->pin_req
= platform_info
->pin_req
;
1299 master
->bus_num
= pdev
->id
;
1300 master
->num_chipselect
= platform_info
->num_chipselect
;
1301 master
->cleanup
= cleanup
;
1302 master
->setup
= setup
;
1303 master
->transfer
= transfer
;
1305 /* Find and map our resources */
1306 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1308 dev_err(dev
, "Cannot get IORESOURCE_MEM\n");
1310 goto out_error_get_res
;
1313 drv_data
->regs_base
= ioremap(res
->start
, (res
->end
- res
->start
+ 1));
1314 if (drv_data
->regs_base
== NULL
) {
1315 dev_err(dev
, "Cannot map IO\n");
1317 goto out_error_ioremap
;
1320 drv_data
->dma_channel
= platform_get_irq(pdev
, 0);
1321 if (drv_data
->dma_channel
< 0) {
1322 dev_err(dev
, "No DMA channel specified\n");
1324 goto out_error_no_dma_ch
;
1327 /* Initial and start queue */
1328 status
= init_queue(drv_data
);
1330 dev_err(dev
, "problem initializing queue\n");
1331 goto out_error_queue_alloc
;
1334 status
= start_queue(drv_data
);
1336 dev_err(dev
, "problem starting queue\n");
1337 goto out_error_queue_alloc
;
1340 status
= peripheral_request_list(drv_data
->pin_req
, DRV_NAME
);
1342 dev_err(&pdev
->dev
, ": Requesting Peripherals failed\n");
1343 goto out_error_queue_alloc
;
1346 /* Register with the SPI framework */
1347 platform_set_drvdata(pdev
, drv_data
);
1348 status
= spi_register_master(master
);
1350 dev_err(dev
, "problem registering spi master\n");
1351 goto out_error_queue_alloc
;
1354 dev_info(dev
, "%s, Version %s, regs_base@%p, dma channel@%d\n",
1355 DRV_DESC
, DRV_VERSION
, drv_data
->regs_base
,
1356 drv_data
->dma_channel
);
1359 out_error_queue_alloc
:
1360 destroy_queue(drv_data
);
1361 out_error_no_dma_ch
:
1362 iounmap((void *) drv_data
->regs_base
);
1365 spi_master_put(master
);
1370 /* stop hardware and remove the driver */
1371 static int __devexit
bfin5xx_spi_remove(struct platform_device
*pdev
)
1373 struct driver_data
*drv_data
= platform_get_drvdata(pdev
);
1379 /* Remove the queue */
1380 status
= destroy_queue(drv_data
);
1384 /* Disable the SSP at the peripheral and SOC level */
1385 bfin_spi_disable(drv_data
);
1388 if (drv_data
->master_info
->enable_dma
) {
1389 if (dma_channel_active(drv_data
->dma_channel
))
1390 free_dma(drv_data
->dma_channel
);
1393 /* Disconnect from the SPI framework */
1394 spi_unregister_master(drv_data
->master
);
1396 peripheral_free_list(drv_data
->pin_req
);
1398 /* Prevent double remove */
1399 platform_set_drvdata(pdev
, NULL
);
1405 static int bfin5xx_spi_suspend(struct platform_device
*pdev
, pm_message_t state
)
1407 struct driver_data
*drv_data
= platform_get_drvdata(pdev
);
1410 status
= stop_queue(drv_data
);
1415 bfin_spi_disable(drv_data
);
1420 static int bfin5xx_spi_resume(struct platform_device
*pdev
)
1422 struct driver_data
*drv_data
= platform_get_drvdata(pdev
);
1425 /* Enable the SPI interface */
1426 bfin_spi_enable(drv_data
);
1428 /* Start the queue running */
1429 status
= start_queue(drv_data
);
1431 dev_err(&pdev
->dev
, "problem starting queue (%d)\n", status
);
1438 #define bfin5xx_spi_suspend NULL
1439 #define bfin5xx_spi_resume NULL
1440 #endif /* CONFIG_PM */
1442 MODULE_ALIAS("platform:bfin-spi");
1443 static struct platform_driver bfin5xx_spi_driver
= {
1446 .owner
= THIS_MODULE
,
1448 .suspend
= bfin5xx_spi_suspend
,
1449 .resume
= bfin5xx_spi_resume
,
1450 .remove
= __devexit_p(bfin5xx_spi_remove
),
1453 static int __init
bfin5xx_spi_init(void)
1455 return platform_driver_probe(&bfin5xx_spi_driver
, bfin5xx_spi_probe
);
1457 module_init(bfin5xx_spi_init
);
1459 static void __exit
bfin5xx_spi_exit(void)
1461 platform_driver_unregister(&bfin5xx_spi_driver
);
1463 module_exit(bfin5xx_spi_exit
);