1 /*drivers/serial/rk2818_spim.c - driver for rk2818 spim device
3 * Copyright (C) 2010 ROCKCHIP, Inc.
5 * This software is licensed under the terms of the GNU General Public
6 * License version 2, as published by the Free Software Foundation, and
7 * may be copied, distributed, and modified under those terms.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
15 #include <linux/dma-mapping.h>
16 #include <linux/interrupt.h>
17 #include <linux/highmem.h>
18 #include <linux/delay.h>
19 #include <linux/slab.h>
20 #include <linux/platform_device.h>
21 #include <linux/clk.h>
22 #include <linux/cpufreq.h>
23 #include <mach/gpio.h>
24 #include <linux/dma-mapping.h>
27 #include "rk2818_spim.h"
28 #include <linux/spi/spi.h>
30 #ifdef CONFIG_DEBUG_FS
31 #include <linux/debugfs.h>
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 ///CS0
44 #define MRST_SPI_ASSERT1 2 ///CS1
46 /* Slave spi_dev related */
49 u8 cs; /* chip select pin */
50 u8 n_bytes; /* current is a 1/2/4 byte op */
51 u8 tmode; /* TR/TO/RO/EEPROM */
52 u8 type; /* SPI/SSP/MicroWire */
54 u8 poll_mode; /* 1 means use poll mode */
61 u16 clk_div; /* baud rate divider */
62 u32 speed_hz; /* baud rate */
63 int (*write)(struct rk2818_spi *dws);
64 int (*read)(struct rk2818_spi *dws);
65 void (*cs_control)(u32 command);
68 #ifdef CONFIG_DEBUG_FS
69 static int spi_show_regs_open(struct inode *inode, struct file *file)
71 file->private_data = inode->i_private;
75 #define SPI_REGS_BUFSIZE 1024
76 static ssize_t spi_show_regs(struct file *file, char __user *user_buf,
77 size_t count, loff_t *ppos)
79 struct rk2818_spi *dws;
84 dws = file->private_data;
86 buf = kzalloc(SPI_REGS_BUFSIZE, GFP_KERNEL);
90 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
91 "MRST SPI0 registers:\n");
92 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
93 "=================================\n");
94 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
95 "CTRL0: \t\t0x%08x\n", rk2818_readl(dws, SPIM_CTRLR0));
96 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
97 "CTRL1: \t\t0x%08x\n", rk2818_readl(dws, SPIM_CTRLR1));
98 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
99 "SSIENR: \t0x%08x\n", rk2818_readl(dws, SPIM_SPIENR));
100 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
101 "SER: \t\t0x%08x\n", rk2818_readl(dws, SPIM_SER));
102 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
103 "BAUDR: \t\t0x%08x\n", rk2818_readl(dws, SPIM_BAUDR));
104 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
105 "TXFTLR: \t0x%08x\n", rk2818_readl(dws, SPIM_TXFTLR));
106 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
107 "RXFTLR: \t0x%08x\n", rk2818_readl(dws, SPIM_RXFTLR));
108 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
109 "TXFLR: \t\t0x%08x\n", rk2818_readl(dws, SPIM_TXFLR));
110 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
111 "RXFLR: \t\t0x%08x\n", rk2818_readl(dws, SPIM_RXFLR));
112 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
113 "SR: \t\t0x%08x\n", rk2818_readl(dws, SPIM_SR));
114 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
115 "IMR: \t\t0x%08x\n", rk2818_readl(dws, SPIM_IMR));
116 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
117 "ISR: \t\t0x%08x\n", rk2818_readl(dws, SPIM_ISR));
118 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
119 "DMACR: \t\t0x%08x\n", rk2818_readl(dws, SPIM_DMACR));
120 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
121 "DMATDLR: \t0x%08x\n", rk2818_readl(dws, SPIM_DMATDLR));
122 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
123 "DMARDLR: \t0x%08x\n", rk2818_readl(dws, SPIM_DMARDLR));
124 len += printk(buf + len, SPI_REGS_BUFSIZE - len,
125 "=================================\n");
127 ret = simple_read_from_buffer(user_buf, count, ppos, buf, len);
132 static const struct file_operations mrst_spi_regs_ops = {
133 .owner = THIS_MODULE,
134 .open = spi_show_regs_open,
135 .read = spi_show_regs,
138 static int mrst_spi_debugfs_init(struct rk2818_spi *dws)
140 dws->debugfs = debugfs_create_dir("mrst_spi", NULL);
144 debugfs_create_file("registers", S_IFREG | S_IRUGO,
145 dws->debugfs, (void *)dws, &mrst_spi_regs_ops);
149 static void mrst_spi_debugfs_remove(struct rk2818_spi *dws)
152 debugfs_remove_recursive(dws->debugfs);
156 static inline int mrst_spi_debugfs_init(struct rk2818_spi *dws)
161 static inline void mrst_spi_debugfs_remove(struct rk2818_spi *dws)
164 #endif /* CONFIG_DEBUG_FS */
166 static void wait_till_not_busy(struct rk2818_spi *dws)
168 unsigned long end = jiffies + 1 + usecs_to_jiffies(1000);
170 while (time_before(jiffies, end)) {
171 if (!(rk2818_readw(dws, SPIM_SR) & SR_BUSY))
174 dev_err(&dws->master->dev,
175 "DW SPI: Status keeps busy for 1000us after a read/write!\n");
178 static void flush(struct rk2818_spi *dws)
180 while (rk2818_readw(dws, SPIM_SR) & SR_RF_NOT_EMPT)
181 rk2818_readw(dws, SPIM_DR0);
183 wait_till_not_busy(dws);
186 static void null_cs_control(u32 command)
188 //printk("null_cs_control [%d]\n", command);
190 gpio_direction_output(RK2818_PIN_PB0,GPIO_LOW);
191 else if(command == 1)
192 gpio_direction_output(RK2818_PIN_PB4,GPIO_LOW);
194 gpio_direction_output(RK2818_PIN_PB0,GPIO_HIGH);
195 gpio_direction_output(RK2818_PIN_PB4,GPIO_HIGH);
199 static int null_writer(struct rk2818_spi *dws)
201 u8 n_bytes = dws->n_bytes;
203 if (!(rk2818_readw(dws, SPIM_SR) & SR_TF_NOT_FULL)
204 || (dws->tx == dws->tx_end))
206 rk2818_writew(dws, SPIM_DR0, 0);
209 wait_till_not_busy(dws);
213 static int null_reader(struct rk2818_spi *dws)
215 u8 n_bytes = dws->n_bytes;
216 while ((rk2818_readw(dws, SPIM_SR) & SR_RF_NOT_EMPT)
217 && (dws->rx < dws->rx_end)) {
218 rk2818_readw(dws, SPIM_DR0);
221 wait_till_not_busy(dws);
222 return dws->rx == dws->rx_end;
225 static int u8_writer(struct rk2818_spi *dws)
227 if (!(rk2818_readw(dws, SPIM_SR) & SR_TF_NOT_FULL)
228 || (dws->tx == dws->tx_end))
230 rk2818_writew(dws, SPIM_DR0, *(u8 *)(dws->tx));
233 wait_till_not_busy(dws);
237 static int u8_reader(struct rk2818_spi *dws)
239 while ((rk2818_readw(dws, SPIM_SR) & SR_RF_NOT_EMPT)
240 && (dws->rx < dws->rx_end)) {
241 *(u8 *)(dws->rx) = rk2818_readw(dws, SPIM_DR0);
245 wait_till_not_busy(dws);
246 return dws->rx == dws->rx_end;
249 static int u16_writer(struct rk2818_spi *dws)
251 if (!(rk2818_readw(dws, SPIM_SR) & SR_TF_NOT_FULL)
252 || (dws->tx == dws->tx_end))
255 rk2818_writew(dws, SPIM_DR0, *(u16 *)(dws->tx));
258 wait_till_not_busy(dws);
262 static int u16_reader(struct rk2818_spi *dws)
266 while ((rk2818_readw(dws, SPIM_SR) & SR_RF_NOT_EMPT)
267 && (dws->rx < dws->rx_end)) {
268 temp = rk2818_readw(dws, SPIM_DR0);
269 *(u16 *)(dws->rx) = temp;
273 wait_till_not_busy(dws);
274 return dws->rx == dws->rx_end;
277 static void *next_transfer(struct rk2818_spi *dws)
279 struct spi_message *msg = dws->cur_msg;
280 struct spi_transfer *trans = dws->cur_transfer;
282 /* Move to next transfer */
283 if (trans->transfer_list.next != &msg->transfers) {
285 list_entry(trans->transfer_list.next,
288 return RUNNING_STATE;
294 * Note: first step is the protocol driver prepares
295 * a dma-capable memory, and this func just need translate
296 * the virt addr to physical
298 static int map_dma_buffers(struct rk2818_spi *dws)
300 if (!dws->cur_msg->is_dma_mapped || !dws->dma_inited
301 || !dws->cur_chip->enable_dma)
304 if (dws->cur_transfer->tx_dma)
305 dws->tx_dma = dws->cur_transfer->tx_dma;
307 if (dws->cur_transfer->rx_dma)
308 dws->rx_dma = dws->cur_transfer->rx_dma;
313 /* Caller already set message->status; dma and pio irqs are blocked */
314 static void giveback(struct rk2818_spi *dws)
316 struct spi_transfer *last_transfer;
318 struct spi_message *msg;
320 spin_lock_irqsave(&dws->lock, flags);
323 dws->cur_transfer = NULL;
324 dws->prev_chip = dws->cur_chip;
325 dws->cur_chip = NULL;
327 queue_work(dws->workqueue, &dws->pump_messages);
328 spin_unlock_irqrestore(&dws->lock, flags);
330 last_transfer = list_entry(msg->transfers.prev,
334 if (!last_transfer->cs_change)
335 dws->cs_control(MRST_SPI_DEASSERT);
339 msg->complete(msg->context);
342 static void int_error_stop(struct rk2818_spi *dws, const char *msg)
344 /* Stop and reset hw */
346 spi_enable_chip(dws, 0);
348 dev_err(&dws->master->dev, "%s\n", msg);
349 dws->cur_msg->state = ERROR_STATE;
350 tasklet_schedule(&dws->pump_transfers);
353 static void transfer_complete(struct rk2818_spi *dws)
355 /* Update total byte transfered return count actual bytes read */
356 dws->cur_msg->actual_length += dws->len;
358 /* Move to next transfer */
359 dws->cur_msg->state = next_transfer(dws);
361 /* Handle end of message */
362 if (dws->cur_msg->state == DONE_STATE) {
363 dws->cur_msg->status = 0;
366 tasklet_schedule(&dws->pump_transfers);
369 static irqreturn_t interrupt_transfer(struct rk2818_spi *dws)
371 u16 irq_status, irq_mask = 0x3f;
372 u32 int_level = dws->fifo_len / 2;
375 irq_status = rk2818_readw(dws, SPIM_ISR) & irq_mask;
377 if (irq_status & (SPI_INT_TXOI | SPI_INT_RXOI | SPI_INT_RXUI)) {
378 rk2818_readw(dws, SPIM_TXOICR);
379 rk2818_readw(dws, SPIM_RXOICR);
380 rk2818_readw(dws, SPIM_RXUICR);
381 int_error_stop(dws, "interrupt_transfer: fifo overrun");
385 if (irq_status & SPI_INT_TXEI) {
386 spi_mask_intr(dws, SPI_INT_TXEI);
388 left = (dws->tx_end - dws->tx) / dws->n_bytes;
389 left = (left > int_level) ? int_level : left;
395 /* Re-enable the IRQ if there is still data left to tx */
396 if (dws->tx_end > dws->tx)
397 spi_umask_intr(dws, SPI_INT_TXEI);
399 transfer_complete(dws);
405 static irqreturn_t rk2818_spi_irq(int irq, void *dev_id)
407 struct rk2818_spi *dws = dev_id;
410 spi_mask_intr(dws, SPI_INT_TXEI);
415 return dws->transfer_handler(dws);
418 /* Must be called inside pump_transfers() */
419 static void poll_transfer(struct rk2818_spi *dws)
421 while (dws->write(dws))
424 transfer_complete(dws);
427 static void dma_transfer(struct rk2818_spi *dws, struct spi_transfer *xfer) //int cs_change)
432 static void spi_chip_sel(struct rk2818_spi *dws, u16 cs)
434 if(cs >= dws->master->num_chipselect)
437 if (dws->cs_control){
438 dws->cs_control(cs+1);
440 //rk2818_writel(dws, SPIM_SER, 1 << cs);
441 rk2818_writel(dws, SPIM_SER, 1 << 0);
444 static void pump_transfers(unsigned long data)
446 struct rk2818_spi *dws = (struct rk2818_spi *)data;
447 struct spi_message *message = NULL;
448 struct spi_transfer *transfer = NULL;
449 struct spi_transfer *previous = NULL;
450 struct spi_device *spi = NULL;
451 struct chip_data *chip = NULL;
460 /* Get current state information */
461 message = dws->cur_msg;
462 transfer = dws->cur_transfer;
463 chip = dws->cur_chip;
465 if (unlikely(!chip->clk_div))
466 chip->clk_div = clk_get_rate(dws->clock_spim) / chip->speed_hz;
467 if (message->state == ERROR_STATE) {
468 message->status = -EIO;
472 /* Handle end of message */
473 if (message->state == DONE_STATE) {
478 /* Delay if requested at end of transfer*/
479 if (message->state == RUNNING_STATE) {
480 previous = list_entry(transfer->transfer_list.prev,
483 if (previous->delay_usecs)
484 udelay(previous->delay_usecs);
487 dws->n_bytes = chip->n_bytes;
488 dws->dma_width = chip->dma_width;
489 dws->cs_control = chip->cs_control;
491 dws->rx_dma = transfer->rx_dma;
492 dws->tx_dma = transfer->tx_dma;
493 dws->tx = (void *)transfer->tx_buf;
494 dws->tx_end = dws->tx + transfer->len;
495 dws->rx = transfer->rx_buf;
496 dws->rx_end = dws->rx + transfer->len;
497 dws->write = dws->tx ? chip->write : null_writer;
498 dws->read = dws->rx ? chip->read : null_reader;
499 dws->cs_change = transfer->cs_change;
500 dws->len = dws->cur_transfer->len;
501 if (chip != dws->prev_chip)
506 /* Handle per transfer options for bpw and speed */
507 if (transfer->speed_hz) {
508 speed = chip->speed_hz;
510 if (transfer->speed_hz != speed) {
511 speed = transfer->speed_hz;
512 if (speed > clk_get_rate(dws->clock_spim)) {
513 printk(KERN_ERR "MRST SPI0: unsupported"
514 "freq: %dHz\n", speed);
515 message->status = -EIO;
519 /* clk_div doesn't support odd number */
520 clk_div = clk_get_rate(dws->clock_spim) / speed;
521 clk_div = (clk_div + 1) & 0xfffe;
523 chip->speed_hz = speed;
524 chip->clk_div = clk_div;
527 if (transfer->bits_per_word) {
528 bits = transfer->bits_per_word;
534 dws->read = (dws->read != null_reader) ?
535 u8_reader : null_reader;
536 dws->write = (dws->write != null_writer) ?
537 u8_writer : null_writer;
542 dws->read = (dws->read != null_reader) ?
543 u16_reader : null_reader;
544 dws->write = (dws->write != null_writer) ?
545 u16_writer : null_writer;
548 printk(KERN_ERR "MRST SPI0: unsupported bits:"
550 message->status = -EIO;
555 | (chip->type << SPI_FRF_OFFSET)
556 | (spi->mode << SPI_MODE_OFFSET)
557 | (chip->tmode << SPI_TMOD_OFFSET);
559 message->state = RUNNING_STATE;
562 * Adjust transfer mode if necessary. Requires platform dependent
563 * chipselect mechanism.
565 if (dws->cs_control) {
566 if (dws->rx && dws->tx)
573 cr0 &= ~(0x3 << SPI_MODE_OFFSET);
574 cr0 |= (chip->tmode << SPI_TMOD_OFFSET);
576 /* Check if current transfer is a DMA transaction */
577 dws->dma_mapped = map_dma_buffers(dws);
581 * we only need set the TXEI IRQ, as TX/RX always happen syncronizely
583 if (!dws->dma_mapped && !chip->poll_mode) {
584 int templen = dws->len / dws->n_bytes;
585 txint_level = dws->fifo_len / 2;
586 txint_level = (templen > txint_level) ? txint_level : templen;
588 imask |= SPI_INT_TXEI;
589 dws->transfer_handler = interrupt_transfer;
593 * Reprogram registers only if
594 * 1. chip select changes
595 * 2. clk_div is changed
596 * 3. control value changes
598 if (rk2818_readw(dws, SPIM_CTRLR0) != cr0 || cs_change || clk_div || imask) {
599 spi_enable_chip(dws, 0);
600 if (rk2818_readw(dws, SPIM_CTRLR0) != cr0)
601 rk2818_writew(dws, SPIM_CTRLR0, cr0);
603 spi_set_clk(dws, clk_div ? clk_div : chip->clk_div);
604 spi_chip_sel(dws, spi->chip_select);
605 /* Set the interrupt mask, for poll mode just diable all int */
606 spi_mask_intr(dws, 0xff);
608 spi_umask_intr(dws, imask);
610 rk2818_writew(dws, SPIM_TXFTLR, txint_level);
612 spi_enable_chip(dws, 1);
614 dws->prev_chip = chip;
618 dma_transfer(dws, transfer); ///cs_change);
630 static void pump_messages(struct work_struct *work)
632 struct rk2818_spi *dws =
633 container_of(work, struct rk2818_spi, pump_messages);
636 /* Lock queue and check for queue work */
637 spin_lock_irqsave(&dws->lock, flags);
638 if (list_empty(&dws->queue) || dws->run == QUEUE_STOPPED) {
640 spin_unlock_irqrestore(&dws->lock, flags);
644 /* Make sure we are not already running a message */
646 spin_unlock_irqrestore(&dws->lock, flags);
650 /* Extract head of queue */
651 dws->cur_msg = list_entry(dws->queue.next, struct spi_message, queue);
652 list_del_init(&dws->cur_msg->queue);
654 /* Initial message state*/
655 dws->cur_msg->state = START_STATE;
656 dws->cur_transfer = list_entry(dws->cur_msg->transfers.next,
659 dws->cur_chip = spi_get_ctldata(dws->cur_msg->spi);
660 dws->prev_chip = NULL; //ÿ¸öpump messageʱǿÖƸüÐÂcs dxj
662 /* Mark as busy and launch transfers */
663 tasklet_schedule(&dws->pump_transfers);
666 spin_unlock_irqrestore(&dws->lock, flags);
669 /* spi_device use this to queue in their spi_msg */
670 static int rk2818_spi_transfer(struct spi_device *spi, struct spi_message *msg)
672 struct rk2818_spi *dws = spi_master_get_devdata(spi->master);
675 spin_lock_irqsave(&dws->lock, flags);
677 if (dws->run == QUEUE_STOPPED) {
678 spin_unlock_irqrestore(&dws->lock, flags);
682 msg->actual_length = 0;
683 msg->status = -EINPROGRESS;
684 msg->state = START_STATE;
686 list_add_tail(&msg->queue, &dws->queue);
688 if (dws->run == QUEUE_RUNNING && !dws->busy) {
690 if (dws->cur_transfer || dws->cur_msg)
691 queue_work(dws->workqueue,
692 &dws->pump_messages);
694 /* If no other data transaction in air, just go */
695 spin_unlock_irqrestore(&dws->lock, flags);
696 pump_messages(&dws->pump_messages);
701 spin_unlock_irqrestore(&dws->lock, flags);
706 /* This may be called twice for each spi dev */
707 static int rk2818_spi_setup(struct spi_device *spi)
709 struct rk2818_spi_chip *chip_info = NULL;
710 struct chip_data *chip;
712 if (spi->bits_per_word != 8 && spi->bits_per_word != 16)
715 /* Only alloc on first setup */
716 chip = spi_get_ctldata(spi);
718 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
722 chip->cs_control = null_cs_control;
723 chip->enable_dma = 1; //0;
727 * Protocol drivers may change the chip settings, so...
728 * if chip_info exists, use it
730 chip_info = spi->controller_data;
732 /* chip_info doesn't always exist */
734 if (chip_info->cs_control)
735 chip->cs_control = chip_info->cs_control;
737 chip->poll_mode = chip_info->poll_mode;
738 chip->type = chip_info->type;
740 chip->rx_threshold = 0;
741 chip->tx_threshold = 0;
743 chip->enable_dma = chip_info->enable_dma;
746 if (spi->bits_per_word <= 8) {
749 chip->read = u8_reader;
750 chip->write = u8_writer;
751 } else if (spi->bits_per_word <= 16) {
754 chip->read = u16_reader;
755 chip->write = u16_writer;
757 /* Never take >16b case for MRST SPIC */
758 dev_err(&spi->dev, "invalid wordsize\n");
761 chip->bits_per_word = spi->bits_per_word;
763 if (!spi->max_speed_hz) {
764 dev_err(&spi->dev, "No max speed HZ parameter\n");
767 chip->speed_hz = spi->max_speed_hz;
769 chip->tmode = 0; /* Tx & Rx */
770 /* Default SPI mode is SCPOL = 0, SCPH = 0 */
771 chip->cr0 = (chip->bits_per_word - 1)
772 | (chip->type << SPI_FRF_OFFSET)
773 | (spi->mode << SPI_MODE_OFFSET)
774 | (chip->tmode << SPI_TMOD_OFFSET);
776 spi_set_ctldata(spi, chip);
780 static void rk2818_spi_cleanup(struct spi_device *spi)
782 struct chip_data *chip = spi_get_ctldata(spi);
786 static int __devinit init_queue(struct rk2818_spi *dws)
788 INIT_LIST_HEAD(&dws->queue);
789 spin_lock_init(&dws->lock);
791 dws->run = QUEUE_STOPPED;
794 tasklet_init(&dws->pump_transfers,
795 pump_transfers, (unsigned long)dws);
797 INIT_WORK(&dws->pump_messages, pump_messages);
798 dws->workqueue = create_singlethread_workqueue(
799 dev_name(dws->master->dev.parent));
800 if (dws->workqueue == NULL)
806 static int start_queue(struct rk2818_spi *dws)
810 spin_lock_irqsave(&dws->lock, flags);
812 if (dws->run == QUEUE_RUNNING || dws->busy) {
813 spin_unlock_irqrestore(&dws->lock, flags);
817 dws->run = QUEUE_RUNNING;
819 dws->cur_transfer = NULL;
820 dws->cur_chip = NULL;
821 dws->prev_chip = NULL;
822 spin_unlock_irqrestore(&dws->lock, flags);
824 queue_work(dws->workqueue, &dws->pump_messages);
829 static int stop_queue(struct rk2818_spi *dws)
835 spin_lock_irqsave(&dws->lock, flags);
836 dws->run = QUEUE_STOPPED;
837 while (!list_empty(&dws->queue) && dws->busy && limit--) {
838 spin_unlock_irqrestore(&dws->lock, flags);
840 spin_lock_irqsave(&dws->lock, flags);
843 if (!list_empty(&dws->queue) || dws->busy)
845 spin_unlock_irqrestore(&dws->lock, flags);
850 static int destroy_queue(struct rk2818_spi *dws)
854 status = stop_queue(dws);
857 destroy_workqueue(dws->workqueue);
861 /* Restart the controller, disable all interrupts, clean rx fifo */
862 static void spi_hw_init(struct rk2818_spi *dws)
864 spi_enable_chip(dws, 0);
865 spi_mask_intr(dws, 0xff);
866 spi_enable_chip(dws, 1);
870 * Try to detect the FIFO depth if not set by interface driver,
871 * the depth could be from 2 to 32 from HW spec
873 if (!dws->fifo_len) {
875 for (fifo = 2; fifo <= 31; fifo++) {
876 rk2818_writew(dws, SPIM_TXFTLR, fifo);
877 if (fifo != rk2818_readw(dws, SPIM_TXFTLR))
881 dws->fifo_len = (fifo == 31) ? 0 : fifo;
882 rk2818_writew(dws, SPIM_TXFTLR, 0);
886 /* cpufreq driver support */
887 #ifdef CONFIG_CPU_FREQ
889 static int rk2818_spim_cpufreq_transition(struct notifier_block *nb, unsigned long val, void *data)
891 struct rk2818_spi *info;
892 unsigned long newclk;
894 info = container_of(nb, struct rk2818_spi, freq_transition);
895 newclk = clk_get_rate(info->clock_spim);
900 static inline int rk2818_spim_cpufreq_register(struct rk2818_spi *info)
902 info->freq_transition.notifier_call = rk2818_spim_cpufreq_transition;
904 return cpufreq_register_notifier(&info->freq_transition, CPUFREQ_TRANSITION_NOTIFIER);
907 static inline void rk2818_spim_cpufreq_deregister(struct rk2818_spi *info)
909 cpufreq_unregister_notifier(&info->freq_transition, CPUFREQ_TRANSITION_NOTIFIER);
913 static inline int rk2818_spim_cpufreq_register(struct rk2818_spi *info)
918 static inline void rk2818_spim_cpufreq_deregister(struct rk2818_spi *info)
922 static int __init rk2818_spim_probe(struct platform_device *pdev)
924 struct resource *regs;
925 struct rk2818_spi *dws;
926 struct spi_master *master;
930 gpio_request(RK2818_PIN_PB0, "rk2818_spim");
931 gpio_request(RK2818_PIN_PB4, "rk2818_spim");
932 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
935 irq = platform_get_irq(pdev, 0);
938 /* setup spi core then atmel-specific driver state */
940 master = spi_alloc_master(&pdev->dev, sizeof *dws);
945 platform_set_drvdata(pdev, master);
946 dws = spi_master_get_devdata(master);
947 dws->clock_spim = clk_get(&pdev->dev, "spi");
948 clk_enable(dws->clock_spim);
949 if (IS_ERR(dws->clock_spim))
950 return PTR_ERR(dws->clock_spim);
951 dws->regs = ioremap(regs->start, (regs->end - regs->start) + 1);
953 release_mem_region(regs->start, (regs->end - regs->start) + 1);
957 dws->master = master;
958 dws->type = SSI_MOTO_SPI;
959 dws->prev_chip = NULL;
960 dws->dma_inited = 1; ///0;
961 ///dws->dma_addr = (dma_addr_t)(dws->paddr + 0x60);
962 ret = request_irq(dws->irq, rk2818_spi_irq, 0,
965 dev_err(&master->dev, "can not get IRQ\n");
966 goto err_free_master;
968 master->mode_bits = SPI_CPOL | SPI_CPHA;
969 master->bus_num = pdev->id;
970 #if defined(CONFIG_MACH_RAHO)
971 master->num_chipselect = 3; //raho ´ó°åÐèÒªÖ§³Ö3¸öƬѡ dxj
973 master->num_chipselect = 2;
975 master->cleanup = rk2818_spi_cleanup;
976 master->setup = rk2818_spi_setup;
977 master->transfer = rk2818_spi_transfer;
981 /* Initial and start queue */
982 ret = init_queue(dws);
984 dev_err(&master->dev, "problem initializing queue\n");
987 ret = start_queue(dws);
989 dev_err(&master->dev, "problem starting queue\n");
992 spi_master_set_devdata(master, dws);
993 ret = spi_register_master(master);
995 dev_err(&master->dev, "problem registering spi master\n");
996 goto err_queue_alloc;
999 ret =rk2818_spim_cpufreq_register(dws);
1001 printk(KERN_ERR"rk2818 spim failed to init cpufreq support\n");
1002 goto err_queue_alloc;
1004 printk(KERN_INFO "rk2818_spim: driver initialized\n");
1005 mrst_spi_debugfs_init(dws);
1011 spi_enable_chip(dws, 0);
1012 free_irq(dws->irq, dws);
1014 spi_master_put(master);
1020 static void __exit rk2818_spim_remove(struct platform_device *pdev)
1022 struct spi_master *master = platform_get_drvdata(pdev);
1023 struct rk2818_spi *dws = spi_master_get_devdata(master);
1028 rk2818_spim_cpufreq_deregister(dws);
1029 mrst_spi_debugfs_remove(dws);
1031 /* Remove the queue */
1032 status = destroy_queue(dws);
1034 dev_err(&dws->master->dev, "rk2818_spi_remove: workqueue will not "
1035 "complete, message memory not freed\n");
1036 clk_put(dws->clock_spim);
1037 clk_disable(dws->clock_spim);
1038 spi_enable_chip(dws, 0);
1040 spi_set_clk(dws, 0);
1041 free_irq(dws->irq, dws);
1043 /* Disconnect from the SPI framework */
1044 spi_unregister_master(dws->master);
1051 static int rk2818_spim_suspend(struct platform_device *pdev, pm_message_t mesg)
1053 struct spi_master *master = platform_get_drvdata(pdev);
1054 struct rk2818_spi *dws = spi_master_get_devdata(master);
1057 status = stop_queue(dws);
1060 clk_disable(dws->clock_spim);
1065 static int rk2818_spim_resume(struct platform_device *pdev)
1067 struct spi_master *master = platform_get_drvdata(pdev);
1068 struct rk2818_spi *dws = spi_master_get_devdata(master);
1071 clk_enable(dws->clock_spim);
1073 ret = start_queue(dws);
1075 dev_err(&dws->master->dev, "fail to start queue (%d)\n", ret);
1080 #define rk2818_spim_suspend NULL
1081 #define rk2818_spim_resume NULL
1084 static struct platform_driver rk2818_platform_spim_driver = {
1085 .remove = __exit_p(rk2818_spim_remove),
1087 .name = "rk2818_spim",
1088 .owner = THIS_MODULE,
1090 .suspend = rk2818_spim_suspend,
1091 .resume = rk2818_spim_resume,
1094 static int __init rk2818_spim_init(void)
1097 ret = platform_driver_probe(&rk2818_platform_spim_driver, rk2818_spim_probe);
1101 static void __exit rk2818_spim_exit(void)
1103 platform_driver_unregister(&rk2818_platform_spim_driver);
1106 subsys_initcall(rk2818_spim_init);
1107 module_exit(rk2818_spim_exit);
1109 MODULE_AUTHOR("lhh lhh@rock-chips.com");
1110 MODULE_DESCRIPTION("Rockchip RK2818 spim port driver");
1111 MODULE_LICENSE("GPL");;