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) {
376 dev_err(tup->uport.dev, "Got Break\n");
377 tup->uport.icount.brk++;
378 /* If FIFO read error without any data, reset Rx FIFO */
379 if (!(lsr & UART_LSR_DR) && (lsr & UART_LSR_FIFOE))
380 tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_RCVR);
386 static int tegra_uart_request_port(struct uart_port *u)
391 static void tegra_uart_release_port(struct uart_port *u)
393 /* Nothing to do here */
396 static void tegra_uart_fill_tx_fifo(struct tegra_uart_port *tup, int max_bytes)
398 struct circ_buf *xmit = &tup->uport.state->xmit;
401 for (i = 0; i < max_bytes; i++) {
402 BUG_ON(uart_circ_empty(xmit));
403 if (tup->cdata->tx_fifo_full_status) {
404 unsigned long lsr = tegra_uart_read(tup, UART_LSR);
405 if ((lsr & TEGRA_UART_LSR_TXFIFO_FULL))
408 tegra_uart_write(tup, xmit->buf[xmit->tail], UART_TX);
409 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
410 tup->uport.icount.tx++;
414 static void tegra_uart_start_pio_tx(struct tegra_uart_port *tup,
417 if (bytes > TEGRA_UART_MIN_DMA)
418 bytes = TEGRA_UART_MIN_DMA;
420 tup->tx_in_progress = TEGRA_UART_TX_PIO;
421 tup->tx_bytes = bytes;
422 tup->ier_shadow |= UART_IER_THRI;
423 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
426 static void tegra_uart_tx_dma_complete(void *args)
428 struct tegra_uart_port *tup = args;
429 struct circ_buf *xmit = &tup->uport.state->xmit;
430 struct dma_tx_state state;
434 dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
435 count = tup->tx_bytes_requested - state.residue;
436 async_tx_ack(tup->tx_dma_desc);
437 spin_lock_irqsave(&tup->uport.lock, flags);
438 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
439 tup->tx_in_progress = 0;
440 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
441 uart_write_wakeup(&tup->uport);
442 tegra_uart_start_next_tx(tup);
443 spin_unlock_irqrestore(&tup->uport.lock, flags);
446 static int tegra_uart_start_tx_dma(struct tegra_uart_port *tup,
449 struct circ_buf *xmit = &tup->uport.state->xmit;
450 dma_addr_t tx_phys_addr;
452 dma_sync_single_for_device(tup->uport.dev, tup->tx_dma_buf_phys,
453 UART_XMIT_SIZE, DMA_TO_DEVICE);
455 tup->tx_bytes = count & ~(0xF);
456 tx_phys_addr = tup->tx_dma_buf_phys + xmit->tail;
457 tup->tx_dma_desc = dmaengine_prep_slave_single(tup->tx_dma_chan,
458 tx_phys_addr, tup->tx_bytes, DMA_MEM_TO_DEV,
460 if (!tup->tx_dma_desc) {
461 dev_err(tup->uport.dev, "Not able to get desc for Tx\n");
465 tup->tx_dma_desc->callback = tegra_uart_tx_dma_complete;
466 tup->tx_dma_desc->callback_param = tup;
467 tup->tx_in_progress = TEGRA_UART_TX_DMA;
468 tup->tx_bytes_requested = tup->tx_bytes;
469 tup->tx_cookie = dmaengine_submit(tup->tx_dma_desc);
470 dma_async_issue_pending(tup->tx_dma_chan);
474 static void tegra_uart_start_next_tx(struct tegra_uart_port *tup)
478 struct circ_buf *xmit = &tup->uport.state->xmit;
480 if (!tup->current_baud)
483 tail = (unsigned long)&xmit->buf[xmit->tail];
484 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
488 if (count < TEGRA_UART_MIN_DMA)
489 tegra_uart_start_pio_tx(tup, count);
490 else if (BYTES_TO_ALIGN(tail) > 0)
491 tegra_uart_start_pio_tx(tup, BYTES_TO_ALIGN(tail));
493 tegra_uart_start_tx_dma(tup, count);
496 /* Called by serial core driver with u->lock taken. */
497 static void tegra_uart_start_tx(struct uart_port *u)
499 struct tegra_uart_port *tup = to_tegra_uport(u);
500 struct circ_buf *xmit = &u->state->xmit;
502 if (!uart_circ_empty(xmit) && !tup->tx_in_progress)
503 tegra_uart_start_next_tx(tup);
506 static unsigned int tegra_uart_tx_empty(struct uart_port *u)
508 struct tegra_uart_port *tup = to_tegra_uport(u);
509 unsigned int ret = 0;
512 spin_lock_irqsave(&u->lock, flags);
513 if (!tup->tx_in_progress) {
514 unsigned long lsr = tegra_uart_read(tup, UART_LSR);
515 if ((lsr & TX_EMPTY_STATUS) == TX_EMPTY_STATUS)
518 spin_unlock_irqrestore(&u->lock, flags);
522 static void tegra_uart_stop_tx(struct uart_port *u)
524 struct tegra_uart_port *tup = to_tegra_uport(u);
525 struct circ_buf *xmit = &tup->uport.state->xmit;
526 struct dma_tx_state state;
529 if (tup->tx_in_progress != TEGRA_UART_TX_DMA)
532 dmaengine_terminate_all(tup->tx_dma_chan);
533 dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
534 count = tup->tx_bytes_requested - state.residue;
535 async_tx_ack(tup->tx_dma_desc);
536 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
537 tup->tx_in_progress = 0;
540 static void tegra_uart_handle_tx_pio(struct tegra_uart_port *tup)
542 struct circ_buf *xmit = &tup->uport.state->xmit;
544 tegra_uart_fill_tx_fifo(tup, tup->tx_bytes);
545 tup->tx_in_progress = 0;
546 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
547 uart_write_wakeup(&tup->uport);
548 tegra_uart_start_next_tx(tup);
551 static void tegra_uart_handle_rx_pio(struct tegra_uart_port *tup,
552 struct tty_port *tty)
555 char flag = TTY_NORMAL;
556 unsigned long lsr = 0;
559 lsr = tegra_uart_read(tup, UART_LSR);
560 if (!(lsr & UART_LSR_DR))
563 flag = tegra_uart_decode_rx_error(tup, lsr);
564 ch = (unsigned char) tegra_uart_read(tup, UART_RX);
565 tup->uport.icount.rx++;
567 if (!uart_handle_sysrq_char(&tup->uport, ch) && tty)
568 tty_insert_flip_char(tty, ch, flag);
570 if (tup->uport.ignore_status_mask & UART_LSR_DR)
575 static void tegra_uart_copy_rx_to_tty(struct tegra_uart_port *tup,
576 struct tty_port *tty,
581 /* If count is zero, then there is no data to be copied */
585 tup->uport.icount.rx += count;
587 dev_err(tup->uport.dev, "No tty port\n");
591 if (tup->uport.ignore_status_mask & UART_LSR_DR)
594 dma_sync_single_for_cpu(tup->uport.dev, tup->rx_dma_buf_phys,
595 TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
596 copied = tty_insert_flip_string(tty,
597 ((unsigned char *)(tup->rx_dma_buf_virt)), count);
598 if (copied != count) {
600 dev_err(tup->uport.dev, "RxData copy to tty layer failed\n");
602 dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
603 TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_TO_DEVICE);
606 static void tegra_uart_rx_buffer_push(struct tegra_uart_port *tup,
607 unsigned int residue)
609 struct tty_port *port = &tup->uport.state->port;
610 struct tty_struct *tty = tty_port_tty_get(port);
613 async_tx_ack(tup->rx_dma_desc);
614 count = tup->rx_bytes_requested - residue;
616 /* If we are here, DMA is stopped */
617 tegra_uart_copy_rx_to_tty(tup, port, count);
619 tegra_uart_handle_rx_pio(tup, port);
621 tty_flip_buffer_push(port);
626 static void tegra_uart_rx_dma_complete(void *args)
628 struct tegra_uart_port *tup = args;
629 struct uart_port *u = &tup->uport;
631 struct dma_tx_state state;
632 enum dma_status status;
634 spin_lock_irqsave(&u->lock, flags);
636 status = dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
638 if (status == DMA_IN_PROGRESS) {
639 dev_dbg(tup->uport.dev, "RX DMA is in progress\n");
643 /* Deactivate flow control to stop sender */
647 tegra_uart_rx_buffer_push(tup, 0);
648 tegra_uart_start_rx_dma(tup);
650 /* Activate flow control to start transfer */
655 spin_unlock_irqrestore(&u->lock, flags);
658 static void tegra_uart_handle_rx_dma(struct tegra_uart_port *tup)
660 struct dma_tx_state state;
662 /* Deactivate flow control to stop sender */
666 dmaengine_terminate_all(tup->rx_dma_chan);
667 dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
668 tegra_uart_rx_buffer_push(tup, state.residue);
669 tegra_uart_start_rx_dma(tup);
675 static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup)
677 unsigned int count = TEGRA_UART_RX_DMA_BUFFER_SIZE;
679 tup->rx_dma_desc = dmaengine_prep_slave_single(tup->rx_dma_chan,
680 tup->rx_dma_buf_phys, count, DMA_DEV_TO_MEM,
682 if (!tup->rx_dma_desc) {
683 dev_err(tup->uport.dev, "Not able to get desc for Rx\n");
687 tup->rx_dma_desc->callback = tegra_uart_rx_dma_complete;
688 tup->rx_dma_desc->callback_param = tup;
689 dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
690 count, DMA_TO_DEVICE);
691 tup->rx_bytes_requested = count;
692 tup->rx_cookie = dmaengine_submit(tup->rx_dma_desc);
693 dma_async_issue_pending(tup->rx_dma_chan);
697 static void tegra_uart_handle_modem_signal_change(struct uart_port *u)
699 struct tegra_uart_port *tup = to_tegra_uport(u);
702 msr = tegra_uart_read(tup, UART_MSR);
703 if (!(msr & UART_MSR_ANY_DELTA))
706 if (msr & UART_MSR_TERI)
707 tup->uport.icount.rng++;
708 if (msr & UART_MSR_DDSR)
709 tup->uport.icount.dsr++;
710 /* We may only get DDCD when HW init and reset */
711 if (msr & UART_MSR_DDCD)
712 uart_handle_dcd_change(&tup->uport, msr & UART_MSR_DCD);
713 /* Will start/stop_tx accordingly */
714 if (msr & UART_MSR_DCTS)
715 uart_handle_cts_change(&tup->uport, msr & UART_MSR_CTS);
718 static irqreturn_t tegra_uart_isr(int irq, void *data)
720 struct tegra_uart_port *tup = data;
721 struct uart_port *u = &tup->uport;
724 bool is_rx_int = false;
727 spin_lock_irqsave(&u->lock, flags);
729 iir = tegra_uart_read(tup, UART_IIR);
730 if (iir & UART_IIR_NO_INT) {
732 tegra_uart_handle_rx_dma(tup);
733 if (tup->rx_in_progress) {
734 ier = tup->ier_shadow;
735 ier |= (UART_IER_RLSI | UART_IER_RTOIE |
736 TEGRA_UART_IER_EORD);
737 tup->ier_shadow = ier;
738 tegra_uart_write(tup, ier, UART_IER);
741 spin_unlock_irqrestore(&u->lock, flags);
745 switch ((iir >> 1) & 0x7) {
746 case 0: /* Modem signal change interrupt */
747 tegra_uart_handle_modem_signal_change(u);
750 case 1: /* Transmit interrupt only triggered when using PIO */
751 tup->ier_shadow &= ~UART_IER_THRI;
752 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
753 tegra_uart_handle_tx_pio(tup);
756 case 4: /* End of data */
757 case 6: /* Rx timeout */
758 case 2: /* Receive */
761 /* Disable Rx interrupts */
762 ier = tup->ier_shadow;
764 tegra_uart_write(tup, ier, UART_IER);
765 ier &= ~(UART_IER_RDI | UART_IER_RLSI |
766 UART_IER_RTOIE | TEGRA_UART_IER_EORD);
767 tup->ier_shadow = ier;
768 tegra_uart_write(tup, ier, UART_IER);
772 case 3: /* Receive error */
773 tegra_uart_decode_rx_error(tup,
774 tegra_uart_read(tup, UART_LSR));
777 case 5: /* break nothing to handle */
778 case 7: /* break nothing to handle */
784 static void tegra_uart_stop_rx(struct uart_port *u)
786 struct tegra_uart_port *tup = to_tegra_uport(u);
787 struct dma_tx_state state;
793 if (!tup->rx_in_progress)
796 tegra_uart_wait_sym_time(tup, 1); /* wait one character interval */
798 ier = tup->ier_shadow;
799 ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE |
800 TEGRA_UART_IER_EORD);
801 tup->ier_shadow = ier;
802 tegra_uart_write(tup, ier, UART_IER);
803 tup->rx_in_progress = 0;
804 dmaengine_terminate_all(tup->rx_dma_chan);
805 dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
806 tegra_uart_rx_buffer_push(tup, state.residue);
809 static void tegra_uart_hw_deinit(struct tegra_uart_port *tup)
812 unsigned long char_time = DIV_ROUND_UP(10000000, tup->current_baud);
813 unsigned long fifo_empty_time = tup->uport.fifosize * char_time;
814 unsigned long wait_time;
819 /* Disable interrupts */
820 tegra_uart_write(tup, 0, UART_IER);
822 lsr = tegra_uart_read(tup, UART_LSR);
823 if ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
824 msr = tegra_uart_read(tup, UART_MSR);
825 mcr = tegra_uart_read(tup, UART_MCR);
826 if ((mcr & TEGRA_UART_MCR_CTS_EN) && (msr & UART_MSR_CTS))
827 dev_err(tup->uport.dev,
828 "Tx Fifo not empty, CTS disabled, waiting\n");
830 /* Wait for Tx fifo to be empty */
831 while ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
832 wait_time = min(fifo_empty_time, 100lu);
834 fifo_empty_time -= wait_time;
835 if (!fifo_empty_time) {
836 msr = tegra_uart_read(tup, UART_MSR);
837 mcr = tegra_uart_read(tup, UART_MCR);
838 if ((mcr & TEGRA_UART_MCR_CTS_EN) &&
839 (msr & UART_MSR_CTS))
840 dev_err(tup->uport.dev,
841 "Slave not ready\n");
844 lsr = tegra_uart_read(tup, UART_LSR);
848 spin_lock_irqsave(&tup->uport.lock, flags);
849 /* Reset the Rx and Tx FIFOs */
850 tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_XMIT | UART_FCR_CLEAR_RCVR);
851 tup->current_baud = 0;
852 spin_unlock_irqrestore(&tup->uport.lock, flags);
854 tup->rx_in_progress = 0;
855 tup->tx_in_progress = 0;
857 tegra_uart_dma_channel_free(tup, true);
858 tegra_uart_dma_channel_free(tup, false);
860 clk_disable_unprepare(tup->uart_clk);
863 static int tegra_uart_hw_init(struct tegra_uart_port *tup)
871 tup->current_baud = 0;
873 clk_prepare_enable(tup->uart_clk);
875 /* Reset the UART controller to clear all previous status.*/
876 reset_control_assert(tup->rst);
878 reset_control_deassert(tup->rst);
880 tup->rx_in_progress = 0;
881 tup->tx_in_progress = 0;
884 * Set the trigger level
888 * For receive, this will interrupt the CPU after that many number of
889 * bytes are received, for the remaining bytes the receive timeout
890 * interrupt is received. Rx high watermark is set to 4.
892 * For transmit, if the trasnmit interrupt is enabled, this will
893 * interrupt the CPU when the number of entries in the FIFO reaches the
894 * low watermark. Tx low watermark is set to 16 bytes.
898 * Set the Tx trigger to 16. This should match the DMA burst size that
899 * programmed in the DMA registers.
901 tup->fcr_shadow = UART_FCR_ENABLE_FIFO;
902 tup->fcr_shadow |= UART_FCR_R_TRIG_01;
903 tup->fcr_shadow |= TEGRA_UART_TX_TRIG_16B;
904 tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
906 /* Dummy read to ensure the write is posted */
907 tegra_uart_read(tup, UART_SCR);
910 * For all tegra devices (up to t210), there is a hardware issue that
911 * requires software to wait for 3 UART clock periods after enabling
912 * the TX fifo, otherwise data could be lost.
914 tegra_uart_wait_cycle_time(tup, 3);
917 * Initialize the UART with default configuration
918 * (115200, N, 8, 1) so that the receive DMA buffer may be
921 tup->lcr_shadow = TEGRA_UART_DEFAULT_LSR;
922 tegra_set_baudrate(tup, TEGRA_UART_DEFAULT_BAUD);
923 tup->fcr_shadow |= UART_FCR_DMA_SELECT;
924 tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
926 ret = tegra_uart_start_rx_dma(tup);
928 dev_err(tup->uport.dev, "Not able to start Rx DMA\n");
931 tup->rx_in_progress = 1;
934 * Enable IE_RXS for the receive status interrupts like line errros.
935 * Enable IE_RX_TIMEOUT to get the bytes which cannot be DMA'd.
937 * If using DMA mode, enable EORD instead of receive interrupt which
938 * will interrupt after the UART is done with the receive instead of
939 * the interrupt when the FIFO "threshold" is reached.
941 * EORD is different interrupt than RX_TIMEOUT - RX_TIMEOUT occurs when
942 * the DATA is sitting in the FIFO and couldn't be transferred to the
943 * DMA as the DMA size alignment (4 bytes) is not met. EORD will be
944 * triggered when there is a pause of the incomming data stream for 4
947 * For pauses in the data which is not aligned to 4 bytes, we get
948 * both the EORD as well as RX_TIMEOUT - SW sees RX_TIMEOUT first
951 tup->ier_shadow = UART_IER_RLSI | UART_IER_RTOIE | TEGRA_UART_IER_EORD;
952 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
956 static void tegra_uart_dma_channel_free(struct tegra_uart_port *tup,
960 dmaengine_terminate_all(tup->rx_dma_chan);
961 dma_release_channel(tup->rx_dma_chan);
962 dma_free_coherent(tup->uport.dev, TEGRA_UART_RX_DMA_BUFFER_SIZE,
963 tup->rx_dma_buf_virt, tup->rx_dma_buf_phys);
964 tup->rx_dma_chan = NULL;
965 tup->rx_dma_buf_phys = 0;
966 tup->rx_dma_buf_virt = NULL;
968 dmaengine_terminate_all(tup->tx_dma_chan);
969 dma_release_channel(tup->tx_dma_chan);
970 dma_unmap_single(tup->uport.dev, tup->tx_dma_buf_phys,
971 UART_XMIT_SIZE, DMA_TO_DEVICE);
972 tup->tx_dma_chan = NULL;
973 tup->tx_dma_buf_phys = 0;
974 tup->tx_dma_buf_virt = NULL;
978 static int tegra_uart_dma_channel_allocate(struct tegra_uart_port *tup,
981 struct dma_chan *dma_chan;
982 unsigned char *dma_buf;
985 struct dma_slave_config dma_sconfig;
987 dma_chan = dma_request_slave_channel_reason(tup->uport.dev,
988 dma_to_memory ? "rx" : "tx");
989 if (IS_ERR(dma_chan)) {
990 ret = PTR_ERR(dma_chan);
991 dev_err(tup->uport.dev,
992 "DMA channel alloc failed: %d\n", ret);
997 dma_buf = dma_alloc_coherent(tup->uport.dev,
998 TEGRA_UART_RX_DMA_BUFFER_SIZE,
999 &dma_phys, GFP_KERNEL);
1001 dev_err(tup->uport.dev,
1002 "Not able to allocate the dma buffer\n");
1003 dma_release_channel(dma_chan);
1006 dma_sconfig.src_addr = tup->uport.mapbase;
1007 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1008 dma_sconfig.src_maxburst = 4;
1009 tup->rx_dma_chan = dma_chan;
1010 tup->rx_dma_buf_virt = dma_buf;
1011 tup->rx_dma_buf_phys = dma_phys;
1013 dma_phys = dma_map_single(tup->uport.dev,
1014 tup->uport.state->xmit.buf, UART_XMIT_SIZE,
1016 if (dma_mapping_error(tup->uport.dev, dma_phys)) {
1017 dev_err(tup->uport.dev, "dma_map_single tx failed\n");
1018 dma_release_channel(dma_chan);
1021 dma_buf = tup->uport.state->xmit.buf;
1022 dma_sconfig.dst_addr = tup->uport.mapbase;
1023 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1024 dma_sconfig.dst_maxburst = 16;
1025 tup->tx_dma_chan = dma_chan;
1026 tup->tx_dma_buf_virt = dma_buf;
1027 tup->tx_dma_buf_phys = dma_phys;
1030 ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
1032 dev_err(tup->uport.dev,
1033 "Dma slave config failed, err = %d\n", ret);
1034 tegra_uart_dma_channel_free(tup, dma_to_memory);
1041 static int tegra_uart_startup(struct uart_port *u)
1043 struct tegra_uart_port *tup = to_tegra_uport(u);
1046 ret = tegra_uart_dma_channel_allocate(tup, false);
1048 dev_err(u->dev, "Tx Dma allocation failed, err = %d\n", ret);
1052 ret = tegra_uart_dma_channel_allocate(tup, true);
1054 dev_err(u->dev, "Rx Dma allocation failed, err = %d\n", ret);
1058 ret = tegra_uart_hw_init(tup);
1060 dev_err(u->dev, "Uart HW init failed, err = %d\n", ret);
1064 ret = request_irq(u->irq, tegra_uart_isr, 0,
1065 dev_name(u->dev), tup);
1067 dev_err(u->dev, "Failed to register ISR for IRQ %d\n", u->irq);
1073 tegra_uart_dma_channel_free(tup, true);
1075 tegra_uart_dma_channel_free(tup, false);
1080 * Flush any TX data submitted for DMA and PIO. Called when the
1081 * TX circular buffer is reset.
1083 static void tegra_uart_flush_buffer(struct uart_port *u)
1085 struct tegra_uart_port *tup = to_tegra_uport(u);
1088 if (tup->tx_dma_chan)
1089 dmaengine_terminate_all(tup->tx_dma_chan);
1092 static void tegra_uart_shutdown(struct uart_port *u)
1094 struct tegra_uart_port *tup = to_tegra_uport(u);
1096 tegra_uart_hw_deinit(tup);
1097 free_irq(u->irq, tup);
1100 static void tegra_uart_enable_ms(struct uart_port *u)
1102 struct tegra_uart_port *tup = to_tegra_uport(u);
1104 if (tup->enable_modem_interrupt) {
1105 tup->ier_shadow |= UART_IER_MSI;
1106 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1110 static void tegra_uart_set_termios(struct uart_port *u,
1111 struct ktermios *termios, struct ktermios *oldtermios)
1113 struct tegra_uart_port *tup = to_tegra_uport(u);
1115 unsigned long flags;
1118 struct clk *parent_clk = clk_get_parent(tup->uart_clk);
1119 unsigned long parent_clk_rate = clk_get_rate(parent_clk);
1120 int max_divider = (tup->cdata->support_clk_src_div) ? 0x7FFF : 0xFFFF;
1123 spin_lock_irqsave(&u->lock, flags);
1125 /* Changing configuration, it is safe to stop any rx now */
1126 if (tup->rts_active)
1127 set_rts(tup, false);
1129 /* Clear all interrupts as configuration is going to be changed */
1130 tegra_uart_write(tup, tup->ier_shadow | UART_IER_RDI, UART_IER);
1131 tegra_uart_read(tup, UART_IER);
1132 tegra_uart_write(tup, 0, UART_IER);
1133 tegra_uart_read(tup, UART_IER);
1136 lcr = tup->lcr_shadow;
1137 lcr &= ~UART_LCR_PARITY;
1139 /* CMSPAR isn't supported by this driver */
1140 termios->c_cflag &= ~CMSPAR;
1142 if ((termios->c_cflag & PARENB) == PARENB) {
1144 if (termios->c_cflag & PARODD) {
1145 lcr |= UART_LCR_PARITY;
1146 lcr &= ~UART_LCR_EPAR;
1147 lcr &= ~UART_LCR_SPAR;
1149 lcr |= UART_LCR_PARITY;
1150 lcr |= UART_LCR_EPAR;
1151 lcr &= ~UART_LCR_SPAR;
1155 lcr &= ~UART_LCR_WLEN8;
1156 switch (termios->c_cflag & CSIZE) {
1158 lcr |= UART_LCR_WLEN5;
1162 lcr |= UART_LCR_WLEN6;
1166 lcr |= UART_LCR_WLEN7;
1170 lcr |= UART_LCR_WLEN8;
1176 if (termios->c_cflag & CSTOPB) {
1177 lcr |= UART_LCR_STOP;
1180 lcr &= ~UART_LCR_STOP;
1184 tegra_uart_write(tup, lcr, UART_LCR);
1185 tup->lcr_shadow = lcr;
1186 tup->symb_bit = symb_bit;
1189 baud = uart_get_baud_rate(u, termios, oldtermios,
1190 parent_clk_rate/max_divider,
1191 parent_clk_rate/16);
1192 spin_unlock_irqrestore(&u->lock, flags);
1193 tegra_set_baudrate(tup, baud);
1194 if (tty_termios_baud_rate(termios))
1195 tty_termios_encode_baud_rate(termios, baud, baud);
1196 spin_lock_irqsave(&u->lock, flags);
1199 if (termios->c_cflag & CRTSCTS) {
1200 tup->mcr_shadow |= TEGRA_UART_MCR_CTS_EN;
1201 tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
1202 tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
1203 /* if top layer has asked to set rts active then do so here */
1204 if (tup->rts_active)
1207 tup->mcr_shadow &= ~TEGRA_UART_MCR_CTS_EN;
1208 tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
1209 tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
1212 /* update the port timeout based on new settings */
1213 uart_update_timeout(u, termios->c_cflag, baud);
1215 /* Make sure all writes have completed */
1216 tegra_uart_read(tup, UART_IER);
1218 /* Re-enable interrupt */
1219 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1220 tegra_uart_read(tup, UART_IER);
1222 tup->uport.ignore_status_mask = 0;
1223 /* Ignore all characters if CREAD is not set */
1224 if ((termios->c_cflag & CREAD) == 0)
1225 tup->uport.ignore_status_mask |= UART_LSR_DR;
1227 spin_unlock_irqrestore(&u->lock, flags);
1230 static const char *tegra_uart_type(struct uart_port *u)
1232 return TEGRA_UART_TYPE;
1235 static const struct uart_ops tegra_uart_ops = {
1236 .tx_empty = tegra_uart_tx_empty,
1237 .set_mctrl = tegra_uart_set_mctrl,
1238 .get_mctrl = tegra_uart_get_mctrl,
1239 .stop_tx = tegra_uart_stop_tx,
1240 .start_tx = tegra_uart_start_tx,
1241 .stop_rx = tegra_uart_stop_rx,
1242 .flush_buffer = tegra_uart_flush_buffer,
1243 .enable_ms = tegra_uart_enable_ms,
1244 .break_ctl = tegra_uart_break_ctl,
1245 .startup = tegra_uart_startup,
1246 .shutdown = tegra_uart_shutdown,
1247 .set_termios = tegra_uart_set_termios,
1248 .type = tegra_uart_type,
1249 .request_port = tegra_uart_request_port,
1250 .release_port = tegra_uart_release_port,
1253 static struct uart_driver tegra_uart_driver = {
1254 .owner = THIS_MODULE,
1255 .driver_name = "tegra_hsuart",
1256 .dev_name = "ttyTHS",
1258 .nr = TEGRA_UART_MAXIMUM,
1261 static int tegra_uart_parse_dt(struct platform_device *pdev,
1262 struct tegra_uart_port *tup)
1264 struct device_node *np = pdev->dev.of_node;
1267 port = of_alias_get_id(np, "serial");
1269 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", port);
1272 tup->uport.line = port;
1274 tup->enable_modem_interrupt = of_property_read_bool(np,
1275 "nvidia,enable-modem-interrupt");
1279 static struct tegra_uart_chip_data tegra20_uart_chip_data = {
1280 .tx_fifo_full_status = false,
1281 .allow_txfifo_reset_fifo_mode = true,
1282 .support_clk_src_div = false,
1285 static struct tegra_uart_chip_data tegra30_uart_chip_data = {
1286 .tx_fifo_full_status = true,
1287 .allow_txfifo_reset_fifo_mode = false,
1288 .support_clk_src_div = true,
1291 static const struct of_device_id tegra_uart_of_match[] = {
1293 .compatible = "nvidia,tegra30-hsuart",
1294 .data = &tegra30_uart_chip_data,
1296 .compatible = "nvidia,tegra20-hsuart",
1297 .data = &tegra20_uart_chip_data,
1301 MODULE_DEVICE_TABLE(of, tegra_uart_of_match);
1303 static int tegra_uart_probe(struct platform_device *pdev)
1305 struct tegra_uart_port *tup;
1306 struct uart_port *u;
1307 struct resource *resource;
1309 const struct tegra_uart_chip_data *cdata;
1310 const struct of_device_id *match;
1312 match = of_match_device(tegra_uart_of_match, &pdev->dev);
1314 dev_err(&pdev->dev, "Error: No device match found\n");
1317 cdata = match->data;
1319 tup = devm_kzalloc(&pdev->dev, sizeof(*tup), GFP_KERNEL);
1321 dev_err(&pdev->dev, "Failed to allocate memory for tup\n");
1325 ret = tegra_uart_parse_dt(pdev, tup);
1330 u->dev = &pdev->dev;
1331 u->ops = &tegra_uart_ops;
1332 u->type = PORT_TEGRA;
1336 platform_set_drvdata(pdev, tup);
1337 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1339 dev_err(&pdev->dev, "No IO memory resource\n");
1343 u->mapbase = resource->start;
1344 u->membase = devm_ioremap_resource(&pdev->dev, resource);
1345 if (IS_ERR(u->membase))
1346 return PTR_ERR(u->membase);
1348 tup->uart_clk = devm_clk_get(&pdev->dev, NULL);
1349 if (IS_ERR(tup->uart_clk)) {
1350 dev_err(&pdev->dev, "Couldn't get the clock\n");
1351 return PTR_ERR(tup->uart_clk);
1354 tup->rst = devm_reset_control_get_exclusive(&pdev->dev, "serial");
1355 if (IS_ERR(tup->rst)) {
1356 dev_err(&pdev->dev, "Couldn't get the reset\n");
1357 return PTR_ERR(tup->rst);
1360 u->iotype = UPIO_MEM32;
1361 ret = platform_get_irq(pdev, 0);
1366 ret = uart_add_one_port(&tegra_uart_driver, u);
1368 dev_err(&pdev->dev, "Failed to add uart port, err %d\n", ret);
1374 static int tegra_uart_remove(struct platform_device *pdev)
1376 struct tegra_uart_port *tup = platform_get_drvdata(pdev);
1377 struct uart_port *u = &tup->uport;
1379 uart_remove_one_port(&tegra_uart_driver, u);
1383 #ifdef CONFIG_PM_SLEEP
1384 static int tegra_uart_suspend(struct device *dev)
1386 struct tegra_uart_port *tup = dev_get_drvdata(dev);
1387 struct uart_port *u = &tup->uport;
1389 return uart_suspend_port(&tegra_uart_driver, u);
1392 static int tegra_uart_resume(struct device *dev)
1394 struct tegra_uart_port *tup = dev_get_drvdata(dev);
1395 struct uart_port *u = &tup->uport;
1397 return uart_resume_port(&tegra_uart_driver, u);
1401 static const struct dev_pm_ops tegra_uart_pm_ops = {
1402 SET_SYSTEM_SLEEP_PM_OPS(tegra_uart_suspend, tegra_uart_resume)
1405 static struct platform_driver tegra_uart_platform_driver = {
1406 .probe = tegra_uart_probe,
1407 .remove = tegra_uart_remove,
1409 .name = "serial-tegra",
1410 .of_match_table = tegra_uart_of_match,
1411 .pm = &tegra_uart_pm_ops,
1415 static int __init tegra_uart_init(void)
1419 ret = uart_register_driver(&tegra_uart_driver);
1421 pr_err("Could not register %s driver\n",
1422 tegra_uart_driver.driver_name);
1426 ret = platform_driver_register(&tegra_uart_platform_driver);
1428 pr_err("Uart platform driver register failed, e = %d\n", ret);
1429 uart_unregister_driver(&tegra_uart_driver);
1435 static void __exit tegra_uart_exit(void)
1437 pr_info("Unloading tegra uart driver\n");
1438 platform_driver_unregister(&tegra_uart_platform_driver);
1439 uart_unregister_driver(&tegra_uart_driver);
1442 module_init(tegra_uart_init);
1443 module_exit(tegra_uart_exit);
1445 MODULE_ALIAS("platform:serial-tegra");
1446 MODULE_DESCRIPTION("High speed UART driver for tegra chipset");
1447 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1448 MODULE_LICENSE("GPL v2");