This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / serial / imx.c
1 /*
2  *  linux/drivers/serial/imx.c
3  *
4  *  Driver for Motorola IMX serial ports
5  *
6  *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
7  *
8  *  Author: Sascha Hauer <sascha@saschahauer.de>
9  *  Copyright (C) 2004 Pengutronix
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  *
25  */
26 #include <linux/config.h>
27
28 #if defined(CONFIG_SERIAL_IMX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
29 #define SUPPORT_SYSRQ
30 #endif
31
32 #include <linux/module.h>
33 #include <linux/ioport.h>
34 #include <linux/init.h>
35 #include <linux/console.h>
36 #include <linux/sysrq.h>
37 #include <linux/device.h>
38 #include <linux/tty.h>
39 #include <linux/tty_flip.h>
40 #include <linux/serial_core.h>
41 #include <linux/serial.h>
42
43 #include <asm/io.h>
44 #include <asm/irq.h>
45 #include <asm/hardware.h>
46
47 /* We've been assigned a range on the "Low-density serial ports" major */
48 #define SERIAL_IMX_MAJOR        204
49 #define MINOR_START             41
50
51 #define NR_PORTS                2
52
53 #define IMX_ISR_PASS_LIMIT      256
54
55 /*
56  * This is the size of our serial port register set.
57  */
58 #define UART_PORT_SIZE  0x100
59
60 /*
61  * This determines how often we check the modem status signals
62  * for any change.  They generally aren't connected to an IRQ
63  * so we have to poll them.  We also check immediately before
64  * filling the TX fifo incase CTS has been dropped.
65  */
66 #define MCTRL_TIMEOUT   (250*HZ/1000)
67
68 #define DRIVER_NAME "IMX-uart"
69
70 struct imx_port {
71         struct uart_port        port;
72         struct timer_list       timer;
73         unsigned int            old_status;
74         int txirq,rxirq;
75 };
76
77 /*
78  * Handle any change of modem status signal since we were last called.
79  */
80 static void imx_mctrl_check(struct imx_port *sport)
81 {
82         unsigned int status, changed;
83
84         status = sport->port.ops->get_mctrl(&sport->port);
85         changed = status ^ sport->old_status;
86
87         if (changed == 0)
88                 return;
89
90         sport->old_status = status;
91
92         if (changed & TIOCM_RI)
93                 sport->port.icount.rng++;
94         if (changed & TIOCM_DSR)
95                 sport->port.icount.dsr++;
96         if (changed & TIOCM_CAR)
97                 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
98         if (changed & TIOCM_CTS)
99                 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
100
101         wake_up_interruptible(&sport->port.info->delta_msr_wait);
102 }
103
104 /*
105  * This is our per-port timeout handler, for checking the
106  * modem status signals.
107  */
108 static void imx_timeout(unsigned long data)
109 {
110         struct imx_port *sport = (struct imx_port *)data;
111         unsigned long flags;
112
113         if (sport->port.info) {
114                 spin_lock_irqsave(&sport->port.lock, flags);
115                 imx_mctrl_check(sport);
116                 spin_unlock_irqrestore(&sport->port.lock, flags);
117
118                 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
119         }
120 }
121
122 /*
123  * interrupts disabled on entry
124  */
125 static void imx_stop_tx(struct uart_port *port, unsigned int tty_stop)
126 {
127         struct imx_port *sport = (struct imx_port *)port;
128         UCR1((u32)sport->port.membase) &= ~UCR1_TXMPTYEN;
129 }
130
131 /*
132  * interrupts disabled on entry
133  */
134 static void imx_stop_rx(struct uart_port *port)
135 {
136         struct imx_port *sport = (struct imx_port *)port;
137         UCR2((u32)sport->port.membase) &= ~UCR2_RXEN;
138 }
139
140 /*
141  * Set the modem control timer to fire immediately.
142  */
143 static void imx_enable_ms(struct uart_port *port)
144 {
145         struct imx_port *sport = (struct imx_port *)port;
146
147         mod_timer(&sport->timer, jiffies);
148 }
149
150 static inline void imx_transmit_buffer(struct imx_port *sport)
151 {
152         struct circ_buf *xmit = &sport->port.info->xmit;
153
154         do {
155                 /* send xmit->buf[xmit->tail]
156                  * out the port here */
157                 URTX0((u32)sport->port.membase) = xmit->buf[xmit->tail];
158                 xmit->tail = (xmit->tail + 1) &
159                          (UART_XMIT_SIZE - 1);
160                 sport->port.icount.tx++;
161                 if (uart_circ_empty(xmit))
162                         break;
163         } while (!(UTS((u32)sport->port.membase) & UTS_TXFULL));
164
165         if (uart_circ_empty(xmit))
166                 imx_stop_tx(&sport->port, 0);
167 }
168
169 /*
170  * interrupts disabled on entry
171  */
172 static void imx_start_tx(struct uart_port *port, unsigned int tty_start)
173 {
174         struct imx_port *sport = (struct imx_port *)port;
175
176         UCR1((u32)sport->port.membase) |= UCR1_TXMPTYEN;
177
178         if(UTS((u32)sport->port.membase) & UTS_TXEMPTY)
179                 imx_transmit_buffer(sport);
180 }
181
182 static irqreturn_t imx_txint(int irq, void *dev_id, struct pt_regs *regs)
183 {
184         struct imx_port *sport = (struct imx_port *)dev_id;
185         struct circ_buf *xmit = &sport->port.info->xmit;
186         unsigned long flags;
187
188         spin_lock_irqsave(&sport->port.lock,flags);
189         if (sport->port.x_char)
190         {
191                 /* Send next char */
192                 URTX0((u32)sport->port.membase) = sport->port.x_char;
193                 goto out;
194         }
195
196         if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
197                 imx_stop_tx(&sport->port, 0);
198                 goto out;
199         }
200
201         imx_transmit_buffer(sport);
202
203         if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
204                 uart_write_wakeup(&sport->port);
205
206 out:
207         spin_unlock_irqrestore(&sport->port.lock,flags);
208         return IRQ_HANDLED;
209 }
210
211 static irqreturn_t imx_rxint(int irq, void *dev_id, struct pt_regs *regs)
212 {
213         struct imx_port *sport = dev_id;
214         unsigned int rx,flg,ignored = 0;
215         struct tty_struct *tty = sport->port.info->tty;
216         unsigned long flags;
217
218         rx = URXD0((u32)sport->port.membase);
219         spin_lock_irqsave(&sport->port.lock,flags);
220
221         do {
222                 flg = TTY_NORMAL;
223                 sport->port.icount.rx++;
224
225                 if( USR2((u32)sport->port.membase) & USR2_BRCD ) {
226                         USR2((u32)sport->port.membase) |= USR2_BRCD;
227                         if(uart_handle_break(&sport->port))
228                                 goto ignore_char;
229                 }
230
231                 if (uart_handle_sysrq_char
232                             (&sport->port, (unsigned char)rx, regs))
233                         goto ignore_char;
234
235                 if( rx & (URXD_PRERR | URXD_OVRRUN | URXD_FRMERR) )
236                         goto handle_error;
237
238         error_return:
239                 tty_insert_flip_char(tty, rx, flg);
240
241                 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
242                         goto out;
243
244         ignore_char:
245                 rx = URXD0((u32)sport->port.membase);
246         } while(rx & URXD_CHARRDY);
247
248 out:
249         spin_unlock_irqrestore(&sport->port.lock,flags);
250         tty_flip_buffer_push(tty);
251         return IRQ_HANDLED;
252
253 handle_error:
254         if (rx & URXD_PRERR)
255                 sport->port.icount.parity++;
256         else if (rx & URXD_FRMERR)
257                 sport->port.icount.frame++;
258         if (rx & URXD_OVRRUN)
259                 sport->port.icount.overrun++;
260
261         if (rx & sport->port.ignore_status_mask) {
262                 if (++ignored > 100)
263                         goto out;
264                 goto ignore_char;
265         }
266
267         rx &= sport->port.read_status_mask;
268
269         if (rx & URXD_PRERR)
270                 flg = TTY_PARITY;
271         else if (rx & URXD_FRMERR)
272                 flg = TTY_FRAME;
273         if (rx & URXD_OVRRUN)
274                 flg = TTY_OVERRUN;
275
276 #ifdef SUPPORT_SYSRQ
277         sport->port.sysrq = 0;
278 #endif
279         goto error_return;
280 }
281
282 /*
283  * Return TIOCSER_TEMT when transmitter is not busy.
284  */
285 static unsigned int imx_tx_empty(struct uart_port *port)
286 {
287         struct imx_port *sport = (struct imx_port *)port;
288
289         return USR2((u32)sport->port.membase) & USR2_TXDC ?  TIOCSER_TEMT : 0;
290 }
291
292 static unsigned int imx_get_mctrl(struct uart_port *port)
293 {
294         return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
295 }
296
297 static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
298 {
299 }
300
301 /*
302  * Interrupts always disabled.
303  */
304 static void imx_break_ctl(struct uart_port *port, int break_state)
305 {
306         struct imx_port *sport = (struct imx_port *)port;
307         unsigned long flags;
308
309         spin_lock_irqsave(&sport->port.lock, flags);
310
311         if ( break_state != 0 )
312                 UCR1((u32)sport->port.membase) |= UCR1_SNDBRK;
313         else
314                 UCR1((u32)sport->port.membase) &= ~UCR1_SNDBRK;
315
316         spin_unlock_irqrestore(&sport->port.lock, flags);
317 }
318
319 #define TXTL 2 /* reset default */
320 #define RXTL 1 /* reset default */
321
322 static int imx_startup(struct uart_port *port)
323 {
324         struct imx_port *sport = (struct imx_port *)port;
325         int retval;
326         unsigned int val;
327         unsigned long flags;
328
329         /* set receiver / transmitter trigger level. We assume
330          * that RFDIV has been set by the arch setup or by the bootloader.
331          */
332         val = (UFCR((u32)sport->port.membase) & UFCR_RFDIV)  | TXTL<<10 | RXTL;
333         UFCR((u32)sport->port.membase) = val;
334
335         /* disable the DREN bit (Data Ready interrupt enable) before
336          * requesting IRQs
337          */
338         UCR4((u32)sport->port.membase) &= ~UCR4_DREN;
339
340         /*
341          * Allocate the IRQ
342          */
343         retval = request_irq(sport->rxirq, imx_rxint, 0,
344                              DRIVER_NAME, sport);
345         if (retval) goto error_out2;
346
347         retval = request_irq(sport->txirq, imx_txint, 0,
348                              "imx-uart", sport);
349         if (retval) goto error_out1;
350
351         /*
352          * Finally, clear and enable interrupts
353          */
354
355         UCR1((u32)sport->port.membase) |=
356                          (UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_UARTEN);
357
358         UCR2((u32)sport->port.membase) |= (UCR2_RXEN | UCR2_TXEN);
359         /*
360          * Enable modem status interrupts
361          */
362         spin_lock_irqsave(&sport->port.lock,flags);
363         imx_enable_ms(&sport->port);
364         spin_unlock_irqrestore(&sport->port.lock,flags);
365
366         return 0;
367
368 error_out1:
369         free_irq(sport->rxirq, sport);
370 error_out2:
371         free_irq(sport->txirq, sport);
372         return retval;
373 }
374
375 static void imx_shutdown(struct uart_port *port)
376 {
377         struct imx_port *sport = (struct imx_port *)port;
378
379         /*
380          * Stop our timer.
381          */
382         del_timer_sync(&sport->timer);
383
384         /*
385          * Free the interrupts
386          */
387         free_irq(sport->txirq, sport);
388         free_irq(sport->rxirq, sport);
389
390         /*
391          * Disable all interrupts, port and break condition.
392          */
393
394         UCR1((u32)sport->port.membase) &=
395                          ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_UARTEN);
396 }
397
398 static void
399 imx_set_termios(struct uart_port *port, struct termios *termios,
400                    struct termios *old)
401 {
402         struct imx_port *sport = (struct imx_port *)port;
403         unsigned long flags;
404         unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
405         unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
406
407         /*
408          * If we don't support modem control lines, don't allow
409          * these to be set.
410          */
411         if (0) {
412                 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
413                 termios->c_cflag |= CLOCAL;
414         }
415
416         /*
417          * We only support CS7 and CS8.
418          */
419         while ((termios->c_cflag & CSIZE) != CS7 &&
420                (termios->c_cflag & CSIZE) != CS8) {
421                 termios->c_cflag &= ~CSIZE;
422                 termios->c_cflag |= old_csize;
423                 old_csize = CS8;
424         }
425
426         if ((termios->c_cflag & CSIZE) == CS8)
427                 ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
428         else
429                 ucr2 = UCR2_SRST | UCR2_IRTS;
430
431         if (termios->c_cflag & CSTOPB)
432                 ucr2 |= UCR2_STPB;
433         if (termios->c_cflag & PARENB) {
434                 ucr2 |= UCR2_PREN;
435                 if (!(termios->c_cflag & PARODD))
436                         ucr2 |= UCR2_PROE;
437         }
438
439         /*
440          * Ask the core to calculate the divisor for us.
441          */
442         baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
443         quot = uart_get_divisor(port, baud);
444
445         spin_lock_irqsave(&sport->port.lock, flags);
446
447         sport->port.read_status_mask = 0;
448         if (termios->c_iflag & INPCK)
449                 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
450         if (termios->c_iflag & (BRKINT | PARMRK))
451                 sport->port.read_status_mask |= URXD_BRK;
452
453         /*
454          * Characters to ignore
455          */
456         sport->port.ignore_status_mask = 0;
457         if (termios->c_iflag & IGNPAR)
458                 sport->port.ignore_status_mask |= URXD_PRERR;
459         if (termios->c_iflag & IGNBRK) {
460                 sport->port.ignore_status_mask |= URXD_BRK;
461                 /*
462                  * If we're ignoring parity and break indicators,
463                  * ignore overruns too (for real raw support).
464                  */
465                 if (termios->c_iflag & IGNPAR)
466                         sport->port.ignore_status_mask |= URXD_OVRRUN;
467         }
468
469         del_timer_sync(&sport->timer);
470
471         /*
472          * Update the per-port timeout.
473          */
474         uart_update_timeout(port, termios->c_cflag, baud);
475
476         /*
477          * disable interrupts and drain transmitter
478          */
479         old_ucr1 = UCR1((u32)sport->port.membase);
480         UCR1((u32)sport->port.membase) &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN);
481
482         while ( !(USR2((u32)sport->port.membase) & USR2_TXDC))
483                 barrier();
484
485         /* then, disable everything */
486         old_txrxen = UCR2((u32)sport->port.membase) & ( UCR2_TXEN | UCR2_RXEN );
487         UCR2((u32)sport->port.membase) &= ~( UCR2_TXEN | UCR2_RXEN);
488
489         /* set the parity, stop bits and data size */
490         UCR2((u32)sport->port.membase) = ucr2;
491
492         /* set the baud rate. We assume uartclk = 16 MHz
493          *
494          * baud * 16   UBIR - 1
495          * --------- = --------
496          *  uartclk    UBMR - 1
497          */
498         UBIR((u32)sport->port.membase) = (baud / 100) - 1;
499         UBMR((u32)sport->port.membase) = 10000 - 1;
500
501         UCR1((u32)sport->port.membase) = old_ucr1;
502         UCR2((u32)sport->port.membase) |= old_txrxen;
503
504         if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
505                 imx_enable_ms(&sport->port);
506
507         spin_unlock_irqrestore(&sport->port.lock, flags);
508 }
509
510 static const char *imx_type(struct uart_port *port)
511 {
512         struct imx_port *sport = (struct imx_port *)port;
513
514         return sport->port.type == PORT_IMX ? "IMX" : NULL;
515 }
516
517 /*
518  * Release the memory region(s) being used by 'port'.
519  */
520 static void imx_release_port(struct uart_port *port)
521 {
522         struct imx_port *sport = (struct imx_port *)port;
523
524         release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
525 }
526
527 /*
528  * Request the memory region(s) being used by 'port'.
529  */
530 static int imx_request_port(struct uart_port *port)
531 {
532         struct imx_port *sport = (struct imx_port *)port;
533
534         return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
535                         "imx-uart") != NULL ? 0 : -EBUSY;
536 }
537
538 /*
539  * Configure/autoconfigure the port.
540  */
541 static void imx_config_port(struct uart_port *port, int flags)
542 {
543         struct imx_port *sport = (struct imx_port *)port;
544
545         if (flags & UART_CONFIG_TYPE &&
546             imx_request_port(&sport->port) == 0)
547                 sport->port.type = PORT_IMX;
548 }
549
550 /*
551  * Verify the new serial_struct (for TIOCSSERIAL).
552  * The only change we allow are to the flags and type, and
553  * even then only between PORT_IMX and PORT_UNKNOWN
554  */
555 static int
556 imx_verify_port(struct uart_port *port, struct serial_struct *ser)
557 {
558         struct imx_port *sport = (struct imx_port *)port;
559         int ret = 0;
560
561         if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
562                 ret = -EINVAL;
563         if (sport->port.irq != ser->irq)
564                 ret = -EINVAL;
565         if (ser->io_type != UPIO_MEM)
566                 ret = -EINVAL;
567         if (sport->port.uartclk / 16 != ser->baud_base)
568                 ret = -EINVAL;
569         if ((void *)sport->port.mapbase != ser->iomem_base)
570                 ret = -EINVAL;
571         if (sport->port.iobase != ser->port)
572                 ret = -EINVAL;
573         if (ser->hub6 != 0)
574                 ret = -EINVAL;
575         return ret;
576 }
577
578 static struct uart_ops imx_pops = {
579         .tx_empty       = imx_tx_empty,
580         .set_mctrl      = imx_set_mctrl,
581         .get_mctrl      = imx_get_mctrl,
582         .stop_tx        = imx_stop_tx,
583         .start_tx       = imx_start_tx,
584         .stop_rx        = imx_stop_rx,
585         .enable_ms      = imx_enable_ms,
586         .break_ctl      = imx_break_ctl,
587         .startup        = imx_startup,
588         .shutdown       = imx_shutdown,
589         .set_termios    = imx_set_termios,
590         .type           = imx_type,
591         .release_port   = imx_release_port,
592         .request_port   = imx_request_port,
593         .config_port    = imx_config_port,
594         .verify_port    = imx_verify_port,
595 };
596
597 static struct imx_port imx_ports[] = {
598         {
599         .txirq  = UART1_MINT_TX,
600         .rxirq  = UART1_MINT_RX,
601         .port   = {
602                 .type           = PORT_IMX,
603                 .iotype         = SERIAL_IO_MEM,
604                 .membase        = (void *)IMX_UART1_BASE,
605                 .mapbase        = IMX_UART1_BASE, /* FIXME */
606                 .irq            = UART1_MINT_RX,
607                 .uartclk        = 16000000,
608                 .fifosize       = 8,
609                 .flags          = ASYNC_BOOT_AUTOCONF,
610                 .ops            = &imx_pops,
611                 .line           = 0,
612         },
613         }, {
614         .txirq  = UART2_MINT_TX,
615         .rxirq  = UART2_MINT_RX,
616         .port   = {
617                 .type           = PORT_IMX,
618                 .iotype         = SERIAL_IO_MEM,
619                 .membase        = (void *)IMX_UART2_BASE,
620                 .mapbase        = IMX_UART2_BASE, /* FIXME */
621                 .irq            = UART2_MINT_RX,
622                 .uartclk        = 16000000,
623                 .fifosize       = 8,
624                 .flags          = ASYNC_BOOT_AUTOCONF,
625                 .ops            = &imx_pops,
626                 .line           = 1,
627         },
628         }
629 };
630
631 /*
632  * Setup the IMX serial ports.
633  * Note also that we support "console=ttySMXx" where "x" is either 0 or 1.
634  * Which serial port this ends up being depends on the machine you're
635  * running this kernel on.  I'm not convinced that this is a good idea,
636  * but that's the way it traditionally works.
637  *
638  */
639 static void __init imx_init_ports(void)
640 {
641         static int first = 1;
642         int i;
643
644         if (!first)
645                 return;
646         first = 0;
647
648         for (i = 0; i < ARRAY_SIZE(imx_ports); i++) {
649                 init_timer(&imx_ports[i].timer);
650                 imx_ports[i].timer.function = imx_timeout;
651                 imx_ports[i].timer.data     = (unsigned long)&imx_ports[i];
652         }
653
654         imx_gpio_mode(PC9_PF_UART1_CTS);
655         imx_gpio_mode(PC10_PF_UART1_RTS);
656         imx_gpio_mode(PC11_PF_UART1_TXD);
657         imx_gpio_mode(PC12_PF_UART1_RXD);
658         imx_gpio_mode(PB28_PF_UART2_CTS);
659         imx_gpio_mode(PB29_PF_UART2_RTS);
660
661         imx_gpio_mode(PB30_PF_UART2_TXD);
662         imx_gpio_mode(PB31_PF_UART2_RXD);
663
664 #if 0 /* We don't need these, on the mx1 the _modem_ side of the uart
665        * is implemented.
666        */
667         imx_gpio_mode(PD7_AF_UART2_DTR);
668         imx_gpio_mode(PD8_AF_UART2_DCD);
669         imx_gpio_mode(PD9_AF_UART2_RI);
670         imx_gpio_mode(PD10_AF_UART2_DSR);
671 #endif
672
673
674 }
675
676 #ifdef CONFIG_SERIAL_IMX_CONSOLE
677
678 /*
679  * Interrupts are disabled on entering
680  */
681 static void
682 imx_console_write(struct console *co, const char *s, unsigned int count)
683 {
684         struct imx_port *sport = &imx_ports[co->index];
685         unsigned int old_ucr1, old_ucr2, i;
686
687         /*
688          *      First, save UCR1/2 and then disable interrupts
689          */
690         old_ucr1 = UCR1((u32)sport->port.membase);
691         old_ucr2 = UCR2((u32)sport->port.membase);
692
693         UCR1((u32)sport->port.membase) =
694                            (old_ucr1 | UCR1_UARTCLKEN | UCR1_UARTEN)
695                            & ~(UCR1_TXMPTYEN | UCR1_RRDYEN);
696         UCR2((u32)sport->port.membase) = old_ucr2 | UCR2_TXEN;
697
698         /*
699          *      Now, do each character
700          */
701         for (i = 0; i < count; i++) {
702
703                 while ((UTS((u32)sport->port.membase) & UTS_TXFULL))
704                         barrier();
705
706                 URTX0((u32)sport->port.membase) = s[i];
707
708                 if (s[i] == '\n') {
709                         while ((UTS((u32)sport->port.membase) & UTS_TXFULL))
710                                 barrier();
711                         URTX0((u32)sport->port.membase) = '\r';
712                 }
713         }
714
715         /*
716          *      Finally, wait for transmitter to become empty
717          *      and restore UCR1/2
718          */
719         while (!(USR2((u32)sport->port.membase) & USR2_TXDC));
720
721         UCR1((u32)sport->port.membase) = old_ucr1;
722         UCR2((u32)sport->port.membase) = old_ucr2;
723 }
724
725 /*
726  * If the port was already initialised (eg, by a boot loader),
727  * try to determine the current setup.
728  */
729 static void __init
730 imx_console_get_options(struct imx_port *sport, int *baud,
731                            int *parity, int *bits)
732 {
733         if ( UCR1((u32)sport->port.membase) | UCR1_UARTEN ) {
734                 /* ok, the port was enabled */
735                 unsigned int ucr2, ubir,ubmr, uartclk;
736
737                 ucr2 = UCR2((u32)sport->port.membase);
738
739                 *parity = 'n';
740                 if (ucr2 & UCR2_PREN) {
741                         if (ucr2 & UCR2_PROE)
742                                 *parity = 'o';
743                         else
744                                 *parity = 'e';
745                 }
746
747                 if (ucr2 & UCR2_WS)
748                         *bits = 8;
749                 else
750                         *bits = 7;
751
752                 ubir = UBIR((u32)sport->port.membase) & 0xffff;
753                 ubmr = UBMR((u32)sport->port.membase) & 0xffff;
754                 uartclk = sport->port.uartclk;
755
756                 *baud = ((uartclk/16) * (ubir + 1)) / (ubmr + 1);
757         }
758 }
759
760 static int __init
761 imx_console_setup(struct console *co, char *options)
762 {
763         struct imx_port *sport;
764         int baud = 9600;
765         int bits = 8;
766         int parity = 'n';
767         int flow = 'n';
768
769         /*
770          * Check whether an invalid uart number has been specified, and
771          * if so, search for the first available port that does have
772          * console support.
773          */
774         if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
775                 co->index = 0;
776         sport = &imx_ports[co->index];
777
778         if (options)
779                 uart_parse_options(options, &baud, &parity, &bits, &flow);
780         else
781                 imx_console_get_options(sport, &baud, &parity, &bits);
782
783         return uart_set_options(&sport->port, co, baud, parity, bits, flow);
784 }
785
786 extern struct uart_driver imx_reg;
787 static struct console imx_console = {
788         .name           = "ttySMX",
789         .write          = imx_console_write,
790         .device         = uart_console_device,
791         .setup          = imx_console_setup,
792         .flags          = CON_PRINTBUFFER,
793         .index          = -1,
794         .data           = &imx_reg,
795 };
796
797 static int __init imx_rs_console_init(void)
798 {
799         imx_init_ports();
800         register_console(&imx_console);
801         return 0;
802 }
803 console_initcall(imx_rs_console_init);
804
805 #define IMX_CONSOLE     &imx_console
806 #else
807 #define IMX_CONSOLE     NULL
808 #endif
809
810 static struct uart_driver imx_reg = {
811         .owner          = THIS_MODULE,
812         .driver_name    = DRIVER_NAME,
813         .dev_name       = "ttySMX",
814         .devfs_name     = "ttsmx/",
815         .major          = SERIAL_IMX_MAJOR,
816         .minor          = MINOR_START,
817         .nr             = ARRAY_SIZE(imx_ports),
818         .cons           = IMX_CONSOLE,
819 };
820
821 static int serial_imx_suspend(struct device *_dev, u32 state, u32 level)
822 {
823         struct imx_port *sport = dev_get_drvdata(_dev);
824
825         if (sport && level == SUSPEND_DISABLE)
826                 uart_suspend_port(&imx_reg, &sport->port);
827
828         return 0;
829 }
830
831 static int serial_imx_resume(struct device *_dev, u32 level)
832 {
833         struct imx_port *sport = dev_get_drvdata(_dev);
834
835         if (sport && level == RESUME_ENABLE)
836                 uart_resume_port(&imx_reg, &sport->port);
837
838         return 0;
839 }
840
841 static int serial_imx_probe(struct device *_dev)
842 {
843         struct platform_device *dev = to_platform_device(_dev);
844
845         imx_ports[dev->id].port.dev = _dev;
846         uart_add_one_port(&imx_reg, &imx_ports[dev->id].port);
847         dev_set_drvdata(_dev, &imx_ports[dev->id]);
848         return 0;
849 }
850
851 static int serial_imx_remove(struct device *_dev)
852 {
853         struct imx_port *sport = dev_get_drvdata(_dev);
854
855         dev_set_drvdata(_dev, NULL);
856
857         if (sport)
858                 uart_remove_one_port(&imx_reg, &sport->port);
859
860         return 0;
861 }
862
863 static struct device_driver serial_imx_driver = {
864         .name           = "imx-uart",
865         .bus            = &platform_bus_type,
866         .probe          = serial_imx_probe,
867         .remove         = serial_imx_remove,
868
869         .suspend        = serial_imx_suspend,
870         .resume         = serial_imx_resume,
871 };
872
873 static int __init imx_serial_init(void)
874 {
875         int ret;
876
877         printk(KERN_INFO "Serial: IMX driver\n");
878
879         imx_init_ports();
880
881         ret = uart_register_driver(&imx_reg);
882         if (ret)
883                 return ret;
884
885         ret = driver_register(&serial_imx_driver);
886         if (ret != 0)
887                 uart_unregister_driver(&imx_reg);
888
889         return 0;
890 }
891
892 static void __exit imx_serial_exit(void)
893 {
894         uart_unregister_driver(&imx_reg);
895 }
896
897 module_init(imx_serial_init);
898 module_exit(imx_serial_exit);
899
900 MODULE_AUTHOR("Sascha Hauer");
901 MODULE_DESCRIPTION("IMX generic serial port driver");
902 MODULE_LICENSE("GPL");