[PATCH] s390: bl_dev array size
[linux-2.6/kvm.git] / drivers / serial / imx.c
blob4c985e6b3784b0b611413c74deba61a98c086997
1 /*
2 * linux/drivers/serial/imx.c
4 * Driver for Motorola IMX serial ports
6 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
8 * Author: Sascha Hauer <sascha@saschahauer.de>
9 * Copyright (C) 2004 Pengutronix
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.
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.
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
25 * [29-Mar-2005] Mike Lee
26 * Added hardware handshake
28 #include <linux/config.h>
30 #if defined(CONFIG_SERIAL_IMX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
31 #define SUPPORT_SYSRQ
32 #endif
34 #include <linux/module.h>
35 #include <linux/ioport.h>
36 #include <linux/init.h>
37 #include <linux/console.h>
38 #include <linux/sysrq.h>
39 #include <linux/device.h>
40 #include <linux/tty.h>
41 #include <linux/tty_flip.h>
42 #include <linux/serial_core.h>
43 #include <linux/serial.h>
45 #include <asm/io.h>
46 #include <asm/irq.h>
47 #include <asm/hardware.h>
49 /* We've been assigned a range on the "Low-density serial ports" major */
50 #define SERIAL_IMX_MAJOR 204
51 #define MINOR_START 41
53 #define NR_PORTS 2
55 #define IMX_ISR_PASS_LIMIT 256
58 * This is the size of our serial port register set.
60 #define UART_PORT_SIZE 0x100
63 * This determines how often we check the modem status signals
64 * for any change. They generally aren't connected to an IRQ
65 * so we have to poll them. We also check immediately before
66 * filling the TX fifo incase CTS has been dropped.
68 #define MCTRL_TIMEOUT (250*HZ/1000)
70 #define DRIVER_NAME "IMX-uart"
72 struct imx_port {
73 struct uart_port port;
74 struct timer_list timer;
75 unsigned int old_status;
76 int txirq,rxirq;
80 * Handle any change of modem status signal since we were last called.
82 static void imx_mctrl_check(struct imx_port *sport)
84 unsigned int status, changed;
86 status = sport->port.ops->get_mctrl(&sport->port);
87 changed = status ^ sport->old_status;
89 if (changed == 0)
90 return;
92 sport->old_status = status;
94 if (changed & TIOCM_RI)
95 sport->port.icount.rng++;
96 if (changed & TIOCM_DSR)
97 sport->port.icount.dsr++;
98 if (changed & TIOCM_CAR)
99 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
100 if (changed & TIOCM_CTS)
101 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
103 wake_up_interruptible(&sport->port.info->delta_msr_wait);
107 * This is our per-port timeout handler, for checking the
108 * modem status signals.
110 static void imx_timeout(unsigned long data)
112 struct imx_port *sport = (struct imx_port *)data;
113 unsigned long flags;
115 if (sport->port.info) {
116 spin_lock_irqsave(&sport->port.lock, flags);
117 imx_mctrl_check(sport);
118 spin_unlock_irqrestore(&sport->port.lock, flags);
120 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
125 * interrupts disabled on entry
127 static void imx_stop_tx(struct uart_port *port)
129 struct imx_port *sport = (struct imx_port *)port;
130 UCR1((u32)sport->port.membase) &= ~UCR1_TXMPTYEN;
134 * interrupts disabled on entry
136 static void imx_stop_rx(struct uart_port *port)
138 struct imx_port *sport = (struct imx_port *)port;
139 UCR2((u32)sport->port.membase) &= ~UCR2_RXEN;
143 * Set the modem control timer to fire immediately.
145 static void imx_enable_ms(struct uart_port *port)
147 struct imx_port *sport = (struct imx_port *)port;
149 mod_timer(&sport->timer, jiffies);
152 static inline void imx_transmit_buffer(struct imx_port *sport)
154 struct circ_buf *xmit = &sport->port.info->xmit;
156 do {
157 /* send xmit->buf[xmit->tail]
158 * out the port here */
159 URTX0((u32)sport->port.membase) = xmit->buf[xmit->tail];
160 xmit->tail = (xmit->tail + 1) &
161 (UART_XMIT_SIZE - 1);
162 sport->port.icount.tx++;
163 if (uart_circ_empty(xmit))
164 break;
165 } while (!(UTS((u32)sport->port.membase) & UTS_TXFULL));
167 if (uart_circ_empty(xmit))
168 imx_stop_tx(&sport->port);
172 * interrupts disabled on entry
174 static void imx_start_tx(struct uart_port *port)
176 struct imx_port *sport = (struct imx_port *)port;
178 UCR1((u32)sport->port.membase) |= UCR1_TXMPTYEN;
180 if(UTS((u32)sport->port.membase) & UTS_TXEMPTY)
181 imx_transmit_buffer(sport);
184 static irqreturn_t imx_txint(int irq, void *dev_id, struct pt_regs *regs)
186 struct imx_port *sport = (struct imx_port *)dev_id;
187 struct circ_buf *xmit = &sport->port.info->xmit;
188 unsigned long flags;
190 spin_lock_irqsave(&sport->port.lock,flags);
191 if (sport->port.x_char)
193 /* Send next char */
194 URTX0((u32)sport->port.membase) = sport->port.x_char;
195 goto out;
198 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
199 imx_stop_tx(&sport->port);
200 goto out;
203 imx_transmit_buffer(sport);
205 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
206 uart_write_wakeup(&sport->port);
208 out:
209 spin_unlock_irqrestore(&sport->port.lock,flags);
210 return IRQ_HANDLED;
213 static irqreturn_t imx_rxint(int irq, void *dev_id, struct pt_regs *regs)
215 struct imx_port *sport = dev_id;
216 unsigned int rx,flg,ignored = 0;
217 struct tty_struct *tty = sport->port.info->tty;
218 unsigned long flags;
220 rx = URXD0((u32)sport->port.membase);
221 spin_lock_irqsave(&sport->port.lock,flags);
223 do {
224 flg = TTY_NORMAL;
225 sport->port.icount.rx++;
227 if( USR2((u32)sport->port.membase) & USR2_BRCD ) {
228 USR2((u32)sport->port.membase) |= USR2_BRCD;
229 if(uart_handle_break(&sport->port))
230 goto ignore_char;
233 if (uart_handle_sysrq_char
234 (&sport->port, (unsigned char)rx, regs))
235 goto ignore_char;
237 if( rx & (URXD_PRERR | URXD_OVRRUN | URXD_FRMERR) )
238 goto handle_error;
240 error_return:
241 tty_insert_flip_char(tty, rx, flg);
243 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
244 goto out;
246 ignore_char:
247 rx = URXD0((u32)sport->port.membase);
248 } while(rx & URXD_CHARRDY);
250 out:
251 spin_unlock_irqrestore(&sport->port.lock,flags);
252 tty_flip_buffer_push(tty);
253 return IRQ_HANDLED;
255 handle_error:
256 if (rx & URXD_PRERR)
257 sport->port.icount.parity++;
258 else if (rx & URXD_FRMERR)
259 sport->port.icount.frame++;
260 if (rx & URXD_OVRRUN)
261 sport->port.icount.overrun++;
263 if (rx & sport->port.ignore_status_mask) {
264 if (++ignored > 100)
265 goto out;
266 goto ignore_char;
269 rx &= sport->port.read_status_mask;
271 if (rx & URXD_PRERR)
272 flg = TTY_PARITY;
273 else if (rx & URXD_FRMERR)
274 flg = TTY_FRAME;
275 if (rx & URXD_OVRRUN)
276 flg = TTY_OVERRUN;
278 #ifdef SUPPORT_SYSRQ
279 sport->port.sysrq = 0;
280 #endif
281 goto error_return;
285 * Return TIOCSER_TEMT when transmitter is not busy.
287 static unsigned int imx_tx_empty(struct uart_port *port)
289 struct imx_port *sport = (struct imx_port *)port;
291 return USR2((u32)sport->port.membase) & USR2_TXDC ? TIOCSER_TEMT : 0;
295 * We have a modem side uart, so the meanings of RTS and CTS are inverted.
297 static unsigned int imx_get_mctrl(struct uart_port *port)
299 struct imx_port *sport = (struct imx_port *)port;
300 unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
302 if (USR1((u32)sport->port.membase) & USR1_RTSS)
303 tmp |= TIOCM_CTS;
305 if (UCR2((u32)sport->port.membase) & UCR2_CTS)
306 tmp |= TIOCM_RTS;
308 return tmp;
311 static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
313 struct imx_port *sport = (struct imx_port *)port;
315 if (mctrl & TIOCM_RTS)
316 UCR2((u32)sport->port.membase) |= UCR2_CTS;
317 else
318 UCR2((u32)sport->port.membase) &= ~UCR2_CTS;
322 * Interrupts always disabled.
324 static void imx_break_ctl(struct uart_port *port, int break_state)
326 struct imx_port *sport = (struct imx_port *)port;
327 unsigned long flags;
329 spin_lock_irqsave(&sport->port.lock, flags);
331 if ( break_state != 0 )
332 UCR1((u32)sport->port.membase) |= UCR1_SNDBRK;
333 else
334 UCR1((u32)sport->port.membase) &= ~UCR1_SNDBRK;
336 spin_unlock_irqrestore(&sport->port.lock, flags);
339 #define TXTL 2 /* reset default */
340 #define RXTL 1 /* reset default */
342 static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
344 unsigned int val;
345 unsigned int ufcr_rfdiv;
347 /* set receiver / transmitter trigger level.
348 * RFDIV is set such way to satisfy requested uartclk value
350 val = TXTL<<10 | RXTL;
351 ufcr_rfdiv = (imx_get_perclk1() + sport->port.uartclk / 2) / sport->port.uartclk;
353 if(!ufcr_rfdiv)
354 ufcr_rfdiv = 1;
356 if(ufcr_rfdiv >= 7)
357 ufcr_rfdiv = 6;
358 else
359 ufcr_rfdiv = 6 - ufcr_rfdiv;
361 val |= UFCR_RFDIV & (ufcr_rfdiv << 7);
363 UFCR((u32)sport->port.membase) = val;
365 return 0;
368 static int imx_startup(struct uart_port *port)
370 struct imx_port *sport = (struct imx_port *)port;
371 int retval;
372 unsigned long flags;
374 imx_setup_ufcr(sport, 0);
376 /* disable the DREN bit (Data Ready interrupt enable) before
377 * requesting IRQs
379 UCR4((u32)sport->port.membase) &= ~UCR4_DREN;
382 * Allocate the IRQ
384 retval = request_irq(sport->rxirq, imx_rxint, 0,
385 DRIVER_NAME, sport);
386 if (retval) goto error_out2;
388 retval = request_irq(sport->txirq, imx_txint, 0,
389 "imx-uart", sport);
390 if (retval) goto error_out1;
393 * Finally, clear and enable interrupts
396 UCR1((u32)sport->port.membase) |=
397 (UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_UARTEN);
399 UCR2((u32)sport->port.membase) |= (UCR2_RXEN | UCR2_TXEN);
401 * Enable modem status interrupts
403 spin_lock_irqsave(&sport->port.lock,flags);
404 imx_enable_ms(&sport->port);
405 spin_unlock_irqrestore(&sport->port.lock,flags);
407 return 0;
409 error_out1:
410 free_irq(sport->rxirq, sport);
411 error_out2:
412 free_irq(sport->txirq, sport);
413 return retval;
416 static void imx_shutdown(struct uart_port *port)
418 struct imx_port *sport = (struct imx_port *)port;
421 * Stop our timer.
423 del_timer_sync(&sport->timer);
426 * Free the interrupts
428 free_irq(sport->txirq, sport);
429 free_irq(sport->rxirq, sport);
432 * Disable all interrupts, port and break condition.
435 UCR1((u32)sport->port.membase) &=
436 ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_UARTEN);
439 static void
440 imx_set_termios(struct uart_port *port, struct termios *termios,
441 struct termios *old)
443 struct imx_port *sport = (struct imx_port *)port;
444 unsigned long flags;
445 unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
446 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
449 * If we don't support modem control lines, don't allow
450 * these to be set.
452 if (0) {
453 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
454 termios->c_cflag |= CLOCAL;
458 * We only support CS7 and CS8.
460 while ((termios->c_cflag & CSIZE) != CS7 &&
461 (termios->c_cflag & CSIZE) != CS8) {
462 termios->c_cflag &= ~CSIZE;
463 termios->c_cflag |= old_csize;
464 old_csize = CS8;
467 if ((termios->c_cflag & CSIZE) == CS8)
468 ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
469 else
470 ucr2 = UCR2_SRST | UCR2_IRTS;
472 if (termios->c_cflag & CRTSCTS) {
473 ucr2 &= ~UCR2_IRTS;
474 ucr2 |= UCR2_CTSC;
477 if (termios->c_cflag & CSTOPB)
478 ucr2 |= UCR2_STPB;
479 if (termios->c_cflag & PARENB) {
480 ucr2 |= UCR2_PREN;
481 if (!(termios->c_cflag & PARODD))
482 ucr2 |= UCR2_PROE;
486 * Ask the core to calculate the divisor for us.
488 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
489 quot = uart_get_divisor(port, baud);
491 spin_lock_irqsave(&sport->port.lock, flags);
493 sport->port.read_status_mask = 0;
494 if (termios->c_iflag & INPCK)
495 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
496 if (termios->c_iflag & (BRKINT | PARMRK))
497 sport->port.read_status_mask |= URXD_BRK;
500 * Characters to ignore
502 sport->port.ignore_status_mask = 0;
503 if (termios->c_iflag & IGNPAR)
504 sport->port.ignore_status_mask |= URXD_PRERR;
505 if (termios->c_iflag & IGNBRK) {
506 sport->port.ignore_status_mask |= URXD_BRK;
508 * If we're ignoring parity and break indicators,
509 * ignore overruns too (for real raw support).
511 if (termios->c_iflag & IGNPAR)
512 sport->port.ignore_status_mask |= URXD_OVRRUN;
515 del_timer_sync(&sport->timer);
518 * Update the per-port timeout.
520 uart_update_timeout(port, termios->c_cflag, baud);
523 * disable interrupts and drain transmitter
525 old_ucr1 = UCR1((u32)sport->port.membase);
526 UCR1((u32)sport->port.membase) &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN);
528 while ( !(USR2((u32)sport->port.membase) & USR2_TXDC))
529 barrier();
531 /* then, disable everything */
532 old_txrxen = UCR2((u32)sport->port.membase) & ( UCR2_TXEN | UCR2_RXEN );
533 UCR2((u32)sport->port.membase) &= ~( UCR2_TXEN | UCR2_RXEN);
535 /* set the parity, stop bits and data size */
536 UCR2((u32)sport->port.membase) = ucr2;
538 /* set the baud rate. We assume uartclk = 16 MHz
540 * baud * 16 UBIR - 1
541 * --------- = --------
542 * uartclk UBMR - 1
544 UBIR((u32)sport->port.membase) = (baud / 100) - 1;
545 UBMR((u32)sport->port.membase) = 10000 - 1;
547 UCR1((u32)sport->port.membase) = old_ucr1;
548 UCR2((u32)sport->port.membase) |= old_txrxen;
550 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
551 imx_enable_ms(&sport->port);
553 spin_unlock_irqrestore(&sport->port.lock, flags);
556 static const char *imx_type(struct uart_port *port)
558 struct imx_port *sport = (struct imx_port *)port;
560 return sport->port.type == PORT_IMX ? "IMX" : NULL;
564 * Release the memory region(s) being used by 'port'.
566 static void imx_release_port(struct uart_port *port)
568 struct imx_port *sport = (struct imx_port *)port;
570 release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
574 * Request the memory region(s) being used by 'port'.
576 static int imx_request_port(struct uart_port *port)
578 struct imx_port *sport = (struct imx_port *)port;
580 return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
581 "imx-uart") != NULL ? 0 : -EBUSY;
585 * Configure/autoconfigure the port.
587 static void imx_config_port(struct uart_port *port, int flags)
589 struct imx_port *sport = (struct imx_port *)port;
591 if (flags & UART_CONFIG_TYPE &&
592 imx_request_port(&sport->port) == 0)
593 sport->port.type = PORT_IMX;
597 * Verify the new serial_struct (for TIOCSSERIAL).
598 * The only change we allow are to the flags and type, and
599 * even then only between PORT_IMX and PORT_UNKNOWN
601 static int
602 imx_verify_port(struct uart_port *port, struct serial_struct *ser)
604 struct imx_port *sport = (struct imx_port *)port;
605 int ret = 0;
607 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
608 ret = -EINVAL;
609 if (sport->port.irq != ser->irq)
610 ret = -EINVAL;
611 if (ser->io_type != UPIO_MEM)
612 ret = -EINVAL;
613 if (sport->port.uartclk / 16 != ser->baud_base)
614 ret = -EINVAL;
615 if ((void *)sport->port.mapbase != ser->iomem_base)
616 ret = -EINVAL;
617 if (sport->port.iobase != ser->port)
618 ret = -EINVAL;
619 if (ser->hub6 != 0)
620 ret = -EINVAL;
621 return ret;
624 static struct uart_ops imx_pops = {
625 .tx_empty = imx_tx_empty,
626 .set_mctrl = imx_set_mctrl,
627 .get_mctrl = imx_get_mctrl,
628 .stop_tx = imx_stop_tx,
629 .start_tx = imx_start_tx,
630 .stop_rx = imx_stop_rx,
631 .enable_ms = imx_enable_ms,
632 .break_ctl = imx_break_ctl,
633 .startup = imx_startup,
634 .shutdown = imx_shutdown,
635 .set_termios = imx_set_termios,
636 .type = imx_type,
637 .release_port = imx_release_port,
638 .request_port = imx_request_port,
639 .config_port = imx_config_port,
640 .verify_port = imx_verify_port,
643 static struct imx_port imx_ports[] = {
645 .txirq = UART1_MINT_TX,
646 .rxirq = UART1_MINT_RX,
647 .port = {
648 .type = PORT_IMX,
649 .iotype = SERIAL_IO_MEM,
650 .membase = (void *)IMX_UART1_BASE,
651 .mapbase = IMX_UART1_BASE, /* FIXME */
652 .irq = UART1_MINT_RX,
653 .uartclk = 16000000,
654 .fifosize = 8,
655 .flags = ASYNC_BOOT_AUTOCONF,
656 .ops = &imx_pops,
657 .line = 0,
659 }, {
660 .txirq = UART2_MINT_TX,
661 .rxirq = UART2_MINT_RX,
662 .port = {
663 .type = PORT_IMX,
664 .iotype = SERIAL_IO_MEM,
665 .membase = (void *)IMX_UART2_BASE,
666 .mapbase = IMX_UART2_BASE, /* FIXME */
667 .irq = UART2_MINT_RX,
668 .uartclk = 16000000,
669 .fifosize = 8,
670 .flags = ASYNC_BOOT_AUTOCONF,
671 .ops = &imx_pops,
672 .line = 1,
678 * Setup the IMX serial ports.
679 * Note also that we support "console=ttySMXx" where "x" is either 0 or 1.
680 * Which serial port this ends up being depends on the machine you're
681 * running this kernel on. I'm not convinced that this is a good idea,
682 * but that's the way it traditionally works.
685 static void __init imx_init_ports(void)
687 static int first = 1;
688 int i;
690 if (!first)
691 return;
692 first = 0;
694 for (i = 0; i < ARRAY_SIZE(imx_ports); i++) {
695 init_timer(&imx_ports[i].timer);
696 imx_ports[i].timer.function = imx_timeout;
697 imx_ports[i].timer.data = (unsigned long)&imx_ports[i];
700 imx_gpio_mode(PC9_PF_UART1_CTS);
701 imx_gpio_mode(PC10_PF_UART1_RTS);
702 imx_gpio_mode(PC11_PF_UART1_TXD);
703 imx_gpio_mode(PC12_PF_UART1_RXD);
704 imx_gpio_mode(PB28_PF_UART2_CTS);
705 imx_gpio_mode(PB29_PF_UART2_RTS);
707 imx_gpio_mode(PB30_PF_UART2_TXD);
708 imx_gpio_mode(PB31_PF_UART2_RXD);
710 #if 0 /* We don't need these, on the mx1 the _modem_ side of the uart
711 * is implemented.
713 imx_gpio_mode(PD7_AF_UART2_DTR);
714 imx_gpio_mode(PD8_AF_UART2_DCD);
715 imx_gpio_mode(PD9_AF_UART2_RI);
716 imx_gpio_mode(PD10_AF_UART2_DSR);
717 #endif
722 #ifdef CONFIG_SERIAL_IMX_CONSOLE
725 * Interrupts are disabled on entering
727 static void
728 imx_console_write(struct console *co, const char *s, unsigned int count)
730 struct imx_port *sport = &imx_ports[co->index];
731 unsigned int old_ucr1, old_ucr2, i;
734 * First, save UCR1/2 and then disable interrupts
736 old_ucr1 = UCR1((u32)sport->port.membase);
737 old_ucr2 = UCR2((u32)sport->port.membase);
739 UCR1((u32)sport->port.membase) =
740 (old_ucr1 | UCR1_UARTCLKEN | UCR1_UARTEN)
741 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN);
742 UCR2((u32)sport->port.membase) = old_ucr2 | UCR2_TXEN;
745 * Now, do each character
747 for (i = 0; i < count; i++) {
749 while ((UTS((u32)sport->port.membase) & UTS_TXFULL))
750 barrier();
752 URTX0((u32)sport->port.membase) = s[i];
754 if (s[i] == '\n') {
755 while ((UTS((u32)sport->port.membase) & UTS_TXFULL))
756 barrier();
757 URTX0((u32)sport->port.membase) = '\r';
762 * Finally, wait for transmitter to become empty
763 * and restore UCR1/2
765 while (!(USR2((u32)sport->port.membase) & USR2_TXDC));
767 UCR1((u32)sport->port.membase) = old_ucr1;
768 UCR2((u32)sport->port.membase) = old_ucr2;
772 * If the port was already initialised (eg, by a boot loader),
773 * try to determine the current setup.
775 static void __init
776 imx_console_get_options(struct imx_port *sport, int *baud,
777 int *parity, int *bits)
780 if ( UCR1((u32)sport->port.membase) | UCR1_UARTEN ) {
781 /* ok, the port was enabled */
782 unsigned int ucr2, ubir,ubmr, uartclk;
783 unsigned int baud_raw;
784 unsigned int ucfr_rfdiv;
786 ucr2 = UCR2((u32)sport->port.membase);
788 *parity = 'n';
789 if (ucr2 & UCR2_PREN) {
790 if (ucr2 & UCR2_PROE)
791 *parity = 'o';
792 else
793 *parity = 'e';
796 if (ucr2 & UCR2_WS)
797 *bits = 8;
798 else
799 *bits = 7;
801 ubir = UBIR((u32)sport->port.membase) & 0xffff;
802 ubmr = UBMR((u32)sport->port.membase) & 0xffff;
805 ucfr_rfdiv = (UFCR((u32)sport->port.membase) & UFCR_RFDIV) >> 7;
806 if (ucfr_rfdiv == 6)
807 ucfr_rfdiv = 7;
808 else
809 ucfr_rfdiv = 6 - ucfr_rfdiv;
811 uartclk = imx_get_perclk1();
812 uartclk /= ucfr_rfdiv;
814 { /*
815 * The next code provides exact computation of
816 * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
817 * without need of float support or long long division,
818 * which would be required to prevent 32bit arithmetic overflow
820 unsigned int mul = ubir + 1;
821 unsigned int div = 16 * (ubmr + 1);
822 unsigned int rem = uartclk % div;
824 baud_raw = (uartclk / div) * mul;
825 baud_raw += (rem * mul + div / 2) / div;
826 *baud = (baud_raw + 50) / 100 * 100;
829 if(*baud != baud_raw)
830 printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
831 baud_raw, *baud);
835 static int __init
836 imx_console_setup(struct console *co, char *options)
838 struct imx_port *sport;
839 int baud = 9600;
840 int bits = 8;
841 int parity = 'n';
842 int flow = 'n';
845 * Check whether an invalid uart number has been specified, and
846 * if so, search for the first available port that does have
847 * console support.
849 if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
850 co->index = 0;
851 sport = &imx_ports[co->index];
853 if (options)
854 uart_parse_options(options, &baud, &parity, &bits, &flow);
855 else
856 imx_console_get_options(sport, &baud, &parity, &bits);
858 imx_setup_ufcr(sport, 0);
860 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
863 extern struct uart_driver imx_reg;
864 static struct console imx_console = {
865 .name = "ttySMX",
866 .write = imx_console_write,
867 .device = uart_console_device,
868 .setup = imx_console_setup,
869 .flags = CON_PRINTBUFFER,
870 .index = -1,
871 .data = &imx_reg,
874 static int __init imx_rs_console_init(void)
876 imx_init_ports();
877 register_console(&imx_console);
878 return 0;
880 console_initcall(imx_rs_console_init);
882 #define IMX_CONSOLE &imx_console
883 #else
884 #define IMX_CONSOLE NULL
885 #endif
887 static struct uart_driver imx_reg = {
888 .owner = THIS_MODULE,
889 .driver_name = DRIVER_NAME,
890 .dev_name = "ttySMX",
891 .devfs_name = "ttsmx/",
892 .major = SERIAL_IMX_MAJOR,
893 .minor = MINOR_START,
894 .nr = ARRAY_SIZE(imx_ports),
895 .cons = IMX_CONSOLE,
898 static int serial_imx_suspend(struct device *_dev, pm_message_t state, u32 level)
900 struct imx_port *sport = dev_get_drvdata(_dev);
902 if (sport && level == SUSPEND_DISABLE)
903 uart_suspend_port(&imx_reg, &sport->port);
905 return 0;
908 static int serial_imx_resume(struct device *_dev, u32 level)
910 struct imx_port *sport = dev_get_drvdata(_dev);
912 if (sport && level == RESUME_ENABLE)
913 uart_resume_port(&imx_reg, &sport->port);
915 return 0;
918 static int serial_imx_probe(struct device *_dev)
920 struct platform_device *dev = to_platform_device(_dev);
922 imx_ports[dev->id].port.dev = _dev;
923 uart_add_one_port(&imx_reg, &imx_ports[dev->id].port);
924 dev_set_drvdata(_dev, &imx_ports[dev->id]);
925 return 0;
928 static int serial_imx_remove(struct device *_dev)
930 struct imx_port *sport = dev_get_drvdata(_dev);
932 dev_set_drvdata(_dev, NULL);
934 if (sport)
935 uart_remove_one_port(&imx_reg, &sport->port);
937 return 0;
940 static struct device_driver serial_imx_driver = {
941 .name = "imx-uart",
942 .bus = &platform_bus_type,
943 .probe = serial_imx_probe,
944 .remove = serial_imx_remove,
946 .suspend = serial_imx_suspend,
947 .resume = serial_imx_resume,
950 static int __init imx_serial_init(void)
952 int ret;
954 printk(KERN_INFO "Serial: IMX driver\n");
956 imx_init_ports();
958 ret = uart_register_driver(&imx_reg);
959 if (ret)
960 return ret;
962 ret = driver_register(&serial_imx_driver);
963 if (ret != 0)
964 uart_unregister_driver(&imx_reg);
966 return 0;
969 static void __exit imx_serial_exit(void)
971 uart_unregister_driver(&imx_reg);
974 module_init(imx_serial_init);
975 module_exit(imx_serial_exit);
977 MODULE_AUTHOR("Sascha Hauer");
978 MODULE_DESCRIPTION("IMX generic serial port driver");
979 MODULE_LICENSE("GPL");