2 * drivers/serial/tegra_hsuart.c
4 * High-speed serial driver for NVIDIA Tegra SoCs
6 * Copyright (C) 2009 NVIDIA Corporation
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 /*#define VERBOSE_DEBUG 1*/
26 #include <linux/module.h>
27 #include <linux/serial.h>
28 #include <linux/serial_core.h>
29 #include <linux/platform_device.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/dmapool.h>
33 #include <linux/termios.h>
34 #include <linux/irq.h>
35 #include <linux/delay.h>
36 #include <linux/clk.h>
37 #include <linux/string.h>
38 #include <linux/pagemap.h>
39 #include <linux/serial_reg.h>
40 #include <linux/serial_8250.h>
41 #include <linux/debugfs.h>
42 #include <linux/slab.h>
43 #include <linux/workqueue.h>
46 #include <mach/tegra_hsuart.h>
48 #define TX_EMPTY_STATUS (UART_LSR_TEMT | UART_LSR_THRE)
50 #define BYTES_TO_ALIGN(x) ((unsigned long)(ALIGN((x), sizeof(u32))) - \
53 #define UART_RX_DMA_BUFFER_SIZE (2048*4)
55 #define UART_LSR_FIFOE 0x80
56 #define UART_IER_EORD 0x20
57 #define UART_MCR_RTS_EN 0x40
58 #define UART_MCR_CTS_EN 0x20
59 #define UART_LSR_ANY (UART_LSR_OE | UART_LSR_BI | \
60 UART_LSR_PE | UART_LSR_FE)
62 #define TX_FORCE_PIO 0
63 #define RX_FORCE_PIO 0
65 const int dma_req_sel[] = {
66 TEGRA_DMA_REQ_SEL_UARTA,
67 TEGRA_DMA_REQ_SEL_UARTB,
68 TEGRA_DMA_REQ_SEL_UARTC,
69 TEGRA_DMA_REQ_SEL_UARTD,
70 TEGRA_DMA_REQ_SEL_UARTE,
73 #define TEGRA_TX_PIO 1
74 #define TEGRA_TX_DMA 2
76 #define TEGRA_UART_MIN_DMA 16
77 #define TEGRA_UART_FIFO_SIZE 8
79 /* Tx fifo trigger level setting in tegra uart is in
80 * reverse way then conventional uart */
81 #define TEGRA_UART_TX_TRIG_16B 0x00
82 #define TEGRA_UART_TX_TRIG_8B 0x10
83 #define TEGRA_UART_TX_TRIG_4B 0x20
84 #define TEGRA_UART_TX_TRIG_1B 0x30
86 struct tegra_uart_port {
87 struct uart_port uport;
96 unsigned char fcr_shadow;
97 unsigned char mcr_shadow;
98 unsigned char lcr_shadow;
99 unsigned char ier_shadow;
100 bool use_cts_control;
104 unsigned int tx_bytes;
106 dma_addr_t xmit_dma_addr;
109 struct tegra_dma_req tx_dma_req;
110 struct tegra_dma_channel *tx_dma;
111 struct work_struct tx_work;
114 struct tegra_dma_req rx_dma_req;
115 struct tegra_dma_channel *rx_dma;
123 /* optional callback to exit low power mode */
124 void (*exit_lpm_cb)(struct uart_port *);
125 /* optional callback to indicate rx is done */
126 void (*rx_done_cb)(struct uart_port *);
130 static inline u8 uart_readb(struct tegra_uart_port *t, unsigned long reg)
132 u8 val = readb(t->uport.membase + (reg << t->uport.regshift));
133 dev_vdbg(t->uport.dev, "%s: %p %03lx = %02x\n", __func__,
134 t->uport.membase, reg << t->uport.regshift, val);
138 static inline void uart_writeb(struct tegra_uart_port *t, u8 val,
141 dev_vdbg(t->uport.dev, "%s: %p %03lx %02x\n",
142 __func__, t->uport.membase, reg << t->uport.regshift, val);
143 writeb(val, t->uport.membase + (reg << t->uport.regshift));
146 static inline void uart_writel(struct tegra_uart_port *t, u32 val,
149 dev_vdbg(t->uport.dev, "%s: %p %03lx %08x\n",
150 __func__, t->uport.membase, reg << t->uport.regshift, val);
151 writel(val, t->uport.membase + (reg << t->uport.regshift));
154 static void tegra_set_baudrate(struct tegra_uart_port *t, unsigned int baud);
155 static void tegra_set_mctrl(struct uart_port *u, unsigned int mctrl);
156 static void do_handle_rx_pio(struct tegra_uart_port *t);
157 static void do_handle_rx_dma(struct tegra_uart_port *t);
158 static void set_rts(struct tegra_uart_port *t, bool active);
159 static void set_dtr(struct tegra_uart_port *t, bool active);
161 static void fill_tx_fifo(struct tegra_uart_port *t, int max_bytes)
164 struct circ_buf *xmit = &t->uport.state->xmit;
166 for (i = 0; i < max_bytes; i++) {
167 BUG_ON(uart_circ_empty(xmit));
168 uart_writeb(t, xmit->buf[xmit->tail], UART_TX);
169 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
170 t->uport.icount.tx++;
174 static void tegra_start_pio_tx(struct tegra_uart_port *t, unsigned int bytes)
176 if (bytes > TEGRA_UART_FIFO_SIZE)
177 bytes = TEGRA_UART_FIFO_SIZE;
179 t->fcr_shadow &= ~UART_FCR_T_TRIG_11;
180 t->fcr_shadow |= TEGRA_UART_TX_TRIG_8B;
181 uart_writeb(t, t->fcr_shadow, UART_FCR);
182 t->tx_in_progress = TEGRA_TX_PIO;
184 t->ier_shadow |= UART_IER_THRI;
185 uart_writeb(t, t->ier_shadow, UART_IER);
188 static void tegra_start_dma_tx(struct tegra_uart_port *t, unsigned long bytes)
190 struct circ_buf *xmit;
191 xmit = &t->uport.state->xmit;
193 dma_sync_single_for_device(t->uport.dev, t->xmit_dma_addr,
194 UART_XMIT_SIZE, DMA_TO_DEVICE);
196 t->fcr_shadow &= ~UART_FCR_T_TRIG_11;
197 t->fcr_shadow |= TEGRA_UART_TX_TRIG_4B;
198 uart_writeb(t, t->fcr_shadow, UART_FCR);
200 t->tx_bytes = bytes & ~(sizeof(u32)-1);
201 t->tx_dma_req.source_addr = t->xmit_dma_addr + xmit->tail;
202 t->tx_dma_req.size = t->tx_bytes;
204 t->tx_in_progress = TEGRA_TX_DMA;
206 tegra_dma_enqueue_req(t->tx_dma, &t->tx_dma_req);
209 /* Called with u->lock taken */
210 static void tegra_start_next_tx(struct tegra_uart_port *t)
215 struct circ_buf *xmit;
217 xmit = &t->uport.state->xmit;
218 tail = (unsigned long)&xmit->buf[xmit->tail];
219 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
222 dev_vdbg(t->uport.dev, "+%s %lu %d\n", __func__, count,
228 if (!t->use_tx_dma || count < TEGRA_UART_MIN_DMA)
229 tegra_start_pio_tx(t, count);
230 else if (BYTES_TO_ALIGN(tail) > 0)
231 tegra_start_pio_tx(t, BYTES_TO_ALIGN(tail));
233 tegra_start_dma_tx(t, count);
236 dev_vdbg(t->uport.dev, "-%s", __func__);
239 /* Called by serial core driver with u->lock taken. */
240 static void tegra_start_tx(struct uart_port *u)
242 struct tegra_uart_port *t;
243 struct circ_buf *xmit;
245 t = container_of(u, struct tegra_uart_port, uport);
246 xmit = &u->state->xmit;
251 if (!uart_circ_empty(xmit) && !t->tx_in_progress)
252 tegra_start_next_tx(t);
255 static int tegra_start_dma_rx(struct tegra_uart_port *t)
258 if (tegra_dma_enqueue_req(t->rx_dma, &t->rx_dma_req)) {
259 dev_err(t->uport.dev, "Could not enqueue Rx DMA req\n");
265 static void tegra_rx_dma_threshold_callback(struct tegra_dma_req *req)
267 struct tegra_uart_port *t = req->dev;
270 spin_lock_irqsave(&t->uport.lock, flags);
274 spin_unlock_irqrestore(&t->uport.lock, flags);
277 /* must be called with uart lock held */
278 static void tegra_rx_dma_complete_req(struct tegra_uart_port *t,
279 struct tegra_dma_req *req)
281 struct uart_port *u = &t->uport;
282 struct tty_struct *tty = u->state->port.tty;
284 /* If we are here, DMA is stopped */
286 dev_dbg(t->uport.dev, "%s: %d %d\n", __func__, req->bytes_transferred,
288 if (req->bytes_transferred) {
289 t->uport.icount.rx += req->bytes_transferred;
290 tty_insert_flip_string(tty,
291 ((unsigned char *)(req->virt_addr)),
292 req->bytes_transferred);
297 /* Push the read data later in caller place. */
298 if (req->status == -TEGRA_DMA_REQ_ERROR_ABORTED)
301 tty_flip_buffer_push(u->state->port.tty);
307 static void tegra_rx_dma_complete_callback(struct tegra_dma_req *req)
309 struct tegra_uart_port *t = req->dev;
313 * should never get called, dma should be dequeued during threshold
317 dev_warn(t->uport.dev, "possible rx overflow\n");
319 spin_lock_irqsave(&t->uport.lock, flags);
320 tegra_rx_dma_complete_req(t, req);
321 spin_unlock_irqrestore(&t->uport.lock, flags);
324 /* Lock already taken */
325 static void do_handle_rx_dma(struct tegra_uart_port *t)
327 struct uart_port *u = &t->uport;
330 if (!tegra_dma_dequeue_req(t->rx_dma, &t->rx_dma_req))
331 tegra_rx_dma_complete_req(t, &t->rx_dma_req);
333 tty_flip_buffer_push(u->state->port.tty);
336 /* enqueue the request again */
337 tegra_start_dma_rx(t);
342 /* Wait for a symbol-time. */
343 static void wait_sym_time(struct tegra_uart_port *t, unsigned int syms)
346 /* Definitely have a start bit. */
347 unsigned int bits = 1;
348 switch (t->lcr_shadow & 3) {
363 /* Technically 5 bits gets 1.5 bits of stop... */
364 if (t->lcr_shadow & UART_LCR_STOP) {
370 if (t->lcr_shadow & UART_LCR_PARITY)
374 udelay(DIV_ROUND_UP(syms * bits * 1000000, t->baud));
377 /* Flush desired FIFO. */
378 static void tegra_fifo_reset(struct tegra_uart_port *t, u8 fcr_bits)
380 unsigned char fcr = t->fcr_shadow;
381 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
382 uart_writeb(t, fcr, UART_FCR);
383 uart_readb(t, UART_SCR); /* Dummy read to ensure the write is posted */
384 wait_sym_time(t, 1); /* Wait for the flush to propagate. */
387 static char do_decode_rx_error(struct tegra_uart_port *t, u8 lsr)
389 char flag = TTY_NORMAL;
391 if (unlikely(lsr & UART_LSR_ANY)) {
392 if (lsr & UART_LSR_OE) {
395 t->uport.icount.overrun++;
396 dev_err(t->uport.dev, "Got overrun errors\n");
397 } else if (lsr & UART_LSR_PE) {
400 t->uport.icount.parity++;
401 dev_err(t->uport.dev, "Got Parity errors\n");
402 } else if (lsr & UART_LSR_FE) {
404 t->uport.icount.frame++;
405 dev_err(t->uport.dev, "Got frame errors\n");
406 } else if (lsr & UART_LSR_BI) {
407 dev_err(t->uport.dev, "Got Break\n");
408 t->uport.icount.brk++;
409 /* If FIFO read error without any data, reset Rx FIFO */
410 if (!(lsr & UART_LSR_DR) && (lsr & UART_LSR_FIFOE))
411 tegra_fifo_reset(t, UART_FCR_CLEAR_RCVR);
417 static void do_handle_rx_pio(struct tegra_uart_port *t)
421 char flag = TTY_NORMAL;
422 unsigned char lsr = 0;
426 lsr = uart_readb(t, UART_LSR);
427 if (!(lsr & UART_LSR_DR))
430 flag = do_decode_rx_error(t, lsr);
431 ch = uart_readb(t, UART_RX);
432 t->uport.icount.rx++;
435 if (!uart_handle_sysrq_char(&t->uport, c))
436 uart_insert_char(&t->uport, lsr, UART_LSR_OE, ch, flag);
439 dev_dbg(t->uport.dev, "PIO received %d bytes\n", count);
444 static void do_handle_modem_signal(struct uart_port *u)
447 struct tegra_uart_port *t;
449 t = container_of(u, struct tegra_uart_port, uport);
450 msr = uart_readb(t, UART_MSR);
451 if (msr & UART_MSR_CTS)
452 dev_dbg(u->dev, "CTS triggered\n");
453 if (msr & UART_MSR_DSR)
454 dev_dbg(u->dev, "DSR enabled\n");
455 if (msr & UART_MSR_DCD)
456 dev_dbg(u->dev, "CD enabled\n");
457 if (msr & UART_MSR_RI)
458 dev_dbg(u->dev, "RI enabled\n");
462 static void do_handle_tx_pio(struct tegra_uart_port *t)
464 struct circ_buf *xmit = &t->uport.state->xmit;
466 fill_tx_fifo(t, t->tx_bytes);
468 t->tx_in_progress = 0;
470 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
471 uart_write_wakeup(&t->uport);
473 tegra_start_next_tx(t);
477 static void tegra_tx_dma_complete_work(struct work_struct *work)
479 struct tegra_uart_port *t =
480 container_of(work, struct tegra_uart_port, tx_work);
481 struct tegra_dma_req *req = &t->tx_dma_req;
485 while ((uart_readb(t, UART_LSR) & TX_EMPTY_STATUS) != TX_EMPTY_STATUS) {
488 dev_err(t->uport.dev,
489 "timed out waiting for TX FIFO to empty\n");
495 spin_lock_irqsave(&t->uport.lock, flags);
497 t->tx_in_progress = 0;
499 if (req->status != -TEGRA_DMA_REQ_ERROR_ABORTED)
500 tegra_start_next_tx(t);
502 spin_unlock_irqrestore(&t->uport.lock, flags);
505 /* must be called with uart lock held */
506 static void tegra_tx_dma_complete_req(struct tegra_uart_port *t,
507 struct tegra_dma_req *req)
509 struct circ_buf *xmit = &t->uport.state->xmit;
510 int count = req->bytes_transferred;
512 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
514 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
515 uart_write_wakeup(&t->uport);
517 schedule_work(&t->tx_work);
520 static void tegra_tx_dma_complete_callback(struct tegra_dma_req *req)
522 struct tegra_uart_port *t = req->dev;
525 dev_vdbg(t->uport.dev, "%s: %d\n", __func__, req->bytes_transferred);
527 spin_lock_irqsave(&t->uport.lock, flags);
529 tegra_tx_dma_complete_req(t, req);
531 spin_unlock_irqrestore(&t->uport.lock, flags);
534 static irqreturn_t tegra_uart_isr(int irq, void *data)
536 struct tegra_uart_port *t = data;
537 struct uart_port *u = &t->uport;
540 bool is_rx_int = false;
543 spin_lock_irqsave(&u->lock, flags);
544 t = container_of(u, struct tegra_uart_port, uport);
546 iir = uart_readb(t, UART_IIR);
547 if (iir & UART_IIR_NO_INT) {
548 if (likely(t->use_rx_dma) && is_rx_int) {
551 if (t->rx_in_progress) {
553 ier |= (UART_IER_RLSI | UART_IER_RTOIE | UART_IER_EORD);
555 uart_writeb(t, ier, UART_IER);
558 spin_unlock_irqrestore(&u->lock, flags);
562 dev_dbg(u->dev, "tegra_uart_isr iir = 0x%x (%d)\n", iir,
564 switch ((iir >> 1) & 0x7) {
565 case 0: /* Modem signal change interrupt */
566 do_handle_modem_signal(u);
568 case 1: /* Transmit interrupt only triggered when using PIO */
569 t->ier_shadow &= ~UART_IER_THRI;
570 uart_writeb(t, t->ier_shadow, UART_IER);
573 case 4: /* End of data */
574 case 6: /* Rx timeout */
575 case 2: /* Receive */
576 if (likely(t->use_rx_dma)) {
579 /* Disable interrups */
582 uart_writeb(t, ier, UART_IER);
583 ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE | UART_IER_EORD);
585 uart_writeb(t, ier, UART_IER);
590 spin_unlock_irqrestore(&u->lock, flags);
591 tty_flip_buffer_push(u->state->port.tty);
592 spin_lock_irqsave(&u->lock, flags);
597 case 3: /* Receive error */
598 /* FIXME how to handle this? Why do we get here */
599 do_decode_rx_error(t, uart_readb(t, UART_LSR));
601 case 5: /* break nothing to handle */
602 case 7: /* break nothing to handle */
608 static void tegra_stop_rx(struct uart_port *u)
610 struct tegra_uart_port *t;
613 t = container_of(u, struct tegra_uart_port, uport);
618 if (t->rx_in_progress) {
619 wait_sym_time(t, 1); /* wait a character interval */
622 ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE | UART_IER_EORD);
624 uart_writeb(t, ier, UART_IER);
625 t->rx_in_progress = 0;
627 if (t->use_rx_dma && t->rx_dma) {
628 if (!tegra_dma_dequeue_req(t->rx_dma, &t->rx_dma_req))
629 tegra_rx_dma_complete_req(t, &t->rx_dma_req);
633 tty_flip_buffer_push(u->state->port.tty);
641 static void tegra_uart_hw_deinit(struct tegra_uart_port *t)
645 flush_work(&t->tx_work);
647 /* Disable interrupts */
648 uart_writeb(t, 0, UART_IER);
650 while ((uart_readb(t, UART_LSR) & UART_LSR_TEMT) != UART_LSR_TEMT);
653 spin_lock_irqsave(&t->uport.lock, flags);
655 /* Reset the Rx and Tx FIFOs */
656 tegra_fifo_reset(t, UART_FCR_CLEAR_XMIT | UART_FCR_CLEAR_RCVR);
661 spin_unlock_irqrestore(&t->uport.lock, flags);
664 static void tegra_uart_free_rx_dma(struct tegra_uart_port *t)
669 tegra_dma_free_channel(t->rx_dma);
672 if (likely(t->rx_dma_req.dest_addr))
673 dma_free_coherent(t->uport.dev, t->rx_dma_req.size,
674 t->rx_dma_req.virt_addr, t->rx_dma_req.dest_addr);
675 t->rx_dma_req.dest_addr = 0;
676 t->rx_dma_req.virt_addr = NULL;
678 t->use_rx_dma = false;
681 static int tegra_uart_hw_init(struct tegra_uart_port *t)
685 dev_vdbg(t->uport.dev, "+tegra_uart_hw_init\n");
695 /* Reset the UART controller to clear all previous status.*/
696 tegra_periph_reset_assert(t->clk);
698 tegra_periph_reset_deassert(t->clk);
701 t->rx_in_progress = 0;
703 /* Set the trigger level
707 * For receive, this will interrupt the CPU after that many number of
708 * bytes are received, for the remaining bytes the receive timeout
709 * interrupt is received.
711 * Rx high watermark is set to 4.
713 * For transmit, if the trasnmit interrupt is enabled, this will
714 * interrupt the CPU when the number of entries in the FIFO reaches the
717 * Tx low watermark is set to 8.
721 * Set the Tx trigger to 4. This should match the DMA burst size that
722 * programmed in the DMA registers.
724 t->fcr_shadow = UART_FCR_ENABLE_FIFO;
725 t->fcr_shadow |= UART_FCR_R_TRIG_01;
726 t->fcr_shadow |= TEGRA_UART_TX_TRIG_8B;
727 uart_writeb(t, t->fcr_shadow, UART_FCR);
730 /* initialize the UART for a simple default configuration
731 * so that the receive DMA buffer may be enqueued */
732 t->lcr_shadow = 3; /* no parity, stop, 8 data bits */
733 tegra_set_baudrate(t, 115200);
734 t->fcr_shadow |= UART_FCR_DMA_SELECT;
735 uart_writeb(t, t->fcr_shadow, UART_FCR);
736 if (tegra_start_dma_rx(t)) {
737 dev_err(t->uport.dev, "Rx DMA enqueue failed\n");
738 tegra_uart_free_rx_dma(t);
739 t->fcr_shadow &= ~UART_FCR_DMA_SELECT;
740 uart_writeb(t, t->fcr_shadow, UART_FCR);
744 uart_writeb(t, t->fcr_shadow, UART_FCR);
746 t->rx_in_progress = 1;
749 * Enable IE_RXS for the receive status interrupts like line errros.
750 * Enable IE_RX_TIMEOUT to get the bytes which cannot be DMA'd.
752 * If using DMA mode, enable EORD instead of receive interrupt which
753 * will interrupt after the UART is done with the receive instead of
754 * the interrupt when the FIFO "threshold" is reached.
756 * EORD is different interrupt than RX_TIMEOUT - RX_TIMEOUT occurs when
757 * the DATA is sitting in the FIFO and couldn't be transferred to the
758 * DMA as the DMA size alignment(4 bytes) is not met. EORD will be
759 * triggered when there is a pause of the incomming data stream for 4
762 * For pauses in the data which is not aligned to 4 bytes, we get
763 * both the EORD as well as RX_TIMEOUT - SW sees RX_TIMEOUT first
766 * Don't get confused, believe in the magic of nvidia hw...:-)
769 ier |= UART_IER_RLSI | UART_IER_RTOIE;
771 ier |= UART_IER_EORD;
775 uart_writeb(t, ier, UART_IER);
777 dev_vdbg(t->uport.dev, "-tegra_uart_hw_init\n");
781 static int tegra_uart_init_rx_dma(struct tegra_uart_port *t)
783 dma_addr_t rx_dma_phys;
786 t->rx_dma = tegra_dma_allocate_channel(TEGRA_DMA_MODE_CONTINUOUS);
788 dev_err(t->uport.dev, "%s: failed to allocate RX DMA.\n", __func__);
792 t->rx_dma_req.size = UART_RX_DMA_BUFFER_SIZE;
793 rx_dma_virt = dma_alloc_coherent(t->uport.dev,
794 t->rx_dma_req.size, &rx_dma_phys, GFP_KERNEL);
796 dev_err(t->uport.dev, "DMA buffers allocate failed\n");
799 t->rx_dma_req.dest_addr = rx_dma_phys;
800 t->rx_dma_req.virt_addr = rx_dma_virt;
802 t->rx_dma_req.source_addr = (unsigned long)t->uport.mapbase;
803 t->rx_dma_req.source_wrap = 4;
804 t->rx_dma_req.dest_wrap = 0;
805 t->rx_dma_req.to_memory = 1;
806 t->rx_dma_req.source_bus_width = 8;
807 t->rx_dma_req.dest_bus_width = 32;
808 t->rx_dma_req.req_sel = dma_req_sel[t->uport.line];
809 t->rx_dma_req.complete = tegra_rx_dma_complete_callback;
810 t->rx_dma_req.threshold = tegra_rx_dma_threshold_callback;
811 t->rx_dma_req.dev = t;
815 tegra_uart_free_rx_dma(t);
819 static int tegra_startup(struct uart_port *u)
821 struct tegra_uart_port *t = container_of(u,
822 struct tegra_uart_port, uport);
825 t = container_of(u, struct tegra_uart_port, uport);
826 sprintf(t->port_name, "tegra_uart_%d", u->line);
828 t->use_tx_dma = false;
830 t->tx_dma = tegra_dma_allocate_channel(TEGRA_DMA_MODE_ONESHOT);
832 t->use_tx_dma = true;
834 pr_err("%s: failed to allocate TX DMA.\n", __func__);
837 t->tx_dma_req.instance = u->line;
838 t->tx_dma_req.complete = tegra_tx_dma_complete_callback;
839 t->tx_dma_req.to_memory = 0;
841 t->tx_dma_req.dest_addr = (unsigned long)t->uport.mapbase;
842 t->tx_dma_req.dest_wrap = 4;
843 t->tx_dma_req.source_wrap = 0;
844 t->tx_dma_req.source_bus_width = 32;
845 t->tx_dma_req.dest_bus_width = 8;
846 t->tx_dma_req.req_sel = dma_req_sel[t->uport.line];
847 t->tx_dma_req.dev = t;
848 t->tx_dma_req.size = 0;
849 t->xmit_dma_addr = dma_map_single(t->uport.dev,
850 t->uport.state->xmit.buf, UART_XMIT_SIZE,
853 t->tx_in_progress = 0;
855 t->use_rx_dma = false;
857 if (!tegra_uart_init_rx_dma(t))
858 t->use_rx_dma = true;
861 ret = tegra_uart_hw_init(t);
865 dev_dbg(u->dev, "Requesting IRQ %d\n", u->irq);
868 ret = request_irq(u->irq, tegra_uart_isr, IRQF_DISABLED,
871 dev_err(u->dev, "Failed to register ISR for IRQ %d\n", u->irq);
874 dev_dbg(u->dev,"Started UART port %d\n", u->line);
878 dev_err(u->dev, "Tegra UART startup failed\n");
882 static void tegra_shutdown(struct uart_port *u)
884 struct tegra_uart_port *t;
886 t = container_of(u, struct tegra_uart_port, uport);
887 dev_vdbg(u->dev, "+tegra_shutdown\n");
889 tegra_uart_hw_deinit(t);
891 t->rx_in_progress = 0;
892 t->tx_in_progress = 0;
894 tegra_uart_free_rx_dma(t);
896 tegra_dma_free_channel(t->tx_dma);
898 t->use_tx_dma = false;
899 dma_unmap_single(t->uport.dev, t->xmit_dma_addr, UART_XMIT_SIZE,
901 t->xmit_dma_addr = 0;
905 dev_vdbg(u->dev, "-tegra_shutdown\n");
908 static unsigned int tegra_get_mctrl(struct uart_port *u)
910 /* RI - Ring detector is active
911 * CD/DCD/CAR - Carrier detect is always active. For some reason
912 * linux has different names for carrier detect.
913 * DSR - Data Set ready is active as the hardware doesn't support it.
914 * Don't know if the linux support this yet?
915 * CTS - Clear to send. Always set to active, as the hardware handles
918 return TIOCM_RI | TIOCM_CD | TIOCM_DSR | TIOCM_CTS;
921 static void set_rts(struct tegra_uart_port *t, bool active)
928 mcr &= ~UART_MCR_RTS;
929 if (mcr != t->mcr_shadow) {
930 uart_writeb(t, mcr, UART_MCR);
936 static void set_dtr(struct tegra_uart_port *t, bool active)
943 mcr &= ~UART_MCR_DTR;
944 if (mcr != t->mcr_shadow) {
945 uart_writeb(t, mcr, UART_MCR);
951 static void tegra_set_mctrl(struct uart_port *u, unsigned int mctrl)
954 struct tegra_uart_port *t;
956 dev_dbg(u->dev, "tegra_set_mctrl called with %d\n", mctrl);
957 t = container_of(u, struct tegra_uart_port, uport);
960 if (mctrl & TIOCM_RTS) {
961 t->rts_active = true;
964 t->rts_active = false;
968 if (mctrl & TIOCM_DTR)
975 static void tegra_break_ctl(struct uart_port *u, int break_ctl)
977 struct tegra_uart_port *t;
980 t = container_of(u, struct tegra_uart_port, uport);
985 lcr &= ~UART_LCR_SBC;
986 uart_writeb(t, lcr, UART_LCR);
990 static int tegra_request_port(struct uart_port *u)
995 static void tegra_release_port(struct uart_port *u)
1000 static unsigned int tegra_tx_empty(struct uart_port *u)
1002 struct tegra_uart_port *t;
1003 unsigned int ret = 0;
1004 unsigned long flags;
1006 t = container_of(u, struct tegra_uart_port, uport);
1007 dev_vdbg(u->dev, "+tegra_tx_empty\n");
1009 spin_lock_irqsave(&u->lock, flags);
1010 if (!t->tx_in_progress)
1012 spin_unlock_irqrestore(&u->lock, flags);
1014 dev_vdbg(u->dev, "-tegra_tx_empty\n");
1018 static void tegra_stop_tx(struct uart_port *u)
1020 struct tegra_uart_port *t;
1022 t = container_of(u, struct tegra_uart_port, uport);
1024 if (t->use_tx_dma) {
1025 if (!tegra_dma_dequeue_req(t->tx_dma, &t->tx_dma_req))
1026 tegra_tx_dma_complete_req(t, &t->tx_dma_req);
1032 static void tegra_enable_ms(struct uart_port *u)
1036 #define UART_CLOCK_ACCURACY 5
1038 static void tegra_set_baudrate(struct tegra_uart_port *t, unsigned int baud)
1041 unsigned int divisor;
1044 if (t->baud == baud)
1047 rate = clk_get_rate(t->clk);
1050 do_div(divisor, 16);
1052 do_div(divisor, baud);
1054 lcr = t->lcr_shadow;
1055 lcr |= UART_LCR_DLAB;
1056 uart_writeb(t, lcr, UART_LCR);
1058 uart_writel(t, divisor & 0xFF, UART_TX);
1059 uart_writel(t, ((divisor >> 8) & 0xFF), UART_IER);
1061 lcr &= ~UART_LCR_DLAB;
1062 uart_writeb(t, lcr, UART_LCR);
1063 uart_readb(t, UART_SCR); /* Dummy read to ensure the write is posted */
1066 wait_sym_time(t, 2); /* wait two character intervals at new rate */
1067 dev_dbg(t->uport.dev, "Baud %u clock freq %lu and divisor of %u\n",
1068 baud, rate, divisor);
1071 static void tegra_set_termios(struct uart_port *u, struct ktermios *termios,
1072 struct ktermios *oldtermios)
1074 struct tegra_uart_port *t;
1076 unsigned long flags;
1078 unsigned int c_cflag = termios->c_cflag;
1081 t = container_of(u, struct tegra_uart_port, uport);
1082 dev_vdbg(t->uport.dev, "+tegra_set_termios\n");
1084 spin_lock_irqsave(&u->lock, flags);
1086 /* Changing configuration, it is safe to stop any rx now */
1091 lcr = t->lcr_shadow;
1092 lcr &= ~UART_LCR_PARITY;
1093 if (PARENB == (c_cflag & PARENB)) {
1094 if (CMSPAR == (c_cflag & CMSPAR)) {
1095 /* FIXME What is space parity? */
1096 /* data |= SPACE_PARITY; */
1097 } else if (c_cflag & PARODD) {
1098 lcr |= UART_LCR_PARITY;
1099 lcr &= ~UART_LCR_EPAR;
1100 lcr &= ~UART_LCR_SPAR;
1102 lcr |= UART_LCR_PARITY;
1103 lcr |= UART_LCR_EPAR;
1104 lcr &= ~UART_LCR_SPAR;
1108 lcr &= ~UART_LCR_WLEN8;
1109 switch (c_cflag & CSIZE) {
1111 lcr |= UART_LCR_WLEN5;
1114 lcr |= UART_LCR_WLEN6;
1117 lcr |= UART_LCR_WLEN7;
1120 lcr |= UART_LCR_WLEN8;
1125 if (termios->c_cflag & CSTOPB)
1126 lcr |= UART_LCR_STOP;
1128 lcr &= ~UART_LCR_STOP;
1130 uart_writeb(t, lcr, UART_LCR);
1131 t->lcr_shadow = lcr;
1134 baud = uart_get_baud_rate(u, termios, oldtermios, 200, 4000000);
1135 tegra_set_baudrate(t, baud);
1138 if (termios->c_cflag & CRTSCTS) {
1139 mcr = t->mcr_shadow;
1140 mcr |= UART_MCR_CTS_EN;
1141 mcr &= ~UART_MCR_RTS_EN;
1142 t->mcr_shadow = mcr;
1143 uart_writeb(t, mcr, UART_MCR);
1144 t->use_cts_control = true;
1145 /* if top layer has asked to set rts active then do so here */
1149 mcr = t->mcr_shadow;
1150 mcr &= ~UART_MCR_CTS_EN;
1151 mcr &= ~UART_MCR_RTS_EN;
1152 t->mcr_shadow = mcr;
1153 uart_writeb(t, mcr, UART_MCR);
1154 t->use_cts_control = false;
1157 /* update the port timeout based on new settings */
1158 uart_update_timeout(u, termios->c_cflag, baud);
1160 spin_unlock_irqrestore(&u->lock, flags);
1161 dev_vdbg(t->uport.dev, "-tegra_set_termios\n");
1166 * Flush any TX data submitted for DMA. Called when the TX circular
1169 static void tegra_flush_buffer(struct uart_port *u)
1171 struct tegra_uart_port *t;
1173 dev_vdbg(u->dev, "%s called", __func__);
1175 t = container_of(u, struct tegra_uart_port, uport);
1177 if (t->use_tx_dma) {
1178 if (!tegra_dma_dequeue_req(t->tx_dma, &t->tx_dma_req))
1179 tegra_tx_dma_complete_req(t, &t->tx_dma_req);
1180 t->tx_dma_req.size = 0;
1186 static void tegra_pm(struct uart_port *u, unsigned int state,
1187 unsigned int oldstate)
1192 static const char *tegra_type(struct uart_port *u)
1197 static struct uart_ops tegra_uart_ops = {
1198 .tx_empty = tegra_tx_empty,
1199 .set_mctrl = tegra_set_mctrl,
1200 .get_mctrl = tegra_get_mctrl,
1201 .stop_tx = tegra_stop_tx,
1202 .start_tx = tegra_start_tx,
1203 .stop_rx = tegra_stop_rx,
1204 .flush_buffer = tegra_flush_buffer,
1205 .enable_ms = tegra_enable_ms,
1206 .break_ctl = tegra_break_ctl,
1207 .startup = tegra_startup,
1208 .shutdown = tegra_shutdown,
1209 .set_termios = tegra_set_termios,
1212 .request_port = tegra_request_port,
1213 .release_port = tegra_release_port,
1216 static int tegra_uart_probe(struct platform_device *pdev);
1217 static int __devexit tegra_uart_remove(struct platform_device *pdev);
1218 static int tegra_uart_suspend(struct platform_device *pdev, pm_message_t state);
1219 static int tegra_uart_resume(struct platform_device *pdev);
1221 static struct platform_driver tegra_uart_platform_driver = {
1222 .remove = tegra_uart_remove,
1223 .probe = tegra_uart_probe,
1224 .suspend = tegra_uart_suspend,
1225 .resume = tegra_uart_resume,
1227 .name = "tegra_uart"
1231 static struct uart_driver tegra_uart_driver =
1233 .owner = THIS_MODULE,
1234 .driver_name = "tegra_uart",
1235 .dev_name = "ttyHS",
1240 static int tegra_uart_suspend(struct platform_device *pdev, pm_message_t state)
1242 struct tegra_uart_port *t = platform_get_drvdata(pdev);
1243 struct uart_port *u;
1245 if (pdev->id < 0 || pdev->id > tegra_uart_driver.nr)
1246 pr_err("Invalid Uart instance (%d)\n", pdev->id);
1249 uart_suspend_port(&tegra_uart_driver, u);
1251 flush_work(&t->tx_work);
1255 static int tegra_uart_resume(struct platform_device *pdev)
1257 struct tegra_uart_port *t = platform_get_drvdata(pdev);
1258 struct uart_port *u;
1260 if (pdev->id < 0 || pdev->id > tegra_uart_driver.nr)
1261 pr_err("Invalid Uart instance (%d)\n", pdev->id);
1264 uart_resume_port(&tegra_uart_driver, u);
1270 static int __devexit tegra_uart_remove(struct platform_device *pdev)
1272 struct tegra_uart_port *t = platform_get_drvdata(pdev);
1273 struct uart_port *u;
1275 if (pdev->id < 0 || pdev->id > tegra_uart_driver.nr)
1276 pr_err("Invalid Uart instance (%d)\n", pdev->id);
1279 uart_remove_one_port(&tegra_uart_driver, u);
1281 platform_set_drvdata(pdev, NULL);
1283 pr_info("Unregistered UART port %s%d\n",
1284 tegra_uart_driver.dev_name, u->line);
1289 static int tegra_uart_probe(struct platform_device *pdev)
1291 struct tegra_uart_port *t;
1292 struct uart_port *u;
1293 struct resource *resource;
1294 struct tegra_hsuart_platform_data *pdata = pdev->dev.platform_data;
1297 if (pdev->id < 0 || pdev->id > tegra_uart_driver.nr) {
1298 pr_err("Invalid Uart instance (%d)\n", pdev->id);
1302 t = kzalloc(sizeof(struct tegra_uart_port), GFP_KERNEL);
1304 pr_err("%s: Failed to allocate memory\n", __func__);
1308 u->dev = &pdev->dev;
1309 platform_set_drvdata(pdev, u);
1311 u->ops = &tegra_uart_ops;
1312 u->type = ~PORT_UNKNOWN;
1315 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1316 if (unlikely(!resource))
1319 u->mapbase = resource->start;
1320 u->membase = IO_ADDRESS(u->mapbase);
1321 if (unlikely(!u->membase))
1324 u->irq = platform_get_irq(pdev, 0);
1325 if (unlikely(u->irq < 0))
1331 t->exit_lpm_cb = pdata->exit_lpm_cb;
1332 t->rx_done_cb = pdata->rx_done_cb;
1335 t->clk = clk_get(&pdev->dev, NULL);
1337 dev_err(&pdev->dev, "Couldn't get the clock\n");
1341 ret = uart_add_one_port(&tegra_uart_driver, u);
1343 pr_err("%s: Failed(%d) to add uart port %s%d\n",
1344 __func__, ret, tegra_uart_driver.dev_name, u->line);
1346 platform_set_drvdata(pdev, NULL);
1350 snprintf(name, sizeof(name), "tegra_hsuart_%d", u->line);
1351 pr_info("Registered UART port %s%d\n",
1352 tegra_uart_driver.dev_name, u->line);
1354 INIT_WORK(&t->tx_work, tegra_tx_dma_complete_work);
1361 static int __init tegra_uart_init(void)
1365 ret = uart_register_driver(&tegra_uart_driver);
1366 if (unlikely(ret)) {
1367 pr_err("Could not register %s driver\n",
1368 tegra_uart_driver.driver_name);
1372 ret = platform_driver_register(&tegra_uart_platform_driver);
1373 if (unlikely(ret)) {
1374 pr_err("Could not register the UART platfrom "
1376 uart_unregister_driver(&tegra_uart_driver);
1380 pr_info("Initialized tegra uart driver\n");
1384 static void __exit tegra_uart_exit(void)
1386 pr_info("Unloading tegra uart driver\n");
1387 platform_driver_unregister(&tegra_uart_platform_driver);
1388 uart_unregister_driver(&tegra_uart_driver);
1391 module_init(tegra_uart_init);
1392 module_exit(tegra_uart_exit);
1393 MODULE_DESCRIPTION("High speed UART driver for tegra chipset");