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
75 #define TEGRA_UART_FCR_IIR_FIFO_EN 0x40
78 * tegra_uart_chip_data: SOC specific data.
80 * @tx_fifo_full_status: Status flag available for checking tx fifo full.
81 * @allow_txfifo_reset_fifo_mode: allow_tx fifo reset with fifo mode or not.
82 * Tegra30 does not allow this.
83 * @support_clk_src_div: Clock source support the clock divider.
85 struct tegra_uart_chip_data {
86 bool tx_fifo_full_status;
87 bool allow_txfifo_reset_fifo_mode;
88 bool support_clk_src_div;
89 bool fifo_mode_enable_status;
92 struct tegra_uart_port {
93 struct uart_port uport;
94 const struct tegra_uart_chip_data *cdata;
97 struct reset_control *rst;
98 unsigned int current_baud;
100 /* Register shadow */
101 unsigned long fcr_shadow;
102 unsigned long mcr_shadow;
103 unsigned long lcr_shadow;
104 unsigned long ier_shadow;
108 unsigned int tx_bytes;
110 bool enable_modem_interrupt;
116 struct dma_chan *rx_dma_chan;
117 struct dma_chan *tx_dma_chan;
118 dma_addr_t rx_dma_buf_phys;
119 dma_addr_t tx_dma_buf_phys;
120 unsigned char *rx_dma_buf_virt;
121 unsigned char *tx_dma_buf_virt;
122 struct dma_async_tx_descriptor *tx_dma_desc;
123 struct dma_async_tx_descriptor *rx_dma_desc;
124 dma_cookie_t tx_cookie;
125 dma_cookie_t rx_cookie;
126 unsigned int tx_bytes_requested;
127 unsigned int rx_bytes_requested;
130 static void tegra_uart_start_next_tx(struct tegra_uart_port *tup);
131 static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup);
132 static void tegra_uart_dma_channel_free(struct tegra_uart_port *tup,
135 static inline unsigned long tegra_uart_read(struct tegra_uart_port *tup,
138 return readl(tup->uport.membase + (reg << tup->uport.regshift));
141 static inline void tegra_uart_write(struct tegra_uart_port *tup, unsigned val,
144 writel(val, tup->uport.membase + (reg << tup->uport.regshift));
147 static inline struct tegra_uart_port *to_tegra_uport(struct uart_port *u)
149 return container_of(u, struct tegra_uart_port, uport);
152 static unsigned int tegra_uart_get_mctrl(struct uart_port *u)
154 struct tegra_uart_port *tup = to_tegra_uport(u);
157 * RI - Ring detector is active
158 * CD/DCD/CAR - Carrier detect is always active. For some reason
159 * linux has different names for carrier detect.
160 * DSR - Data Set ready is active as the hardware doesn't support it.
161 * Don't know if the linux support this yet?
162 * CTS - Clear to send. Always set to active, as the hardware handles
165 if (tup->enable_modem_interrupt)
166 return TIOCM_RI | TIOCM_CD | TIOCM_DSR | TIOCM_CTS;
170 static void set_rts(struct tegra_uart_port *tup, bool active)
174 mcr = tup->mcr_shadow;
176 mcr |= TEGRA_UART_MCR_RTS_EN;
178 mcr &= ~TEGRA_UART_MCR_RTS_EN;
179 if (mcr != tup->mcr_shadow) {
180 tegra_uart_write(tup, mcr, UART_MCR);
181 tup->mcr_shadow = mcr;
185 static void set_dtr(struct tegra_uart_port *tup, bool active)
189 mcr = tup->mcr_shadow;
193 mcr &= ~UART_MCR_DTR;
194 if (mcr != tup->mcr_shadow) {
195 tegra_uart_write(tup, mcr, UART_MCR);
196 tup->mcr_shadow = mcr;
200 static void set_loopbk(struct tegra_uart_port *tup, bool active)
202 unsigned long mcr = tup->mcr_shadow;
205 mcr |= UART_MCR_LOOP;
207 mcr &= ~UART_MCR_LOOP;
209 if (mcr != tup->mcr_shadow) {
210 tegra_uart_write(tup, mcr, UART_MCR);
211 tup->mcr_shadow = mcr;
215 static void tegra_uart_set_mctrl(struct uart_port *u, unsigned int mctrl)
217 struct tegra_uart_port *tup = to_tegra_uport(u);
220 tup->rts_active = !!(mctrl & TIOCM_RTS);
221 set_rts(tup, tup->rts_active);
223 enable = !!(mctrl & TIOCM_DTR);
224 set_dtr(tup, enable);
226 enable = !!(mctrl & TIOCM_LOOP);
227 set_loopbk(tup, enable);
230 static void tegra_uart_break_ctl(struct uart_port *u, int break_ctl)
232 struct tegra_uart_port *tup = to_tegra_uport(u);
235 lcr = tup->lcr_shadow;
239 lcr &= ~UART_LCR_SBC;
240 tegra_uart_write(tup, lcr, UART_LCR);
241 tup->lcr_shadow = lcr;
245 * tegra_uart_wait_cycle_time: Wait for N UART clock periods
247 * @tup: Tegra serial port data structure.
248 * @cycles: Number of clock periods to wait.
250 * Tegra UARTs are clocked at 16X the baud/bit rate and hence the UART
251 * clock speed is 16X the current baud rate.
253 static void tegra_uart_wait_cycle_time(struct tegra_uart_port *tup,
256 if (tup->current_baud)
257 udelay(DIV_ROUND_UP(cycles * 1000000, tup->current_baud * 16));
260 /* Wait for a symbol-time. */
261 static void tegra_uart_wait_sym_time(struct tegra_uart_port *tup,
264 if (tup->current_baud)
265 udelay(DIV_ROUND_UP(syms * tup->symb_bit * 1000000,
269 static int tegra_uart_wait_fifo_mode_enabled(struct tegra_uart_port *tup)
272 unsigned int tmout = 100;
275 iir = tegra_uart_read(tup, UART_IIR);
276 if (iir & TEGRA_UART_FCR_IIR_FIFO_EN)
284 static void tegra_uart_fifo_reset(struct tegra_uart_port *tup, u8 fcr_bits)
286 unsigned long fcr = tup->fcr_shadow;
287 unsigned int lsr, tmout = 10000;
292 if (tup->cdata->allow_txfifo_reset_fifo_mode) {
293 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
294 tegra_uart_write(tup, fcr, UART_FCR);
296 fcr &= ~UART_FCR_ENABLE_FIFO;
297 tegra_uart_write(tup, fcr, UART_FCR);
299 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
300 tegra_uart_write(tup, fcr, UART_FCR);
301 fcr |= UART_FCR_ENABLE_FIFO;
302 tegra_uart_write(tup, fcr, UART_FCR);
303 if (tup->cdata->fifo_mode_enable_status)
304 tegra_uart_wait_fifo_mode_enabled(tup);
307 /* Dummy read to ensure the write is posted */
308 tegra_uart_read(tup, UART_SCR);
311 * For all tegra devices (up to t210), there is a hardware issue that
312 * requires software to wait for 32 UART clock periods for the flush
313 * to propagate, otherwise data could be lost.
315 tegra_uart_wait_cycle_time(tup, 32);
318 lsr = tegra_uart_read(tup, UART_LSR);
319 if ((lsr | UART_LSR_TEMT) && !(lsr & UART_LSR_DR))
328 static int tegra_set_baudrate(struct tegra_uart_port *tup, unsigned int baud)
331 unsigned int divisor;
336 if (tup->current_baud == baud)
339 if (tup->cdata->support_clk_src_div) {
341 ret = clk_set_rate(tup->uart_clk, rate);
343 dev_err(tup->uport.dev,
344 "clk_set_rate() failed for rate %lu\n", rate);
349 rate = clk_get_rate(tup->uart_clk);
350 divisor = DIV_ROUND_CLOSEST(rate, baud * 16);
353 spin_lock_irqsave(&tup->uport.lock, flags);
354 lcr = tup->lcr_shadow;
355 lcr |= UART_LCR_DLAB;
356 tegra_uart_write(tup, lcr, UART_LCR);
358 tegra_uart_write(tup, divisor & 0xFF, UART_TX);
359 tegra_uart_write(tup, ((divisor >> 8) & 0xFF), UART_IER);
361 lcr &= ~UART_LCR_DLAB;
362 tegra_uart_write(tup, lcr, UART_LCR);
364 /* Dummy read to ensure the write is posted */
365 tegra_uart_read(tup, UART_SCR);
366 spin_unlock_irqrestore(&tup->uport.lock, flags);
368 tup->current_baud = baud;
370 /* wait two character intervals at new rate */
371 tegra_uart_wait_sym_time(tup, 2);
375 static char tegra_uart_decode_rx_error(struct tegra_uart_port *tup,
378 char flag = TTY_NORMAL;
380 if (unlikely(lsr & TEGRA_UART_LSR_ANY)) {
381 if (lsr & UART_LSR_OE) {
384 tup->uport.icount.overrun++;
385 dev_err(tup->uport.dev, "Got overrun errors\n");
386 } else if (lsr & UART_LSR_PE) {
389 tup->uport.icount.parity++;
390 dev_err(tup->uport.dev, "Got Parity errors\n");
391 } else if (lsr & UART_LSR_FE) {
393 tup->uport.icount.frame++;
394 dev_err(tup->uport.dev, "Got frame errors\n");
395 } else if (lsr & UART_LSR_BI) {
398 * If FIFO read error without any data, reset Rx FIFO
400 if (!(lsr & UART_LSR_DR) && (lsr & UART_LSR_FIFOE))
401 tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_RCVR);
402 if (tup->uport.ignore_status_mask & UART_LSR_BI)
405 tup->uport.icount.brk++;
406 dev_dbg(tup->uport.dev, "Got Break\n");
408 uart_insert_char(&tup->uport, lsr, UART_LSR_OE, 0, flag);
414 static int tegra_uart_request_port(struct uart_port *u)
419 static void tegra_uart_release_port(struct uart_port *u)
421 /* Nothing to do here */
424 static void tegra_uart_fill_tx_fifo(struct tegra_uart_port *tup, int max_bytes)
426 struct circ_buf *xmit = &tup->uport.state->xmit;
429 for (i = 0; i < max_bytes; i++) {
430 BUG_ON(uart_circ_empty(xmit));
431 if (tup->cdata->tx_fifo_full_status) {
432 unsigned long lsr = tegra_uart_read(tup, UART_LSR);
433 if ((lsr & TEGRA_UART_LSR_TXFIFO_FULL))
436 tegra_uart_write(tup, xmit->buf[xmit->tail], UART_TX);
437 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
438 tup->uport.icount.tx++;
442 static void tegra_uart_start_pio_tx(struct tegra_uart_port *tup,
445 if (bytes > TEGRA_UART_MIN_DMA)
446 bytes = TEGRA_UART_MIN_DMA;
448 tup->tx_in_progress = TEGRA_UART_TX_PIO;
449 tup->tx_bytes = bytes;
450 tup->ier_shadow |= UART_IER_THRI;
451 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
454 static void tegra_uart_tx_dma_complete(void *args)
456 struct tegra_uart_port *tup = args;
457 struct circ_buf *xmit = &tup->uport.state->xmit;
458 struct dma_tx_state state;
462 dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
463 count = tup->tx_bytes_requested - state.residue;
464 async_tx_ack(tup->tx_dma_desc);
465 spin_lock_irqsave(&tup->uport.lock, flags);
466 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
467 tup->tx_in_progress = 0;
468 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
469 uart_write_wakeup(&tup->uport);
470 tegra_uart_start_next_tx(tup);
471 spin_unlock_irqrestore(&tup->uport.lock, flags);
474 static int tegra_uart_start_tx_dma(struct tegra_uart_port *tup,
477 struct circ_buf *xmit = &tup->uport.state->xmit;
478 dma_addr_t tx_phys_addr;
480 dma_sync_single_for_device(tup->uport.dev, tup->tx_dma_buf_phys,
481 UART_XMIT_SIZE, DMA_TO_DEVICE);
483 tup->tx_bytes = count & ~(0xF);
484 tx_phys_addr = tup->tx_dma_buf_phys + xmit->tail;
485 tup->tx_dma_desc = dmaengine_prep_slave_single(tup->tx_dma_chan,
486 tx_phys_addr, tup->tx_bytes, DMA_MEM_TO_DEV,
488 if (!tup->tx_dma_desc) {
489 dev_err(tup->uport.dev, "Not able to get desc for Tx\n");
493 tup->tx_dma_desc->callback = tegra_uart_tx_dma_complete;
494 tup->tx_dma_desc->callback_param = tup;
495 tup->tx_in_progress = TEGRA_UART_TX_DMA;
496 tup->tx_bytes_requested = tup->tx_bytes;
497 tup->tx_cookie = dmaengine_submit(tup->tx_dma_desc);
498 dma_async_issue_pending(tup->tx_dma_chan);
502 static void tegra_uart_start_next_tx(struct tegra_uart_port *tup)
506 struct circ_buf *xmit = &tup->uport.state->xmit;
508 if (!tup->current_baud)
511 tail = (unsigned long)&xmit->buf[xmit->tail];
512 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
516 if (count < TEGRA_UART_MIN_DMA)
517 tegra_uart_start_pio_tx(tup, count);
518 else if (BYTES_TO_ALIGN(tail) > 0)
519 tegra_uart_start_pio_tx(tup, BYTES_TO_ALIGN(tail));
521 tegra_uart_start_tx_dma(tup, count);
524 /* Called by serial core driver with u->lock taken. */
525 static void tegra_uart_start_tx(struct uart_port *u)
527 struct tegra_uart_port *tup = to_tegra_uport(u);
528 struct circ_buf *xmit = &u->state->xmit;
530 if (!uart_circ_empty(xmit) && !tup->tx_in_progress)
531 tegra_uart_start_next_tx(tup);
534 static unsigned int tegra_uart_tx_empty(struct uart_port *u)
536 struct tegra_uart_port *tup = to_tegra_uport(u);
537 unsigned int ret = 0;
540 spin_lock_irqsave(&u->lock, flags);
541 if (!tup->tx_in_progress) {
542 unsigned long lsr = tegra_uart_read(tup, UART_LSR);
543 if ((lsr & TX_EMPTY_STATUS) == TX_EMPTY_STATUS)
546 spin_unlock_irqrestore(&u->lock, flags);
550 static void tegra_uart_stop_tx(struct uart_port *u)
552 struct tegra_uart_port *tup = to_tegra_uport(u);
553 struct circ_buf *xmit = &tup->uport.state->xmit;
554 struct dma_tx_state state;
557 if (tup->tx_in_progress != TEGRA_UART_TX_DMA)
560 dmaengine_terminate_all(tup->tx_dma_chan);
561 dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
562 count = tup->tx_bytes_requested - state.residue;
563 async_tx_ack(tup->tx_dma_desc);
564 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
565 tup->tx_in_progress = 0;
568 static void tegra_uart_handle_tx_pio(struct tegra_uart_port *tup)
570 struct circ_buf *xmit = &tup->uport.state->xmit;
572 tegra_uart_fill_tx_fifo(tup, tup->tx_bytes);
573 tup->tx_in_progress = 0;
574 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
575 uart_write_wakeup(&tup->uport);
576 tegra_uart_start_next_tx(tup);
579 static void tegra_uart_handle_rx_pio(struct tegra_uart_port *tup,
580 struct tty_port *tty)
583 char flag = TTY_NORMAL;
584 unsigned long lsr = 0;
587 lsr = tegra_uart_read(tup, UART_LSR);
588 if (!(lsr & UART_LSR_DR))
591 flag = tegra_uart_decode_rx_error(tup, lsr);
592 if (flag != TTY_NORMAL)
595 ch = (unsigned char) tegra_uart_read(tup, UART_RX);
596 tup->uport.icount.rx++;
598 if (!uart_handle_sysrq_char(&tup->uport, ch) && tty)
599 tty_insert_flip_char(tty, ch, flag);
601 if (tup->uport.ignore_status_mask & UART_LSR_DR)
606 static void tegra_uart_copy_rx_to_tty(struct tegra_uart_port *tup,
607 struct tty_port *tty,
612 /* If count is zero, then there is no data to be copied */
616 tup->uport.icount.rx += count;
618 dev_err(tup->uport.dev, "No tty port\n");
622 if (tup->uport.ignore_status_mask & UART_LSR_DR)
625 dma_sync_single_for_cpu(tup->uport.dev, tup->rx_dma_buf_phys,
626 TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
627 copied = tty_insert_flip_string(tty,
628 ((unsigned char *)(tup->rx_dma_buf_virt)), count);
629 if (copied != count) {
631 dev_err(tup->uport.dev, "RxData copy to tty layer failed\n");
633 dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
634 TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_TO_DEVICE);
637 static void tegra_uart_rx_buffer_push(struct tegra_uart_port *tup,
638 unsigned int residue)
640 struct tty_port *port = &tup->uport.state->port;
641 struct tty_struct *tty = tty_port_tty_get(port);
644 async_tx_ack(tup->rx_dma_desc);
645 count = tup->rx_bytes_requested - residue;
647 /* If we are here, DMA is stopped */
648 tegra_uart_copy_rx_to_tty(tup, port, count);
650 tegra_uart_handle_rx_pio(tup, port);
652 tty_flip_buffer_push(port);
657 static void tegra_uart_rx_dma_complete(void *args)
659 struct tegra_uart_port *tup = args;
660 struct uart_port *u = &tup->uport;
662 struct dma_tx_state state;
663 enum dma_status status;
665 spin_lock_irqsave(&u->lock, flags);
667 status = dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
669 if (status == DMA_IN_PROGRESS) {
670 dev_dbg(tup->uport.dev, "RX DMA is in progress\n");
674 /* Deactivate flow control to stop sender */
678 tegra_uart_rx_buffer_push(tup, 0);
679 tegra_uart_start_rx_dma(tup);
681 /* Activate flow control to start transfer */
686 spin_unlock_irqrestore(&u->lock, flags);
689 static void tegra_uart_handle_rx_dma(struct tegra_uart_port *tup)
691 struct dma_tx_state state;
693 /* Deactivate flow control to stop sender */
697 dmaengine_terminate_all(tup->rx_dma_chan);
698 dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
699 tegra_uart_rx_buffer_push(tup, state.residue);
700 tegra_uart_start_rx_dma(tup);
706 static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup)
708 unsigned int count = TEGRA_UART_RX_DMA_BUFFER_SIZE;
710 tup->rx_dma_desc = dmaengine_prep_slave_single(tup->rx_dma_chan,
711 tup->rx_dma_buf_phys, count, DMA_DEV_TO_MEM,
713 if (!tup->rx_dma_desc) {
714 dev_err(tup->uport.dev, "Not able to get desc for Rx\n");
718 tup->rx_dma_desc->callback = tegra_uart_rx_dma_complete;
719 tup->rx_dma_desc->callback_param = tup;
720 dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
721 count, DMA_TO_DEVICE);
722 tup->rx_bytes_requested = count;
723 tup->rx_cookie = dmaengine_submit(tup->rx_dma_desc);
724 dma_async_issue_pending(tup->rx_dma_chan);
728 static void tegra_uart_handle_modem_signal_change(struct uart_port *u)
730 struct tegra_uart_port *tup = to_tegra_uport(u);
733 msr = tegra_uart_read(tup, UART_MSR);
734 if (!(msr & UART_MSR_ANY_DELTA))
737 if (msr & UART_MSR_TERI)
738 tup->uport.icount.rng++;
739 if (msr & UART_MSR_DDSR)
740 tup->uport.icount.dsr++;
741 /* We may only get DDCD when HW init and reset */
742 if (msr & UART_MSR_DDCD)
743 uart_handle_dcd_change(&tup->uport, msr & UART_MSR_DCD);
744 /* Will start/stop_tx accordingly */
745 if (msr & UART_MSR_DCTS)
746 uart_handle_cts_change(&tup->uport, msr & UART_MSR_CTS);
749 static irqreturn_t tegra_uart_isr(int irq, void *data)
751 struct tegra_uart_port *tup = data;
752 struct uart_port *u = &tup->uport;
755 bool is_rx_int = false;
758 spin_lock_irqsave(&u->lock, flags);
760 iir = tegra_uart_read(tup, UART_IIR);
761 if (iir & UART_IIR_NO_INT) {
763 tegra_uart_handle_rx_dma(tup);
764 if (tup->rx_in_progress) {
765 ier = tup->ier_shadow;
766 ier |= (UART_IER_RLSI | UART_IER_RTOIE |
767 TEGRA_UART_IER_EORD);
768 tup->ier_shadow = ier;
769 tegra_uart_write(tup, ier, UART_IER);
772 spin_unlock_irqrestore(&u->lock, flags);
776 switch ((iir >> 1) & 0x7) {
777 case 0: /* Modem signal change interrupt */
778 tegra_uart_handle_modem_signal_change(u);
781 case 1: /* Transmit interrupt only triggered when using PIO */
782 tup->ier_shadow &= ~UART_IER_THRI;
783 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
784 tegra_uart_handle_tx_pio(tup);
787 case 4: /* End of data */
788 case 6: /* Rx timeout */
789 case 2: /* Receive */
792 /* Disable Rx interrupts */
793 ier = tup->ier_shadow;
795 tegra_uart_write(tup, ier, UART_IER);
796 ier &= ~(UART_IER_RDI | UART_IER_RLSI |
797 UART_IER_RTOIE | TEGRA_UART_IER_EORD);
798 tup->ier_shadow = ier;
799 tegra_uart_write(tup, ier, UART_IER);
803 case 3: /* Receive error */
804 tegra_uart_decode_rx_error(tup,
805 tegra_uart_read(tup, UART_LSR));
808 case 5: /* break nothing to handle */
809 case 7: /* break nothing to handle */
815 static void tegra_uart_stop_rx(struct uart_port *u)
817 struct tegra_uart_port *tup = to_tegra_uport(u);
818 struct dma_tx_state state;
824 if (!tup->rx_in_progress)
827 tegra_uart_wait_sym_time(tup, 1); /* wait one character interval */
829 ier = tup->ier_shadow;
830 ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE |
831 TEGRA_UART_IER_EORD);
832 tup->ier_shadow = ier;
833 tegra_uart_write(tup, ier, UART_IER);
834 tup->rx_in_progress = 0;
835 dmaengine_terminate_all(tup->rx_dma_chan);
836 dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
837 tegra_uart_rx_buffer_push(tup, state.residue);
840 static void tegra_uart_hw_deinit(struct tegra_uart_port *tup)
843 unsigned long char_time = DIV_ROUND_UP(10000000, tup->current_baud);
844 unsigned long fifo_empty_time = tup->uport.fifosize * char_time;
845 unsigned long wait_time;
850 /* Disable interrupts */
851 tegra_uart_write(tup, 0, UART_IER);
853 lsr = tegra_uart_read(tup, UART_LSR);
854 if ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
855 msr = tegra_uart_read(tup, UART_MSR);
856 mcr = tegra_uart_read(tup, UART_MCR);
857 if ((mcr & TEGRA_UART_MCR_CTS_EN) && (msr & UART_MSR_CTS))
858 dev_err(tup->uport.dev,
859 "Tx Fifo not empty, CTS disabled, waiting\n");
861 /* Wait for Tx fifo to be empty */
862 while ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
863 wait_time = min(fifo_empty_time, 100lu);
865 fifo_empty_time -= wait_time;
866 if (!fifo_empty_time) {
867 msr = tegra_uart_read(tup, UART_MSR);
868 mcr = tegra_uart_read(tup, UART_MCR);
869 if ((mcr & TEGRA_UART_MCR_CTS_EN) &&
870 (msr & UART_MSR_CTS))
871 dev_err(tup->uport.dev,
872 "Slave not ready\n");
875 lsr = tegra_uart_read(tup, UART_LSR);
879 spin_lock_irqsave(&tup->uport.lock, flags);
880 /* Reset the Rx and Tx FIFOs */
881 tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_XMIT | UART_FCR_CLEAR_RCVR);
882 tup->current_baud = 0;
883 spin_unlock_irqrestore(&tup->uport.lock, flags);
885 tup->rx_in_progress = 0;
886 tup->tx_in_progress = 0;
888 tegra_uart_dma_channel_free(tup, true);
889 tegra_uart_dma_channel_free(tup, false);
891 clk_disable_unprepare(tup->uart_clk);
894 static int tegra_uart_hw_init(struct tegra_uart_port *tup)
902 tup->current_baud = 0;
904 clk_prepare_enable(tup->uart_clk);
906 /* Reset the UART controller to clear all previous status.*/
907 reset_control_assert(tup->rst);
909 reset_control_deassert(tup->rst);
911 tup->rx_in_progress = 0;
912 tup->tx_in_progress = 0;
915 * Set the trigger level
919 * For receive, this will interrupt the CPU after that many number of
920 * bytes are received, for the remaining bytes the receive timeout
921 * interrupt is received. Rx high watermark is set to 4.
923 * For transmit, if the trasnmit interrupt is enabled, this will
924 * interrupt the CPU when the number of entries in the FIFO reaches the
925 * low watermark. Tx low watermark is set to 16 bytes.
929 * Set the Tx trigger to 16. This should match the DMA burst size that
930 * programmed in the DMA registers.
932 tup->fcr_shadow = UART_FCR_ENABLE_FIFO;
933 tup->fcr_shadow |= UART_FCR_R_TRIG_01;
934 tup->fcr_shadow |= TEGRA_UART_TX_TRIG_16B;
935 tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
937 /* Dummy read to ensure the write is posted */
938 tegra_uart_read(tup, UART_SCR);
940 if (tup->cdata->fifo_mode_enable_status) {
941 ret = tegra_uart_wait_fifo_mode_enabled(tup);
942 dev_err(tup->uport.dev, "FIFO mode not enabled\n");
947 * For all tegra devices (up to t210), there is a hardware
948 * issue that requires software to wait for 3 UART clock
949 * periods after enabling the TX fifo, otherwise data could
952 tegra_uart_wait_cycle_time(tup, 3);
956 * Initialize the UART with default configuration
957 * (115200, N, 8, 1) so that the receive DMA buffer may be
960 tup->lcr_shadow = TEGRA_UART_DEFAULT_LSR;
961 tegra_set_baudrate(tup, TEGRA_UART_DEFAULT_BAUD);
962 tup->fcr_shadow |= UART_FCR_DMA_SELECT;
963 tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
965 ret = tegra_uart_start_rx_dma(tup);
967 dev_err(tup->uport.dev, "Not able to start Rx DMA\n");
970 tup->rx_in_progress = 1;
973 * Enable IE_RXS for the receive status interrupts like line errros.
974 * Enable IE_RX_TIMEOUT to get the bytes which cannot be DMA'd.
976 * If using DMA mode, enable EORD instead of receive interrupt which
977 * will interrupt after the UART is done with the receive instead of
978 * the interrupt when the FIFO "threshold" is reached.
980 * EORD is different interrupt than RX_TIMEOUT - RX_TIMEOUT occurs when
981 * the DATA is sitting in the FIFO and couldn't be transferred to the
982 * DMA as the DMA size alignment (4 bytes) is not met. EORD will be
983 * triggered when there is a pause of the incomming data stream for 4
986 * For pauses in the data which is not aligned to 4 bytes, we get
987 * both the EORD as well as RX_TIMEOUT - SW sees RX_TIMEOUT first
990 tup->ier_shadow = UART_IER_RLSI | UART_IER_RTOIE | TEGRA_UART_IER_EORD;
991 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
995 static void tegra_uart_dma_channel_free(struct tegra_uart_port *tup,
999 dmaengine_terminate_all(tup->rx_dma_chan);
1000 dma_release_channel(tup->rx_dma_chan);
1001 dma_free_coherent(tup->uport.dev, TEGRA_UART_RX_DMA_BUFFER_SIZE,
1002 tup->rx_dma_buf_virt, tup->rx_dma_buf_phys);
1003 tup->rx_dma_chan = NULL;
1004 tup->rx_dma_buf_phys = 0;
1005 tup->rx_dma_buf_virt = NULL;
1007 dmaengine_terminate_all(tup->tx_dma_chan);
1008 dma_release_channel(tup->tx_dma_chan);
1009 dma_unmap_single(tup->uport.dev, tup->tx_dma_buf_phys,
1010 UART_XMIT_SIZE, DMA_TO_DEVICE);
1011 tup->tx_dma_chan = NULL;
1012 tup->tx_dma_buf_phys = 0;
1013 tup->tx_dma_buf_virt = NULL;
1017 static int tegra_uart_dma_channel_allocate(struct tegra_uart_port *tup,
1020 struct dma_chan *dma_chan;
1021 unsigned char *dma_buf;
1022 dma_addr_t dma_phys;
1024 struct dma_slave_config dma_sconfig;
1026 dma_chan = dma_request_slave_channel_reason(tup->uport.dev,
1027 dma_to_memory ? "rx" : "tx");
1028 if (IS_ERR(dma_chan)) {
1029 ret = PTR_ERR(dma_chan);
1030 dev_err(tup->uport.dev,
1031 "DMA channel alloc failed: %d\n", ret);
1035 if (dma_to_memory) {
1036 dma_buf = dma_alloc_coherent(tup->uport.dev,
1037 TEGRA_UART_RX_DMA_BUFFER_SIZE,
1038 &dma_phys, GFP_KERNEL);
1040 dev_err(tup->uport.dev,
1041 "Not able to allocate the dma buffer\n");
1042 dma_release_channel(dma_chan);
1045 dma_sconfig.src_addr = tup->uport.mapbase;
1046 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1047 dma_sconfig.src_maxburst = 4;
1048 tup->rx_dma_chan = dma_chan;
1049 tup->rx_dma_buf_virt = dma_buf;
1050 tup->rx_dma_buf_phys = dma_phys;
1052 dma_phys = dma_map_single(tup->uport.dev,
1053 tup->uport.state->xmit.buf, UART_XMIT_SIZE,
1055 if (dma_mapping_error(tup->uport.dev, dma_phys)) {
1056 dev_err(tup->uport.dev, "dma_map_single tx failed\n");
1057 dma_release_channel(dma_chan);
1060 dma_buf = tup->uport.state->xmit.buf;
1061 dma_sconfig.dst_addr = tup->uport.mapbase;
1062 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1063 dma_sconfig.dst_maxburst = 16;
1064 tup->tx_dma_chan = dma_chan;
1065 tup->tx_dma_buf_virt = dma_buf;
1066 tup->tx_dma_buf_phys = dma_phys;
1069 ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
1071 dev_err(tup->uport.dev,
1072 "Dma slave config failed, err = %d\n", ret);
1073 tegra_uart_dma_channel_free(tup, dma_to_memory);
1080 static int tegra_uart_startup(struct uart_port *u)
1082 struct tegra_uart_port *tup = to_tegra_uport(u);
1085 ret = tegra_uart_dma_channel_allocate(tup, false);
1087 dev_err(u->dev, "Tx Dma allocation failed, err = %d\n", ret);
1091 ret = tegra_uart_dma_channel_allocate(tup, true);
1093 dev_err(u->dev, "Rx Dma allocation failed, err = %d\n", ret);
1097 ret = tegra_uart_hw_init(tup);
1099 dev_err(u->dev, "Uart HW init failed, err = %d\n", ret);
1103 ret = request_irq(u->irq, tegra_uart_isr, 0,
1104 dev_name(u->dev), tup);
1106 dev_err(u->dev, "Failed to register ISR for IRQ %d\n", u->irq);
1112 tegra_uart_dma_channel_free(tup, true);
1114 tegra_uart_dma_channel_free(tup, false);
1119 * Flush any TX data submitted for DMA and PIO. Called when the
1120 * TX circular buffer is reset.
1122 static void tegra_uart_flush_buffer(struct uart_port *u)
1124 struct tegra_uart_port *tup = to_tegra_uport(u);
1127 if (tup->tx_dma_chan)
1128 dmaengine_terminate_all(tup->tx_dma_chan);
1131 static void tegra_uart_shutdown(struct uart_port *u)
1133 struct tegra_uart_port *tup = to_tegra_uport(u);
1135 tegra_uart_hw_deinit(tup);
1136 free_irq(u->irq, tup);
1139 static void tegra_uart_enable_ms(struct uart_port *u)
1141 struct tegra_uart_port *tup = to_tegra_uport(u);
1143 if (tup->enable_modem_interrupt) {
1144 tup->ier_shadow |= UART_IER_MSI;
1145 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1149 static void tegra_uart_set_termios(struct uart_port *u,
1150 struct ktermios *termios, struct ktermios *oldtermios)
1152 struct tegra_uart_port *tup = to_tegra_uport(u);
1154 unsigned long flags;
1157 struct clk *parent_clk = clk_get_parent(tup->uart_clk);
1158 unsigned long parent_clk_rate = clk_get_rate(parent_clk);
1159 int max_divider = (tup->cdata->support_clk_src_div) ? 0x7FFF : 0xFFFF;
1162 spin_lock_irqsave(&u->lock, flags);
1164 /* Changing configuration, it is safe to stop any rx now */
1165 if (tup->rts_active)
1166 set_rts(tup, false);
1168 /* Clear all interrupts as configuration is going to be changed */
1169 tegra_uart_write(tup, tup->ier_shadow | UART_IER_RDI, UART_IER);
1170 tegra_uart_read(tup, UART_IER);
1171 tegra_uart_write(tup, 0, UART_IER);
1172 tegra_uart_read(tup, UART_IER);
1175 lcr = tup->lcr_shadow;
1176 lcr &= ~UART_LCR_PARITY;
1178 /* CMSPAR isn't supported by this driver */
1179 termios->c_cflag &= ~CMSPAR;
1181 if ((termios->c_cflag & PARENB) == PARENB) {
1183 if (termios->c_cflag & PARODD) {
1184 lcr |= UART_LCR_PARITY;
1185 lcr &= ~UART_LCR_EPAR;
1186 lcr &= ~UART_LCR_SPAR;
1188 lcr |= UART_LCR_PARITY;
1189 lcr |= UART_LCR_EPAR;
1190 lcr &= ~UART_LCR_SPAR;
1194 lcr &= ~UART_LCR_WLEN8;
1195 switch (termios->c_cflag & CSIZE) {
1197 lcr |= UART_LCR_WLEN5;
1201 lcr |= UART_LCR_WLEN6;
1205 lcr |= UART_LCR_WLEN7;
1209 lcr |= UART_LCR_WLEN8;
1215 if (termios->c_cflag & CSTOPB) {
1216 lcr |= UART_LCR_STOP;
1219 lcr &= ~UART_LCR_STOP;
1223 tegra_uart_write(tup, lcr, UART_LCR);
1224 tup->lcr_shadow = lcr;
1225 tup->symb_bit = symb_bit;
1228 baud = uart_get_baud_rate(u, termios, oldtermios,
1229 parent_clk_rate/max_divider,
1230 parent_clk_rate/16);
1231 spin_unlock_irqrestore(&u->lock, flags);
1232 tegra_set_baudrate(tup, baud);
1233 if (tty_termios_baud_rate(termios))
1234 tty_termios_encode_baud_rate(termios, baud, baud);
1235 spin_lock_irqsave(&u->lock, flags);
1238 if (termios->c_cflag & CRTSCTS) {
1239 tup->mcr_shadow |= TEGRA_UART_MCR_CTS_EN;
1240 tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
1241 tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
1242 /* if top layer has asked to set rts active then do so here */
1243 if (tup->rts_active)
1246 tup->mcr_shadow &= ~TEGRA_UART_MCR_CTS_EN;
1247 tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
1248 tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
1251 /* update the port timeout based on new settings */
1252 uart_update_timeout(u, termios->c_cflag, baud);
1254 /* Make sure all writes have completed */
1255 tegra_uart_read(tup, UART_IER);
1257 /* Re-enable interrupt */
1258 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1259 tegra_uart_read(tup, UART_IER);
1261 tup->uport.ignore_status_mask = 0;
1262 /* Ignore all characters if CREAD is not set */
1263 if ((termios->c_cflag & CREAD) == 0)
1264 tup->uport.ignore_status_mask |= UART_LSR_DR;
1265 if (termios->c_iflag & IGNBRK)
1266 tup->uport.ignore_status_mask |= UART_LSR_BI;
1268 spin_unlock_irqrestore(&u->lock, flags);
1271 static const char *tegra_uart_type(struct uart_port *u)
1273 return TEGRA_UART_TYPE;
1276 static const struct uart_ops tegra_uart_ops = {
1277 .tx_empty = tegra_uart_tx_empty,
1278 .set_mctrl = tegra_uart_set_mctrl,
1279 .get_mctrl = tegra_uart_get_mctrl,
1280 .stop_tx = tegra_uart_stop_tx,
1281 .start_tx = tegra_uart_start_tx,
1282 .stop_rx = tegra_uart_stop_rx,
1283 .flush_buffer = tegra_uart_flush_buffer,
1284 .enable_ms = tegra_uart_enable_ms,
1285 .break_ctl = tegra_uart_break_ctl,
1286 .startup = tegra_uart_startup,
1287 .shutdown = tegra_uart_shutdown,
1288 .set_termios = tegra_uart_set_termios,
1289 .type = tegra_uart_type,
1290 .request_port = tegra_uart_request_port,
1291 .release_port = tegra_uart_release_port,
1294 static struct uart_driver tegra_uart_driver = {
1295 .owner = THIS_MODULE,
1296 .driver_name = "tegra_hsuart",
1297 .dev_name = "ttyTHS",
1299 .nr = TEGRA_UART_MAXIMUM,
1302 static int tegra_uart_parse_dt(struct platform_device *pdev,
1303 struct tegra_uart_port *tup)
1305 struct device_node *np = pdev->dev.of_node;
1308 port = of_alias_get_id(np, "serial");
1310 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", port);
1313 tup->uport.line = port;
1315 tup->enable_modem_interrupt = of_property_read_bool(np,
1316 "nvidia,enable-modem-interrupt");
1320 static struct tegra_uart_chip_data tegra20_uart_chip_data = {
1321 .tx_fifo_full_status = false,
1322 .allow_txfifo_reset_fifo_mode = true,
1323 .support_clk_src_div = false,
1324 .fifo_mode_enable_status = false,
1327 static struct tegra_uart_chip_data tegra30_uart_chip_data = {
1328 .tx_fifo_full_status = true,
1329 .allow_txfifo_reset_fifo_mode = false,
1330 .support_clk_src_div = true,
1331 .fifo_mode_enable_status = false,
1334 static struct tegra_uart_chip_data tegra186_uart_chip_data = {
1335 .tx_fifo_full_status = true,
1336 .allow_txfifo_reset_fifo_mode = false,
1337 .support_clk_src_div = true,
1338 .fifo_mode_enable_status = true,
1341 static const struct of_device_id tegra_uart_of_match[] = {
1343 .compatible = "nvidia,tegra30-hsuart",
1344 .data = &tegra30_uart_chip_data,
1346 .compatible = "nvidia,tegra20-hsuart",
1347 .data = &tegra20_uart_chip_data,
1349 .compatible = "nvidia,tegra186-hsuart",
1350 .data = &tegra186_uart_chip_data,
1354 MODULE_DEVICE_TABLE(of, tegra_uart_of_match);
1356 static int tegra_uart_probe(struct platform_device *pdev)
1358 struct tegra_uart_port *tup;
1359 struct uart_port *u;
1360 struct resource *resource;
1362 const struct tegra_uart_chip_data *cdata;
1363 const struct of_device_id *match;
1365 match = of_match_device(tegra_uart_of_match, &pdev->dev);
1367 dev_err(&pdev->dev, "Error: No device match found\n");
1370 cdata = match->data;
1372 tup = devm_kzalloc(&pdev->dev, sizeof(*tup), GFP_KERNEL);
1374 dev_err(&pdev->dev, "Failed to allocate memory for tup\n");
1378 ret = tegra_uart_parse_dt(pdev, tup);
1383 u->dev = &pdev->dev;
1384 u->ops = &tegra_uart_ops;
1385 u->type = PORT_TEGRA;
1389 platform_set_drvdata(pdev, tup);
1390 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1392 dev_err(&pdev->dev, "No IO memory resource\n");
1396 u->mapbase = resource->start;
1397 u->membase = devm_ioremap_resource(&pdev->dev, resource);
1398 if (IS_ERR(u->membase))
1399 return PTR_ERR(u->membase);
1401 tup->uart_clk = devm_clk_get(&pdev->dev, NULL);
1402 if (IS_ERR(tup->uart_clk)) {
1403 dev_err(&pdev->dev, "Couldn't get the clock\n");
1404 return PTR_ERR(tup->uart_clk);
1407 tup->rst = devm_reset_control_get_exclusive(&pdev->dev, "serial");
1408 if (IS_ERR(tup->rst)) {
1409 dev_err(&pdev->dev, "Couldn't get the reset\n");
1410 return PTR_ERR(tup->rst);
1413 u->iotype = UPIO_MEM32;
1414 ret = platform_get_irq(pdev, 0);
1419 ret = uart_add_one_port(&tegra_uart_driver, u);
1421 dev_err(&pdev->dev, "Failed to add uart port, err %d\n", ret);
1427 static int tegra_uart_remove(struct platform_device *pdev)
1429 struct tegra_uart_port *tup = platform_get_drvdata(pdev);
1430 struct uart_port *u = &tup->uport;
1432 uart_remove_one_port(&tegra_uart_driver, u);
1436 #ifdef CONFIG_PM_SLEEP
1437 static int tegra_uart_suspend(struct device *dev)
1439 struct tegra_uart_port *tup = dev_get_drvdata(dev);
1440 struct uart_port *u = &tup->uport;
1442 return uart_suspend_port(&tegra_uart_driver, u);
1445 static int tegra_uart_resume(struct device *dev)
1447 struct tegra_uart_port *tup = dev_get_drvdata(dev);
1448 struct uart_port *u = &tup->uport;
1450 return uart_resume_port(&tegra_uart_driver, u);
1454 static const struct dev_pm_ops tegra_uart_pm_ops = {
1455 SET_SYSTEM_SLEEP_PM_OPS(tegra_uart_suspend, tegra_uart_resume)
1458 static struct platform_driver tegra_uart_platform_driver = {
1459 .probe = tegra_uart_probe,
1460 .remove = tegra_uart_remove,
1462 .name = "serial-tegra",
1463 .of_match_table = tegra_uart_of_match,
1464 .pm = &tegra_uart_pm_ops,
1468 static int __init tegra_uart_init(void)
1472 ret = uart_register_driver(&tegra_uart_driver);
1474 pr_err("Could not register %s driver\n",
1475 tegra_uart_driver.driver_name);
1479 ret = platform_driver_register(&tegra_uart_platform_driver);
1481 pr_err("Uart platform driver register failed, e = %d\n", ret);
1482 uart_unregister_driver(&tegra_uart_driver);
1488 static void __exit tegra_uart_exit(void)
1490 pr_info("Unloading tegra uart driver\n");
1491 platform_driver_unregister(&tegra_uart_platform_driver);
1492 uart_unregister_driver(&tegra_uart_driver);
1495 module_init(tegra_uart_init);
1496 module_exit(tegra_uart_exit);
1498 MODULE_ALIAS("platform:serial-tegra");
1499 MODULE_DESCRIPTION("High speed UART driver for tegra chipset");
1500 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1501 MODULE_LICENSE("GPL v2");