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>
47 #define TX_EMPTY_STATUS (UART_LSR_TEMT | UART_LSR_THRE)
49 #define BYTES_TO_ALIGN(x) ((unsigned long)(ALIGN((x), sizeof(u32))) - \
52 #define UART_RX_DMA_BUFFER_SIZE (2048*4)
54 #define UART_LSR_FIFOE 0x80
55 #define UART_IER_EORD 0x20
56 #define UART_MCR_RTS_EN 0x40
57 #define UART_MCR_CTS_EN 0x20
58 #define UART_LSR_ANY (UART_LSR_OE | UART_LSR_BI | \
59 UART_LSR_PE | UART_LSR_FE)
61 #define TX_FORCE_PIO 0
62 #define RX_FORCE_PIO 0
64 const int dma_req_sel[] = {
65 TEGRA_DMA_REQ_SEL_UARTA,
66 TEGRA_DMA_REQ_SEL_UARTB,
67 TEGRA_DMA_REQ_SEL_UARTC,
68 TEGRA_DMA_REQ_SEL_UARTD,
69 TEGRA_DMA_REQ_SEL_UARTE,
72 #define TEGRA_TX_PIO 1
73 #define TEGRA_TX_DMA 2
75 #define TEGRA_UART_MIN_DMA 16
76 #define TEGRA_UART_FIFO_SIZE 8
78 /* Tx fifo trigger level setting in tegra uart is in
79 * reverse way then conventional uart */
80 #define TEGRA_UART_TX_TRIG_16B 0x00
81 #define TEGRA_UART_TX_TRIG_8B 0x10
82 #define TEGRA_UART_TX_TRIG_4B 0x20
83 #define TEGRA_UART_TX_TRIG_1B 0x30
85 struct tegra_uart_port {
86 struct uart_port uport;
95 unsigned char fcr_shadow;
96 unsigned char mcr_shadow;
97 unsigned char lcr_shadow;
98 unsigned char ier_shadow;
103 unsigned int tx_bytes;
105 dma_addr_t xmit_dma_addr;
108 struct tegra_dma_req tx_dma_req;
109 struct tegra_dma_channel *tx_dma;
110 struct work_struct tx_work;
113 struct tegra_dma_req rx_dma_req;
114 struct tegra_dma_channel *rx_dma;
123 static inline u8 uart_readb(struct tegra_uart_port *t, unsigned long reg)
125 u8 val = readb(t->uport.membase + (reg << t->uport.regshift));
126 dev_vdbg(t->uport.dev, "%s: %p %03lx = %02x\n", __func__,
127 t->uport.membase, reg << t->uport.regshift, val);
131 static inline void uart_writeb(struct tegra_uart_port *t, u8 val,
134 dev_vdbg(t->uport.dev, "%s: %p %03lx %02x\n",
135 __func__, t->uport.membase, reg << t->uport.regshift, val);
136 writeb(val, t->uport.membase + (reg << t->uport.regshift));
139 static inline void uart_writel(struct tegra_uart_port *t, u32 val,
142 dev_vdbg(t->uport.dev, "%s: %p %03lx %08x\n",
143 __func__, t->uport.membase, reg << t->uport.regshift, val);
144 writel(val, t->uport.membase + (reg << t->uport.regshift));
147 static void tegra_set_baudrate(struct tegra_uart_port *t, unsigned int baud);
148 static void tegra_set_mctrl(struct uart_port *u, unsigned int mctrl);
149 static void do_handle_rx_pio(struct tegra_uart_port *t);
150 static void do_handle_rx_dma(struct tegra_uart_port *t);
151 static void set_rts(struct tegra_uart_port *t, bool active);
152 static void set_dtr(struct tegra_uart_port *t, bool active);
154 static void fill_tx_fifo(struct tegra_uart_port *t, int max_bytes)
157 struct circ_buf *xmit = &t->uport.state->xmit;
159 for (i = 0; i < max_bytes; i++) {
160 BUG_ON(uart_circ_empty(xmit));
161 uart_writeb(t, xmit->buf[xmit->tail], UART_TX);
162 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
163 t->uport.icount.tx++;
167 static void tegra_start_pio_tx(struct tegra_uart_port *t, unsigned int bytes)
169 if (bytes > TEGRA_UART_FIFO_SIZE)
170 bytes = TEGRA_UART_FIFO_SIZE;
172 t->fcr_shadow &= ~UART_FCR_T_TRIG_11;
173 t->fcr_shadow |= TEGRA_UART_TX_TRIG_8B;
174 uart_writeb(t, t->fcr_shadow, UART_FCR);
175 t->tx_in_progress = TEGRA_TX_PIO;
177 t->ier_shadow |= UART_IER_THRI;
178 uart_writeb(t, t->ier_shadow, UART_IER);
181 static void tegra_start_dma_tx(struct tegra_uart_port *t, unsigned long bytes)
183 struct circ_buf *xmit;
184 xmit = &t->uport.state->xmit;
186 dma_sync_single_for_device(t->uport.dev, t->xmit_dma_addr,
187 UART_XMIT_SIZE, DMA_TO_DEVICE);
189 t->fcr_shadow &= ~UART_FCR_T_TRIG_11;
190 t->fcr_shadow |= TEGRA_UART_TX_TRIG_4B;
191 uart_writeb(t, t->fcr_shadow, UART_FCR);
193 t->tx_bytes = bytes & ~(sizeof(u32)-1);
194 t->tx_dma_req.source_addr = t->xmit_dma_addr + xmit->tail;
195 t->tx_dma_req.size = t->tx_bytes;
197 t->tx_in_progress = TEGRA_TX_DMA;
199 tegra_dma_enqueue_req(t->tx_dma, &t->tx_dma_req);
202 /* Called with u->lock taken */
203 static void tegra_start_next_tx(struct tegra_uart_port *t)
208 struct circ_buf *xmit;
210 xmit = &t->uport.state->xmit;
211 tail = (unsigned long)&xmit->buf[xmit->tail];
212 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
215 dev_vdbg(t->uport.dev, "+%s %lu %d\n", __func__, count,
221 if (!t->use_tx_dma || count < TEGRA_UART_MIN_DMA)
222 tegra_start_pio_tx(t, count);
223 else if (BYTES_TO_ALIGN(tail) > 0)
224 tegra_start_pio_tx(t, BYTES_TO_ALIGN(tail));
226 tegra_start_dma_tx(t, count);
229 dev_vdbg(t->uport.dev, "-%s", __func__);
232 /* Called by serial core driver with u->lock taken. */
233 static void tegra_start_tx(struct uart_port *u)
235 struct tegra_uart_port *t;
236 struct circ_buf *xmit;
238 t = container_of(u, struct tegra_uart_port, uport);
239 xmit = &u->state->xmit;
241 if (!uart_circ_empty(xmit) && !t->tx_in_progress)
242 tegra_start_next_tx(t);
245 static int tegra_start_dma_rx(struct tegra_uart_port *t)
248 if (tegra_dma_enqueue_req(t->rx_dma, &t->rx_dma_req)) {
249 dev_err(t->uport.dev, "Could not enqueue Rx DMA req\n");
255 static void tegra_rx_dma_threshold_callback(struct tegra_dma_req *req)
257 struct tegra_uart_port *t = req->dev;
260 spin_lock_irqsave(&t->uport.lock, flags);
264 spin_unlock_irqrestore(&t->uport.lock, flags);
267 /* must be called with uart lock held */
268 static void tegra_rx_dma_complete_req(struct tegra_uart_port *t,
269 struct tegra_dma_req *req)
271 struct uart_port *u = &t->uport;
272 struct tty_struct *tty = u->state->port.tty;
274 /* If we are here, DMA is stopped */
276 dev_dbg(t->uport.dev, "%s: %d %d\n", __func__, req->bytes_transferred,
278 if (req->bytes_transferred) {
279 t->uport.icount.rx += req->bytes_transferred;
280 tty_insert_flip_string(tty,
281 ((unsigned char *)(req->virt_addr)),
282 req->bytes_transferred);
287 /* Push the read data later in caller place. */
288 if (req->status == -TEGRA_DMA_REQ_ERROR_ABORTED)
291 tty_flip_buffer_push(u->state->port.tty);
294 static void tegra_rx_dma_complete_callback(struct tegra_dma_req *req)
296 struct tegra_uart_port *t = req->dev;
300 * should never get called, dma should be dequeued during threshold
304 dev_warn(t->uport.dev, "possible rx overflow\n");
306 spin_lock_irqsave(&t->uport.lock, flags);
307 tegra_rx_dma_complete_req(t, req);
308 spin_unlock_irqrestore(&t->uport.lock, flags);
311 /* Lock already taken */
312 static void do_handle_rx_dma(struct tegra_uart_port *t)
314 struct uart_port *u = &t->uport;
317 if (!tegra_dma_dequeue_req(t->rx_dma, &t->rx_dma_req))
318 tegra_rx_dma_complete_req(t, &t->rx_dma_req);
320 tty_flip_buffer_push(u->state->port.tty);
321 /* enqueue the request again */
322 tegra_start_dma_rx(t);
327 /* Wait for a symbol-time. */
328 static void wait_sym_time(struct tegra_uart_port *t, unsigned int syms)
331 /* Definitely have a start bit. */
332 unsigned int bits = 1;
333 switch (t->lcr_shadow & 3) {
348 /* Technically 5 bits gets 1.5 bits of stop... */
349 if (t->lcr_shadow & UART_LCR_STOP) {
355 if (t->lcr_shadow & UART_LCR_PARITY)
359 udelay(DIV_ROUND_UP(syms * bits * 1000000, t->baud));
362 /* Flush desired FIFO. */
363 static void tegra_fifo_reset(struct tegra_uart_port *t, u8 fcr_bits)
365 unsigned char fcr = t->fcr_shadow;
366 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
367 uart_writeb(t, fcr, UART_FCR);
368 uart_readb(t, UART_SCR); /* Dummy read to ensure the write is posted */
369 wait_sym_time(t, 1); /* Wait for the flush to propagate. */
372 static char do_decode_rx_error(struct tegra_uart_port *t, u8 lsr)
374 char flag = TTY_NORMAL;
376 if (unlikely(lsr & UART_LSR_ANY)) {
377 if (lsr & UART_LSR_OE) {
380 t->uport.icount.overrun++;
381 dev_err(t->uport.dev, "Got overrun errors\n");
382 } else if (lsr & UART_LSR_PE) {
385 t->uport.icount.parity++;
386 dev_err(t->uport.dev, "Got Parity errors\n");
387 } else if (lsr & UART_LSR_FE) {
389 t->uport.icount.frame++;
390 dev_err(t->uport.dev, "Got frame errors\n");
391 } else if (lsr & UART_LSR_BI) {
392 dev_err(t->uport.dev, "Got Break\n");
393 t->uport.icount.brk++;
394 /* If FIFO read error without any data, reset Rx FIFO */
395 if (!(lsr & UART_LSR_DR) && (lsr & UART_LSR_FIFOE))
396 tegra_fifo_reset(t, UART_FCR_CLEAR_RCVR);
402 static void do_handle_rx_pio(struct tegra_uart_port *t)
406 char flag = TTY_NORMAL;
407 unsigned char lsr = 0;
411 lsr = uart_readb(t, UART_LSR);
412 if (!(lsr & UART_LSR_DR))
415 flag = do_decode_rx_error(t, lsr);
416 ch = uart_readb(t, UART_RX);
417 t->uport.icount.rx++;
420 if (!uart_handle_sysrq_char(&t->uport, c))
421 uart_insert_char(&t->uport, lsr, UART_LSR_OE, ch, flag);
424 dev_dbg(t->uport.dev, "PIO received %d bytes\n", count);
429 static void do_handle_modem_signal(struct uart_port *u)
432 struct tegra_uart_port *t;
434 t = container_of(u, struct tegra_uart_port, uport);
435 msr = uart_readb(t, UART_MSR);
436 if (msr & UART_MSR_CTS)
437 dev_dbg(u->dev, "CTS triggered\n");
438 if (msr & UART_MSR_DSR)
439 dev_dbg(u->dev, "DSR enabled\n");
440 if (msr & UART_MSR_DCD)
441 dev_dbg(u->dev, "CD enabled\n");
442 if (msr & UART_MSR_RI)
443 dev_dbg(u->dev, "RI enabled\n");
447 static void do_handle_tx_pio(struct tegra_uart_port *t)
449 struct circ_buf *xmit = &t->uport.state->xmit;
451 fill_tx_fifo(t, t->tx_bytes);
453 t->tx_in_progress = 0;
455 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
456 uart_write_wakeup(&t->uport);
458 tegra_start_next_tx(t);
462 static void tegra_tx_dma_complete_work(struct work_struct *work)
464 struct tegra_uart_port *t =
465 container_of(work, struct tegra_uart_port, tx_work);
466 struct tegra_dma_req *req = &t->tx_dma_req;
470 while ((uart_readb(t, UART_LSR) & TX_EMPTY_STATUS) != TX_EMPTY_STATUS) {
473 dev_err(t->uport.dev,
474 "timed out waiting for TX FIFO to empty\n");
480 spin_lock_irqsave(&t->uport.lock, flags);
482 t->tx_in_progress = 0;
484 if (req->status != -TEGRA_DMA_REQ_ERROR_ABORTED)
485 tegra_start_next_tx(t);
487 spin_unlock_irqrestore(&t->uport.lock, flags);
490 /* must be called with uart lock held */
491 static void tegra_tx_dma_complete_req(struct tegra_uart_port *t,
492 struct tegra_dma_req *req)
494 struct circ_buf *xmit = &t->uport.state->xmit;
495 int count = req->bytes_transferred;
497 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
499 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
500 uart_write_wakeup(&t->uport);
502 schedule_work(&t->tx_work);
505 static void tegra_tx_dma_complete_callback(struct tegra_dma_req *req)
507 struct tegra_uart_port *t = req->dev;
510 dev_vdbg(t->uport.dev, "%s: %d\n", __func__, req->bytes_transferred);
512 spin_lock_irqsave(&t->uport.lock, flags);
514 tegra_tx_dma_complete_req(t, req);
516 spin_unlock_irqrestore(&t->uport.lock, flags);
519 static irqreturn_t tegra_uart_isr(int irq, void *data)
521 struct tegra_uart_port *t = data;
522 struct uart_port *u = &t->uport;
525 bool is_rx_int = false;
528 spin_lock_irqsave(&u->lock, flags);
529 t = container_of(u, struct tegra_uart_port, uport);
531 iir = uart_readb(t, UART_IIR);
532 if (iir & UART_IIR_NO_INT) {
533 if (likely(t->use_rx_dma) && is_rx_int) {
536 if (t->rx_in_progress) {
538 ier |= (UART_IER_RLSI | UART_IER_RTOIE | UART_IER_EORD);
540 uart_writeb(t, ier, UART_IER);
543 spin_unlock_irqrestore(&u->lock, flags);
547 dev_dbg(u->dev, "tegra_uart_isr iir = 0x%x (%d)\n", iir,
549 switch ((iir >> 1) & 0x7) {
550 case 0: /* Modem signal change interrupt */
551 do_handle_modem_signal(u);
553 case 1: /* Transmit interrupt only triggered when using PIO */
554 t->ier_shadow &= ~UART_IER_THRI;
555 uart_writeb(t, t->ier_shadow, UART_IER);
558 case 4: /* End of data */
559 case 6: /* Rx timeout */
560 case 2: /* Receive */
561 if (likely(t->use_rx_dma)) {
564 /* Disable interrups */
567 uart_writeb(t, ier, UART_IER);
568 ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE | UART_IER_EORD);
570 uart_writeb(t, ier, UART_IER);
575 spin_unlock_irqrestore(&u->lock, flags);
576 tty_flip_buffer_push(u->state->port.tty);
577 spin_lock_irqsave(&u->lock, flags);
580 case 3: /* Receive error */
581 /* FIXME how to handle this? Why do we get here */
582 do_decode_rx_error(t, uart_readb(t, UART_LSR));
584 case 5: /* break nothing to handle */
585 case 7: /* break nothing to handle */
591 static void tegra_stop_rx(struct uart_port *u)
593 struct tegra_uart_port *t;
596 t = container_of(u, struct tegra_uart_port, uport);
601 if (t->rx_in_progress) {
602 wait_sym_time(t, 1); /* wait a character interval */
605 ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE | UART_IER_EORD);
607 uart_writeb(t, ier, UART_IER);
608 t->rx_in_progress = 0;
610 if (t->use_rx_dma && t->rx_dma) {
611 if (!tegra_dma_dequeue_req(t->rx_dma, &t->rx_dma_req))
612 tegra_rx_dma_complete_req(t, &t->rx_dma_req);
616 tty_flip_buffer_push(u->state->port.tty);
622 static void tegra_uart_hw_deinit(struct tegra_uart_port *t)
626 flush_work(&t->tx_work);
628 /* Disable interrupts */
629 uart_writeb(t, 0, UART_IER);
631 while ((uart_readb(t, UART_LSR) & UART_LSR_TEMT) != UART_LSR_TEMT);
634 spin_lock_irqsave(&t->uport.lock, flags);
636 /* Reset the Rx and Tx FIFOs */
637 tegra_fifo_reset(t, UART_FCR_CLEAR_XMIT | UART_FCR_CLEAR_RCVR);
642 spin_unlock_irqrestore(&t->uport.lock, flags);
645 static void tegra_uart_free_rx_dma(struct tegra_uart_port *t)
650 tegra_dma_free_channel(t->rx_dma);
653 if (likely(t->rx_dma_req.dest_addr))
654 dma_free_coherent(t->uport.dev, t->rx_dma_req.size,
655 t->rx_dma_req.virt_addr, t->rx_dma_req.dest_addr);
656 t->rx_dma_req.dest_addr = 0;
657 t->rx_dma_req.virt_addr = NULL;
659 t->use_rx_dma = false;
662 static int tegra_uart_hw_init(struct tegra_uart_port *t)
666 dev_vdbg(t->uport.dev, "+tegra_uart_hw_init\n");
676 /* Reset the UART controller to clear all previous status.*/
677 tegra_periph_reset_assert(t->clk);
679 tegra_periph_reset_deassert(t->clk);
682 t->rx_in_progress = 0;
684 /* Set the trigger level
688 * For receive, this will interrupt the CPU after that many number of
689 * bytes are received, for the remaining bytes the receive timeout
690 * interrupt is received.
692 * Rx high watermark is set to 4.
694 * For transmit, if the trasnmit interrupt is enabled, this will
695 * interrupt the CPU when the number of entries in the FIFO reaches the
698 * Tx low watermark is set to 8.
702 * Set the Tx trigger to 4. This should match the DMA burst size that
703 * programmed in the DMA registers.
705 t->fcr_shadow = UART_FCR_ENABLE_FIFO;
706 t->fcr_shadow |= UART_FCR_R_TRIG_01;
707 t->fcr_shadow |= TEGRA_UART_TX_TRIG_8B;
708 uart_writeb(t, t->fcr_shadow, UART_FCR);
711 /* initialize the UART for a simple default configuration
712 * so that the receive DMA buffer may be enqueued */
713 t->lcr_shadow = 3; /* no parity, stop, 8 data bits */
714 tegra_set_baudrate(t, 115200);
715 t->fcr_shadow |= UART_FCR_DMA_SELECT;
716 uart_writeb(t, t->fcr_shadow, UART_FCR);
717 if (tegra_start_dma_rx(t)) {
718 dev_err(t->uport.dev, "Rx DMA enqueue failed\n");
719 tegra_uart_free_rx_dma(t);
720 t->fcr_shadow &= ~UART_FCR_DMA_SELECT;
721 uart_writeb(t, t->fcr_shadow, UART_FCR);
725 uart_writeb(t, t->fcr_shadow, UART_FCR);
727 t->rx_in_progress = 1;
730 * Enable IE_RXS for the receive status interrupts like line errros.
731 * Enable IE_RX_TIMEOUT to get the bytes which cannot be DMA'd.
733 * If using DMA mode, enable EORD instead of receive interrupt which
734 * will interrupt after the UART is done with the receive instead of
735 * the interrupt when the FIFO "threshold" is reached.
737 * EORD is different interrupt than RX_TIMEOUT - RX_TIMEOUT occurs when
738 * the DATA is sitting in the FIFO and couldn't be transferred to the
739 * DMA as the DMA size alignment(4 bytes) is not met. EORD will be
740 * triggered when there is a pause of the incomming data stream for 4
743 * For pauses in the data which is not aligned to 4 bytes, we get
744 * both the EORD as well as RX_TIMEOUT - SW sees RX_TIMEOUT first
747 * Don't get confused, believe in the magic of nvidia hw...:-)
750 ier |= UART_IER_RLSI | UART_IER_RTOIE;
752 ier |= UART_IER_EORD;
756 uart_writeb(t, ier, UART_IER);
758 dev_vdbg(t->uport.dev, "-tegra_uart_hw_init\n");
762 static int tegra_uart_init_rx_dma(struct tegra_uart_port *t)
764 dma_addr_t rx_dma_phys;
767 t->rx_dma = tegra_dma_allocate_channel(TEGRA_DMA_MODE_CONTINUOUS);
769 dev_err(t->uport.dev, "%s: failed to allocate RX DMA.\n", __func__);
773 t->rx_dma_req.size = UART_RX_DMA_BUFFER_SIZE;
774 rx_dma_virt = dma_alloc_coherent(t->uport.dev,
775 t->rx_dma_req.size, &rx_dma_phys, GFP_KERNEL);
777 dev_err(t->uport.dev, "DMA buffers allocate failed\n");
780 t->rx_dma_req.dest_addr = rx_dma_phys;
781 t->rx_dma_req.virt_addr = rx_dma_virt;
783 t->rx_dma_req.source_addr = (unsigned long)t->uport.mapbase;
784 t->rx_dma_req.source_wrap = 4;
785 t->rx_dma_req.dest_wrap = 0;
786 t->rx_dma_req.to_memory = 1;
787 t->rx_dma_req.source_bus_width = 8;
788 t->rx_dma_req.dest_bus_width = 32;
789 t->rx_dma_req.req_sel = dma_req_sel[t->uport.line];
790 t->rx_dma_req.complete = tegra_rx_dma_complete_callback;
791 t->rx_dma_req.threshold = tegra_rx_dma_threshold_callback;
792 t->rx_dma_req.dev = t;
796 tegra_uart_free_rx_dma(t);
800 static int tegra_startup(struct uart_port *u)
802 struct tegra_uart_port *t = container_of(u,
803 struct tegra_uart_port, uport);
806 t = container_of(u, struct tegra_uart_port, uport);
807 sprintf(t->port_name, "tegra_uart_%d", u->line);
809 t->use_tx_dma = false;
811 t->tx_dma = tegra_dma_allocate_channel(TEGRA_DMA_MODE_ONESHOT);
813 t->use_tx_dma = true;
815 pr_err("%s: failed to allocate TX DMA.\n", __func__);
818 t->tx_dma_req.instance = u->line;
819 t->tx_dma_req.complete = tegra_tx_dma_complete_callback;
820 t->tx_dma_req.to_memory = 0;
822 t->tx_dma_req.dest_addr = (unsigned long)t->uport.mapbase;
823 t->tx_dma_req.dest_wrap = 4;
824 t->tx_dma_req.source_wrap = 0;
825 t->tx_dma_req.source_bus_width = 32;
826 t->tx_dma_req.dest_bus_width = 8;
827 t->tx_dma_req.req_sel = dma_req_sel[t->uport.line];
828 t->tx_dma_req.dev = t;
829 t->tx_dma_req.size = 0;
830 t->xmit_dma_addr = dma_map_single(t->uport.dev,
831 t->uport.state->xmit.buf, UART_XMIT_SIZE,
834 t->tx_in_progress = 0;
836 t->use_rx_dma = false;
838 if (!tegra_uart_init_rx_dma(t))
839 t->use_rx_dma = true;
842 ret = tegra_uart_hw_init(t);
846 dev_dbg(u->dev, "Requesting IRQ %d\n", u->irq);
849 ret = request_irq(u->irq, tegra_uart_isr, IRQF_DISABLED,
852 dev_err(u->dev, "Failed to register ISR for IRQ %d\n", u->irq);
855 dev_dbg(u->dev,"Started UART port %d\n", u->line);
859 dev_err(u->dev, "Tegra UART startup failed\n");
863 static void tegra_shutdown(struct uart_port *u)
865 struct tegra_uart_port *t;
867 t = container_of(u, struct tegra_uart_port, uport);
868 dev_vdbg(u->dev, "+tegra_shutdown\n");
870 tegra_uart_hw_deinit(t);
872 t->rx_in_progress = 0;
873 t->tx_in_progress = 0;
875 tegra_uart_free_rx_dma(t);
877 tegra_dma_free_channel(t->tx_dma);
879 t->use_tx_dma = false;
880 dma_unmap_single(t->uport.dev, t->xmit_dma_addr, UART_XMIT_SIZE,
882 t->xmit_dma_addr = 0;
886 dev_vdbg(u->dev, "-tegra_shutdown\n");
889 static unsigned int tegra_get_mctrl(struct uart_port *u)
891 /* RI - Ring detector is active
892 * CD/DCD/CAR - Carrier detect is always active. For some reason
893 * linux has different names for carrier detect.
894 * DSR - Data Set ready is active as the hardware doesn't support it.
895 * Don't know if the linux support this yet?
896 * CTS - Clear to send. Always set to active, as the hardware handles
899 return TIOCM_RI | TIOCM_CD | TIOCM_DSR | TIOCM_CTS;
902 static void set_rts(struct tegra_uart_port *t, bool active)
909 mcr &= ~UART_MCR_RTS;
910 if (mcr != t->mcr_shadow) {
911 uart_writeb(t, mcr, UART_MCR);
917 static void set_dtr(struct tegra_uart_port *t, bool active)
924 mcr &= ~UART_MCR_DTR;
925 if (mcr != t->mcr_shadow) {
926 uart_writeb(t, mcr, UART_MCR);
932 static void tegra_set_mctrl(struct uart_port *u, unsigned int mctrl)
935 struct tegra_uart_port *t;
937 dev_dbg(u->dev, "tegra_set_mctrl called with %d\n", mctrl);
938 t = container_of(u, struct tegra_uart_port, uport);
941 if (mctrl & TIOCM_RTS) {
942 t->rts_active = true;
945 t->rts_active = false;
949 if (mctrl & TIOCM_DTR)
956 static void tegra_break_ctl(struct uart_port *u, int break_ctl)
958 struct tegra_uart_port *t;
961 t = container_of(u, struct tegra_uart_port, uport);
966 lcr &= ~UART_LCR_SBC;
967 uart_writeb(t, lcr, UART_LCR);
971 static int tegra_request_port(struct uart_port *u)
976 static void tegra_release_port(struct uart_port *u)
981 static unsigned int tegra_tx_empty(struct uart_port *u)
983 struct tegra_uart_port *t;
984 unsigned int ret = 0;
987 t = container_of(u, struct tegra_uart_port, uport);
988 dev_vdbg(u->dev, "+tegra_tx_empty\n");
990 spin_lock_irqsave(&u->lock, flags);
991 if (!t->tx_in_progress)
993 spin_unlock_irqrestore(&u->lock, flags);
995 dev_vdbg(u->dev, "-tegra_tx_empty\n");
999 static void tegra_stop_tx(struct uart_port *u)
1001 struct tegra_uart_port *t;
1003 t = container_of(u, struct tegra_uart_port, uport);
1005 if (t->use_tx_dma) {
1006 if (!tegra_dma_dequeue_req(t->tx_dma, &t->tx_dma_req))
1007 tegra_tx_dma_complete_req(t, &t->tx_dma_req);
1013 static void tegra_enable_ms(struct uart_port *u)
1017 #define UART_CLOCK_ACCURACY 5
1019 static void tegra_set_baudrate(struct tegra_uart_port *t, unsigned int baud)
1022 unsigned int divisor;
1025 if (t->baud == baud)
1028 rate = clk_get_rate(t->clk);
1031 do_div(divisor, 16);
1033 do_div(divisor, baud);
1035 lcr = t->lcr_shadow;
1036 lcr |= UART_LCR_DLAB;
1037 uart_writeb(t, lcr, UART_LCR);
1039 uart_writel(t, divisor & 0xFF, UART_TX);
1040 uart_writel(t, ((divisor >> 8) & 0xFF), UART_IER);
1042 lcr &= ~UART_LCR_DLAB;
1043 uart_writeb(t, lcr, UART_LCR);
1044 uart_readb(t, UART_SCR); /* Dummy read to ensure the write is posted */
1047 wait_sym_time(t, 2); /* wait two character intervals at new rate */
1048 dev_dbg(t->uport.dev, "Baud %u clock freq %lu and divisor of %u\n",
1049 baud, rate, divisor);
1052 static void tegra_set_termios(struct uart_port *u, struct ktermios *termios,
1053 struct ktermios *oldtermios)
1055 struct tegra_uart_port *t;
1057 unsigned long flags;
1059 unsigned int c_cflag = termios->c_cflag;
1062 t = container_of(u, struct tegra_uart_port, uport);
1063 dev_vdbg(t->uport.dev, "+tegra_set_termios\n");
1065 spin_lock_irqsave(&u->lock, flags);
1067 /* Changing configuration, it is safe to stop any rx now */
1072 lcr = t->lcr_shadow;
1073 lcr &= ~UART_LCR_PARITY;
1074 if (PARENB == (c_cflag & PARENB)) {
1075 if (CMSPAR == (c_cflag & CMSPAR)) {
1076 /* FIXME What is space parity? */
1077 /* data |= SPACE_PARITY; */
1078 } else if (c_cflag & PARODD) {
1079 lcr |= UART_LCR_PARITY;
1080 lcr &= ~UART_LCR_EPAR;
1081 lcr &= ~UART_LCR_SPAR;
1083 lcr |= UART_LCR_PARITY;
1084 lcr |= UART_LCR_EPAR;
1085 lcr &= ~UART_LCR_SPAR;
1089 lcr &= ~UART_LCR_WLEN8;
1090 switch (c_cflag & CSIZE) {
1092 lcr |= UART_LCR_WLEN5;
1095 lcr |= UART_LCR_WLEN6;
1098 lcr |= UART_LCR_WLEN7;
1101 lcr |= UART_LCR_WLEN8;
1106 if (termios->c_cflag & CSTOPB)
1107 lcr |= UART_LCR_STOP;
1109 lcr &= ~UART_LCR_STOP;
1111 uart_writeb(t, lcr, UART_LCR);
1112 t->lcr_shadow = lcr;
1115 baud = uart_get_baud_rate(u, termios, oldtermios, 200, 4000000);
1116 tegra_set_baudrate(t, baud);
1119 if (termios->c_cflag & CRTSCTS) {
1120 mcr = t->mcr_shadow;
1121 mcr |= UART_MCR_CTS_EN;
1122 mcr &= ~UART_MCR_RTS_EN;
1123 t->mcr_shadow = mcr;
1124 uart_writeb(t, mcr, UART_MCR);
1125 t->use_cts_control = true;
1126 /* if top layer has asked to set rts active then do so here */
1130 mcr = t->mcr_shadow;
1131 mcr &= ~UART_MCR_CTS_EN;
1132 mcr &= ~UART_MCR_RTS_EN;
1133 t->mcr_shadow = mcr;
1134 uart_writeb(t, mcr, UART_MCR);
1135 t->use_cts_control = false;
1138 /* update the port timeout based on new settings */
1139 uart_update_timeout(u, termios->c_cflag, baud);
1141 spin_unlock_irqrestore(&u->lock, flags);
1142 dev_vdbg(t->uport.dev, "-tegra_set_termios\n");
1147 * Flush any TX data submitted for DMA. Called when the TX circular
1150 static void tegra_flush_buffer(struct uart_port *u)
1152 struct tegra_uart_port *t;
1154 dev_vdbg(u->dev, "%s called", __func__);
1156 t = container_of(u, struct tegra_uart_port, uport);
1158 if (t->use_tx_dma) {
1159 if (!tegra_dma_dequeue_req(t->tx_dma, &t->tx_dma_req))
1160 tegra_tx_dma_complete_req(t, &t->tx_dma_req);
1161 t->tx_dma_req.size = 0;
1167 static void tegra_pm(struct uart_port *u, unsigned int state,
1168 unsigned int oldstate)
1173 static const char *tegra_type(struct uart_port *u)
1178 static struct uart_ops tegra_uart_ops = {
1179 .tx_empty = tegra_tx_empty,
1180 .set_mctrl = tegra_set_mctrl,
1181 .get_mctrl = tegra_get_mctrl,
1182 .stop_tx = tegra_stop_tx,
1183 .start_tx = tegra_start_tx,
1184 .stop_rx = tegra_stop_rx,
1185 .flush_buffer = tegra_flush_buffer,
1186 .enable_ms = tegra_enable_ms,
1187 .break_ctl = tegra_break_ctl,
1188 .startup = tegra_startup,
1189 .shutdown = tegra_shutdown,
1190 .set_termios = tegra_set_termios,
1193 .request_port = tegra_request_port,
1194 .release_port = tegra_release_port,
1197 static int tegra_uart_probe(struct platform_device *pdev);
1198 static int __devexit tegra_uart_remove(struct platform_device *pdev);
1199 static int tegra_uart_suspend(struct platform_device *pdev, pm_message_t state);
1200 static int tegra_uart_resume(struct platform_device *pdev);
1202 static struct platform_driver tegra_uart_platform_driver = {
1203 .remove = tegra_uart_remove,
1204 .probe = tegra_uart_probe,
1205 .suspend = tegra_uart_suspend,
1206 .resume = tegra_uart_resume,
1208 .name = "tegra_uart"
1212 static struct uart_driver tegra_uart_driver =
1214 .owner = THIS_MODULE,
1215 .driver_name = "tegra_uart",
1216 .dev_name = "ttyHS",
1221 static int tegra_uart_suspend(struct platform_device *pdev, pm_message_t state)
1223 struct tegra_uart_port *t = platform_get_drvdata(pdev);
1224 struct uart_port *u;
1226 if (pdev->id < 0 || pdev->id > tegra_uart_driver.nr)
1227 pr_err("Invalid Uart instance (%d)\n", pdev->id);
1230 uart_suspend_port(&tegra_uart_driver, u);
1232 flush_work(&t->tx_work);
1236 static int tegra_uart_resume(struct platform_device *pdev)
1238 struct tegra_uart_port *t = platform_get_drvdata(pdev);
1239 struct uart_port *u;
1241 if (pdev->id < 0 || pdev->id > tegra_uart_driver.nr)
1242 pr_err("Invalid Uart instance (%d)\n", pdev->id);
1245 uart_resume_port(&tegra_uart_driver, u);
1251 static int __devexit tegra_uart_remove(struct platform_device *pdev)
1253 struct tegra_uart_port *t = platform_get_drvdata(pdev);
1254 struct uart_port *u;
1256 if (pdev->id < 0 || pdev->id > tegra_uart_driver.nr)
1257 pr_err("Invalid Uart instance (%d)\n", pdev->id);
1260 uart_remove_one_port(&tegra_uart_driver, u);
1262 platform_set_drvdata(pdev, NULL);
1264 pr_info("Unregistered UART port %s%d\n",
1265 tegra_uart_driver.dev_name, u->line);
1270 static int tegra_uart_probe(struct platform_device *pdev)
1272 struct tegra_uart_port *t;
1273 struct uart_port *u;
1274 struct resource *resource;
1277 if (pdev->id < 0 || pdev->id > tegra_uart_driver.nr) {
1278 pr_err("Invalid Uart instance (%d)\n", pdev->id);
1282 t = kzalloc(sizeof(struct tegra_uart_port), GFP_KERNEL);
1284 pr_err("%s: Failed to allocate memory\n", __func__);
1288 u->dev = &pdev->dev;
1289 platform_set_drvdata(pdev, u);
1291 u->ops = &tegra_uart_ops;
1292 u->type = ~PORT_UNKNOWN;
1295 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1296 if (unlikely(!resource))
1299 u->mapbase = resource->start;
1300 u->membase = IO_ADDRESS(u->mapbase);
1301 if (unlikely(!u->membase))
1304 u->irq = platform_get_irq(pdev, 0);
1305 if (unlikely(u->irq < 0))
1310 t->clk = clk_get(&pdev->dev, NULL);
1312 dev_err(&pdev->dev, "Couldn't get the clock\n");
1316 ret = uart_add_one_port(&tegra_uart_driver, u);
1318 pr_err("%s: Failed(%d) to add uart port %s%d\n",
1319 __func__, ret, tegra_uart_driver.dev_name, u->line);
1321 platform_set_drvdata(pdev, NULL);
1325 snprintf(name, sizeof(name), "tegra_hsuart_%d", u->line);
1326 pr_info("Registered UART port %s%d\n",
1327 tegra_uart_driver.dev_name, u->line);
1329 INIT_WORK(&t->tx_work, tegra_tx_dma_complete_work);
1336 static int __init tegra_uart_init(void)
1340 ret = uart_register_driver(&tegra_uart_driver);
1341 if (unlikely(ret)) {
1342 pr_err("Could not register %s driver\n",
1343 tegra_uart_driver.driver_name);
1347 ret = platform_driver_register(&tegra_uart_platform_driver);
1348 if (unlikely(ret)) {
1349 pr_err("Could not register the UART platfrom "
1351 uart_unregister_driver(&tegra_uart_driver);
1355 pr_info("Initialized tegra uart driver\n");
1359 static void __exit tegra_uart_exit(void)
1361 pr_info("Unloading tegra uart driver\n");
1362 platform_driver_unregister(&tegra_uart_platform_driver);
1363 uart_unregister_driver(&tegra_uart_driver);
1366 module_init(tegra_uart_init);
1367 module_exit(tegra_uart_exit);
1368 MODULE_DESCRIPTION("High speed UART driver for tegra chipset");