1 // SPDX-License-Identifier: GPL-2.0
5 * High-speed serial driver for NVIDIA Tegra SoCs
7 * Copyright (c) 2012-2013, 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 8
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;
91 int max_dma_burst_bytes;
92 int error_tolerance_low_range;
93 int error_tolerance_high_range;
96 struct tegra_baud_tolerance {
102 struct tegra_uart_port {
103 struct uart_port uport;
104 const struct tegra_uart_chip_data *cdata;
106 struct clk *uart_clk;
107 struct reset_control *rst;
108 unsigned int current_baud;
110 /* Register shadow */
111 unsigned long fcr_shadow;
112 unsigned long mcr_shadow;
113 unsigned long lcr_shadow;
114 unsigned long ier_shadow;
118 unsigned int tx_bytes;
120 bool enable_modem_interrupt;
126 struct dma_chan *rx_dma_chan;
127 struct dma_chan *tx_dma_chan;
128 dma_addr_t rx_dma_buf_phys;
129 dma_addr_t tx_dma_buf_phys;
130 unsigned char *rx_dma_buf_virt;
131 unsigned char *tx_dma_buf_virt;
132 struct dma_async_tx_descriptor *tx_dma_desc;
133 struct dma_async_tx_descriptor *rx_dma_desc;
134 dma_cookie_t tx_cookie;
135 dma_cookie_t rx_cookie;
136 unsigned int tx_bytes_requested;
137 unsigned int rx_bytes_requested;
138 struct tegra_baud_tolerance *baud_tolerance;
139 int n_adjustable_baud_rates;
147 static void tegra_uart_start_next_tx(struct tegra_uart_port *tup);
148 static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup);
149 static void tegra_uart_dma_channel_free(struct tegra_uart_port *tup,
152 static inline unsigned long tegra_uart_read(struct tegra_uart_port *tup,
155 return readl(tup->uport.membase + (reg << tup->uport.regshift));
158 static inline void tegra_uart_write(struct tegra_uart_port *tup, unsigned val,
161 writel(val, tup->uport.membase + (reg << tup->uport.regshift));
164 static inline struct tegra_uart_port *to_tegra_uport(struct uart_port *u)
166 return container_of(u, struct tegra_uart_port, uport);
169 static unsigned int tegra_uart_get_mctrl(struct uart_port *u)
171 struct tegra_uart_port *tup = to_tegra_uport(u);
174 * RI - Ring detector is active
175 * CD/DCD/CAR - Carrier detect is always active. For some reason
176 * linux has different names for carrier detect.
177 * DSR - Data Set ready is active as the hardware doesn't support it.
178 * Don't know if the linux support this yet?
179 * CTS - Clear to send. Always set to active, as the hardware handles
182 if (tup->enable_modem_interrupt)
183 return TIOCM_RI | TIOCM_CD | TIOCM_DSR | TIOCM_CTS;
187 static void set_rts(struct tegra_uart_port *tup, bool active)
191 mcr = tup->mcr_shadow;
193 mcr |= TEGRA_UART_MCR_RTS_EN;
195 mcr &= ~TEGRA_UART_MCR_RTS_EN;
196 if (mcr != tup->mcr_shadow) {
197 tegra_uart_write(tup, mcr, UART_MCR);
198 tup->mcr_shadow = mcr;
202 static void set_dtr(struct tegra_uart_port *tup, bool active)
206 mcr = tup->mcr_shadow;
210 mcr &= ~UART_MCR_DTR;
211 if (mcr != tup->mcr_shadow) {
212 tegra_uart_write(tup, mcr, UART_MCR);
213 tup->mcr_shadow = mcr;
217 static void tegra_uart_set_mctrl(struct uart_port *u, unsigned int mctrl)
219 struct tegra_uart_port *tup = to_tegra_uport(u);
222 tup->rts_active = !!(mctrl & TIOCM_RTS);
223 set_rts(tup, tup->rts_active);
225 dtr_enable = !!(mctrl & TIOCM_DTR);
226 set_dtr(tup, dtr_enable);
229 static void tegra_uart_break_ctl(struct uart_port *u, int break_ctl)
231 struct tegra_uart_port *tup = to_tegra_uport(u);
234 lcr = tup->lcr_shadow;
238 lcr &= ~UART_LCR_SBC;
239 tegra_uart_write(tup, lcr, UART_LCR);
240 tup->lcr_shadow = lcr;
244 * tegra_uart_wait_cycle_time: Wait for N UART clock periods
246 * @tup: Tegra serial port data structure.
247 * @cycles: Number of clock periods to wait.
249 * Tegra UARTs are clocked at 16X the baud/bit rate and hence the UART
250 * clock speed is 16X the current baud rate.
252 static void tegra_uart_wait_cycle_time(struct tegra_uart_port *tup,
255 if (tup->current_baud)
256 udelay(DIV_ROUND_UP(cycles * 1000000, tup->current_baud * 16));
259 /* Wait for a symbol-time. */
260 static void tegra_uart_wait_sym_time(struct tegra_uart_port *tup,
263 if (tup->current_baud)
264 udelay(DIV_ROUND_UP(syms * tup->symb_bit * 1000000,
268 static int tegra_uart_wait_fifo_mode_enabled(struct tegra_uart_port *tup)
271 unsigned int tmout = 100;
274 iir = tegra_uart_read(tup, UART_IIR);
275 if (iir & TEGRA_UART_FCR_IIR_FIFO_EN)
283 static void tegra_uart_fifo_reset(struct tegra_uart_port *tup, u8 fcr_bits)
285 unsigned long fcr = tup->fcr_shadow;
287 if (tup->cdata->allow_txfifo_reset_fifo_mode) {
288 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
289 tegra_uart_write(tup, fcr, UART_FCR);
291 fcr &= ~UART_FCR_ENABLE_FIFO;
292 tegra_uart_write(tup, fcr, UART_FCR);
294 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
295 tegra_uart_write(tup, fcr, UART_FCR);
296 fcr |= UART_FCR_ENABLE_FIFO;
297 tegra_uart_write(tup, fcr, UART_FCR);
298 if (tup->cdata->fifo_mode_enable_status)
299 tegra_uart_wait_fifo_mode_enabled(tup);
302 /* Dummy read to ensure the write is posted */
303 tegra_uart_read(tup, UART_SCR);
306 * For all tegra devices (up to t210), there is a hardware issue that
307 * requires software to wait for 32 UART clock periods for the flush
308 * to propagate, otherwise data could be lost.
310 tegra_uart_wait_cycle_time(tup, 32);
313 static long tegra_get_tolerance_rate(struct tegra_uart_port *tup,
314 unsigned int baud, long rate)
318 for (i = 0; i < tup->n_adjustable_baud_rates; ++i) {
319 if (baud >= tup->baud_tolerance[i].lower_range_baud &&
320 baud <= tup->baud_tolerance[i].upper_range_baud)
321 return (rate + (rate *
322 tup->baud_tolerance[i].tolerance) / 10000);
328 static int tegra_check_rate_in_range(struct tegra_uart_port *tup)
332 diff = ((long)(tup->configured_rate - tup->required_rate) * 10000)
333 / tup->required_rate;
334 if (diff < (tup->cdata->error_tolerance_low_range * 100) ||
335 diff > (tup->cdata->error_tolerance_high_range * 100)) {
336 dev_err(tup->uport.dev,
337 "configured baud rate is out of range by %ld", diff);
344 static int tegra_set_baudrate(struct tegra_uart_port *tup, unsigned int baud)
347 unsigned int divisor;
351 if (tup->current_baud == baud)
354 if (tup->cdata->support_clk_src_div) {
356 tup->required_rate = rate;
358 if (tup->n_adjustable_baud_rates)
359 rate = tegra_get_tolerance_rate(tup, baud, rate);
361 ret = clk_set_rate(tup->uart_clk, rate);
363 dev_err(tup->uport.dev,
364 "clk_set_rate() failed for rate %lu\n", rate);
367 tup->configured_rate = clk_get_rate(tup->uart_clk);
369 ret = tegra_check_rate_in_range(tup);
373 rate = clk_get_rate(tup->uart_clk);
374 divisor = DIV_ROUND_CLOSEST(rate, baud * 16);
377 lcr = tup->lcr_shadow;
378 lcr |= UART_LCR_DLAB;
379 tegra_uart_write(tup, lcr, UART_LCR);
381 tegra_uart_write(tup, divisor & 0xFF, UART_TX);
382 tegra_uart_write(tup, ((divisor >> 8) & 0xFF), UART_IER);
384 lcr &= ~UART_LCR_DLAB;
385 tegra_uart_write(tup, lcr, UART_LCR);
387 /* Dummy read to ensure the write is posted */
388 tegra_uart_read(tup, UART_SCR);
390 tup->current_baud = baud;
392 /* wait two character intervals at new rate */
393 tegra_uart_wait_sym_time(tup, 2);
397 static char tegra_uart_decode_rx_error(struct tegra_uart_port *tup,
400 char flag = TTY_NORMAL;
402 if (unlikely(lsr & TEGRA_UART_LSR_ANY)) {
403 if (lsr & UART_LSR_OE) {
406 tup->uport.icount.overrun++;
407 dev_err(tup->uport.dev, "Got overrun errors\n");
408 } else if (lsr & UART_LSR_PE) {
411 tup->uport.icount.parity++;
412 dev_err(tup->uport.dev, "Got Parity errors\n");
413 } else if (lsr & UART_LSR_FE) {
415 tup->uport.icount.frame++;
416 dev_err(tup->uport.dev, "Got frame errors\n");
417 } else if (lsr & UART_LSR_BI) {
418 dev_err(tup->uport.dev, "Got Break\n");
419 tup->uport.icount.brk++;
420 /* If FIFO read error without any data, reset Rx FIFO */
421 if (!(lsr & UART_LSR_DR) && (lsr & UART_LSR_FIFOE))
422 tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_RCVR);
428 static int tegra_uart_request_port(struct uart_port *u)
433 static void tegra_uart_release_port(struct uart_port *u)
435 /* Nothing to do here */
438 static void tegra_uart_fill_tx_fifo(struct tegra_uart_port *tup, int max_bytes)
440 struct circ_buf *xmit = &tup->uport.state->xmit;
443 for (i = 0; i < max_bytes; i++) {
444 BUG_ON(uart_circ_empty(xmit));
445 if (tup->cdata->tx_fifo_full_status) {
446 unsigned long lsr = tegra_uart_read(tup, UART_LSR);
447 if ((lsr & TEGRA_UART_LSR_TXFIFO_FULL))
450 tegra_uart_write(tup, xmit->buf[xmit->tail], UART_TX);
451 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
452 tup->uport.icount.tx++;
456 static void tegra_uart_start_pio_tx(struct tegra_uart_port *tup,
459 if (bytes > TEGRA_UART_MIN_DMA)
460 bytes = TEGRA_UART_MIN_DMA;
462 tup->tx_in_progress = TEGRA_UART_TX_PIO;
463 tup->tx_bytes = bytes;
464 tup->ier_shadow |= UART_IER_THRI;
465 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
468 static void tegra_uart_tx_dma_complete(void *args)
470 struct tegra_uart_port *tup = args;
471 struct circ_buf *xmit = &tup->uport.state->xmit;
472 struct dma_tx_state state;
476 dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
477 count = tup->tx_bytes_requested - state.residue;
478 async_tx_ack(tup->tx_dma_desc);
479 spin_lock_irqsave(&tup->uport.lock, flags);
480 uart_xmit_advance(&tup->uport, count);
481 tup->tx_in_progress = 0;
482 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
483 uart_write_wakeup(&tup->uport);
484 tegra_uart_start_next_tx(tup);
485 spin_unlock_irqrestore(&tup->uport.lock, flags);
488 static int tegra_uart_start_tx_dma(struct tegra_uart_port *tup,
491 struct circ_buf *xmit = &tup->uport.state->xmit;
492 dma_addr_t tx_phys_addr;
494 dma_sync_single_for_device(tup->uport.dev, tup->tx_dma_buf_phys,
495 UART_XMIT_SIZE, DMA_TO_DEVICE);
497 tup->tx_bytes = count & ~(0xF);
498 tx_phys_addr = tup->tx_dma_buf_phys + xmit->tail;
499 tup->tx_dma_desc = dmaengine_prep_slave_single(tup->tx_dma_chan,
500 tx_phys_addr, tup->tx_bytes, DMA_MEM_TO_DEV,
502 if (!tup->tx_dma_desc) {
503 dev_err(tup->uport.dev, "Not able to get desc for Tx\n");
507 tup->tx_dma_desc->callback = tegra_uart_tx_dma_complete;
508 tup->tx_dma_desc->callback_param = tup;
509 tup->tx_in_progress = TEGRA_UART_TX_DMA;
510 tup->tx_bytes_requested = tup->tx_bytes;
511 tup->tx_cookie = dmaengine_submit(tup->tx_dma_desc);
512 dma_async_issue_pending(tup->tx_dma_chan);
516 static void tegra_uart_start_next_tx(struct tegra_uart_port *tup)
520 struct circ_buf *xmit = &tup->uport.state->xmit;
522 if (!tup->current_baud)
525 tail = (unsigned long)&xmit->buf[xmit->tail];
526 count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
530 if (tup->use_tx_pio || count < TEGRA_UART_MIN_DMA)
531 tegra_uart_start_pio_tx(tup, count);
532 else if (BYTES_TO_ALIGN(tail) > 0)
533 tegra_uart_start_pio_tx(tup, BYTES_TO_ALIGN(tail));
535 tegra_uart_start_tx_dma(tup, count);
538 /* Called by serial core driver with u->lock taken. */
539 static void tegra_uart_start_tx(struct uart_port *u)
541 struct tegra_uart_port *tup = to_tegra_uport(u);
542 struct circ_buf *xmit = &u->state->xmit;
544 if (!uart_circ_empty(xmit) && !tup->tx_in_progress)
545 tegra_uart_start_next_tx(tup);
548 static unsigned int tegra_uart_tx_empty(struct uart_port *u)
550 struct tegra_uart_port *tup = to_tegra_uport(u);
551 unsigned int ret = 0;
554 spin_lock_irqsave(&u->lock, flags);
555 if (!tup->tx_in_progress) {
556 unsigned long lsr = tegra_uart_read(tup, UART_LSR);
557 if ((lsr & TX_EMPTY_STATUS) == TX_EMPTY_STATUS)
560 spin_unlock_irqrestore(&u->lock, flags);
564 static void tegra_uart_stop_tx(struct uart_port *u)
566 struct tegra_uart_port *tup = to_tegra_uport(u);
567 struct dma_tx_state state;
570 if (tup->tx_in_progress != TEGRA_UART_TX_DMA)
573 dmaengine_pause(tup->tx_dma_chan);
574 dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
575 dmaengine_terminate_all(tup->tx_dma_chan);
576 count = tup->tx_bytes_requested - state.residue;
577 async_tx_ack(tup->tx_dma_desc);
578 uart_xmit_advance(&tup->uport, count);
579 tup->tx_in_progress = 0;
582 static void tegra_uart_handle_tx_pio(struct tegra_uart_port *tup)
584 struct circ_buf *xmit = &tup->uport.state->xmit;
586 tegra_uart_fill_tx_fifo(tup, tup->tx_bytes);
587 tup->tx_in_progress = 0;
588 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
589 uart_write_wakeup(&tup->uport);
590 tegra_uart_start_next_tx(tup);
593 static void tegra_uart_handle_rx_pio(struct tegra_uart_port *tup,
594 struct tty_port *tty)
597 char flag = TTY_NORMAL;
598 unsigned long lsr = 0;
601 lsr = tegra_uart_read(tup, UART_LSR);
602 if (!(lsr & UART_LSR_DR))
605 flag = tegra_uart_decode_rx_error(tup, lsr);
606 ch = (unsigned char) tegra_uart_read(tup, UART_RX);
607 tup->uport.icount.rx++;
609 if (!uart_handle_sysrq_char(&tup->uport, ch) && tty)
610 tty_insert_flip_char(tty, ch, flag);
614 static void tegra_uart_copy_rx_to_tty(struct tegra_uart_port *tup,
615 struct tty_port *tty,
620 /* If count is zero, then there is no data to be copied */
624 tup->uport.icount.rx += count;
626 dev_err(tup->uport.dev, "No tty port\n");
629 dma_sync_single_for_cpu(tup->uport.dev, tup->rx_dma_buf_phys,
630 TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
631 copied = tty_insert_flip_string(tty,
632 ((unsigned char *)(tup->rx_dma_buf_virt)), count);
633 if (copied != count) {
635 dev_err(tup->uport.dev, "RxData copy to tty layer failed\n");
637 dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
638 TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_TO_DEVICE);
641 static void do_handle_rx_pio(struct tegra_uart_port *tup)
643 struct tty_struct *tty = tty_port_tty_get(&tup->uport.state->port);
644 struct tty_port *port = &tup->uport.state->port;
646 tegra_uart_handle_rx_pio(tup, port);
648 tty_flip_buffer_push(port);
653 static void tegra_uart_rx_buffer_push(struct tegra_uart_port *tup,
654 unsigned int residue)
656 struct tty_port *port = &tup->uport.state->port;
659 async_tx_ack(tup->rx_dma_desc);
660 count = tup->rx_bytes_requested - residue;
662 /* If we are here, DMA is stopped */
663 tegra_uart_copy_rx_to_tty(tup, port, count);
665 do_handle_rx_pio(tup);
668 static void tegra_uart_rx_dma_complete(void *args)
670 struct tegra_uart_port *tup = args;
671 struct uart_port *u = &tup->uport;
673 struct dma_tx_state state;
674 enum dma_status status;
676 spin_lock_irqsave(&u->lock, flags);
678 status = dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
680 if (status == DMA_IN_PROGRESS) {
681 dev_dbg(tup->uport.dev, "RX DMA is in progress\n");
685 /* Deactivate flow control to stop sender */
689 tup->rx_dma_active = false;
690 tegra_uart_rx_buffer_push(tup, 0);
691 tegra_uart_start_rx_dma(tup);
693 /* Activate flow control to start transfer */
698 spin_unlock_irqrestore(&u->lock, flags);
701 static void tegra_uart_terminate_rx_dma(struct tegra_uart_port *tup)
703 struct dma_tx_state state;
705 if (!tup->rx_dma_active) {
706 do_handle_rx_pio(tup);
710 dmaengine_pause(tup->rx_dma_chan);
711 dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
712 dmaengine_terminate_all(tup->rx_dma_chan);
714 tegra_uart_rx_buffer_push(tup, state.residue);
715 tup->rx_dma_active = false;
718 static void tegra_uart_handle_rx_dma(struct tegra_uart_port *tup)
720 /* Deactivate flow control to stop sender */
724 tegra_uart_terminate_rx_dma(tup);
730 static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup)
732 unsigned int count = TEGRA_UART_RX_DMA_BUFFER_SIZE;
734 if (tup->rx_dma_active)
737 tup->rx_dma_desc = dmaengine_prep_slave_single(tup->rx_dma_chan,
738 tup->rx_dma_buf_phys, count, DMA_DEV_TO_MEM,
740 if (!tup->rx_dma_desc) {
741 dev_err(tup->uport.dev, "Not able to get desc for Rx\n");
745 tup->rx_dma_active = true;
746 tup->rx_dma_desc->callback = tegra_uart_rx_dma_complete;
747 tup->rx_dma_desc->callback_param = tup;
748 dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
749 count, DMA_TO_DEVICE);
750 tup->rx_bytes_requested = count;
751 tup->rx_cookie = dmaengine_submit(tup->rx_dma_desc);
752 dma_async_issue_pending(tup->rx_dma_chan);
756 static void tegra_uart_handle_modem_signal_change(struct uart_port *u)
758 struct tegra_uart_port *tup = to_tegra_uport(u);
761 msr = tegra_uart_read(tup, UART_MSR);
762 if (!(msr & UART_MSR_ANY_DELTA))
765 if (msr & UART_MSR_TERI)
766 tup->uport.icount.rng++;
767 if (msr & UART_MSR_DDSR)
768 tup->uport.icount.dsr++;
769 /* We may only get DDCD when HW init and reset */
770 if (msr & UART_MSR_DDCD)
771 uart_handle_dcd_change(&tup->uport, msr & UART_MSR_DCD);
772 /* Will start/stop_tx accordingly */
773 if (msr & UART_MSR_DCTS)
774 uart_handle_cts_change(&tup->uport, msr & UART_MSR_CTS);
777 static irqreturn_t tegra_uart_isr(int irq, void *data)
779 struct tegra_uart_port *tup = data;
780 struct uart_port *u = &tup->uport;
783 bool is_rx_start = false;
784 bool is_rx_int = false;
787 spin_lock_irqsave(&u->lock, flags);
789 iir = tegra_uart_read(tup, UART_IIR);
790 if (iir & UART_IIR_NO_INT) {
791 if (!tup->use_rx_pio && is_rx_int) {
792 tegra_uart_handle_rx_dma(tup);
793 if (tup->rx_in_progress) {
794 ier = tup->ier_shadow;
795 ier |= (UART_IER_RLSI | UART_IER_RTOIE |
796 TEGRA_UART_IER_EORD | UART_IER_RDI);
797 tup->ier_shadow = ier;
798 tegra_uart_write(tup, ier, UART_IER);
800 } else if (is_rx_start) {
801 tegra_uart_start_rx_dma(tup);
803 spin_unlock_irqrestore(&u->lock, flags);
807 switch ((iir >> 1) & 0x7) {
808 case 0: /* Modem signal change interrupt */
809 tegra_uart_handle_modem_signal_change(u);
812 case 1: /* Transmit interrupt only triggered when using PIO */
813 tup->ier_shadow &= ~UART_IER_THRI;
814 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
815 tegra_uart_handle_tx_pio(tup);
818 case 4: /* End of data */
819 case 6: /* Rx timeout */
820 if (!tup->use_rx_pio) {
821 is_rx_int = tup->rx_in_progress;
822 /* Disable Rx interrupts */
823 ier = tup->ier_shadow;
824 ier &= ~(UART_IER_RDI | UART_IER_RLSI |
825 UART_IER_RTOIE | TEGRA_UART_IER_EORD);
826 tup->ier_shadow = ier;
827 tegra_uart_write(tup, ier, UART_IER);
831 case 2: /* Receive */
832 if (!tup->use_rx_pio) {
833 is_rx_start = tup->rx_in_progress;
834 tup->ier_shadow &= ~UART_IER_RDI;
835 tegra_uart_write(tup, tup->ier_shadow,
838 do_handle_rx_pio(tup);
842 case 3: /* Receive error */
843 tegra_uart_decode_rx_error(tup,
844 tegra_uart_read(tup, UART_LSR));
847 case 5: /* break nothing to handle */
848 case 7: /* break nothing to handle */
854 static void tegra_uart_stop_rx(struct uart_port *u)
856 struct tegra_uart_port *tup = to_tegra_uport(u);
857 struct tty_port *port = &tup->uport.state->port;
863 if (!tup->rx_in_progress)
866 tegra_uart_wait_sym_time(tup, 1); /* wait a character interval */
868 ier = tup->ier_shadow;
869 ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE |
870 TEGRA_UART_IER_EORD);
871 tup->ier_shadow = ier;
872 tegra_uart_write(tup, ier, UART_IER);
873 tup->rx_in_progress = 0;
875 if (!tup->use_rx_pio)
876 tegra_uart_terminate_rx_dma(tup);
878 tegra_uart_handle_rx_pio(tup, port);
881 static void tegra_uart_hw_deinit(struct tegra_uart_port *tup)
884 unsigned long char_time = DIV_ROUND_UP(10000000, tup->current_baud);
885 unsigned long fifo_empty_time = tup->uport.fifosize * char_time;
886 unsigned long wait_time;
891 /* Disable interrupts */
892 tegra_uart_write(tup, 0, UART_IER);
894 lsr = tegra_uart_read(tup, UART_LSR);
895 if ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
896 msr = tegra_uart_read(tup, UART_MSR);
897 mcr = tegra_uart_read(tup, UART_MCR);
898 if ((mcr & TEGRA_UART_MCR_CTS_EN) && (msr & UART_MSR_CTS))
899 dev_err(tup->uport.dev,
900 "Tx Fifo not empty, CTS disabled, waiting\n");
902 /* Wait for Tx fifo to be empty */
903 while ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
904 wait_time = min(fifo_empty_time, 100lu);
906 fifo_empty_time -= wait_time;
907 if (!fifo_empty_time) {
908 msr = tegra_uart_read(tup, UART_MSR);
909 mcr = tegra_uart_read(tup, UART_MCR);
910 if ((mcr & TEGRA_UART_MCR_CTS_EN) &&
911 (msr & UART_MSR_CTS))
912 dev_err(tup->uport.dev,
913 "Slave not ready\n");
916 lsr = tegra_uart_read(tup, UART_LSR);
920 spin_lock_irqsave(&tup->uport.lock, flags);
921 /* Reset the Rx and Tx FIFOs */
922 tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_XMIT | UART_FCR_CLEAR_RCVR);
923 tup->current_baud = 0;
924 spin_unlock_irqrestore(&tup->uport.lock, flags);
926 tup->rx_in_progress = 0;
927 tup->tx_in_progress = 0;
929 if (!tup->use_rx_pio)
930 tegra_uart_dma_channel_free(tup, true);
931 if (!tup->use_tx_pio)
932 tegra_uart_dma_channel_free(tup, false);
934 clk_disable_unprepare(tup->uart_clk);
937 static int tegra_uart_hw_init(struct tegra_uart_port *tup)
945 tup->current_baud = 0;
947 ret = clk_prepare_enable(tup->uart_clk);
949 dev_err(tup->uport.dev, "could not enable clk\n");
953 /* Reset the UART controller to clear all previous status.*/
954 reset_control_assert(tup->rst);
956 reset_control_deassert(tup->rst);
958 tup->rx_in_progress = 0;
959 tup->tx_in_progress = 0;
962 * Set the trigger level
966 * For receive, this will interrupt the CPU after that many number of
967 * bytes are received, for the remaining bytes the receive timeout
968 * interrupt is received. Rx high watermark is set to 4.
970 * For transmit, if the trasnmit interrupt is enabled, this will
971 * interrupt the CPU when the number of entries in the FIFO reaches the
972 * low watermark. Tx low watermark is set to 16 bytes.
976 * Set the Tx trigger to 16. This should match the DMA burst size that
977 * programmed in the DMA registers.
979 tup->fcr_shadow = UART_FCR_ENABLE_FIFO;
981 if (tup->use_rx_pio) {
982 tup->fcr_shadow |= UART_FCR_R_TRIG_11;
984 if (tup->cdata->max_dma_burst_bytes == 8)
985 tup->fcr_shadow |= UART_FCR_R_TRIG_10;
987 tup->fcr_shadow |= UART_FCR_R_TRIG_01;
990 tup->fcr_shadow |= TEGRA_UART_TX_TRIG_16B;
991 tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
993 /* Dummy read to ensure the write is posted */
994 tegra_uart_read(tup, UART_SCR);
996 if (tup->cdata->fifo_mode_enable_status) {
997 ret = tegra_uart_wait_fifo_mode_enabled(tup);
999 dev_err(tup->uport.dev,
1000 "Failed to enable FIFO mode: %d\n", ret);
1005 * For all tegra devices (up to t210), there is a hardware
1006 * issue that requires software to wait for 3 UART clock
1007 * periods after enabling the TX fifo, otherwise data could
1010 tegra_uart_wait_cycle_time(tup, 3);
1014 * Initialize the UART with default configuration
1015 * (115200, N, 8, 1) so that the receive DMA buffer may be
1018 ret = tegra_set_baudrate(tup, TEGRA_UART_DEFAULT_BAUD);
1020 dev_err(tup->uport.dev, "Failed to set baud rate\n");
1023 if (!tup->use_rx_pio) {
1024 tup->lcr_shadow = TEGRA_UART_DEFAULT_LSR;
1025 tup->fcr_shadow |= UART_FCR_DMA_SELECT;
1026 tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
1028 tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
1030 tup->rx_in_progress = 1;
1033 * Enable IE_RXS for the receive status interrupts like line errros.
1034 * Enable IE_RX_TIMEOUT to get the bytes which cannot be DMA'd.
1036 * EORD is different interrupt than RX_TIMEOUT - RX_TIMEOUT occurs when
1037 * the DATA is sitting in the FIFO and couldn't be transferred to the
1038 * DMA as the DMA size alignment(4 bytes) is not met. EORD will be
1039 * triggered when there is a pause of the incomming data stream for 4
1042 * For pauses in the data which is not aligned to 4 bytes, we get
1043 * both the EORD as well as RX_TIMEOUT - SW sees RX_TIMEOUT first
1046 tup->ier_shadow = UART_IER_RLSI | UART_IER_RTOIE | UART_IER_RDI;
1049 * If using DMA mode, enable EORD interrupt to notify about RX
1052 if (!tup->use_rx_pio)
1053 tup->ier_shadow |= TEGRA_UART_IER_EORD;
1055 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1059 static void tegra_uart_dma_channel_free(struct tegra_uart_port *tup,
1062 if (dma_to_memory) {
1063 dmaengine_terminate_all(tup->rx_dma_chan);
1064 dma_release_channel(tup->rx_dma_chan);
1065 dma_free_coherent(tup->uport.dev, TEGRA_UART_RX_DMA_BUFFER_SIZE,
1066 tup->rx_dma_buf_virt, tup->rx_dma_buf_phys);
1067 tup->rx_dma_chan = NULL;
1068 tup->rx_dma_buf_phys = 0;
1069 tup->rx_dma_buf_virt = NULL;
1071 dmaengine_terminate_all(tup->tx_dma_chan);
1072 dma_release_channel(tup->tx_dma_chan);
1073 dma_unmap_single(tup->uport.dev, tup->tx_dma_buf_phys,
1074 UART_XMIT_SIZE, DMA_TO_DEVICE);
1075 tup->tx_dma_chan = NULL;
1076 tup->tx_dma_buf_phys = 0;
1077 tup->tx_dma_buf_virt = NULL;
1081 static int tegra_uart_dma_channel_allocate(struct tegra_uart_port *tup,
1084 struct dma_chan *dma_chan;
1085 unsigned char *dma_buf;
1086 dma_addr_t dma_phys;
1088 struct dma_slave_config dma_sconfig;
1090 dma_chan = dma_request_slave_channel_reason(tup->uport.dev,
1091 dma_to_memory ? "rx" : "tx");
1092 if (IS_ERR(dma_chan)) {
1093 ret = PTR_ERR(dma_chan);
1094 dev_err(tup->uport.dev,
1095 "DMA channel alloc failed: %d\n", ret);
1099 if (dma_to_memory) {
1100 dma_buf = dma_alloc_coherent(tup->uport.dev,
1101 TEGRA_UART_RX_DMA_BUFFER_SIZE,
1102 &dma_phys, GFP_KERNEL);
1104 dev_err(tup->uport.dev,
1105 "Not able to allocate the dma buffer\n");
1106 dma_release_channel(dma_chan);
1109 dma_sconfig.src_addr = tup->uport.mapbase;
1110 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1111 dma_sconfig.src_maxburst = tup->cdata->max_dma_burst_bytes;
1112 tup->rx_dma_chan = dma_chan;
1113 tup->rx_dma_buf_virt = dma_buf;
1114 tup->rx_dma_buf_phys = dma_phys;
1116 dma_phys = dma_map_single(tup->uport.dev,
1117 tup->uport.state->xmit.buf, UART_XMIT_SIZE,
1119 if (dma_mapping_error(tup->uport.dev, dma_phys)) {
1120 dev_err(tup->uport.dev, "dma_map_single tx failed\n");
1121 dma_release_channel(dma_chan);
1124 dma_buf = tup->uport.state->xmit.buf;
1125 dma_sconfig.dst_addr = tup->uport.mapbase;
1126 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1127 dma_sconfig.dst_maxburst = 16;
1128 tup->tx_dma_chan = dma_chan;
1129 tup->tx_dma_buf_virt = dma_buf;
1130 tup->tx_dma_buf_phys = dma_phys;
1133 ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
1135 dev_err(tup->uport.dev,
1136 "Dma slave config failed, err = %d\n", ret);
1137 tegra_uart_dma_channel_free(tup, dma_to_memory);
1144 static int tegra_uart_startup(struct uart_port *u)
1146 struct tegra_uart_port *tup = to_tegra_uport(u);
1149 if (!tup->use_tx_pio) {
1150 ret = tegra_uart_dma_channel_allocate(tup, false);
1152 dev_err(u->dev, "Tx Dma allocation failed, err = %d\n",
1158 if (!tup->use_rx_pio) {
1159 ret = tegra_uart_dma_channel_allocate(tup, true);
1161 dev_err(u->dev, "Rx Dma allocation failed, err = %d\n",
1167 ret = tegra_uart_hw_init(tup);
1169 dev_err(u->dev, "Uart HW init failed, err = %d\n", ret);
1173 ret = request_irq(u->irq, tegra_uart_isr, 0,
1174 dev_name(u->dev), tup);
1176 dev_err(u->dev, "Failed to register ISR for IRQ %d\n", u->irq);
1182 if (!tup->use_rx_pio)
1183 tegra_uart_dma_channel_free(tup, true);
1185 if (!tup->use_tx_pio)
1186 tegra_uart_dma_channel_free(tup, false);
1191 * Flush any TX data submitted for DMA and PIO. Called when the
1192 * TX circular buffer is reset.
1194 static void tegra_uart_flush_buffer(struct uart_port *u)
1196 struct tegra_uart_port *tup = to_tegra_uport(u);
1199 if (tup->tx_dma_chan)
1200 dmaengine_terminate_all(tup->tx_dma_chan);
1203 static void tegra_uart_shutdown(struct uart_port *u)
1205 struct tegra_uart_port *tup = to_tegra_uport(u);
1207 tegra_uart_hw_deinit(tup);
1208 free_irq(u->irq, tup);
1211 static void tegra_uart_enable_ms(struct uart_port *u)
1213 struct tegra_uart_port *tup = to_tegra_uport(u);
1215 if (tup->enable_modem_interrupt) {
1216 tup->ier_shadow |= UART_IER_MSI;
1217 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1221 static void tegra_uart_set_termios(struct uart_port *u,
1222 struct ktermios *termios, struct ktermios *oldtermios)
1224 struct tegra_uart_port *tup = to_tegra_uport(u);
1226 unsigned long flags;
1229 struct clk *parent_clk = clk_get_parent(tup->uart_clk);
1230 unsigned long parent_clk_rate = clk_get_rate(parent_clk);
1231 int max_divider = (tup->cdata->support_clk_src_div) ? 0x7FFF : 0xFFFF;
1235 spin_lock_irqsave(&u->lock, flags);
1237 /* Changing configuration, it is safe to stop any rx now */
1238 if (tup->rts_active)
1239 set_rts(tup, false);
1241 /* Clear all interrupts as configuration is going to be change */
1242 tegra_uart_write(tup, tup->ier_shadow | UART_IER_RDI, UART_IER);
1243 tegra_uart_read(tup, UART_IER);
1244 tegra_uart_write(tup, 0, UART_IER);
1245 tegra_uart_read(tup, UART_IER);
1248 lcr = tup->lcr_shadow;
1249 lcr &= ~UART_LCR_PARITY;
1251 /* CMSPAR isn't supported by this driver */
1252 termios->c_cflag &= ~CMSPAR;
1254 if ((termios->c_cflag & PARENB) == PARENB) {
1256 if (termios->c_cflag & PARODD) {
1257 lcr |= UART_LCR_PARITY;
1258 lcr &= ~UART_LCR_EPAR;
1259 lcr &= ~UART_LCR_SPAR;
1261 lcr |= UART_LCR_PARITY;
1262 lcr |= UART_LCR_EPAR;
1263 lcr &= ~UART_LCR_SPAR;
1267 lcr &= ~UART_LCR_WLEN8;
1268 switch (termios->c_cflag & CSIZE) {
1270 lcr |= UART_LCR_WLEN5;
1274 lcr |= UART_LCR_WLEN6;
1278 lcr |= UART_LCR_WLEN7;
1282 lcr |= UART_LCR_WLEN8;
1288 if (termios->c_cflag & CSTOPB) {
1289 lcr |= UART_LCR_STOP;
1292 lcr &= ~UART_LCR_STOP;
1296 tegra_uart_write(tup, lcr, UART_LCR);
1297 tup->lcr_shadow = lcr;
1298 tup->symb_bit = symb_bit;
1301 baud = uart_get_baud_rate(u, termios, oldtermios,
1302 parent_clk_rate/max_divider,
1303 parent_clk_rate/16);
1304 spin_unlock_irqrestore(&u->lock, flags);
1305 ret = tegra_set_baudrate(tup, baud);
1307 dev_err(tup->uport.dev, "Failed to set baud rate\n");
1310 if (tty_termios_baud_rate(termios))
1311 tty_termios_encode_baud_rate(termios, baud, baud);
1312 spin_lock_irqsave(&u->lock, flags);
1315 if (termios->c_cflag & CRTSCTS) {
1316 tup->mcr_shadow |= TEGRA_UART_MCR_CTS_EN;
1317 tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
1318 tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
1319 /* if top layer has asked to set rts active then do so here */
1320 if (tup->rts_active)
1323 tup->mcr_shadow &= ~TEGRA_UART_MCR_CTS_EN;
1324 tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
1325 tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
1328 /* update the port timeout based on new settings */
1329 uart_update_timeout(u, termios->c_cflag, baud);
1331 /* Make sure all write has completed */
1332 tegra_uart_read(tup, UART_IER);
1334 /* Reenable interrupt */
1335 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1336 tegra_uart_read(tup, UART_IER);
1338 spin_unlock_irqrestore(&u->lock, flags);
1341 static const char *tegra_uart_type(struct uart_port *u)
1343 return TEGRA_UART_TYPE;
1346 static const struct uart_ops tegra_uart_ops = {
1347 .tx_empty = tegra_uart_tx_empty,
1348 .set_mctrl = tegra_uart_set_mctrl,
1349 .get_mctrl = tegra_uart_get_mctrl,
1350 .stop_tx = tegra_uart_stop_tx,
1351 .start_tx = tegra_uart_start_tx,
1352 .stop_rx = tegra_uart_stop_rx,
1353 .flush_buffer = tegra_uart_flush_buffer,
1354 .enable_ms = tegra_uart_enable_ms,
1355 .break_ctl = tegra_uart_break_ctl,
1356 .startup = tegra_uart_startup,
1357 .shutdown = tegra_uart_shutdown,
1358 .set_termios = tegra_uart_set_termios,
1359 .type = tegra_uart_type,
1360 .request_port = tegra_uart_request_port,
1361 .release_port = tegra_uart_release_port,
1364 static struct uart_driver tegra_uart_driver = {
1365 .owner = THIS_MODULE,
1366 .driver_name = "tegra_hsuart",
1367 .dev_name = "ttyTHS",
1369 .nr = TEGRA_UART_MAXIMUM,
1372 static int tegra_uart_parse_dt(struct platform_device *pdev,
1373 struct tegra_uart_port *tup)
1375 struct device_node *np = pdev->dev.of_node;
1383 port = of_alias_get_id(np, "serial");
1385 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", port);
1388 tup->uport.line = port;
1390 tup->enable_modem_interrupt = of_property_read_bool(np,
1391 "nvidia,enable-modem-interrupt");
1393 index = of_property_match_string(np, "dma-names", "rx");
1395 tup->use_rx_pio = true;
1396 dev_info(&pdev->dev, "RX in PIO mode\n");
1398 index = of_property_match_string(np, "dma-names", "tx");
1400 tup->use_tx_pio = true;
1401 dev_info(&pdev->dev, "TX in PIO mode\n");
1404 n_entries = of_property_count_u32_elems(np, "nvidia,adjust-baud-rates");
1405 if (n_entries > 0) {
1406 tup->n_adjustable_baud_rates = n_entries / 3;
1407 tup->baud_tolerance =
1408 devm_kzalloc(&pdev->dev, (tup->n_adjustable_baud_rates) *
1409 sizeof(*tup->baud_tolerance), GFP_KERNEL);
1410 if (!tup->baud_tolerance)
1412 for (count = 0, index = 0; count < n_entries; count += 3,
1415 of_property_read_u32_index(np,
1416 "nvidia,adjust-baud-rates",
1419 tup->baud_tolerance[index].lower_range_baud =
1422 of_property_read_u32_index(np,
1423 "nvidia,adjust-baud-rates",
1426 tup->baud_tolerance[index].upper_range_baud =
1429 of_property_read_u32_index(np,
1430 "nvidia,adjust-baud-rates",
1433 tup->baud_tolerance[index].tolerance =
1437 tup->n_adjustable_baud_rates = 0;
1443 static struct tegra_uart_chip_data tegra20_uart_chip_data = {
1444 .tx_fifo_full_status = false,
1445 .allow_txfifo_reset_fifo_mode = true,
1446 .support_clk_src_div = false,
1447 .fifo_mode_enable_status = false,
1449 .max_dma_burst_bytes = 4,
1450 .error_tolerance_low_range = -4,
1451 .error_tolerance_high_range = 4,
1454 static struct tegra_uart_chip_data tegra30_uart_chip_data = {
1455 .tx_fifo_full_status = true,
1456 .allow_txfifo_reset_fifo_mode = false,
1457 .support_clk_src_div = true,
1458 .fifo_mode_enable_status = false,
1460 .max_dma_burst_bytes = 4,
1461 .error_tolerance_low_range = -4,
1462 .error_tolerance_high_range = 4,
1465 static struct tegra_uart_chip_data tegra186_uart_chip_data = {
1466 .tx_fifo_full_status = true,
1467 .allow_txfifo_reset_fifo_mode = false,
1468 .support_clk_src_div = true,
1469 .fifo_mode_enable_status = true,
1471 .max_dma_burst_bytes = 8,
1472 .error_tolerance_low_range = 0,
1473 .error_tolerance_high_range = 4,
1476 static struct tegra_uart_chip_data tegra194_uart_chip_data = {
1477 .tx_fifo_full_status = true,
1478 .allow_txfifo_reset_fifo_mode = false,
1479 .support_clk_src_div = true,
1480 .fifo_mode_enable_status = true,
1482 .max_dma_burst_bytes = 8,
1483 .error_tolerance_low_range = -2,
1484 .error_tolerance_high_range = 2,
1487 static const struct of_device_id tegra_uart_of_match[] = {
1489 .compatible = "nvidia,tegra30-hsuart",
1490 .data = &tegra30_uart_chip_data,
1492 .compatible = "nvidia,tegra20-hsuart",
1493 .data = &tegra20_uart_chip_data,
1495 .compatible = "nvidia,tegra186-hsuart",
1496 .data = &tegra186_uart_chip_data,
1498 .compatible = "nvidia,tegra194-hsuart",
1499 .data = &tegra194_uart_chip_data,
1503 MODULE_DEVICE_TABLE(of, tegra_uart_of_match);
1505 static int tegra_uart_probe(struct platform_device *pdev)
1507 struct tegra_uart_port *tup;
1508 struct uart_port *u;
1509 struct resource *resource;
1511 const struct tegra_uart_chip_data *cdata;
1512 const struct of_device_id *match;
1514 match = of_match_device(tegra_uart_of_match, &pdev->dev);
1516 dev_err(&pdev->dev, "Error: No device match found\n");
1519 cdata = match->data;
1521 tup = devm_kzalloc(&pdev->dev, sizeof(*tup), GFP_KERNEL);
1523 dev_err(&pdev->dev, "Failed to allocate memory for tup\n");
1527 ret = tegra_uart_parse_dt(pdev, tup);
1532 u->dev = &pdev->dev;
1533 u->ops = &tegra_uart_ops;
1534 u->type = PORT_TEGRA;
1538 platform_set_drvdata(pdev, tup);
1539 resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1541 dev_err(&pdev->dev, "No IO memory resource\n");
1545 u->mapbase = resource->start;
1546 u->membase = devm_ioremap_resource(&pdev->dev, resource);
1547 if (IS_ERR(u->membase))
1548 return PTR_ERR(u->membase);
1550 tup->uart_clk = devm_clk_get(&pdev->dev, NULL);
1551 if (IS_ERR(tup->uart_clk)) {
1552 dev_err(&pdev->dev, "Couldn't get the clock\n");
1553 return PTR_ERR(tup->uart_clk);
1556 tup->rst = devm_reset_control_get_exclusive(&pdev->dev, "serial");
1557 if (IS_ERR(tup->rst)) {
1558 dev_err(&pdev->dev, "Couldn't get the reset\n");
1559 return PTR_ERR(tup->rst);
1562 u->iotype = UPIO_MEM32;
1563 ret = platform_get_irq(pdev, 0);
1565 dev_err(&pdev->dev, "Couldn't get IRQ\n");
1570 ret = uart_add_one_port(&tegra_uart_driver, u);
1572 dev_err(&pdev->dev, "Failed to add uart port, err %d\n", ret);
1578 static int tegra_uart_remove(struct platform_device *pdev)
1580 struct tegra_uart_port *tup = platform_get_drvdata(pdev);
1581 struct uart_port *u = &tup->uport;
1583 uart_remove_one_port(&tegra_uart_driver, u);
1587 #ifdef CONFIG_PM_SLEEP
1588 static int tegra_uart_suspend(struct device *dev)
1590 struct tegra_uart_port *tup = dev_get_drvdata(dev);
1591 struct uart_port *u = &tup->uport;
1593 return uart_suspend_port(&tegra_uart_driver, u);
1596 static int tegra_uart_resume(struct device *dev)
1598 struct tegra_uart_port *tup = dev_get_drvdata(dev);
1599 struct uart_port *u = &tup->uport;
1601 return uart_resume_port(&tegra_uart_driver, u);
1605 static const struct dev_pm_ops tegra_uart_pm_ops = {
1606 SET_SYSTEM_SLEEP_PM_OPS(tegra_uart_suspend, tegra_uart_resume)
1609 static struct platform_driver tegra_uart_platform_driver = {
1610 .probe = tegra_uart_probe,
1611 .remove = tegra_uart_remove,
1613 .name = "serial-tegra",
1614 .of_match_table = tegra_uart_of_match,
1615 .pm = &tegra_uart_pm_ops,
1619 static int __init tegra_uart_init(void)
1622 struct device_node *node;
1623 const struct of_device_id *match = NULL;
1624 const struct tegra_uart_chip_data *cdata = NULL;
1626 node = of_find_matching_node(NULL, tegra_uart_of_match);
1628 match = of_match_node(tegra_uart_of_match, node);
1630 cdata = match->data;
1632 tegra_uart_driver.nr = cdata->uart_max_port;
1634 ret = uart_register_driver(&tegra_uart_driver);
1636 pr_err("Could not register %s driver\n",
1637 tegra_uart_driver.driver_name);
1641 ret = platform_driver_register(&tegra_uart_platform_driver);
1643 pr_err("Uart platform driver register failed, e = %d\n", ret);
1644 uart_unregister_driver(&tegra_uart_driver);
1650 static void __exit tegra_uart_exit(void)
1652 pr_info("Unloading tegra uart driver\n");
1653 platform_driver_unregister(&tegra_uart_platform_driver);
1654 uart_unregister_driver(&tegra_uart_driver);
1657 module_init(tegra_uart_init);
1658 module_exit(tegra_uart_exit);
1660 MODULE_ALIAS("platform:serial-tegra");
1661 MODULE_DESCRIPTION("High speed UART driver for tegra chipset");
1662 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1663 MODULE_LICENSE("GPL v2");