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;
270 if (tup->cdata->allow_txfifo_reset_fifo_mode) {
271 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
272 tegra_uart_write(tup, fcr, UART_FCR);
274 fcr &= ~UART_FCR_ENABLE_FIFO;
275 tegra_uart_write(tup, fcr, UART_FCR);
277 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
278 tegra_uart_write(tup, fcr, UART_FCR);
279 fcr |= UART_FCR_ENABLE_FIFO;
280 tegra_uart_write(tup, fcr, UART_FCR);
283 /* Dummy read to ensure the write is posted */
284 tegra_uart_read(tup, UART_SCR);
287 * For all tegra devices (up to t210), there is a hardware issue that
288 * requires software to wait for 32 UART clock periods for the flush
289 * to propagate, otherwise data could be lost.
291 tegra_uart_wait_cycle_time(tup, 32);
294 static int tegra_set_baudrate(struct tegra_uart_port *tup, unsigned int baud)
297 unsigned int divisor;
302 if (tup->current_baud == baud)
305 if (tup->cdata->support_clk_src_div) {
307 ret = clk_set_rate(tup->uart_clk, rate);
309 dev_err(tup->uport.dev,
310 "clk_set_rate() failed for rate %lu\n", rate);
315 rate = clk_get_rate(tup->uart_clk);
316 divisor = DIV_ROUND_CLOSEST(rate, baud * 16);
319 spin_lock_irqsave(&tup->uport.lock, flags);
320 lcr = tup->lcr_shadow;
321 lcr |= UART_LCR_DLAB;
322 tegra_uart_write(tup, lcr, UART_LCR);
324 tegra_uart_write(tup, divisor & 0xFF, UART_TX);
325 tegra_uart_write(tup, ((divisor >> 8) & 0xFF), UART_IER);
327 lcr &= ~UART_LCR_DLAB;
328 tegra_uart_write(tup, lcr, UART_LCR);
330 /* Dummy read to ensure the write is posted */
331 tegra_uart_read(tup, UART_SCR);
332 spin_unlock_irqrestore(&tup->uport.lock, flags);
334 tup->current_baud = baud;
336 /* wait two character intervals at new rate */
337 tegra_uart_wait_sym_time(tup, 2);
341 static char tegra_uart_decode_rx_error(struct tegra_uart_port *tup,
344 char flag = TTY_NORMAL;
346 if (unlikely(lsr & TEGRA_UART_LSR_ANY)) {
347 if (lsr & UART_LSR_OE) {
350 tup->uport.icount.overrun++;
351 dev_err(tup->uport.dev, "Got overrun errors\n");
352 } else if (lsr & UART_LSR_PE) {
355 tup->uport.icount.parity++;
356 dev_err(tup->uport.dev, "Got Parity errors\n");
357 } else if (lsr & UART_LSR_FE) {
359 tup->uport.icount.frame++;
360 dev_err(tup->uport.dev, "Got frame errors\n");
361 } else if (lsr & UART_LSR_BI) {
362 dev_err(tup->uport.dev, "Got Break\n");
363 tup->uport.icount.brk++;
364 /* If FIFO read error without any data, reset Rx FIFO */
365 if (!(lsr & UART_LSR_DR) && (lsr & UART_LSR_FIFOE))
366 tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_RCVR);
372 static int tegra_uart_request_port(struct uart_port *u)
377 static void tegra_uart_release_port(struct uart_port *u)
379 /* Nothing to do here */
382 static void tegra_uart_fill_tx_fifo(struct tegra_uart_port *tup, int max_bytes)
384 struct circ_buf *xmit = &tup->uport.state->xmit;
387 for (i = 0; i < max_bytes; i++) {
388 BUG_ON(uart_circ_empty(xmit));
389 if (tup->cdata->tx_fifo_full_status) {
390 unsigned long lsr = tegra_uart_read(tup, UART_LSR);
391 if ((lsr & TEGRA_UART_LSR_TXFIFO_FULL))
394 tegra_uart_write(tup, xmit->buf[xmit->tail], UART_TX);
395 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
396 tup->uport.icount.tx++;
400 static void tegra_uart_start_pio_tx(struct tegra_uart_port *tup,
403 if (bytes > TEGRA_UART_MIN_DMA)
404 bytes = TEGRA_UART_MIN_DMA;
406 tup->tx_in_progress = TEGRA_UART_TX_PIO;
407 tup->tx_bytes = bytes;
408 tup->ier_shadow |= UART_IER_THRI;
409 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
412 static void tegra_uart_tx_dma_complete(void *args)
414 struct tegra_uart_port *tup = args;
415 struct circ_buf *xmit = &tup->uport.state->xmit;
416 struct dma_tx_state state;
420 dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
421 count = tup->tx_bytes_requested - state.residue;
422 async_tx_ack(tup->tx_dma_desc);
423 spin_lock_irqsave(&tup->uport.lock, flags);
424 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
425 tup->tx_in_progress = 0;
426 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
427 uart_write_wakeup(&tup->uport);
428 tegra_uart_start_next_tx(tup);
429 spin_unlock_irqrestore(&tup->uport.lock, flags);
432 static int tegra_uart_start_tx_dma(struct tegra_uart_port *tup,
435 struct circ_buf *xmit = &tup->uport.state->xmit;
436 dma_addr_t tx_phys_addr;
438 dma_sync_single_for_device(tup->uport.dev, tup->tx_dma_buf_phys,
439 UART_XMIT_SIZE, DMA_TO_DEVICE);
441 tup->tx_bytes = count & ~(0xF);
442 tx_phys_addr = tup->tx_dma_buf_phys + xmit->tail;
443 tup->tx_dma_desc = dmaengine_prep_slave_single(tup->tx_dma_chan,
444 tx_phys_addr, tup->tx_bytes, DMA_MEM_TO_DEV,
446 if (!tup->tx_dma_desc) {
447 dev_err(tup->uport.dev, "Not able to get desc for Tx\n");
451 tup->tx_dma_desc->callback = tegra_uart_tx_dma_complete;
452 tup->tx_dma_desc->callback_param = tup;
453 tup->tx_in_progress = TEGRA_UART_TX_DMA;
454 tup->tx_bytes_requested = tup->tx_bytes;
455 tup->tx_cookie = dmaengine_submit(tup->tx_dma_desc);
456 dma_async_issue_pending(tup->tx_dma_chan);
460 static void tegra_uart_start_next_tx(struct tegra_uart_port *tup)
464 struct circ_buf *xmit = &tup->uport.state->xmit;
466 if (!tup->current_baud)
469 tail = (unsigned long)&xmit->buf[xmit->tail];
470 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
474 if (count < TEGRA_UART_MIN_DMA)
475 tegra_uart_start_pio_tx(tup, count);
476 else if (BYTES_TO_ALIGN(tail) > 0)
477 tegra_uart_start_pio_tx(tup, BYTES_TO_ALIGN(tail));
479 tegra_uart_start_tx_dma(tup, count);
482 /* Called by serial core driver with u->lock taken. */
483 static void tegra_uart_start_tx(struct uart_port *u)
485 struct tegra_uart_port *tup = to_tegra_uport(u);
486 struct circ_buf *xmit = &u->state->xmit;
488 if (!uart_circ_empty(xmit) && !tup->tx_in_progress)
489 tegra_uart_start_next_tx(tup);
492 static unsigned int tegra_uart_tx_empty(struct uart_port *u)
494 struct tegra_uart_port *tup = to_tegra_uport(u);
495 unsigned int ret = 0;
498 spin_lock_irqsave(&u->lock, flags);
499 if (!tup->tx_in_progress) {
500 unsigned long lsr = tegra_uart_read(tup, UART_LSR);
501 if ((lsr & TX_EMPTY_STATUS) == TX_EMPTY_STATUS)
504 spin_unlock_irqrestore(&u->lock, flags);
508 static void tegra_uart_stop_tx(struct uart_port *u)
510 struct tegra_uart_port *tup = to_tegra_uport(u);
511 struct circ_buf *xmit = &tup->uport.state->xmit;
512 struct dma_tx_state state;
515 if (tup->tx_in_progress != TEGRA_UART_TX_DMA)
518 dmaengine_terminate_all(tup->tx_dma_chan);
519 dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
520 count = tup->tx_bytes_requested - state.residue;
521 async_tx_ack(tup->tx_dma_desc);
522 xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
523 tup->tx_in_progress = 0;
526 static void tegra_uart_handle_tx_pio(struct tegra_uart_port *tup)
528 struct circ_buf *xmit = &tup->uport.state->xmit;
530 tegra_uart_fill_tx_fifo(tup, tup->tx_bytes);
531 tup->tx_in_progress = 0;
532 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
533 uart_write_wakeup(&tup->uport);
534 tegra_uart_start_next_tx(tup);
537 static void tegra_uart_handle_rx_pio(struct tegra_uart_port *tup,
538 struct tty_port *tty)
541 char flag = TTY_NORMAL;
542 unsigned long lsr = 0;
545 lsr = tegra_uart_read(tup, UART_LSR);
546 if (!(lsr & UART_LSR_DR))
549 flag = tegra_uart_decode_rx_error(tup, lsr);
550 ch = (unsigned char) tegra_uart_read(tup, UART_RX);
551 tup->uport.icount.rx++;
553 if (!uart_handle_sysrq_char(&tup->uport, ch) && tty)
554 tty_insert_flip_char(tty, ch, flag);
556 if (tup->uport.ignore_status_mask & UART_LSR_DR)
561 static void tegra_uart_copy_rx_to_tty(struct tegra_uart_port *tup,
562 struct tty_port *tty,
567 /* If count is zero, then there is no data to be copied */
571 tup->uport.icount.rx += count;
573 dev_err(tup->uport.dev, "No tty port\n");
577 if (tup->uport.ignore_status_mask & UART_LSR_DR)
580 dma_sync_single_for_cpu(tup->uport.dev, tup->rx_dma_buf_phys,
581 TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
582 copied = tty_insert_flip_string(tty,
583 ((unsigned char *)(tup->rx_dma_buf_virt)), count);
584 if (copied != count) {
586 dev_err(tup->uport.dev, "RxData copy to tty layer failed\n");
588 dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
589 TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_TO_DEVICE);
592 static void tegra_uart_rx_buffer_push(struct tegra_uart_port *tup,
593 unsigned int residue)
595 struct tty_port *port = &tup->uport.state->port;
596 struct tty_struct *tty = tty_port_tty_get(port);
599 async_tx_ack(tup->rx_dma_desc);
600 count = tup->rx_bytes_requested - residue;
602 /* If we are here, DMA is stopped */
603 tegra_uart_copy_rx_to_tty(tup, port, count);
605 tegra_uart_handle_rx_pio(tup, port);
607 tty_flip_buffer_push(port);
612 static void tegra_uart_rx_dma_complete(void *args)
614 struct tegra_uart_port *tup = args;
615 struct uart_port *u = &tup->uport;
617 struct dma_tx_state state;
618 enum dma_status status;
620 spin_lock_irqsave(&u->lock, flags);
622 status = dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
624 if (status == DMA_IN_PROGRESS) {
625 dev_dbg(tup->uport.dev, "RX DMA is in progress\n");
629 /* Deactivate flow control to stop sender */
633 tegra_uart_rx_buffer_push(tup, 0);
634 tegra_uart_start_rx_dma(tup);
636 /* Activate flow control to start transfer */
641 spin_unlock_irqrestore(&u->lock, flags);
644 static void tegra_uart_handle_rx_dma(struct tegra_uart_port *tup)
646 struct dma_tx_state state;
648 /* Deactivate flow control to stop sender */
652 dmaengine_terminate_all(tup->rx_dma_chan);
653 dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
654 tegra_uart_rx_buffer_push(tup, state.residue);
655 tegra_uart_start_rx_dma(tup);
661 static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup)
663 unsigned int count = TEGRA_UART_RX_DMA_BUFFER_SIZE;
665 tup->rx_dma_desc = dmaengine_prep_slave_single(tup->rx_dma_chan,
666 tup->rx_dma_buf_phys, count, DMA_DEV_TO_MEM,
668 if (!tup->rx_dma_desc) {
669 dev_err(tup->uport.dev, "Not able to get desc for Rx\n");
673 tup->rx_dma_desc->callback = tegra_uart_rx_dma_complete;
674 tup->rx_dma_desc->callback_param = tup;
675 dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
676 count, DMA_TO_DEVICE);
677 tup->rx_bytes_requested = count;
678 tup->rx_cookie = dmaengine_submit(tup->rx_dma_desc);
679 dma_async_issue_pending(tup->rx_dma_chan);
683 static void tegra_uart_handle_modem_signal_change(struct uart_port *u)
685 struct tegra_uart_port *tup = to_tegra_uport(u);
688 msr = tegra_uart_read(tup, UART_MSR);
689 if (!(msr & UART_MSR_ANY_DELTA))
692 if (msr & UART_MSR_TERI)
693 tup->uport.icount.rng++;
694 if (msr & UART_MSR_DDSR)
695 tup->uport.icount.dsr++;
696 /* We may only get DDCD when HW init and reset */
697 if (msr & UART_MSR_DDCD)
698 uart_handle_dcd_change(&tup->uport, msr & UART_MSR_DCD);
699 /* Will start/stop_tx accordingly */
700 if (msr & UART_MSR_DCTS)
701 uart_handle_cts_change(&tup->uport, msr & UART_MSR_CTS);
704 static irqreturn_t tegra_uart_isr(int irq, void *data)
706 struct tegra_uart_port *tup = data;
707 struct uart_port *u = &tup->uport;
710 bool is_rx_int = false;
713 spin_lock_irqsave(&u->lock, flags);
715 iir = tegra_uart_read(tup, UART_IIR);
716 if (iir & UART_IIR_NO_INT) {
718 tegra_uart_handle_rx_dma(tup);
719 if (tup->rx_in_progress) {
720 ier = tup->ier_shadow;
721 ier |= (UART_IER_RLSI | UART_IER_RTOIE |
722 TEGRA_UART_IER_EORD);
723 tup->ier_shadow = ier;
724 tegra_uart_write(tup, ier, UART_IER);
727 spin_unlock_irqrestore(&u->lock, flags);
731 switch ((iir >> 1) & 0x7) {
732 case 0: /* Modem signal change interrupt */
733 tegra_uart_handle_modem_signal_change(u);
736 case 1: /* Transmit interrupt only triggered when using PIO */
737 tup->ier_shadow &= ~UART_IER_THRI;
738 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
739 tegra_uart_handle_tx_pio(tup);
742 case 4: /* End of data */
743 case 6: /* Rx timeout */
744 case 2: /* Receive */
747 /* Disable Rx interrupts */
748 ier = tup->ier_shadow;
750 tegra_uart_write(tup, ier, UART_IER);
751 ier &= ~(UART_IER_RDI | UART_IER_RLSI |
752 UART_IER_RTOIE | TEGRA_UART_IER_EORD);
753 tup->ier_shadow = ier;
754 tegra_uart_write(tup, ier, UART_IER);
758 case 3: /* Receive error */
759 tegra_uart_decode_rx_error(tup,
760 tegra_uart_read(tup, UART_LSR));
763 case 5: /* break nothing to handle */
764 case 7: /* break nothing to handle */
770 static void tegra_uart_stop_rx(struct uart_port *u)
772 struct tegra_uart_port *tup = to_tegra_uport(u);
773 struct dma_tx_state state;
779 if (!tup->rx_in_progress)
782 tegra_uart_wait_sym_time(tup, 1); /* wait one character interval */
784 ier = tup->ier_shadow;
785 ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE |
786 TEGRA_UART_IER_EORD);
787 tup->ier_shadow = ier;
788 tegra_uart_write(tup, ier, UART_IER);
789 tup->rx_in_progress = 0;
790 dmaengine_terminate_all(tup->rx_dma_chan);
791 dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
792 tegra_uart_rx_buffer_push(tup, state.residue);
795 static void tegra_uart_hw_deinit(struct tegra_uart_port *tup)
798 unsigned long char_time = DIV_ROUND_UP(10000000, tup->current_baud);
799 unsigned long fifo_empty_time = tup->uport.fifosize * char_time;
800 unsigned long wait_time;
805 /* Disable interrupts */
806 tegra_uart_write(tup, 0, UART_IER);
808 lsr = tegra_uart_read(tup, UART_LSR);
809 if ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
810 msr = tegra_uart_read(tup, UART_MSR);
811 mcr = tegra_uart_read(tup, UART_MCR);
812 if ((mcr & TEGRA_UART_MCR_CTS_EN) && (msr & UART_MSR_CTS))
813 dev_err(tup->uport.dev,
814 "Tx Fifo not empty, CTS disabled, waiting\n");
816 /* Wait for Tx fifo to be empty */
817 while ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
818 wait_time = min(fifo_empty_time, 100lu);
820 fifo_empty_time -= wait_time;
821 if (!fifo_empty_time) {
822 msr = tegra_uart_read(tup, UART_MSR);
823 mcr = tegra_uart_read(tup, UART_MCR);
824 if ((mcr & TEGRA_UART_MCR_CTS_EN) &&
825 (msr & UART_MSR_CTS))
826 dev_err(tup->uport.dev,
827 "Slave not ready\n");
830 lsr = tegra_uart_read(tup, UART_LSR);
834 spin_lock_irqsave(&tup->uport.lock, flags);
835 /* Reset the Rx and Tx FIFOs */
836 tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_XMIT | UART_FCR_CLEAR_RCVR);
837 tup->current_baud = 0;
838 spin_unlock_irqrestore(&tup->uport.lock, flags);
840 tup->rx_in_progress = 0;
841 tup->tx_in_progress = 0;
843 tegra_uart_dma_channel_free(tup, true);
844 tegra_uart_dma_channel_free(tup, false);
846 clk_disable_unprepare(tup->uart_clk);
849 static int tegra_uart_hw_init(struct tegra_uart_port *tup)
857 tup->current_baud = 0;
859 clk_prepare_enable(tup->uart_clk);
861 /* Reset the UART controller to clear all previous status.*/
862 reset_control_assert(tup->rst);
864 reset_control_deassert(tup->rst);
866 tup->rx_in_progress = 0;
867 tup->tx_in_progress = 0;
870 * Set the trigger level
874 * For receive, this will interrupt the CPU after that many number of
875 * bytes are received, for the remaining bytes the receive timeout
876 * interrupt is received. Rx high watermark is set to 4.
878 * For transmit, if the trasnmit interrupt is enabled, this will
879 * interrupt the CPU when the number of entries in the FIFO reaches the
880 * low watermark. Tx low watermark is set to 16 bytes.
884 * Set the Tx trigger to 16. This should match the DMA burst size that
885 * programmed in the DMA registers.
887 tup->fcr_shadow = UART_FCR_ENABLE_FIFO;
888 tup->fcr_shadow |= UART_FCR_R_TRIG_01;
889 tup->fcr_shadow |= TEGRA_UART_TX_TRIG_16B;
890 tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
892 /* Dummy read to ensure the write is posted */
893 tegra_uart_read(tup, UART_SCR);
896 * For all tegra devices (up to t210), there is a hardware issue that
897 * requires software to wait for 3 UART clock periods after enabling
898 * the TX fifo, otherwise data could be lost.
900 tegra_uart_wait_cycle_time(tup, 3);
903 * Initialize the UART with default configuration
904 * (115200, N, 8, 1) so that the receive DMA buffer may be
907 tup->lcr_shadow = TEGRA_UART_DEFAULT_LSR;
908 tegra_set_baudrate(tup, TEGRA_UART_DEFAULT_BAUD);
909 tup->fcr_shadow |= UART_FCR_DMA_SELECT;
910 tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
912 ret = tegra_uart_start_rx_dma(tup);
914 dev_err(tup->uport.dev, "Not able to start Rx DMA\n");
917 tup->rx_in_progress = 1;
920 * Enable IE_RXS for the receive status interrupts like line errros.
921 * Enable IE_RX_TIMEOUT to get the bytes which cannot be DMA'd.
923 * If using DMA mode, enable EORD instead of receive interrupt which
924 * will interrupt after the UART is done with the receive instead of
925 * the interrupt when the FIFO "threshold" is reached.
927 * EORD is different interrupt than RX_TIMEOUT - RX_TIMEOUT occurs when
928 * the DATA is sitting in the FIFO and couldn't be transferred to the
929 * DMA as the DMA size alignment (4 bytes) is not met. EORD will be
930 * triggered when there is a pause of the incomming data stream for 4
933 * For pauses in the data which is not aligned to 4 bytes, we get
934 * both the EORD as well as RX_TIMEOUT - SW sees RX_TIMEOUT first
937 tup->ier_shadow = UART_IER_RLSI | UART_IER_RTOIE | TEGRA_UART_IER_EORD;
938 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
942 static void tegra_uart_dma_channel_free(struct tegra_uart_port *tup,
946 dmaengine_terminate_all(tup->rx_dma_chan);
947 dma_release_channel(tup->rx_dma_chan);
948 dma_free_coherent(tup->uport.dev, TEGRA_UART_RX_DMA_BUFFER_SIZE,
949 tup->rx_dma_buf_virt, tup->rx_dma_buf_phys);
950 tup->rx_dma_chan = NULL;
951 tup->rx_dma_buf_phys = 0;
952 tup->rx_dma_buf_virt = NULL;
954 dmaengine_terminate_all(tup->tx_dma_chan);
955 dma_release_channel(tup->tx_dma_chan);
956 dma_unmap_single(tup->uport.dev, tup->tx_dma_buf_phys,
957 UART_XMIT_SIZE, DMA_TO_DEVICE);
958 tup->tx_dma_chan = NULL;
959 tup->tx_dma_buf_phys = 0;
960 tup->tx_dma_buf_virt = NULL;
964 static int tegra_uart_dma_channel_allocate(struct tegra_uart_port *tup,
967 struct dma_chan *dma_chan;
968 unsigned char *dma_buf;
971 struct dma_slave_config dma_sconfig;
973 dma_chan = dma_request_slave_channel_reason(tup->uport.dev,
974 dma_to_memory ? "rx" : "tx");
975 if (IS_ERR(dma_chan)) {
976 ret = PTR_ERR(dma_chan);
977 dev_err(tup->uport.dev,
978 "DMA channel alloc failed: %d\n", ret);
983 dma_buf = dma_alloc_coherent(tup->uport.dev,
984 TEGRA_UART_RX_DMA_BUFFER_SIZE,
985 &dma_phys, GFP_KERNEL);
987 dev_err(tup->uport.dev,
988 "Not able to allocate the dma buffer\n");
989 dma_release_channel(dma_chan);
992 dma_sconfig.src_addr = tup->uport.mapbase;
993 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
994 dma_sconfig.src_maxburst = 4;
995 tup->rx_dma_chan = dma_chan;
996 tup->rx_dma_buf_virt = dma_buf;
997 tup->rx_dma_buf_phys = dma_phys;
999 dma_phys = dma_map_single(tup->uport.dev,
1000 tup->uport.state->xmit.buf, UART_XMIT_SIZE,
1002 if (dma_mapping_error(tup->uport.dev, dma_phys)) {
1003 dev_err(tup->uport.dev, "dma_map_single tx failed\n");
1004 dma_release_channel(dma_chan);
1007 dma_buf = tup->uport.state->xmit.buf;
1008 dma_sconfig.dst_addr = tup->uport.mapbase;
1009 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1010 dma_sconfig.dst_maxburst = 16;
1011 tup->tx_dma_chan = dma_chan;
1012 tup->tx_dma_buf_virt = dma_buf;
1013 tup->tx_dma_buf_phys = dma_phys;
1016 ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
1018 dev_err(tup->uport.dev,
1019 "Dma slave config failed, err = %d\n", ret);
1020 tegra_uart_dma_channel_free(tup, dma_to_memory);
1027 static int tegra_uart_startup(struct uart_port *u)
1029 struct tegra_uart_port *tup = to_tegra_uport(u);
1032 ret = tegra_uart_dma_channel_allocate(tup, false);
1034 dev_err(u->dev, "Tx Dma allocation failed, err = %d\n", ret);
1038 ret = tegra_uart_dma_channel_allocate(tup, true);
1040 dev_err(u->dev, "Rx Dma allocation failed, err = %d\n", ret);
1044 ret = tegra_uart_hw_init(tup);
1046 dev_err(u->dev, "Uart HW init failed, err = %d\n", ret);
1050 ret = request_irq(u->irq, tegra_uart_isr, 0,
1051 dev_name(u->dev), tup);
1053 dev_err(u->dev, "Failed to register ISR for IRQ %d\n", u->irq);
1059 tegra_uart_dma_channel_free(tup, true);
1061 tegra_uart_dma_channel_free(tup, false);
1066 * Flush any TX data submitted for DMA and PIO. Called when the
1067 * TX circular buffer is reset.
1069 static void tegra_uart_flush_buffer(struct uart_port *u)
1071 struct tegra_uart_port *tup = to_tegra_uport(u);
1074 if (tup->tx_dma_chan)
1075 dmaengine_terminate_all(tup->tx_dma_chan);
1078 static void tegra_uart_shutdown(struct uart_port *u)
1080 struct tegra_uart_port *tup = to_tegra_uport(u);
1082 tegra_uart_hw_deinit(tup);
1083 free_irq(u->irq, tup);
1086 static void tegra_uart_enable_ms(struct uart_port *u)
1088 struct tegra_uart_port *tup = to_tegra_uport(u);
1090 if (tup->enable_modem_interrupt) {
1091 tup->ier_shadow |= UART_IER_MSI;
1092 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1096 static void tegra_uart_set_termios(struct uart_port *u,
1097 struct ktermios *termios, struct ktermios *oldtermios)
1099 struct tegra_uart_port *tup = to_tegra_uport(u);
1101 unsigned long flags;
1104 struct clk *parent_clk = clk_get_parent(tup->uart_clk);
1105 unsigned long parent_clk_rate = clk_get_rate(parent_clk);
1106 int max_divider = (tup->cdata->support_clk_src_div) ? 0x7FFF : 0xFFFF;
1109 spin_lock_irqsave(&u->lock, flags);
1111 /* Changing configuration, it is safe to stop any rx now */
1112 if (tup->rts_active)
1113 set_rts(tup, false);
1115 /* Clear all interrupts as configuration is going to be changed */
1116 tegra_uart_write(tup, tup->ier_shadow | UART_IER_RDI, UART_IER);
1117 tegra_uart_read(tup, UART_IER);
1118 tegra_uart_write(tup, 0, UART_IER);
1119 tegra_uart_read(tup, UART_IER);
1122 lcr = tup->lcr_shadow;
1123 lcr &= ~UART_LCR_PARITY;
1125 /* CMSPAR isn't supported by this driver */
1126 termios->c_cflag &= ~CMSPAR;
1128 if ((termios->c_cflag & PARENB) == PARENB) {
1130 if (termios->c_cflag & PARODD) {
1131 lcr |= UART_LCR_PARITY;
1132 lcr &= ~UART_LCR_EPAR;
1133 lcr &= ~UART_LCR_SPAR;
1135 lcr |= UART_LCR_PARITY;
1136 lcr |= UART_LCR_EPAR;
1137 lcr &= ~UART_LCR_SPAR;
1141 lcr &= ~UART_LCR_WLEN8;
1142 switch (termios->c_cflag & CSIZE) {
1144 lcr |= UART_LCR_WLEN5;
1148 lcr |= UART_LCR_WLEN6;
1152 lcr |= UART_LCR_WLEN7;
1156 lcr |= UART_LCR_WLEN8;
1162 if (termios->c_cflag & CSTOPB) {
1163 lcr |= UART_LCR_STOP;
1166 lcr &= ~UART_LCR_STOP;
1170 tegra_uart_write(tup, lcr, UART_LCR);
1171 tup->lcr_shadow = lcr;
1172 tup->symb_bit = symb_bit;
1175 baud = uart_get_baud_rate(u, termios, oldtermios,
1176 parent_clk_rate/max_divider,
1177 parent_clk_rate/16);
1178 spin_unlock_irqrestore(&u->lock, flags);
1179 tegra_set_baudrate(tup, baud);
1180 if (tty_termios_baud_rate(termios))
1181 tty_termios_encode_baud_rate(termios, baud, baud);
1182 spin_lock_irqsave(&u->lock, flags);
1185 if (termios->c_cflag & CRTSCTS) {
1186 tup->mcr_shadow |= TEGRA_UART_MCR_CTS_EN;
1187 tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
1188 tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
1189 /* if top layer has asked to set rts active then do so here */
1190 if (tup->rts_active)
1193 tup->mcr_shadow &= ~TEGRA_UART_MCR_CTS_EN;
1194 tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
1195 tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
1198 /* update the port timeout based on new settings */
1199 uart_update_timeout(u, termios->c_cflag, baud);
1201 /* Make sure all writes have completed */
1202 tegra_uart_read(tup, UART_IER);
1204 /* Re-enable interrupt */
1205 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1206 tegra_uart_read(tup, UART_IER);
1208 tup->uport.ignore_status_mask = 0;
1209 /* Ignore all characters if CREAD is not set */
1210 if ((termios->c_cflag & CREAD) == 0)
1211 tup->uport.ignore_status_mask |= UART_LSR_DR;
1213 spin_unlock_irqrestore(&u->lock, flags);
1216 static const char *tegra_uart_type(struct uart_port *u)
1218 return TEGRA_UART_TYPE;
1221 static const struct uart_ops tegra_uart_ops = {
1222 .tx_empty = tegra_uart_tx_empty,
1223 .set_mctrl = tegra_uart_set_mctrl,
1224 .get_mctrl = tegra_uart_get_mctrl,
1225 .stop_tx = tegra_uart_stop_tx,
1226 .start_tx = tegra_uart_start_tx,
1227 .stop_rx = tegra_uart_stop_rx,
1228 .flush_buffer = tegra_uart_flush_buffer,
1229 .enable_ms = tegra_uart_enable_ms,
1230 .break_ctl = tegra_uart_break_ctl,
1231 .startup = tegra_uart_startup,
1232 .shutdown = tegra_uart_shutdown,
1233 .set_termios = tegra_uart_set_termios,
1234 .type = tegra_uart_type,
1235 .request_port = tegra_uart_request_port,
1236 .release_port = tegra_uart_release_port,
1239 static struct uart_driver tegra_uart_driver = {
1240 .owner = THIS_MODULE,
1241 .driver_name = "tegra_hsuart",
1242 .dev_name = "ttyTHS",
1244 .nr = TEGRA_UART_MAXIMUM,
1247 static int tegra_uart_parse_dt(struct platform_device *pdev,
1248 struct tegra_uart_port *tup)
1250 struct device_node *np = pdev->dev.of_node;
1253 port = of_alias_get_id(np, "serial");
1255 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", port);
1258 tup->uport.line = port;
1260 tup->enable_modem_interrupt = of_property_read_bool(np,
1261 "nvidia,enable-modem-interrupt");
1265 static struct tegra_uart_chip_data tegra20_uart_chip_data = {
1266 .tx_fifo_full_status = false,
1267 .allow_txfifo_reset_fifo_mode = true,
1268 .support_clk_src_div = false,
1271 static struct tegra_uart_chip_data tegra30_uart_chip_data = {
1272 .tx_fifo_full_status = true,
1273 .allow_txfifo_reset_fifo_mode = false,
1274 .support_clk_src_div = true,
1277 static const struct of_device_id tegra_uart_of_match[] = {
1279 .compatible = "nvidia,tegra30-hsuart",
1280 .data = &tegra30_uart_chip_data,
1282 .compatible = "nvidia,tegra20-hsuart",
1283 .data = &tegra20_uart_chip_data,
1287 MODULE_DEVICE_TABLE(of, tegra_uart_of_match);
1289 static int tegra_uart_probe(struct platform_device *pdev)
1291 struct tegra_uart_port *tup;
1292 struct uart_port *u;
1293 struct resource *resource;
1295 const struct tegra_uart_chip_data *cdata;
1296 const struct of_device_id *match;
1298 match = of_match_device(tegra_uart_of_match, &pdev->dev);
1300 dev_err(&pdev->dev, "Error: No device match found\n");
1303 cdata = match->data;
1305 tup = devm_kzalloc(&pdev->dev, sizeof(*tup), GFP_KERNEL);
1307 dev_err(&pdev->dev, "Failed to allocate memory for tup\n");
1311 ret = tegra_uart_parse_dt(pdev, tup);
1316 u->dev = &pdev->dev;
1317 u->ops = &tegra_uart_ops;
1318 u->type = PORT_TEGRA;
1322 platform_set_drvdata(pdev, tup);
1323 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1325 dev_err(&pdev->dev, "No IO memory resource\n");
1329 u->mapbase = resource->start;
1330 u->membase = devm_ioremap_resource(&pdev->dev, resource);
1331 if (IS_ERR(u->membase))
1332 return PTR_ERR(u->membase);
1334 tup->uart_clk = devm_clk_get(&pdev->dev, NULL);
1335 if (IS_ERR(tup->uart_clk)) {
1336 dev_err(&pdev->dev, "Couldn't get the clock\n");
1337 return PTR_ERR(tup->uart_clk);
1340 tup->rst = devm_reset_control_get_exclusive(&pdev->dev, "serial");
1341 if (IS_ERR(tup->rst)) {
1342 dev_err(&pdev->dev, "Couldn't get the reset\n");
1343 return PTR_ERR(tup->rst);
1346 u->iotype = UPIO_MEM32;
1347 ret = platform_get_irq(pdev, 0);
1352 ret = uart_add_one_port(&tegra_uart_driver, u);
1354 dev_err(&pdev->dev, "Failed to add uart port, err %d\n", ret);
1360 static int tegra_uart_remove(struct platform_device *pdev)
1362 struct tegra_uart_port *tup = platform_get_drvdata(pdev);
1363 struct uart_port *u = &tup->uport;
1365 uart_remove_one_port(&tegra_uart_driver, u);
1369 #ifdef CONFIG_PM_SLEEP
1370 static int tegra_uart_suspend(struct device *dev)
1372 struct tegra_uart_port *tup = dev_get_drvdata(dev);
1373 struct uart_port *u = &tup->uport;
1375 return uart_suspend_port(&tegra_uart_driver, u);
1378 static int tegra_uart_resume(struct device *dev)
1380 struct tegra_uart_port *tup = dev_get_drvdata(dev);
1381 struct uart_port *u = &tup->uport;
1383 return uart_resume_port(&tegra_uart_driver, u);
1387 static const struct dev_pm_ops tegra_uart_pm_ops = {
1388 SET_SYSTEM_SLEEP_PM_OPS(tegra_uart_suspend, tegra_uart_resume)
1391 static struct platform_driver tegra_uart_platform_driver = {
1392 .probe = tegra_uart_probe,
1393 .remove = tegra_uart_remove,
1395 .name = "serial-tegra",
1396 .of_match_table = tegra_uart_of_match,
1397 .pm = &tegra_uart_pm_ops,
1401 static int __init tegra_uart_init(void)
1405 ret = uart_register_driver(&tegra_uart_driver);
1407 pr_err("Could not register %s driver\n",
1408 tegra_uart_driver.driver_name);
1412 ret = platform_driver_register(&tegra_uart_platform_driver);
1414 pr_err("Uart platform driver register failed, e = %d\n", ret);
1415 uart_unregister_driver(&tegra_uart_driver);
1421 static void __exit tegra_uart_exit(void)
1423 pr_info("Unloading tegra uart driver\n");
1424 platform_driver_unregister(&tegra_uart_platform_driver);
1425 uart_unregister_driver(&tegra_uart_driver);
1428 module_init(tegra_uart_init);
1429 module_exit(tegra_uart_exit);
1431 MODULE_ALIAS("platform:serial-tegra");
1432 MODULE_DESCRIPTION("High speed UART driver for tegra chipset");
1433 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1434 MODULE_LICENSE("GPL v2");