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