]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/tty/serial/serial-tegra.c
serial: tegra: flush the RX fifo on frame error
[linux.git] / drivers / tty / serial / serial-tegra.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * serial_tegra.c
4  *
5  * High-speed serial driver for NVIDIA Tegra SoCs
6  *
7  * Copyright (c) 2012-2019, NVIDIA CORPORATION.  All rights reserved.
8  *
9  * Author: Laxman Dewangan <ldewangan@nvidia.com>
10  */
11
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>
19 #include <linux/io.h>
20 #include <linux/irq.h>
21 #include <linux/module.h>
22 #include <linux/of.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>
36
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)
40
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
50
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
55
56 /*
57  * Tx fifo trigger level setting in tegra uart is in
58  * reverse way then conventional uart.
59  */
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
64
65 #define TEGRA_UART_MAXIMUM                      5
66
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
70
71 /* Tx transfer mode */
72 #define TEGRA_TX_PIO                            1
73 #define TEGRA_TX_DMA                            2
74
75 /**
76  * tegra_uart_chip_data: SOC specific data.
77  *
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.
82  */
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;
87 };
88
89 struct tegra_uart_port {
90         struct uart_port                        uport;
91         const struct tegra_uart_chip_data       *cdata;
92
93         struct clk                              *uart_clk;
94         struct reset_control                    *rst;
95         unsigned int                            current_baud;
96
97         /* Register shadow */
98         unsigned long                           fcr_shadow;
99         unsigned long                           mcr_shadow;
100         unsigned long                           lcr_shadow;
101         unsigned long                           ier_shadow;
102         bool                                    rts_active;
103
104         int                                     tx_in_progress;
105         unsigned int                            tx_bytes;
106
107         bool                                    enable_modem_interrupt;
108
109         bool                                    rx_timeout;
110         int                                     rx_in_progress;
111         int                                     symb_bit;
112
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;
125 };
126
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,
130                                         bool dma_to_memory);
131
132 static inline unsigned long tegra_uart_read(struct tegra_uart_port *tup,
133                 unsigned long reg)
134 {
135         return readl(tup->uport.membase + (reg << tup->uport.regshift));
136 }
137
138 static inline void tegra_uart_write(struct tegra_uart_port *tup, unsigned val,
139         unsigned long reg)
140 {
141         writel(val, tup->uport.membase + (reg << tup->uport.regshift));
142 }
143
144 static inline struct tegra_uart_port *to_tegra_uport(struct uart_port *u)
145 {
146         return container_of(u, struct tegra_uart_port, uport);
147 }
148
149 static unsigned int tegra_uart_get_mctrl(struct uart_port *u)
150 {
151         struct tegra_uart_port *tup = to_tegra_uport(u);
152
153         /*
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
160          *      CTS automatically.
161          */
162         if (tup->enable_modem_interrupt)
163                 return TIOCM_RI | TIOCM_CD | TIOCM_DSR | TIOCM_CTS;
164         return TIOCM_CTS;
165 }
166
167 static void set_rts(struct tegra_uart_port *tup, bool active)
168 {
169         unsigned long mcr;
170
171         mcr = tup->mcr_shadow;
172         if (active)
173                 mcr |= TEGRA_UART_MCR_RTS_EN;
174         else
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;
179         }
180 }
181
182 static void set_dtr(struct tegra_uart_port *tup, bool active)
183 {
184         unsigned long mcr;
185
186         mcr = tup->mcr_shadow;
187         if (active)
188                 mcr |= UART_MCR_DTR;
189         else
190                 mcr &= ~UART_MCR_DTR;
191         if (mcr != tup->mcr_shadow) {
192                 tegra_uart_write(tup, mcr, UART_MCR);
193                 tup->mcr_shadow = mcr;
194         }
195 }
196
197 static void set_loopbk(struct tegra_uart_port *tup, bool active)
198 {
199         unsigned long mcr = tup->mcr_shadow;
200
201         if (active)
202                 mcr |= UART_MCR_LOOP;
203         else
204                 mcr &= ~UART_MCR_LOOP;
205
206         if (mcr != tup->mcr_shadow) {
207                 tegra_uart_write(tup, mcr, UART_MCR);
208                 tup->mcr_shadow = mcr;
209         }
210 }
211
212 static void tegra_uart_set_mctrl(struct uart_port *u, unsigned int mctrl)
213 {
214         struct tegra_uart_port *tup = to_tegra_uport(u);
215         int enable;
216
217         tup->rts_active = !!(mctrl & TIOCM_RTS);
218         set_rts(tup, tup->rts_active);
219
220         enable = !!(mctrl & TIOCM_DTR);
221         set_dtr(tup, enable);
222
223         enable = !!(mctrl & TIOCM_LOOP);
224         set_loopbk(tup, enable);
225 }
226
227 static void tegra_uart_break_ctl(struct uart_port *u, int break_ctl)
228 {
229         struct tegra_uart_port *tup = to_tegra_uport(u);
230         unsigned long lcr;
231
232         lcr = tup->lcr_shadow;
233         if (break_ctl)
234                 lcr |= UART_LCR_SBC;
235         else
236                 lcr &= ~UART_LCR_SBC;
237         tegra_uart_write(tup, lcr, UART_LCR);
238         tup->lcr_shadow = lcr;
239 }
240
241 /**
242  * tegra_uart_wait_cycle_time: Wait for N UART clock periods
243  *
244  * @tup:        Tegra serial port data structure.
245  * @cycles:     Number of clock periods to wait.
246  *
247  * Tegra UARTs are clocked at 16X the baud/bit rate and hence the UART
248  * clock speed is 16X the current baud rate.
249  */
250 static void tegra_uart_wait_cycle_time(struct tegra_uart_port *tup,
251                                        unsigned int cycles)
252 {
253         if (tup->current_baud)
254                 udelay(DIV_ROUND_UP(cycles * 1000000, tup->current_baud * 16));
255 }
256
257 /* Wait for a symbol-time. */
258 static void tegra_uart_wait_sym_time(struct tegra_uart_port *tup,
259                 unsigned int syms)
260 {
261         if (tup->current_baud)
262                 udelay(DIV_ROUND_UP(syms * tup->symb_bit * 1000000,
263                         tup->current_baud));
264 }
265
266 static void tegra_uart_fifo_reset(struct tegra_uart_port *tup, u8 fcr_bits)
267 {
268         unsigned long fcr = tup->fcr_shadow;
269         unsigned int lsr, tmout = 10000;
270
271         if (tup->rts_active)
272                 set_rts(tup, false);
273
274         if (tup->cdata->allow_txfifo_reset_fifo_mode) {
275                 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
276                 tegra_uart_write(tup, fcr, UART_FCR);
277         } else {
278                 fcr &= ~UART_FCR_ENABLE_FIFO;
279                 tegra_uart_write(tup, fcr, UART_FCR);
280                 udelay(60);
281                 fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
282                 tegra_uart_write(tup, fcr, UART_FCR);
283                 fcr |= UART_FCR_ENABLE_FIFO;
284                 tegra_uart_write(tup, fcr, UART_FCR);
285         }
286
287         /* Dummy read to ensure the write is posted */
288         tegra_uart_read(tup, UART_SCR);
289
290         /*
291          * For all tegra devices (up to t210), there is a hardware issue that
292          * requires software to wait for 32 UART clock periods for the flush
293          * to propagate, otherwise data could be lost.
294          */
295         tegra_uart_wait_cycle_time(tup, 32);
296
297         do {
298                 lsr = tegra_uart_read(tup, UART_LSR);
299                 if ((lsr | UART_LSR_TEMT) && !(lsr & UART_LSR_DR))
300                         break;
301                 udelay(1);
302         } while (--tmout);
303
304         if (tup->rts_active)
305                 set_rts(tup, true);
306 }
307
308 static int tegra_set_baudrate(struct tegra_uart_port *tup, unsigned int baud)
309 {
310         unsigned long rate;
311         unsigned int divisor;
312         unsigned long lcr;
313         unsigned long flags;
314         int ret;
315
316         if (tup->current_baud == baud)
317                 return 0;
318
319         if (tup->cdata->support_clk_src_div) {
320                 rate = baud * 16;
321                 ret = clk_set_rate(tup->uart_clk, rate);
322                 if (ret < 0) {
323                         dev_err(tup->uport.dev,
324                                 "clk_set_rate() failed for rate %lu\n", rate);
325                         return ret;
326                 }
327                 divisor = 1;
328         } else {
329                 rate = clk_get_rate(tup->uart_clk);
330                 divisor = DIV_ROUND_CLOSEST(rate, baud * 16);
331         }
332
333         spin_lock_irqsave(&tup->uport.lock, flags);
334         lcr = tup->lcr_shadow;
335         lcr |= UART_LCR_DLAB;
336         tegra_uart_write(tup, lcr, UART_LCR);
337
338         tegra_uart_write(tup, divisor & 0xFF, UART_TX);
339         tegra_uart_write(tup, ((divisor >> 8) & 0xFF), UART_IER);
340
341         lcr &= ~UART_LCR_DLAB;
342         tegra_uart_write(tup, lcr, UART_LCR);
343
344         /* Dummy read to ensure the write is posted */
345         tegra_uart_read(tup, UART_SCR);
346         spin_unlock_irqrestore(&tup->uport.lock, flags);
347
348         tup->current_baud = baud;
349
350         /* wait two character intervals at new rate */
351         tegra_uart_wait_sym_time(tup, 2);
352         return 0;
353 }
354
355 static char tegra_uart_decode_rx_error(struct tegra_uart_port *tup,
356                         unsigned long lsr)
357 {
358         char flag = TTY_NORMAL;
359
360         if (unlikely(lsr & TEGRA_UART_LSR_ANY)) {
361                 if (lsr & UART_LSR_OE) {
362                         /* Overrrun error */
363                         flag = TTY_OVERRUN;
364                         tup->uport.icount.overrun++;
365                         dev_err(tup->uport.dev, "Got overrun errors\n");
366                 } else if (lsr & UART_LSR_PE) {
367                         /* Parity error */
368                         flag = TTY_PARITY;
369                         tup->uport.icount.parity++;
370                         dev_err(tup->uport.dev, "Got Parity errors\n");
371                 } else if (lsr & UART_LSR_FE) {
372                         flag = TTY_FRAME;
373                         tup->uport.icount.frame++;
374                         dev_err(tup->uport.dev, "Got frame errors\n");
375                 } else if (lsr & UART_LSR_BI) {
376                         dev_err(tup->uport.dev, "Got Break\n");
377                         tup->uport.icount.brk++;
378                         /* If FIFO read error without any data, reset Rx FIFO */
379                         if (!(lsr & UART_LSR_DR) && (lsr & UART_LSR_FIFOE))
380                                 tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_RCVR);
381                 }
382         }
383         return flag;
384 }
385
386 static int tegra_uart_request_port(struct uart_port *u)
387 {
388         return 0;
389 }
390
391 static void tegra_uart_release_port(struct uart_port *u)
392 {
393         /* Nothing to do here */
394 }
395
396 static void tegra_uart_fill_tx_fifo(struct tegra_uart_port *tup, int max_bytes)
397 {
398         struct circ_buf *xmit = &tup->uport.state->xmit;
399         int i;
400
401         for (i = 0; i < max_bytes; i++) {
402                 BUG_ON(uart_circ_empty(xmit));
403                 if (tup->cdata->tx_fifo_full_status) {
404                         unsigned long lsr = tegra_uart_read(tup, UART_LSR);
405                         if ((lsr & TEGRA_UART_LSR_TXFIFO_FULL))
406                                 break;
407                 }
408                 tegra_uart_write(tup, xmit->buf[xmit->tail], UART_TX);
409                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
410                 tup->uport.icount.tx++;
411         }
412 }
413
414 static void tegra_uart_start_pio_tx(struct tegra_uart_port *tup,
415                 unsigned int bytes)
416 {
417         if (bytes > TEGRA_UART_MIN_DMA)
418                 bytes = TEGRA_UART_MIN_DMA;
419
420         tup->tx_in_progress = TEGRA_UART_TX_PIO;
421         tup->tx_bytes = bytes;
422         tup->ier_shadow |= UART_IER_THRI;
423         tegra_uart_write(tup, tup->ier_shadow, UART_IER);
424 }
425
426 static void tegra_uart_tx_dma_complete(void *args)
427 {
428         struct tegra_uart_port *tup = args;
429         struct circ_buf *xmit = &tup->uport.state->xmit;
430         struct dma_tx_state state;
431         unsigned long flags;
432         unsigned int count;
433
434         dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
435         count = tup->tx_bytes_requested - state.residue;
436         async_tx_ack(tup->tx_dma_desc);
437         spin_lock_irqsave(&tup->uport.lock, flags);
438         xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
439         tup->tx_in_progress = 0;
440         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
441                 uart_write_wakeup(&tup->uport);
442         tegra_uart_start_next_tx(tup);
443         spin_unlock_irqrestore(&tup->uport.lock, flags);
444 }
445
446 static int tegra_uart_start_tx_dma(struct tegra_uart_port *tup,
447                 unsigned long count)
448 {
449         struct circ_buf *xmit = &tup->uport.state->xmit;
450         dma_addr_t tx_phys_addr;
451
452         dma_sync_single_for_device(tup->uport.dev, tup->tx_dma_buf_phys,
453                                 UART_XMIT_SIZE, DMA_TO_DEVICE);
454
455         tup->tx_bytes = count & ~(0xF);
456         tx_phys_addr = tup->tx_dma_buf_phys + xmit->tail;
457         tup->tx_dma_desc = dmaengine_prep_slave_single(tup->tx_dma_chan,
458                                 tx_phys_addr, tup->tx_bytes, DMA_MEM_TO_DEV,
459                                 DMA_PREP_INTERRUPT);
460         if (!tup->tx_dma_desc) {
461                 dev_err(tup->uport.dev, "Not able to get desc for Tx\n");
462                 return -EIO;
463         }
464
465         tup->tx_dma_desc->callback = tegra_uart_tx_dma_complete;
466         tup->tx_dma_desc->callback_param = tup;
467         tup->tx_in_progress = TEGRA_UART_TX_DMA;
468         tup->tx_bytes_requested = tup->tx_bytes;
469         tup->tx_cookie = dmaengine_submit(tup->tx_dma_desc);
470         dma_async_issue_pending(tup->tx_dma_chan);
471         return 0;
472 }
473
474 static void tegra_uart_start_next_tx(struct tegra_uart_port *tup)
475 {
476         unsigned long tail;
477         unsigned long count;
478         struct circ_buf *xmit = &tup->uport.state->xmit;
479
480         if (!tup->current_baud)
481                 return;
482
483         tail = (unsigned long)&xmit->buf[xmit->tail];
484         count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
485         if (!count)
486                 return;
487
488         if (count < TEGRA_UART_MIN_DMA)
489                 tegra_uart_start_pio_tx(tup, count);
490         else if (BYTES_TO_ALIGN(tail) > 0)
491                 tegra_uart_start_pio_tx(tup, BYTES_TO_ALIGN(tail));
492         else
493                 tegra_uart_start_tx_dma(tup, count);
494 }
495
496 /* Called by serial core driver with u->lock taken. */
497 static void tegra_uart_start_tx(struct uart_port *u)
498 {
499         struct tegra_uart_port *tup = to_tegra_uport(u);
500         struct circ_buf *xmit = &u->state->xmit;
501
502         if (!uart_circ_empty(xmit) && !tup->tx_in_progress)
503                 tegra_uart_start_next_tx(tup);
504 }
505
506 static unsigned int tegra_uart_tx_empty(struct uart_port *u)
507 {
508         struct tegra_uart_port *tup = to_tegra_uport(u);
509         unsigned int ret = 0;
510         unsigned long flags;
511
512         spin_lock_irqsave(&u->lock, flags);
513         if (!tup->tx_in_progress) {
514                 unsigned long lsr = tegra_uart_read(tup, UART_LSR);
515                 if ((lsr & TX_EMPTY_STATUS) == TX_EMPTY_STATUS)
516                         ret = TIOCSER_TEMT;
517         }
518         spin_unlock_irqrestore(&u->lock, flags);
519         return ret;
520 }
521
522 static void tegra_uart_stop_tx(struct uart_port *u)
523 {
524         struct tegra_uart_port *tup = to_tegra_uport(u);
525         struct circ_buf *xmit = &tup->uport.state->xmit;
526         struct dma_tx_state state;
527         unsigned int count;
528
529         if (tup->tx_in_progress != TEGRA_UART_TX_DMA)
530                 return;
531
532         dmaengine_terminate_all(tup->tx_dma_chan);
533         dmaengine_tx_status(tup->tx_dma_chan, tup->tx_cookie, &state);
534         count = tup->tx_bytes_requested - state.residue;
535         async_tx_ack(tup->tx_dma_desc);
536         xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
537         tup->tx_in_progress = 0;
538 }
539
540 static void tegra_uart_handle_tx_pio(struct tegra_uart_port *tup)
541 {
542         struct circ_buf *xmit = &tup->uport.state->xmit;
543
544         tegra_uart_fill_tx_fifo(tup, tup->tx_bytes);
545         tup->tx_in_progress = 0;
546         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
547                 uart_write_wakeup(&tup->uport);
548         tegra_uart_start_next_tx(tup);
549 }
550
551 static void tegra_uart_handle_rx_pio(struct tegra_uart_port *tup,
552                 struct tty_port *tty)
553 {
554         do {
555                 char flag = TTY_NORMAL;
556                 unsigned long lsr = 0;
557                 unsigned char ch;
558
559                 lsr = tegra_uart_read(tup, UART_LSR);
560                 if (!(lsr & UART_LSR_DR))
561                         break;
562
563                 flag = tegra_uart_decode_rx_error(tup, lsr);
564                 ch = (unsigned char) tegra_uart_read(tup, UART_RX);
565                 tup->uport.icount.rx++;
566
567                 if (!uart_handle_sysrq_char(&tup->uport, ch) && tty)
568                         tty_insert_flip_char(tty, ch, flag);
569
570                 if (tup->uport.ignore_status_mask & UART_LSR_DR)
571                         continue;
572         } while (1);
573 }
574
575 static void tegra_uart_copy_rx_to_tty(struct tegra_uart_port *tup,
576                                       struct tty_port *tty,
577                                       unsigned int count)
578 {
579         int copied;
580
581         /* If count is zero, then there is no data to be copied */
582         if (!count)
583                 return;
584
585         tup->uport.icount.rx += count;
586         if (!tty) {
587                 dev_err(tup->uport.dev, "No tty port\n");
588                 return;
589         }
590
591         if (tup->uport.ignore_status_mask & UART_LSR_DR)
592                 return;
593
594         dma_sync_single_for_cpu(tup->uport.dev, tup->rx_dma_buf_phys,
595                                 TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_FROM_DEVICE);
596         copied = tty_insert_flip_string(tty,
597                         ((unsigned char *)(tup->rx_dma_buf_virt)), count);
598         if (copied != count) {
599                 WARN_ON(1);
600                 dev_err(tup->uport.dev, "RxData copy to tty layer failed\n");
601         }
602         dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
603                                 TEGRA_UART_RX_DMA_BUFFER_SIZE, DMA_TO_DEVICE);
604 }
605
606 static void tegra_uart_rx_buffer_push(struct tegra_uart_port *tup,
607                                       unsigned int residue)
608 {
609         struct tty_port *port = &tup->uport.state->port;
610         struct tty_struct *tty = tty_port_tty_get(port);
611         unsigned int count;
612
613         async_tx_ack(tup->rx_dma_desc);
614         count = tup->rx_bytes_requested - residue;
615
616         /* If we are here, DMA is stopped */
617         tegra_uart_copy_rx_to_tty(tup, port, count);
618
619         tegra_uart_handle_rx_pio(tup, port);
620         if (tty) {
621                 tty_flip_buffer_push(port);
622                 tty_kref_put(tty);
623         }
624 }
625
626 static void tegra_uart_rx_dma_complete(void *args)
627 {
628         struct tegra_uart_port *tup = args;
629         struct uart_port *u = &tup->uport;
630         unsigned long flags;
631         struct dma_tx_state state;
632         enum dma_status status;
633
634         spin_lock_irqsave(&u->lock, flags);
635
636         status = dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
637
638         if (status == DMA_IN_PROGRESS) {
639                 dev_dbg(tup->uport.dev, "RX DMA is in progress\n");
640                 goto done;
641         }
642
643         /* Deactivate flow control to stop sender */
644         if (tup->rts_active)
645                 set_rts(tup, false);
646
647         tegra_uart_rx_buffer_push(tup, 0);
648         tegra_uart_start_rx_dma(tup);
649
650         /* Activate flow control to start transfer */
651         if (tup->rts_active)
652                 set_rts(tup, true);
653
654 done:
655         spin_unlock_irqrestore(&u->lock, flags);
656 }
657
658 static void tegra_uart_handle_rx_dma(struct tegra_uart_port *tup)
659 {
660         struct dma_tx_state state;
661
662         /* Deactivate flow control to stop sender */
663         if (tup->rts_active)
664                 set_rts(tup, false);
665
666         dmaengine_terminate_all(tup->rx_dma_chan);
667         dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
668         tegra_uart_rx_buffer_push(tup, state.residue);
669         tegra_uart_start_rx_dma(tup);
670
671         if (tup->rts_active)
672                 set_rts(tup, true);
673 }
674
675 static int tegra_uart_start_rx_dma(struct tegra_uart_port *tup)
676 {
677         unsigned int count = TEGRA_UART_RX_DMA_BUFFER_SIZE;
678
679         tup->rx_dma_desc = dmaengine_prep_slave_single(tup->rx_dma_chan,
680                                 tup->rx_dma_buf_phys, count, DMA_DEV_TO_MEM,
681                                 DMA_PREP_INTERRUPT);
682         if (!tup->rx_dma_desc) {
683                 dev_err(tup->uport.dev, "Not able to get desc for Rx\n");
684                 return -EIO;
685         }
686
687         tup->rx_dma_desc->callback = tegra_uart_rx_dma_complete;
688         tup->rx_dma_desc->callback_param = tup;
689         dma_sync_single_for_device(tup->uport.dev, tup->rx_dma_buf_phys,
690                                 count, DMA_TO_DEVICE);
691         tup->rx_bytes_requested = count;
692         tup->rx_cookie = dmaengine_submit(tup->rx_dma_desc);
693         dma_async_issue_pending(tup->rx_dma_chan);
694         return 0;
695 }
696
697 static void tegra_uart_handle_modem_signal_change(struct uart_port *u)
698 {
699         struct tegra_uart_port *tup = to_tegra_uport(u);
700         unsigned long msr;
701
702         msr = tegra_uart_read(tup, UART_MSR);
703         if (!(msr & UART_MSR_ANY_DELTA))
704                 return;
705
706         if (msr & UART_MSR_TERI)
707                 tup->uport.icount.rng++;
708         if (msr & UART_MSR_DDSR)
709                 tup->uport.icount.dsr++;
710         /* We may only get DDCD when HW init and reset */
711         if (msr & UART_MSR_DDCD)
712                 uart_handle_dcd_change(&tup->uport, msr & UART_MSR_DCD);
713         /* Will start/stop_tx accordingly */
714         if (msr & UART_MSR_DCTS)
715                 uart_handle_cts_change(&tup->uport, msr & UART_MSR_CTS);
716 }
717
718 static irqreturn_t tegra_uart_isr(int irq, void *data)
719 {
720         struct tegra_uart_port *tup = data;
721         struct uart_port *u = &tup->uport;
722         unsigned long iir;
723         unsigned long ier;
724         bool is_rx_int = false;
725         unsigned long flags;
726
727         spin_lock_irqsave(&u->lock, flags);
728         while (1) {
729                 iir = tegra_uart_read(tup, UART_IIR);
730                 if (iir & UART_IIR_NO_INT) {
731                         if (is_rx_int) {
732                                 tegra_uart_handle_rx_dma(tup);
733                                 if (tup->rx_in_progress) {
734                                         ier = tup->ier_shadow;
735                                         ier |= (UART_IER_RLSI | UART_IER_RTOIE |
736                                                 TEGRA_UART_IER_EORD);
737                                         tup->ier_shadow = ier;
738                                         tegra_uart_write(tup, ier, UART_IER);
739                                 }
740                         }
741                         spin_unlock_irqrestore(&u->lock, flags);
742                         return IRQ_HANDLED;
743                 }
744
745                 switch ((iir >> 1) & 0x7) {
746                 case 0: /* Modem signal change interrupt */
747                         tegra_uart_handle_modem_signal_change(u);
748                         break;
749
750                 case 1: /* Transmit interrupt only triggered when using PIO */
751                         tup->ier_shadow &= ~UART_IER_THRI;
752                         tegra_uart_write(tup, tup->ier_shadow, UART_IER);
753                         tegra_uart_handle_tx_pio(tup);
754                         break;
755
756                 case 4: /* End of data */
757                 case 6: /* Rx timeout */
758                 case 2: /* Receive */
759                         if (!is_rx_int) {
760                                 is_rx_int = true;
761                                 /* Disable Rx interrupts */
762                                 ier = tup->ier_shadow;
763                                 ier |= UART_IER_RDI;
764                                 tegra_uart_write(tup, ier, UART_IER);
765                                 ier &= ~(UART_IER_RDI | UART_IER_RLSI |
766                                         UART_IER_RTOIE | TEGRA_UART_IER_EORD);
767                                 tup->ier_shadow = ier;
768                                 tegra_uart_write(tup, ier, UART_IER);
769                         }
770                         break;
771
772                 case 3: /* Receive error */
773                         tegra_uart_decode_rx_error(tup,
774                                         tegra_uart_read(tup, UART_LSR));
775                         break;
776
777                 case 5: /* break nothing to handle */
778                 case 7: /* break nothing to handle */
779                         break;
780                 }
781         }
782 }
783
784 static void tegra_uart_stop_rx(struct uart_port *u)
785 {
786         struct tegra_uart_port *tup = to_tegra_uport(u);
787         struct dma_tx_state state;
788         unsigned long ier;
789
790         if (tup->rts_active)
791                 set_rts(tup, false);
792
793         if (!tup->rx_in_progress)
794                 return;
795
796         tegra_uart_wait_sym_time(tup, 1); /* wait one character interval */
797
798         ier = tup->ier_shadow;
799         ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE |
800                                         TEGRA_UART_IER_EORD);
801         tup->ier_shadow = ier;
802         tegra_uart_write(tup, ier, UART_IER);
803         tup->rx_in_progress = 0;
804         dmaengine_terminate_all(tup->rx_dma_chan);
805         dmaengine_tx_status(tup->rx_dma_chan, tup->rx_cookie, &state);
806         tegra_uart_rx_buffer_push(tup, state.residue);
807 }
808
809 static void tegra_uart_hw_deinit(struct tegra_uart_port *tup)
810 {
811         unsigned long flags;
812         unsigned long char_time = DIV_ROUND_UP(10000000, tup->current_baud);
813         unsigned long fifo_empty_time = tup->uport.fifosize * char_time;
814         unsigned long wait_time;
815         unsigned long lsr;
816         unsigned long msr;
817         unsigned long mcr;
818
819         /* Disable interrupts */
820         tegra_uart_write(tup, 0, UART_IER);
821
822         lsr = tegra_uart_read(tup, UART_LSR);
823         if ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
824                 msr = tegra_uart_read(tup, UART_MSR);
825                 mcr = tegra_uart_read(tup, UART_MCR);
826                 if ((mcr & TEGRA_UART_MCR_CTS_EN) && (msr & UART_MSR_CTS))
827                         dev_err(tup->uport.dev,
828                                 "Tx Fifo not empty, CTS disabled, waiting\n");
829
830                 /* Wait for Tx fifo to be empty */
831                 while ((lsr & UART_LSR_TEMT) != UART_LSR_TEMT) {
832                         wait_time = min(fifo_empty_time, 100lu);
833                         udelay(wait_time);
834                         fifo_empty_time -= wait_time;
835                         if (!fifo_empty_time) {
836                                 msr = tegra_uart_read(tup, UART_MSR);
837                                 mcr = tegra_uart_read(tup, UART_MCR);
838                                 if ((mcr & TEGRA_UART_MCR_CTS_EN) &&
839                                         (msr & UART_MSR_CTS))
840                                         dev_err(tup->uport.dev,
841                                                 "Slave not ready\n");
842                                 break;
843                         }
844                         lsr = tegra_uart_read(tup, UART_LSR);
845                 }
846         }
847
848         spin_lock_irqsave(&tup->uport.lock, flags);
849         /* Reset the Rx and Tx FIFOs */
850         tegra_uart_fifo_reset(tup, UART_FCR_CLEAR_XMIT | UART_FCR_CLEAR_RCVR);
851         tup->current_baud = 0;
852         spin_unlock_irqrestore(&tup->uport.lock, flags);
853
854         tup->rx_in_progress = 0;
855         tup->tx_in_progress = 0;
856
857         tegra_uart_dma_channel_free(tup, true);
858         tegra_uart_dma_channel_free(tup, false);
859
860         clk_disable_unprepare(tup->uart_clk);
861 }
862
863 static int tegra_uart_hw_init(struct tegra_uart_port *tup)
864 {
865         int ret;
866
867         tup->fcr_shadow = 0;
868         tup->mcr_shadow = 0;
869         tup->lcr_shadow = 0;
870         tup->ier_shadow = 0;
871         tup->current_baud = 0;
872
873         clk_prepare_enable(tup->uart_clk);
874
875         /* Reset the UART controller to clear all previous status.*/
876         reset_control_assert(tup->rst);
877         udelay(10);
878         reset_control_deassert(tup->rst);
879
880         tup->rx_in_progress = 0;
881         tup->tx_in_progress = 0;
882
883         /*
884          * Set the trigger level
885          *
886          * For PIO mode:
887          *
888          * For receive, this will interrupt the CPU after that many number of
889          * bytes are received, for the remaining bytes the receive timeout
890          * interrupt is received. Rx high watermark is set to 4.
891          *
892          * For transmit, if the trasnmit interrupt is enabled, this will
893          * interrupt the CPU when the number of entries in the FIFO reaches the
894          * low watermark. Tx low watermark is set to 16 bytes.
895          *
896          * For DMA mode:
897          *
898          * Set the Tx trigger to 16. This should match the DMA burst size that
899          * programmed in the DMA registers.
900          */
901         tup->fcr_shadow = UART_FCR_ENABLE_FIFO;
902         tup->fcr_shadow |= UART_FCR_R_TRIG_01;
903         tup->fcr_shadow |= TEGRA_UART_TX_TRIG_16B;
904         tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
905
906         /* Dummy read to ensure the write is posted */
907         tegra_uart_read(tup, UART_SCR);
908
909         /*
910          * For all tegra devices (up to t210), there is a hardware issue that
911          * requires software to wait for 3 UART clock periods after enabling
912          * the TX fifo, otherwise data could be lost.
913          */
914         tegra_uart_wait_cycle_time(tup, 3);
915
916         /*
917          * Initialize the UART with default configuration
918          * (115200, N, 8, 1) so that the receive DMA buffer may be
919          * enqueued
920          */
921         tup->lcr_shadow = TEGRA_UART_DEFAULT_LSR;
922         tegra_set_baudrate(tup, TEGRA_UART_DEFAULT_BAUD);
923         tup->fcr_shadow |= UART_FCR_DMA_SELECT;
924         tegra_uart_write(tup, tup->fcr_shadow, UART_FCR);
925
926         ret = tegra_uart_start_rx_dma(tup);
927         if (ret < 0) {
928                 dev_err(tup->uport.dev, "Not able to start Rx DMA\n");
929                 return ret;
930         }
931         tup->rx_in_progress = 1;
932
933         /*
934          * Enable IE_RXS for the receive status interrupts like line errros.
935          * Enable IE_RX_TIMEOUT to get the bytes which cannot be DMA'd.
936          *
937          * If using DMA mode, enable EORD instead of receive interrupt which
938          * will interrupt after the UART is done with the receive instead of
939          * the interrupt when the FIFO "threshold" is reached.
940          *
941          * EORD is different interrupt than RX_TIMEOUT - RX_TIMEOUT occurs when
942          * the DATA is sitting in the FIFO and couldn't be transferred to the
943          * DMA as the DMA size alignment (4 bytes) is not met. EORD will be
944          * triggered when there is a pause of the incomming data stream for 4
945          * characters long.
946          *
947          * For pauses in the data which is not aligned to 4 bytes, we get
948          * both the EORD as well as RX_TIMEOUT - SW sees RX_TIMEOUT first
949          * then the EORD.
950          */
951         tup->ier_shadow = UART_IER_RLSI | UART_IER_RTOIE | TEGRA_UART_IER_EORD;
952         tegra_uart_write(tup, tup->ier_shadow, UART_IER);
953         return 0;
954 }
955
956 static void tegra_uart_dma_channel_free(struct tegra_uart_port *tup,
957                 bool dma_to_memory)
958 {
959         if (dma_to_memory) {
960                 dmaengine_terminate_all(tup->rx_dma_chan);
961                 dma_release_channel(tup->rx_dma_chan);
962                 dma_free_coherent(tup->uport.dev, TEGRA_UART_RX_DMA_BUFFER_SIZE,
963                                 tup->rx_dma_buf_virt, tup->rx_dma_buf_phys);
964                 tup->rx_dma_chan = NULL;
965                 tup->rx_dma_buf_phys = 0;
966                 tup->rx_dma_buf_virt = NULL;
967         } else {
968                 dmaengine_terminate_all(tup->tx_dma_chan);
969                 dma_release_channel(tup->tx_dma_chan);
970                 dma_unmap_single(tup->uport.dev, tup->tx_dma_buf_phys,
971                         UART_XMIT_SIZE, DMA_TO_DEVICE);
972                 tup->tx_dma_chan = NULL;
973                 tup->tx_dma_buf_phys = 0;
974                 tup->tx_dma_buf_virt = NULL;
975         }
976 }
977
978 static int tegra_uart_dma_channel_allocate(struct tegra_uart_port *tup,
979                         bool dma_to_memory)
980 {
981         struct dma_chan *dma_chan;
982         unsigned char *dma_buf;
983         dma_addr_t dma_phys;
984         int ret;
985         struct dma_slave_config dma_sconfig;
986
987         dma_chan = dma_request_slave_channel_reason(tup->uport.dev,
988                                                 dma_to_memory ? "rx" : "tx");
989         if (IS_ERR(dma_chan)) {
990                 ret = PTR_ERR(dma_chan);
991                 dev_err(tup->uport.dev,
992                         "DMA channel alloc failed: %d\n", ret);
993                 return ret;
994         }
995
996         if (dma_to_memory) {
997                 dma_buf = dma_alloc_coherent(tup->uport.dev,
998                                 TEGRA_UART_RX_DMA_BUFFER_SIZE,
999                                  &dma_phys, GFP_KERNEL);
1000                 if (!dma_buf) {
1001                         dev_err(tup->uport.dev,
1002                                 "Not able to allocate the dma buffer\n");
1003                         dma_release_channel(dma_chan);
1004                         return -ENOMEM;
1005                 }
1006                 dma_sconfig.src_addr = tup->uport.mapbase;
1007                 dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1008                 dma_sconfig.src_maxburst = 4;
1009                 tup->rx_dma_chan = dma_chan;
1010                 tup->rx_dma_buf_virt = dma_buf;
1011                 tup->rx_dma_buf_phys = dma_phys;
1012         } else {
1013                 dma_phys = dma_map_single(tup->uport.dev,
1014                         tup->uport.state->xmit.buf, UART_XMIT_SIZE,
1015                         DMA_TO_DEVICE);
1016                 if (dma_mapping_error(tup->uport.dev, dma_phys)) {
1017                         dev_err(tup->uport.dev, "dma_map_single tx failed\n");
1018                         dma_release_channel(dma_chan);
1019                         return -ENOMEM;
1020                 }
1021                 dma_buf = tup->uport.state->xmit.buf;
1022                 dma_sconfig.dst_addr = tup->uport.mapbase;
1023                 dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
1024                 dma_sconfig.dst_maxburst = 16;
1025                 tup->tx_dma_chan = dma_chan;
1026                 tup->tx_dma_buf_virt = dma_buf;
1027                 tup->tx_dma_buf_phys = dma_phys;
1028         }
1029
1030         ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
1031         if (ret < 0) {
1032                 dev_err(tup->uport.dev,
1033                         "Dma slave config failed, err = %d\n", ret);
1034                 tegra_uart_dma_channel_free(tup, dma_to_memory);
1035                 return ret;
1036         }
1037
1038         return 0;
1039 }
1040
1041 static int tegra_uart_startup(struct uart_port *u)
1042 {
1043         struct tegra_uart_port *tup = to_tegra_uport(u);
1044         int ret;
1045
1046         ret = tegra_uart_dma_channel_allocate(tup, false);
1047         if (ret < 0) {
1048                 dev_err(u->dev, "Tx Dma allocation failed, err = %d\n", ret);
1049                 return ret;
1050         }
1051
1052         ret = tegra_uart_dma_channel_allocate(tup, true);
1053         if (ret < 0) {
1054                 dev_err(u->dev, "Rx Dma allocation failed, err = %d\n", ret);
1055                 goto fail_rx_dma;
1056         }
1057
1058         ret = tegra_uart_hw_init(tup);
1059         if (ret < 0) {
1060                 dev_err(u->dev, "Uart HW init failed, err = %d\n", ret);
1061                 goto fail_hw_init;
1062         }
1063
1064         ret = request_irq(u->irq, tegra_uart_isr, 0,
1065                                 dev_name(u->dev), tup);
1066         if (ret < 0) {
1067                 dev_err(u->dev, "Failed to register ISR for IRQ %d\n", u->irq);
1068                 goto fail_hw_init;
1069         }
1070         return 0;
1071
1072 fail_hw_init:
1073         tegra_uart_dma_channel_free(tup, true);
1074 fail_rx_dma:
1075         tegra_uart_dma_channel_free(tup, false);
1076         return ret;
1077 }
1078
1079 /*
1080  * Flush any TX data submitted for DMA and PIO. Called when the
1081  * TX circular buffer is reset.
1082  */
1083 static void tegra_uart_flush_buffer(struct uart_port *u)
1084 {
1085         struct tegra_uart_port *tup = to_tegra_uport(u);
1086
1087         tup->tx_bytes = 0;
1088         if (tup->tx_dma_chan)
1089                 dmaengine_terminate_all(tup->tx_dma_chan);
1090 }
1091
1092 static void tegra_uart_shutdown(struct uart_port *u)
1093 {
1094         struct tegra_uart_port *tup = to_tegra_uport(u);
1095
1096         tegra_uart_hw_deinit(tup);
1097         free_irq(u->irq, tup);
1098 }
1099
1100 static void tegra_uart_enable_ms(struct uart_port *u)
1101 {
1102         struct tegra_uart_port *tup = to_tegra_uport(u);
1103
1104         if (tup->enable_modem_interrupt) {
1105                 tup->ier_shadow |= UART_IER_MSI;
1106                 tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1107         }
1108 }
1109
1110 static void tegra_uart_set_termios(struct uart_port *u,
1111                 struct ktermios *termios, struct ktermios *oldtermios)
1112 {
1113         struct tegra_uart_port *tup = to_tegra_uport(u);
1114         unsigned int baud;
1115         unsigned long flags;
1116         unsigned int lcr;
1117         int symb_bit = 1;
1118         struct clk *parent_clk = clk_get_parent(tup->uart_clk);
1119         unsigned long parent_clk_rate = clk_get_rate(parent_clk);
1120         int max_divider = (tup->cdata->support_clk_src_div) ? 0x7FFF : 0xFFFF;
1121
1122         max_divider *= 16;
1123         spin_lock_irqsave(&u->lock, flags);
1124
1125         /* Changing configuration, it is safe to stop any rx now */
1126         if (tup->rts_active)
1127                 set_rts(tup, false);
1128
1129         /* Clear all interrupts as configuration is going to be changed */
1130         tegra_uart_write(tup, tup->ier_shadow | UART_IER_RDI, UART_IER);
1131         tegra_uart_read(tup, UART_IER);
1132         tegra_uart_write(tup, 0, UART_IER);
1133         tegra_uart_read(tup, UART_IER);
1134
1135         /* Parity */
1136         lcr = tup->lcr_shadow;
1137         lcr &= ~UART_LCR_PARITY;
1138
1139         /* CMSPAR isn't supported by this driver */
1140         termios->c_cflag &= ~CMSPAR;
1141
1142         if ((termios->c_cflag & PARENB) == PARENB) {
1143                 symb_bit++;
1144                 if (termios->c_cflag & PARODD) {
1145                         lcr |= UART_LCR_PARITY;
1146                         lcr &= ~UART_LCR_EPAR;
1147                         lcr &= ~UART_LCR_SPAR;
1148                 } else {
1149                         lcr |= UART_LCR_PARITY;
1150                         lcr |= UART_LCR_EPAR;
1151                         lcr &= ~UART_LCR_SPAR;
1152                 }
1153         }
1154
1155         lcr &= ~UART_LCR_WLEN8;
1156         switch (termios->c_cflag & CSIZE) {
1157         case CS5:
1158                 lcr |= UART_LCR_WLEN5;
1159                 symb_bit += 5;
1160                 break;
1161         case CS6:
1162                 lcr |= UART_LCR_WLEN6;
1163                 symb_bit += 6;
1164                 break;
1165         case CS7:
1166                 lcr |= UART_LCR_WLEN7;
1167                 symb_bit += 7;
1168                 break;
1169         default:
1170                 lcr |= UART_LCR_WLEN8;
1171                 symb_bit += 8;
1172                 break;
1173         }
1174
1175         /* Stop bits */
1176         if (termios->c_cflag & CSTOPB) {
1177                 lcr |= UART_LCR_STOP;
1178                 symb_bit += 2;
1179         } else {
1180                 lcr &= ~UART_LCR_STOP;
1181                 symb_bit++;
1182         }
1183
1184         tegra_uart_write(tup, lcr, UART_LCR);
1185         tup->lcr_shadow = lcr;
1186         tup->symb_bit = symb_bit;
1187
1188         /* Baud rate. */
1189         baud = uart_get_baud_rate(u, termios, oldtermios,
1190                         parent_clk_rate/max_divider,
1191                         parent_clk_rate/16);
1192         spin_unlock_irqrestore(&u->lock, flags);
1193         tegra_set_baudrate(tup, baud);
1194         if (tty_termios_baud_rate(termios))
1195                 tty_termios_encode_baud_rate(termios, baud, baud);
1196         spin_lock_irqsave(&u->lock, flags);
1197
1198         /* Flow control */
1199         if (termios->c_cflag & CRTSCTS) {
1200                 tup->mcr_shadow |= TEGRA_UART_MCR_CTS_EN;
1201                 tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
1202                 tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
1203                 /* if top layer has asked to set rts active then do so here */
1204                 if (tup->rts_active)
1205                         set_rts(tup, true);
1206         } else {
1207                 tup->mcr_shadow &= ~TEGRA_UART_MCR_CTS_EN;
1208                 tup->mcr_shadow &= ~TEGRA_UART_MCR_RTS_EN;
1209                 tegra_uart_write(tup, tup->mcr_shadow, UART_MCR);
1210         }
1211
1212         /* update the port timeout based on new settings */
1213         uart_update_timeout(u, termios->c_cflag, baud);
1214
1215         /* Make sure all writes have completed */
1216         tegra_uart_read(tup, UART_IER);
1217
1218         /* Re-enable interrupt */
1219         tegra_uart_write(tup, tup->ier_shadow, UART_IER);
1220         tegra_uart_read(tup, UART_IER);
1221
1222         tup->uport.ignore_status_mask = 0;
1223         /* Ignore all characters if CREAD is not set */
1224         if ((termios->c_cflag & CREAD) == 0)
1225                 tup->uport.ignore_status_mask |= UART_LSR_DR;
1226
1227         spin_unlock_irqrestore(&u->lock, flags);
1228 }
1229
1230 static const char *tegra_uart_type(struct uart_port *u)
1231 {
1232         return TEGRA_UART_TYPE;
1233 }
1234
1235 static const struct uart_ops tegra_uart_ops = {
1236         .tx_empty       = tegra_uart_tx_empty,
1237         .set_mctrl      = tegra_uart_set_mctrl,
1238         .get_mctrl      = tegra_uart_get_mctrl,
1239         .stop_tx        = tegra_uart_stop_tx,
1240         .start_tx       = tegra_uart_start_tx,
1241         .stop_rx        = tegra_uart_stop_rx,
1242         .flush_buffer   = tegra_uart_flush_buffer,
1243         .enable_ms      = tegra_uart_enable_ms,
1244         .break_ctl      = tegra_uart_break_ctl,
1245         .startup        = tegra_uart_startup,
1246         .shutdown       = tegra_uart_shutdown,
1247         .set_termios    = tegra_uart_set_termios,
1248         .type           = tegra_uart_type,
1249         .request_port   = tegra_uart_request_port,
1250         .release_port   = tegra_uart_release_port,
1251 };
1252
1253 static struct uart_driver tegra_uart_driver = {
1254         .owner          = THIS_MODULE,
1255         .driver_name    = "tegra_hsuart",
1256         .dev_name       = "ttyTHS",
1257         .cons           = NULL,
1258         .nr             = TEGRA_UART_MAXIMUM,
1259 };
1260
1261 static int tegra_uart_parse_dt(struct platform_device *pdev,
1262         struct tegra_uart_port *tup)
1263 {
1264         struct device_node *np = pdev->dev.of_node;
1265         int port;
1266
1267         port = of_alias_get_id(np, "serial");
1268         if (port < 0) {
1269                 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", port);
1270                 return port;
1271         }
1272         tup->uport.line = port;
1273
1274         tup->enable_modem_interrupt = of_property_read_bool(np,
1275                                         "nvidia,enable-modem-interrupt");
1276         return 0;
1277 }
1278
1279 static struct tegra_uart_chip_data tegra20_uart_chip_data = {
1280         .tx_fifo_full_status            = false,
1281         .allow_txfifo_reset_fifo_mode   = true,
1282         .support_clk_src_div            = false,
1283 };
1284
1285 static struct tegra_uart_chip_data tegra30_uart_chip_data = {
1286         .tx_fifo_full_status            = true,
1287         .allow_txfifo_reset_fifo_mode   = false,
1288         .support_clk_src_div            = true,
1289 };
1290
1291 static const struct of_device_id tegra_uart_of_match[] = {
1292         {
1293                 .compatible     = "nvidia,tegra30-hsuart",
1294                 .data           = &tegra30_uart_chip_data,
1295         }, {
1296                 .compatible     = "nvidia,tegra20-hsuart",
1297                 .data           = &tegra20_uart_chip_data,
1298         }, {
1299         },
1300 };
1301 MODULE_DEVICE_TABLE(of, tegra_uart_of_match);
1302
1303 static int tegra_uart_probe(struct platform_device *pdev)
1304 {
1305         struct tegra_uart_port *tup;
1306         struct uart_port *u;
1307         struct resource *resource;
1308         int ret;
1309         const struct tegra_uart_chip_data *cdata;
1310         const struct of_device_id *match;
1311
1312         match = of_match_device(tegra_uart_of_match, &pdev->dev);
1313         if (!match) {
1314                 dev_err(&pdev->dev, "Error: No device match found\n");
1315                 return -ENODEV;
1316         }
1317         cdata = match->data;
1318
1319         tup = devm_kzalloc(&pdev->dev, sizeof(*tup), GFP_KERNEL);
1320         if (!tup) {
1321                 dev_err(&pdev->dev, "Failed to allocate memory for tup\n");
1322                 return -ENOMEM;
1323         }
1324
1325         ret = tegra_uart_parse_dt(pdev, tup);
1326         if (ret < 0)
1327                 return ret;
1328
1329         u = &tup->uport;
1330         u->dev = &pdev->dev;
1331         u->ops = &tegra_uart_ops;
1332         u->type = PORT_TEGRA;
1333         u->fifosize = 32;
1334         tup->cdata = cdata;
1335
1336         platform_set_drvdata(pdev, tup);
1337         resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1338         if (!resource) {
1339                 dev_err(&pdev->dev, "No IO memory resource\n");
1340                 return -ENODEV;
1341         }
1342
1343         u->mapbase = resource->start;
1344         u->membase = devm_ioremap_resource(&pdev->dev, resource);
1345         if (IS_ERR(u->membase))
1346                 return PTR_ERR(u->membase);
1347
1348         tup->uart_clk = devm_clk_get(&pdev->dev, NULL);
1349         if (IS_ERR(tup->uart_clk)) {
1350                 dev_err(&pdev->dev, "Couldn't get the clock\n");
1351                 return PTR_ERR(tup->uart_clk);
1352         }
1353
1354         tup->rst = devm_reset_control_get_exclusive(&pdev->dev, "serial");
1355         if (IS_ERR(tup->rst)) {
1356                 dev_err(&pdev->dev, "Couldn't get the reset\n");
1357                 return PTR_ERR(tup->rst);
1358         }
1359
1360         u->iotype = UPIO_MEM32;
1361         ret = platform_get_irq(pdev, 0);
1362         if (ret < 0)
1363                 return ret;
1364         u->irq = ret;
1365         u->regshift = 2;
1366         ret = uart_add_one_port(&tegra_uart_driver, u);
1367         if (ret < 0) {
1368                 dev_err(&pdev->dev, "Failed to add uart port, err %d\n", ret);
1369                 return ret;
1370         }
1371         return ret;
1372 }
1373
1374 static int tegra_uart_remove(struct platform_device *pdev)
1375 {
1376         struct tegra_uart_port *tup = platform_get_drvdata(pdev);
1377         struct uart_port *u = &tup->uport;
1378
1379         uart_remove_one_port(&tegra_uart_driver, u);
1380         return 0;
1381 }
1382
1383 #ifdef CONFIG_PM_SLEEP
1384 static int tegra_uart_suspend(struct device *dev)
1385 {
1386         struct tegra_uart_port *tup = dev_get_drvdata(dev);
1387         struct uart_port *u = &tup->uport;
1388
1389         return uart_suspend_port(&tegra_uart_driver, u);
1390 }
1391
1392 static int tegra_uart_resume(struct device *dev)
1393 {
1394         struct tegra_uart_port *tup = dev_get_drvdata(dev);
1395         struct uart_port *u = &tup->uport;
1396
1397         return uart_resume_port(&tegra_uart_driver, u);
1398 }
1399 #endif
1400
1401 static const struct dev_pm_ops tegra_uart_pm_ops = {
1402         SET_SYSTEM_SLEEP_PM_OPS(tegra_uart_suspend, tegra_uart_resume)
1403 };
1404
1405 static struct platform_driver tegra_uart_platform_driver = {
1406         .probe          = tegra_uart_probe,
1407         .remove         = tegra_uart_remove,
1408         .driver         = {
1409                 .name   = "serial-tegra",
1410                 .of_match_table = tegra_uart_of_match,
1411                 .pm     = &tegra_uart_pm_ops,
1412         },
1413 };
1414
1415 static int __init tegra_uart_init(void)
1416 {
1417         int ret;
1418
1419         ret = uart_register_driver(&tegra_uart_driver);
1420         if (ret < 0) {
1421                 pr_err("Could not register %s driver\n",
1422                         tegra_uart_driver.driver_name);
1423                 return ret;
1424         }
1425
1426         ret = platform_driver_register(&tegra_uart_platform_driver);
1427         if (ret < 0) {
1428                 pr_err("Uart platform driver register failed, e = %d\n", ret);
1429                 uart_unregister_driver(&tegra_uart_driver);
1430                 return ret;
1431         }
1432         return 0;
1433 }
1434
1435 static void __exit tegra_uart_exit(void)
1436 {
1437         pr_info("Unloading tegra uart driver\n");
1438         platform_driver_unregister(&tegra_uart_platform_driver);
1439         uart_unregister_driver(&tegra_uart_driver);
1440 }
1441
1442 module_init(tegra_uart_init);
1443 module_exit(tegra_uart_exit);
1444
1445 MODULE_ALIAS("platform:serial-tegra");
1446 MODULE_DESCRIPTION("High speed UART driver for tegra chipset");
1447 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1448 MODULE_LICENSE("GPL v2");