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