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>
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)
48 void (*write) (struct master_data *);
49 void (*read) (struct master_data *);
50 void (*duplex) (struct 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 slave_data *cur_chip;
105 const struct transfer_ops *ops;
115 u8 width; /* 0 or 1 */
117 u8 bits_per_word; /* 8 or 16 */
118 u16 cs_chg_udelay; /* Some devices require > 255usec delay */
121 u8 pio_interrupt; /* use spi data irq */
122 const struct transfer_ops *ops;
125 #define DEFINE_SPI_REG(reg, off) \
126 static inline u16 read_##reg(struct master_data *drv_data) \
127 { return bfin_read16(drv_data->regs_base + off); } \
128 static inline void write_##reg(struct 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 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 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 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 master_data *drv_data, struct 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 master_data *drv_data, struct slave_data *chip)
199 if (likely(chip->chip_select_num < MAX_CTRL_CS)) {
200 u16 flag = read_FLAG(drv_data);
204 write_FLAG(drv_data, flag);
206 gpio_set_value(chip->cs_gpio, 1);
209 /* Move delay here for consistency */
210 if (chip->cs_chg_udelay)
211 udelay(chip->cs_chg_udelay);
214 /* enable or disable the pin muxed by GPIO and SPI CS to work as SPI CS */
215 static inline void bfin_spi_cs_enable(struct master_data *drv_data, struct slave_data *chip)
217 if (chip->chip_select_num < MAX_CTRL_CS) {
218 u16 flag = read_FLAG(drv_data);
220 flag |= (chip->flag >> 8);
222 write_FLAG(drv_data, flag);
226 static inline void bfin_spi_cs_disable(struct master_data *drv_data, struct slave_data *chip)
228 if (chip->chip_select_num < MAX_CTRL_CS) {
229 u16 flag = read_FLAG(drv_data);
231 flag &= ~(chip->flag >> 8);
233 write_FLAG(drv_data, flag);
237 /* stop controller and re-config current chip*/
238 static void bfin_spi_restore_state(struct master_data *drv_data)
240 struct slave_data *chip = drv_data->cur_chip;
242 /* Clear status and disable clock */
243 write_STAT(drv_data, BIT_STAT_CLR);
244 bfin_spi_disable(drv_data);
245 dev_dbg(&drv_data->pdev->dev, "restoring spi ctl state\n");
247 /* Load the registers */
248 write_CTRL(drv_data, chip->ctl_reg);
249 write_BAUD(drv_data, chip->baud);
251 bfin_spi_enable(drv_data);
252 bfin_spi_cs_active(drv_data, chip);
255 /* used to kick off transfer in rx mode and read unwanted RX data */
256 static inline void bfin_spi_dummy_read(struct master_data *drv_data)
258 (void) read_RDBR(drv_data);
261 static void bfin_spi_u8_writer(struct master_data *drv_data)
263 /* clear RXS (we check for RXS inside the loop) */
264 bfin_spi_dummy_read(drv_data);
266 while (drv_data->tx < drv_data->tx_end) {
267 write_TDBR(drv_data, (*(u8 *) (drv_data->tx++)));
268 /* wait until transfer finished.
269 checking SPIF or TXS may not guarantee transfer completion */
270 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
272 /* discard RX data and clear RXS */
273 bfin_spi_dummy_read(drv_data);
277 static void bfin_spi_u8_reader(struct master_data *drv_data)
279 u16 tx_val = drv_data->cur_chip->idle_tx_val;
281 /* discard old RX data and clear RXS */
282 bfin_spi_dummy_read(drv_data);
284 while (drv_data->rx < drv_data->rx_end) {
285 write_TDBR(drv_data, tx_val);
286 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
288 *(u8 *) (drv_data->rx++) = read_RDBR(drv_data);
292 static void bfin_spi_u8_duplex(struct master_data *drv_data)
294 /* discard old RX data and clear RXS */
295 bfin_spi_dummy_read(drv_data);
297 while (drv_data->rx < drv_data->rx_end) {
298 write_TDBR(drv_data, (*(u8 *) (drv_data->tx++)));
299 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
301 *(u8 *) (drv_data->rx++) = read_RDBR(drv_data);
305 static const struct transfer_ops bfin_transfer_ops_u8 = {
306 .write = bfin_spi_u8_writer,
307 .read = bfin_spi_u8_reader,
308 .duplex = bfin_spi_u8_duplex,
311 static void bfin_spi_u16_writer(struct master_data *drv_data)
313 /* clear RXS (we check for RXS inside the loop) */
314 bfin_spi_dummy_read(drv_data);
316 while (drv_data->tx < drv_data->tx_end) {
317 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
319 /* wait until transfer finished.
320 checking SPIF or TXS may not guarantee transfer completion */
321 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
323 /* discard RX data and clear RXS */
324 bfin_spi_dummy_read(drv_data);
328 static void bfin_spi_u16_reader(struct master_data *drv_data)
330 u16 tx_val = drv_data->cur_chip->idle_tx_val;
332 /* discard old RX data and clear RXS */
333 bfin_spi_dummy_read(drv_data);
335 while (drv_data->rx < drv_data->rx_end) {
336 write_TDBR(drv_data, tx_val);
337 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
339 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
344 static void bfin_spi_u16_duplex(struct master_data *drv_data)
346 /* discard old RX data and clear RXS */
347 bfin_spi_dummy_read(drv_data);
349 while (drv_data->rx < drv_data->rx_end) {
350 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
352 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
354 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
359 static const struct transfer_ops bfin_transfer_ops_u16 = {
360 .write = bfin_spi_u16_writer,
361 .read = bfin_spi_u16_reader,
362 .duplex = bfin_spi_u16_duplex,
365 /* test if ther is more transfer to be done */
366 static void *bfin_spi_next_transfer(struct master_data *drv_data)
368 struct spi_message *msg = drv_data->cur_msg;
369 struct spi_transfer *trans = drv_data->cur_transfer;
371 /* Move to next transfer */
372 if (trans->transfer_list.next != &msg->transfers) {
373 drv_data->cur_transfer =
374 list_entry(trans->transfer_list.next,
375 struct spi_transfer, transfer_list);
376 return RUNNING_STATE;
382 * caller already set message->status;
383 * dma and pio irqs are blocked give finished message back
385 static void bfin_spi_giveback(struct master_data *drv_data)
387 struct slave_data *chip = drv_data->cur_chip;
388 struct spi_transfer *last_transfer;
390 struct spi_message *msg;
392 spin_lock_irqsave(&drv_data->lock, flags);
393 msg = drv_data->cur_msg;
394 drv_data->cur_msg = NULL;
395 drv_data->cur_transfer = NULL;
396 drv_data->cur_chip = NULL;
397 queue_work(drv_data->workqueue, &drv_data->pump_messages);
398 spin_unlock_irqrestore(&drv_data->lock, flags);
400 last_transfer = list_entry(msg->transfers.prev,
401 struct spi_transfer, transfer_list);
405 if (!drv_data->cs_change)
406 bfin_spi_cs_deactive(drv_data, chip);
408 /* Not stop spi in autobuffer mode */
409 if (drv_data->tx_dma != 0xFFFF)
410 bfin_spi_disable(drv_data);
413 msg->complete(msg->context);
416 /* spi data irq handler */
417 static irqreturn_t bfin_spi_pio_irq_handler(int irq, void *dev_id)
419 struct master_data *drv_data = dev_id;
420 struct slave_data *chip = drv_data->cur_chip;
421 struct spi_message *msg = drv_data->cur_msg;
422 int n_bytes = drv_data->n_bytes;
424 /* wait until transfer finished. */
425 while (!(read_STAT(drv_data) & BIT_STAT_RXS))
428 if ((drv_data->tx && drv_data->tx >= drv_data->tx_end) ||
429 (drv_data->rx && drv_data->rx >= (drv_data->rx_end - n_bytes))) {
432 dev_dbg(&drv_data->pdev->dev, "last read\n");
434 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
435 else if (n_bytes == 1)
436 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
437 drv_data->rx += n_bytes;
440 msg->actual_length += drv_data->len_in_bytes;
441 if (drv_data->cs_change)
442 bfin_spi_cs_deactive(drv_data, chip);
443 /* Move to next transfer */
444 msg->state = bfin_spi_next_transfer(drv_data);
446 disable_irq(drv_data->spi_irq);
448 /* Schedule transfer tasklet */
449 tasklet_schedule(&drv_data->pump_transfers);
453 if (drv_data->rx && drv_data->tx) {
455 dev_dbg(&drv_data->pdev->dev, "duplex: write_TDBR\n");
456 if (drv_data->n_bytes == 2) {
457 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
458 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
459 } else if (drv_data->n_bytes == 1) {
460 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
461 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
463 } else if (drv_data->rx) {
465 dev_dbg(&drv_data->pdev->dev, "read: write_TDBR\n");
466 if (drv_data->n_bytes == 2)
467 *(u16 *) (drv_data->rx) = read_RDBR(drv_data);
468 else if (drv_data->n_bytes == 1)
469 *(u8 *) (drv_data->rx) = read_RDBR(drv_data);
470 write_TDBR(drv_data, chip->idle_tx_val);
471 } else if (drv_data->tx) {
473 dev_dbg(&drv_data->pdev->dev, "write: write_TDBR\n");
474 bfin_spi_dummy_read(drv_data);
475 if (drv_data->n_bytes == 2)
476 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
477 else if (drv_data->n_bytes == 1)
478 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
482 drv_data->tx += n_bytes;
484 drv_data->rx += n_bytes;
489 static irqreturn_t bfin_spi_dma_irq_handler(int irq, void *dev_id)
491 struct master_data *drv_data = dev_id;
492 struct slave_data *chip = drv_data->cur_chip;
493 struct spi_message *msg = drv_data->cur_msg;
494 unsigned long timeout;
495 unsigned short dmastat = get_dma_curr_irqstat(drv_data->dma_channel);
496 u16 spistat = read_STAT(drv_data);
498 dev_dbg(&drv_data->pdev->dev,
499 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
502 clear_dma_irqstat(drv_data->dma_channel);
505 * wait for the last transaction shifted out. HRM states:
506 * at this point there may still be data in the SPI DMA FIFO waiting
507 * to be transmitted ... software needs to poll TXS in the SPI_STAT
508 * register until it goes low for 2 successive reads
510 if (drv_data->tx != NULL) {
511 while ((read_STAT(drv_data) & BIT_STAT_TXS) ||
512 (read_STAT(drv_data) & BIT_STAT_TXS))
516 dev_dbg(&drv_data->pdev->dev,
517 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
518 dmastat, read_STAT(drv_data));
520 timeout = jiffies + HZ;
521 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
522 if (!time_before(jiffies, timeout)) {
523 dev_warn(&drv_data->pdev->dev, "timeout waiting for SPIF");
528 if ((dmastat & DMA_ERR) && (spistat & BIT_STAT_RBSY)) {
529 msg->state = ERROR_STATE;
530 dev_err(&drv_data->pdev->dev, "dma receive: fifo/buffer overflow\n");
532 msg->actual_length += drv_data->len_in_bytes;
534 if (drv_data->cs_change)
535 bfin_spi_cs_deactive(drv_data, chip);
537 /* Move to next transfer */
538 msg->state = bfin_spi_next_transfer(drv_data);
541 /* Schedule transfer tasklet */
542 tasklet_schedule(&drv_data->pump_transfers);
544 /* free the irq handler before next transfer */
545 dev_dbg(&drv_data->pdev->dev,
546 "disable dma channel irq%d\n",
547 drv_data->dma_channel);
548 dma_disable_irq(drv_data->dma_channel);
553 static void bfin_spi_pump_transfers(unsigned long data)
555 struct master_data *drv_data = (struct master_data *)data;
556 struct spi_message *message = NULL;
557 struct spi_transfer *transfer = NULL;
558 struct spi_transfer *previous = NULL;
559 struct slave_data *chip = NULL;
561 u16 cr, dma_width, dma_config;
562 u32 tranf_success = 1;
565 /* Get current state information */
566 message = drv_data->cur_msg;
567 transfer = drv_data->cur_transfer;
568 chip = drv_data->cur_chip;
571 * if msg is error or done, report it back using complete() callback
574 /* Handle for abort */
575 if (message->state == ERROR_STATE) {
576 dev_dbg(&drv_data->pdev->dev, "transfer: we've hit an error\n");
577 message->status = -EIO;
578 bfin_spi_giveback(drv_data);
582 /* Handle end of message */
583 if (message->state == DONE_STATE) {
584 dev_dbg(&drv_data->pdev->dev, "transfer: all done!\n");
586 bfin_spi_giveback(drv_data);
590 /* Delay if requested at end of transfer */
591 if (message->state == RUNNING_STATE) {
592 dev_dbg(&drv_data->pdev->dev, "transfer: still running ...\n");
593 previous = list_entry(transfer->transfer_list.prev,
594 struct spi_transfer, transfer_list);
595 if (previous->delay_usecs)
596 udelay(previous->delay_usecs);
599 /* Flush any existing transfers that may be sitting in the hardware */
600 if (bfin_spi_flush(drv_data) == 0) {
601 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
602 message->status = -EIO;
603 bfin_spi_giveback(drv_data);
607 if (transfer->len == 0) {
608 /* Move to next transfer of this msg */
609 message->state = bfin_spi_next_transfer(drv_data);
610 /* Schedule next transfer tasklet */
611 tasklet_schedule(&drv_data->pump_transfers);
614 if (transfer->tx_buf != NULL) {
615 drv_data->tx = (void *)transfer->tx_buf;
616 drv_data->tx_end = drv_data->tx + transfer->len;
617 dev_dbg(&drv_data->pdev->dev, "tx_buf is %p, tx_end is %p\n",
618 transfer->tx_buf, drv_data->tx_end);
623 if (transfer->rx_buf != NULL) {
624 full_duplex = transfer->tx_buf != NULL;
625 drv_data->rx = transfer->rx_buf;
626 drv_data->rx_end = drv_data->rx + transfer->len;
627 dev_dbg(&drv_data->pdev->dev, "rx_buf is %p, rx_end is %p\n",
628 transfer->rx_buf, drv_data->rx_end);
633 drv_data->rx_dma = transfer->rx_dma;
634 drv_data->tx_dma = transfer->tx_dma;
635 drv_data->len_in_bytes = transfer->len;
636 drv_data->cs_change = transfer->cs_change;
638 /* Bits per word setup */
639 switch (transfer->bits_per_word) {
641 drv_data->n_bytes = 1;
642 width = CFG_SPI_WORDSIZE8;
643 drv_data->ops = &bfin_transfer_ops_u8;
647 drv_data->n_bytes = 2;
648 width = CFG_SPI_WORDSIZE16;
649 drv_data->ops = &bfin_transfer_ops_u16;
653 /* No change, the same as default setting */
654 transfer->bits_per_word = chip->bits_per_word;
655 drv_data->n_bytes = chip->n_bytes;
657 drv_data->ops = chip->ops;
660 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
662 write_CTRL(drv_data, cr);
664 if (width == CFG_SPI_WORDSIZE16) {
665 drv_data->len = (transfer->len) >> 1;
667 drv_data->len = transfer->len;
669 dev_dbg(&drv_data->pdev->dev,
670 "transfer: drv_data->ops is %p, chip->ops is %p, u8_ops is %p\n",
671 drv_data->ops, chip->ops, &bfin_transfer_ops_u8);
673 message->state = RUNNING_STATE;
676 /* Speed setup (surely valid because already checked) */
677 if (transfer->speed_hz)
678 write_BAUD(drv_data, hz_to_spi_baud(transfer->speed_hz));
680 write_BAUD(drv_data, chip->baud);
682 write_STAT(drv_data, BIT_STAT_CLR);
683 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
684 if (drv_data->cs_change)
685 bfin_spi_cs_active(drv_data, chip);
687 dev_dbg(&drv_data->pdev->dev,
688 "now pumping a transfer: width is %d, len is %d\n",
689 width, transfer->len);
692 * Try to map dma buffer and do a dma transfer. If successful use,
693 * different way to r/w according to the enable_dma settings and if
694 * we are not doing a full duplex transfer (since the hardware does
695 * not support full duplex DMA transfers).
697 if (!full_duplex && drv_data->cur_chip->enable_dma
698 && drv_data->len > 6) {
700 unsigned long dma_start_addr, flags;
702 disable_dma(drv_data->dma_channel);
703 clear_dma_irqstat(drv_data->dma_channel);
705 /* config dma channel */
706 dev_dbg(&drv_data->pdev->dev, "doing dma transfer\n");
707 set_dma_x_count(drv_data->dma_channel, drv_data->len);
708 if (width == CFG_SPI_WORDSIZE16) {
709 set_dma_x_modify(drv_data->dma_channel, 2);
710 dma_width = WDSIZE_16;
712 set_dma_x_modify(drv_data->dma_channel, 1);
713 dma_width = WDSIZE_8;
716 /* poll for SPI completion before start */
717 while (!(read_STAT(drv_data) & BIT_STAT_SPIF))
720 /* dirty hack for autobuffer DMA mode */
721 if (drv_data->tx_dma == 0xFFFF) {
722 dev_dbg(&drv_data->pdev->dev,
723 "doing autobuffer DMA out.\n");
725 /* no irq in autobuffer mode */
727 (DMAFLOW_AUTO | RESTART | dma_width | DI_EN);
728 set_dma_config(drv_data->dma_channel, dma_config);
729 set_dma_start_addr(drv_data->dma_channel,
730 (unsigned long)drv_data->tx);
731 enable_dma(drv_data->dma_channel);
733 /* start SPI transfer */
734 write_CTRL(drv_data, cr | BIT_CTL_TIMOD_DMA_TX);
736 /* just return here, there can only be one transfer
740 bfin_spi_giveback(drv_data);
744 /* In dma mode, rx or tx must be NULL in one transfer */
745 dma_config = (RESTART | dma_width | DI_EN);
746 if (drv_data->rx != NULL) {
747 /* set transfer mode, and enable SPI */
748 dev_dbg(&drv_data->pdev->dev, "doing DMA in to %p (size %zx)\n",
749 drv_data->rx, drv_data->len_in_bytes);
751 /* invalidate caches, if needed */
752 if (bfin_addr_dcacheable((unsigned long) drv_data->rx))
753 invalidate_dcache_range((unsigned long) drv_data->rx,
754 (unsigned long) (drv_data->rx +
755 drv_data->len_in_bytes));
758 dma_start_addr = (unsigned long)drv_data->rx;
759 cr |= BIT_CTL_TIMOD_DMA_RX | BIT_CTL_SENDOPT;
761 } else if (drv_data->tx != NULL) {
762 dev_dbg(&drv_data->pdev->dev, "doing DMA out.\n");
764 /* flush caches, if needed */
765 if (bfin_addr_dcacheable((unsigned long) drv_data->tx))
766 flush_dcache_range((unsigned long) drv_data->tx,
767 (unsigned long) (drv_data->tx +
768 drv_data->len_in_bytes));
770 dma_start_addr = (unsigned long)drv_data->tx;
771 cr |= BIT_CTL_TIMOD_DMA_TX;
776 /* oh man, here there be monsters ... and i dont mean the
777 * fluffy cute ones from pixar, i mean the kind that'll eat
778 * your data, kick your dog, and love it all. do *not* try
779 * and change these lines unless you (1) heavily test DMA
780 * with SPI flashes on a loaded system (e.g. ping floods),
781 * (2) know just how broken the DMA engine interaction with
782 * the SPI peripheral is, and (3) have someone else to blame
783 * when you screw it all up anyways.
785 set_dma_start_addr(drv_data->dma_channel, dma_start_addr);
786 set_dma_config(drv_data->dma_channel, dma_config);
787 local_irq_save(flags);
789 write_CTRL(drv_data, cr);
790 enable_dma(drv_data->dma_channel);
791 dma_enable_irq(drv_data->dma_channel);
792 local_irq_restore(flags);
797 if (chip->pio_interrupt) {
798 /* use write mode. spi irq should have been disabled */
799 cr = (read_CTRL(drv_data) & (~BIT_CTL_TIMOD));
800 write_CTRL(drv_data, (cr | CFG_SPI_WRITE));
802 /* discard old RX data and clear RXS */
803 bfin_spi_dummy_read(drv_data);
806 if (drv_data->tx == NULL)
807 write_TDBR(drv_data, chip->idle_tx_val);
809 if (transfer->bits_per_word == 8)
810 write_TDBR(drv_data, (*(u8 *) (drv_data->tx)));
811 else if (transfer->bits_per_word == 16)
812 write_TDBR(drv_data, (*(u16 *) (drv_data->tx)));
813 drv_data->tx += drv_data->n_bytes;
816 /* once TDBR is empty, interrupt is triggered */
817 enable_irq(drv_data->spi_irq);
822 dev_dbg(&drv_data->pdev->dev, "doing IO transfer\n");
824 /* we always use SPI_WRITE mode. SPI_READ mode
825 seems to have problems with setting up the
826 output value in TDBR prior to the transfer. */
827 write_CTRL(drv_data, (cr | CFG_SPI_WRITE));
830 /* full duplex mode */
831 BUG_ON((drv_data->tx_end - drv_data->tx) !=
832 (drv_data->rx_end - drv_data->rx));
833 dev_dbg(&drv_data->pdev->dev,
834 "IO duplex: cr is 0x%x\n", cr);
836 drv_data->ops->duplex(drv_data);
838 if (drv_data->tx != drv_data->tx_end)
840 } else if (drv_data->tx != NULL) {
841 /* write only half duplex */
842 dev_dbg(&drv_data->pdev->dev,
843 "IO write: cr is 0x%x\n", cr);
845 drv_data->ops->write(drv_data);
847 if (drv_data->tx != drv_data->tx_end)
849 } else if (drv_data->rx != NULL) {
850 /* read only half duplex */
851 dev_dbg(&drv_data->pdev->dev,
852 "IO read: cr is 0x%x\n", cr);
854 drv_data->ops->read(drv_data);
855 if (drv_data->rx != drv_data->rx_end)
859 if (!tranf_success) {
860 dev_dbg(&drv_data->pdev->dev,
861 "IO write error!\n");
862 message->state = ERROR_STATE;
864 /* Update total byte transfered */
865 message->actual_length += drv_data->len_in_bytes;
866 /* Move to next transfer of this msg */
867 message->state = bfin_spi_next_transfer(drv_data);
868 if (drv_data->cs_change)
869 bfin_spi_cs_deactive(drv_data, chip);
872 /* Schedule next transfer tasklet */
873 tasklet_schedule(&drv_data->pump_transfers);
876 /* pop a msg from queue and kick off real transfer */
877 static void bfin_spi_pump_messages(struct work_struct *work)
879 struct master_data *drv_data;
882 drv_data = container_of(work, struct master_data, pump_messages);
884 /* Lock queue and check for queue work */
885 spin_lock_irqsave(&drv_data->lock, flags);
886 if (list_empty(&drv_data->queue) || !drv_data->running) {
887 /* pumper kicked off but no work to do */
889 spin_unlock_irqrestore(&drv_data->lock, flags);
893 /* Make sure we are not already running a message */
894 if (drv_data->cur_msg) {
895 spin_unlock_irqrestore(&drv_data->lock, flags);
899 /* Extract head of queue */
900 drv_data->cur_msg = list_entry(drv_data->queue.next,
901 struct spi_message, queue);
903 /* Setup the SSP using the per chip configuration */
904 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
905 bfin_spi_restore_state(drv_data);
907 list_del_init(&drv_data->cur_msg->queue);
909 /* Initial message state */
910 drv_data->cur_msg->state = START_STATE;
911 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
912 struct spi_transfer, transfer_list);
914 dev_dbg(&drv_data->pdev->dev, "got a message to pump, "
915 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
916 drv_data->cur_chip->baud, drv_data->cur_chip->flag,
917 drv_data->cur_chip->ctl_reg);
919 dev_dbg(&drv_data->pdev->dev,
920 "the first transfer len is %d\n",
921 drv_data->cur_transfer->len);
923 /* Mark as busy and launch transfers */
924 tasklet_schedule(&drv_data->pump_transfers);
927 spin_unlock_irqrestore(&drv_data->lock, flags);
931 * got a msg to transfer, queue it in drv_data->queue.
932 * And kick off message pumper
934 static int bfin_spi_transfer(struct spi_device *spi, struct spi_message *msg)
936 struct master_data *drv_data = spi_master_get_devdata(spi->master);
939 spin_lock_irqsave(&drv_data->lock, flags);
941 if (!drv_data->running) {
942 spin_unlock_irqrestore(&drv_data->lock, flags);
946 msg->actual_length = 0;
947 msg->status = -EINPROGRESS;
948 msg->state = START_STATE;
950 dev_dbg(&spi->dev, "adding an msg in transfer() \n");
951 list_add_tail(&msg->queue, &drv_data->queue);
953 if (drv_data->running && !drv_data->busy)
954 queue_work(drv_data->workqueue, &drv_data->pump_messages);
956 spin_unlock_irqrestore(&drv_data->lock, flags);
961 #define MAX_SPI_SSEL 7
963 static u16 ssel[][MAX_SPI_SSEL] = {
964 {P_SPI0_SSEL1, P_SPI0_SSEL2, P_SPI0_SSEL3,
965 P_SPI0_SSEL4, P_SPI0_SSEL5,
966 P_SPI0_SSEL6, P_SPI0_SSEL7},
968 {P_SPI1_SSEL1, P_SPI1_SSEL2, P_SPI1_SSEL3,
969 P_SPI1_SSEL4, P_SPI1_SSEL5,
970 P_SPI1_SSEL6, P_SPI1_SSEL7},
972 {P_SPI2_SSEL1, P_SPI2_SSEL2, P_SPI2_SSEL3,
973 P_SPI2_SSEL4, P_SPI2_SSEL5,
974 P_SPI2_SSEL6, P_SPI2_SSEL7},
977 /* setup for devices (may be called multiple times -- not just first setup) */
978 static int bfin_spi_setup(struct spi_device *spi)
980 struct bfin5xx_spi_chip *chip_info;
981 struct slave_data *chip = NULL;
982 struct master_data *drv_data = spi_master_get_devdata(spi->master);
985 if (spi->bits_per_word != 8 && spi->bits_per_word != 16)
988 /* Only alloc (or use chip_info) on first setup */
990 chip = spi_get_ctldata(spi);
992 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
994 dev_err(&spi->dev, "cannot allocate chip data\n");
999 chip->enable_dma = 0;
1000 chip_info = spi->controller_data;
1003 /* chip_info isn't always needed */
1005 /* Make sure people stop trying to set fields via ctl_reg
1006 * when they should actually be using common SPI framework.
1007 * Currently we let through: WOM EMISO PSSE GM SZ.
1008 * Not sure if a user actually needs/uses any of these,
1009 * but let's assume (for now) they do.
1011 if (chip_info->ctl_reg & ~(BIT_CTL_OPENDRAIN | BIT_CTL_EMISO | \
1012 BIT_CTL_PSSE | BIT_CTL_GM | BIT_CTL_SZ)) {
1013 dev_err(&spi->dev, "do not set bits in ctl_reg "
1014 "that the SPI framework manages\n");
1018 chip->enable_dma = chip_info->enable_dma != 0
1019 && drv_data->master_info->enable_dma;
1020 chip->ctl_reg = chip_info->ctl_reg;
1021 chip->bits_per_word = chip_info->bits_per_word;
1022 chip->cs_chg_udelay = chip_info->cs_chg_udelay;
1023 chip->idle_tx_val = chip_info->idle_tx_val;
1024 chip->pio_interrupt = chip_info->pio_interrupt;
1027 /* translate common spi framework into our register */
1028 if (spi->mode & SPI_CPOL)
1029 chip->ctl_reg |= BIT_CTL_CPOL;
1030 if (spi->mode & SPI_CPHA)
1031 chip->ctl_reg |= BIT_CTL_CPHA;
1032 if (spi->mode & SPI_LSB_FIRST)
1033 chip->ctl_reg |= BIT_CTL_LSBF;
1034 /* we dont support running in slave mode (yet?) */
1035 chip->ctl_reg |= BIT_CTL_MASTER;
1038 * Notice: for blackfin, the speed_hz is the value of register
1039 * SPI_BAUD, not the real baudrate
1041 chip->baud = hz_to_spi_baud(spi->max_speed_hz);
1042 chip->chip_select_num = spi->chip_select;
1043 if (chip->chip_select_num < MAX_CTRL_CS)
1044 chip->flag = (1 << spi->chip_select) << 8;
1046 chip->cs_gpio = chip->chip_select_num - MAX_CTRL_CS;
1048 switch (chip->bits_per_word) {
1051 chip->width = CFG_SPI_WORDSIZE8;
1052 chip->ops = &bfin_transfer_ops_u8;
1057 chip->width = CFG_SPI_WORDSIZE16;
1058 chip->ops = &bfin_transfer_ops_u16;
1062 dev_err(&spi->dev, "%d bits_per_word is not supported\n",
1063 chip->bits_per_word);
1067 if (chip->enable_dma && chip->pio_interrupt) {
1068 dev_err(&spi->dev, "enable_dma is set, "
1069 "do not set pio_interrupt\n");
1073 * if any one SPI chip is registered and wants DMA, request the
1074 * DMA channel for it
1076 if (chip->enable_dma && !drv_data->dma_requested) {
1077 /* register dma irq handler */
1078 ret = request_dma(drv_data->dma_channel, "BFIN_SPI_DMA");
1081 "Unable to request BlackFin SPI DMA channel\n");
1084 drv_data->dma_requested = 1;
1086 ret = set_dma_callback(drv_data->dma_channel,
1087 bfin_spi_dma_irq_handler, drv_data);
1089 dev_err(&spi->dev, "Unable to set dma callback\n");
1092 dma_disable_irq(drv_data->dma_channel);
1095 if (chip->pio_interrupt && !drv_data->irq_requested) {
1096 ret = request_irq(drv_data->spi_irq, bfin_spi_pio_irq_handler,
1097 IRQF_DISABLED, "BFIN_SPI", drv_data);
1099 dev_err(&spi->dev, "Unable to register spi IRQ\n");
1102 drv_data->irq_requested = 1;
1103 /* we use write mode, spi irq has to be disabled here */
1104 disable_irq(drv_data->spi_irq);
1107 if (chip->chip_select_num >= MAX_CTRL_CS) {
1108 ret = gpio_request(chip->cs_gpio, spi->modalias);
1110 dev_err(&spi->dev, "gpio_request() error\n");
1113 gpio_direction_output(chip->cs_gpio, 1);
1116 dev_dbg(&spi->dev, "setup spi chip %s, width is %d, dma is %d\n",
1117 spi->modalias, chip->width, chip->enable_dma);
1118 dev_dbg(&spi->dev, "ctl_reg is 0x%x, flag_reg is 0x%x\n",
1119 chip->ctl_reg, chip->flag);
1121 spi_set_ctldata(spi, chip);
1123 dev_dbg(&spi->dev, "chip select number is %d\n", chip->chip_select_num);
1124 if (chip->chip_select_num < MAX_CTRL_CS) {
1125 ret = peripheral_request(ssel[spi->master->bus_num]
1126 [chip->chip_select_num-1], spi->modalias);
1128 dev_err(&spi->dev, "peripheral_request() error\n");
1133 bfin_spi_cs_enable(drv_data, chip);
1134 bfin_spi_cs_deactive(drv_data, chip);
1139 if (chip->chip_select_num >= MAX_CTRL_CS)
1140 gpio_free(chip->cs_gpio);
1142 peripheral_free(ssel[spi->master->bus_num]
1143 [chip->chip_select_num - 1]);
1146 if (drv_data->dma_requested)
1147 free_dma(drv_data->dma_channel);
1148 drv_data->dma_requested = 0;
1151 /* prevent free 'chip' twice */
1152 spi_set_ctldata(spi, NULL);
1159 * callback for spi framework.
1160 * clean driver specific data
1162 static void bfin_spi_cleanup(struct spi_device *spi)
1164 struct slave_data *chip = spi_get_ctldata(spi);
1165 struct master_data *drv_data = spi_master_get_devdata(spi->master);
1170 if (chip->chip_select_num < MAX_CTRL_CS) {
1171 peripheral_free(ssel[spi->master->bus_num]
1172 [chip->chip_select_num-1]);
1173 bfin_spi_cs_disable(drv_data, chip);
1175 gpio_free(chip->cs_gpio);
1178 /* prevent free 'chip' twice */
1179 spi_set_ctldata(spi, NULL);
1182 static inline int bfin_spi_init_queue(struct master_data *drv_data)
1184 INIT_LIST_HEAD(&drv_data->queue);
1185 spin_lock_init(&drv_data->lock);
1187 drv_data->running = false;
1190 /* init transfer tasklet */
1191 tasklet_init(&drv_data->pump_transfers,
1192 bfin_spi_pump_transfers, (unsigned long)drv_data);
1194 /* init messages workqueue */
1195 INIT_WORK(&drv_data->pump_messages, bfin_spi_pump_messages);
1196 drv_data->workqueue = create_singlethread_workqueue(
1197 dev_name(drv_data->master->dev.parent));
1198 if (drv_data->workqueue == NULL)
1204 static inline int bfin_spi_start_queue(struct master_data *drv_data)
1206 unsigned long flags;
1208 spin_lock_irqsave(&drv_data->lock, flags);
1210 if (drv_data->running || drv_data->busy) {
1211 spin_unlock_irqrestore(&drv_data->lock, flags);
1215 drv_data->running = true;
1216 drv_data->cur_msg = NULL;
1217 drv_data->cur_transfer = NULL;
1218 drv_data->cur_chip = NULL;
1219 spin_unlock_irqrestore(&drv_data->lock, flags);
1221 queue_work(drv_data->workqueue, &drv_data->pump_messages);
1226 static inline int bfin_spi_stop_queue(struct master_data *drv_data)
1228 unsigned long flags;
1229 unsigned limit = 500;
1232 spin_lock_irqsave(&drv_data->lock, flags);
1235 * This is a bit lame, but is optimized for the common execution path.
1236 * A wait_queue on the drv_data->busy could be used, but then the common
1237 * execution path (pump_messages) would be required to call wake_up or
1238 * friends on every SPI message. Do this instead
1240 drv_data->running = false;
1241 while (!list_empty(&drv_data->queue) && drv_data->busy && limit--) {
1242 spin_unlock_irqrestore(&drv_data->lock, flags);
1244 spin_lock_irqsave(&drv_data->lock, flags);
1247 if (!list_empty(&drv_data->queue) || drv_data->busy)
1250 spin_unlock_irqrestore(&drv_data->lock, flags);
1255 static inline int bfin_spi_destroy_queue(struct master_data *drv_data)
1259 status = bfin_spi_stop_queue(drv_data);
1263 destroy_workqueue(drv_data->workqueue);
1268 static int __init bfin_spi_probe(struct platform_device *pdev)
1270 struct device *dev = &pdev->dev;
1271 struct bfin5xx_spi_master *platform_info;
1272 struct spi_master *master;
1273 struct master_data *drv_data;
1274 struct resource *res;
1277 platform_info = dev->platform_data;
1279 /* Allocate master with space for drv_data */
1280 master = spi_alloc_master(dev, sizeof(*drv_data));
1282 dev_err(&pdev->dev, "can not alloc spi_master\n");
1286 drv_data = spi_master_get_devdata(master);
1287 drv_data->master = master;
1288 drv_data->master_info = platform_info;
1289 drv_data->pdev = pdev;
1290 drv_data->pin_req = platform_info->pin_req;
1292 /* the spi->mode bits supported by this driver: */
1293 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
1295 master->bus_num = pdev->id;
1296 master->num_chipselect = platform_info->num_chipselect;
1297 master->cleanup = bfin_spi_cleanup;
1298 master->setup = bfin_spi_setup;
1299 master->transfer = bfin_spi_transfer;
1301 /* Find and map our resources */
1302 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1304 dev_err(dev, "Cannot get IORESOURCE_MEM\n");
1306 goto out_error_get_res;
1309 drv_data->regs_base = ioremap(res->start, resource_size(res));
1310 if (drv_data->regs_base == NULL) {
1311 dev_err(dev, "Cannot map IO\n");
1313 goto out_error_ioremap;
1316 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1318 dev_err(dev, "No DMA channel specified\n");
1320 goto out_error_free_io;
1322 drv_data->dma_channel = res->start;
1324 drv_data->spi_irq = platform_get_irq(pdev, 0);
1325 if (drv_data->spi_irq < 0) {
1326 dev_err(dev, "No spi pio irq specified\n");
1328 goto out_error_free_io;
1331 /* Initial and start queue */
1332 status = bfin_spi_init_queue(drv_data);
1334 dev_err(dev, "problem initializing queue\n");
1335 goto out_error_queue_alloc;
1338 status = bfin_spi_start_queue(drv_data);
1340 dev_err(dev, "problem starting queue\n");
1341 goto out_error_queue_alloc;
1344 status = peripheral_request_list(drv_data->pin_req, DRV_NAME);
1346 dev_err(&pdev->dev, ": Requesting Peripherals failed\n");
1347 goto out_error_queue_alloc;
1350 /* Reset SPI registers. If these registers were used by the boot loader,
1351 * the sky may fall on your head if you enable the dma controller.
1353 write_CTRL(drv_data, BIT_CTL_CPHA | BIT_CTL_MASTER);
1354 write_FLAG(drv_data, 0xFF00);
1356 /* Register with the SPI framework */
1357 platform_set_drvdata(pdev, drv_data);
1358 status = spi_register_master(master);
1360 dev_err(dev, "problem registering spi master\n");
1361 goto out_error_queue_alloc;
1364 dev_info(dev, "%s, Version %s, regs_base@%p, dma channel@%d\n",
1365 DRV_DESC, DRV_VERSION, drv_data->regs_base,
1366 drv_data->dma_channel);
1369 out_error_queue_alloc:
1370 bfin_spi_destroy_queue(drv_data);
1372 iounmap((void *) drv_data->regs_base);
1375 spi_master_put(master);
1380 /* stop hardware and remove the driver */
1381 static int __devexit bfin_spi_remove(struct platform_device *pdev)
1383 struct master_data *drv_data = platform_get_drvdata(pdev);
1389 /* Remove the queue */
1390 status = bfin_spi_destroy_queue(drv_data);
1394 /* Disable the SSP at the peripheral and SOC level */
1395 bfin_spi_disable(drv_data);
1398 if (drv_data->master_info->enable_dma) {
1399 if (dma_channel_active(drv_data->dma_channel))
1400 free_dma(drv_data->dma_channel);
1403 if (drv_data->irq_requested) {
1404 free_irq(drv_data->spi_irq, drv_data);
1405 drv_data->irq_requested = 0;
1408 /* Disconnect from the SPI framework */
1409 spi_unregister_master(drv_data->master);
1411 peripheral_free_list(drv_data->pin_req);
1413 /* Prevent double remove */
1414 platform_set_drvdata(pdev, NULL);
1420 static int bfin_spi_suspend(struct platform_device *pdev, pm_message_t state)
1422 struct master_data *drv_data = platform_get_drvdata(pdev);
1425 status = bfin_spi_stop_queue(drv_data);
1430 bfin_spi_disable(drv_data);
1435 static int bfin_spi_resume(struct platform_device *pdev)
1437 struct master_data *drv_data = platform_get_drvdata(pdev);
1440 /* Enable the SPI interface */
1441 bfin_spi_enable(drv_data);
1443 /* Start the queue running */
1444 status = bfin_spi_start_queue(drv_data);
1446 dev_err(&pdev->dev, "problem starting queue (%d)\n", status);
1453 #define bfin_spi_suspend NULL
1454 #define bfin_spi_resume NULL
1455 #endif /* CONFIG_PM */
1457 MODULE_ALIAS("platform:bfin-spi");
1458 static struct platform_driver bfin_spi_driver = {
1461 .owner = THIS_MODULE,
1463 .suspend = bfin_spi_suspend,
1464 .resume = bfin_spi_resume,
1465 .remove = __devexit_p(bfin_spi_remove),
1468 static int __init bfin_spi_init(void)
1470 return platform_driver_probe(&bfin_spi_driver, bfin_spi_probe);
1472 module_init(bfin_spi_init);
1474 static void __exit bfin_spi_exit(void)
1476 platform_driver_unregister(&bfin_spi_driver);
1478 module_exit(bfin_spi_exit);