2 * Blackfin On-Chip SPI Driver
4 * Copyright 2004-2010 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>
15 #include <linux/slab.h>
17 #include <linux/ioport.h>
18 #include <linux/irq.h>
19 #include <linux/errno.h>
20 #include <linux/interrupt.h>
21 #include <linux/platform_device.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/spi/spi.h>
24 #include <linux/workqueue.h>
27 #include <asm/portmux.h>
28 #include <asm/bfin5xx_spi.h>
29 #include <asm/cacheflush.h>
31 #define DRV_NAME "bfin-spi"
32 #define DRV_AUTHOR "Bryan Wu, Luke Yang"
33 #define DRV_DESC "Blackfin on-chip SPI Controller Driver"
34 #define DRV_VERSION "1.0"
36 MODULE_AUTHOR(DRV_AUTHOR
);
37 MODULE_DESCRIPTION(DRV_DESC
);
38 MODULE_LICENSE("GPL");
40 #define START_STATE ((void *)0)
41 #define RUNNING_STATE ((void *)1)
42 #define DONE_STATE ((void *)2)
43 #define ERROR_STATE ((void *)-1)
45 struct bfin_spi_master_data
;
47 struct bfin_spi_transfer_ops
{
48 void (*write
) (struct bfin_spi_master_data
*);
49 void (*read
) (struct bfin_spi_master_data
*);
50 void (*duplex
) (struct bfin_spi_master_data
*);
53 struct bfin_spi_master_data
{
54 /* Driver model hookup */
55 struct platform_device
*pdev
;
57 /* SPI framework hookup */
58 struct spi_master
*master
;
60 /* Regs base of SPI controller */
61 void __iomem
*regs_base
;
63 /* Pin request list */
67 struct bfin5xx_spi_master
*master_info
;
69 /* Driver message queue */
70 struct workqueue_struct
*workqueue
;
71 struct work_struct pump_messages
;
73 struct list_head queue
;
77 /* Message Transfer pump */
78 struct tasklet_struct pump_transfers
;
80 /* Current message transfer state info */
81 struct spi_message
*cur_msg
;
82 struct spi_transfer
*cur_transfer
;
83 struct bfin_spi_slave_data
*cur_chip
;
108 const struct bfin_spi_transfer_ops
*ops
;
111 struct bfin_spi_slave_data
{
118 u16 cs_chg_udelay
; /* Some devices require > 255usec delay */
121 u8 pio_interrupt
; /* use spi data irq */
122 const struct bfin_spi_transfer_ops
*ops
;
125 #define DEFINE_SPI_REG(reg, off) \
126 static inline u16 read_##reg(struct bfin_spi_master_data *drv_data) \
127 { return bfin_read16(drv_data->regs_base + off); } \
128 static inline void write_##reg(struct bfin_spi_master_data *drv_data, u16 v) \
129 { bfin_write16(drv_data->regs_base + off, v); }
131 DEFINE_SPI_REG(CTRL
, 0x00)
132 DEFINE_SPI_REG(FLAG
, 0x04)
133 DEFINE_SPI_REG(STAT
, 0x08)
134 DEFINE_SPI_REG(TDBR
, 0x0C)
135 DEFINE_SPI_REG(RDBR
, 0x10)
136 DEFINE_SPI_REG(BAUD
, 0x14)
137 DEFINE_SPI_REG(SHAW
, 0x18)
139 static void bfin_spi_enable(struct bfin_spi_master_data
*drv_data
)
143 cr
= read_CTRL(drv_data
);
144 write_CTRL(drv_data
, (cr
| BIT_CTL_ENABLE
));
147 static void bfin_spi_disable(struct bfin_spi_master_data
*drv_data
)
151 cr
= read_CTRL(drv_data
);
152 write_CTRL(drv_data
, (cr
& (~BIT_CTL_ENABLE
)));
155 /* Caculate the SPI_BAUD register value based on input HZ */
156 static u16
hz_to_spi_baud(u32 speed_hz
)
158 u_long sclk
= get_sclk();
159 u16 spi_baud
= (sclk
/ (2 * speed_hz
));
161 if ((sclk
% (2 * speed_hz
)) > 0)
164 if (spi_baud
< MIN_SPI_BAUD_VAL
)
165 spi_baud
= MIN_SPI_BAUD_VAL
;
170 static int bfin_spi_flush(struct bfin_spi_master_data
*drv_data
)
172 unsigned long limit
= loops_per_jiffy
<< 1;
174 /* wait for stop and clear stat */
175 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
) && --limit
)
178 write_STAT(drv_data
, BIT_STAT_CLR
);
183 /* Chip select operation functions for cs_change flag */
184 static void bfin_spi_cs_active(struct bfin_spi_master_data
*drv_data
, struct bfin_spi_slave_data
*chip
)
186 if (likely(chip
->chip_select_num
< MAX_CTRL_CS
)) {
187 u16 flag
= read_FLAG(drv_data
);
191 write_FLAG(drv_data
, flag
);
193 gpio_set_value(chip
->cs_gpio
, 0);
197 static void bfin_spi_cs_deactive(struct bfin_spi_master_data
*drv_data
,
198 struct bfin_spi_slave_data
*chip
)
200 if (likely(chip
->chip_select_num
< MAX_CTRL_CS
)) {
201 u16 flag
= read_FLAG(drv_data
);
205 write_FLAG(drv_data
, flag
);
207 gpio_set_value(chip
->cs_gpio
, 1);
210 /* Move delay here for consistency */
211 if (chip
->cs_chg_udelay
)
212 udelay(chip
->cs_chg_udelay
);
215 /* enable or disable the pin muxed by GPIO and SPI CS to work as SPI CS */
216 static inline void bfin_spi_cs_enable(struct bfin_spi_master_data
*drv_data
,
217 struct bfin_spi_slave_data
*chip
)
219 if (chip
->chip_select_num
< MAX_CTRL_CS
) {
220 u16 flag
= read_FLAG(drv_data
);
222 flag
|= (chip
->flag
>> 8);
224 write_FLAG(drv_data
, flag
);
228 static inline void bfin_spi_cs_disable(struct bfin_spi_master_data
*drv_data
,
229 struct bfin_spi_slave_data
*chip
)
231 if (chip
->chip_select_num
< MAX_CTRL_CS
) {
232 u16 flag
= read_FLAG(drv_data
);
234 flag
&= ~(chip
->flag
>> 8);
236 write_FLAG(drv_data
, flag
);
240 /* stop controller and re-config current chip*/
241 static void bfin_spi_restore_state(struct bfin_spi_master_data
*drv_data
)
243 struct bfin_spi_slave_data
*chip
= drv_data
->cur_chip
;
245 /* Clear status and disable clock */
246 write_STAT(drv_data
, BIT_STAT_CLR
);
247 bfin_spi_disable(drv_data
);
248 dev_dbg(&drv_data
->pdev
->dev
, "restoring spi ctl state\n");
252 /* Load the registers */
253 write_CTRL(drv_data
, chip
->ctl_reg
);
254 write_BAUD(drv_data
, chip
->baud
);
256 bfin_spi_enable(drv_data
);
257 bfin_spi_cs_active(drv_data
, chip
);
260 /* used to kick off transfer in rx mode and read unwanted RX data */
261 static inline void bfin_spi_dummy_read(struct bfin_spi_master_data
*drv_data
)
263 (void) read_RDBR(drv_data
);
266 static void bfin_spi_u8_writer(struct bfin_spi_master_data
*drv_data
)
268 /* clear RXS (we check for RXS inside the loop) */
269 bfin_spi_dummy_read(drv_data
);
271 while (drv_data
->tx
< drv_data
->tx_end
) {
272 write_TDBR(drv_data
, (*(u8
*) (drv_data
->tx
++)));
273 /* wait until transfer finished.
274 checking SPIF or TXS may not guarantee transfer completion */
275 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
277 /* discard RX data and clear RXS */
278 bfin_spi_dummy_read(drv_data
);
282 static void bfin_spi_u8_reader(struct bfin_spi_master_data
*drv_data
)
284 u16 tx_val
= drv_data
->cur_chip
->idle_tx_val
;
286 /* discard old RX data and clear RXS */
287 bfin_spi_dummy_read(drv_data
);
289 while (drv_data
->rx
< drv_data
->rx_end
) {
290 write_TDBR(drv_data
, tx_val
);
291 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
293 *(u8
*) (drv_data
->rx
++) = read_RDBR(drv_data
);
297 static void bfin_spi_u8_duplex(struct bfin_spi_master_data
*drv_data
)
299 /* discard old RX data and clear RXS */
300 bfin_spi_dummy_read(drv_data
);
302 while (drv_data
->rx
< drv_data
->rx_end
) {
303 write_TDBR(drv_data
, (*(u8
*) (drv_data
->tx
++)));
304 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
306 *(u8
*) (drv_data
->rx
++) = read_RDBR(drv_data
);
310 static const struct bfin_spi_transfer_ops bfin_bfin_spi_transfer_ops_u8
= {
311 .write
= bfin_spi_u8_writer
,
312 .read
= bfin_spi_u8_reader
,
313 .duplex
= bfin_spi_u8_duplex
,
316 static void bfin_spi_u16_writer(struct bfin_spi_master_data
*drv_data
)
318 /* clear RXS (we check for RXS inside the loop) */
319 bfin_spi_dummy_read(drv_data
);
321 while (drv_data
->tx
< drv_data
->tx_end
) {
322 write_TDBR(drv_data
, (*(u16
*) (drv_data
->tx
)));
324 /* wait until transfer finished.
325 checking SPIF or TXS may not guarantee transfer completion */
326 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
328 /* discard RX data and clear RXS */
329 bfin_spi_dummy_read(drv_data
);
333 static void bfin_spi_u16_reader(struct bfin_spi_master_data
*drv_data
)
335 u16 tx_val
= drv_data
->cur_chip
->idle_tx_val
;
337 /* discard old RX data and clear RXS */
338 bfin_spi_dummy_read(drv_data
);
340 while (drv_data
->rx
< drv_data
->rx_end
) {
341 write_TDBR(drv_data
, tx_val
);
342 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
344 *(u16
*) (drv_data
->rx
) = read_RDBR(drv_data
);
349 static void bfin_spi_u16_duplex(struct bfin_spi_master_data
*drv_data
)
351 /* discard old RX data and clear RXS */
352 bfin_spi_dummy_read(drv_data
);
354 while (drv_data
->rx
< drv_data
->rx_end
) {
355 write_TDBR(drv_data
, (*(u16
*) (drv_data
->tx
)));
357 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
359 *(u16
*) (drv_data
->rx
) = read_RDBR(drv_data
);
364 static const struct bfin_spi_transfer_ops bfin_bfin_spi_transfer_ops_u16
= {
365 .write
= bfin_spi_u16_writer
,
366 .read
= bfin_spi_u16_reader
,
367 .duplex
= bfin_spi_u16_duplex
,
370 /* test if there is more transfer to be done */
371 static void *bfin_spi_next_transfer(struct bfin_spi_master_data
*drv_data
)
373 struct spi_message
*msg
= drv_data
->cur_msg
;
374 struct spi_transfer
*trans
= drv_data
->cur_transfer
;
376 /* Move to next transfer */
377 if (trans
->transfer_list
.next
!= &msg
->transfers
) {
378 drv_data
->cur_transfer
=
379 list_entry(trans
->transfer_list
.next
,
380 struct spi_transfer
, transfer_list
);
381 return RUNNING_STATE
;
387 * caller already set message->status;
388 * dma and pio irqs are blocked give finished message back
390 static void bfin_spi_giveback(struct bfin_spi_master_data
*drv_data
)
392 struct bfin_spi_slave_data
*chip
= drv_data
->cur_chip
;
393 struct spi_transfer
*last_transfer
;
395 struct spi_message
*msg
;
397 spin_lock_irqsave(&drv_data
->lock
, flags
);
398 msg
= drv_data
->cur_msg
;
399 drv_data
->cur_msg
= NULL
;
400 drv_data
->cur_transfer
= NULL
;
401 drv_data
->cur_chip
= NULL
;
402 queue_work(drv_data
->workqueue
, &drv_data
->pump_messages
);
403 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
405 last_transfer
= list_entry(msg
->transfers
.prev
,
406 struct spi_transfer
, transfer_list
);
410 if (!drv_data
->cs_change
)
411 bfin_spi_cs_deactive(drv_data
, chip
);
413 /* Not stop spi in autobuffer mode */
414 if (drv_data
->tx_dma
!= 0xFFFF)
415 bfin_spi_disable(drv_data
);
418 msg
->complete(msg
->context
);
421 /* spi data irq handler */
422 static irqreturn_t
bfin_spi_pio_irq_handler(int irq
, void *dev_id
)
424 struct bfin_spi_master_data
*drv_data
= dev_id
;
425 struct bfin_spi_slave_data
*chip
= drv_data
->cur_chip
;
426 struct spi_message
*msg
= drv_data
->cur_msg
;
427 int n_bytes
= drv_data
->n_bytes
;
430 /* wait until transfer finished. */
431 while (!(read_STAT(drv_data
) & BIT_STAT_RXS
))
434 if ((drv_data
->tx
&& drv_data
->tx
>= drv_data
->tx_end
) ||
435 (drv_data
->rx
&& drv_data
->rx
>= (drv_data
->rx_end
- n_bytes
))) {
438 dev_dbg(&drv_data
->pdev
->dev
, "last read\n");
440 u16
*buf
= (u16
*)drv_data
->rx
;
441 for (loop
= 0; loop
< n_bytes
/ 2; loop
++)
442 *buf
++ = read_RDBR(drv_data
);
444 u8
*buf
= (u8
*)drv_data
->rx
;
445 for (loop
= 0; loop
< n_bytes
; loop
++)
446 *buf
++ = read_RDBR(drv_data
);
448 drv_data
->rx
+= n_bytes
;
451 msg
->actual_length
+= drv_data
->len_in_bytes
;
452 if (drv_data
->cs_change
)
453 bfin_spi_cs_deactive(drv_data
, chip
);
454 /* Move to next transfer */
455 msg
->state
= bfin_spi_next_transfer(drv_data
);
457 disable_irq_nosync(drv_data
->spi_irq
);
459 /* Schedule transfer tasklet */
460 tasklet_schedule(&drv_data
->pump_transfers
);
464 if (drv_data
->rx
&& drv_data
->tx
) {
466 dev_dbg(&drv_data
->pdev
->dev
, "duplex: write_TDBR\n");
468 u16
*buf
= (u16
*)drv_data
->rx
;
469 u16
*buf2
= (u16
*)drv_data
->tx
;
470 for (loop
= 0; loop
< n_bytes
/ 2; loop
++) {
471 *buf
++ = read_RDBR(drv_data
);
472 write_TDBR(drv_data
, *buf2
++);
475 u8
*buf
= (u8
*)drv_data
->rx
;
476 u8
*buf2
= (u8
*)drv_data
->tx
;
477 for (loop
= 0; loop
< n_bytes
; loop
++) {
478 *buf
++ = read_RDBR(drv_data
);
479 write_TDBR(drv_data
, *buf2
++);
482 } else if (drv_data
->rx
) {
484 dev_dbg(&drv_data
->pdev
->dev
, "read: write_TDBR\n");
486 u16
*buf
= (u16
*)drv_data
->rx
;
487 for (loop
= 0; loop
< n_bytes
/ 2; loop
++) {
488 *buf
++ = read_RDBR(drv_data
);
489 write_TDBR(drv_data
, chip
->idle_tx_val
);
492 u8
*buf
= (u8
*)drv_data
->rx
;
493 for (loop
= 0; loop
< n_bytes
; loop
++) {
494 *buf
++ = read_RDBR(drv_data
);
495 write_TDBR(drv_data
, chip
->idle_tx_val
);
498 } else if (drv_data
->tx
) {
500 dev_dbg(&drv_data
->pdev
->dev
, "write: write_TDBR\n");
502 u16
*buf
= (u16
*)drv_data
->tx
;
503 for (loop
= 0; loop
< n_bytes
/ 2; loop
++) {
505 write_TDBR(drv_data
, *buf
++);
508 u8
*buf
= (u8
*)drv_data
->tx
;
509 for (loop
= 0; loop
< n_bytes
; loop
++) {
511 write_TDBR(drv_data
, *buf
++);
517 drv_data
->tx
+= n_bytes
;
519 drv_data
->rx
+= n_bytes
;
524 static irqreturn_t
bfin_spi_dma_irq_handler(int irq
, void *dev_id
)
526 struct bfin_spi_master_data
*drv_data
= dev_id
;
527 struct bfin_spi_slave_data
*chip
= drv_data
->cur_chip
;
528 struct spi_message
*msg
= drv_data
->cur_msg
;
529 unsigned long timeout
;
530 unsigned short dmastat
= get_dma_curr_irqstat(drv_data
->dma_channel
);
531 u16 spistat
= read_STAT(drv_data
);
533 dev_dbg(&drv_data
->pdev
->dev
,
534 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
537 if (drv_data
->rx
!= NULL
) {
538 u16 cr
= read_CTRL(drv_data
);
539 /* discard old RX data and clear RXS */
540 bfin_spi_dummy_read(drv_data
);
541 write_CTRL(drv_data
, cr
& ~BIT_CTL_ENABLE
); /* Disable SPI */
542 write_CTRL(drv_data
, cr
& ~BIT_CTL_TIMOD
); /* Restore State */
543 write_STAT(drv_data
, BIT_STAT_CLR
); /* Clear Status */
546 clear_dma_irqstat(drv_data
->dma_channel
);
549 * wait for the last transaction shifted out. HRM states:
550 * at this point there may still be data in the SPI DMA FIFO waiting
551 * to be transmitted ... software needs to poll TXS in the SPI_STAT
552 * register until it goes low for 2 successive reads
554 if (drv_data
->tx
!= NULL
) {
555 while ((read_STAT(drv_data
) & BIT_STAT_TXS
) ||
556 (read_STAT(drv_data
) & BIT_STAT_TXS
))
560 dev_dbg(&drv_data
->pdev
->dev
,
561 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
562 dmastat
, read_STAT(drv_data
));
564 timeout
= jiffies
+ HZ
;
565 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
566 if (!time_before(jiffies
, timeout
)) {
567 dev_warn(&drv_data
->pdev
->dev
, "timeout waiting for SPIF");
572 if ((dmastat
& DMA_ERR
) && (spistat
& BIT_STAT_RBSY
)) {
573 msg
->state
= ERROR_STATE
;
574 dev_err(&drv_data
->pdev
->dev
, "dma receive: fifo/buffer overflow\n");
576 msg
->actual_length
+= drv_data
->len_in_bytes
;
578 if (drv_data
->cs_change
)
579 bfin_spi_cs_deactive(drv_data
, chip
);
581 /* Move to next transfer */
582 msg
->state
= bfin_spi_next_transfer(drv_data
);
585 /* Schedule transfer tasklet */
586 tasklet_schedule(&drv_data
->pump_transfers
);
588 /* free the irq handler before next transfer */
589 dev_dbg(&drv_data
->pdev
->dev
,
590 "disable dma channel irq%d\n",
591 drv_data
->dma_channel
);
592 dma_disable_irq_nosync(drv_data
->dma_channel
);
597 static void bfin_spi_pump_transfers(unsigned long data
)
599 struct bfin_spi_master_data
*drv_data
= (struct bfin_spi_master_data
*)data
;
600 struct spi_message
*message
= NULL
;
601 struct spi_transfer
*transfer
= NULL
;
602 struct spi_transfer
*previous
= NULL
;
603 struct bfin_spi_slave_data
*chip
= NULL
;
604 unsigned int bits_per_word
;
605 u16 cr
, cr_width
, dma_width
, dma_config
;
606 u32 tranf_success
= 1;
609 /* Get current state information */
610 message
= drv_data
->cur_msg
;
611 transfer
= drv_data
->cur_transfer
;
612 chip
= drv_data
->cur_chip
;
615 * if msg is error or done, report it back using complete() callback
618 /* Handle for abort */
619 if (message
->state
== ERROR_STATE
) {
620 dev_dbg(&drv_data
->pdev
->dev
, "transfer: we've hit an error\n");
621 message
->status
= -EIO
;
622 bfin_spi_giveback(drv_data
);
626 /* Handle end of message */
627 if (message
->state
== DONE_STATE
) {
628 dev_dbg(&drv_data
->pdev
->dev
, "transfer: all done!\n");
630 bfin_spi_giveback(drv_data
);
634 /* Delay if requested at end of transfer */
635 if (message
->state
== RUNNING_STATE
) {
636 dev_dbg(&drv_data
->pdev
->dev
, "transfer: still running ...\n");
637 previous
= list_entry(transfer
->transfer_list
.prev
,
638 struct spi_transfer
, transfer_list
);
639 if (previous
->delay_usecs
)
640 udelay(previous
->delay_usecs
);
643 /* Flush any existing transfers that may be sitting in the hardware */
644 if (bfin_spi_flush(drv_data
) == 0) {
645 dev_err(&drv_data
->pdev
->dev
, "pump_transfers: flush failed\n");
646 message
->status
= -EIO
;
647 bfin_spi_giveback(drv_data
);
651 if (transfer
->len
== 0) {
652 /* Move to next transfer of this msg */
653 message
->state
= bfin_spi_next_transfer(drv_data
);
654 /* Schedule next transfer tasklet */
655 tasklet_schedule(&drv_data
->pump_transfers
);
659 if (transfer
->tx_buf
!= NULL
) {
660 drv_data
->tx
= (void *)transfer
->tx_buf
;
661 drv_data
->tx_end
= drv_data
->tx
+ transfer
->len
;
662 dev_dbg(&drv_data
->pdev
->dev
, "tx_buf is %p, tx_end is %p\n",
663 transfer
->tx_buf
, drv_data
->tx_end
);
668 if (transfer
->rx_buf
!= NULL
) {
669 full_duplex
= transfer
->tx_buf
!= NULL
;
670 drv_data
->rx
= transfer
->rx_buf
;
671 drv_data
->rx_end
= drv_data
->rx
+ transfer
->len
;
672 dev_dbg(&drv_data
->pdev
->dev
, "rx_buf is %p, rx_end is %p\n",
673 transfer
->rx_buf
, drv_data
->rx_end
);
678 drv_data
->rx_dma
= transfer
->rx_dma
;
679 drv_data
->tx_dma
= transfer
->tx_dma
;
680 drv_data
->len_in_bytes
= transfer
->len
;
681 drv_data
->cs_change
= transfer
->cs_change
;
683 /* Bits per word setup */
684 bits_per_word
= transfer
->bits_per_word
? : message
->spi
->bits_per_word
;
685 if ((bits_per_word
> 0) && (bits_per_word
% 16 == 0)) {
686 drv_data
->n_bytes
= bits_per_word
/8;
687 drv_data
->len
= (transfer
->len
) >> 1;
688 cr_width
= BIT_CTL_WORDSIZE
;
689 drv_data
->ops
= &bfin_bfin_spi_transfer_ops_u16
;
690 } else if ((bits_per_word
> 0) && (bits_per_word
% 8 == 0)) {
691 drv_data
->n_bytes
= bits_per_word
/8;
692 drv_data
->len
= transfer
->len
;
694 drv_data
->ops
= &bfin_bfin_spi_transfer_ops_u8
;
696 dev_err(&drv_data
->pdev
->dev
, "transfer: unsupported bits_per_word\n");
697 message
->status
= -EINVAL
;
698 bfin_spi_giveback(drv_data
);
701 cr
= read_CTRL(drv_data
) & ~(BIT_CTL_TIMOD
| BIT_CTL_WORDSIZE
);
703 write_CTRL(drv_data
, cr
);
705 dev_dbg(&drv_data
->pdev
->dev
,
706 "transfer: drv_data->ops is %p, chip->ops is %p, u8_ops is %p\n",
707 drv_data
->ops
, chip
->ops
, &bfin_bfin_spi_transfer_ops_u8
);
709 message
->state
= RUNNING_STATE
;
712 /* Speed setup (surely valid because already checked) */
713 if (transfer
->speed_hz
)
714 write_BAUD(drv_data
, hz_to_spi_baud(transfer
->speed_hz
));
716 write_BAUD(drv_data
, chip
->baud
);
718 write_STAT(drv_data
, BIT_STAT_CLR
);
719 bfin_spi_cs_active(drv_data
, chip
);
721 dev_dbg(&drv_data
->pdev
->dev
,
722 "now pumping a transfer: width is %d, len is %d\n",
723 cr_width
, transfer
->len
);
726 * Try to map dma buffer and do a dma transfer. If successful use,
727 * different way to r/w according to the enable_dma settings and if
728 * we are not doing a full duplex transfer (since the hardware does
729 * not support full duplex DMA transfers).
731 if (!full_duplex
&& drv_data
->cur_chip
->enable_dma
732 && drv_data
->len
> 6) {
734 unsigned long dma_start_addr
, flags
;
736 disable_dma(drv_data
->dma_channel
);
737 clear_dma_irqstat(drv_data
->dma_channel
);
739 /* config dma channel */
740 dev_dbg(&drv_data
->pdev
->dev
, "doing dma transfer\n");
741 set_dma_x_count(drv_data
->dma_channel
, drv_data
->len
);
742 if (cr_width
== BIT_CTL_WORDSIZE
) {
743 set_dma_x_modify(drv_data
->dma_channel
, 2);
744 dma_width
= WDSIZE_16
;
746 set_dma_x_modify(drv_data
->dma_channel
, 1);
747 dma_width
= WDSIZE_8
;
750 /* poll for SPI completion before start */
751 while (!(read_STAT(drv_data
) & BIT_STAT_SPIF
))
754 /* dirty hack for autobuffer DMA mode */
755 if (drv_data
->tx_dma
== 0xFFFF) {
756 dev_dbg(&drv_data
->pdev
->dev
,
757 "doing autobuffer DMA out.\n");
759 /* no irq in autobuffer mode */
761 (DMAFLOW_AUTO
| RESTART
| dma_width
| DI_EN
);
762 set_dma_config(drv_data
->dma_channel
, dma_config
);
763 set_dma_start_addr(drv_data
->dma_channel
,
764 (unsigned long)drv_data
->tx
);
765 enable_dma(drv_data
->dma_channel
);
767 /* start SPI transfer */
768 write_CTRL(drv_data
, cr
| BIT_CTL_TIMOD_DMA_TX
);
770 /* just return here, there can only be one transfer
774 bfin_spi_giveback(drv_data
);
778 /* In dma mode, rx or tx must be NULL in one transfer */
779 dma_config
= (RESTART
| dma_width
| DI_EN
);
780 if (drv_data
->rx
!= NULL
) {
781 /* set transfer mode, and enable SPI */
782 dev_dbg(&drv_data
->pdev
->dev
, "doing DMA in to %p (size %zx)\n",
783 drv_data
->rx
, drv_data
->len_in_bytes
);
785 /* invalidate caches, if needed */
786 if (bfin_addr_dcacheable((unsigned long) drv_data
->rx
))
787 invalidate_dcache_range((unsigned long) drv_data
->rx
,
788 (unsigned long) (drv_data
->rx
+
789 drv_data
->len_in_bytes
));
792 dma_start_addr
= (unsigned long)drv_data
->rx
;
793 cr
|= BIT_CTL_TIMOD_DMA_RX
| BIT_CTL_SENDOPT
;
795 } else if (drv_data
->tx
!= NULL
) {
796 dev_dbg(&drv_data
->pdev
->dev
, "doing DMA out.\n");
798 /* flush caches, if needed */
799 if (bfin_addr_dcacheable((unsigned long) drv_data
->tx
))
800 flush_dcache_range((unsigned long) drv_data
->tx
,
801 (unsigned long) (drv_data
->tx
+
802 drv_data
->len_in_bytes
));
804 dma_start_addr
= (unsigned long)drv_data
->tx
;
805 cr
|= BIT_CTL_TIMOD_DMA_TX
;
810 /* oh man, here there be monsters ... and i dont mean the
811 * fluffy cute ones from pixar, i mean the kind that'll eat
812 * your data, kick your dog, and love it all. do *not* try
813 * and change these lines unless you (1) heavily test DMA
814 * with SPI flashes on a loaded system (e.g. ping floods),
815 * (2) know just how broken the DMA engine interaction with
816 * the SPI peripheral is, and (3) have someone else to blame
817 * when you screw it all up anyways.
819 set_dma_start_addr(drv_data
->dma_channel
, dma_start_addr
);
820 set_dma_config(drv_data
->dma_channel
, dma_config
);
821 local_irq_save(flags
);
823 write_CTRL(drv_data
, cr
);
824 enable_dma(drv_data
->dma_channel
);
825 dma_enable_irq(drv_data
->dma_channel
);
826 local_irq_restore(flags
);
832 * We always use SPI_WRITE mode (transfer starts with TDBR write).
833 * SPI_READ mode (transfer starts with RDBR read) seems to have
834 * problems with setting up the output value in TDBR prior to the
835 * start of the transfer.
837 write_CTRL(drv_data
, cr
| BIT_CTL_TXMOD
);
839 if (chip
->pio_interrupt
) {
840 /* SPI irq should have been disabled by now */
842 /* discard old RX data and clear RXS */
843 bfin_spi_dummy_read(drv_data
);
846 if (drv_data
->tx
== NULL
)
847 write_TDBR(drv_data
, chip
->idle_tx_val
);
850 if (bits_per_word
% 16 == 0) {
851 u16
*buf
= (u16
*)drv_data
->tx
;
852 for (loop
= 0; loop
< bits_per_word
/ 16;
854 write_TDBR(drv_data
, *buf
++);
856 } else if (bits_per_word
% 8 == 0) {
857 u8
*buf
= (u8
*)drv_data
->tx
;
858 for (loop
= 0; loop
< bits_per_word
/ 8; loop
++)
859 write_TDBR(drv_data
, *buf
++);
862 drv_data
->tx
+= drv_data
->n_bytes
;
865 /* once TDBR is empty, interrupt is triggered */
866 enable_irq(drv_data
->spi_irq
);
871 dev_dbg(&drv_data
->pdev
->dev
, "doing IO transfer\n");
874 /* full duplex mode */
875 BUG_ON((drv_data
->tx_end
- drv_data
->tx
) !=
876 (drv_data
->rx_end
- drv_data
->rx
));
877 dev_dbg(&drv_data
->pdev
->dev
,
878 "IO duplex: cr is 0x%x\n", cr
);
880 drv_data
->ops
->duplex(drv_data
);
882 if (drv_data
->tx
!= drv_data
->tx_end
)
884 } else if (drv_data
->tx
!= NULL
) {
885 /* write only half duplex */
886 dev_dbg(&drv_data
->pdev
->dev
,
887 "IO write: cr is 0x%x\n", cr
);
889 drv_data
->ops
->write(drv_data
);
891 if (drv_data
->tx
!= drv_data
->tx_end
)
893 } else if (drv_data
->rx
!= NULL
) {
894 /* read only half duplex */
895 dev_dbg(&drv_data
->pdev
->dev
,
896 "IO read: cr is 0x%x\n", cr
);
898 drv_data
->ops
->read(drv_data
);
899 if (drv_data
->rx
!= drv_data
->rx_end
)
903 if (!tranf_success
) {
904 dev_dbg(&drv_data
->pdev
->dev
,
905 "IO write error!\n");
906 message
->state
= ERROR_STATE
;
908 /* Update total byte transfered */
909 message
->actual_length
+= drv_data
->len_in_bytes
;
910 /* Move to next transfer of this msg */
911 message
->state
= bfin_spi_next_transfer(drv_data
);
912 if (drv_data
->cs_change
)
913 bfin_spi_cs_deactive(drv_data
, chip
);
916 /* Schedule next transfer tasklet */
917 tasklet_schedule(&drv_data
->pump_transfers
);
920 /* pop a msg from queue and kick off real transfer */
921 static void bfin_spi_pump_messages(struct work_struct
*work
)
923 struct bfin_spi_master_data
*drv_data
;
926 drv_data
= container_of(work
, struct bfin_spi_master_data
, pump_messages
);
928 /* Lock queue and check for queue work */
929 spin_lock_irqsave(&drv_data
->lock
, flags
);
930 if (list_empty(&drv_data
->queue
) || !drv_data
->running
) {
931 /* pumper kicked off but no work to do */
933 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
937 /* Make sure we are not already running a message */
938 if (drv_data
->cur_msg
) {
939 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
943 /* Extract head of queue */
944 drv_data
->cur_msg
= list_entry(drv_data
->queue
.next
,
945 struct spi_message
, queue
);
947 /* Setup the SSP using the per chip configuration */
948 drv_data
->cur_chip
= spi_get_ctldata(drv_data
->cur_msg
->spi
);
949 bfin_spi_restore_state(drv_data
);
951 list_del_init(&drv_data
->cur_msg
->queue
);
953 /* Initial message state */
954 drv_data
->cur_msg
->state
= START_STATE
;
955 drv_data
->cur_transfer
= list_entry(drv_data
->cur_msg
->transfers
.next
,
956 struct spi_transfer
, transfer_list
);
958 dev_dbg(&drv_data
->pdev
->dev
, "got a message to pump, "
959 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
960 drv_data
->cur_chip
->baud
, drv_data
->cur_chip
->flag
,
961 drv_data
->cur_chip
->ctl_reg
);
963 dev_dbg(&drv_data
->pdev
->dev
,
964 "the first transfer len is %d\n",
965 drv_data
->cur_transfer
->len
);
967 /* Mark as busy and launch transfers */
968 tasklet_schedule(&drv_data
->pump_transfers
);
971 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
975 * got a msg to transfer, queue it in drv_data->queue.
976 * And kick off message pumper
978 static int bfin_spi_transfer(struct spi_device
*spi
, struct spi_message
*msg
)
980 struct bfin_spi_master_data
*drv_data
= spi_master_get_devdata(spi
->master
);
983 spin_lock_irqsave(&drv_data
->lock
, flags
);
985 if (!drv_data
->running
) {
986 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
990 msg
->actual_length
= 0;
991 msg
->status
= -EINPROGRESS
;
992 msg
->state
= START_STATE
;
994 dev_dbg(&spi
->dev
, "adding an msg in transfer() \n");
995 list_add_tail(&msg
->queue
, &drv_data
->queue
);
997 if (drv_data
->running
&& !drv_data
->busy
)
998 queue_work(drv_data
->workqueue
, &drv_data
->pump_messages
);
1000 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1005 #define MAX_SPI_SSEL 7
1007 static u16 ssel
[][MAX_SPI_SSEL
] = {
1008 {P_SPI0_SSEL1
, P_SPI0_SSEL2
, P_SPI0_SSEL3
,
1009 P_SPI0_SSEL4
, P_SPI0_SSEL5
,
1010 P_SPI0_SSEL6
, P_SPI0_SSEL7
},
1012 {P_SPI1_SSEL1
, P_SPI1_SSEL2
, P_SPI1_SSEL3
,
1013 P_SPI1_SSEL4
, P_SPI1_SSEL5
,
1014 P_SPI1_SSEL6
, P_SPI1_SSEL7
},
1016 {P_SPI2_SSEL1
, P_SPI2_SSEL2
, P_SPI2_SSEL3
,
1017 P_SPI2_SSEL4
, P_SPI2_SSEL5
,
1018 P_SPI2_SSEL6
, P_SPI2_SSEL7
},
1021 /* setup for devices (may be called multiple times -- not just first setup) */
1022 static int bfin_spi_setup(struct spi_device
*spi
)
1024 struct bfin5xx_spi_chip
*chip_info
;
1025 struct bfin_spi_slave_data
*chip
= NULL
;
1026 struct bfin_spi_master_data
*drv_data
= spi_master_get_devdata(spi
->master
);
1030 /* Only alloc (or use chip_info) on first setup */
1032 chip
= spi_get_ctldata(spi
);
1034 chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
1036 dev_err(&spi
->dev
, "cannot allocate chip data\n");
1041 chip
->enable_dma
= 0;
1042 chip_info
= spi
->controller_data
;
1045 /* Let people set non-standard bits directly */
1046 bfin_ctl_reg
= BIT_CTL_OPENDRAIN
| BIT_CTL_EMISO
|
1047 BIT_CTL_PSSE
| BIT_CTL_GM
| BIT_CTL_SZ
;
1049 /* chip_info isn't always needed */
1051 /* Make sure people stop trying to set fields via ctl_reg
1052 * when they should actually be using common SPI framework.
1053 * Currently we let through: WOM EMISO PSSE GM SZ.
1054 * Not sure if a user actually needs/uses any of these,
1055 * but let's assume (for now) they do.
1057 if (chip_info
->ctl_reg
& ~bfin_ctl_reg
) {
1058 dev_err(&spi
->dev
, "do not set bits in ctl_reg "
1059 "that the SPI framework manages\n");
1062 chip
->enable_dma
= chip_info
->enable_dma
!= 0
1063 && drv_data
->master_info
->enable_dma
;
1064 chip
->ctl_reg
= chip_info
->ctl_reg
;
1065 chip
->cs_chg_udelay
= chip_info
->cs_chg_udelay
;
1066 chip
->idle_tx_val
= chip_info
->idle_tx_val
;
1067 chip
->pio_interrupt
= chip_info
->pio_interrupt
;
1068 spi
->bits_per_word
= chip_info
->bits_per_word
;
1070 /* force a default base state */
1071 chip
->ctl_reg
&= bfin_ctl_reg
;
1074 if (spi
->bits_per_word
% 8) {
1075 dev_err(&spi
->dev
, "%d bits_per_word is not supported\n",
1076 spi
->bits_per_word
);
1080 /* translate common spi framework into our register */
1081 if (spi
->mode
& ~(SPI_CPOL
| SPI_CPHA
| SPI_LSB_FIRST
)) {
1082 dev_err(&spi
->dev
, "unsupported spi modes detected\n");
1085 if (spi
->mode
& SPI_CPOL
)
1086 chip
->ctl_reg
|= BIT_CTL_CPOL
;
1087 if (spi
->mode
& SPI_CPHA
)
1088 chip
->ctl_reg
|= BIT_CTL_CPHA
;
1089 if (spi
->mode
& SPI_LSB_FIRST
)
1090 chip
->ctl_reg
|= BIT_CTL_LSBF
;
1091 /* we dont support running in slave mode (yet?) */
1092 chip
->ctl_reg
|= BIT_CTL_MASTER
;
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 chip
->chip_select_num
= spi
->chip_select
;
1100 if (chip
->chip_select_num
< MAX_CTRL_CS
) {
1101 if (!(spi
->mode
& SPI_CPHA
))
1102 dev_warn(&spi
->dev
, "Warning: SPI CPHA not set:"
1103 " Slave Select not under software control!\n"
1104 " See Documentation/blackfin/bfin-spi-notes.txt");
1106 chip
->flag
= (1 << spi
->chip_select
) << 8;
1108 chip
->cs_gpio
= chip
->chip_select_num
- MAX_CTRL_CS
;
1110 if (chip
->enable_dma
&& chip
->pio_interrupt
) {
1111 dev_err(&spi
->dev
, "enable_dma is set, "
1112 "do not set pio_interrupt\n");
1116 * if any one SPI chip is registered and wants DMA, request the
1117 * DMA channel for it
1119 if (chip
->enable_dma
&& !drv_data
->dma_requested
) {
1120 /* register dma irq handler */
1121 ret
= request_dma(drv_data
->dma_channel
, "BFIN_SPI_DMA");
1124 "Unable to request BlackFin SPI DMA channel\n");
1127 drv_data
->dma_requested
= 1;
1129 ret
= set_dma_callback(drv_data
->dma_channel
,
1130 bfin_spi_dma_irq_handler
, drv_data
);
1132 dev_err(&spi
->dev
, "Unable to set dma callback\n");
1135 dma_disable_irq(drv_data
->dma_channel
);
1138 if (chip
->pio_interrupt
&& !drv_data
->irq_requested
) {
1139 ret
= request_irq(drv_data
->spi_irq
, bfin_spi_pio_irq_handler
,
1140 IRQF_DISABLED
, "BFIN_SPI", drv_data
);
1142 dev_err(&spi
->dev
, "Unable to register spi IRQ\n");
1145 drv_data
->irq_requested
= 1;
1146 /* we use write mode, spi irq has to be disabled here */
1147 disable_irq(drv_data
->spi_irq
);
1150 if (chip
->chip_select_num
>= MAX_CTRL_CS
) {
1151 /* Only request on first setup */
1152 if (spi_get_ctldata(spi
) == NULL
) {
1153 ret
= gpio_request(chip
->cs_gpio
, spi
->modalias
);
1155 dev_err(&spi
->dev
, "gpio_request() error\n");
1158 gpio_direction_output(chip
->cs_gpio
, 1);
1162 dev_dbg(&spi
->dev
, "setup spi chip %s, width is %d, dma is %d\n",
1163 spi
->modalias
, spi
->bits_per_word
, chip
->enable_dma
);
1164 dev_dbg(&spi
->dev
, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
1165 chip
->ctl_reg
, chip
->flag
);
1167 spi_set_ctldata(spi
, chip
);
1169 dev_dbg(&spi
->dev
, "chip select number is %d\n", chip
->chip_select_num
);
1170 if (chip
->chip_select_num
< MAX_CTRL_CS
) {
1171 ret
= peripheral_request(ssel
[spi
->master
->bus_num
]
1172 [chip
->chip_select_num
-1], spi
->modalias
);
1174 dev_err(&spi
->dev
, "peripheral_request() error\n");
1179 bfin_spi_cs_enable(drv_data
, chip
);
1180 bfin_spi_cs_deactive(drv_data
, chip
);
1185 if (chip
->chip_select_num
>= MAX_CTRL_CS
)
1186 gpio_free(chip
->cs_gpio
);
1188 peripheral_free(ssel
[spi
->master
->bus_num
]
1189 [chip
->chip_select_num
- 1]);
1192 if (drv_data
->dma_requested
)
1193 free_dma(drv_data
->dma_channel
);
1194 drv_data
->dma_requested
= 0;
1197 /* prevent free 'chip' twice */
1198 spi_set_ctldata(spi
, NULL
);
1205 * callback for spi framework.
1206 * clean driver specific data
1208 static void bfin_spi_cleanup(struct spi_device
*spi
)
1210 struct bfin_spi_slave_data
*chip
= spi_get_ctldata(spi
);
1211 struct bfin_spi_master_data
*drv_data
= spi_master_get_devdata(spi
->master
);
1216 if (chip
->chip_select_num
< MAX_CTRL_CS
) {
1217 peripheral_free(ssel
[spi
->master
->bus_num
]
1218 [chip
->chip_select_num
-1]);
1219 bfin_spi_cs_disable(drv_data
, chip
);
1221 gpio_free(chip
->cs_gpio
);
1224 /* prevent free 'chip' twice */
1225 spi_set_ctldata(spi
, NULL
);
1228 static inline int bfin_spi_init_queue(struct bfin_spi_master_data
*drv_data
)
1230 INIT_LIST_HEAD(&drv_data
->queue
);
1231 spin_lock_init(&drv_data
->lock
);
1233 drv_data
->running
= false;
1236 /* init transfer tasklet */
1237 tasklet_init(&drv_data
->pump_transfers
,
1238 bfin_spi_pump_transfers
, (unsigned long)drv_data
);
1240 /* init messages workqueue */
1241 INIT_WORK(&drv_data
->pump_messages
, bfin_spi_pump_messages
);
1242 drv_data
->workqueue
= create_singlethread_workqueue(
1243 dev_name(drv_data
->master
->dev
.parent
));
1244 if (drv_data
->workqueue
== NULL
)
1250 static inline int bfin_spi_start_queue(struct bfin_spi_master_data
*drv_data
)
1252 unsigned long flags
;
1254 spin_lock_irqsave(&drv_data
->lock
, flags
);
1256 if (drv_data
->running
|| drv_data
->busy
) {
1257 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1261 drv_data
->running
= true;
1262 drv_data
->cur_msg
= NULL
;
1263 drv_data
->cur_transfer
= NULL
;
1264 drv_data
->cur_chip
= NULL
;
1265 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1267 queue_work(drv_data
->workqueue
, &drv_data
->pump_messages
);
1272 static inline int bfin_spi_stop_queue(struct bfin_spi_master_data
*drv_data
)
1274 unsigned long flags
;
1275 unsigned limit
= 500;
1278 spin_lock_irqsave(&drv_data
->lock
, flags
);
1281 * This is a bit lame, but is optimized for the common execution path.
1282 * A wait_queue on the drv_data->busy could be used, but then the common
1283 * execution path (pump_messages) would be required to call wake_up or
1284 * friends on every SPI message. Do this instead
1286 drv_data
->running
= false;
1287 while (!list_empty(&drv_data
->queue
) && drv_data
->busy
&& limit
--) {
1288 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1290 spin_lock_irqsave(&drv_data
->lock
, flags
);
1293 if (!list_empty(&drv_data
->queue
) || drv_data
->busy
)
1296 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
1301 static inline int bfin_spi_destroy_queue(struct bfin_spi_master_data
*drv_data
)
1305 status
= bfin_spi_stop_queue(drv_data
);
1309 destroy_workqueue(drv_data
->workqueue
);
1314 static int __init
bfin_spi_probe(struct platform_device
*pdev
)
1316 struct device
*dev
= &pdev
->dev
;
1317 struct bfin5xx_spi_master
*platform_info
;
1318 struct spi_master
*master
;
1319 struct bfin_spi_master_data
*drv_data
;
1320 struct resource
*res
;
1323 platform_info
= dev
->platform_data
;
1325 /* Allocate master with space for drv_data */
1326 master
= spi_alloc_master(dev
, sizeof(*drv_data
));
1328 dev_err(&pdev
->dev
, "can not alloc spi_master\n");
1332 drv_data
= spi_master_get_devdata(master
);
1333 drv_data
->master
= master
;
1334 drv_data
->master_info
= platform_info
;
1335 drv_data
->pdev
= pdev
;
1336 drv_data
->pin_req
= platform_info
->pin_req
;
1338 /* the spi->mode bits supported by this driver: */
1339 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_LSB_FIRST
;
1341 master
->bus_num
= pdev
->id
;
1342 master
->num_chipselect
= platform_info
->num_chipselect
;
1343 master
->cleanup
= bfin_spi_cleanup
;
1344 master
->setup
= bfin_spi_setup
;
1345 master
->transfer
= bfin_spi_transfer
;
1347 /* Find and map our resources */
1348 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1350 dev_err(dev
, "Cannot get IORESOURCE_MEM\n");
1352 goto out_error_get_res
;
1355 drv_data
->regs_base
= ioremap(res
->start
, resource_size(res
));
1356 if (drv_data
->regs_base
== NULL
) {
1357 dev_err(dev
, "Cannot map IO\n");
1359 goto out_error_ioremap
;
1362 res
= platform_get_resource(pdev
, IORESOURCE_DMA
, 0);
1364 dev_err(dev
, "No DMA channel specified\n");
1366 goto out_error_free_io
;
1368 drv_data
->dma_channel
= res
->start
;
1370 drv_data
->spi_irq
= platform_get_irq(pdev
, 0);
1371 if (drv_data
->spi_irq
< 0) {
1372 dev_err(dev
, "No spi pio irq specified\n");
1374 goto out_error_free_io
;
1377 /* Initial and start queue */
1378 status
= bfin_spi_init_queue(drv_data
);
1380 dev_err(dev
, "problem initializing queue\n");
1381 goto out_error_queue_alloc
;
1384 status
= bfin_spi_start_queue(drv_data
);
1386 dev_err(dev
, "problem starting queue\n");
1387 goto out_error_queue_alloc
;
1390 status
= peripheral_request_list(drv_data
->pin_req
, DRV_NAME
);
1392 dev_err(&pdev
->dev
, ": Requesting Peripherals failed\n");
1393 goto out_error_queue_alloc
;
1396 /* Reset SPI registers. If these registers were used by the boot loader,
1397 * the sky may fall on your head if you enable the dma controller.
1399 write_CTRL(drv_data
, BIT_CTL_CPHA
| BIT_CTL_MASTER
);
1400 write_FLAG(drv_data
, 0xFF00);
1402 /* Register with the SPI framework */
1403 platform_set_drvdata(pdev
, drv_data
);
1404 status
= spi_register_master(master
);
1406 dev_err(dev
, "problem registering spi master\n");
1407 goto out_error_queue_alloc
;
1410 dev_info(dev
, "%s, Version %s, regs_base@%p, dma channel@%d\n",
1411 DRV_DESC
, DRV_VERSION
, drv_data
->regs_base
,
1412 drv_data
->dma_channel
);
1415 out_error_queue_alloc
:
1416 bfin_spi_destroy_queue(drv_data
);
1418 iounmap((void *) drv_data
->regs_base
);
1421 spi_master_put(master
);
1426 /* stop hardware and remove the driver */
1427 static int __devexit
bfin_spi_remove(struct platform_device
*pdev
)
1429 struct bfin_spi_master_data
*drv_data
= platform_get_drvdata(pdev
);
1435 /* Remove the queue */
1436 status
= bfin_spi_destroy_queue(drv_data
);
1440 /* Disable the SSP at the peripheral and SOC level */
1441 bfin_spi_disable(drv_data
);
1444 if (drv_data
->master_info
->enable_dma
) {
1445 if (dma_channel_active(drv_data
->dma_channel
))
1446 free_dma(drv_data
->dma_channel
);
1449 if (drv_data
->irq_requested
) {
1450 free_irq(drv_data
->spi_irq
, drv_data
);
1451 drv_data
->irq_requested
= 0;
1454 /* Disconnect from the SPI framework */
1455 spi_unregister_master(drv_data
->master
);
1457 peripheral_free_list(drv_data
->pin_req
);
1459 /* Prevent double remove */
1460 platform_set_drvdata(pdev
, NULL
);
1466 static int bfin_spi_suspend(struct platform_device
*pdev
, pm_message_t state
)
1468 struct bfin_spi_master_data
*drv_data
= platform_get_drvdata(pdev
);
1471 status
= bfin_spi_stop_queue(drv_data
);
1475 drv_data
->ctrl_reg
= read_CTRL(drv_data
);
1476 drv_data
->flag_reg
= read_FLAG(drv_data
);
1479 * reset SPI_CTL and SPI_FLG registers
1481 write_CTRL(drv_data
, BIT_CTL_CPHA
| BIT_CTL_MASTER
);
1482 write_FLAG(drv_data
, 0xFF00);
1487 static int bfin_spi_resume(struct platform_device
*pdev
)
1489 struct bfin_spi_master_data
*drv_data
= platform_get_drvdata(pdev
);
1492 write_CTRL(drv_data
, drv_data
->ctrl_reg
);
1493 write_FLAG(drv_data
, drv_data
->flag_reg
);
1495 /* Start the queue running */
1496 status
= bfin_spi_start_queue(drv_data
);
1498 dev_err(&pdev
->dev
, "problem starting queue (%d)\n", status
);
1505 #define bfin_spi_suspend NULL
1506 #define bfin_spi_resume NULL
1507 #endif /* CONFIG_PM */
1509 MODULE_ALIAS("platform:bfin-spi");
1510 static struct platform_driver bfin_spi_driver
= {
1513 .owner
= THIS_MODULE
,
1515 .suspend
= bfin_spi_suspend
,
1516 .resume
= bfin_spi_resume
,
1517 .remove
= __devexit_p(bfin_spi_remove
),
1520 static int __init
bfin_spi_init(void)
1522 return platform_driver_probe(&bfin_spi_driver
, bfin_spi_probe
);
1524 subsys_initcall(bfin_spi_init
);
1526 static void __exit
bfin_spi_exit(void)
1528 platform_driver_unregister(&bfin_spi_driver
);
1530 module_exit(bfin_spi_exit
);