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 * Copyright (C) 2009 emlix GmbH
12 * Author: Fabian Godehardt (added IrDA support for iMX)
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * [29-Mar-2005] Mike Lee
29 * Added hardware handshake
32 #if defined(CONFIG_SERIAL_IMX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
36 #include <linux/module.h>
37 #include <linux/ioport.h>
38 #include <linux/init.h>
39 #include <linux/console.h>
40 #include <linux/sysrq.h>
41 #include <linux/platform_device.h>
42 #include <linux/tty.h>
43 #include <linux/tty_flip.h>
44 #include <linux/serial_core.h>
45 #include <linux/serial.h>
46 #include <linux/clk.h>
47 #include <linux/delay.h>
48 #include <linux/rational.h>
52 #include <mach/hardware.h>
53 #include <mach/imx-uart.h>
55 /* Register definitions */
56 #define URXD0 0x0 /* Receiver Register */
57 #define URTX0 0x40 /* Transmitter Register */
58 #define UCR1 0x80 /* Control Register 1 */
59 #define UCR2 0x84 /* Control Register 2 */
60 #define UCR3 0x88 /* Control Register 3 */
61 #define UCR4 0x8c /* Control Register 4 */
62 #define UFCR 0x90 /* FIFO Control Register */
63 #define USR1 0x94 /* Status Register 1 */
64 #define USR2 0x98 /* Status Register 2 */
65 #define UESC 0x9c /* Escape Character Register */
66 #define UTIM 0xa0 /* Escape Timer Register */
67 #define UBIR 0xa4 /* BRM Incremental Register */
68 #define UBMR 0xa8 /* BRM Modulator Register */
69 #define UBRC 0xac /* Baud Rate Count Register */
70 #if defined CONFIG_ARCH_MX3 || defined CONFIG_ARCH_MX2
71 #define ONEMS 0xb0 /* One Millisecond register */
72 #define UTS 0xb4 /* UART Test Register */
74 #ifdef CONFIG_ARCH_MX1
75 #define BIPR1 0xb0 /* Incremental Preset Register 1 */
76 #define BIPR2 0xb4 /* Incremental Preset Register 2 */
77 #define BIPR3 0xb8 /* Incremental Preset Register 3 */
78 #define BIPR4 0xbc /* Incremental Preset Register 4 */
79 #define BMPR1 0xc0 /* BRM Modulator Register 1 */
80 #define BMPR2 0xc4 /* BRM Modulator Register 2 */
81 #define BMPR3 0xc8 /* BRM Modulator Register 3 */
82 #define BMPR4 0xcc /* BRM Modulator Register 4 */
83 #define UTS 0xd0 /* UART Test Register */
86 /* UART Control Register Bit Fields.*/
87 #define URXD_CHARRDY (1<<15)
88 #define URXD_ERR (1<<14)
89 #define URXD_OVRRUN (1<<13)
90 #define URXD_FRMERR (1<<12)
91 #define URXD_BRK (1<<11)
92 #define URXD_PRERR (1<<10)
93 #define UCR1_ADEN (1<<15) /* Auto dectect interrupt */
94 #define UCR1_ADBR (1<<14) /* Auto detect baud rate */
95 #define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
96 #define UCR1_IDEN (1<<12) /* Idle condition interrupt */
97 #define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
98 #define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
99 #define UCR1_IREN (1<<7) /* Infrared interface enable */
100 #define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
101 #define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
102 #define UCR1_SNDBRK (1<<4) /* Send break */
103 #define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
104 #ifdef CONFIG_ARCH_MX1
105 #define UCR1_UARTCLKEN (1<<2) /* UART clock enabled */
107 #if defined CONFIG_ARCH_MX3 || defined CONFIG_ARCH_MX2
108 #define UCR1_UARTCLKEN (0) /* not present on mx2/mx3 */
110 #define UCR1_DOZE (1<<1) /* Doze */
111 #define UCR1_UARTEN (1<<0) /* UART enabled */
112 #define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
113 #define UCR2_IRTS (1<<14) /* Ignore RTS pin */
114 #define UCR2_CTSC (1<<13) /* CTS pin control */
115 #define UCR2_CTS (1<<12) /* Clear to send */
116 #define UCR2_ESCEN (1<<11) /* Escape enable */
117 #define UCR2_PREN (1<<8) /* Parity enable */
118 #define UCR2_PROE (1<<7) /* Parity odd/even */
119 #define UCR2_STPB (1<<6) /* Stop */
120 #define UCR2_WS (1<<5) /* Word size */
121 #define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
122 #define UCR2_TXEN (1<<2) /* Transmitter enabled */
123 #define UCR2_RXEN (1<<1) /* Receiver enabled */
124 #define UCR2_SRST (1<<0) /* SW reset */
125 #define UCR3_DTREN (1<<13) /* DTR interrupt enable */
126 #define UCR3_PARERREN (1<<12) /* Parity enable */
127 #define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
128 #define UCR3_DSR (1<<10) /* Data set ready */
129 #define UCR3_DCD (1<<9) /* Data carrier detect */
130 #define UCR3_RI (1<<8) /* Ring indicator */
131 #define UCR3_TIMEOUTEN (1<<7) /* Timeout interrupt enable */
132 #define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
133 #define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
134 #define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
135 #ifdef CONFIG_ARCH_MX1
136 #define UCR3_REF25 (1<<3) /* Ref freq 25 MHz, only on mx1 */
137 #define UCR3_REF30 (1<<2) /* Ref Freq 30 MHz, only on mx1 */
139 #if defined CONFIG_ARCH_MX2 || defined CONFIG_ARCH_MX3
140 #define UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select, on mx2/mx3 */
142 #define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
143 #define UCR3_BPEN (1<<0) /* Preset registers enable */
144 #define UCR4_CTSTL_32 (32<<10) /* CTS trigger level (32 chars) */
145 #define UCR4_INVR (1<<9) /* Inverted infrared reception */
146 #define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
147 #define UCR4_WKEN (1<<7) /* Wake interrupt enable */
148 #define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
149 #define UCR4_IRSC (1<<5) /* IR special case */
150 #define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
151 #define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
152 #define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
153 #define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
154 #define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
155 #define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
156 #define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
157 #define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
158 #define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
159 #define USR1_RTSS (1<<14) /* RTS pin status */
160 #define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
161 #define USR1_RTSD (1<<12) /* RTS delta */
162 #define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
163 #define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
164 #define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
165 #define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */
166 #define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
167 #define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
168 #define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
169 #define USR2_ADET (1<<15) /* Auto baud rate detect complete */
170 #define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
171 #define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
172 #define USR2_IDLE (1<<12) /* Idle condition */
173 #define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
174 #define USR2_WAKE (1<<7) /* Wake */
175 #define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
176 #define USR2_TXDC (1<<3) /* Transmitter complete */
177 #define USR2_BRCD (1<<2) /* Break condition */
178 #define USR2_ORE (1<<1) /* Overrun error */
179 #define USR2_RDR (1<<0) /* Recv data ready */
180 #define UTS_FRCPERR (1<<13) /* Force parity error */
181 #define UTS_LOOP (1<<12) /* Loop tx and rx */
182 #define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
183 #define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
184 #define UTS_TXFULL (1<<4) /* TxFIFO full */
185 #define UTS_RXFULL (1<<3) /* RxFIFO full */
186 #define UTS_SOFTRST (1<<0) /* Software reset */
188 /* We've been assigned a range on the "Low-density serial ports" major */
189 #ifdef CONFIG_ARCH_MXC
190 #define SERIAL_IMX_MAJOR 207
191 #define MINOR_START 16
192 #define DEV_NAME "ttymxc"
193 #define MAX_INTERNAL_IRQ MXC_INTERNAL_IRQS
197 * This determines how often we check the modem status signals
198 * for any change. They generally aren't connected to an IRQ
199 * so we have to poll them. We also check immediately before
200 * filling the TX fifo incase CTS has been dropped.
202 #define MCTRL_TIMEOUT (250*HZ/1000)
204 #define DRIVER_NAME "IMX-uart"
209 struct uart_port port;
210 struct timer_list timer;
211 unsigned int old_status;
212 int txirq,rxirq,rtsirq;
213 unsigned int have_rtscts:1;
214 unsigned int use_irda:1;
215 unsigned int irda_inv_rx:1;
216 unsigned int irda_inv_tx:1;
217 unsigned short trcv_delay; /* transceiver delay */
222 #define USE_IRDA(sport) ((sport)->use_irda)
224 #define USE_IRDA(sport) (0)
228 * Handle any change of modem status signal since we were last called.
230 static void imx_mctrl_check(struct imx_port *sport)
232 unsigned int status, changed;
234 status = sport->port.ops->get_mctrl(&sport->port);
235 changed = status ^ sport->old_status;
240 sport->old_status = status;
242 if (changed & TIOCM_RI)
243 sport->port.icount.rng++;
244 if (changed & TIOCM_DSR)
245 sport->port.icount.dsr++;
246 if (changed & TIOCM_CAR)
247 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
248 if (changed & TIOCM_CTS)
249 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
251 wake_up_interruptible(&sport->port.info->delta_msr_wait);
255 * This is our per-port timeout handler, for checking the
256 * modem status signals.
258 static void imx_timeout(unsigned long data)
260 struct imx_port *sport = (struct imx_port *)data;
263 if (sport->port.info) {
264 spin_lock_irqsave(&sport->port.lock, flags);
265 imx_mctrl_check(sport);
266 spin_unlock_irqrestore(&sport->port.lock, flags);
268 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
273 * interrupts disabled on entry
275 static void imx_stop_tx(struct uart_port *port)
277 struct imx_port *sport = (struct imx_port *)port;
280 if (USE_IRDA(sport)) {
281 /* half duplex - wait for end of transmission */
284 !(readl(sport->port.membase + USR2) & USR2_TXDC)) {
289 * irda transceiver - wait a bit more to avoid
290 * cutoff, hardware dependent
292 udelay(sport->trcv_delay);
295 * half duplex - reactivate receive mode,
296 * flush receive pipe echo crap
298 if (readl(sport->port.membase + USR2) & USR2_TXDC) {
299 temp = readl(sport->port.membase + UCR1);
300 temp &= ~(UCR1_TXMPTYEN | UCR1_TRDYEN);
301 writel(temp, sport->port.membase + UCR1);
303 temp = readl(sport->port.membase + UCR4);
304 temp &= ~(UCR4_TCEN);
305 writel(temp, sport->port.membase + UCR4);
307 while (readl(sport->port.membase + URXD0) &
311 temp = readl(sport->port.membase + UCR1);
313 writel(temp, sport->port.membase + UCR1);
315 temp = readl(sport->port.membase + UCR4);
317 writel(temp, sport->port.membase + UCR4);
322 temp = readl(sport->port.membase + UCR1);
323 writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1);
327 * interrupts disabled on entry
329 static void imx_stop_rx(struct uart_port *port)
331 struct imx_port *sport = (struct imx_port *)port;
334 temp = readl(sport->port.membase + UCR2);
335 writel(temp &~ UCR2_RXEN, sport->port.membase + UCR2);
339 * Set the modem control timer to fire immediately.
341 static void imx_enable_ms(struct uart_port *port)
343 struct imx_port *sport = (struct imx_port *)port;
345 mod_timer(&sport->timer, jiffies);
348 static inline void imx_transmit_buffer(struct imx_port *sport)
350 struct circ_buf *xmit = &sport->port.info->xmit;
352 while (!(readl(sport->port.membase + UTS) & UTS_TXFULL)) {
353 /* send xmit->buf[xmit->tail]
354 * out the port here */
355 writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
356 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
357 sport->port.icount.tx++;
358 if (uart_circ_empty(xmit))
362 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
363 uart_write_wakeup(&sport->port);
365 if (uart_circ_empty(xmit))
366 imx_stop_tx(&sport->port);
370 * interrupts disabled on entry
372 static void imx_start_tx(struct uart_port *port)
374 struct imx_port *sport = (struct imx_port *)port;
377 if (USE_IRDA(sport)) {
378 /* half duplex in IrDA mode; have to disable receive mode */
379 temp = readl(sport->port.membase + UCR4);
380 temp &= ~(UCR4_DREN);
381 writel(temp, sport->port.membase + UCR4);
383 temp = readl(sport->port.membase + UCR1);
384 temp &= ~(UCR1_RRDYEN);
385 writel(temp, sport->port.membase + UCR1);
388 temp = readl(sport->port.membase + UCR1);
389 writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
391 if (USE_IRDA(sport)) {
392 temp = readl(sport->port.membase + UCR1);
394 writel(temp, sport->port.membase + UCR1);
396 temp = readl(sport->port.membase + UCR4);
398 writel(temp, sport->port.membase + UCR4);
401 if (readl(sport->port.membase + UTS) & UTS_TXEMPTY)
402 imx_transmit_buffer(sport);
405 static irqreturn_t imx_rtsint(int irq, void *dev_id)
407 struct imx_port *sport = dev_id;
408 unsigned int val = readl(sport->port.membase + USR1) & USR1_RTSS;
411 spin_lock_irqsave(&sport->port.lock, flags);
413 writel(USR1_RTSD, sport->port.membase + USR1);
414 uart_handle_cts_change(&sport->port, !!val);
415 wake_up_interruptible(&sport->port.info->delta_msr_wait);
417 spin_unlock_irqrestore(&sport->port.lock, flags);
421 static irqreturn_t imx_txint(int irq, void *dev_id)
423 struct imx_port *sport = dev_id;
424 struct circ_buf *xmit = &sport->port.info->xmit;
427 spin_lock_irqsave(&sport->port.lock,flags);
428 if (sport->port.x_char)
431 writel(sport->port.x_char, sport->port.membase + URTX0);
435 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
436 imx_stop_tx(&sport->port);
440 imx_transmit_buffer(sport);
442 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
443 uart_write_wakeup(&sport->port);
446 spin_unlock_irqrestore(&sport->port.lock,flags);
450 static irqreturn_t imx_rxint(int irq, void *dev_id)
452 struct imx_port *sport = dev_id;
453 unsigned int rx,flg,ignored = 0;
454 struct tty_struct *tty = sport->port.info->port.tty;
455 unsigned long flags, temp;
457 spin_lock_irqsave(&sport->port.lock,flags);
459 while (readl(sport->port.membase + USR2) & USR2_RDR) {
461 sport->port.icount.rx++;
463 rx = readl(sport->port.membase + URXD0);
465 temp = readl(sport->port.membase + USR2);
466 if (temp & USR2_BRCD) {
467 writel(temp | USR2_BRCD, sport->port.membase + USR2);
468 if (uart_handle_break(&sport->port))
472 if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
475 if (rx & (URXD_PRERR | URXD_OVRRUN | URXD_FRMERR) ) {
477 sport->port.icount.parity++;
478 else if (rx & URXD_FRMERR)
479 sport->port.icount.frame++;
480 if (rx & URXD_OVRRUN)
481 sport->port.icount.overrun++;
483 if (rx & sport->port.ignore_status_mask) {
489 rx &= sport->port.read_status_mask;
493 else if (rx & URXD_FRMERR)
495 if (rx & URXD_OVRRUN)
499 sport->port.sysrq = 0;
503 tty_insert_flip_char(tty, rx, flg);
507 spin_unlock_irqrestore(&sport->port.lock,flags);
508 tty_flip_buffer_push(tty);
512 static irqreturn_t imx_int(int irq, void *dev_id)
514 struct imx_port *sport = dev_id;
517 sts = readl(sport->port.membase + USR1);
520 imx_rxint(irq, dev_id);
522 if (sts & USR1_TRDY &&
523 readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN)
524 imx_txint(irq, dev_id);
527 imx_rtsint(irq, dev_id);
533 * Return TIOCSER_TEMT when transmitter is not busy.
535 static unsigned int imx_tx_empty(struct uart_port *port)
537 struct imx_port *sport = (struct imx_port *)port;
539 return (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
543 * We have a modem side uart, so the meanings of RTS and CTS are inverted.
545 static unsigned int imx_get_mctrl(struct uart_port *port)
547 struct imx_port *sport = (struct imx_port *)port;
548 unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
550 if (readl(sport->port.membase + USR1) & USR1_RTSS)
553 if (readl(sport->port.membase + UCR2) & UCR2_CTS)
559 static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
561 struct imx_port *sport = (struct imx_port *)port;
564 temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS;
566 if (mctrl & TIOCM_RTS)
569 writel(temp, sport->port.membase + UCR2);
573 * Interrupts always disabled.
575 static void imx_break_ctl(struct uart_port *port, int break_state)
577 struct imx_port *sport = (struct imx_port *)port;
578 unsigned long flags, temp;
580 spin_lock_irqsave(&sport->port.lock, flags);
582 temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
584 if ( break_state != 0 )
587 writel(temp, sport->port.membase + UCR1);
589 spin_unlock_irqrestore(&sport->port.lock, flags);
592 #define TXTL 2 /* reset default */
593 #define RXTL 1 /* reset default */
595 static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
598 unsigned int ufcr_rfdiv;
600 /* set receiver / transmitter trigger level.
601 * RFDIV is set such way to satisfy requested uartclk value
603 val = TXTL << 10 | RXTL;
604 ufcr_rfdiv = (clk_get_rate(sport->clk) + sport->port.uartclk / 2)
605 / sport->port.uartclk;
610 val |= UFCR_RFDIV_REG(ufcr_rfdiv);
612 writel(val, sport->port.membase + UFCR);
617 static int imx_startup(struct uart_port *port)
619 struct imx_port *sport = (struct imx_port *)port;
621 unsigned long flags, temp;
623 imx_setup_ufcr(sport, 0);
625 /* disable the DREN bit (Data Ready interrupt enable) before
628 temp = readl(sport->port.membase + UCR4);
633 writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
635 if (USE_IRDA(sport)) {
636 /* reset fifo's and state machines */
638 temp = readl(sport->port.membase + UCR2);
640 writel(temp, sport->port.membase + UCR2);
641 while (!(readl(sport->port.membase + UCR2) & UCR2_SRST) &&
648 * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
649 * chips only have one interrupt.
651 if (sport->txirq > 0) {
652 retval = request_irq(sport->rxirq, imx_rxint, 0,
657 retval = request_irq(sport->txirq, imx_txint, 0,
662 /* do not use RTS IRQ on IrDA */
663 if (!USE_IRDA(sport)) {
664 retval = request_irq(sport->rtsirq, imx_rtsint,
665 (sport->rtsirq < MAX_INTERNAL_IRQ) ? 0 :
666 IRQF_TRIGGER_FALLING |
673 retval = request_irq(sport->port.irq, imx_int, 0,
676 free_irq(sport->port.irq, sport);
682 * Finally, clear and enable interrupts
684 writel(USR1_RTSD, sport->port.membase + USR1);
686 temp = readl(sport->port.membase + UCR1);
687 temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
689 if (USE_IRDA(sport)) {
691 temp &= ~(UCR1_RTSDEN);
694 writel(temp, sport->port.membase + UCR1);
696 temp = readl(sport->port.membase + UCR2);
697 temp |= (UCR2_RXEN | UCR2_TXEN);
698 writel(temp, sport->port.membase + UCR2);
700 if (USE_IRDA(sport)) {
704 (readl(sport->port.membase + URXD0) & URXD_CHARRDY)) {
709 #if defined CONFIG_ARCH_MX2 || defined CONFIG_ARCH_MX3
710 temp = readl(sport->port.membase + UCR3);
711 temp |= UCR3_RXDMUXSEL;
712 writel(temp, sport->port.membase + UCR3);
715 if (USE_IRDA(sport)) {
716 temp = readl(sport->port.membase + UCR4);
717 if (sport->irda_inv_rx)
720 temp &= ~(UCR4_INVR);
721 writel(temp | UCR4_DREN, sport->port.membase + UCR4);
723 temp = readl(sport->port.membase + UCR3);
724 if (sport->irda_inv_tx)
727 temp &= ~(UCR3_INVT);
728 writel(temp, sport->port.membase + UCR3);
732 * Enable modem status interrupts
734 spin_lock_irqsave(&sport->port.lock,flags);
735 imx_enable_ms(&sport->port);
736 spin_unlock_irqrestore(&sport->port.lock,flags);
738 if (USE_IRDA(sport)) {
739 struct imxuart_platform_data *pdata;
740 pdata = sport->port.dev->platform_data;
741 sport->irda_inv_rx = pdata->irda_inv_rx;
742 sport->irda_inv_tx = pdata->irda_inv_tx;
743 sport->trcv_delay = pdata->transceiver_delay;
744 if (pdata->irda_enable)
745 pdata->irda_enable(1);
752 free_irq(sport->txirq, sport);
755 free_irq(sport->rxirq, sport);
760 static void imx_shutdown(struct uart_port *port)
762 struct imx_port *sport = (struct imx_port *)port;
765 temp = readl(sport->port.membase + UCR2);
766 temp &= ~(UCR2_TXEN);
767 writel(temp, sport->port.membase + UCR2);
769 if (USE_IRDA(sport)) {
770 struct imxuart_platform_data *pdata;
771 pdata = sport->port.dev->platform_data;
772 if (pdata->irda_enable)
773 pdata->irda_enable(0);
779 del_timer_sync(&sport->timer);
782 * Free the interrupts
784 if (sport->txirq > 0) {
785 if (!USE_IRDA(sport))
786 free_irq(sport->rtsirq, sport);
787 free_irq(sport->txirq, sport);
788 free_irq(sport->rxirq, sport);
790 free_irq(sport->port.irq, sport);
793 * Disable all interrupts, port and break condition.
796 temp = readl(sport->port.membase + UCR1);
797 temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
799 temp &= ~(UCR1_IREN);
801 writel(temp, sport->port.membase + UCR1);
805 imx_set_termios(struct uart_port *port, struct ktermios *termios,
806 struct ktermios *old)
808 struct imx_port *sport = (struct imx_port *)port;
810 unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
811 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
812 unsigned int div, ufcr;
813 unsigned long num, denom;
817 * If we don't support modem control lines, don't allow
821 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
822 termios->c_cflag |= CLOCAL;
826 * We only support CS7 and CS8.
828 while ((termios->c_cflag & CSIZE) != CS7 &&
829 (termios->c_cflag & CSIZE) != CS8) {
830 termios->c_cflag &= ~CSIZE;
831 termios->c_cflag |= old_csize;
835 if ((termios->c_cflag & CSIZE) == CS8)
836 ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
838 ucr2 = UCR2_SRST | UCR2_IRTS;
840 if (termios->c_cflag & CRTSCTS) {
841 if( sport->have_rtscts ) {
845 termios->c_cflag &= ~CRTSCTS;
849 if (termios->c_cflag & CSTOPB)
851 if (termios->c_cflag & PARENB) {
853 if (termios->c_cflag & PARODD)
858 * Ask the core to calculate the divisor for us.
860 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
861 quot = uart_get_divisor(port, baud);
863 spin_lock_irqsave(&sport->port.lock, flags);
865 sport->port.read_status_mask = 0;
866 if (termios->c_iflag & INPCK)
867 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
868 if (termios->c_iflag & (BRKINT | PARMRK))
869 sport->port.read_status_mask |= URXD_BRK;
872 * Characters to ignore
874 sport->port.ignore_status_mask = 0;
875 if (termios->c_iflag & IGNPAR)
876 sport->port.ignore_status_mask |= URXD_PRERR;
877 if (termios->c_iflag & IGNBRK) {
878 sport->port.ignore_status_mask |= URXD_BRK;
880 * If we're ignoring parity and break indicators,
881 * ignore overruns too (for real raw support).
883 if (termios->c_iflag & IGNPAR)
884 sport->port.ignore_status_mask |= URXD_OVRRUN;
887 del_timer_sync(&sport->timer);
890 * Update the per-port timeout.
892 uart_update_timeout(port, termios->c_cflag, baud);
895 * disable interrupts and drain transmitter
897 old_ucr1 = readl(sport->port.membase + UCR1);
898 writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
899 sport->port.membase + UCR1);
901 while ( !(readl(sport->port.membase + USR2) & USR2_TXDC))
904 /* then, disable everything */
905 old_txrxen = readl(sport->port.membase + UCR2);
906 writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN),
907 sport->port.membase + UCR2);
908 old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
910 if (USE_IRDA(sport)) {
912 * use maximum available submodule frequency to
913 * avoid missing short pulses due to low sampling rate
917 div = sport->port.uartclk / (baud * 16);
924 rational_best_approximation(16 * div * baud, sport->port.uartclk,
925 1 << 16, 1 << 16, &num, &denom);
927 tdiv64 = sport->port.uartclk;
929 do_div(tdiv64, denom * 16 * div);
930 tty_encode_baud_rate(sport->port.info->port.tty,
931 (speed_t)tdiv64, (speed_t)tdiv64);
936 ufcr = readl(sport->port.membase + UFCR);
937 ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
938 writel(ufcr, sport->port.membase + UFCR);
940 writel(num, sport->port.membase + UBIR);
941 writel(denom, sport->port.membase + UBMR);
944 writel(sport->port.uartclk / div / 1000, sport->port.membase + ONEMS);
947 writel(old_ucr1, sport->port.membase + UCR1);
949 /* set the parity, stop bits and data size */
950 writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
952 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
953 imx_enable_ms(&sport->port);
955 spin_unlock_irqrestore(&sport->port.lock, flags);
958 static const char *imx_type(struct uart_port *port)
960 struct imx_port *sport = (struct imx_port *)port;
962 return sport->port.type == PORT_IMX ? "IMX" : NULL;
966 * Release the memory region(s) being used by 'port'.
968 static void imx_release_port(struct uart_port *port)
970 struct platform_device *pdev = to_platform_device(port->dev);
971 struct resource *mmres;
973 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
974 release_mem_region(mmres->start, mmres->end - mmres->start + 1);
978 * Request the memory region(s) being used by 'port'.
980 static int imx_request_port(struct uart_port *port)
982 struct platform_device *pdev = to_platform_device(port->dev);
983 struct resource *mmres;
986 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
990 ret = request_mem_region(mmres->start, mmres->end - mmres->start + 1,
993 return ret ? 0 : -EBUSY;
997 * Configure/autoconfigure the port.
999 static void imx_config_port(struct uart_port *port, int flags)
1001 struct imx_port *sport = (struct imx_port *)port;
1003 if (flags & UART_CONFIG_TYPE &&
1004 imx_request_port(&sport->port) == 0)
1005 sport->port.type = PORT_IMX;
1009 * Verify the new serial_struct (for TIOCSSERIAL).
1010 * The only change we allow are to the flags and type, and
1011 * even then only between PORT_IMX and PORT_UNKNOWN
1014 imx_verify_port(struct uart_port *port, struct serial_struct *ser)
1016 struct imx_port *sport = (struct imx_port *)port;
1019 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
1021 if (sport->port.irq != ser->irq)
1023 if (ser->io_type != UPIO_MEM)
1025 if (sport->port.uartclk / 16 != ser->baud_base)
1027 if ((void *)sport->port.mapbase != ser->iomem_base)
1029 if (sport->port.iobase != ser->port)
1036 static struct uart_ops imx_pops = {
1037 .tx_empty = imx_tx_empty,
1038 .set_mctrl = imx_set_mctrl,
1039 .get_mctrl = imx_get_mctrl,
1040 .stop_tx = imx_stop_tx,
1041 .start_tx = imx_start_tx,
1042 .stop_rx = imx_stop_rx,
1043 .enable_ms = imx_enable_ms,
1044 .break_ctl = imx_break_ctl,
1045 .startup = imx_startup,
1046 .shutdown = imx_shutdown,
1047 .set_termios = imx_set_termios,
1049 .release_port = imx_release_port,
1050 .request_port = imx_request_port,
1051 .config_port = imx_config_port,
1052 .verify_port = imx_verify_port,
1055 static struct imx_port *imx_ports[UART_NR];
1057 #ifdef CONFIG_SERIAL_IMX_CONSOLE
1058 static void imx_console_putchar(struct uart_port *port, int ch)
1060 struct imx_port *sport = (struct imx_port *)port;
1062 while (readl(sport->port.membase + UTS) & UTS_TXFULL)
1065 writel(ch, sport->port.membase + URTX0);
1069 * Interrupts are disabled on entering
1072 imx_console_write(struct console *co, const char *s, unsigned int count)
1074 struct imx_port *sport = imx_ports[co->index];
1075 unsigned int old_ucr1, old_ucr2;
1078 * First, save UCR1/2 and then disable interrupts
1080 old_ucr1 = readl(sport->port.membase + UCR1);
1081 old_ucr2 = readl(sport->port.membase + UCR2);
1083 writel((old_ucr1 | UCR1_UARTCLKEN | UCR1_UARTEN) &
1084 ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
1085 sport->port.membase + UCR1);
1087 writel(old_ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
1089 uart_console_write(&sport->port, s, count, imx_console_putchar);
1092 * Finally, wait for transmitter to become empty
1093 * and restore UCR1/2
1095 while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
1097 writel(old_ucr1, sport->port.membase + UCR1);
1098 writel(old_ucr2, sport->port.membase + UCR2);
1102 * If the port was already initialised (eg, by a boot loader),
1103 * try to determine the current setup.
1106 imx_console_get_options(struct imx_port *sport, int *baud,
1107 int *parity, int *bits)
1110 if ( readl(sport->port.membase + UCR1) | UCR1_UARTEN ) {
1111 /* ok, the port was enabled */
1112 unsigned int ucr2, ubir,ubmr, uartclk;
1113 unsigned int baud_raw;
1114 unsigned int ucfr_rfdiv;
1116 ucr2 = readl(sport->port.membase + UCR2);
1119 if (ucr2 & UCR2_PREN) {
1120 if (ucr2 & UCR2_PROE)
1131 ubir = readl(sport->port.membase + UBIR) & 0xffff;
1132 ubmr = readl(sport->port.membase + UBMR) & 0xffff;
1134 ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
1135 if (ucfr_rfdiv == 6)
1138 ucfr_rfdiv = 6 - ucfr_rfdiv;
1140 uartclk = clk_get_rate(sport->clk);
1141 uartclk /= ucfr_rfdiv;
1144 * The next code provides exact computation of
1145 * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
1146 * without need of float support or long long division,
1147 * which would be required to prevent 32bit arithmetic overflow
1149 unsigned int mul = ubir + 1;
1150 unsigned int div = 16 * (ubmr + 1);
1151 unsigned int rem = uartclk % div;
1153 baud_raw = (uartclk / div) * mul;
1154 baud_raw += (rem * mul + div / 2) / div;
1155 *baud = (baud_raw + 50) / 100 * 100;
1158 if(*baud != baud_raw)
1159 printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
1165 imx_console_setup(struct console *co, char *options)
1167 struct imx_port *sport;
1174 * Check whether an invalid uart number has been specified, and
1175 * if so, search for the first available port that does have
1178 if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
1180 sport = imx_ports[co->index];
1185 uart_parse_options(options, &baud, &parity, &bits, &flow);
1187 imx_console_get_options(sport, &baud, &parity, &bits);
1189 imx_setup_ufcr(sport, 0);
1191 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
1194 static struct uart_driver imx_reg;
1195 static struct console imx_console = {
1197 .write = imx_console_write,
1198 .device = uart_console_device,
1199 .setup = imx_console_setup,
1200 .flags = CON_PRINTBUFFER,
1205 #define IMX_CONSOLE &imx_console
1207 #define IMX_CONSOLE NULL
1210 static struct uart_driver imx_reg = {
1211 .owner = THIS_MODULE,
1212 .driver_name = DRIVER_NAME,
1213 .dev_name = DEV_NAME,
1214 .major = SERIAL_IMX_MAJOR,
1215 .minor = MINOR_START,
1216 .nr = ARRAY_SIZE(imx_ports),
1217 .cons = IMX_CONSOLE,
1220 static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
1222 struct imx_port *sport = platform_get_drvdata(dev);
1225 uart_suspend_port(&imx_reg, &sport->port);
1230 static int serial_imx_resume(struct platform_device *dev)
1232 struct imx_port *sport = platform_get_drvdata(dev);
1235 uart_resume_port(&imx_reg, &sport->port);
1240 static int serial_imx_probe(struct platform_device *pdev)
1242 struct imx_port *sport;
1243 struct imxuart_platform_data *pdata;
1246 struct resource *res;
1248 sport = kzalloc(sizeof(*sport), GFP_KERNEL);
1252 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1258 base = ioremap(res->start, PAGE_SIZE);
1264 sport->port.dev = &pdev->dev;
1265 sport->port.mapbase = res->start;
1266 sport->port.membase = base;
1267 sport->port.type = PORT_IMX,
1268 sport->port.iotype = UPIO_MEM;
1269 sport->port.irq = platform_get_irq(pdev, 0);
1270 sport->rxirq = platform_get_irq(pdev, 0);
1271 sport->txirq = platform_get_irq(pdev, 1);
1272 sport->rtsirq = platform_get_irq(pdev, 2);
1273 sport->port.fifosize = 32;
1274 sport->port.ops = &imx_pops;
1275 sport->port.flags = UPF_BOOT_AUTOCONF;
1276 sport->port.line = pdev->id;
1277 init_timer(&sport->timer);
1278 sport->timer.function = imx_timeout;
1279 sport->timer.data = (unsigned long)sport;
1281 sport->clk = clk_get(&pdev->dev, "uart");
1282 if (IS_ERR(sport->clk)) {
1283 ret = PTR_ERR(sport->clk);
1286 clk_enable(sport->clk);
1288 sport->port.uartclk = clk_get_rate(sport->clk);
1290 imx_ports[pdev->id] = sport;
1292 pdata = pdev->dev.platform_data;
1293 if (pdata && (pdata->flags & IMXUART_HAVE_RTSCTS))
1294 sport->have_rtscts = 1;
1297 if (pdata && (pdata->flags & IMXUART_IRDA))
1298 sport->use_irda = 1;
1302 ret = pdata->init(pdev);
1307 ret = uart_add_one_port(&imx_reg, &sport->port);
1310 platform_set_drvdata(pdev, &sport->port);
1317 clk_put(sport->clk);
1318 clk_disable(sport->clk);
1320 iounmap(sport->port.membase);
1327 static int serial_imx_remove(struct platform_device *pdev)
1329 struct imxuart_platform_data *pdata;
1330 struct imx_port *sport = platform_get_drvdata(pdev);
1332 pdata = pdev->dev.platform_data;
1334 platform_set_drvdata(pdev, NULL);
1337 uart_remove_one_port(&imx_reg, &sport->port);
1338 clk_put(sport->clk);
1341 clk_disable(sport->clk);
1346 iounmap(sport->port.membase);
1352 static struct platform_driver serial_imx_driver = {
1353 .probe = serial_imx_probe,
1354 .remove = serial_imx_remove,
1356 .suspend = serial_imx_suspend,
1357 .resume = serial_imx_resume,
1360 .owner = THIS_MODULE,
1364 static int __init imx_serial_init(void)
1368 printk(KERN_INFO "Serial: IMX driver\n");
1370 ret = uart_register_driver(&imx_reg);
1374 ret = platform_driver_register(&serial_imx_driver);
1376 uart_unregister_driver(&imx_reg);
1381 static void __exit imx_serial_exit(void)
1383 platform_driver_unregister(&serial_imx_driver);
1384 uart_unregister_driver(&imx_reg);
1387 module_init(imx_serial_init);
1388 module_exit(imx_serial_exit);
1390 MODULE_AUTHOR("Sascha Hauer");
1391 MODULE_DESCRIPTION("IMX generic serial port driver");
1392 MODULE_LICENSE("GPL");
1393 MODULE_ALIAS("platform:imx-uart");