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);
130 static inline unsigned long tegra_uart_read(struct tegra_uart_port *tup,
133 return readl(tup->uport.membase + (reg << tup->uport.regshift));
136 static inline void tegra_uart_write(struct tegra_uart_port *tup, unsigned val,
139 writel(val, tup->uport.membase + (reg << tup->uport.regshift));
142 static inline struct tegra_uart_port *to_tegra_uport(struct uart_port *u)
144 return container_of(u, struct tegra_uart_port, uport);
147 static unsigned int tegra_uart_get_mctrl(struct uart_port *u)
149 struct tegra_uart_port *tup = to_tegra_uport(u);
152 * RI - Ring detector is active
153 * CD/DCD/CAR - Carrier detect is always active. For some reason
154 * linux has different names for carrier detect.
155 * DSR - Data Set ready is active as the hardware doesn't support it.
156 * Don't know if the linux support this yet?
157 * CTS - Clear to send. Always set to active, as the hardware handles
160 if (tup->enable_modem_interrupt)
161 return TIOCM_RI | TIOCM_CD | TIOCM_DSR | TIOCM_CTS;
165 static void set_rts(struct tegra_uart_port *tup, bool active)
169 mcr = tup->mcr_shadow;
171 mcr |= TEGRA_UART_MCR_RTS_EN;
173 mcr &= ~TEGRA_UART_MCR_RTS_EN;
174 if (mcr != tup->mcr_shadow) {
175 tegra_uart_write(tup, mcr, UART_MCR);
176 tup->mcr_shadow = mcr;
180 static void set_dtr(struct tegra_uart_port *tup, bool active)
184 mcr = tup->mcr_shadow;
188 mcr &= ~UART_MCR_DTR;
189 if (mcr != tup->mcr_shadow) {
190 tegra_uart_write(tup, mcr, UART_MCR);
191 tup->mcr_shadow = mcr;
195 static void set_loopbk(struct tegra_uart_port *tup, bool active)
197 unsigned long mcr = tup->mcr_shadow;
200 mcr |= UART_MCR_LOOP;
202 mcr &= ~UART_MCR_LOOP;
204 if (mcr != tup->mcr_shadow) {
205 tegra_uart_write(tup, mcr, UART_MCR);
206 tup->mcr_shadow = mcr;
210 static void tegra_uart_set_mctrl(struct uart_port *u, unsigned int mctrl)
212 struct tegra_uart_port *tup = to_tegra_uport(u);
215 tup->rts_active = !!(mctrl & TIOCM_RTS);
216 set_rts(tup, tup->rts_active);
218 enable = !!(mctrl & TIOCM_DTR);
219 set_dtr(tup, enable);
221 enable = !!(mctrl & TIOCM_LOOP);
222 set_loopbk(tup, enable);
225 static void tegra_uart_break_ctl(struct uart_port *u, int break_ctl)
227 struct tegra_uart_port *tup = to_tegra_uport(u);
230 lcr = tup->lcr_shadow;
234 lcr &= ~UART_LCR_SBC;
235 tegra_uart_write(tup, lcr, UART_LCR);
236 tup->lcr_shadow = lcr;
240 * tegra_uart_wait_cycle_time: Wait for N UART clock periods
242 * @tup: Tegra serial port data structure.
243 * @cycles: Number of clock periods to wait.
245 * Tegra UARTs are clocked at 16X the baud/bit rate and hence the UART
246 * clock speed is 16X the current baud rate.
248 static void tegra_uart_wait_cycle_time(struct tegra_uart_port *tup,
251 if (tup->current_baud)
252 udelay(DIV_ROUND_UP(cycles * 1000000, tup->current_baud * 16));
255 /* Wait for a symbol-time. */
256 static void tegra_uart_wait_sym_time(struct tegra_uart_port *tup,
259 if (tup->current_baud)
260 udelay(DIV_ROUND_UP(syms * tup->symb_bit * 1000000,
264 static void tegra_uart_fifo_reset(struct tegra_uart_port *tup, u8 fcr_bits)
266 unsigned long fcr = tup->fcr_shadow;
268 if (tup->cdata->allow_txfifo_reset_fifo_mode) {
269 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
270 tegra_uart_write(tup, fcr, UART_FCR);
272 fcr &= ~UART_FCR_ENABLE_FIFO;
273 tegra_uart_write(tup, fcr, UART_FCR);
275 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
276 tegra_uart_write(tup, fcr, UART_FCR);
277 fcr |= UART_FCR_ENABLE_FIFO;
278 tegra_uart_write(tup, fcr, UART_FCR);
281 /* Dummy read to ensure the write is posted */
282 tegra_uart_read(tup, UART_SCR);
285 * For all tegra devices (up to t210), there is a hardware issue that
286 * requires software to wait for 32 UART clock periods for the flush
287 * to propagate, otherwise data could be lost.
289 tegra_uart_wait_cycle_time(tup, 32);
292 static int tegra_set_baudrate(struct tegra_uart_port *tup, unsigned int baud)
295 unsigned int divisor;
300 if (tup->current_baud == baud)
303 if (tup->cdata->support_clk_src_div) {
305 ret = clk_set_rate(tup->uart_clk, rate);
307 dev_err(tup->uport.dev,
308 "clk_set_rate() failed for rate %lu\n", rate);
313 rate = clk_get_rate(tup->uart_clk);
314 divisor = DIV_ROUND_CLOSEST(rate, baud * 16);
317 spin_lock_irqsave(&tup->uport.lock, flags);
318 lcr = tup->lcr_shadow;
319 lcr |= UART_LCR_DLAB;
320 tegra_uart_write(tup, lcr, UART_LCR);
322 tegra_uart_write(tup, divisor & 0xFF, UART_TX);
323 tegra_uart_write(tup, ((divisor >> 8) & 0xFF), UART_IER);
325 lcr &= ~UART_LCR_DLAB;
326 tegra_uart_write(tup, lcr, UART_LCR);
328 /* Dummy read to ensure the write is posted */
329 tegra_uart_read(tup, UART_SCR);
330 spin_unlock_irqrestore(&tup->uport.lock, flags);
332 tup->current_baud = baud;
334 /* wait two character intervals at new rate */
335 tegra_uart_wait_sym_time(tup, 2);
339 static char tegra_uart_decode_rx_error(struct tegra_uart_port *tup,
342 char flag = TTY_NORMAL;
344 if (unlikely(lsr & TEGRA_UART_LSR_ANY)) {
345 if (lsr & UART_LSR_OE) {
348 tup->uport.icount.overrun++;
349 dev_err(tup->uport.dev, "Got overrun errors\n");
350 } else if (lsr & UART_LSR_PE) {
353 tup->uport.icount.parity++;
354 dev_err(tup->uport.dev, "Got Parity errors\n");
355 } else if (lsr & UART_LSR_FE) {
357 tup->uport.icount.frame++;
358 dev_err(tup->uport.dev, "Got frame errors\n");
359 } else if (lsr & UART_LSR_BI) {
360 dev_err(tup->uport.dev, "Got Break\n");
361 tup->uport.icount.brk++;
362 /* If FIFO read error without any data, reset Rx FIFO */
363 if (!(lsr & UART_LSR_DR) && (lsr & UART_LSR_FIFOE))
364 tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_RCVR);
370 static int tegra_uart_request_port(struct uart_port *u)
375 static void tegra_uart_release_port(struct uart_port *u)
377 /* Nothing to do here */
380 static void tegra_uart_fill_tx_fifo(struct tegra_uart_port *tup, int max_bytes)
382 struct circ_buf *xmit = &tup->uport.state->xmit;
385 for (i = 0; i < max_bytes; i++) {
386 BUG_ON(uart_circ_empty(xmit));
387 if (tup->cdata->tx_fifo_full_status) {
388 unsigned long lsr = tegra_uart_read(tup, UART_LSR);
389 if ((lsr & TEGRA_UART_LSR_TXFIFO_FULL))
392 tegra_uart_write(tup, xmit->buf[xmit->tail], UART_TX);
393 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
394 tup->uport.icount.tx++;
398 static void tegra_uart_start_pio_tx(struct tegra_uart_port *tup,
401 if (bytes > TEGRA_UART_MIN_DMA)
402 bytes = TEGRA_UART_MIN_DMA;
404 tup->tx_in_progress = TEGRA_UART_TX_PIO;
405 tup->tx_bytes = bytes;
406 tup->ier_shadow |= UART_IER_THRI;
407 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
410 static void tegra_uart_tx_dma_complete(void *args)
412 struct tegra_uart_port *tup = args;
413 struct circ_buf *xmit = &tup->uport.state->xmit;
414 struct dma_tx_state state;
418 dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
419 count = tup->tx_bytes_requested - state.residue;
420 async_tx_ack(tup->tx_dma_desc);
421 spin_lock_irqsave(&tup->uport.lock, flags);
422 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
423 tup->tx_in_progress = 0;
424 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
425 uart_write_wakeup(&tup->uport);
426 tegra_uart_start_next_tx(tup);
427 spin_unlock_irqrestore(&tup->uport.lock, flags);
430 static int tegra_uart_start_tx_dma(struct tegra_uart_port *tup,
433 struct circ_buf *xmit = &tup->uport.state->xmit;
434 dma_addr_t tx_phys_addr;
436 dma_sync_single_for_device(tup->uport.dev, tup->tx_dma_buf_phys,
437 UART_XMIT_SIZE, DMA_TO_DEVICE);
439 tup->tx_bytes = count & ~(0xF);
440 tx_phys_addr = tup->tx_dma_buf_phys + xmit->tail;
441 tup->tx_dma_desc = dmaengine_prep_slave_single(tup->tx_dma_chan,
442 tx_phys_addr, tup->tx_bytes, DMA_MEM_TO_DEV,
444 if (!tup->tx_dma_desc) {
445 dev_err(tup->uport.dev, "Not able to get desc for Tx\n");
449 tup->tx_dma_desc->callback = tegra_uart_tx_dma_complete;
450 tup->tx_dma_desc->callback_param = tup;
451 tup->tx_in_progress = TEGRA_UART_TX_DMA;
452 tup->tx_bytes_requested = tup->tx_bytes;
453 tup->tx_cookie = dmaengine_submit(tup->tx_dma_desc);
454 dma_async_issue_pending(tup->tx_dma_chan);
458 static void tegra_uart_start_next_tx(struct tegra_uart_port *tup)
462 struct circ_buf *xmit = &tup->uport.state->xmit;
464 tail = (unsigned long)&xmit->buf[xmit->tail];
465 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
469 if (count < TEGRA_UART_MIN_DMA)
470 tegra_uart_start_pio_tx(tup, count);
471 else if (BYTES_TO_ALIGN(tail) > 0)
472 tegra_uart_start_pio_tx(tup, BYTES_TO_ALIGN(tail));
474 tegra_uart_start_tx_dma(tup, count);
477 /* Called by serial core driver with u->lock taken. */
478 static void tegra_uart_start_tx(struct uart_port *u)
480 struct tegra_uart_port *tup = to_tegra_uport(u);
481 struct circ_buf *xmit = &u->state->xmit;
483 if (!uart_circ_empty(xmit) && !tup->tx_in_progress)
484 tegra_uart_start_next_tx(tup);
487 static unsigned int tegra_uart_tx_empty(struct uart_port *u)
489 struct tegra_uart_port *tup = to_tegra_uport(u);
490 unsigned int ret = 0;
493 spin_lock_irqsave(&u->lock, flags);
494 if (!tup->tx_in_progress) {
495 unsigned long lsr = tegra_uart_read(tup, UART_LSR);
496 if ((lsr & TX_EMPTY_STATUS) == TX_EMPTY_STATUS)
499 spin_unlock_irqrestore(&u->lock, flags);
503 static void tegra_uart_stop_tx(struct uart_port *u)
505 struct tegra_uart_port *tup = to_tegra_uport(u);
506 struct circ_buf *xmit = &tup->uport.state->xmit;
507 struct dma_tx_state state;
510 if (tup->tx_in_progress != TEGRA_UART_TX_DMA)
513 dmaengine_terminate_all(tup->tx_dma_chan);
514 dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
515 count = tup->tx_bytes_requested - state.residue;
516 async_tx_ack(tup->tx_dma_desc);
517 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
518 tup->tx_in_progress = 0;
521 static void tegra_uart_handle_tx_pio(struct tegra_uart_port *tup)
523 struct circ_buf *xmit = &tup->uport.state->xmit;
525 tegra_uart_fill_tx_fifo(tup, tup->tx_bytes);
526 tup->tx_in_progress = 0;
527 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
528 uart_write_wakeup(&tup->uport);
529 tegra_uart_start_next_tx(tup);
532 static void tegra_uart_handle_rx_pio(struct tegra_uart_port *tup,
533 struct tty_port *tty)
536 char flag = TTY_NORMAL;
537 unsigned long lsr = 0;
540 lsr = tegra_uart_read(tup, UART_LSR);
541 if (!(lsr & UART_LSR_DR))
544 flag = tegra_uart_decode_rx_error(tup, lsr);
545 ch = (unsigned char) tegra_uart_read(tup, UART_RX);
546 tup->uport.icount.rx++;
548 if (!uart_handle_sysrq_char(&tup->uport, ch) && tty)
549 tty_insert_flip_char(tty, ch, flag);
551 if (tup->uport.ignore_status_mask & UART_LSR_DR)
556 static void tegra_uart_copy_rx_to_tty(struct tegra_uart_port *tup,
557 struct tty_port *tty,
562 /* If count is zero, then there is no data to be copied */
566 tup->uport.icount.rx += count;
568 dev_err(tup->uport.dev, "No tty port\n");
572 if (tup->uport.ignore_status_mask & UART_LSR_DR)
575 dma_sync_single_for_cpu(tup->uport.dev, tup->rx_dma_buf_phys,
576 TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
577 copied = tty_insert_flip_string(tty,
578 ((unsigned char *)(tup->rx_dma_buf_virt)), count);
579 if (copied != count) {
581 dev_err(tup->uport.dev, "RxData copy to tty layer failed\n");
583 dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
584 TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_TO_DEVICE);
587 static void tegra_uart_rx_buffer_push(struct tegra_uart_port *tup,
588 unsigned int residue)
590 struct tty_port *port = &tup->uport.state->port;
591 struct tty_struct *tty = tty_port_tty_get(port);
594 async_tx_ack(tup->rx_dma_desc);
595 count = tup->rx_bytes_requested - residue;
597 /* If we are here, DMA is stopped */
598 tegra_uart_copy_rx_to_tty(tup, port, count);
600 tegra_uart_handle_rx_pio(tup, port);
602 tty_flip_buffer_push(port);
607 static void tegra_uart_rx_dma_complete(void *args)
609 struct tegra_uart_port *tup = args;
610 struct uart_port *u = &tup->uport;
612 struct dma_tx_state state;
613 enum dma_status status;
615 spin_lock_irqsave(&u->lock, flags);
617 status = dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
619 if (status == DMA_IN_PROGRESS) {
620 dev_dbg(tup->uport.dev, "RX DMA is in progress\n");
624 /* Deactivate flow control to stop sender */
628 tegra_uart_rx_buffer_push(tup, 0);
629 tegra_uart_start_rx_dma(tup);
631 /* Activate flow control to start transfer */
636 spin_unlock_irqrestore(&u->lock, flags);
639 static void tegra_uart_handle_rx_dma(struct tegra_uart_port *tup)
641 struct dma_tx_state state;
643 /* Deactivate flow control to stop sender */
647 dmaengine_terminate_all(tup->rx_dma_chan);
648 dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
649 tegra_uart_rx_buffer_push(tup, state.residue);
650 tegra_uart_start_rx_dma(tup);
656 static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup)
658 unsigned int count = TEGRA_UART_RX_DMA_BUFFER_SIZE;
660 tup->rx_dma_desc = dmaengine_prep_slave_single(tup->rx_dma_chan,
661 tup->rx_dma_buf_phys, count, DMA_DEV_TO_MEM,
663 if (!tup->rx_dma_desc) {
664 dev_err(tup->uport.dev, "Not able to get desc for Rx\n");
668 tup->rx_dma_desc->callback = tegra_uart_rx_dma_complete;
669 tup->rx_dma_desc->callback_param = tup;
670 dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
671 count, DMA_TO_DEVICE);
672 tup->rx_bytes_requested = count;
673 tup->rx_cookie = dmaengine_submit(tup->rx_dma_desc);
674 dma_async_issue_pending(tup->rx_dma_chan);
678 static void tegra_uart_handle_modem_signal_change(struct uart_port *u)
680 struct tegra_uart_port *tup = to_tegra_uport(u);
683 msr = tegra_uart_read(tup, UART_MSR);
684 if (!(msr & UART_MSR_ANY_DELTA))
687 if (msr & UART_MSR_TERI)
688 tup->uport.icount.rng++;
689 if (msr & UART_MSR_DDSR)
690 tup->uport.icount.dsr++;
691 /* We may only get DDCD when HW init and reset */
692 if (msr & UART_MSR_DDCD)
693 uart_handle_dcd_change(&tup->uport, msr & UART_MSR_DCD);
694 /* Will start/stop_tx accordingly */
695 if (msr & UART_MSR_DCTS)
696 uart_handle_cts_change(&tup->uport, msr & UART_MSR_CTS);
699 static irqreturn_t tegra_uart_isr(int irq, void *data)
701 struct tegra_uart_port *tup = data;
702 struct uart_port *u = &tup->uport;
705 bool is_rx_int = false;
708 spin_lock_irqsave(&u->lock, flags);
710 iir = tegra_uart_read(tup, UART_IIR);
711 if (iir & UART_IIR_NO_INT) {
713 tegra_uart_handle_rx_dma(tup);
714 if (tup->rx_in_progress) {
715 ier = tup->ier_shadow;
716 ier |= (UART_IER_RLSI | UART_IER_RTOIE |
717 TEGRA_UART_IER_EORD);
718 tup->ier_shadow = ier;
719 tegra_uart_write(tup, ier, UART_IER);
722 spin_unlock_irqrestore(&u->lock, flags);
726 switch ((iir >> 1) & 0x7) {
727 case 0: /* Modem signal change interrupt */
728 tegra_uart_handle_modem_signal_change(u);
731 case 1: /* Transmit interrupt only triggered when using PIO */
732 tup->ier_shadow &= ~UART_IER_THRI;
733 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
734 tegra_uart_handle_tx_pio(tup);
737 case 4: /* End of data */
738 case 6: /* Rx timeout */
739 case 2: /* Receive */
742 /* Disable Rx interrupts */
743 ier = tup->ier_shadow;
745 tegra_uart_write(tup, ier, UART_IER);
746 ier &= ~(UART_IER_RDI | UART_IER_RLSI |
747 UART_IER_RTOIE | TEGRA_UART_IER_EORD);
748 tup->ier_shadow = ier;
749 tegra_uart_write(tup, ier, UART_IER);
753 case 3: /* Receive error */
754 tegra_uart_decode_rx_error(tup,
755 tegra_uart_read(tup, UART_LSR));
758 case 5: /* break nothing to handle */
759 case 7: /* break nothing to handle */
765 static void tegra_uart_stop_rx(struct uart_port *u)
767 struct tegra_uart_port *tup = to_tegra_uport(u);
768 struct dma_tx_state state;
774 if (!tup->rx_in_progress)
777 tegra_uart_wait_sym_time(tup, 1); /* wait one character interval */
779 ier = tup->ier_shadow;
780 ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE |
781 TEGRA_UART_IER_EORD);
782 tup->ier_shadow = ier;
783 tegra_uart_write(tup, ier, UART_IER);
784 tup->rx_in_progress = 0;
785 dmaengine_terminate_all(tup->rx_dma_chan);
786 dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
787 tegra_uart_rx_buffer_push(tup, state.residue);
790 static void tegra_uart_hw_deinit(struct tegra_uart_port *tup)
793 unsigned long char_time = DIV_ROUND_UP(10000000, tup->current_baud);
794 unsigned long fifo_empty_time = tup->uport.fifosize * char_time;
795 unsigned long wait_time;
800 /* Disable interrupts */
801 tegra_uart_write(tup, 0, UART_IER);
803 lsr = tegra_uart_read(tup, UART_LSR);
804 if ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
805 msr = tegra_uart_read(tup, UART_MSR);
806 mcr = tegra_uart_read(tup, UART_MCR);
807 if ((mcr & TEGRA_UART_MCR_CTS_EN) && (msr & UART_MSR_CTS))
808 dev_err(tup->uport.dev,
809 "Tx Fifo not empty, CTS disabled, waiting\n");
811 /* Wait for Tx fifo to be empty */
812 while ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
813 wait_time = min(fifo_empty_time, 100lu);
815 fifo_empty_time -= wait_time;
816 if (!fifo_empty_time) {
817 msr = tegra_uart_read(tup, UART_MSR);
818 mcr = tegra_uart_read(tup, UART_MCR);
819 if ((mcr & TEGRA_UART_MCR_CTS_EN) &&
820 (msr & UART_MSR_CTS))
821 dev_err(tup->uport.dev,
822 "Slave not ready\n");
825 lsr = tegra_uart_read(tup, UART_LSR);
829 spin_lock_irqsave(&tup->uport.lock, flags);
830 /* Reset the Rx and Tx FIFOs */
831 tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_XMIT | UART_FCR_CLEAR_RCVR);
832 tup->current_baud = 0;
833 spin_unlock_irqrestore(&tup->uport.lock, flags);
835 clk_disable_unprepare(tup->uart_clk);
838 static int tegra_uart_hw_init(struct tegra_uart_port *tup)
846 tup->current_baud = 0;
848 clk_prepare_enable(tup->uart_clk);
850 /* Reset the UART controller to clear all previous status.*/
851 reset_control_assert(tup->rst);
853 reset_control_deassert(tup->rst);
855 tup->rx_in_progress = 0;
856 tup->tx_in_progress = 0;
859 * Set the trigger level
863 * For receive, this will interrupt the CPU after that many number of
864 * bytes are received, for the remaining bytes the receive timeout
865 * interrupt is received. Rx high watermark is set to 4.
867 * For transmit, if the trasnmit interrupt is enabled, this will
868 * interrupt the CPU when the number of entries in the FIFO reaches the
869 * low watermark. Tx low watermark is set to 16 bytes.
873 * Set the Tx trigger to 16. This should match the DMA burst size that
874 * programmed in the DMA registers.
876 tup->fcr_shadow = UART_FCR_ENABLE_FIFO;
877 tup->fcr_shadow |= UART_FCR_R_TRIG_01;
878 tup->fcr_shadow |= TEGRA_UART_TX_TRIG_16B;
879 tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
881 /* Dummy read to ensure the write is posted */
882 tegra_uart_read(tup, UART_SCR);
885 * For all tegra devices (up to t210), there is a hardware issue that
886 * requires software to wait for 3 UART clock periods after enabling
887 * the TX fifo, otherwise data could be lost.
889 tegra_uart_wait_cycle_time(tup, 3);
892 * Initialize the UART with default configuration
893 * (115200, N, 8, 1) so that the receive DMA buffer may be
896 tup->lcr_shadow = TEGRA_UART_DEFAULT_LSR;
897 tegra_set_baudrate(tup, TEGRA_UART_DEFAULT_BAUD);
898 tup->fcr_shadow |= UART_FCR_DMA_SELECT;
899 tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
901 ret = tegra_uart_start_rx_dma(tup);
903 dev_err(tup->uport.dev, "Not able to start Rx DMA\n");
906 tup->rx_in_progress = 1;
909 * Enable IE_RXS for the receive status interrupts like line errros.
910 * Enable IE_RX_TIMEOUT to get the bytes which cannot be DMA'd.
912 * If using DMA mode, enable EORD instead of receive interrupt which
913 * will interrupt after the UART is done with the receive instead of
914 * the interrupt when the FIFO "threshold" is reached.
916 * EORD is different interrupt than RX_TIMEOUT - RX_TIMEOUT occurs when
917 * the DATA is sitting in the FIFO and couldn't be transferred to the
918 * DMA as the DMA size alignment (4 bytes) is not met. EORD will be
919 * triggered when there is a pause of the incomming data stream for 4
922 * For pauses in the data which is not aligned to 4 bytes, we get
923 * both the EORD as well as RX_TIMEOUT - SW sees RX_TIMEOUT first
926 tup->ier_shadow = UART_IER_RLSI | UART_IER_RTOIE | TEGRA_UART_IER_EORD;
927 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
931 static void tegra_uart_dma_channel_free(struct tegra_uart_port *tup,
935 dmaengine_terminate_all(tup->rx_dma_chan);
936 dma_release_channel(tup->rx_dma_chan);
937 dma_free_coherent(tup->uport.dev, TEGRA_UART_RX_DMA_BUFFER_SIZE,
938 tup->rx_dma_buf_virt, tup->rx_dma_buf_phys);
939 tup->rx_dma_chan = NULL;
940 tup->rx_dma_buf_phys = 0;
941 tup->rx_dma_buf_virt = NULL;
943 dmaengine_terminate_all(tup->tx_dma_chan);
944 dma_release_channel(tup->tx_dma_chan);
945 dma_unmap_single(tup->uport.dev, tup->tx_dma_buf_phys,
946 UART_XMIT_SIZE, DMA_TO_DEVICE);
947 tup->tx_dma_chan = NULL;
948 tup->tx_dma_buf_phys = 0;
949 tup->tx_dma_buf_virt = NULL;
953 static int tegra_uart_dma_channel_allocate(struct tegra_uart_port *tup,
956 struct dma_chan *dma_chan;
957 unsigned char *dma_buf;
960 struct dma_slave_config dma_sconfig;
962 dma_chan = dma_request_slave_channel_reason(tup->uport.dev,
963 dma_to_memory ? "rx" : "tx");
964 if (IS_ERR(dma_chan)) {
965 ret = PTR_ERR(dma_chan);
966 dev_err(tup->uport.dev,
967 "DMA channel alloc failed: %d\n", ret);
972 dma_buf = dma_alloc_coherent(tup->uport.dev,
973 TEGRA_UART_RX_DMA_BUFFER_SIZE,
974 &dma_phys, GFP_KERNEL);
976 dev_err(tup->uport.dev,
977 "Not able to allocate the dma buffer\n");
978 dma_release_channel(dma_chan);
981 dma_sconfig.src_addr = tup->uport.mapbase;
982 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
983 dma_sconfig.src_maxburst = 4;
984 tup->rx_dma_chan = dma_chan;
985 tup->rx_dma_buf_virt = dma_buf;
986 tup->rx_dma_buf_phys = dma_phys;
988 dma_phys = dma_map_single(tup->uport.dev,
989 tup->uport.state->xmit.buf, UART_XMIT_SIZE,
991 if (dma_mapping_error(tup->uport.dev, dma_phys)) {
992 dev_err(tup->uport.dev, "dma_map_single tx failed\n");
993 dma_release_channel(dma_chan);
996 dma_buf = tup->uport.state->xmit.buf;
997 dma_sconfig.dst_addr = tup->uport.mapbase;
998 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
999 dma_sconfig.dst_maxburst = 16;
1000 tup->tx_dma_chan = dma_chan;
1001 tup->tx_dma_buf_virt = dma_buf;
1002 tup->tx_dma_buf_phys = dma_phys;
1005 ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
1007 dev_err(tup->uport.dev,
1008 "Dma slave config failed, err = %d\n", ret);
1009 tegra_uart_dma_channel_free(tup, dma_to_memory);
1016 static int tegra_uart_startup(struct uart_port *u)
1018 struct tegra_uart_port *tup = to_tegra_uport(u);
1021 ret = tegra_uart_dma_channel_allocate(tup, false);
1023 dev_err(u->dev, "Tx Dma allocation failed, err = %d\n", ret);
1027 ret = tegra_uart_dma_channel_allocate(tup, true);
1029 dev_err(u->dev, "Rx Dma allocation failed, err = %d\n", ret);
1033 ret = tegra_uart_hw_init(tup);
1035 dev_err(u->dev, "Uart HW init failed, err = %d\n", ret);
1039 ret = request_irq(u->irq, tegra_uart_isr, 0,
1040 dev_name(u->dev), tup);
1042 dev_err(u->dev, "Failed to register ISR for IRQ %d\n", u->irq);
1048 tegra_uart_dma_channel_free(tup, true);
1050 tegra_uart_dma_channel_free(tup, false);
1055 * Flush any TX data submitted for DMA and PIO. Called when the
1056 * TX circular buffer is reset.
1058 static void tegra_uart_flush_buffer(struct uart_port *u)
1060 struct tegra_uart_port *tup = to_tegra_uport(u);
1063 if (tup->tx_dma_chan)
1064 dmaengine_terminate_all(tup->tx_dma_chan);
1067 static void tegra_uart_shutdown(struct uart_port *u)
1069 struct tegra_uart_port *tup = to_tegra_uport(u);
1071 tegra_uart_hw_deinit(tup);
1073 tup->rx_in_progress = 0;
1074 tup->tx_in_progress = 0;
1076 tegra_uart_dma_channel_free(tup, true);
1077 tegra_uart_dma_channel_free(tup, false);
1078 free_irq(u->irq, tup);
1081 static void tegra_uart_enable_ms(struct uart_port *u)
1083 struct tegra_uart_port *tup = to_tegra_uport(u);
1085 if (tup->enable_modem_interrupt) {
1086 tup->ier_shadow |= UART_IER_MSI;
1087 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1091 static void tegra_uart_set_termios(struct uart_port *u,
1092 struct ktermios *termios, struct ktermios *oldtermios)
1094 struct tegra_uart_port *tup = to_tegra_uport(u);
1096 unsigned long flags;
1099 struct clk *parent_clk = clk_get_parent(tup->uart_clk);
1100 unsigned long parent_clk_rate = clk_get_rate(parent_clk);
1101 int max_divider = (tup->cdata->support_clk_src_div) ? 0x7FFF : 0xFFFF;
1104 spin_lock_irqsave(&u->lock, flags);
1106 /* Changing configuration, it is safe to stop any rx now */
1107 if (tup->rts_active)
1108 set_rts(tup, false);
1110 /* Clear all interrupts as configuration is going to be changed */
1111 tegra_uart_write(tup, tup->ier_shadow | UART_IER_RDI, UART_IER);
1112 tegra_uart_read(tup, UART_IER);
1113 tegra_uart_write(tup, 0, UART_IER);
1114 tegra_uart_read(tup, UART_IER);
1117 lcr = tup->lcr_shadow;
1118 lcr &= ~UART_LCR_PARITY;
1120 /* CMSPAR isn't supported by this driver */
1121 termios->c_cflag &= ~CMSPAR;
1123 if ((termios->c_cflag & PARENB) == PARENB) {
1125 if (termios->c_cflag & PARODD) {
1126 lcr |= UART_LCR_PARITY;
1127 lcr &= ~UART_LCR_EPAR;
1128 lcr &= ~UART_LCR_SPAR;
1130 lcr |= UART_LCR_PARITY;
1131 lcr |= UART_LCR_EPAR;
1132 lcr &= ~UART_LCR_SPAR;
1136 lcr &= ~UART_LCR_WLEN8;
1137 switch (termios->c_cflag & CSIZE) {
1139 lcr |= UART_LCR_WLEN5;
1143 lcr |= UART_LCR_WLEN6;
1147 lcr |= UART_LCR_WLEN7;
1151 lcr |= UART_LCR_WLEN8;
1157 if (termios->c_cflag & CSTOPB) {
1158 lcr |= UART_LCR_STOP;
1161 lcr &= ~UART_LCR_STOP;
1165 tegra_uart_write(tup, lcr, UART_LCR);
1166 tup->lcr_shadow = lcr;
1167 tup->symb_bit = symb_bit;
1170 baud = uart_get_baud_rate(u, termios, oldtermios,
1171 parent_clk_rate/max_divider,
1172 parent_clk_rate/16);
1173 spin_unlock_irqrestore(&u->lock, flags);
1174 tegra_set_baudrate(tup, baud);
1175 if (tty_termios_baud_rate(termios))
1176 tty_termios_encode_baud_rate(termios, baud, baud);
1177 spin_lock_irqsave(&u->lock, flags);
1180 if (termios->c_cflag & CRTSCTS) {
1181 tup->mcr_shadow |= TEGRA_UART_MCR_CTS_EN;
1182 tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
1183 tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
1184 /* if top layer has asked to set rts active then do so here */
1185 if (tup->rts_active)
1188 tup->mcr_shadow &= ~TEGRA_UART_MCR_CTS_EN;
1189 tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
1190 tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
1193 /* update the port timeout based on new settings */
1194 uart_update_timeout(u, termios->c_cflag, baud);
1196 /* Make sure all writes have completed */
1197 tegra_uart_read(tup, UART_IER);
1199 /* Re-enable interrupt */
1200 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1201 tegra_uart_read(tup, UART_IER);
1203 tup->uport.ignore_status_mask = 0;
1204 /* Ignore all characters if CREAD is not set */
1205 if ((termios->c_cflag & CREAD) == 0)
1206 tup->uport.ignore_status_mask |= UART_LSR_DR;
1208 spin_unlock_irqrestore(&u->lock, flags);
1211 static const char *tegra_uart_type(struct uart_port *u)
1213 return TEGRA_UART_TYPE;
1216 static const struct uart_ops tegra_uart_ops = {
1217 .tx_empty = tegra_uart_tx_empty,
1218 .set_mctrl = tegra_uart_set_mctrl,
1219 .get_mctrl = tegra_uart_get_mctrl,
1220 .stop_tx = tegra_uart_stop_tx,
1221 .start_tx = tegra_uart_start_tx,
1222 .stop_rx = tegra_uart_stop_rx,
1223 .flush_buffer = tegra_uart_flush_buffer,
1224 .enable_ms = tegra_uart_enable_ms,
1225 .break_ctl = tegra_uart_break_ctl,
1226 .startup = tegra_uart_startup,
1227 .shutdown = tegra_uart_shutdown,
1228 .set_termios = tegra_uart_set_termios,
1229 .type = tegra_uart_type,
1230 .request_port = tegra_uart_request_port,
1231 .release_port = tegra_uart_release_port,
1234 static struct uart_driver tegra_uart_driver = {
1235 .owner = THIS_MODULE,
1236 .driver_name = "tegra_hsuart",
1237 .dev_name = "ttyTHS",
1239 .nr = TEGRA_UART_MAXIMUM,
1242 static int tegra_uart_parse_dt(struct platform_device *pdev,
1243 struct tegra_uart_port *tup)
1245 struct device_node *np = pdev->dev.of_node;
1248 port = of_alias_get_id(np, "serial");
1250 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", port);
1253 tup->uport.line = port;
1255 tup->enable_modem_interrupt = of_property_read_bool(np,
1256 "nvidia,enable-modem-interrupt");
1260 static struct tegra_uart_chip_data tegra20_uart_chip_data = {
1261 .tx_fifo_full_status = false,
1262 .allow_txfifo_reset_fifo_mode = true,
1263 .support_clk_src_div = false,
1266 static struct tegra_uart_chip_data tegra30_uart_chip_data = {
1267 .tx_fifo_full_status = true,
1268 .allow_txfifo_reset_fifo_mode = false,
1269 .support_clk_src_div = true,
1272 static const struct of_device_id tegra_uart_of_match[] = {
1274 .compatible = "nvidia,tegra30-hsuart",
1275 .data = &tegra30_uart_chip_data,
1277 .compatible = "nvidia,tegra20-hsuart",
1278 .data = &tegra20_uart_chip_data,
1282 MODULE_DEVICE_TABLE(of, tegra_uart_of_match);
1284 static int tegra_uart_probe(struct platform_device *pdev)
1286 struct tegra_uart_port *tup;
1287 struct uart_port *u;
1288 struct resource *resource;
1290 const struct tegra_uart_chip_data *cdata;
1291 const struct of_device_id *match;
1293 match = of_match_device(tegra_uart_of_match, &pdev->dev);
1295 dev_err(&pdev->dev, "Error: No device match found\n");
1298 cdata = match->data;
1300 tup = devm_kzalloc(&pdev->dev, sizeof(*tup), GFP_KERNEL);
1302 dev_err(&pdev->dev, "Failed to allocate memory for tup\n");
1306 ret = tegra_uart_parse_dt(pdev, tup);
1311 u->dev = &pdev->dev;
1312 u->ops = &tegra_uart_ops;
1313 u->type = PORT_TEGRA;
1317 platform_set_drvdata(pdev, tup);
1318 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1320 dev_err(&pdev->dev, "No IO memory resource\n");
1324 u->mapbase = resource->start;
1325 u->membase = devm_ioremap_resource(&pdev->dev, resource);
1326 if (IS_ERR(u->membase))
1327 return PTR_ERR(u->membase);
1329 tup->uart_clk = devm_clk_get(&pdev->dev, NULL);
1330 if (IS_ERR(tup->uart_clk)) {
1331 dev_err(&pdev->dev, "Couldn't get the clock\n");
1332 return PTR_ERR(tup->uart_clk);
1335 tup->rst = devm_reset_control_get_exclusive(&pdev->dev, "serial");
1336 if (IS_ERR(tup->rst)) {
1337 dev_err(&pdev->dev, "Couldn't get the reset\n");
1338 return PTR_ERR(tup->rst);
1341 u->iotype = UPIO_MEM32;
1342 ret = platform_get_irq(pdev, 0);
1347 ret = uart_add_one_port(&tegra_uart_driver, u);
1349 dev_err(&pdev->dev, "Failed to add uart port, err %d\n", ret);
1355 static int tegra_uart_remove(struct platform_device *pdev)
1357 struct tegra_uart_port *tup = platform_get_drvdata(pdev);
1358 struct uart_port *u = &tup->uport;
1360 uart_remove_one_port(&tegra_uart_driver, u);
1364 #ifdef CONFIG_PM_SLEEP
1365 static int tegra_uart_suspend(struct device *dev)
1367 struct tegra_uart_port *tup = dev_get_drvdata(dev);
1368 struct uart_port *u = &tup->uport;
1370 return uart_suspend_port(&tegra_uart_driver, u);
1373 static int tegra_uart_resume(struct device *dev)
1375 struct tegra_uart_port *tup = dev_get_drvdata(dev);
1376 struct uart_port *u = &tup->uport;
1378 return uart_resume_port(&tegra_uart_driver, u);
1382 static const struct dev_pm_ops tegra_uart_pm_ops = {
1383 SET_SYSTEM_SLEEP_PM_OPS(tegra_uart_suspend, tegra_uart_resume)
1386 static struct platform_driver tegra_uart_platform_driver = {
1387 .probe = tegra_uart_probe,
1388 .remove = tegra_uart_remove,
1390 .name = "serial-tegra",
1391 .of_match_table = tegra_uart_of_match,
1392 .pm = &tegra_uart_pm_ops,
1396 static int __init tegra_uart_init(void)
1400 ret = uart_register_driver(&tegra_uart_driver);
1402 pr_err("Could not register %s driver\n",
1403 tegra_uart_driver.driver_name);
1407 ret = platform_driver_register(&tegra_uart_platform_driver);
1409 pr_err("Uart platform driver register failed, e = %d\n", ret);
1410 uart_unregister_driver(&tegra_uart_driver);
1416 static void __exit tegra_uart_exit(void)
1418 pr_info("Unloading tegra uart driver\n");
1419 platform_driver_unregister(&tegra_uart_platform_driver);
1420 uart_unregister_driver(&tegra_uart_driver);
1423 module_init(tegra_uart_init);
1424 module_exit(tegra_uart_exit);
1426 MODULE_ALIAS("platform:serial-tegra");
1427 MODULE_DESCRIPTION("High speed UART driver for tegra chipset");
1428 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1429 MODULE_LICENSE("GPL v2");