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