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