22 #include <linux/module.h>
25 #include <linux/slab.h>
30 #ifdef CONFIG_DEBUG_FS
34 #define START_STATE ((void *)0)
35 #define RUNNING_STATE ((void *)1)
36 #define DONE_STATE ((void *)2)
37 #define ERROR_STATE ((void *)-1)
39 #define QUEUE_RUNNING 0
40 #define QUEUE_STOPPED 1
42 #define MRST_SPI_DEASSERT 0
43 #define MRST_SPI_ASSERT 1
65 #ifdef CONFIG_DEBUG_FS
66 #define SPI_REGS_BUFSIZE 1024
68 size_t count, loff_t *ppos)
81 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
82 "MRST SPI0 registers:\n");
83 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
84 "=================================\n");
85 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
87 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
89 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
91 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
93 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
95 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
97 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
99 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
101 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
103 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
105 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
107 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
109 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
111 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
113 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
115 len +=
snprintf(buf + len, SPI_REGS_BUFSIZE - len,
116 "=================================\n");
126 .read = spi_show_regs,
130 static int mrst_spi_debugfs_init(
struct dw_spi *dws)
137 dws->debugfs, (
void *)dws, &mrst_spi_regs_ops);
141 static void mrst_spi_debugfs_remove(
struct dw_spi *dws)
148 static inline int mrst_spi_debugfs_init(
struct dw_spi *dws)
153 static inline void mrst_spi_debugfs_remove(
struct dw_spi *dws)
159 static inline u32 tx_max(
struct dw_spi *dws)
161 u32 tx_left, tx_room, rxtx_gap;
181 static inline u32 rx_max(
struct dw_spi *dws)
188 static void dw_writer(
struct dw_spi *dws)
197 txw = *(
u8 *)(dws->
tx);
199 txw = *(
u16 *)(dws->
tx);
206 static void dw_reader(
struct dw_spi *dws)
208 u32 max = rx_max(dws);
216 *(
u8 *)(dws->
rx) = rxw;
218 *(
u16 *)(dws->
rx) = rxw;
224 static void *next_transfer(
struct dw_spi *dws)
245 static int map_dma_buffers(
struct dw_spi *dws)
247 if (!dws->
cur_msg->is_dma_mapped
263 static void giveback(
struct dw_spi *dws)
277 spin_unlock_irqrestore(&dws->
lock, flags);
291 static void int_error_stop(
struct dw_spi *dws,
const char *msg)
294 spi_enable_chip(dws, 0);
307 dws->
cur_msg->state = next_transfer(dws);
327 int_error_stop(dws,
"interrupt_transfer: fifo overrun/underrun");
338 spi_mask_intr(dws, SPI_INT_TXEI);
341 spi_umask_intr(dws, SPI_INT_TXEI);
364 static void poll_transfer(
struct dw_spi *dws)
398 chip->clk_div = dws->
max_freq / chip->speed_hz;
439 speed = chip->speed_hz;
445 "freq: %dHz\n", speed);
452 clk_div = (clk_div + 1) & 0xfffe;
454 chip->speed_hz = speed;
485 if (dws->
rx && dws->
tx)
506 txint_level = (templen > txint_level) ? txint_level : templen;
518 if (dw_readw(dws,
DW_SPI_CTRL0) != cr0 || cs_change || clk_div || imask) {
519 spi_enable_chip(dws, 0);
524 spi_set_clk(dws, clk_div ? clk_div : chip->clk_div);
528 spi_mask_intr(dws, 0xff);
530 spi_umask_intr(dws, imask);
534 spi_enable_chip(dws, 1);
540 dws->
dma_ops->dma_transfer(dws, cs_change);
562 spin_unlock_irqrestore(&dws->
lock, flags);
568 spin_unlock_irqrestore(&dws->
lock, flags);
574 list_del_init(&dws->
cur_msg->queue);
587 spin_unlock_irqrestore(&dws->
lock, flags);
593 struct dw_spi *dws = spi_master_get_devdata(spi->
master);
599 spin_unlock_irqrestore(&dws->
lock, flags);
616 spin_unlock_irqrestore(&dws->
lock, flags);
622 spin_unlock_irqrestore(&dws->
lock, flags);
627 static int dw_spi_setup(
struct spi_device *spi)
630 struct chip_data *
chip;
636 chip = spi_get_ctldata(spi);
638 chip = kzalloc(
sizeof(
struct chip_data),
GFP_KERNEL);
655 chip->type = chip_info->
type;
657 chip->rx_threshold = 0;
658 chip->tx_threshold = 0;
677 dev_err(&spi->
dev,
"No max speed HZ parameter\n");
684 chip->cr0 = (chip->bits_per_word - 1)
689 spi_set_ctldata(spi, chip);
693 static void dw_spi_cleanup(
struct spi_device *spi)
695 struct chip_data *chip = spi_get_ctldata(spi);
701 INIT_LIST_HEAD(&dws->
queue);
708 pump_transfers, (
unsigned long)dws);
712 dev_name(dws->
master->dev.parent));
719 static int start_queue(
struct dw_spi *dws)
726 spin_unlock_irqrestore(&dws->
lock, flags);
735 spin_unlock_irqrestore(&dws->
lock, flags);
742 static int stop_queue(
struct dw_spi *dws)
750 while ((!list_empty(&dws->
queue) || dws->
busy) && limit--) {
751 spin_unlock_irqrestore(&dws->
lock, flags);
756 if (!list_empty(&dws->
queue) || dws->
busy)
758 spin_unlock_irqrestore(&dws->
lock, flags);
763 static int destroy_queue(
struct dw_spi *dws)
767 status = stop_queue(dws);
775 static void spi_hw_init(
struct dw_spi *dws)
777 spi_enable_chip(dws, 0);
778 spi_mask_intr(dws, 0xff);
779 spi_enable_chip(dws, 1);
787 for (fifo = 2; fifo <= 257; fifo++) {
793 dws->
fifo_len = (fifo == 257) ? 0 : fifo;
823 goto err_free_master;
829 master->
cleanup = dw_spi_cleanup;
830 master->
setup = dw_spi_setup;
837 ret = dws->
dma_ops->dma_init(dws);
845 ret = init_queue(dws);
847 dev_err(&master->
dev,
"problem initializing queue\n");
850 ret = start_queue(dws);
852 dev_err(&master->
dev,
"problem starting queue\n");
856 spi_master_set_devdata(master, dws);
859 dev_err(&master->
dev,
"problem registering spi master\n");
860 goto err_queue_alloc;
863 mrst_spi_debugfs_init(dws);
871 spi_enable_chip(dws, 0);
874 spi_master_put(master);
886 mrst_spi_debugfs_remove(dws);
889 status = destroy_queue(dws);
891 dev_err(&dws->
master->dev,
"dw_spi_remove: workqueue will not "
892 "complete, message memory not freed\n");
896 spi_enable_chip(dws, 0);
910 ret = stop_queue(dws);
913 spi_enable_chip(dws, 0);
924 ret = start_queue(dws);
926 dev_err(&dws->
master->dev,
"fail to start queue (%d)\n", ret);