12 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/slab.h>
19 #include <linux/errno.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"
40 #define START_STATE ((void *)0)
41 #define RUNNING_STATE ((void *)1)
42 #define DONE_STATE ((void *)2)
43 #define ERROR_STATE ((void *)-1)
136 static u16 hz_to_spi_baud(
u32 speed_hz)
139 u16 spi_baud = (sclk / (2 * speed_hz));
141 if ((sclk % (2 * speed_hz)) > 0)
207 bfin_spi_disable(drv_data);
208 dev_dbg(&drv_data->
pdev->dev,
"restoring spi ctl state\n");
216 bfin_spi_enable(drv_data);
217 bfin_spi_cs_active(drv_data, chip);
229 bfin_spi_dummy_read(drv_data);
231 while (drv_data->
tx < drv_data->
tx_end) {
238 bfin_spi_dummy_read(drv_data);
247 bfin_spi_dummy_read(drv_data);
249 while (drv_data->
rx < drv_data->
rx_end) {
260 bfin_spi_dummy_read(drv_data);
262 while (drv_data->
rx < drv_data->
rx_end) {
271 .write = bfin_spi_u8_writer,
272 .read = bfin_spi_u8_reader,
273 .duplex = bfin_spi_u8_duplex,
279 bfin_spi_dummy_read(drv_data);
281 while (drv_data->
tx < drv_data->
tx_end) {
289 bfin_spi_dummy_read(drv_data);
298 bfin_spi_dummy_read(drv_data);
300 while (drv_data->
rx < drv_data->
rx_end) {
312 bfin_spi_dummy_read(drv_data);
314 while (drv_data->
rx < drv_data->
rx_end) {
325 .write = bfin_spi_u16_writer,
326 .read = bfin_spi_u16_reader,
327 .duplex = bfin_spi_u16_duplex,
363 spin_unlock_irqrestore(&drv_data->
lock, flags);
371 bfin_spi_cs_deactive(drv_data, chip);
374 if (drv_data->
tx_dma != 0xFFFF)
375 bfin_spi_disable(drv_data);
387 int n_bytes = drv_data->
n_bytes;
394 if ((drv_data->
tx && drv_data->
tx >= drv_data->
tx_end) ||
395 (drv_data->
rx && drv_data->
rx >= (drv_data->
rx_end - n_bytes))) {
399 if (!(n_bytes % 2)) {
401 for (loop = 0; loop < n_bytes / 2; loop++)
404 u8 *buf = (
u8 *)drv_data->
rx;
405 for (loop = 0; loop < n_bytes; loop++)
408 drv_data->
rx += n_bytes;
413 bfin_spi_cs_deactive(drv_data, chip);
415 msg->
state = bfin_spi_next_transfer(drv_data);
424 if (drv_data->
rx && drv_data->
tx) {
426 dev_dbg(&drv_data->
pdev->dev,
"duplex: write_TDBR\n");
427 if (!(n_bytes % 2)) {
430 for (loop = 0; loop < n_bytes / 2; loop++) {
435 u8 *buf = (
u8 *)drv_data->
rx;
436 u8 *buf2 = (
u8 *)drv_data->
tx;
437 for (loop = 0; loop < n_bytes; loop++) {
442 }
else if (drv_data->
rx) {
444 dev_dbg(&drv_data->
pdev->dev,
"read: write_TDBR\n");
445 if (!(n_bytes % 2)) {
447 for (loop = 0; loop < n_bytes / 2; loop++) {
452 u8 *buf = (
u8 *)drv_data->
rx;
453 for (loop = 0; loop < n_bytes; loop++) {
458 }
else if (drv_data->
tx) {
460 dev_dbg(&drv_data->
pdev->dev,
"write: write_TDBR\n");
461 if (!(n_bytes % 2)) {
463 for (loop = 0; loop < n_bytes / 2; loop++) {
468 u8 *buf = (
u8 *)drv_data->
tx;
469 for (loop = 0; loop < n_bytes; loop++) {
477 drv_data->
tx += n_bytes;
479 drv_data->
rx += n_bytes;
484 static irqreturn_t bfin_spi_dma_irq_handler(
int irq,
void *dev_id)
494 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
497 if (drv_data->
rx !=
NULL) {
500 bfin_spi_dummy_read(drv_data);
514 if (drv_data->
tx !=
NULL) {
521 "in dma_irq_handler dmastat:0x%x spistat:0x%x\n",
527 dev_warn(&drv_data->
pdev->dev,
"timeout waiting for SPIF");
534 dev_err(&drv_data->
pdev->dev,
"dma receive: fifo/buffer overflow\n");
539 bfin_spi_cs_deactive(drv_data, chip);
542 msg->
state = bfin_spi_next_transfer(drv_data);
550 "disable dma channel irq%d\n",
557 static void bfin_spi_pump_transfers(
unsigned long data)
564 unsigned int bits_per_word;
565 u16 cr, cr_width, dma_width, dma_config;
566 u32 tranf_success = 1;
580 dev_dbg(&drv_data->
pdev->dev,
"transfer: we've hit an error\n");
582 bfin_spi_giveback(drv_data);
588 dev_dbg(&drv_data->
pdev->dev,
"transfer: all done!\n");
590 bfin_spi_flush(drv_data);
591 bfin_spi_giveback(drv_data);
597 dev_dbg(&drv_data->
pdev->dev,
"transfer: still running ...\n");
605 if (bfin_spi_flush(drv_data) == 0) {
606 dev_err(&drv_data->
pdev->dev,
"pump_transfers: flush failed\n");
608 bfin_spi_giveback(drv_data);
612 if (transfer->
len == 0) {
614 message->
state = bfin_spi_next_transfer(drv_data);
621 drv_data->
tx = (
void *)transfer->
tx_buf;
623 dev_dbg(&drv_data->
pdev->dev,
"tx_buf is %p, tx_end is %p\n",
633 dev_dbg(&drv_data->
pdev->dev,
"rx_buf is %p, rx_end is %p\n",
646 message->
spi->bits_per_word ? : 8;
647 if (bits_per_word % 16 == 0) {
648 drv_data->
n_bytes = bits_per_word/8;
649 drv_data->
len = (transfer->
len) >> 1;
651 drv_data->
ops = &bfin_bfin_spi_transfer_ops_u16;
652 }
else if (bits_per_word % 8 == 0) {
653 drv_data->
n_bytes = bits_per_word/8;
654 drv_data->
len = transfer->
len;
656 drv_data->
ops = &bfin_bfin_spi_transfer_ops_u8;
658 dev_err(&drv_data->
pdev->dev,
"transfer: unsupported bits_per_word\n");
660 bfin_spi_giveback(drv_data);
668 "transfer: drv_data->ops is %p, chip->ops is %p, u8_ops is %p\n",
669 drv_data->
ops, chip->
ops, &bfin_bfin_spi_transfer_ops_u8);
681 bfin_spi_cs_active(drv_data, chip);
684 "now pumping a transfer: width is %d, len is %d\n",
685 cr_width, transfer->
len);
693 if (!full_duplex && drv_data->
cur_chip->enable_dma
694 && drv_data->
len > 6) {
696 unsigned long dma_start_addr,
flags;
702 dev_dbg(&drv_data->
pdev->dev,
"doing dma transfer\n");
717 if (drv_data->
tx_dma == 0xFFFF) {
719 "doing autobuffer DMA out.\n");
726 (
unsigned long)drv_data->
tx);
736 bfin_spi_giveback(drv_data);
742 if (drv_data->
rx !=
NULL) {
744 dev_dbg(&drv_data->
pdev->dev,
"doing DMA in to %p (size %zx)\n",
748 if (bfin_addr_dcacheable((
unsigned long) drv_data->
rx))
750 (
unsigned long) (drv_data->
rx +
754 dma_start_addr = (
unsigned long)drv_data->
rx;
757 }
else if (drv_data->
tx !=
NULL) {
761 if (bfin_addr_dcacheable((
unsigned long) drv_data->
tx))
763 (
unsigned long) (drv_data->
tx +
766 dma_start_addr = (
unsigned long)drv_data->
tx;
781 set_dma_start_addr(drv_data->
dma_channel, dma_start_addr);
805 bfin_spi_dummy_read(drv_data);
812 if (bits_per_word % 16 == 0) {
814 for (loop = 0; loop < bits_per_word / 16;
818 }
else if (bits_per_word % 8 == 0) {
819 u8 *buf = (
u8 *)drv_data->
tx;
820 for (loop = 0; loop < bits_per_word / 8; loop++)
833 dev_dbg(&drv_data->
pdev->dev,
"doing IO transfer\n");
840 "IO duplex: cr is 0x%x\n", cr);
842 drv_data->
ops->duplex(drv_data);
844 if (drv_data->
tx != drv_data->
tx_end)
846 }
else if (drv_data->
tx !=
NULL) {
849 "IO write: cr is 0x%x\n", cr);
851 drv_data->
ops->write(drv_data);
853 if (drv_data->
tx != drv_data->
tx_end)
855 }
else if (drv_data->
rx !=
NULL) {
858 "IO read: cr is 0x%x\n", cr);
860 drv_data->
ops->read(drv_data);
861 if (drv_data->
rx != drv_data->
rx_end)
865 if (!tranf_success) {
867 "IO write error!\n");
873 message->
state = bfin_spi_next_transfer(drv_data);
875 bfin_spi_flush(drv_data);
876 bfin_spi_cs_deactive(drv_data, chip);
894 if (list_empty(&drv_data->
queue) || !drv_data->
running) {
897 spin_unlock_irqrestore(&drv_data->
lock, flags);
903 spin_unlock_irqrestore(&drv_data->
lock, flags);
913 bfin_spi_restore_state(drv_data);
915 list_del_init(&drv_data->
cur_msg->queue);
922 dev_dbg(&drv_data->
pdev->dev,
"got a message to pump, "
923 "state is set to: baud %d, flag 0x%x, ctl 0x%x\n",
928 "the first transfer len is %d\n",
935 spin_unlock_irqrestore(&drv_data->
lock, flags);
950 spin_unlock_irqrestore(&drv_data->
lock, flags);
958 dev_dbg(&spi->
dev,
"adding an msg in transfer() \n");
964 spin_unlock_irqrestore(&drv_data->
lock, flags);
969 #define MAX_SPI_SSEL 7
986 static int bfin_spi_setup(
struct spi_device *spi)
996 chip = spi_get_ctldata(spi);
1000 dev_err(&spi->
dev,
"cannot allocate chip data\n");
1021 if (chip_info->
ctl_reg & ~bfin_ctl_reg) {
1022 dev_err(&spi->
dev,
"do not set bits in ctl_reg "
1023 "that the SPI framework manages\n");
1034 chip->
ctl_reg &= bfin_ctl_reg;
1038 dev_err(&spi->
dev,
"%d bits_per_word is not supported\n",
1045 dev_err(&spi->
dev,
"unsupported spi modes detected\n");
1066 " Slave Select not under software control!\n"
1067 " See Documentation/blackfin/bfin-spi-notes.txt");
1075 "do not set pio_interrupt\n");
1087 "Unable to request BlackFin SPI DMA channel\n");
1093 bfin_spi_dma_irq_handler, drv_data);
1095 dev_err(&spi->
dev,
"Unable to set dma callback\n");
1103 0,
"BFIN_SPI", drv_data);
1105 dev_err(&spi->
dev,
"Unable to register spi IRQ\n");
1115 if (spi_get_ctldata(spi) ==
NULL) {
1118 dev_err(&spi->
dev,
"gpio_request() error\n");
1125 dev_dbg(&spi->
dev,
"setup spi chip %s, width is %d, dma is %d\n",
1127 dev_dbg(&spi->
dev,
"ctl_reg is 0x%x, flag_reg is 0x%x\n",
1130 spi_set_ctldata(spi, chip);
1137 dev_err(&spi->
dev,
"peripheral_request() error\n");
1142 bfin_spi_cs_enable(drv_data, chip);
1143 bfin_spi_cs_deactive(drv_data, chip);
1161 spi_set_ctldata(spi,
NULL);
1171 static void bfin_spi_cleanup(
struct spi_device *spi)
1182 bfin_spi_cs_disable(drv_data, chip);
1188 spi_set_ctldata(spi,
NULL);
1193 INIT_LIST_HEAD(&drv_data->
queue);
1201 bfin_spi_pump_transfers, (
unsigned long)drv_data);
1206 dev_name(drv_data->
master->dev.parent));
1215 unsigned long flags;
1220 spin_unlock_irqrestore(&drv_data->
lock, flags);
1228 spin_unlock_irqrestore(&drv_data->
lock, flags);
1237 unsigned long flags;
1238 unsigned limit = 500;
1250 while ((!list_empty(&drv_data->
queue) || drv_data->
busy) && limit--) {
1251 spin_unlock_irqrestore(&drv_data->
lock, flags);
1256 if (!list_empty(&drv_data->
queue) || drv_data->
busy)
1259 spin_unlock_irqrestore(&drv_data->
lock, flags);
1268 status = bfin_spi_stop_queue(drv_data);
1291 dev_err(&pdev->
dev,
"can not alloc spi_master\n");
1295 drv_data = spi_master_get_devdata(master);
1296 drv_data->
master = master;
1298 drv_data->
pdev = pdev;
1306 master->
cleanup = bfin_spi_cleanup;
1307 master->
setup = bfin_spi_setup;
1308 master->
transfer = bfin_spi_transfer;
1313 dev_err(dev,
"Cannot get IORESOURCE_MEM\n");
1315 goto out_error_get_res;
1320 dev_err(dev,
"Cannot map IO\n");
1322 goto out_error_ioremap;
1327 dev_err(dev,
"No DMA channel specified\n");
1329 goto out_error_free_io;
1335 dev_err(dev,
"No spi pio irq specified\n");
1337 goto out_error_free_io;
1341 status = bfin_spi_init_queue(drv_data);
1343 dev_err(dev,
"problem initializing queue\n");
1344 goto out_error_queue_alloc;
1347 status = bfin_spi_start_queue(drv_data);
1349 dev_err(dev,
"problem starting queue\n");
1350 goto out_error_queue_alloc;
1355 dev_err(&pdev->
dev,
": Requesting Peripherals failed\n");
1356 goto out_error_queue_alloc;
1366 platform_set_drvdata(pdev, drv_data);
1369 dev_err(dev,
"problem registering spi master\n");
1370 goto out_error_queue_alloc;
1373 dev_info(dev,
"%s, Version %s, regs@%p, dma channel@%d\n",
1378 out_error_queue_alloc:
1379 bfin_spi_destroy_queue(drv_data);
1384 spi_master_put(master);
1399 status = bfin_spi_destroy_queue(drv_data);
1404 bfin_spi_disable(drv_data);
1423 platform_set_drvdata(pdev,
NULL);
1434 status = bfin_spi_stop_queue(drv_data);
1459 status = bfin_spi_start_queue(drv_data);
1461 dev_err(&pdev->
dev,
"problem starting queue (%d)\n", status);
1468 #define bfin_spi_suspend NULL
1469 #define bfin_spi_resume NULL
1483 static int __init bfin_spi_init(
void)
1489 static void __exit bfin_spi_exit(
void)