diff options
Diffstat (limited to 'drivers/tty/serial/atmel_serial.c')
-rw-r--r-- | drivers/tty/serial/atmel_serial.c | 857 |
1 files changed, 709 insertions, 148 deletions
diff --git a/drivers/tty/serial/atmel_serial.c b/drivers/tty/serial/atmel_serial.c index 691265faebbe..d067285a2d20 100644 --- a/drivers/tty/serial/atmel_serial.c +++ b/drivers/tty/serial/atmel_serial.c | |||
@@ -39,8 +39,8 @@ | |||
39 | #include <linux/atmel_pdc.h> | 39 | #include <linux/atmel_pdc.h> |
40 | #include <linux/atmel_serial.h> | 40 | #include <linux/atmel_serial.h> |
41 | #include <linux/uaccess.h> | 41 | #include <linux/uaccess.h> |
42 | #include <linux/pinctrl/consumer.h> | ||
43 | #include <linux/platform_data/atmel.h> | 42 | #include <linux/platform_data/atmel.h> |
43 | #include <linux/timer.h> | ||
44 | 44 | ||
45 | #include <asm/io.h> | 45 | #include <asm/io.h> |
46 | #include <asm/ioctls.h> | 46 | #include <asm/ioctls.h> |
@@ -98,6 +98,7 @@ static void atmel_stop_rx(struct uart_port *port); | |||
98 | #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR) | 98 | #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR) |
99 | #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR) | 99 | #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR) |
100 | #define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR) | 100 | #define UART_PUT_TTGR(port, v) __raw_writel(v, (port)->membase + ATMEL_US_TTGR) |
101 | #define UART_GET_IP_NAME(port) __raw_readl((port)->membase + ATMEL_US_NAME) | ||
101 | 102 | ||
102 | /* PDC registers */ | 103 | /* PDC registers */ |
103 | #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR) | 104 | #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR) |
@@ -140,13 +141,25 @@ struct atmel_uart_port { | |||
140 | u32 backup_imr; /* IMR saved during suspend */ | 141 | u32 backup_imr; /* IMR saved during suspend */ |
141 | int break_active; /* break being received */ | 142 | int break_active; /* break being received */ |
142 | 143 | ||
143 | short use_dma_rx; /* enable PDC receiver */ | 144 | bool use_dma_rx; /* enable DMA receiver */ |
145 | bool use_pdc_rx; /* enable PDC receiver */ | ||
144 | short pdc_rx_idx; /* current PDC RX buffer */ | 146 | short pdc_rx_idx; /* current PDC RX buffer */ |
145 | struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */ | 147 | struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */ |
146 | 148 | ||
147 | short use_dma_tx; /* enable PDC transmitter */ | 149 | bool use_dma_tx; /* enable DMA transmitter */ |
150 | bool use_pdc_tx; /* enable PDC transmitter */ | ||
148 | struct atmel_dma_buffer pdc_tx; /* PDC transmitter */ | 151 | struct atmel_dma_buffer pdc_tx; /* PDC transmitter */ |
149 | 152 | ||
153 | spinlock_t lock_tx; /* port lock */ | ||
154 | spinlock_t lock_rx; /* port lock */ | ||
155 | struct dma_chan *chan_tx; | ||
156 | struct dma_chan *chan_rx; | ||
157 | struct dma_async_tx_descriptor *desc_tx; | ||
158 | struct dma_async_tx_descriptor *desc_rx; | ||
159 | dma_cookie_t cookie_tx; | ||
160 | dma_cookie_t cookie_rx; | ||
161 | struct scatterlist sg_tx; | ||
162 | struct scatterlist sg_rx; | ||
150 | struct tasklet_struct tasklet; | 163 | struct tasklet_struct tasklet; |
151 | unsigned int irq_status; | 164 | unsigned int irq_status; |
152 | unsigned int irq_status_prev; | 165 | unsigned int irq_status_prev; |
@@ -155,6 +168,14 @@ struct atmel_uart_port { | |||
155 | 168 | ||
156 | struct serial_rs485 rs485; /* rs485 settings */ | 169 | struct serial_rs485 rs485; /* rs485 settings */ |
157 | unsigned int tx_done_mask; | 170 | unsigned int tx_done_mask; |
171 | bool is_usart; /* usart or uart */ | ||
172 | struct timer_list uart_timer; /* uart timer */ | ||
173 | int (*prepare_rx)(struct uart_port *port); | ||
174 | int (*prepare_tx)(struct uart_port *port); | ||
175 | void (*schedule_rx)(struct uart_port *port); | ||
176 | void (*schedule_tx)(struct uart_port *port); | ||
177 | void (*release_rx)(struct uart_port *port); | ||
178 | void (*release_tx)(struct uart_port *port); | ||
158 | }; | 179 | }; |
159 | 180 | ||
160 | static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART]; | 181 | static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART]; |
@@ -181,31 +202,45 @@ to_atmel_uart_port(struct uart_port *uart) | |||
181 | } | 202 | } |
182 | 203 | ||
183 | #ifdef CONFIG_SERIAL_ATMEL_PDC | 204 | #ifdef CONFIG_SERIAL_ATMEL_PDC |
184 | static bool atmel_use_dma_rx(struct uart_port *port) | 205 | static bool atmel_use_pdc_rx(struct uart_port *port) |
185 | { | 206 | { |
186 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | 207 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
187 | 208 | ||
188 | return atmel_port->use_dma_rx; | 209 | return atmel_port->use_pdc_rx; |
189 | } | 210 | } |
190 | 211 | ||
191 | static bool atmel_use_dma_tx(struct uart_port *port) | 212 | static bool atmel_use_pdc_tx(struct uart_port *port) |
192 | { | 213 | { |
193 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | 214 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
194 | 215 | ||
195 | return atmel_port->use_dma_tx; | 216 | return atmel_port->use_pdc_tx; |
196 | } | 217 | } |
197 | #else | 218 | #else |
198 | static bool atmel_use_dma_rx(struct uart_port *port) | 219 | static bool atmel_use_pdc_rx(struct uart_port *port) |
199 | { | 220 | { |
200 | return false; | 221 | return false; |
201 | } | 222 | } |
202 | 223 | ||
203 | static bool atmel_use_dma_tx(struct uart_port *port) | 224 | static bool atmel_use_pdc_tx(struct uart_port *port) |
204 | { | 225 | { |
205 | return false; | 226 | return false; |
206 | } | 227 | } |
207 | #endif | 228 | #endif |
208 | 229 | ||
230 | static bool atmel_use_dma_tx(struct uart_port *port) | ||
231 | { | ||
232 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
233 | |||
234 | return atmel_port->use_dma_tx; | ||
235 | } | ||
236 | |||
237 | static bool atmel_use_dma_rx(struct uart_port *port) | ||
238 | { | ||
239 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
240 | |||
241 | return atmel_port->use_dma_rx; | ||
242 | } | ||
243 | |||
209 | /* Enable or disable the rs485 support */ | 244 | /* Enable or disable the rs485 support */ |
210 | void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf) | 245 | void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf) |
211 | { | 246 | { |
@@ -233,7 +268,7 @@ void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf) | |||
233 | mode |= ATMEL_US_USMODE_RS485; | 268 | mode |= ATMEL_US_USMODE_RS485; |
234 | } else { | 269 | } else { |
235 | dev_dbg(port->dev, "Setting UART to RS232\n"); | 270 | dev_dbg(port->dev, "Setting UART to RS232\n"); |
236 | if (atmel_use_dma_tx(port)) | 271 | if (atmel_use_pdc_tx(port)) |
237 | atmel_port->tx_done_mask = ATMEL_US_ENDTX | | 272 | atmel_port->tx_done_mask = ATMEL_US_ENDTX | |
238 | ATMEL_US_TXBUFE; | 273 | ATMEL_US_TXBUFE; |
239 | else | 274 | else |
@@ -345,7 +380,7 @@ static void atmel_stop_tx(struct uart_port *port) | |||
345 | { | 380 | { |
346 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | 381 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
347 | 382 | ||
348 | if (atmel_use_dma_tx(port)) { | 383 | if (atmel_use_pdc_tx(port)) { |
349 | /* disable PDC transmit */ | 384 | /* disable PDC transmit */ |
350 | UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); | 385 | UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS); |
351 | } | 386 | } |
@@ -364,7 +399,7 @@ static void atmel_start_tx(struct uart_port *port) | |||
364 | { | 399 | { |
365 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | 400 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
366 | 401 | ||
367 | if (atmel_use_dma_tx(port)) { | 402 | if (atmel_use_pdc_tx(port)) { |
368 | if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN) | 403 | if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN) |
369 | /* The transmitter is already running. Yes, we | 404 | /* The transmitter is already running. Yes, we |
370 | really need this.*/ | 405 | really need this.*/ |
@@ -390,7 +425,7 @@ static void atmel_start_rx(struct uart_port *port) | |||
390 | 425 | ||
391 | UART_PUT_CR(port, ATMEL_US_RXEN); | 426 | UART_PUT_CR(port, ATMEL_US_RXEN); |
392 | 427 | ||
393 | if (atmel_use_dma_rx(port)) { | 428 | if (atmel_use_pdc_rx(port)) { |
394 | /* enable PDC controller */ | 429 | /* enable PDC controller */ |
395 | UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | | 430 | UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | |
396 | port->read_status_mask); | 431 | port->read_status_mask); |
@@ -407,7 +442,7 @@ static void atmel_stop_rx(struct uart_port *port) | |||
407 | { | 442 | { |
408 | UART_PUT_CR(port, ATMEL_US_RXDIS); | 443 | UART_PUT_CR(port, ATMEL_US_RXDIS); |
409 | 444 | ||
410 | if (atmel_use_dma_rx(port)) { | 445 | if (atmel_use_pdc_rx(port)) { |
411 | /* disable PDC receive */ | 446 | /* disable PDC receive */ |
412 | UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS); | 447 | UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS); |
413 | UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | | 448 | UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT | |
@@ -564,6 +599,372 @@ static void atmel_tx_chars(struct uart_port *port) | |||
564 | UART_PUT_IER(port, atmel_port->tx_done_mask); | 599 | UART_PUT_IER(port, atmel_port->tx_done_mask); |
565 | } | 600 | } |
566 | 601 | ||
602 | static void atmel_complete_tx_dma(void *arg) | ||
603 | { | ||
604 | struct atmel_uart_port *atmel_port = arg; | ||
605 | struct uart_port *port = &atmel_port->uart; | ||
606 | struct circ_buf *xmit = &port->state->xmit; | ||
607 | struct dma_chan *chan = atmel_port->chan_tx; | ||
608 | unsigned long flags; | ||
609 | |||
610 | spin_lock_irqsave(&port->lock, flags); | ||
611 | |||
612 | if (chan) | ||
613 | dmaengine_terminate_all(chan); | ||
614 | xmit->tail += sg_dma_len(&atmel_port->sg_tx); | ||
615 | xmit->tail &= UART_XMIT_SIZE - 1; | ||
616 | |||
617 | port->icount.tx += sg_dma_len(&atmel_port->sg_tx); | ||
618 | |||
619 | spin_lock_irq(&atmel_port->lock_tx); | ||
620 | async_tx_ack(atmel_port->desc_tx); | ||
621 | atmel_port->cookie_tx = -EINVAL; | ||
622 | atmel_port->desc_tx = NULL; | ||
623 | spin_unlock_irq(&atmel_port->lock_tx); | ||
624 | |||
625 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) | ||
626 | uart_write_wakeup(port); | ||
627 | |||
628 | /* Do we really need this? */ | ||
629 | if (!uart_circ_empty(xmit)) | ||
630 | tasklet_schedule(&atmel_port->tasklet); | ||
631 | |||
632 | spin_unlock_irqrestore(&port->lock, flags); | ||
633 | } | ||
634 | |||
635 | static void atmel_release_tx_dma(struct uart_port *port) | ||
636 | { | ||
637 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
638 | struct dma_chan *chan = atmel_port->chan_tx; | ||
639 | |||
640 | if (chan) { | ||
641 | dmaengine_terminate_all(chan); | ||
642 | dma_release_channel(chan); | ||
643 | dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1, | ||
644 | DMA_MEM_TO_DEV); | ||
645 | } | ||
646 | |||
647 | atmel_port->desc_tx = NULL; | ||
648 | atmel_port->chan_tx = NULL; | ||
649 | atmel_port->cookie_tx = -EINVAL; | ||
650 | } | ||
651 | |||
652 | /* | ||
653 | * Called from tasklet with TXRDY interrupt is disabled. | ||
654 | */ | ||
655 | static void atmel_tx_dma(struct uart_port *port) | ||
656 | { | ||
657 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
658 | struct circ_buf *xmit = &port->state->xmit; | ||
659 | struct dma_chan *chan = atmel_port->chan_tx; | ||
660 | struct dma_async_tx_descriptor *desc; | ||
661 | struct scatterlist *sg = &atmel_port->sg_tx; | ||
662 | |||
663 | /* Make sure we have an idle channel */ | ||
664 | if (atmel_port->desc_tx != NULL) | ||
665 | return; | ||
666 | |||
667 | if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) { | ||
668 | /* | ||
669 | * DMA is idle now. | ||
670 | * Port xmit buffer is already mapped, | ||
671 | * and it is one page... Just adjust | ||
672 | * offsets and lengths. Since it is a circular buffer, | ||
673 | * we have to transmit till the end, and then the rest. | ||
674 | * Take the port lock to get a | ||
675 | * consistent xmit buffer state. | ||
676 | */ | ||
677 | sg->offset = xmit->tail & (UART_XMIT_SIZE - 1); | ||
678 | sg_dma_address(sg) = (sg_dma_address(sg) & | ||
679 | ~(UART_XMIT_SIZE - 1)) | ||
680 | + sg->offset; | ||
681 | sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head, | ||
682 | xmit->tail, | ||
683 | UART_XMIT_SIZE); | ||
684 | BUG_ON(!sg_dma_len(sg)); | ||
685 | |||
686 | desc = dmaengine_prep_slave_sg(chan, | ||
687 | sg, | ||
688 | 1, | ||
689 | DMA_MEM_TO_DEV, | ||
690 | DMA_PREP_INTERRUPT | | ||
691 | DMA_CTRL_ACK); | ||
692 | if (!desc) { | ||
693 | dev_err(port->dev, "Failed to send via dma!\n"); | ||
694 | return; | ||
695 | } | ||
696 | |||
697 | dma_sync_sg_for_device(port->dev, sg, 1, DMA_MEM_TO_DEV); | ||
698 | |||
699 | atmel_port->desc_tx = desc; | ||
700 | desc->callback = atmel_complete_tx_dma; | ||
701 | desc->callback_param = atmel_port; | ||
702 | atmel_port->cookie_tx = dmaengine_submit(desc); | ||
703 | |||
704 | } else { | ||
705 | if (atmel_port->rs485.flags & SER_RS485_ENABLED) { | ||
706 | /* DMA done, stop TX, start RX for RS485 */ | ||
707 | atmel_start_rx(port); | ||
708 | } | ||
709 | } | ||
710 | |||
711 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) | ||
712 | uart_write_wakeup(port); | ||
713 | } | ||
714 | |||
715 | static int atmel_prepare_tx_dma(struct uart_port *port) | ||
716 | { | ||
717 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
718 | dma_cap_mask_t mask; | ||
719 | struct dma_slave_config config; | ||
720 | int ret, nent; | ||
721 | |||
722 | dma_cap_zero(mask); | ||
723 | dma_cap_set(DMA_SLAVE, mask); | ||
724 | |||
725 | atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx"); | ||
726 | if (atmel_port->chan_tx == NULL) | ||
727 | goto chan_err; | ||
728 | dev_info(port->dev, "using %s for tx DMA transfers\n", | ||
729 | dma_chan_name(atmel_port->chan_tx)); | ||
730 | |||
731 | spin_lock_init(&atmel_port->lock_tx); | ||
732 | sg_init_table(&atmel_port->sg_tx, 1); | ||
733 | /* UART circular tx buffer is an aligned page. */ | ||
734 | BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK); | ||
735 | sg_set_page(&atmel_port->sg_tx, | ||
736 | virt_to_page(port->state->xmit.buf), | ||
737 | UART_XMIT_SIZE, | ||
738 | (int)port->state->xmit.buf & ~PAGE_MASK); | ||
739 | nent = dma_map_sg(port->dev, | ||
740 | &atmel_port->sg_tx, | ||
741 | 1, | ||
742 | DMA_MEM_TO_DEV); | ||
743 | |||
744 | if (!nent) { | ||
745 | dev_dbg(port->dev, "need to release resource of dma\n"); | ||
746 | goto chan_err; | ||
747 | } else { | ||
748 | dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__, | ||
749 | sg_dma_len(&atmel_port->sg_tx), | ||
750 | port->state->xmit.buf, | ||
751 | sg_dma_address(&atmel_port->sg_tx)); | ||
752 | } | ||
753 | |||
754 | /* Configure the slave DMA */ | ||
755 | memset(&config, 0, sizeof(config)); | ||
756 | config.direction = DMA_MEM_TO_DEV; | ||
757 | config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; | ||
758 | config.dst_addr = port->mapbase + ATMEL_US_THR; | ||
759 | |||
760 | ret = dmaengine_device_control(atmel_port->chan_tx, | ||
761 | DMA_SLAVE_CONFIG, | ||
762 | (unsigned long)&config); | ||
763 | if (ret) { | ||
764 | dev_err(port->dev, "DMA tx slave configuration failed\n"); | ||
765 | goto chan_err; | ||
766 | } | ||
767 | |||
768 | return 0; | ||
769 | |||
770 | chan_err: | ||
771 | dev_err(port->dev, "TX channel not available, switch to pio\n"); | ||
772 | atmel_port->use_dma_tx = 0; | ||
773 | if (atmel_port->chan_tx) | ||
774 | atmel_release_tx_dma(port); | ||
775 | return -EINVAL; | ||
776 | } | ||
777 | |||
778 | static void atmel_flip_buffer_rx_dma(struct uart_port *port, | ||
779 | char *buf, size_t count) | ||
780 | { | ||
781 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
782 | struct tty_port *tport = &port->state->port; | ||
783 | |||
784 | dma_sync_sg_for_cpu(port->dev, | ||
785 | &atmel_port->sg_rx, | ||
786 | 1, | ||
787 | DMA_DEV_TO_MEM); | ||
788 | |||
789 | tty_insert_flip_string(tport, buf, count); | ||
790 | |||
791 | dma_sync_sg_for_device(port->dev, | ||
792 | &atmel_port->sg_rx, | ||
793 | 1, | ||
794 | DMA_DEV_TO_MEM); | ||
795 | /* | ||
796 | * Drop the lock here since it might end up calling | ||
797 | * uart_start(), which takes the lock. | ||
798 | */ | ||
799 | spin_unlock(&port->lock); | ||
800 | tty_flip_buffer_push(tport); | ||
801 | spin_lock(&port->lock); | ||
802 | } | ||
803 | |||
804 | static void atmel_complete_rx_dma(void *arg) | ||
805 | { | ||
806 | struct uart_port *port = arg; | ||
807 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
808 | |||
809 | tasklet_schedule(&atmel_port->tasklet); | ||
810 | } | ||
811 | |||
812 | static void atmel_release_rx_dma(struct uart_port *port) | ||
813 | { | ||
814 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
815 | struct dma_chan *chan = atmel_port->chan_rx; | ||
816 | |||
817 | if (chan) { | ||
818 | dmaengine_terminate_all(chan); | ||
819 | dma_release_channel(chan); | ||
820 | dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1, | ||
821 | DMA_DEV_TO_MEM); | ||
822 | } | ||
823 | |||
824 | atmel_port->desc_rx = NULL; | ||
825 | atmel_port->chan_rx = NULL; | ||
826 | atmel_port->cookie_rx = -EINVAL; | ||
827 | |||
828 | if (!atmel_port->is_usart) | ||
829 | del_timer_sync(&atmel_port->uart_timer); | ||
830 | } | ||
831 | |||
832 | static void atmel_rx_from_dma(struct uart_port *port) | ||
833 | { | ||
834 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
835 | struct circ_buf *ring = &atmel_port->rx_ring; | ||
836 | struct dma_chan *chan = atmel_port->chan_rx; | ||
837 | struct dma_tx_state state; | ||
838 | enum dma_status dmastat; | ||
839 | size_t pending, count; | ||
840 | |||
841 | |||
842 | /* Reset the UART timeout early so that we don't miss one */ | ||
843 | UART_PUT_CR(port, ATMEL_US_STTTO); | ||
844 | dmastat = dmaengine_tx_status(chan, | ||
845 | atmel_port->cookie_rx, | ||
846 | &state); | ||
847 | /* Restart a new tasklet if DMA status is error */ | ||
848 | if (dmastat == DMA_ERROR) { | ||
849 | dev_dbg(port->dev, "Get residue error, restart tasklet\n"); | ||
850 | UART_PUT_IER(port, ATMEL_US_TIMEOUT); | ||
851 | tasklet_schedule(&atmel_port->tasklet); | ||
852 | return; | ||
853 | } | ||
854 | /* current transfer size should no larger than dma buffer */ | ||
855 | pending = sg_dma_len(&atmel_port->sg_rx) - state.residue; | ||
856 | BUG_ON(pending > sg_dma_len(&atmel_port->sg_rx)); | ||
857 | |||
858 | /* | ||
859 | * This will take the chars we have so far, | ||
860 | * ring->head will record the transfer size, only new bytes come | ||
861 | * will insert into the framework. | ||
862 | */ | ||
863 | if (pending > ring->head) { | ||
864 | count = pending - ring->head; | ||
865 | |||
866 | atmel_flip_buffer_rx_dma(port, ring->buf + ring->head, count); | ||
867 | |||
868 | ring->head += count; | ||
869 | if (ring->head == sg_dma_len(&atmel_port->sg_rx)) | ||
870 | ring->head = 0; | ||
871 | |||
872 | port->icount.rx += count; | ||
873 | } | ||
874 | |||
875 | UART_PUT_IER(port, ATMEL_US_TIMEOUT); | ||
876 | } | ||
877 | |||
878 | static int atmel_prepare_rx_dma(struct uart_port *port) | ||
879 | { | ||
880 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
881 | struct dma_async_tx_descriptor *desc; | ||
882 | dma_cap_mask_t mask; | ||
883 | struct dma_slave_config config; | ||
884 | struct circ_buf *ring; | ||
885 | int ret, nent; | ||
886 | |||
887 | ring = &atmel_port->rx_ring; | ||
888 | |||
889 | dma_cap_zero(mask); | ||
890 | dma_cap_set(DMA_CYCLIC, mask); | ||
891 | |||
892 | atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx"); | ||
893 | if (atmel_port->chan_rx == NULL) | ||
894 | goto chan_err; | ||
895 | dev_info(port->dev, "using %s for rx DMA transfers\n", | ||
896 | dma_chan_name(atmel_port->chan_rx)); | ||
897 | |||
898 | spin_lock_init(&atmel_port->lock_rx); | ||
899 | sg_init_table(&atmel_port->sg_rx, 1); | ||
900 | /* UART circular rx buffer is an aligned page. */ | ||
901 | BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK); | ||
902 | sg_set_page(&atmel_port->sg_rx, | ||
903 | virt_to_page(ring->buf), | ||
904 | ATMEL_SERIAL_RINGSIZE, | ||
905 | (int)ring->buf & ~PAGE_MASK); | ||
906 | nent = dma_map_sg(port->dev, | ||
907 | &atmel_port->sg_rx, | ||
908 | 1, | ||
909 | DMA_DEV_TO_MEM); | ||
910 | |||
911 | if (!nent) { | ||
912 | dev_dbg(port->dev, "need to release resource of dma\n"); | ||
913 | goto chan_err; | ||
914 | } else { | ||
915 | dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__, | ||
916 | sg_dma_len(&atmel_port->sg_rx), | ||
917 | ring->buf, | ||
918 | sg_dma_address(&atmel_port->sg_rx)); | ||
919 | } | ||
920 | |||
921 | /* Configure the slave DMA */ | ||
922 | memset(&config, 0, sizeof(config)); | ||
923 | config.direction = DMA_DEV_TO_MEM; | ||
924 | config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; | ||
925 | config.src_addr = port->mapbase + ATMEL_US_RHR; | ||
926 | |||
927 | ret = dmaengine_device_control(atmel_port->chan_rx, | ||
928 | DMA_SLAVE_CONFIG, | ||
929 | (unsigned long)&config); | ||
930 | if (ret) { | ||
931 | dev_err(port->dev, "DMA rx slave configuration failed\n"); | ||
932 | goto chan_err; | ||
933 | } | ||
934 | /* | ||
935 | * Prepare a cyclic dma transfer, assign 2 descriptors, | ||
936 | * each one is half ring buffer size | ||
937 | */ | ||
938 | desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx, | ||
939 | sg_dma_address(&atmel_port->sg_rx), | ||
940 | sg_dma_len(&atmel_port->sg_rx), | ||
941 | sg_dma_len(&atmel_port->sg_rx)/2, | ||
942 | DMA_DEV_TO_MEM, | ||
943 | DMA_PREP_INTERRUPT); | ||
944 | desc->callback = atmel_complete_rx_dma; | ||
945 | desc->callback_param = port; | ||
946 | atmel_port->desc_rx = desc; | ||
947 | atmel_port->cookie_rx = dmaengine_submit(desc); | ||
948 | |||
949 | return 0; | ||
950 | |||
951 | chan_err: | ||
952 | dev_err(port->dev, "RX channel not available, switch to pio\n"); | ||
953 | atmel_port->use_dma_rx = 0; | ||
954 | if (atmel_port->chan_rx) | ||
955 | atmel_release_rx_dma(port); | ||
956 | return -EINVAL; | ||
957 | } | ||
958 | |||
959 | static void atmel_uart_timer_callback(unsigned long data) | ||
960 | { | ||
961 | struct uart_port *port = (void *)data; | ||
962 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
963 | |||
964 | tasklet_schedule(&atmel_port->tasklet); | ||
965 | mod_timer(&atmel_port->uart_timer, jiffies + uart_poll_timeout(port)); | ||
966 | } | ||
967 | |||
567 | /* | 968 | /* |
568 | * receive interrupt handler. | 969 | * receive interrupt handler. |
569 | */ | 970 | */ |
@@ -572,7 +973,7 @@ atmel_handle_receive(struct uart_port *port, unsigned int pending) | |||
572 | { | 973 | { |
573 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | 974 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
574 | 975 | ||
575 | if (atmel_use_dma_rx(port)) { | 976 | if (atmel_use_pdc_rx(port)) { |
576 | /* | 977 | /* |
577 | * PDC receive. Just schedule the tasklet and let it | 978 | * PDC receive. Just schedule the tasklet and let it |
578 | * figure out the details. | 979 | * figure out the details. |
@@ -591,6 +992,13 @@ atmel_handle_receive(struct uart_port *port, unsigned int pending) | |||
591 | atmel_pdc_rxerr(port, pending); | 992 | atmel_pdc_rxerr(port, pending); |
592 | } | 993 | } |
593 | 994 | ||
995 | if (atmel_use_dma_rx(port)) { | ||
996 | if (pending & ATMEL_US_TIMEOUT) { | ||
997 | UART_PUT_IDR(port, ATMEL_US_TIMEOUT); | ||
998 | tasklet_schedule(&atmel_port->tasklet); | ||
999 | } | ||
1000 | } | ||
1001 | |||
594 | /* Interrupt receive */ | 1002 | /* Interrupt receive */ |
595 | if (pending & ATMEL_US_RXRDY) | 1003 | if (pending & ATMEL_US_RXRDY) |
596 | atmel_rx_chars(port); | 1004 | atmel_rx_chars(port); |
@@ -658,10 +1066,21 @@ static irqreturn_t atmel_interrupt(int irq, void *dev_id) | |||
658 | return pass_counter ? IRQ_HANDLED : IRQ_NONE; | 1066 | return pass_counter ? IRQ_HANDLED : IRQ_NONE; |
659 | } | 1067 | } |
660 | 1068 | ||
1069 | static void atmel_release_tx_pdc(struct uart_port *port) | ||
1070 | { | ||
1071 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
1072 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; | ||
1073 | |||
1074 | dma_unmap_single(port->dev, | ||
1075 | pdc->dma_addr, | ||
1076 | pdc->dma_size, | ||
1077 | DMA_TO_DEVICE); | ||
1078 | } | ||
1079 | |||
661 | /* | 1080 | /* |
662 | * Called from tasklet with ENDTX and TXBUFE interrupts disabled. | 1081 | * Called from tasklet with ENDTX and TXBUFE interrupts disabled. |
663 | */ | 1082 | */ |
664 | static void atmel_tx_dma(struct uart_port *port) | 1083 | static void atmel_tx_pdc(struct uart_port *port) |
665 | { | 1084 | { |
666 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | 1085 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
667 | struct circ_buf *xmit = &port->state->xmit; | 1086 | struct circ_buf *xmit = &port->state->xmit; |
@@ -710,6 +1129,23 @@ static void atmel_tx_dma(struct uart_port *port) | |||
710 | uart_write_wakeup(port); | 1129 | uart_write_wakeup(port); |
711 | } | 1130 | } |
712 | 1131 | ||
1132 | static int atmel_prepare_tx_pdc(struct uart_port *port) | ||
1133 | { | ||
1134 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
1135 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; | ||
1136 | struct circ_buf *xmit = &port->state->xmit; | ||
1137 | |||
1138 | pdc->buf = xmit->buf; | ||
1139 | pdc->dma_addr = dma_map_single(port->dev, | ||
1140 | pdc->buf, | ||
1141 | UART_XMIT_SIZE, | ||
1142 | DMA_TO_DEVICE); | ||
1143 | pdc->dma_size = UART_XMIT_SIZE; | ||
1144 | pdc->ofs = 0; | ||
1145 | |||
1146 | return 0; | ||
1147 | } | ||
1148 | |||
713 | static void atmel_rx_from_ring(struct uart_port *port) | 1149 | static void atmel_rx_from_ring(struct uart_port *port) |
714 | { | 1150 | { |
715 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | 1151 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
@@ -778,7 +1214,26 @@ static void atmel_rx_from_ring(struct uart_port *port) | |||
778 | spin_lock(&port->lock); | 1214 | spin_lock(&port->lock); |
779 | } | 1215 | } |
780 | 1216 | ||
781 | static void atmel_rx_from_dma(struct uart_port *port) | 1217 | static void atmel_release_rx_pdc(struct uart_port *port) |
1218 | { | ||
1219 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
1220 | int i; | ||
1221 | |||
1222 | for (i = 0; i < 2; i++) { | ||
1223 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; | ||
1224 | |||
1225 | dma_unmap_single(port->dev, | ||
1226 | pdc->dma_addr, | ||
1227 | pdc->dma_size, | ||
1228 | DMA_FROM_DEVICE); | ||
1229 | kfree(pdc->buf); | ||
1230 | } | ||
1231 | |||
1232 | if (!atmel_port->is_usart) | ||
1233 | del_timer_sync(&atmel_port->uart_timer); | ||
1234 | } | ||
1235 | |||
1236 | static void atmel_rx_from_pdc(struct uart_port *port) | ||
782 | { | 1237 | { |
783 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | 1238 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
784 | struct tty_port *tport = &port->state->port; | 1239 | struct tty_port *tport = &port->state->port; |
@@ -855,6 +1310,45 @@ static void atmel_rx_from_dma(struct uart_port *port) | |||
855 | UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); | 1310 | UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); |
856 | } | 1311 | } |
857 | 1312 | ||
1313 | static int atmel_prepare_rx_pdc(struct uart_port *port) | ||
1314 | { | ||
1315 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
1316 | int i; | ||
1317 | |||
1318 | for (i = 0; i < 2; i++) { | ||
1319 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; | ||
1320 | |||
1321 | pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL); | ||
1322 | if (pdc->buf == NULL) { | ||
1323 | if (i != 0) { | ||
1324 | dma_unmap_single(port->dev, | ||
1325 | atmel_port->pdc_rx[0].dma_addr, | ||
1326 | PDC_BUFFER_SIZE, | ||
1327 | DMA_FROM_DEVICE); | ||
1328 | kfree(atmel_port->pdc_rx[0].buf); | ||
1329 | } | ||
1330 | atmel_port->use_pdc_rx = 0; | ||
1331 | return -ENOMEM; | ||
1332 | } | ||
1333 | pdc->dma_addr = dma_map_single(port->dev, | ||
1334 | pdc->buf, | ||
1335 | PDC_BUFFER_SIZE, | ||
1336 | DMA_FROM_DEVICE); | ||
1337 | pdc->dma_size = PDC_BUFFER_SIZE; | ||
1338 | pdc->ofs = 0; | ||
1339 | } | ||
1340 | |||
1341 | atmel_port->pdc_rx_idx = 0; | ||
1342 | |||
1343 | UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr); | ||
1344 | UART_PUT_RCR(port, PDC_BUFFER_SIZE); | ||
1345 | |||
1346 | UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr); | ||
1347 | UART_PUT_RNCR(port, PDC_BUFFER_SIZE); | ||
1348 | |||
1349 | return 0; | ||
1350 | } | ||
1351 | |||
858 | /* | 1352 | /* |
859 | * tasklet handling tty stuff outside the interrupt handler. | 1353 | * tasklet handling tty stuff outside the interrupt handler. |
860 | */ | 1354 | */ |
@@ -868,10 +1362,7 @@ static void atmel_tasklet_func(unsigned long data) | |||
868 | /* The interrupt handler does not take the lock */ | 1362 | /* The interrupt handler does not take the lock */ |
869 | spin_lock(&port->lock); | 1363 | spin_lock(&port->lock); |
870 | 1364 | ||
871 | if (atmel_use_dma_tx(port)) | 1365 | atmel_port->schedule_tx(port); |
872 | atmel_tx_dma(port); | ||
873 | else | ||
874 | atmel_tx_chars(port); | ||
875 | 1366 | ||
876 | status = atmel_port->irq_status; | 1367 | status = atmel_port->irq_status; |
877 | status_change = status ^ atmel_port->irq_status_prev; | 1368 | status_change = status ^ atmel_port->irq_status_prev; |
@@ -893,19 +1384,152 @@ static void atmel_tasklet_func(unsigned long data) | |||
893 | atmel_port->irq_status_prev = status; | 1384 | atmel_port->irq_status_prev = status; |
894 | } | 1385 | } |
895 | 1386 | ||
896 | if (atmel_use_dma_rx(port)) | 1387 | atmel_port->schedule_rx(port); |
897 | atmel_rx_from_dma(port); | ||
898 | else | ||
899 | atmel_rx_from_ring(port); | ||
900 | 1388 | ||
901 | spin_unlock(&port->lock); | 1389 | spin_unlock(&port->lock); |
902 | } | 1390 | } |
903 | 1391 | ||
1392 | static int atmel_init_property(struct atmel_uart_port *atmel_port, | ||
1393 | struct platform_device *pdev) | ||
1394 | { | ||
1395 | struct device_node *np = pdev->dev.of_node; | ||
1396 | struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); | ||
1397 | |||
1398 | if (np) { | ||
1399 | /* DMA/PDC usage specification */ | ||
1400 | if (of_get_property(np, "atmel,use-dma-rx", NULL)) { | ||
1401 | if (of_get_property(np, "dmas", NULL)) { | ||
1402 | atmel_port->use_dma_rx = true; | ||
1403 | atmel_port->use_pdc_rx = false; | ||
1404 | } else { | ||
1405 | atmel_port->use_dma_rx = false; | ||
1406 | atmel_port->use_pdc_rx = true; | ||
1407 | } | ||
1408 | } else { | ||
1409 | atmel_port->use_dma_rx = false; | ||
1410 | atmel_port->use_pdc_rx = false; | ||
1411 | } | ||
1412 | |||
1413 | if (of_get_property(np, "atmel,use-dma-tx", NULL)) { | ||
1414 | if (of_get_property(np, "dmas", NULL)) { | ||
1415 | atmel_port->use_dma_tx = true; | ||
1416 | atmel_port->use_pdc_tx = false; | ||
1417 | } else { | ||
1418 | atmel_port->use_dma_tx = false; | ||
1419 | atmel_port->use_pdc_tx = true; | ||
1420 | } | ||
1421 | } else { | ||
1422 | atmel_port->use_dma_tx = false; | ||
1423 | atmel_port->use_pdc_tx = false; | ||
1424 | } | ||
1425 | |||
1426 | } else { | ||
1427 | atmel_port->use_pdc_rx = pdata->use_dma_rx; | ||
1428 | atmel_port->use_pdc_tx = pdata->use_dma_tx; | ||
1429 | atmel_port->use_dma_rx = false; | ||
1430 | atmel_port->use_dma_tx = false; | ||
1431 | } | ||
1432 | |||
1433 | return 0; | ||
1434 | } | ||
1435 | |||
1436 | static void atmel_init_rs485(struct atmel_uart_port *atmel_port, | ||
1437 | struct platform_device *pdev) | ||
1438 | { | ||
1439 | struct device_node *np = pdev->dev.of_node; | ||
1440 | struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); | ||
1441 | |||
1442 | if (np) { | ||
1443 | u32 rs485_delay[2]; | ||
1444 | /* rs485 properties */ | ||
1445 | if (of_property_read_u32_array(np, "rs485-rts-delay", | ||
1446 | rs485_delay, 2) == 0) { | ||
1447 | struct serial_rs485 *rs485conf = &atmel_port->rs485; | ||
1448 | |||
1449 | rs485conf->delay_rts_before_send = rs485_delay[0]; | ||
1450 | rs485conf->delay_rts_after_send = rs485_delay[1]; | ||
1451 | rs485conf->flags = 0; | ||
1452 | |||
1453 | if (of_get_property(np, "rs485-rx-during-tx", NULL)) | ||
1454 | rs485conf->flags |= SER_RS485_RX_DURING_TX; | ||
1455 | |||
1456 | if (of_get_property(np, "linux,rs485-enabled-at-boot-time", | ||
1457 | NULL)) | ||
1458 | rs485conf->flags |= SER_RS485_ENABLED; | ||
1459 | } | ||
1460 | } else { | ||
1461 | atmel_port->rs485 = pdata->rs485; | ||
1462 | } | ||
1463 | |||
1464 | } | ||
1465 | |||
1466 | static void atmel_set_ops(struct uart_port *port) | ||
1467 | { | ||
1468 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
1469 | |||
1470 | if (atmel_use_dma_rx(port)) { | ||
1471 | atmel_port->prepare_rx = &atmel_prepare_rx_dma; | ||
1472 | atmel_port->schedule_rx = &atmel_rx_from_dma; | ||
1473 | atmel_port->release_rx = &atmel_release_rx_dma; | ||
1474 | } else if (atmel_use_pdc_rx(port)) { | ||
1475 | atmel_port->prepare_rx = &atmel_prepare_rx_pdc; | ||
1476 | atmel_port->schedule_rx = &atmel_rx_from_pdc; | ||
1477 | atmel_port->release_rx = &atmel_release_rx_pdc; | ||
1478 | } else { | ||
1479 | atmel_port->prepare_rx = NULL; | ||
1480 | atmel_port->schedule_rx = &atmel_rx_from_ring; | ||
1481 | atmel_port->release_rx = NULL; | ||
1482 | } | ||
1483 | |||
1484 | if (atmel_use_dma_tx(port)) { | ||
1485 | atmel_port->prepare_tx = &atmel_prepare_tx_dma; | ||
1486 | atmel_port->schedule_tx = &atmel_tx_dma; | ||
1487 | atmel_port->release_tx = &atmel_release_tx_dma; | ||
1488 | } else if (atmel_use_pdc_tx(port)) { | ||
1489 | atmel_port->prepare_tx = &atmel_prepare_tx_pdc; | ||
1490 | atmel_port->schedule_tx = &atmel_tx_pdc; | ||
1491 | atmel_port->release_tx = &atmel_release_tx_pdc; | ||
1492 | } else { | ||
1493 | atmel_port->prepare_tx = NULL; | ||
1494 | atmel_port->schedule_tx = &atmel_tx_chars; | ||
1495 | atmel_port->release_tx = NULL; | ||
1496 | } | ||
1497 | } | ||
1498 | |||
1499 | /* | ||
1500 | * Get ip name usart or uart | ||
1501 | */ | ||
1502 | static int atmel_get_ip_name(struct uart_port *port) | ||
1503 | { | ||
1504 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | ||
1505 | int name = UART_GET_IP_NAME(port); | ||
1506 | int usart, uart; | ||
1507 | /* usart and uart ascii */ | ||
1508 | usart = 0x55534152; | ||
1509 | uart = 0x44424755; | ||
1510 | |||
1511 | atmel_port->is_usart = false; | ||
1512 | |||
1513 | if (name == usart) { | ||
1514 | dev_dbg(port->dev, "This is usart\n"); | ||
1515 | atmel_port->is_usart = true; | ||
1516 | } else if (name == uart) { | ||
1517 | dev_dbg(port->dev, "This is uart\n"); | ||
1518 | atmel_port->is_usart = false; | ||
1519 | } else { | ||
1520 | dev_err(port->dev, "Not supported ip name, set to uart\n"); | ||
1521 | return -EINVAL; | ||
1522 | } | ||
1523 | |||
1524 | return 0; | ||
1525 | } | ||
1526 | |||
904 | /* | 1527 | /* |
905 | * Perform initialization and enable port for reception | 1528 | * Perform initialization and enable port for reception |
906 | */ | 1529 | */ |
907 | static int atmel_startup(struct uart_port *port) | 1530 | static int atmel_startup(struct uart_port *port) |
908 | { | 1531 | { |
1532 | struct platform_device *pdev = to_platform_device(port->dev); | ||
909 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | 1533 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
910 | struct tty_struct *tty = port->state->port.tty; | 1534 | struct tty_struct *tty = port->state->port.tty; |
911 | int retval; | 1535 | int retval; |
@@ -930,53 +1554,19 @@ static int atmel_startup(struct uart_port *port) | |||
930 | /* | 1554 | /* |
931 | * Initialize DMA (if necessary) | 1555 | * Initialize DMA (if necessary) |
932 | */ | 1556 | */ |
933 | if (atmel_use_dma_rx(port)) { | 1557 | atmel_init_property(atmel_port, pdev); |
934 | int i; | ||
935 | |||
936 | for (i = 0; i < 2; i++) { | ||
937 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; | ||
938 | |||
939 | pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL); | ||
940 | if (pdc->buf == NULL) { | ||
941 | if (i != 0) { | ||
942 | dma_unmap_single(port->dev, | ||
943 | atmel_port->pdc_rx[0].dma_addr, | ||
944 | PDC_BUFFER_SIZE, | ||
945 | DMA_FROM_DEVICE); | ||
946 | kfree(atmel_port->pdc_rx[0].buf); | ||
947 | } | ||
948 | free_irq(port->irq, port); | ||
949 | return -ENOMEM; | ||
950 | } | ||
951 | pdc->dma_addr = dma_map_single(port->dev, | ||
952 | pdc->buf, | ||
953 | PDC_BUFFER_SIZE, | ||
954 | DMA_FROM_DEVICE); | ||
955 | pdc->dma_size = PDC_BUFFER_SIZE; | ||
956 | pdc->ofs = 0; | ||
957 | } | ||
958 | 1558 | ||
959 | atmel_port->pdc_rx_idx = 0; | 1559 | if (atmel_port->prepare_rx) { |
960 | 1560 | retval = atmel_port->prepare_rx(port); | |
961 | UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr); | 1561 | if (retval < 0) |
962 | UART_PUT_RCR(port, PDC_BUFFER_SIZE); | 1562 | atmel_set_ops(port); |
963 | |||
964 | UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr); | ||
965 | UART_PUT_RNCR(port, PDC_BUFFER_SIZE); | ||
966 | } | 1563 | } |
967 | if (atmel_use_dma_tx(port)) { | ||
968 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; | ||
969 | struct circ_buf *xmit = &port->state->xmit; | ||
970 | 1564 | ||
971 | pdc->buf = xmit->buf; | 1565 | if (atmel_port->prepare_tx) { |
972 | pdc->dma_addr = dma_map_single(port->dev, | 1566 | retval = atmel_port->prepare_tx(port); |
973 | pdc->buf, | 1567 | if (retval < 0) |
974 | UART_XMIT_SIZE, | 1568 | atmel_set_ops(port); |
975 | DMA_TO_DEVICE); | ||
976 | pdc->dma_size = UART_XMIT_SIZE; | ||
977 | pdc->ofs = 0; | ||
978 | } | 1569 | } |
979 | |||
980 | /* | 1570 | /* |
981 | * If there is a specific "open" function (to register | 1571 | * If there is a specific "open" function (to register |
982 | * control line interrupts) | 1572 | * control line interrupts) |
@@ -1000,14 +1590,38 @@ static int atmel_startup(struct uart_port *port) | |||
1000 | /* enable xmit & rcvr */ | 1590 | /* enable xmit & rcvr */ |
1001 | UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); | 1591 | UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); |
1002 | 1592 | ||
1003 | if (atmel_use_dma_rx(port)) { | 1593 | if (atmel_use_pdc_rx(port)) { |
1004 | /* set UART timeout */ | 1594 | /* set UART timeout */ |
1005 | UART_PUT_RTOR(port, PDC_RX_TIMEOUT); | 1595 | if (!atmel_port->is_usart) { |
1006 | UART_PUT_CR(port, ATMEL_US_STTTO); | 1596 | setup_timer(&atmel_port->uart_timer, |
1007 | 1597 | atmel_uart_timer_callback, | |
1008 | UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); | 1598 | (unsigned long)port); |
1599 | mod_timer(&atmel_port->uart_timer, | ||
1600 | jiffies + uart_poll_timeout(port)); | ||
1601 | /* set USART timeout */ | ||
1602 | } else { | ||
1603 | UART_PUT_RTOR(port, PDC_RX_TIMEOUT); | ||
1604 | UART_PUT_CR(port, ATMEL_US_STTTO); | ||
1605 | |||
1606 | UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT); | ||
1607 | } | ||
1009 | /* enable PDC controller */ | 1608 | /* enable PDC controller */ |
1010 | UART_PUT_PTCR(port, ATMEL_PDC_RXTEN); | 1609 | UART_PUT_PTCR(port, ATMEL_PDC_RXTEN); |
1610 | } else if (atmel_use_dma_rx(port)) { | ||
1611 | /* set UART timeout */ | ||
1612 | if (!atmel_port->is_usart) { | ||
1613 | setup_timer(&atmel_port->uart_timer, | ||
1614 | atmel_uart_timer_callback, | ||
1615 | (unsigned long)port); | ||
1616 | mod_timer(&atmel_port->uart_timer, | ||
1617 | jiffies + uart_poll_timeout(port)); | ||
1618 | /* set USART timeout */ | ||
1619 | } else { | ||
1620 | UART_PUT_RTOR(port, PDC_RX_TIMEOUT); | ||
1621 | UART_PUT_CR(port, ATMEL_US_STTTO); | ||
1622 | |||
1623 | UART_PUT_IER(port, ATMEL_US_TIMEOUT); | ||
1624 | } | ||
1011 | } else { | 1625 | } else { |
1012 | /* enable receive only */ | 1626 | /* enable receive only */ |
1013 | UART_PUT_IER(port, ATMEL_US_RXRDY); | 1627 | UART_PUT_IER(port, ATMEL_US_RXRDY); |
@@ -1031,27 +1645,10 @@ static void atmel_shutdown(struct uart_port *port) | |||
1031 | /* | 1645 | /* |
1032 | * Shut-down the DMA. | 1646 | * Shut-down the DMA. |
1033 | */ | 1647 | */ |
1034 | if (atmel_use_dma_rx(port)) { | 1648 | if (atmel_port->release_rx) |
1035 | int i; | 1649 | atmel_port->release_rx(port); |
1036 | 1650 | if (atmel_port->release_tx) | |
1037 | for (i = 0; i < 2; i++) { | 1651 | atmel_port->release_tx(port); |
1038 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i]; | ||
1039 | |||
1040 | dma_unmap_single(port->dev, | ||
1041 | pdc->dma_addr, | ||
1042 | pdc->dma_size, | ||
1043 | DMA_FROM_DEVICE); | ||
1044 | kfree(pdc->buf); | ||
1045 | } | ||
1046 | } | ||
1047 | if (atmel_use_dma_tx(port)) { | ||
1048 | struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx; | ||
1049 | |||
1050 | dma_unmap_single(port->dev, | ||
1051 | pdc->dma_addr, | ||
1052 | pdc->dma_size, | ||
1053 | DMA_TO_DEVICE); | ||
1054 | } | ||
1055 | 1652 | ||
1056 | /* | 1653 | /* |
1057 | * Disable all interrupts, port and break condition. | 1654 | * Disable all interrupts, port and break condition. |
@@ -1080,7 +1677,7 @@ static void atmel_flush_buffer(struct uart_port *port) | |||
1080 | { | 1677 | { |
1081 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); | 1678 | struct atmel_uart_port *atmel_port = to_atmel_uart_port(port); |
1082 | 1679 | ||
1083 | if (atmel_use_dma_tx(port)) { | 1680 | if (atmel_use_pdc_tx(port)) { |
1084 | UART_PUT_TCR(port, 0); | 1681 | UART_PUT_TCR(port, 0); |
1085 | atmel_port->pdc_tx.ofs = 0; | 1682 | atmel_port->pdc_tx.ofs = 0; |
1086 | } | 1683 | } |
@@ -1193,7 +1790,7 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios, | |||
1193 | if (termios->c_iflag & (BRKINT | PARMRK)) | 1790 | if (termios->c_iflag & (BRKINT | PARMRK)) |
1194 | port->read_status_mask |= ATMEL_US_RXBRK; | 1791 | port->read_status_mask |= ATMEL_US_RXBRK; |
1195 | 1792 | ||
1196 | if (atmel_use_dma_rx(port)) | 1793 | if (atmel_use_pdc_rx(port)) |
1197 | /* need to enable error interrupts */ | 1794 | /* need to enable error interrupts */ |
1198 | UART_PUT_IER(port, port->read_status_mask); | 1795 | UART_PUT_IER(port, port->read_status_mask); |
1199 | 1796 | ||
@@ -1423,38 +2020,6 @@ static struct uart_ops atmel_pops = { | |||
1423 | #endif | 2020 | #endif |
1424 | }; | 2021 | }; |
1425 | 2022 | ||
1426 | static void atmel_of_init_port(struct atmel_uart_port *atmel_port, | ||
1427 | struct device_node *np) | ||
1428 | { | ||
1429 | u32 rs485_delay[2]; | ||
1430 | |||
1431 | /* DMA/PDC usage specification */ | ||
1432 | if (of_get_property(np, "atmel,use-dma-rx", NULL)) | ||
1433 | atmel_port->use_dma_rx = 1; | ||
1434 | else | ||
1435 | atmel_port->use_dma_rx = 0; | ||
1436 | if (of_get_property(np, "atmel,use-dma-tx", NULL)) | ||
1437 | atmel_port->use_dma_tx = 1; | ||
1438 | else | ||
1439 | atmel_port->use_dma_tx = 0; | ||
1440 | |||
1441 | /* rs485 properties */ | ||
1442 | if (of_property_read_u32_array(np, "rs485-rts-delay", | ||
1443 | rs485_delay, 2) == 0) { | ||
1444 | struct serial_rs485 *rs485conf = &atmel_port->rs485; | ||
1445 | |||
1446 | rs485conf->delay_rts_before_send = rs485_delay[0]; | ||
1447 | rs485conf->delay_rts_after_send = rs485_delay[1]; | ||
1448 | rs485conf->flags = 0; | ||
1449 | |||
1450 | if (of_get_property(np, "rs485-rx-during-tx", NULL)) | ||
1451 | rs485conf->flags |= SER_RS485_RX_DURING_TX; | ||
1452 | |||
1453 | if (of_get_property(np, "linux,rs485-enabled-at-boot-time", NULL)) | ||
1454 | rs485conf->flags |= SER_RS485_ENABLED; | ||
1455 | } | ||
1456 | } | ||
1457 | |||
1458 | /* | 2023 | /* |
1459 | * Configure the port from the platform device resource info. | 2024 | * Configure the port from the platform device resource info. |
1460 | */ | 2025 | */ |
@@ -1463,15 +2028,12 @@ static int atmel_init_port(struct atmel_uart_port *atmel_port, | |||
1463 | { | 2028 | { |
1464 | int ret; | 2029 | int ret; |
1465 | struct uart_port *port = &atmel_port->uart; | 2030 | struct uart_port *port = &atmel_port->uart; |
1466 | struct atmel_uart_data *pdata = pdev->dev.platform_data; | 2031 | struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); |
1467 | 2032 | ||
1468 | if (pdev->dev.of_node) { | 2033 | if (!atmel_init_property(atmel_port, pdev)) |
1469 | atmel_of_init_port(atmel_port, pdev->dev.of_node); | 2034 | atmel_set_ops(port); |
1470 | } else { | 2035 | |
1471 | atmel_port->use_dma_rx = pdata->use_dma_rx; | 2036 | atmel_init_rs485(atmel_port, pdev); |
1472 | atmel_port->use_dma_tx = pdata->use_dma_tx; | ||
1473 | atmel_port->rs485 = pdata->rs485; | ||
1474 | } | ||
1475 | 2037 | ||
1476 | port->iotype = UPIO_MEM; | 2038 | port->iotype = UPIO_MEM; |
1477 | port->flags = UPF_BOOT_AUTOCONF; | 2039 | port->flags = UPF_BOOT_AUTOCONF; |
@@ -1516,7 +2078,7 @@ static int atmel_init_port(struct atmel_uart_port *atmel_port, | |||
1516 | /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */ | 2078 | /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */ |
1517 | if (atmel_port->rs485.flags & SER_RS485_ENABLED) | 2079 | if (atmel_port->rs485.flags & SER_RS485_ENABLED) |
1518 | atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; | 2080 | atmel_port->tx_done_mask = ATMEL_US_TXEMPTY; |
1519 | else if (atmel_use_dma_tx(port)) { | 2081 | else if (atmel_use_pdc_tx(port)) { |
1520 | port->fifosize = PDC_BUFFER_SIZE; | 2082 | port->fifosize = PDC_BUFFER_SIZE; |
1521 | atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE; | 2083 | atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE; |
1522 | } else { | 2084 | } else { |
@@ -1664,7 +2226,7 @@ static int __init atmel_console_init(void) | |||
1664 | int ret; | 2226 | int ret; |
1665 | if (atmel_default_console_device) { | 2227 | if (atmel_default_console_device) { |
1666 | struct atmel_uart_data *pdata = | 2228 | struct atmel_uart_data *pdata = |
1667 | atmel_default_console_device->dev.platform_data; | 2229 | dev_get_platdata(&atmel_default_console_device->dev); |
1668 | int id = pdata->num; | 2230 | int id = pdata->num; |
1669 | struct atmel_uart_port *port = &atmel_ports[id]; | 2231 | struct atmel_uart_port *port = &atmel_ports[id]; |
1670 | 2232 | ||
@@ -1772,10 +2334,9 @@ static int atmel_serial_probe(struct platform_device *pdev) | |||
1772 | { | 2334 | { |
1773 | struct atmel_uart_port *port; | 2335 | struct atmel_uart_port *port; |
1774 | struct device_node *np = pdev->dev.of_node; | 2336 | struct device_node *np = pdev->dev.of_node; |
1775 | struct atmel_uart_data *pdata = pdev->dev.platform_data; | 2337 | struct atmel_uart_data *pdata = dev_get_platdata(&pdev->dev); |
1776 | void *data; | 2338 | void *data; |
1777 | int ret = -ENODEV; | 2339 | int ret = -ENODEV; |
1778 | struct pinctrl *pinctrl; | ||
1779 | 2340 | ||
1780 | BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1)); | 2341 | BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1)); |
1781 | 2342 | ||
@@ -1809,13 +2370,7 @@ static int atmel_serial_probe(struct platform_device *pdev) | |||
1809 | if (ret) | 2370 | if (ret) |
1810 | goto err; | 2371 | goto err; |
1811 | 2372 | ||
1812 | pinctrl = devm_pinctrl_get_select_default(&pdev->dev); | 2373 | if (!atmel_use_pdc_rx(&port->uart)) { |
1813 | if (IS_ERR(pinctrl)) { | ||
1814 | ret = PTR_ERR(pinctrl); | ||
1815 | goto err; | ||
1816 | } | ||
1817 | |||
1818 | if (!atmel_use_dma_rx(&port->uart)) { | ||
1819 | ret = -ENOMEM; | 2374 | ret = -ENOMEM; |
1820 | data = kmalloc(sizeof(struct atmel_uart_char) | 2375 | data = kmalloc(sizeof(struct atmel_uart_char) |
1821 | * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL); | 2376 | * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL); |
@@ -1847,6 +2402,13 @@ static int atmel_serial_probe(struct platform_device *pdev) | |||
1847 | UART_PUT_CR(&port->uart, ATMEL_US_RTSEN); | 2402 | UART_PUT_CR(&port->uart, ATMEL_US_RTSEN); |
1848 | } | 2403 | } |
1849 | 2404 | ||
2405 | /* | ||
2406 | * Get port name of usart or uart | ||
2407 | */ | ||
2408 | ret = atmel_get_ip_name(&port->uart); | ||
2409 | if (ret < 0) | ||
2410 | goto err_add_port; | ||
2411 | |||
1850 | return 0; | 2412 | return 0; |
1851 | 2413 | ||
1852 | err_add_port: | 2414 | err_add_port: |
@@ -1868,7 +2430,6 @@ static int atmel_serial_remove(struct platform_device *pdev) | |||
1868 | int ret = 0; | 2430 | int ret = 0; |
1869 | 2431 | ||
1870 | device_init_wakeup(&pdev->dev, 0); | 2432 | device_init_wakeup(&pdev->dev, 0); |
1871 | platform_set_drvdata(pdev, NULL); | ||
1872 | 2433 | ||
1873 | ret = uart_remove_one_port(&atmel_uart, port); | 2434 | ret = uart_remove_one_port(&atmel_uart, port); |
1874 | 2435 | ||