1 // SPDX-License-Identifier: GPL-2.0
5 * High-speed serial driver for NVIDIA Tegra SoCs
7 * Copyright (c) 2012-2019, NVIDIA CORPORATION. All rights reserved.
9 * Author: Laxman Dewangan <ldewangan@nvidia.com>
12 #include <linux/clk.h>
13 #include <linux/debugfs.h>
14 #include <linux/delay.h>
15 #include <linux/dmaengine.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/dmapool.h>
18 #include <linux/err.h>
20 #include <linux/irq.h>
21 #include <linux/module.h>
23 #include <linux/of_device.h>
24 #include <linux/pagemap.h>
25 #include <linux/platform_device.h>
26 #include <linux/reset.h>
27 #include <linux/serial.h>
28 #include <linux/serial_8250.h>
29 #include <linux/serial_core.h>
30 #include <linux/serial_reg.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
33 #include <linux/termios.h>
34 #include <linux/tty.h>
35 #include <linux/tty_flip.h>
37 #define TEGRA_UART_TYPE "TEGRA_UART"
38 #define TX_EMPTY_STATUS (UART_LSR_TEMT | UART_LSR_THRE)
39 #define BYTES_TO_ALIGN(x) ((unsigned long)(x) & 0x3)
41 #define TEGRA_UART_RX_DMA_BUFFER_SIZE 4096
42 #define TEGRA_UART_LSR_TXFIFO_FULL 0x100
43 #define TEGRA_UART_IER_EORD 0x20
44 #define TEGRA_UART_MCR_RTS_EN 0x40
45 #define TEGRA_UART_MCR_CTS_EN 0x20
46 #define TEGRA_UART_LSR_ANY (UART_LSR_OE | UART_LSR_BI | \
47 UART_LSR_PE | UART_LSR_FE)
48 #define TEGRA_UART_IRDA_CSR 0x08
49 #define TEGRA_UART_SIR_ENABLED 0x80
51 #define TEGRA_UART_TX_PIO 1
52 #define TEGRA_UART_TX_DMA 2
53 #define TEGRA_UART_MIN_DMA 16
54 #define TEGRA_UART_FIFO_SIZE 32
57 * Tx fifo trigger level setting in tegra uart is in
58 * reverse way then conventional uart.
60 #define TEGRA_UART_TX_TRIG_16B 0x00
61 #define TEGRA_UART_TX_TRIG_8B 0x10
62 #define TEGRA_UART_TX_TRIG_4B 0x20
63 #define TEGRA_UART_TX_TRIG_1B 0x30
65 #define TEGRA_UART_MAXIMUM 5
67 /* Default UART setting when started: 115200 no parity, stop, 8 data bits */
68 #define TEGRA_UART_DEFAULT_BAUD 115200
69 #define TEGRA_UART_DEFAULT_LSR UART_LCR_WLEN8
71 /* Tx transfer mode */
72 #define TEGRA_TX_PIO 1
73 #define TEGRA_TX_DMA 2
76 * tegra_uart_chip_data: SOC specific data.
78 * @tx_fifo_full_status: Status flag available for checking tx fifo full.
79 * @allow_txfifo_reset_fifo_mode: allow_tx fifo reset with fifo mode or not.
80 * Tegra30 does not allow this.
81 * @support_clk_src_div: Clock source support the clock divider.
83 struct tegra_uart_chip_data {
84 bool tx_fifo_full_status;
85 bool allow_txfifo_reset_fifo_mode;
86 bool support_clk_src_div;
89 struct tegra_uart_port {
90 struct uart_port uport;
91 const struct tegra_uart_chip_data *cdata;
94 struct reset_control *rst;
95 unsigned int current_baud;
98 unsigned long fcr_shadow;
99 unsigned long mcr_shadow;
100 unsigned long lcr_shadow;
101 unsigned long ier_shadow;
105 unsigned int tx_bytes;
107 bool enable_modem_interrupt;
113 struct dma_chan *rx_dma_chan;
114 struct dma_chan *tx_dma_chan;
115 dma_addr_t rx_dma_buf_phys;
116 dma_addr_t tx_dma_buf_phys;
117 unsigned char *rx_dma_buf_virt;
118 unsigned char *tx_dma_buf_virt;
119 struct dma_async_tx_descriptor *tx_dma_desc;
120 struct dma_async_tx_descriptor *rx_dma_desc;
121 dma_cookie_t tx_cookie;
122 dma_cookie_t rx_cookie;
123 unsigned int tx_bytes_requested;
124 unsigned int rx_bytes_requested;
127 static void tegra_uart_start_next_tx(struct tegra_uart_port *tup);
128 static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup);
129 static void tegra_uart_dma_channel_free(struct tegra_uart_port *tup,
132 static inline unsigned long tegra_uart_read(struct tegra_uart_port *tup,
135 return readl(tup->uport.membase + (reg << tup->uport.regshift));
138 static inline void tegra_uart_write(struct tegra_uart_port *tup, unsigned val,
141 writel(val, tup->uport.membase + (reg << tup->uport.regshift));
144 static inline struct tegra_uart_port *to_tegra_uport(struct uart_port *u)
146 return container_of(u, struct tegra_uart_port, uport);
149 static unsigned int tegra_uart_get_mctrl(struct uart_port *u)
151 struct tegra_uart_port *tup = to_tegra_uport(u);
154 * RI - Ring detector is active
155 * CD/DCD/CAR - Carrier detect is always active. For some reason
156 * linux has different names for carrier detect.
157 * DSR - Data Set ready is active as the hardware doesn't support it.
158 * Don't know if the linux support this yet?
159 * CTS - Clear to send. Always set to active, as the hardware handles
162 if (tup->enable_modem_interrupt)
163 return TIOCM_RI | TIOCM_CD | TIOCM_DSR | TIOCM_CTS;
167 static void set_rts(struct tegra_uart_port *tup, bool active)
171 mcr = tup->mcr_shadow;
173 mcr |= TEGRA_UART_MCR_RTS_EN;
175 mcr &= ~TEGRA_UART_MCR_RTS_EN;
176 if (mcr != tup->mcr_shadow) {
177 tegra_uart_write(tup, mcr, UART_MCR);
178 tup->mcr_shadow = mcr;
182 static void set_dtr(struct tegra_uart_port *tup, bool active)
186 mcr = tup->mcr_shadow;
190 mcr &= ~UART_MCR_DTR;
191 if (mcr != tup->mcr_shadow) {
192 tegra_uart_write(tup, mcr, UART_MCR);
193 tup->mcr_shadow = mcr;
197 static void set_loopbk(struct tegra_uart_port *tup, bool active)
199 unsigned long mcr = tup->mcr_shadow;
202 mcr |= UART_MCR_LOOP;
204 mcr &= ~UART_MCR_LOOP;
206 if (mcr != tup->mcr_shadow) {
207 tegra_uart_write(tup, mcr, UART_MCR);
208 tup->mcr_shadow = mcr;
212 static void tegra_uart_set_mctrl(struct uart_port *u, unsigned int mctrl)
214 struct tegra_uart_port *tup = to_tegra_uport(u);
217 tup->rts_active = !!(mctrl & TIOCM_RTS);
218 set_rts(tup, tup->rts_active);
220 enable = !!(mctrl & TIOCM_DTR);
221 set_dtr(tup, enable);
223 enable = !!(mctrl & TIOCM_LOOP);
224 set_loopbk(tup, enable);
227 static void tegra_uart_break_ctl(struct uart_port *u, int break_ctl)
229 struct tegra_uart_port *tup = to_tegra_uport(u);
232 lcr = tup->lcr_shadow;
236 lcr &= ~UART_LCR_SBC;
237 tegra_uart_write(tup, lcr, UART_LCR);
238 tup->lcr_shadow = lcr;
242 * tegra_uart_wait_cycle_time: Wait for N UART clock periods
244 * @tup: Tegra serial port data structure.
245 * @cycles: Number of clock periods to wait.
247 * Tegra UARTs are clocked at 16X the baud/bit rate and hence the UART
248 * clock speed is 16X the current baud rate.
250 static void tegra_uart_wait_cycle_time(struct tegra_uart_port *tup,
253 if (tup->current_baud)
254 udelay(DIV_ROUND_UP(cycles * 1000000, tup->current_baud * 16));
257 /* Wait for a symbol-time. */
258 static void tegra_uart_wait_sym_time(struct tegra_uart_port *tup,
261 if (tup->current_baud)
262 udelay(DIV_ROUND_UP(syms * tup->symb_bit * 1000000,
266 static void tegra_uart_fifo_reset(struct tegra_uart_port *tup, u8 fcr_bits)
268 unsigned long fcr = tup->fcr_shadow;
269 unsigned int lsr, tmout = 10000;
274 if (tup->cdata->allow_txfifo_reset_fifo_mode) {
275 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
276 tegra_uart_write(tup, fcr, UART_FCR);
278 fcr &= ~UART_FCR_ENABLE_FIFO;
279 tegra_uart_write(tup, fcr, UART_FCR);
281 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
282 tegra_uart_write(tup, fcr, UART_FCR);
283 fcr |= UART_FCR_ENABLE_FIFO;
284 tegra_uart_write(tup, fcr, UART_FCR);
287 /* Dummy read to ensure the write is posted */
288 tegra_uart_read(tup, UART_SCR);
291 * For all tegra devices (up to t210), there is a hardware issue that
292 * requires software to wait for 32 UART clock periods for the flush
293 * to propagate, otherwise data could be lost.
295 tegra_uart_wait_cycle_time(tup, 32);
298 lsr = tegra_uart_read(tup, UART_LSR);
299 if ((lsr | UART_LSR_TEMT) && !(lsr & UART_LSR_DR))
308 static int tegra_set_baudrate(struct tegra_uart_port *tup, unsigned int baud)
311 unsigned int divisor;
316 if (tup->current_baud == baud)
319 if (tup->cdata->support_clk_src_div) {
321 ret = clk_set_rate(tup->uart_clk, rate);
323 dev_err(tup->uport.dev,
324 "clk_set_rate() failed for rate %lu\n", rate);
329 rate = clk_get_rate(tup->uart_clk);
330 divisor = DIV_ROUND_CLOSEST(rate, baud * 16);
333 spin_lock_irqsave(&tup->uport.lock, flags);
334 lcr = tup->lcr_shadow;
335 lcr |= UART_LCR_DLAB;
336 tegra_uart_write(tup, lcr, UART_LCR);
338 tegra_uart_write(tup, divisor & 0xFF, UART_TX);
339 tegra_uart_write(tup, ((divisor >> 8) & 0xFF), UART_IER);
341 lcr &= ~UART_LCR_DLAB;
342 tegra_uart_write(tup, lcr, UART_LCR);
344 /* Dummy read to ensure the write is posted */
345 tegra_uart_read(tup, UART_SCR);
346 spin_unlock_irqrestore(&tup->uport.lock, flags);
348 tup->current_baud = baud;
350 /* wait two character intervals at new rate */
351 tegra_uart_wait_sym_time(tup, 2);
355 static char tegra_uart_decode_rx_error(struct tegra_uart_port *tup,
358 char flag = TTY_NORMAL;
360 if (unlikely(lsr & TEGRA_UART_LSR_ANY)) {
361 if (lsr & UART_LSR_OE) {
364 tup->uport.icount.overrun++;
365 dev_err(tup->uport.dev, "Got overrun errors\n");
366 } else if (lsr & UART_LSR_PE) {
369 tup->uport.icount.parity++;
370 dev_err(tup->uport.dev, "Got Parity errors\n");
371 } else if (lsr & UART_LSR_FE) {
373 tup->uport.icount.frame++;
374 dev_err(tup->uport.dev, "Got frame errors\n");
375 } else if (lsr & UART_LSR_BI) {
378 * If FIFO read error without any data, reset Rx FIFO
380 if (!(lsr & UART_LSR_DR) && (lsr & UART_LSR_FIFOE))
381 tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_RCVR);
382 if (tup->uport.ignore_status_mask & UART_LSR_BI)
385 tup->uport.icount.brk++;
386 dev_dbg(tup->uport.dev, "Got Break\n");
388 uart_insert_char(&tup->uport, lsr, UART_LSR_OE, 0, flag);
394 static int tegra_uart_request_port(struct uart_port *u)
399 static void tegra_uart_release_port(struct uart_port *u)
401 /* Nothing to do here */
404 static void tegra_uart_fill_tx_fifo(struct tegra_uart_port *tup, int max_bytes)
406 struct circ_buf *xmit = &tup->uport.state->xmit;
409 for (i = 0; i < max_bytes; i++) {
410 BUG_ON(uart_circ_empty(xmit));
411 if (tup->cdata->tx_fifo_full_status) {
412 unsigned long lsr = tegra_uart_read(tup, UART_LSR);
413 if ((lsr & TEGRA_UART_LSR_TXFIFO_FULL))
416 tegra_uart_write(tup, xmit->buf[xmit->tail], UART_TX);
417 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
418 tup->uport.icount.tx++;
422 static void tegra_uart_start_pio_tx(struct tegra_uart_port *tup,
425 if (bytes > TEGRA_UART_MIN_DMA)
426 bytes = TEGRA_UART_MIN_DMA;
428 tup->tx_in_progress = TEGRA_UART_TX_PIO;
429 tup->tx_bytes = bytes;
430 tup->ier_shadow |= UART_IER_THRI;
431 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
434 static void tegra_uart_tx_dma_complete(void *args)
436 struct tegra_uart_port *tup = args;
437 struct circ_buf *xmit = &tup->uport.state->xmit;
438 struct dma_tx_state state;
442 dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
443 count = tup->tx_bytes_requested - state.residue;
444 async_tx_ack(tup->tx_dma_desc);
445 spin_lock_irqsave(&tup->uport.lock, flags);
446 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
447 tup->tx_in_progress = 0;
448 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
449 uart_write_wakeup(&tup->uport);
450 tegra_uart_start_next_tx(tup);
451 spin_unlock_irqrestore(&tup->uport.lock, flags);
454 static int tegra_uart_start_tx_dma(struct tegra_uart_port *tup,
457 struct circ_buf *xmit = &tup->uport.state->xmit;
458 dma_addr_t tx_phys_addr;
460 dma_sync_single_for_device(tup->uport.dev, tup->tx_dma_buf_phys,
461 UART_XMIT_SIZE, DMA_TO_DEVICE);
463 tup->tx_bytes = count & ~(0xF);
464 tx_phys_addr = tup->tx_dma_buf_phys + xmit->tail;
465 tup->tx_dma_desc = dmaengine_prep_slave_single(tup->tx_dma_chan,
466 tx_phys_addr, tup->tx_bytes, DMA_MEM_TO_DEV,
468 if (!tup->tx_dma_desc) {
469 dev_err(tup->uport.dev, "Not able to get desc for Tx\n");
473 tup->tx_dma_desc->callback = tegra_uart_tx_dma_complete;
474 tup->tx_dma_desc->callback_param = tup;
475 tup->tx_in_progress = TEGRA_UART_TX_DMA;
476 tup->tx_bytes_requested = tup->tx_bytes;
477 tup->tx_cookie = dmaengine_submit(tup->tx_dma_desc);
478 dma_async_issue_pending(tup->tx_dma_chan);
482 static void tegra_uart_start_next_tx(struct tegra_uart_port *tup)
486 struct circ_buf *xmit = &tup->uport.state->xmit;
488 if (!tup->current_baud)
491 tail = (unsigned long)&xmit->buf[xmit->tail];
492 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
496 if (count < TEGRA_UART_MIN_DMA)
497 tegra_uart_start_pio_tx(tup, count);
498 else if (BYTES_TO_ALIGN(tail) > 0)
499 tegra_uart_start_pio_tx(tup, BYTES_TO_ALIGN(tail));
501 tegra_uart_start_tx_dma(tup, count);
504 /* Called by serial core driver with u->lock taken. */
505 static void tegra_uart_start_tx(struct uart_port *u)
507 struct tegra_uart_port *tup = to_tegra_uport(u);
508 struct circ_buf *xmit = &u->state->xmit;
510 if (!uart_circ_empty(xmit) && !tup->tx_in_progress)
511 tegra_uart_start_next_tx(tup);
514 static unsigned int tegra_uart_tx_empty(struct uart_port *u)
516 struct tegra_uart_port *tup = to_tegra_uport(u);
517 unsigned int ret = 0;
520 spin_lock_irqsave(&u->lock, flags);
521 if (!tup->tx_in_progress) {
522 unsigned long lsr = tegra_uart_read(tup, UART_LSR);
523 if ((lsr & TX_EMPTY_STATUS) == TX_EMPTY_STATUS)
526 spin_unlock_irqrestore(&u->lock, flags);
530 static void tegra_uart_stop_tx(struct uart_port *u)
532 struct tegra_uart_port *tup = to_tegra_uport(u);
533 struct circ_buf *xmit = &tup->uport.state->xmit;
534 struct dma_tx_state state;
537 if (tup->tx_in_progress != TEGRA_UART_TX_DMA)
540 dmaengine_terminate_all(tup->tx_dma_chan);
541 dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
542 count = tup->tx_bytes_requested - state.residue;
543 async_tx_ack(tup->tx_dma_desc);
544 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
545 tup->tx_in_progress = 0;
548 static void tegra_uart_handle_tx_pio(struct tegra_uart_port *tup)
550 struct circ_buf *xmit = &tup->uport.state->xmit;
552 tegra_uart_fill_tx_fifo(tup, tup->tx_bytes);
553 tup->tx_in_progress = 0;
554 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
555 uart_write_wakeup(&tup->uport);
556 tegra_uart_start_next_tx(tup);
559 static void tegra_uart_handle_rx_pio(struct tegra_uart_port *tup,
560 struct tty_port *tty)
563 char flag = TTY_NORMAL;
564 unsigned long lsr = 0;
567 lsr = tegra_uart_read(tup, UART_LSR);
568 if (!(lsr & UART_LSR_DR))
571 flag = tegra_uart_decode_rx_error(tup, lsr);
572 if (flag != TTY_NORMAL)
575 ch = (unsigned char) tegra_uart_read(tup, UART_RX);
576 tup->uport.icount.rx++;
578 if (!uart_handle_sysrq_char(&tup->uport, ch) && tty)
579 tty_insert_flip_char(tty, ch, flag);
581 if (tup->uport.ignore_status_mask & UART_LSR_DR)
586 static void tegra_uart_copy_rx_to_tty(struct tegra_uart_port *tup,
587 struct tty_port *tty,
592 /* If count is zero, then there is no data to be copied */
596 tup->uport.icount.rx += count;
598 dev_err(tup->uport.dev, "No tty port\n");
602 if (tup->uport.ignore_status_mask & UART_LSR_DR)
605 dma_sync_single_for_cpu(tup->uport.dev, tup->rx_dma_buf_phys,
606 TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
607 copied = tty_insert_flip_string(tty,
608 ((unsigned char *)(tup->rx_dma_buf_virt)), count);
609 if (copied != count) {
611 dev_err(tup->uport.dev, "RxData copy to tty layer failed\n");
613 dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
614 TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_TO_DEVICE);
617 static void tegra_uart_rx_buffer_push(struct tegra_uart_port *tup,
618 unsigned int residue)
620 struct tty_port *port = &tup->uport.state->port;
621 struct tty_struct *tty = tty_port_tty_get(port);
624 async_tx_ack(tup->rx_dma_desc);
625 count = tup->rx_bytes_requested - residue;
627 /* If we are here, DMA is stopped */
628 tegra_uart_copy_rx_to_tty(tup, port, count);
630 tegra_uart_handle_rx_pio(tup, port);
632 tty_flip_buffer_push(port);
637 static void tegra_uart_rx_dma_complete(void *args)
639 struct tegra_uart_port *tup = args;
640 struct uart_port *u = &tup->uport;
642 struct dma_tx_state state;
643 enum dma_status status;
645 spin_lock_irqsave(&u->lock, flags);
647 status = dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
649 if (status == DMA_IN_PROGRESS) {
650 dev_dbg(tup->uport.dev, "RX DMA is in progress\n");
654 /* Deactivate flow control to stop sender */
658 tegra_uart_rx_buffer_push(tup, 0);
659 tegra_uart_start_rx_dma(tup);
661 /* Activate flow control to start transfer */
666 spin_unlock_irqrestore(&u->lock, flags);
669 static void tegra_uart_handle_rx_dma(struct tegra_uart_port *tup)
671 struct dma_tx_state state;
673 /* Deactivate flow control to stop sender */
677 dmaengine_terminate_all(tup->rx_dma_chan);
678 dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
679 tegra_uart_rx_buffer_push(tup, state.residue);
680 tegra_uart_start_rx_dma(tup);
686 static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup)
688 unsigned int count = TEGRA_UART_RX_DMA_BUFFER_SIZE;
690 tup->rx_dma_desc = dmaengine_prep_slave_single(tup->rx_dma_chan,
691 tup->rx_dma_buf_phys, count, DMA_DEV_TO_MEM,
693 if (!tup->rx_dma_desc) {
694 dev_err(tup->uport.dev, "Not able to get desc for Rx\n");
698 tup->rx_dma_desc->callback = tegra_uart_rx_dma_complete;
699 tup->rx_dma_desc->callback_param = tup;
700 dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
701 count, DMA_TO_DEVICE);
702 tup->rx_bytes_requested = count;
703 tup->rx_cookie = dmaengine_submit(tup->rx_dma_desc);
704 dma_async_issue_pending(tup->rx_dma_chan);
708 static void tegra_uart_handle_modem_signal_change(struct uart_port *u)
710 struct tegra_uart_port *tup = to_tegra_uport(u);
713 msr = tegra_uart_read(tup, UART_MSR);
714 if (!(msr & UART_MSR_ANY_DELTA))
717 if (msr & UART_MSR_TERI)
718 tup->uport.icount.rng++;
719 if (msr & UART_MSR_DDSR)
720 tup->uport.icount.dsr++;
721 /* We may only get DDCD when HW init and reset */
722 if (msr & UART_MSR_DDCD)
723 uart_handle_dcd_change(&tup->uport, msr & UART_MSR_DCD);
724 /* Will start/stop_tx accordingly */
725 if (msr & UART_MSR_DCTS)
726 uart_handle_cts_change(&tup->uport, msr & UART_MSR_CTS);
729 static irqreturn_t tegra_uart_isr(int irq, void *data)
731 struct tegra_uart_port *tup = data;
732 struct uart_port *u = &tup->uport;
735 bool is_rx_int = false;
738 spin_lock_irqsave(&u->lock, flags);
740 iir = tegra_uart_read(tup, UART_IIR);
741 if (iir & UART_IIR_NO_INT) {
743 tegra_uart_handle_rx_dma(tup);
744 if (tup->rx_in_progress) {
745 ier = tup->ier_shadow;
746 ier |= (UART_IER_RLSI | UART_IER_RTOIE |
747 TEGRA_UART_IER_EORD);
748 tup->ier_shadow = ier;
749 tegra_uart_write(tup, ier, UART_IER);
752 spin_unlock_irqrestore(&u->lock, flags);
756 switch ((iir >> 1) & 0x7) {
757 case 0: /* Modem signal change interrupt */
758 tegra_uart_handle_modem_signal_change(u);
761 case 1: /* Transmit interrupt only triggered when using PIO */
762 tup->ier_shadow &= ~UART_IER_THRI;
763 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
764 tegra_uart_handle_tx_pio(tup);
767 case 4: /* End of data */
768 case 6: /* Rx timeout */
769 case 2: /* Receive */
772 /* Disable Rx interrupts */
773 ier = tup->ier_shadow;
775 tegra_uart_write(tup, ier, UART_IER);
776 ier &= ~(UART_IER_RDI | UART_IER_RLSI |
777 UART_IER_RTOIE | TEGRA_UART_IER_EORD);
778 tup->ier_shadow = ier;
779 tegra_uart_write(tup, ier, UART_IER);
783 case 3: /* Receive error */
784 tegra_uart_decode_rx_error(tup,
785 tegra_uart_read(tup, UART_LSR));
788 case 5: /* break nothing to handle */
789 case 7: /* break nothing to handle */
795 static void tegra_uart_stop_rx(struct uart_port *u)
797 struct tegra_uart_port *tup = to_tegra_uport(u);
798 struct dma_tx_state state;
804 if (!tup->rx_in_progress)
807 tegra_uart_wait_sym_time(tup, 1); /* wait one character interval */
809 ier = tup->ier_shadow;
810 ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE |
811 TEGRA_UART_IER_EORD);
812 tup->ier_shadow = ier;
813 tegra_uart_write(tup, ier, UART_IER);
814 tup->rx_in_progress = 0;
815 dmaengine_terminate_all(tup->rx_dma_chan);
816 dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
817 tegra_uart_rx_buffer_push(tup, state.residue);
820 static void tegra_uart_hw_deinit(struct tegra_uart_port *tup)
823 unsigned long char_time = DIV_ROUND_UP(10000000, tup->current_baud);
824 unsigned long fifo_empty_time = tup->uport.fifosize * char_time;
825 unsigned long wait_time;
830 /* Disable interrupts */
831 tegra_uart_write(tup, 0, UART_IER);
833 lsr = tegra_uart_read(tup, UART_LSR);
834 if ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
835 msr = tegra_uart_read(tup, UART_MSR);
836 mcr = tegra_uart_read(tup, UART_MCR);
837 if ((mcr & TEGRA_UART_MCR_CTS_EN) && (msr & UART_MSR_CTS))
838 dev_err(tup->uport.dev,
839 "Tx Fifo not empty, CTS disabled, waiting\n");
841 /* Wait for Tx fifo to be empty */
842 while ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
843 wait_time = min(fifo_empty_time, 100lu);
845 fifo_empty_time -= wait_time;
846 if (!fifo_empty_time) {
847 msr = tegra_uart_read(tup, UART_MSR);
848 mcr = tegra_uart_read(tup, UART_MCR);
849 if ((mcr & TEGRA_UART_MCR_CTS_EN) &&
850 (msr & UART_MSR_CTS))
851 dev_err(tup->uport.dev,
852 "Slave not ready\n");
855 lsr = tegra_uart_read(tup, UART_LSR);
859 spin_lock_irqsave(&tup->uport.lock, flags);
860 /* Reset the Rx and Tx FIFOs */
861 tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_XMIT | UART_FCR_CLEAR_RCVR);
862 tup->current_baud = 0;
863 spin_unlock_irqrestore(&tup->uport.lock, flags);
865 tup->rx_in_progress = 0;
866 tup->tx_in_progress = 0;
868 tegra_uart_dma_channel_free(tup, true);
869 tegra_uart_dma_channel_free(tup, false);
871 clk_disable_unprepare(tup->uart_clk);
874 static int tegra_uart_hw_init(struct tegra_uart_port *tup)
882 tup->current_baud = 0;
884 clk_prepare_enable(tup->uart_clk);
886 /* Reset the UART controller to clear all previous status.*/
887 reset_control_assert(tup->rst);
889 reset_control_deassert(tup->rst);
891 tup->rx_in_progress = 0;
892 tup->tx_in_progress = 0;
895 * Set the trigger level
899 * For receive, this will interrupt the CPU after that many number of
900 * bytes are received, for the remaining bytes the receive timeout
901 * interrupt is received. Rx high watermark is set to 4.
903 * For transmit, if the trasnmit interrupt is enabled, this will
904 * interrupt the CPU when the number of entries in the FIFO reaches the
905 * low watermark. Tx low watermark is set to 16 bytes.
909 * Set the Tx trigger to 16. This should match the DMA burst size that
910 * programmed in the DMA registers.
912 tup->fcr_shadow = UART_FCR_ENABLE_FIFO;
913 tup->fcr_shadow |= UART_FCR_R_TRIG_01;
914 tup->fcr_shadow |= TEGRA_UART_TX_TRIG_16B;
915 tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
917 /* Dummy read to ensure the write is posted */
918 tegra_uart_read(tup, UART_SCR);
921 * For all tegra devices (up to t210), there is a hardware issue that
922 * requires software to wait for 3 UART clock periods after enabling
923 * the TX fifo, otherwise data could be lost.
925 tegra_uart_wait_cycle_time(tup, 3);
928 * Initialize the UART with default configuration
929 * (115200, N, 8, 1) so that the receive DMA buffer may be
932 tup->lcr_shadow = TEGRA_UART_DEFAULT_LSR;
933 tegra_set_baudrate(tup, TEGRA_UART_DEFAULT_BAUD);
934 tup->fcr_shadow |= UART_FCR_DMA_SELECT;
935 tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
937 ret = tegra_uart_start_rx_dma(tup);
939 dev_err(tup->uport.dev, "Not able to start Rx DMA\n");
942 tup->rx_in_progress = 1;
945 * Enable IE_RXS for the receive status interrupts like line errros.
946 * Enable IE_RX_TIMEOUT to get the bytes which cannot be DMA'd.
948 * If using DMA mode, enable EORD instead of receive interrupt which
949 * will interrupt after the UART is done with the receive instead of
950 * the interrupt when the FIFO "threshold" is reached.
952 * EORD is different interrupt than RX_TIMEOUT - RX_TIMEOUT occurs when
953 * the DATA is sitting in the FIFO and couldn't be transferred to the
954 * DMA as the DMA size alignment (4 bytes) is not met. EORD will be
955 * triggered when there is a pause of the incomming data stream for 4
958 * For pauses in the data which is not aligned to 4 bytes, we get
959 * both the EORD as well as RX_TIMEOUT - SW sees RX_TIMEOUT first
962 tup->ier_shadow = UART_IER_RLSI | UART_IER_RTOIE | TEGRA_UART_IER_EORD;
963 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
967 static void tegra_uart_dma_channel_free(struct tegra_uart_port *tup,
971 dmaengine_terminate_all(tup->rx_dma_chan);
972 dma_release_channel(tup->rx_dma_chan);
973 dma_free_coherent(tup->uport.dev, TEGRA_UART_RX_DMA_BUFFER_SIZE,
974 tup->rx_dma_buf_virt, tup->rx_dma_buf_phys);
975 tup->rx_dma_chan = NULL;
976 tup->rx_dma_buf_phys = 0;
977 tup->rx_dma_buf_virt = NULL;
979 dmaengine_terminate_all(tup->tx_dma_chan);
980 dma_release_channel(tup->tx_dma_chan);
981 dma_unmap_single(tup->uport.dev, tup->tx_dma_buf_phys,
982 UART_XMIT_SIZE, DMA_TO_DEVICE);
983 tup->tx_dma_chan = NULL;
984 tup->tx_dma_buf_phys = 0;
985 tup->tx_dma_buf_virt = NULL;
989 static int tegra_uart_dma_channel_allocate(struct tegra_uart_port *tup,
992 struct dma_chan *dma_chan;
993 unsigned char *dma_buf;
996 struct dma_slave_config dma_sconfig;
998 dma_chan = dma_request_slave_channel_reason(tup->uport.dev,
999 dma_to_memory ? "rx" : "tx");
1000 if (IS_ERR(dma_chan)) {
1001 ret = PTR_ERR(dma_chan);
1002 dev_err(tup->uport.dev,
1003 "DMA channel alloc failed: %d\n", ret);
1007 if (dma_to_memory) {
1008 dma_buf = dma_alloc_coherent(tup->uport.dev,
1009 TEGRA_UART_RX_DMA_BUFFER_SIZE,
1010 &dma_phys, GFP_KERNEL);
1012 dev_err(tup->uport.dev,
1013 "Not able to allocate the dma buffer\n");
1014 dma_release_channel(dma_chan);
1017 dma_sconfig.src_addr = tup->uport.mapbase;
1018 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1019 dma_sconfig.src_maxburst = 4;
1020 tup->rx_dma_chan = dma_chan;
1021 tup->rx_dma_buf_virt = dma_buf;
1022 tup->rx_dma_buf_phys = dma_phys;
1024 dma_phys = dma_map_single(tup->uport.dev,
1025 tup->uport.state->xmit.buf, UART_XMIT_SIZE,
1027 if (dma_mapping_error(tup->uport.dev, dma_phys)) {
1028 dev_err(tup->uport.dev, "dma_map_single tx failed\n");
1029 dma_release_channel(dma_chan);
1032 dma_buf = tup->uport.state->xmit.buf;
1033 dma_sconfig.dst_addr = tup->uport.mapbase;
1034 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1035 dma_sconfig.dst_maxburst = 16;
1036 tup->tx_dma_chan = dma_chan;
1037 tup->tx_dma_buf_virt = dma_buf;
1038 tup->tx_dma_buf_phys = dma_phys;
1041 ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
1043 dev_err(tup->uport.dev,
1044 "Dma slave config failed, err = %d\n", ret);
1045 tegra_uart_dma_channel_free(tup, dma_to_memory);
1052 static int tegra_uart_startup(struct uart_port *u)
1054 struct tegra_uart_port *tup = to_tegra_uport(u);
1057 ret = tegra_uart_dma_channel_allocate(tup, false);
1059 dev_err(u->dev, "Tx Dma allocation failed, err = %d\n", ret);
1063 ret = tegra_uart_dma_channel_allocate(tup, true);
1065 dev_err(u->dev, "Rx Dma allocation failed, err = %d\n", ret);
1069 ret = tegra_uart_hw_init(tup);
1071 dev_err(u->dev, "Uart HW init failed, err = %d\n", ret);
1075 ret = request_irq(u->irq, tegra_uart_isr, 0,
1076 dev_name(u->dev), tup);
1078 dev_err(u->dev, "Failed to register ISR for IRQ %d\n", u->irq);
1084 tegra_uart_dma_channel_free(tup, true);
1086 tegra_uart_dma_channel_free(tup, false);
1091 * Flush any TX data submitted for DMA and PIO. Called when the
1092 * TX circular buffer is reset.
1094 static void tegra_uart_flush_buffer(struct uart_port *u)
1096 struct tegra_uart_port *tup = to_tegra_uport(u);
1099 if (tup->tx_dma_chan)
1100 dmaengine_terminate_all(tup->tx_dma_chan);
1103 static void tegra_uart_shutdown(struct uart_port *u)
1105 struct tegra_uart_port *tup = to_tegra_uport(u);
1107 tegra_uart_hw_deinit(tup);
1108 free_irq(u->irq, tup);
1111 static void tegra_uart_enable_ms(struct uart_port *u)
1113 struct tegra_uart_port *tup = to_tegra_uport(u);
1115 if (tup->enable_modem_interrupt) {
1116 tup->ier_shadow |= UART_IER_MSI;
1117 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1121 static void tegra_uart_set_termios(struct uart_port *u,
1122 struct ktermios *termios, struct ktermios *oldtermios)
1124 struct tegra_uart_port *tup = to_tegra_uport(u);
1126 unsigned long flags;
1129 struct clk *parent_clk = clk_get_parent(tup->uart_clk);
1130 unsigned long parent_clk_rate = clk_get_rate(parent_clk);
1131 int max_divider = (tup->cdata->support_clk_src_div) ? 0x7FFF : 0xFFFF;
1134 spin_lock_irqsave(&u->lock, flags);
1136 /* Changing configuration, it is safe to stop any rx now */
1137 if (tup->rts_active)
1138 set_rts(tup, false);
1140 /* Clear all interrupts as configuration is going to be changed */
1141 tegra_uart_write(tup, tup->ier_shadow | UART_IER_RDI, UART_IER);
1142 tegra_uart_read(tup, UART_IER);
1143 tegra_uart_write(tup, 0, UART_IER);
1144 tegra_uart_read(tup, UART_IER);
1147 lcr = tup->lcr_shadow;
1148 lcr &= ~UART_LCR_PARITY;
1150 /* CMSPAR isn't supported by this driver */
1151 termios->c_cflag &= ~CMSPAR;
1153 if ((termios->c_cflag & PARENB) == PARENB) {
1155 if (termios->c_cflag & PARODD) {
1156 lcr |= UART_LCR_PARITY;
1157 lcr &= ~UART_LCR_EPAR;
1158 lcr &= ~UART_LCR_SPAR;
1160 lcr |= UART_LCR_PARITY;
1161 lcr |= UART_LCR_EPAR;
1162 lcr &= ~UART_LCR_SPAR;
1166 lcr &= ~UART_LCR_WLEN8;
1167 switch (termios->c_cflag & CSIZE) {
1169 lcr |= UART_LCR_WLEN5;
1173 lcr |= UART_LCR_WLEN6;
1177 lcr |= UART_LCR_WLEN7;
1181 lcr |= UART_LCR_WLEN8;
1187 if (termios->c_cflag & CSTOPB) {
1188 lcr |= UART_LCR_STOP;
1191 lcr &= ~UART_LCR_STOP;
1195 tegra_uart_write(tup, lcr, UART_LCR);
1196 tup->lcr_shadow = lcr;
1197 tup->symb_bit = symb_bit;
1200 baud = uart_get_baud_rate(u, termios, oldtermios,
1201 parent_clk_rate/max_divider,
1202 parent_clk_rate/16);
1203 spin_unlock_irqrestore(&u->lock, flags);
1204 tegra_set_baudrate(tup, baud);
1205 if (tty_termios_baud_rate(termios))
1206 tty_termios_encode_baud_rate(termios, baud, baud);
1207 spin_lock_irqsave(&u->lock, flags);
1210 if (termios->c_cflag & CRTSCTS) {
1211 tup->mcr_shadow |= TEGRA_UART_MCR_CTS_EN;
1212 tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
1213 tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
1214 /* if top layer has asked to set rts active then do so here */
1215 if (tup->rts_active)
1218 tup->mcr_shadow &= ~TEGRA_UART_MCR_CTS_EN;
1219 tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
1220 tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
1223 /* update the port timeout based on new settings */
1224 uart_update_timeout(u, termios->c_cflag, baud);
1226 /* Make sure all writes have completed */
1227 tegra_uart_read(tup, UART_IER);
1229 /* Re-enable interrupt */
1230 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1231 tegra_uart_read(tup, UART_IER);
1233 tup->uport.ignore_status_mask = 0;
1234 /* Ignore all characters if CREAD is not set */
1235 if ((termios->c_cflag & CREAD) == 0)
1236 tup->uport.ignore_status_mask |= UART_LSR_DR;
1237 if (termios->c_iflag & IGNBRK)
1238 tup->uport.ignore_status_mask |= UART_LSR_BI;
1240 spin_unlock_irqrestore(&u->lock, flags);
1243 static const char *tegra_uart_type(struct uart_port *u)
1245 return TEGRA_UART_TYPE;
1248 static const struct uart_ops tegra_uart_ops = {
1249 .tx_empty = tegra_uart_tx_empty,
1250 .set_mctrl = tegra_uart_set_mctrl,
1251 .get_mctrl = tegra_uart_get_mctrl,
1252 .stop_tx = tegra_uart_stop_tx,
1253 .start_tx = tegra_uart_start_tx,
1254 .stop_rx = tegra_uart_stop_rx,
1255 .flush_buffer = tegra_uart_flush_buffer,
1256 .enable_ms = tegra_uart_enable_ms,
1257 .break_ctl = tegra_uart_break_ctl,
1258 .startup = tegra_uart_startup,
1259 .shutdown = tegra_uart_shutdown,
1260 .set_termios = tegra_uart_set_termios,
1261 .type = tegra_uart_type,
1262 .request_port = tegra_uart_request_port,
1263 .release_port = tegra_uart_release_port,
1266 static struct uart_driver tegra_uart_driver = {
1267 .owner = THIS_MODULE,
1268 .driver_name = "tegra_hsuart",
1269 .dev_name = "ttyTHS",
1271 .nr = TEGRA_UART_MAXIMUM,
1274 static int tegra_uart_parse_dt(struct platform_device *pdev,
1275 struct tegra_uart_port *tup)
1277 struct device_node *np = pdev->dev.of_node;
1280 port = of_alias_get_id(np, "serial");
1282 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", port);
1285 tup->uport.line = port;
1287 tup->enable_modem_interrupt = of_property_read_bool(np,
1288 "nvidia,enable-modem-interrupt");
1292 static struct tegra_uart_chip_data tegra20_uart_chip_data = {
1293 .tx_fifo_full_status = false,
1294 .allow_txfifo_reset_fifo_mode = true,
1295 .support_clk_src_div = false,
1298 static struct tegra_uart_chip_data tegra30_uart_chip_data = {
1299 .tx_fifo_full_status = true,
1300 .allow_txfifo_reset_fifo_mode = false,
1301 .support_clk_src_div = true,
1304 static const struct of_device_id tegra_uart_of_match[] = {
1306 .compatible = "nvidia,tegra30-hsuart",
1307 .data = &tegra30_uart_chip_data,
1309 .compatible = "nvidia,tegra20-hsuart",
1310 .data = &tegra20_uart_chip_data,
1314 MODULE_DEVICE_TABLE(of, tegra_uart_of_match);
1316 static int tegra_uart_probe(struct platform_device *pdev)
1318 struct tegra_uart_port *tup;
1319 struct uart_port *u;
1320 struct resource *resource;
1322 const struct tegra_uart_chip_data *cdata;
1323 const struct of_device_id *match;
1325 match = of_match_device(tegra_uart_of_match, &pdev->dev);
1327 dev_err(&pdev->dev, "Error: No device match found\n");
1330 cdata = match->data;
1332 tup = devm_kzalloc(&pdev->dev, sizeof(*tup), GFP_KERNEL);
1334 dev_err(&pdev->dev, "Failed to allocate memory for tup\n");
1338 ret = tegra_uart_parse_dt(pdev, tup);
1343 u->dev = &pdev->dev;
1344 u->ops = &tegra_uart_ops;
1345 u->type = PORT_TEGRA;
1349 platform_set_drvdata(pdev, tup);
1350 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1352 dev_err(&pdev->dev, "No IO memory resource\n");
1356 u->mapbase = resource->start;
1357 u->membase = devm_ioremap_resource(&pdev->dev, resource);
1358 if (IS_ERR(u->membase))
1359 return PTR_ERR(u->membase);
1361 tup->uart_clk = devm_clk_get(&pdev->dev, NULL);
1362 if (IS_ERR(tup->uart_clk)) {
1363 dev_err(&pdev->dev, "Couldn't get the clock\n");
1364 return PTR_ERR(tup->uart_clk);
1367 tup->rst = devm_reset_control_get_exclusive(&pdev->dev, "serial");
1368 if (IS_ERR(tup->rst)) {
1369 dev_err(&pdev->dev, "Couldn't get the reset\n");
1370 return PTR_ERR(tup->rst);
1373 u->iotype = UPIO_MEM32;
1374 ret = platform_get_irq(pdev, 0);
1379 ret = uart_add_one_port(&tegra_uart_driver, u);
1381 dev_err(&pdev->dev, "Failed to add uart port, err %d\n", ret);
1387 static int tegra_uart_remove(struct platform_device *pdev)
1389 struct tegra_uart_port *tup = platform_get_drvdata(pdev);
1390 struct uart_port *u = &tup->uport;
1392 uart_remove_one_port(&tegra_uart_driver, u);
1396 #ifdef CONFIG_PM_SLEEP
1397 static int tegra_uart_suspend(struct device *dev)
1399 struct tegra_uart_port *tup = dev_get_drvdata(dev);
1400 struct uart_port *u = &tup->uport;
1402 return uart_suspend_port(&tegra_uart_driver, u);
1405 static int tegra_uart_resume(struct device *dev)
1407 struct tegra_uart_port *tup = dev_get_drvdata(dev);
1408 struct uart_port *u = &tup->uport;
1410 return uart_resume_port(&tegra_uart_driver, u);
1414 static const struct dev_pm_ops tegra_uart_pm_ops = {
1415 SET_SYSTEM_SLEEP_PM_OPS(tegra_uart_suspend, tegra_uart_resume)
1418 static struct platform_driver tegra_uart_platform_driver = {
1419 .probe = tegra_uart_probe,
1420 .remove = tegra_uart_remove,
1422 .name = "serial-tegra",
1423 .of_match_table = tegra_uart_of_match,
1424 .pm = &tegra_uart_pm_ops,
1428 static int __init tegra_uart_init(void)
1432 ret = uart_register_driver(&tegra_uart_driver);
1434 pr_err("Could not register %s driver\n",
1435 tegra_uart_driver.driver_name);
1439 ret = platform_driver_register(&tegra_uart_platform_driver);
1441 pr_err("Uart platform driver register failed, e = %d\n", ret);
1442 uart_unregister_driver(&tegra_uart_driver);
1448 static void __exit tegra_uart_exit(void)
1450 pr_info("Unloading tegra uart driver\n");
1451 platform_driver_unregister(&tegra_uart_platform_driver);
1452 uart_unregister_driver(&tegra_uart_driver);
1455 module_init(tegra_uart_init);
1456 module_exit(tegra_uart_exit);
1458 MODULE_ALIAS("platform:serial-tegra");
1459 MODULE_DESCRIPTION("High speed UART driver for tegra chipset");
1460 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1461 MODULE_LICENSE("GPL v2");