Merge remote branch 'common/android-2.6.36' into android-tegra-2.6.36
[firefly-linux-kernel-4.4.55.git] / drivers / serial / tegra_hsuart.c
1 /*
2  * drivers/serial/tegra_hsuart.c
3  *
4  * High-speed serial driver for NVIDIA Tegra SoCs
5  *
6  * Copyright (C) 2009 NVIDIA Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2 of the License, or (at your
11  * option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21  */
22
23 /*#define DEBUG           1*/
24 /*#define VERBOSE_DEBUG   1*/
25
26 #include <linux/module.h>
27 #include <linux/serial.h>
28 #include <linux/serial_core.h>
29 #include <linux/platform_device.h>
30 #include <linux/io.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/dmapool.h>
33 #include <linux/termios.h>
34 #include <linux/irq.h>
35 #include <linux/delay.h>
36 #include <linux/clk.h>
37 #include <linux/string.h>
38 #include <linux/pagemap.h>
39 #include <linux/serial_reg.h>
40 #include <linux/serial_8250.h>
41 #include <linux/debugfs.h>
42 #include <linux/slab.h>
43 #include <linux/workqueue.h>
44 #include <mach/dma.h>
45 #include <mach/clk.h>
46
47 #define TX_EMPTY_STATUS (UART_LSR_TEMT | UART_LSR_THRE)
48
49 #define BYTES_TO_ALIGN(x) ((unsigned long)(ALIGN((x), sizeof(u32))) - \
50         (unsigned long)(x))
51
52 #define UART_RX_DMA_BUFFER_SIZE    (2048*4)
53
54 #define UART_LSR_FIFOE          0x80
55 #define UART_IER_EORD           0x20
56 #define UART_MCR_RTS_EN         0x40
57 #define UART_MCR_CTS_EN         0x20
58 #define UART_LSR_ANY            (UART_LSR_OE | UART_LSR_BI | \
59                                 UART_LSR_PE | UART_LSR_FE)
60
61 #define TX_FORCE_PIO 0
62 #define RX_FORCE_PIO 0
63
64 const int dma_req_sel[] = {
65         TEGRA_DMA_REQ_SEL_UARTA,
66         TEGRA_DMA_REQ_SEL_UARTB,
67         TEGRA_DMA_REQ_SEL_UARTC,
68         TEGRA_DMA_REQ_SEL_UARTD,
69         TEGRA_DMA_REQ_SEL_UARTE,
70 };
71
72 #define TEGRA_TX_PIO                    1
73 #define TEGRA_TX_DMA                    2
74
75 #define TEGRA_UART_MIN_DMA              16
76 #define TEGRA_UART_FIFO_SIZE            8
77
78 /* Tx fifo trigger level setting in tegra uart is in
79  * reverse way then conventional uart */
80 #define TEGRA_UART_TX_TRIG_16B 0x00
81 #define TEGRA_UART_TX_TRIG_8B  0x10
82 #define TEGRA_UART_TX_TRIG_4B  0x20
83 #define TEGRA_UART_TX_TRIG_1B  0x30
84
85 struct tegra_uart_port {
86         struct uart_port        uport;
87         char                    port_name[32];
88
89         /* Module info */
90         unsigned long           size;
91         struct clk              *clk;
92         unsigned int            baud;
93
94         /* Register shadow */
95         unsigned char           fcr_shadow;
96         unsigned char           mcr_shadow;
97         unsigned char           lcr_shadow;
98         unsigned char           ier_shadow;
99         bool                    use_cts_control;
100         bool                    rts_active;
101
102         int                     tx_in_progress;
103         unsigned int            tx_bytes;
104
105         dma_addr_t              xmit_dma_addr;
106
107         /* TX DMA */
108         struct tegra_dma_req    tx_dma_req;
109         struct tegra_dma_channel *tx_dma;
110         struct work_struct      tx_work;
111
112         /* RX DMA */
113         struct tegra_dma_req    rx_dma_req;
114         struct tegra_dma_channel *rx_dma;
115
116         bool                    use_rx_dma;
117         bool                    use_tx_dma;
118
119         bool                    rx_timeout;
120         int                     rx_in_progress;
121 };
122
123 static inline u8 uart_readb(struct tegra_uart_port *t, unsigned long reg)
124 {
125         u8 val = readb(t->uport.membase + (reg << t->uport.regshift));
126         dev_vdbg(t->uport.dev, "%s: %p %03lx = %02x\n", __func__,
127                 t->uport.membase, reg << t->uport.regshift, val);
128         return val;
129 }
130
131 static inline void uart_writeb(struct tegra_uart_port *t, u8 val,
132         unsigned long reg)
133 {
134         dev_vdbg(t->uport.dev, "%s: %p %03lx %02x\n",
135                 __func__, t->uport.membase, reg << t->uport.regshift, val);
136         writeb(val, t->uport.membase + (reg << t->uport.regshift));
137 }
138
139 static inline void uart_writel(struct tegra_uart_port *t, u32 val,
140         unsigned long reg)
141 {
142         dev_vdbg(t->uport.dev, "%s: %p %03lx %08x\n",
143                 __func__, t->uport.membase, reg << t->uport.regshift, val);
144         writel(val, t->uport.membase + (reg << t->uport.regshift));
145 }
146
147 static void tegra_set_baudrate(struct tegra_uart_port *t, unsigned int baud);
148 static void tegra_set_mctrl(struct uart_port *u, unsigned int mctrl);
149 static void do_handle_rx_pio(struct tegra_uart_port *t);
150 static void do_handle_rx_dma(struct tegra_uart_port *t);
151 static void set_rts(struct tegra_uart_port *t, bool active);
152 static void set_dtr(struct tegra_uart_port *t, bool active);
153
154 static void fill_tx_fifo(struct tegra_uart_port *t, int max_bytes)
155 {
156         int i;
157         struct circ_buf *xmit = &t->uport.state->xmit;
158
159         for (i = 0; i < max_bytes; i++) {
160                 BUG_ON(uart_circ_empty(xmit));
161                 uart_writeb(t, xmit->buf[xmit->tail], UART_TX);
162                 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
163                 t->uport.icount.tx++;
164         }
165 }
166
167 static void tegra_start_pio_tx(struct tegra_uart_port *t, unsigned int bytes)
168 {
169         if (bytes > TEGRA_UART_FIFO_SIZE)
170                 bytes = TEGRA_UART_FIFO_SIZE;
171
172         t->fcr_shadow &= ~UART_FCR_T_TRIG_11;
173         t->fcr_shadow |= TEGRA_UART_TX_TRIG_8B;
174         uart_writeb(t, t->fcr_shadow, UART_FCR);
175         t->tx_in_progress = TEGRA_TX_PIO;
176         t->tx_bytes = bytes;
177         t->ier_shadow |= UART_IER_THRI;
178         uart_writeb(t, t->ier_shadow, UART_IER);
179 }
180
181 static void tegra_start_dma_tx(struct tegra_uart_port *t, unsigned long bytes)
182 {
183         struct circ_buf *xmit;
184         xmit = &t->uport.state->xmit;
185
186         dma_sync_single_for_device(t->uport.dev, t->xmit_dma_addr,
187                 UART_XMIT_SIZE, DMA_TO_DEVICE);
188
189         t->fcr_shadow &= ~UART_FCR_T_TRIG_11;
190         t->fcr_shadow |= TEGRA_UART_TX_TRIG_4B;
191         uart_writeb(t, t->fcr_shadow, UART_FCR);
192
193         t->tx_bytes = bytes & ~(sizeof(u32)-1);
194         t->tx_dma_req.source_addr = t->xmit_dma_addr + xmit->tail;
195         t->tx_dma_req.size = t->tx_bytes;
196
197         t->tx_in_progress = TEGRA_TX_DMA;
198
199         tegra_dma_enqueue_req(t->tx_dma, &t->tx_dma_req);
200 }
201
202 /* Called with u->lock taken */
203 static void tegra_start_next_tx(struct tegra_uart_port *t)
204 {
205         unsigned long tail;
206         unsigned long count;
207
208         struct circ_buf *xmit;
209
210         xmit = &t->uport.state->xmit;
211         tail = (unsigned long)&xmit->buf[xmit->tail];
212         count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
213
214
215         dev_vdbg(t->uport.dev, "+%s %lu %d\n", __func__, count,
216                 t->tx_in_progress);
217
218         if (count == 0)
219                 goto out;
220
221         if (!t->use_tx_dma || count < TEGRA_UART_MIN_DMA)
222                 tegra_start_pio_tx(t, count);
223         else if (BYTES_TO_ALIGN(tail) > 0)
224                 tegra_start_pio_tx(t, BYTES_TO_ALIGN(tail));
225         else
226                 tegra_start_dma_tx(t, count);
227
228 out:
229         dev_vdbg(t->uport.dev, "-%s", __func__);
230 }
231
232 /* Called by serial core driver with u->lock taken. */
233 static void tegra_start_tx(struct uart_port *u)
234 {
235         struct tegra_uart_port *t;
236         struct circ_buf *xmit;
237
238         t = container_of(u, struct tegra_uart_port, uport);
239         xmit = &u->state->xmit;
240
241         if (!uart_circ_empty(xmit) && !t->tx_in_progress)
242                 tegra_start_next_tx(t);
243 }
244
245 static int tegra_start_dma_rx(struct tegra_uart_port *t)
246 {
247         wmb();
248         if (tegra_dma_enqueue_req(t->rx_dma, &t->rx_dma_req)) {
249                 dev_err(t->uport.dev, "Could not enqueue Rx DMA req\n");
250                 return -EINVAL;
251         }
252         return 0;
253 }
254
255 static void tegra_rx_dma_threshold_callback(struct tegra_dma_req *req)
256 {
257         struct tegra_uart_port *t = req->dev;
258         unsigned long flags;
259
260         spin_lock_irqsave(&t->uport.lock, flags);
261
262         do_handle_rx_dma(t);
263
264         spin_unlock_irqrestore(&t->uport.lock, flags);
265 }
266
267 /* must be called with uart lock held */
268 static void tegra_rx_dma_complete_req(struct tegra_uart_port *t,
269         struct tegra_dma_req *req)
270 {
271         struct uart_port *u = &t->uport;
272         struct tty_struct *tty = u->state->port.tty;
273
274         /* If we are here, DMA is stopped */
275
276         dev_dbg(t->uport.dev, "%s: %d %d\n", __func__, req->bytes_transferred,
277                 req->status);
278         if (req->bytes_transferred) {
279                 t->uport.icount.rx += req->bytes_transferred;
280                 tty_insert_flip_string(tty,
281                         ((unsigned char *)(req->virt_addr)),
282                         req->bytes_transferred);
283         }
284
285         do_handle_rx_pio(t);
286
287         /* Push the read data later in caller place. */
288         if (req->status == -TEGRA_DMA_REQ_ERROR_ABORTED)
289                 return;
290
291         tty_flip_buffer_push(u->state->port.tty);
292 }
293
294 static void tegra_rx_dma_complete_callback(struct tegra_dma_req *req)
295 {
296         struct tegra_uart_port *t = req->dev;
297         unsigned long flags;
298
299         /*
300          * should never get called, dma should be dequeued during threshold
301          * callback
302          */
303
304         dev_warn(t->uport.dev, "possible rx overflow\n");
305
306         spin_lock_irqsave(&t->uport.lock, flags);
307         tegra_rx_dma_complete_req(t, req);
308         spin_unlock_irqrestore(&t->uport.lock, flags);
309 }
310
311 /* Lock already taken */
312 static void do_handle_rx_dma(struct tegra_uart_port *t)
313 {
314         struct uart_port *u = &t->uport;
315         if (t->rts_active)
316                 set_rts(t, false);
317         if (!tegra_dma_dequeue_req(t->rx_dma, &t->rx_dma_req))
318                 tegra_rx_dma_complete_req(t, &t->rx_dma_req);
319
320         tty_flip_buffer_push(u->state->port.tty);
321         /* enqueue the request again */
322         tegra_start_dma_rx(t);
323         if (t->rts_active)
324                 set_rts(t, true);
325 }
326
327 /* Wait for a symbol-time. */
328 static void wait_sym_time(struct tegra_uart_port *t, unsigned int syms)
329 {
330
331         /* Definitely have a start bit. */
332         unsigned int bits = 1;
333         switch (t->lcr_shadow & 3) {
334         case UART_LCR_WLEN5:
335                 bits += 5;
336                 break;
337         case UART_LCR_WLEN6:
338                 bits += 6;
339                 break;
340         case UART_LCR_WLEN7:
341                 bits += 7;
342                 break;
343         default:
344                 bits += 8;
345                 break;
346         }
347
348         /* Technically 5 bits gets 1.5 bits of stop... */
349         if (t->lcr_shadow & UART_LCR_STOP) {
350                 bits += 2;
351         } else {
352                 bits++;
353         }
354
355         if (t->lcr_shadow & UART_LCR_PARITY)
356                 bits++;
357
358         if (likely(t->baud))
359                 udelay(DIV_ROUND_UP(syms * bits * 1000000, t->baud));
360 }
361
362 /* Flush desired FIFO. */
363 static void tegra_fifo_reset(struct tegra_uart_port *t, u8 fcr_bits)
364 {
365         unsigned char fcr = t->fcr_shadow;
366         fcr |= fcr_bits & (UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
367         uart_writeb(t, fcr, UART_FCR);
368         uart_readb(t, UART_SCR); /* Dummy read to ensure the write is posted */
369         wait_sym_time(t, 1); /* Wait for the flush to propagate. */
370 }
371
372 static char do_decode_rx_error(struct tegra_uart_port *t, u8 lsr)
373 {
374         char flag = TTY_NORMAL;
375
376         if (unlikely(lsr & UART_LSR_ANY)) {
377                 if (lsr & UART_LSR_OE) {
378                         /* Overrrun error  */
379                         flag |= TTY_OVERRUN;
380                         t->uport.icount.overrun++;
381                         dev_err(t->uport.dev, "Got overrun errors\n");
382                 } else if (lsr & UART_LSR_PE) {
383                         /* Parity error */
384                         flag |= TTY_PARITY;
385                         t->uport.icount.parity++;
386                         dev_err(t->uport.dev, "Got Parity errors\n");
387                 } else if (lsr & UART_LSR_FE) {
388                         flag |= TTY_FRAME;
389                         t->uport.icount.frame++;
390                         dev_err(t->uport.dev, "Got frame errors\n");
391                 } else if (lsr & UART_LSR_BI) {
392                         dev_err(t->uport.dev, "Got Break\n");
393                         t->uport.icount.brk++;
394                         /* If FIFO read error without any data, reset Rx FIFO */
395                         if (!(lsr & UART_LSR_DR) && (lsr & UART_LSR_FIFOE))
396                                 tegra_fifo_reset(t, UART_FCR_CLEAR_RCVR);
397                 }
398         }
399         return flag;
400 }
401
402 static void do_handle_rx_pio(struct tegra_uart_port *t)
403 {
404         int count = 0;
405         do {
406                 char flag = TTY_NORMAL;
407                 unsigned char lsr = 0;
408                 unsigned char ch;
409
410
411                 lsr = uart_readb(t, UART_LSR);
412                 if (!(lsr & UART_LSR_DR))
413                         break;
414
415                 flag =  do_decode_rx_error(t, lsr);
416                 ch = uart_readb(t, UART_RX);
417                 t->uport.icount.rx++;
418                 count++;
419
420                 if (!uart_handle_sysrq_char(&t->uport, c))
421                         uart_insert_char(&t->uport, lsr, UART_LSR_OE, ch, flag);
422         } while (1);
423
424         dev_dbg(t->uport.dev, "PIO received %d bytes\n", count);
425
426         return;
427 }
428
429 static void do_handle_modem_signal(struct uart_port *u)
430 {
431         unsigned char msr;
432         struct tegra_uart_port *t;
433
434         t = container_of(u, struct tegra_uart_port, uport);
435         msr = uart_readb(t, UART_MSR);
436         if (msr & UART_MSR_CTS)
437                 dev_dbg(u->dev, "CTS triggered\n");
438         if (msr & UART_MSR_DSR)
439                 dev_dbg(u->dev, "DSR enabled\n");
440         if (msr & UART_MSR_DCD)
441                 dev_dbg(u->dev, "CD enabled\n");
442         if (msr & UART_MSR_RI)
443                 dev_dbg(u->dev, "RI enabled\n");
444         return;
445 }
446
447 static void do_handle_tx_pio(struct tegra_uart_port *t)
448 {
449         struct circ_buf *xmit = &t->uport.state->xmit;
450
451         fill_tx_fifo(t, t->tx_bytes);
452
453         t->tx_in_progress = 0;
454
455         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
456                 uart_write_wakeup(&t->uport);
457
458         tegra_start_next_tx(t);
459         return;
460 }
461
462 static void tegra_tx_dma_complete_work(struct work_struct *work)
463 {
464         struct tegra_uart_port *t =
465                         container_of(work, struct tegra_uart_port, tx_work);
466         struct tegra_dma_req *req = &t->tx_dma_req;
467         unsigned long flags;
468         int timeout = 20;
469
470         while ((uart_readb(t, UART_LSR) & TX_EMPTY_STATUS) != TX_EMPTY_STATUS) {
471                 timeout--;
472                 if (timeout == 0) {
473                         dev_err(t->uport.dev,
474                                 "timed out waiting for TX FIFO to empty\n");
475                         return;
476                 }
477                 msleep(1);
478         }
479
480         spin_lock_irqsave(&t->uport.lock, flags);
481
482         t->tx_in_progress = 0;
483
484         if (req->status != -TEGRA_DMA_REQ_ERROR_ABORTED)
485                 tegra_start_next_tx(t);
486
487         spin_unlock_irqrestore(&t->uport.lock, flags);
488 }
489
490 /* must be called with uart lock held */
491 static void tegra_tx_dma_complete_req(struct tegra_uart_port *t,
492         struct tegra_dma_req *req)
493 {
494         struct circ_buf *xmit = &t->uport.state->xmit;
495         int count = req->bytes_transferred;
496
497         xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
498
499         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
500                 uart_write_wakeup(&t->uport);
501
502         schedule_work(&t->tx_work);
503 }
504
505 static void tegra_tx_dma_complete_callback(struct tegra_dma_req *req)
506 {
507         struct tegra_uart_port *t = req->dev;
508         unsigned long flags;
509
510         dev_vdbg(t->uport.dev, "%s: %d\n", __func__, req->bytes_transferred);
511
512         spin_lock_irqsave(&t->uport.lock, flags);
513
514         tegra_tx_dma_complete_req(t, req);
515
516         spin_unlock_irqrestore(&t->uport.lock, flags);
517 }
518
519 static irqreturn_t tegra_uart_isr(int irq, void *data)
520 {
521         struct tegra_uart_port *t = data;
522         struct uart_port *u = &t->uport;
523         unsigned char iir;
524         unsigned char ier;
525         bool is_rx_int = false;
526         unsigned long flags;
527
528         spin_lock_irqsave(&u->lock, flags);
529         t  = container_of(u, struct tegra_uart_port, uport);
530         while (1) {
531                 iir = uart_readb(t, UART_IIR);
532                 if (iir & UART_IIR_NO_INT) {
533                         if (likely(t->use_rx_dma) && is_rx_int) {
534                                 do_handle_rx_dma(t);
535
536                                 if (t->rx_in_progress) {
537                                         ier = t->ier_shadow;
538                                         ier |= (UART_IER_RLSI | UART_IER_RTOIE | UART_IER_EORD);
539                                         t->ier_shadow = ier;
540                                         uart_writeb(t, ier, UART_IER);
541                                 }
542                         }
543                         spin_unlock_irqrestore(&u->lock, flags);
544                         return IRQ_HANDLED;
545                 }
546
547                 dev_dbg(u->dev, "tegra_uart_isr iir = 0x%x (%d)\n", iir,
548                         (iir >> 1) & 0x7);
549                 switch ((iir >> 1) & 0x7) {
550                 case 0: /* Modem signal change interrupt */
551                         do_handle_modem_signal(u);
552                         break;
553                 case 1: /* Transmit interrupt only triggered when using PIO */
554                         t->ier_shadow &= ~UART_IER_THRI;
555                         uart_writeb(t, t->ier_shadow, UART_IER);
556                         do_handle_tx_pio(t);
557                         break;
558                 case 4: /* End of data */
559                 case 6: /* Rx timeout */
560                 case 2: /* Receive */
561                         if (likely(t->use_rx_dma)) {
562                                 if (!is_rx_int) {
563                                         is_rx_int = true;
564                                         /* Disable interrups */
565                                         ier = t->ier_shadow;
566                                         ier |= UART_IER_RDI;
567                                         uart_writeb(t, ier, UART_IER);
568                                         ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE | UART_IER_EORD);
569                                         t->ier_shadow = ier;
570                                         uart_writeb(t, ier, UART_IER);
571                                 }
572                         } else {
573                                 do_handle_rx_pio(t);
574
575                                 spin_unlock_irqrestore(&u->lock, flags);
576                                 tty_flip_buffer_push(u->state->port.tty);
577                                 spin_lock_irqsave(&u->lock, flags);
578                         }
579                         break;
580                 case 3: /* Receive error */
581                         /* FIXME how to handle this? Why do we get here */
582                         do_decode_rx_error(t, uart_readb(t, UART_LSR));
583                         break;
584                 case 5: /* break nothing to handle */
585                 case 7: /* break nothing to handle */
586                         break;
587                 }
588         }
589 }
590
591 static void tegra_stop_rx(struct uart_port *u)
592 {
593         struct tegra_uart_port *t;
594         unsigned char ier;
595
596         t = container_of(u, struct tegra_uart_port, uport);
597
598         if (t->rts_active)
599                 set_rts(t, false);
600
601         if (t->rx_in_progress) {
602                 wait_sym_time(t, 1); /* wait a character interval */
603
604                 ier = t->ier_shadow;
605                 ier &= ~(UART_IER_RDI | UART_IER_RLSI | UART_IER_RTOIE | UART_IER_EORD);
606                 t->ier_shadow = ier;
607                 uart_writeb(t, ier, UART_IER);
608                 t->rx_in_progress = 0;
609
610                 if (t->use_rx_dma && t->rx_dma) {
611                         if (!tegra_dma_dequeue_req(t->rx_dma, &t->rx_dma_req))
612                                 tegra_rx_dma_complete_req(t, &t->rx_dma_req);
613                 } else {
614                         do_handle_rx_pio(t);
615                 }
616                 tty_flip_buffer_push(u->state->port.tty);
617         }
618
619         return;
620 }
621
622 static void tegra_uart_hw_deinit(struct tegra_uart_port *t)
623 {
624         unsigned long flags;
625
626         flush_work(&t->tx_work);
627
628         /* Disable interrupts */
629         uart_writeb(t, 0, UART_IER);
630
631         while ((uart_readb(t, UART_LSR) & UART_LSR_TEMT) != UART_LSR_TEMT);
632                 udelay(200);
633
634         spin_lock_irqsave(&t->uport.lock, flags);
635
636         /* Reset the Rx and Tx FIFOs */
637         tegra_fifo_reset(t, UART_FCR_CLEAR_XMIT | UART_FCR_CLEAR_RCVR);
638
639         clk_disable(t->clk);
640         t->baud = 0;
641
642         spin_unlock_irqrestore(&t->uport.lock, flags);
643 }
644
645 static void tegra_uart_free_rx_dma(struct tegra_uart_port *t)
646 {
647         if (!t->use_rx_dma)
648                return;
649
650         tegra_dma_free_channel(t->rx_dma);
651         t->rx_dma = NULL;
652
653         if (likely(t->rx_dma_req.dest_addr))
654                 dma_free_coherent(t->uport.dev, t->rx_dma_req.size,
655                         t->rx_dma_req.virt_addr, t->rx_dma_req.dest_addr);
656         t->rx_dma_req.dest_addr = 0;
657         t->rx_dma_req.virt_addr = NULL;
658
659         t->use_rx_dma = false;
660 }
661
662 static int tegra_uart_hw_init(struct tegra_uart_port *t)
663 {
664         unsigned char ier;
665
666         dev_vdbg(t->uport.dev, "+tegra_uart_hw_init\n");
667
668         t->fcr_shadow = 0;
669         t->mcr_shadow = 0;
670         t->lcr_shadow = 0;
671         t->ier_shadow = 0;
672         t->baud = 0;
673
674         clk_enable(t->clk);
675
676         /* Reset the UART controller to clear all previous status.*/
677         tegra_periph_reset_assert(t->clk);
678         udelay(100);
679         tegra_periph_reset_deassert(t->clk);
680         udelay(100);
681
682         t->rx_in_progress = 0;
683
684         /* Set the trigger level
685          *
686          * For PIO mode:
687          *
688          * For receive, this will interrupt the CPU after that many number of
689          * bytes are received, for the remaining bytes the receive timeout
690          * interrupt is received.
691          *
692          *  Rx high watermark is set to 4.
693          *
694          * For transmit, if the trasnmit interrupt is enabled, this will
695          * interrupt the CPU when the number of entries in the FIFO reaches the
696          * low watermark.
697          *
698          *  Tx low watermark is set to 8.
699          *
700          *  For DMA mode:
701          *
702          *  Set the Tx trigger to 4. This should match the DMA burst size that
703          *  programmed in the DMA registers.
704          * */
705         t->fcr_shadow = UART_FCR_ENABLE_FIFO;
706         t->fcr_shadow |= UART_FCR_R_TRIG_01;
707         t->fcr_shadow |= TEGRA_UART_TX_TRIG_8B;
708         uart_writeb(t, t->fcr_shadow, UART_FCR);
709
710         if (t->use_rx_dma) {
711                 /* initialize the UART for a simple default configuration
712                   * so that the receive DMA buffer may be enqueued */
713                 t->lcr_shadow = 3;  /* no parity, stop, 8 data bits */
714                 tegra_set_baudrate(t, 115200);
715                 t->fcr_shadow |= UART_FCR_DMA_SELECT;
716                 uart_writeb(t, t->fcr_shadow, UART_FCR);
717                 if (tegra_start_dma_rx(t)) {
718                         dev_err(t->uport.dev, "Rx DMA enqueue failed\n");
719                         tegra_uart_free_rx_dma(t);
720                         t->fcr_shadow &= ~UART_FCR_DMA_SELECT;
721                         uart_writeb(t, t->fcr_shadow, UART_FCR);
722                 }
723         }
724         else
725                 uart_writeb(t, t->fcr_shadow, UART_FCR);
726
727         t->rx_in_progress = 1;
728
729         /*
730          *  Enable IE_RXS for the receive status interrupts like line errros.
731          *  Enable IE_RX_TIMEOUT to get the bytes which cannot be DMA'd.
732          *
733          *  If using DMA mode, enable EORD instead of receive interrupt which
734          *  will interrupt after the UART is done with the receive instead of
735          *  the interrupt when the FIFO "threshold" is reached.
736          *
737          *  EORD is different interrupt than RX_TIMEOUT - RX_TIMEOUT occurs when
738          *  the DATA is sitting in the FIFO and couldn't be transferred to the
739          *  DMA as the DMA size alignment(4 bytes) is not met. EORD will be
740          *  triggered when there is a pause of the incomming data stream for 4
741          *  characters long.
742          *
743          *  For pauses in the data which is not aligned to 4 bytes, we get
744          *  both the EORD as well as RX_TIMEOUT - SW sees RX_TIMEOUT first
745          *  then the EORD.
746          *
747          *  Don't get confused, believe in the magic of nvidia hw...:-)
748          */
749         ier = 0;
750         ier |= UART_IER_RLSI | UART_IER_RTOIE;
751         if (t->use_rx_dma)
752                 ier |= UART_IER_EORD;
753         else
754                 ier |= UART_IER_RDI;
755         t->ier_shadow = ier;
756         uart_writeb(t, ier, UART_IER);
757
758         dev_vdbg(t->uport.dev, "-tegra_uart_hw_init\n");
759         return 0;
760 }
761
762 static int tegra_uart_init_rx_dma(struct tegra_uart_port *t)
763 {
764         dma_addr_t rx_dma_phys;
765         void *rx_dma_virt;
766
767         t->rx_dma = tegra_dma_allocate_channel(TEGRA_DMA_MODE_CONTINUOUS);
768         if (!t->rx_dma) {
769                 dev_err(t->uport.dev, "%s: failed to allocate RX DMA.\n", __func__);
770                 return -ENODEV;
771         }
772
773         t->rx_dma_req.size = UART_RX_DMA_BUFFER_SIZE;
774         rx_dma_virt = dma_alloc_coherent(t->uport.dev,
775                 t->rx_dma_req.size, &rx_dma_phys, GFP_KERNEL);
776         if (!rx_dma_virt) {
777                 dev_err(t->uport.dev, "DMA buffers allocate failed\n");
778                 goto fail;
779         }
780         t->rx_dma_req.dest_addr = rx_dma_phys;
781         t->rx_dma_req.virt_addr = rx_dma_virt;
782
783         t->rx_dma_req.source_addr = (unsigned long)t->uport.mapbase;
784         t->rx_dma_req.source_wrap = 4;
785         t->rx_dma_req.dest_wrap = 0;
786         t->rx_dma_req.to_memory = 1;
787         t->rx_dma_req.source_bus_width = 8;
788         t->rx_dma_req.dest_bus_width = 32;
789         t->rx_dma_req.req_sel = dma_req_sel[t->uport.line];
790         t->rx_dma_req.complete = tegra_rx_dma_complete_callback;
791         t->rx_dma_req.threshold = tegra_rx_dma_threshold_callback;
792         t->rx_dma_req.dev = t;
793
794         return 0;
795 fail:
796         tegra_uart_free_rx_dma(t);
797         return -ENODEV;
798 }
799
800 static int tegra_startup(struct uart_port *u)
801 {
802         struct tegra_uart_port *t = container_of(u,
803                 struct tegra_uart_port, uport);
804         int ret = 0;
805
806         t = container_of(u, struct tegra_uart_port, uport);
807         sprintf(t->port_name, "tegra_uart_%d", u->line);
808
809         t->use_tx_dma = false;
810         if (!TX_FORCE_PIO) {
811                 t->tx_dma = tegra_dma_allocate_channel(TEGRA_DMA_MODE_ONESHOT);
812                 if (t->tx_dma)
813                         t->use_tx_dma = true;
814                 else
815                         pr_err("%s: failed to allocate TX DMA.\n", __func__);
816         }
817         if (t->use_tx_dma) {
818                 t->tx_dma_req.instance = u->line;
819                 t->tx_dma_req.complete = tegra_tx_dma_complete_callback;
820                 t->tx_dma_req.to_memory = 0;
821
822                 t->tx_dma_req.dest_addr = (unsigned long)t->uport.mapbase;
823                 t->tx_dma_req.dest_wrap = 4;
824                 t->tx_dma_req.source_wrap = 0;
825                 t->tx_dma_req.source_bus_width = 32;
826                 t->tx_dma_req.dest_bus_width = 8;
827                 t->tx_dma_req.req_sel = dma_req_sel[t->uport.line];
828                 t->tx_dma_req.dev = t;
829                 t->tx_dma_req.size = 0;
830                 t->xmit_dma_addr = dma_map_single(t->uport.dev,
831                         t->uport.state->xmit.buf, UART_XMIT_SIZE,
832                         DMA_TO_DEVICE);
833         }
834         t->tx_in_progress = 0;
835
836         t->use_rx_dma = false;
837         if (!RX_FORCE_PIO) {
838                 if (!tegra_uart_init_rx_dma(t))
839                         t->use_rx_dma = true;
840         }
841
842         ret = tegra_uart_hw_init(t);
843         if (ret)
844                 goto fail;
845
846         dev_dbg(u->dev, "Requesting IRQ %d\n", u->irq);
847         msleep(1);
848
849         ret = request_irq(u->irq, tegra_uart_isr, IRQF_DISABLED,
850                 t->port_name, t);
851         if (ret) {
852                 dev_err(u->dev, "Failed to register ISR for IRQ %d\n", u->irq);
853                 goto fail;
854         }
855         dev_dbg(u->dev,"Started UART port %d\n", u->line);
856
857         return 0;
858 fail:
859         dev_err(u->dev, "Tegra UART startup failed\n");
860         return ret;
861 }
862
863 static void tegra_shutdown(struct uart_port *u)
864 {
865         struct tegra_uart_port *t;
866
867         t = container_of(u, struct tegra_uart_port, uport);
868         dev_vdbg(u->dev, "+tegra_shutdown\n");
869
870         tegra_uart_hw_deinit(t);
871
872         t->rx_in_progress = 0;
873         t->tx_in_progress = 0;
874
875         tegra_uart_free_rx_dma(t);
876         if (t->use_tx_dma) {
877                 tegra_dma_free_channel(t->tx_dma);
878                 t->tx_dma = NULL;
879                 t->use_tx_dma = false;
880                 dma_unmap_single(t->uport.dev, t->xmit_dma_addr, UART_XMIT_SIZE,
881                                 DMA_TO_DEVICE);
882                 t->xmit_dma_addr = 0;
883         }
884
885         free_irq(u->irq, t);
886         dev_vdbg(u->dev, "-tegra_shutdown\n");
887 }
888
889 static unsigned int tegra_get_mctrl(struct uart_port *u)
890 {
891         /* RI - Ring detector is active
892          * CD/DCD/CAR - Carrier detect is always active. For some reason
893          *                        linux has different names for carrier detect.
894          * DSR - Data Set ready is active as the hardware doesn't support it.
895          *         Don't know if the linux support this yet?
896          * CTS - Clear to send. Always set to active, as the hardware handles
897          *         CTS automatically.
898          * */
899         return TIOCM_RI | TIOCM_CD | TIOCM_DSR | TIOCM_CTS;
900 }
901
902 static void set_rts(struct tegra_uart_port *t, bool active)
903 {
904         unsigned char mcr;
905         mcr = t->mcr_shadow;
906         if (active)
907                 mcr |= UART_MCR_RTS;
908         else
909                 mcr &= ~UART_MCR_RTS;
910         if (mcr != t->mcr_shadow) {
911                 uart_writeb(t, mcr, UART_MCR);
912                 t->mcr_shadow = mcr;
913         }
914         return;
915 }
916
917 static void set_dtr(struct tegra_uart_port *t, bool active)
918 {
919         unsigned char mcr;
920         mcr = t->mcr_shadow;
921         if (active)
922                 mcr |= UART_MCR_DTR;
923         else
924                 mcr &= ~UART_MCR_DTR;
925         if (mcr != t->mcr_shadow) {
926                 uart_writeb(t, mcr, UART_MCR);
927                 t->mcr_shadow = mcr;
928         }
929         return;
930 }
931
932 static void tegra_set_mctrl(struct uart_port *u, unsigned int mctrl)
933 {
934         unsigned char mcr;
935         struct tegra_uart_port *t;
936
937         dev_dbg(u->dev, "tegra_set_mctrl called with %d\n", mctrl);
938         t = container_of(u, struct tegra_uart_port, uport);
939
940         mcr = t->mcr_shadow;
941         if (mctrl & TIOCM_RTS) {
942                 t->rts_active = true;
943                 set_rts(t, true);
944         } else {
945                 t->rts_active = false;
946                 set_rts(t, false);
947         }
948
949         if (mctrl & TIOCM_DTR)
950                 set_dtr(t, true);
951         else
952                 set_dtr(t, false);
953         return;
954 }
955
956 static void tegra_break_ctl(struct uart_port *u, int break_ctl)
957 {
958         struct tegra_uart_port *t;
959         unsigned char lcr;
960
961         t = container_of(u, struct tegra_uart_port, uport);
962         lcr = t->lcr_shadow;
963         if (break_ctl)
964                 lcr |= UART_LCR_SBC;
965         else
966                 lcr &= ~UART_LCR_SBC;
967         uart_writeb(t, lcr, UART_LCR);
968         t->lcr_shadow = lcr;
969 }
970
971 static int tegra_request_port(struct uart_port *u)
972 {
973         return 0;
974 }
975
976 static void tegra_release_port(struct uart_port *u)
977 {
978
979 }
980
981 static unsigned int tegra_tx_empty(struct uart_port *u)
982 {
983         struct tegra_uart_port *t;
984         unsigned int ret = 0;
985         unsigned long flags;
986
987         t = container_of(u, struct tegra_uart_port, uport);
988         dev_vdbg(u->dev, "+tegra_tx_empty\n");
989
990         spin_lock_irqsave(&u->lock, flags);
991         if (!t->tx_in_progress)
992                 ret = TIOCSER_TEMT;
993         spin_unlock_irqrestore(&u->lock, flags);
994
995         dev_vdbg(u->dev, "-tegra_tx_empty\n");
996         return ret;
997 }
998
999 static void tegra_stop_tx(struct uart_port *u)
1000 {
1001         struct tegra_uart_port *t;
1002
1003         t = container_of(u, struct tegra_uart_port, uport);
1004
1005         if (t->use_tx_dma) {
1006                 if (!tegra_dma_dequeue_req(t->tx_dma, &t->tx_dma_req))
1007                         tegra_tx_dma_complete_req(t, &t->tx_dma_req);
1008         }
1009
1010         return;
1011 }
1012
1013 static void tegra_enable_ms(struct uart_port *u)
1014 {
1015 }
1016
1017 #define UART_CLOCK_ACCURACY 5
1018
1019 static void tegra_set_baudrate(struct tegra_uart_port *t, unsigned int baud)
1020 {
1021         unsigned long rate;
1022         unsigned int divisor;
1023         unsigned char lcr;
1024
1025         if (t->baud == baud)
1026                 return;
1027
1028         rate = clk_get_rate(t->clk);
1029
1030         divisor = rate;
1031         do_div(divisor, 16);
1032         divisor += baud/2;
1033         do_div(divisor, baud);
1034
1035         lcr = t->lcr_shadow;
1036         lcr |= UART_LCR_DLAB;
1037         uart_writeb(t, lcr, UART_LCR);
1038
1039         uart_writel(t, divisor & 0xFF, UART_TX);
1040         uart_writel(t, ((divisor >> 8) & 0xFF), UART_IER);
1041
1042         lcr &= ~UART_LCR_DLAB;
1043         uart_writeb(t, lcr, UART_LCR);
1044         uart_readb(t, UART_SCR); /* Dummy read to ensure the write is posted */
1045
1046         t->baud = baud;
1047         wait_sym_time(t, 2); /* wait two character intervals at new rate */
1048         dev_dbg(t->uport.dev, "Baud %u clock freq %lu and divisor of %u\n",
1049                 baud, rate, divisor);
1050 }
1051
1052 static void tegra_set_termios(struct uart_port *u, struct ktermios *termios,
1053                                            struct ktermios *oldtermios)
1054 {
1055         struct tegra_uart_port *t;
1056         unsigned int baud;
1057         unsigned long flags;
1058         unsigned int lcr;
1059         unsigned int c_cflag = termios->c_cflag;
1060         unsigned char mcr;
1061
1062         t = container_of(u, struct tegra_uart_port, uport);
1063         dev_vdbg(t->uport.dev, "+tegra_set_termios\n");
1064
1065         spin_lock_irqsave(&u->lock, flags);
1066
1067         /* Changing configuration, it is safe to stop any rx now */
1068         if (t->rts_active)
1069                 set_rts(t, false);
1070
1071         /* Parity */
1072         lcr = t->lcr_shadow;
1073         lcr &= ~UART_LCR_PARITY;
1074         if (PARENB == (c_cflag & PARENB)) {
1075                 if (CMSPAR == (c_cflag & CMSPAR)) {
1076                         /* FIXME What is space parity? */
1077                         /* data |= SPACE_PARITY; */
1078                 } else if (c_cflag & PARODD) {
1079                         lcr |= UART_LCR_PARITY;
1080                         lcr &= ~UART_LCR_EPAR;
1081                         lcr &= ~UART_LCR_SPAR;
1082                 } else {
1083                         lcr |= UART_LCR_PARITY;
1084                         lcr |= UART_LCR_EPAR;
1085                         lcr &= ~UART_LCR_SPAR;
1086                 }
1087         }
1088
1089         lcr &= ~UART_LCR_WLEN8;
1090         switch (c_cflag & CSIZE) {
1091         case CS5:
1092                 lcr |= UART_LCR_WLEN5;
1093                 break;
1094         case CS6:
1095                 lcr |= UART_LCR_WLEN6;
1096                 break;
1097         case CS7:
1098                 lcr |= UART_LCR_WLEN7;
1099                 break;
1100         default:
1101                 lcr |= UART_LCR_WLEN8;
1102                 break;
1103         }
1104
1105         /* Stop bits */
1106         if (termios->c_cflag & CSTOPB)
1107                 lcr |= UART_LCR_STOP;
1108         else
1109                 lcr &= ~UART_LCR_STOP;
1110
1111         uart_writeb(t, lcr, UART_LCR);
1112         t->lcr_shadow = lcr;
1113
1114         /* Baud rate. */
1115         baud = uart_get_baud_rate(u, termios, oldtermios, 200, 4000000);
1116         tegra_set_baudrate(t, baud);
1117
1118         /* Flow control */
1119         if (termios->c_cflag & CRTSCTS) {
1120                 mcr = t->mcr_shadow;
1121                 mcr |= UART_MCR_CTS_EN;
1122                 mcr &= ~UART_MCR_RTS_EN;
1123                 t->mcr_shadow = mcr;
1124                 uart_writeb(t, mcr, UART_MCR);
1125                 t->use_cts_control = true;
1126                 /* if top layer has asked to set rts active then do so here */
1127                 if (t->rts_active)
1128                         set_rts(t, true);
1129         } else {
1130                 mcr = t->mcr_shadow;
1131                 mcr &= ~UART_MCR_CTS_EN;
1132                 mcr &= ~UART_MCR_RTS_EN;
1133                 t->mcr_shadow = mcr;
1134                 uart_writeb(t, mcr, UART_MCR);
1135                 t->use_cts_control = false;
1136         }
1137
1138         /* update the port timeout based on new settings */
1139         uart_update_timeout(u, termios->c_cflag, baud);
1140
1141         spin_unlock_irqrestore(&u->lock, flags);
1142         dev_vdbg(t->uport.dev, "-tegra_set_termios\n");
1143         return;
1144 }
1145
1146 /*
1147  * Flush any TX data submitted for DMA. Called when the TX circular
1148  * buffer is reset.
1149  */
1150 static void tegra_flush_buffer(struct uart_port *u)
1151 {
1152         struct tegra_uart_port *t;
1153
1154         dev_vdbg(u->dev, "%s called", __func__);
1155
1156         t = container_of(u, struct tegra_uart_port, uport);
1157
1158         if (t->use_tx_dma) {
1159                 if (!tegra_dma_dequeue_req(t->tx_dma, &t->tx_dma_req))
1160                         tegra_tx_dma_complete_req(t, &t->tx_dma_req);
1161                 t->tx_dma_req.size = 0;
1162         }
1163         return;
1164 }
1165
1166
1167 static void tegra_pm(struct uart_port *u, unsigned int state,
1168         unsigned int oldstate)
1169 {
1170
1171 }
1172
1173 static const char *tegra_type(struct uart_port *u)
1174 {
1175         return 0;
1176 }
1177
1178 static struct uart_ops tegra_uart_ops = {
1179         .tx_empty       = tegra_tx_empty,
1180         .set_mctrl      = tegra_set_mctrl,
1181         .get_mctrl      = tegra_get_mctrl,
1182         .stop_tx        = tegra_stop_tx,
1183         .start_tx       = tegra_start_tx,
1184         .stop_rx        = tegra_stop_rx,
1185         .flush_buffer   = tegra_flush_buffer,
1186         .enable_ms      = tegra_enable_ms,
1187         .break_ctl      = tegra_break_ctl,
1188         .startup        = tegra_startup,
1189         .shutdown       = tegra_shutdown,
1190         .set_termios    = tegra_set_termios,
1191         .pm             = tegra_pm,
1192         .type           = tegra_type,
1193         .request_port   = tegra_request_port,
1194         .release_port   = tegra_release_port,
1195 };
1196
1197 static int tegra_uart_probe(struct platform_device *pdev);
1198 static int __devexit tegra_uart_remove(struct platform_device *pdev);
1199 static int tegra_uart_suspend(struct platform_device *pdev, pm_message_t state);
1200 static int tegra_uart_resume(struct platform_device *pdev);
1201
1202 static struct platform_driver tegra_uart_platform_driver = {
1203         .remove         = tegra_uart_remove,
1204         .probe          = tegra_uart_probe,
1205         .suspend        = tegra_uart_suspend,
1206         .resume         = tegra_uart_resume,
1207         .driver         = {
1208                 .name   = "tegra_uart"
1209         }
1210 };
1211
1212 static struct uart_driver tegra_uart_driver =
1213 {
1214         .owner          = THIS_MODULE,
1215         .driver_name    = "tegra_uart",
1216         .dev_name       = "ttyHS",
1217         .cons           = 0,
1218         .nr             = 5,
1219 };
1220
1221 static int tegra_uart_suspend(struct platform_device *pdev, pm_message_t state)
1222 {
1223         struct tegra_uart_port *t = platform_get_drvdata(pdev);
1224         struct uart_port *u;
1225
1226         if (pdev->id < 0 || pdev->id > tegra_uart_driver.nr)
1227                 pr_err("Invalid Uart instance (%d)\n", pdev->id);
1228
1229         u = &t->uport;
1230         uart_suspend_port(&tegra_uart_driver, u);
1231
1232         flush_work(&t->tx_work);
1233         return 0;
1234 }
1235
1236 static int tegra_uart_resume(struct platform_device *pdev)
1237 {
1238         struct tegra_uart_port *t = platform_get_drvdata(pdev);
1239         struct uart_port *u;
1240
1241         if (pdev->id < 0 || pdev->id > tegra_uart_driver.nr)
1242                 pr_err("Invalid Uart instance (%d)\n", pdev->id);
1243
1244         u = &t->uport;
1245         uart_resume_port(&tegra_uart_driver, u);
1246         return 0;
1247 }
1248
1249
1250
1251 static int __devexit tegra_uart_remove(struct platform_device *pdev)
1252 {
1253         struct tegra_uart_port *t = platform_get_drvdata(pdev);
1254         struct uart_port *u;
1255
1256         if (pdev->id < 0 || pdev->id > tegra_uart_driver.nr)
1257                 pr_err("Invalid Uart instance (%d)\n", pdev->id);
1258
1259         u = &t->uport;
1260         uart_remove_one_port(&tegra_uart_driver, u);
1261
1262         platform_set_drvdata(pdev, NULL);
1263
1264         pr_info("Unregistered UART port %s%d\n",
1265                 tegra_uart_driver.dev_name, u->line);
1266         kfree(t);
1267         return 0;
1268 }
1269
1270 static int tegra_uart_probe(struct platform_device *pdev)
1271 {
1272         struct tegra_uart_port *t;
1273         struct uart_port *u;
1274         struct resource *resource;
1275         int ret;
1276         char name[64];
1277         if (pdev->id < 0 || pdev->id > tegra_uart_driver.nr) {
1278                 pr_err("Invalid Uart instance (%d)\n", pdev->id);
1279                 return -ENODEV;
1280         }
1281
1282         t = kzalloc(sizeof(struct tegra_uart_port), GFP_KERNEL);
1283         if (!t) {
1284                 pr_err("%s: Failed to allocate memory\n", __func__);
1285                 return -ENOMEM;
1286         }
1287         u = &t->uport;
1288         u->dev = &pdev->dev;
1289         platform_set_drvdata(pdev, u);
1290         u->line = pdev->id;
1291         u->ops = &tegra_uart_ops;
1292         u->type = ~PORT_UNKNOWN;
1293         u->fifosize = 32;
1294
1295         resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1296         if (unlikely(!resource))
1297                 return -ENXIO;
1298
1299         u->mapbase = resource->start;
1300         u->membase = IO_ADDRESS(u->mapbase);
1301         if (unlikely(!u->membase))
1302                 return -ENOMEM;
1303
1304         u->irq = platform_get_irq(pdev, 0);
1305         if (unlikely(u->irq < 0))
1306                 return -ENXIO;
1307
1308         u->regshift = 2;
1309
1310         t->clk = clk_get(&pdev->dev, NULL);
1311         if (!t->clk) {
1312                 dev_err(&pdev->dev, "Couldn't get the clock\n");
1313                 goto fail;
1314         }
1315
1316         ret = uart_add_one_port(&tegra_uart_driver, u);
1317         if (ret) {
1318                 pr_err("%s: Failed(%d) to add uart port %s%d\n",
1319                         __func__, ret, tegra_uart_driver.dev_name, u->line);
1320                 kfree(t);
1321                 platform_set_drvdata(pdev, NULL);
1322                 return ret;
1323         }
1324
1325         snprintf(name, sizeof(name), "tegra_hsuart_%d", u->line);
1326         pr_info("Registered UART port %s%d\n",
1327                 tegra_uart_driver.dev_name, u->line);
1328
1329         INIT_WORK(&t->tx_work, tegra_tx_dma_complete_work);
1330         return ret;
1331 fail:
1332         kfree(t);
1333         return -ENODEV;
1334 }
1335
1336 static int __init tegra_uart_init(void)
1337 {
1338         int ret;
1339
1340         ret = uart_register_driver(&tegra_uart_driver);
1341         if (unlikely(ret)) {
1342                 pr_err("Could not register %s driver\n",
1343                         tegra_uart_driver.driver_name);
1344                 return ret;
1345         }
1346
1347         ret = platform_driver_register(&tegra_uart_platform_driver);
1348         if (unlikely(ret)) {
1349                 pr_err("Could not register the UART platfrom "
1350                         "driver\n");
1351                 uart_unregister_driver(&tegra_uart_driver);
1352                 return ret;
1353         }
1354
1355         pr_info("Initialized tegra uart driver\n");
1356         return 0;
1357 }
1358
1359 static void __exit tegra_uart_exit(void)
1360 {
1361         pr_info("Unloading tegra uart driver\n");
1362         platform_driver_unregister(&tegra_uart_platform_driver);
1363         uart_unregister_driver(&tegra_uart_driver);
1364 }
1365
1366 module_init(tegra_uart_init);
1367 module_exit(tegra_uart_exit);
1368 MODULE_DESCRIPTION("High speed UART driver for tegra chipset");