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