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