blob: e6c2ba26dcbb191f0007cb4b0b6e9821b060e6fe [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 * [29-Mar-2005] Mike Lee
26 * Added hardware handshake
27 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#if defined(CONFIG_SERIAL_IMX_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
30#define SUPPORT_SYSRQ
31#endif
32
33#include <linux/module.h>
34#include <linux/ioport.h>
35#include <linux/init.h>
36#include <linux/console.h>
37#include <linux/sysrq.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010038#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/tty.h>
40#include <linux/tty_flip.h>
41#include <linux/serial_core.h>
42#include <linux/serial.h>
Sascha Hauer38a41fd2008-07-05 10:02:46 +020043#include <linux/clk.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45#include <asm/io.h>
46#include <asm/irq.h>
Russell Kinga09e64f2008-08-05 16:14:15 +010047#include <mach/hardware.h>
48#include <mach/imx-uart.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Sascha Hauerff4bfb22007-04-26 08:26:13 +010050/* Register definitions */
51#define URXD0 0x0 /* Receiver Register */
52#define URTX0 0x40 /* Transmitter Register */
53#define UCR1 0x80 /* Control Register 1 */
54#define UCR2 0x84 /* Control Register 2 */
55#define UCR3 0x88 /* Control Register 3 */
56#define UCR4 0x8c /* Control Register 4 */
57#define UFCR 0x90 /* FIFO Control Register */
58#define USR1 0x94 /* Status Register 1 */
59#define USR2 0x98 /* Status Register 2 */
60#define UESC 0x9c /* Escape Character Register */
61#define UTIM 0xa0 /* Escape Timer Register */
62#define UBIR 0xa4 /* BRM Incremental Register */
63#define UBMR 0xa8 /* BRM Modulator Register */
64#define UBRC 0xac /* Baud Rate Count Register */
Sascha Hauer604cbad2008-07-05 10:02:58 +020065#if defined CONFIG_ARCH_MX3 || defined CONFIG_ARCH_MX2
Sascha Hauere3d13ff2008-07-05 10:02:48 +020066#define ONEMS 0xb0 /* One Millisecond register */
67#define UTS 0xb4 /* UART Test Register */
68#endif
Paulius Zaleckasbd006a92008-11-14 11:01:39 +010069#if defined(CONFIG_ARCH_IMX) || defined(CONFIG_ARCH_MX1)
Sascha Hauerff4bfb22007-04-26 08:26:13 +010070#define BIPR1 0xb0 /* Incremental Preset Register 1 */
71#define BIPR2 0xb4 /* Incremental Preset Register 2 */
72#define BIPR3 0xb8 /* Incremental Preset Register 3 */
73#define BIPR4 0xbc /* Incremental Preset Register 4 */
74#define BMPR1 0xc0 /* BRM Modulator Register 1 */
75#define BMPR2 0xc4 /* BRM Modulator Register 2 */
76#define BMPR3 0xc8 /* BRM Modulator Register 3 */
77#define BMPR4 0xcc /* BRM Modulator Register 4 */
78#define UTS 0xd0 /* UART Test Register */
Sascha Hauere3d13ff2008-07-05 10:02:48 +020079#endif
Sascha Hauerff4bfb22007-04-26 08:26:13 +010080
81/* UART Control Register Bit Fields.*/
82#define URXD_CHARRDY (1<<15)
83#define URXD_ERR (1<<14)
84#define URXD_OVRRUN (1<<13)
85#define URXD_FRMERR (1<<12)
86#define URXD_BRK (1<<11)
87#define URXD_PRERR (1<<10)
88#define UCR1_ADEN (1<<15) /* Auto dectect interrupt */
89#define UCR1_ADBR (1<<14) /* Auto detect baud rate */
90#define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
91#define UCR1_IDEN (1<<12) /* Idle condition interrupt */
92#define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
93#define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
94#define UCR1_IREN (1<<7) /* Infrared interface enable */
95#define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
96#define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
97#define UCR1_SNDBRK (1<<4) /* Send break */
98#define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
Paulius Zaleckasbd006a92008-11-14 11:01:39 +010099#if defined(CONFIG_ARCH_IMX) || defined(CONFIG_ARCH_MX1)
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100100#define UCR1_UARTCLKEN (1<<2) /* UART clock enabled */
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200101#endif
Sascha Hauer604cbad2008-07-05 10:02:58 +0200102#if defined CONFIG_ARCH_MX3 || defined CONFIG_ARCH_MX2
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200103#define UCR1_UARTCLKEN (0) /* not present on mx2/mx3 */
104#endif
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100105#define UCR1_DOZE (1<<1) /* Doze */
106#define UCR1_UARTEN (1<<0) /* UART enabled */
107#define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
108#define UCR2_IRTS (1<<14) /* Ignore RTS pin */
109#define UCR2_CTSC (1<<13) /* CTS pin control */
110#define UCR2_CTS (1<<12) /* Clear to send */
111#define UCR2_ESCEN (1<<11) /* Escape enable */
112#define UCR2_PREN (1<<8) /* Parity enable */
113#define UCR2_PROE (1<<7) /* Parity odd/even */
114#define UCR2_STPB (1<<6) /* Stop */
115#define UCR2_WS (1<<5) /* Word size */
116#define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
117#define UCR2_TXEN (1<<2) /* Transmitter enabled */
118#define UCR2_RXEN (1<<1) /* Receiver enabled */
119#define UCR2_SRST (1<<0) /* SW reset */
120#define UCR3_DTREN (1<<13) /* DTR interrupt enable */
121#define UCR3_PARERREN (1<<12) /* Parity enable */
122#define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
123#define UCR3_DSR (1<<10) /* Data set ready */
124#define UCR3_DCD (1<<9) /* Data carrier detect */
125#define UCR3_RI (1<<8) /* Ring indicator */
126#define UCR3_TIMEOUTEN (1<<7) /* Timeout interrupt enable */
127#define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
128#define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
129#define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
Marc Kleine-Budde44118052008-07-28 12:10:34 +0200130#ifdef CONFIG_ARCH_IMX
131#define UCR3_REF25 (1<<3) /* Ref freq 25 MHz, only on mx1 */
132#define UCR3_REF30 (1<<2) /* Ref Freq 30 MHz, only on mx1 */
133#endif
134#if defined CONFIG_ARCH_MX2 || defined CONFIG_ARCH_MX3
135#define UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select, on mx2/mx3 */
136#endif
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100137#define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
138#define UCR3_BPEN (1<<0) /* Preset registers enable */
139#define UCR4_CTSTL_32 (32<<10) /* CTS trigger level (32 chars) */
140#define UCR4_INVR (1<<9) /* Inverted infrared reception */
141#define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
142#define UCR4_WKEN (1<<7) /* Wake interrupt enable */
143#define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
144#define UCR4_IRSC (1<<5) /* IR special case */
145#define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
146#define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
147#define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
148#define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
149#define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
150#define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
151#define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
152#define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
153#define USR1_RTSS (1<<14) /* RTS pin status */
154#define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
155#define USR1_RTSD (1<<12) /* RTS delta */
156#define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
157#define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
158#define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
159#define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */
160#define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
161#define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
162#define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
163#define USR2_ADET (1<<15) /* Auto baud rate detect complete */
164#define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
165#define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
166#define USR2_IDLE (1<<12) /* Idle condition */
167#define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
168#define USR2_WAKE (1<<7) /* Wake */
169#define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
170#define USR2_TXDC (1<<3) /* Transmitter complete */
171#define USR2_BRCD (1<<2) /* Break condition */
172#define USR2_ORE (1<<1) /* Overrun error */
173#define USR2_RDR (1<<0) /* Recv data ready */
174#define UTS_FRCPERR (1<<13) /* Force parity error */
175#define UTS_LOOP (1<<12) /* Loop tx and rx */
176#define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
177#define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
178#define UTS_TXFULL (1<<4) /* TxFIFO full */
179#define UTS_RXFULL (1<<3) /* RxFIFO full */
180#define UTS_SOFTRST (1<<0) /* Software reset */
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/* We've been assigned a range on the "Low-density serial ports" major */
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200183#ifdef CONFIG_ARCH_IMX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184#define SERIAL_IMX_MAJOR 204
185#define MINOR_START 41
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200186#define DEV_NAME "ttySMX"
187#define MAX_INTERNAL_IRQ IMX_IRQS
188#endif
189
Paulius Zaleckasbd006a92008-11-14 11:01:39 +0100190#ifdef CONFIG_ARCH_MXC
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200191#define SERIAL_IMX_MAJOR 207
192#define MINOR_START 16
193#define DEV_NAME "ttymxc"
Sascha Hauer9d631b82008-12-18 11:08:55 +0100194#define MAX_INTERNAL_IRQ MXC_INTERNAL_IRQS
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200195#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * This determines how often we check the modem status signals
199 * for any change. They generally aren't connected to an IRQ
200 * so we have to poll them. We also check immediately before
201 * filling the TX fifo incase CTS has been dropped.
202 */
203#define MCTRL_TIMEOUT (250*HZ/1000)
204
205#define DRIVER_NAME "IMX-uart"
206
Sascha Hauerdbff4e92008-07-05 10:02:45 +0200207#define UART_NR 8
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209struct imx_port {
210 struct uart_port port;
211 struct timer_list timer;
212 unsigned int old_status;
Sascha Hauer5b802342006-05-04 14:07:42 +0100213 int txirq,rxirq,rtsirq;
Daniel Glöckner26bbb3f2009-06-11 14:36:29 +0100214 unsigned int have_rtscts:1;
Sascha Hauer38a41fd2008-07-05 10:02:46 +0200215 struct clk *clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
218/*
219 * Handle any change of modem status signal since we were last called.
220 */
221static void imx_mctrl_check(struct imx_port *sport)
222{
223 unsigned int status, changed;
224
225 status = sport->port.ops->get_mctrl(&sport->port);
226 changed = status ^ sport->old_status;
227
228 if (changed == 0)
229 return;
230
231 sport->old_status = status;
232
233 if (changed & TIOCM_RI)
234 sport->port.icount.rng++;
235 if (changed & TIOCM_DSR)
236 sport->port.icount.dsr++;
237 if (changed & TIOCM_CAR)
238 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
239 if (changed & TIOCM_CTS)
240 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
241
242 wake_up_interruptible(&sport->port.info->delta_msr_wait);
243}
244
245/*
246 * This is our per-port timeout handler, for checking the
247 * modem status signals.
248 */
249static void imx_timeout(unsigned long data)
250{
251 struct imx_port *sport = (struct imx_port *)data;
252 unsigned long flags;
253
254 if (sport->port.info) {
255 spin_lock_irqsave(&sport->port.lock, flags);
256 imx_mctrl_check(sport);
257 spin_unlock_irqrestore(&sport->port.lock, flags);
258
259 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
260 }
261}
262
263/*
264 * interrupts disabled on entry
265 */
Russell Kingb129a8c2005-08-31 10:12:14 +0100266static void imx_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100269 unsigned long temp;
270
271 temp = readl(sport->port.membase + UCR1);
272 writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275/*
276 * interrupts disabled on entry
277 */
278static void imx_stop_rx(struct uart_port *port)
279{
280 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100281 unsigned long temp;
282
283 temp = readl(sport->port.membase + UCR2);
284 writel(temp &~ UCR2_RXEN, sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287/*
288 * Set the modem control timer to fire immediately.
289 */
290static void imx_enable_ms(struct uart_port *port)
291{
292 struct imx_port *sport = (struct imx_port *)port;
293
294 mod_timer(&sport->timer, jiffies);
295}
296
297static inline void imx_transmit_buffer(struct imx_port *sport)
298{
299 struct circ_buf *xmit = &sport->port.info->xmit;
300
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100301 while (!(readl(sport->port.membase + UTS) & UTS_TXFULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 /* send xmit->buf[xmit->tail]
303 * out the port here */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100304 writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
Oskar Schirmerd3810cd2009-06-11 14:35:01 +0100305 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 sport->port.icount.tx++;
307 if (uart_circ_empty(xmit))
308 break;
Sascha Hauer8c0b2542007-02-05 16:10:16 -0800309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Fabian Godehardt977757312009-06-11 14:37:19 +0100311 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
312 uart_write_wakeup(&sport->port);
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100315 imx_stop_tx(&sport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
318/*
319 * interrupts disabled on entry
320 */
Russell Kingb129a8c2005-08-31 10:12:14 +0100321static void imx_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100324 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100326 temp = readl(sport->port.membase + UCR1);
327 writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100329 if (readl(sport->port.membase + UTS) & UTS_TXEMPTY)
330 imx_transmit_buffer(sport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331}
332
David Howells7d12e782006-10-05 14:55:46 +0100333static irqreturn_t imx_rtsint(int irq, void *dev_id)
Sascha Hauerceca6292005-10-12 19:58:08 +0100334{
Jeff Garzik15aafa22008-02-06 01:36:20 -0800335 struct imx_port *sport = dev_id;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100336 unsigned int val = readl(sport->port.membase + USR1) & USR1_RTSS;
Sascha Hauerceca6292005-10-12 19:58:08 +0100337 unsigned long flags;
338
339 spin_lock_irqsave(&sport->port.lock, flags);
340
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100341 writel(USR1_RTSD, sport->port.membase + USR1);
Sascha Hauerceca6292005-10-12 19:58:08 +0100342 uart_handle_cts_change(&sport->port, !!val);
343 wake_up_interruptible(&sport->port.info->delta_msr_wait);
344
345 spin_unlock_irqrestore(&sport->port.lock, flags);
346 return IRQ_HANDLED;
347}
348
David Howells7d12e782006-10-05 14:55:46 +0100349static irqreturn_t imx_txint(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Jeff Garzik15aafa22008-02-06 01:36:20 -0800351 struct imx_port *sport = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct circ_buf *xmit = &sport->port.info->xmit;
353 unsigned long flags;
354
355 spin_lock_irqsave(&sport->port.lock,flags);
356 if (sport->port.x_char)
357 {
358 /* Send next char */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100359 writel(sport->port.x_char, sport->port.membase + URTX0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 goto out;
361 }
362
363 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100364 imx_stop_tx(&sport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 goto out;
366 }
367
368 imx_transmit_buffer(sport);
369
370 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
371 uart_write_wakeup(&sport->port);
372
373out:
374 spin_unlock_irqrestore(&sport->port.lock,flags);
375 return IRQ_HANDLED;
376}
377
David Howells7d12e782006-10-05 14:55:46 +0100378static irqreturn_t imx_rxint(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 struct imx_port *sport = dev_id;
381 unsigned int rx,flg,ignored = 0;
Takashi Iwaia88487c2008-07-16 21:54:42 +0100382 struct tty_struct *tty = sport->port.info->port.tty;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100383 unsigned long flags, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 spin_lock_irqsave(&sport->port.lock,flags);
386
Sascha Hauer0d3c3932008-04-17 08:43:14 +0100387 while (readl(sport->port.membase + USR2) & USR2_RDR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 flg = TTY_NORMAL;
389 sport->port.icount.rx++;
390
Sascha Hauer0d3c3932008-04-17 08:43:14 +0100391 rx = readl(sport->port.membase + URXD0);
392
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100393 temp = readl(sport->port.membase + USR2);
Sascha Hauer864eeed2008-04-17 08:39:22 +0100394 if (temp & USR2_BRCD) {
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100395 writel(temp | USR2_BRCD, sport->port.membase + USR2);
Sascha Hauer864eeed2008-04-17 08:39:22 +0100396 if (uart_handle_break(&sport->port))
397 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
Oskar Schirmerd3810cd2009-06-11 14:35:01 +0100400 if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
Sascha Hauer864eeed2008-04-17 08:39:22 +0100401 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Sascha Hauer864eeed2008-04-17 08:39:22 +0100403 if (rx & (URXD_PRERR | URXD_OVRRUN | URXD_FRMERR) ) {
404 if (rx & URXD_PRERR)
405 sport->port.icount.parity++;
406 else if (rx & URXD_FRMERR)
407 sport->port.icount.frame++;
408 if (rx & URXD_OVRRUN)
409 sport->port.icount.overrun++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Sascha Hauer864eeed2008-04-17 08:39:22 +0100411 if (rx & sport->port.ignore_status_mask) {
412 if (++ignored > 100)
413 goto out;
414 continue;
415 }
416
417 rx &= sport->port.read_status_mask;
418
419 if (rx & URXD_PRERR)
420 flg = TTY_PARITY;
421 else if (rx & URXD_FRMERR)
422 flg = TTY_FRAME;
423 if (rx & URXD_OVRRUN)
424 flg = TTY_OVERRUN;
425
426#ifdef SUPPORT_SYSRQ
427 sport->port.sysrq = 0;
428#endif
429 }
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 tty_insert_flip_char(tty, rx, flg);
Sascha Hauer864eeed2008-04-17 08:39:22 +0100432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434out:
435 spin_unlock_irqrestore(&sport->port.lock,flags);
436 tty_flip_buffer_push(tty);
437 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438}
439
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200440static irqreturn_t imx_int(int irq, void *dev_id)
441{
442 struct imx_port *sport = dev_id;
443 unsigned int sts;
444
445 sts = readl(sport->port.membase + USR1);
446
447 if (sts & USR1_RRDY)
448 imx_rxint(irq, dev_id);
449
450 if (sts & USR1_TRDY &&
451 readl(sport->port.membase + UCR1) & UCR1_TXMPTYEN)
452 imx_txint(irq, dev_id);
453
Marc Kleine-Budde9fbe6042008-07-28 21:26:01 +0200454 if (sts & USR1_RTSD)
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200455 imx_rtsint(irq, dev_id);
456
457 return IRQ_HANDLED;
458}
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460/*
461 * Return TIOCSER_TEMT when transmitter is not busy.
462 */
463static unsigned int imx_tx_empty(struct uart_port *port)
464{
465 struct imx_port *sport = (struct imx_port *)port;
466
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100467 return (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100470/*
471 * We have a modem side uart, so the meanings of RTS and CTS are inverted.
472 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473static unsigned int imx_get_mctrl(struct uart_port *port)
474{
Oskar Schirmerd3810cd2009-06-11 14:35:01 +0100475 struct imx_port *sport = (struct imx_port *)port;
476 unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100477
Oskar Schirmerd3810cd2009-06-11 14:35:01 +0100478 if (readl(sport->port.membase + USR1) & USR1_RTSS)
479 tmp |= TIOCM_CTS;
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100480
Oskar Schirmerd3810cd2009-06-11 14:35:01 +0100481 if (readl(sport->port.membase + UCR2) & UCR2_CTS)
482 tmp |= TIOCM_RTS;
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100483
Oskar Schirmerd3810cd2009-06-11 14:35:01 +0100484 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
487static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
488{
Oskar Schirmerd3810cd2009-06-11 14:35:01 +0100489 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100490 unsigned long temp;
491
492 temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS;
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100493
Oskar Schirmerd3810cd2009-06-11 14:35:01 +0100494 if (mctrl & TIOCM_RTS)
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100495 temp |= UCR2_CTS;
496
497 writel(temp, sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
500/*
501 * Interrupts always disabled.
502 */
503static void imx_break_ctl(struct uart_port *port, int break_state)
504{
505 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100506 unsigned long flags, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 spin_lock_irqsave(&sport->port.lock, flags);
509
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100510 temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if ( break_state != 0 )
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100513 temp |= UCR1_SNDBRK;
514
515 writel(temp, sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 spin_unlock_irqrestore(&sport->port.lock, flags);
518}
519
520#define TXTL 2 /* reset default */
521#define RXTL 1 /* reset default */
522
Sascha Hauer587897f2005-04-29 22:46:40 +0100523static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
524{
525 unsigned int val;
526 unsigned int ufcr_rfdiv;
527
528 /* set receiver / transmitter trigger level.
529 * RFDIV is set such way to satisfy requested uartclk value
530 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100531 val = TXTL << 10 | RXTL;
Sascha Hauer38a41fd2008-07-05 10:02:46 +0200532 ufcr_rfdiv = (clk_get_rate(sport->clk) + sport->port.uartclk / 2)
533 / sport->port.uartclk;
Sascha Hauer587897f2005-04-29 22:46:40 +0100534
535 if(!ufcr_rfdiv)
536 ufcr_rfdiv = 1;
537
538 if(ufcr_rfdiv >= 7)
539 ufcr_rfdiv = 6;
540 else
541 ufcr_rfdiv = 6 - ufcr_rfdiv;
542
543 val |= UFCR_RFDIV & (ufcr_rfdiv << 7);
544
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100545 writel(val, sport->port.membase + UFCR);
Sascha Hauer587897f2005-04-29 22:46:40 +0100546
547 return 0;
548}
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550static int imx_startup(struct uart_port *port)
551{
552 struct imx_port *sport = (struct imx_port *)port;
553 int retval;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100554 unsigned long flags, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Sascha Hauer587897f2005-04-29 22:46:40 +0100556 imx_setup_ufcr(sport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /* disable the DREN bit (Data Ready interrupt enable) before
559 * requesting IRQs
560 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100561 temp = readl(sport->port.membase + UCR4);
562 writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /*
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200565 * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
566 * chips only have one interrupt.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 */
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200568 if (sport->txirq > 0) {
569 retval = request_irq(sport->rxirq, imx_rxint, 0,
570 DRIVER_NAME, sport);
571 if (retval)
572 goto error_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200574 retval = request_irq(sport->txirq, imx_txint, 0,
575 DRIVER_NAME, sport);
576 if (retval)
577 goto error_out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200579 retval = request_irq(sport->rtsirq, imx_rtsint,
580 (sport->rtsirq < MAX_INTERNAL_IRQ) ? 0 :
Pavel Pisad7ea10d2007-02-05 16:10:20 -0800581 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200582 DRIVER_NAME, sport);
583 if (retval)
584 goto error_out3;
585 } else {
586 retval = request_irq(sport->port.irq, imx_int, 0,
587 DRIVER_NAME, sport);
588 if (retval) {
589 free_irq(sport->port.irq, sport);
590 goto error_out1;
591 }
592 }
Sascha Hauerceca6292005-10-12 19:58:08 +0100593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /*
595 * Finally, clear and enable interrupts
596 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100597 writel(USR1_RTSD, sport->port.membase + USR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100599 temp = readl(sport->port.membase + UCR1);
Sascha Hauer789d5252008-04-17 08:44:47 +0100600 temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100601 writel(temp, sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100603 temp = readl(sport->port.membase + UCR2);
604 temp |= (UCR2_RXEN | UCR2_TXEN);
605 writel(temp, sport->port.membase + UCR2);
606
Marc Kleine-Budde44118052008-07-28 12:10:34 +0200607#if defined CONFIG_ARCH_MX2 || defined CONFIG_ARCH_MX3
608 temp = readl(sport->port.membase + UCR3);
609 temp |= UCR3_RXDMUXSEL;
610 writel(temp, sport->port.membase + UCR3);
611#endif
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /*
614 * Enable modem status interrupts
615 */
616 spin_lock_irqsave(&sport->port.lock,flags);
617 imx_enable_ms(&sport->port);
618 spin_unlock_irqrestore(&sport->port.lock,flags);
619
620 return 0;
621
Sascha Hauerceca6292005-10-12 19:58:08 +0100622error_out3:
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200623 if (sport->txirq)
624 free_irq(sport->txirq, sport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625error_out2:
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200626 if (sport->rxirq)
627 free_irq(sport->rxirq, sport);
Sascha Hauer86371d02005-10-10 10:17:42 +0100628error_out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 return retval;
630}
631
632static void imx_shutdown(struct uart_port *port)
633{
634 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100635 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Fabian Godehardt2e146392009-06-11 14:38:38 +0100637 temp = readl(sport->port.membase + UCR2);
638 temp &= ~(UCR2_TXEN);
639 writel(temp, sport->port.membase + UCR2);
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /*
642 * Stop our timer.
643 */
644 del_timer_sync(&sport->timer);
645
646 /*
647 * Free the interrupts
648 */
Sascha Hauere3d13ff2008-07-05 10:02:48 +0200649 if (sport->txirq > 0) {
650 free_irq(sport->rtsirq, sport);
651 free_irq(sport->txirq, sport);
652 free_irq(sport->rxirq, sport);
653 } else
654 free_irq(sport->port.irq, sport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /*
657 * Disable all interrupts, port and break condition.
658 */
659
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100660 temp = readl(sport->port.membase + UCR1);
661 temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
662 writel(temp, sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665static void
Alan Cox606d0992006-12-08 02:38:45 -0800666imx_set_termios(struct uart_port *port, struct ktermios *termios,
667 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 struct imx_port *sport = (struct imx_port *)port;
670 unsigned long flags;
671 unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
672 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
Sascha Hauer036bb152008-07-05 10:02:44 +0200673 unsigned int div, num, denom, ufcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 /*
676 * If we don't support modem control lines, don't allow
677 * these to be set.
678 */
679 if (0) {
680 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
681 termios->c_cflag |= CLOCAL;
682 }
683
684 /*
685 * We only support CS7 and CS8.
686 */
687 while ((termios->c_cflag & CSIZE) != CS7 &&
688 (termios->c_cflag & CSIZE) != CS8) {
689 termios->c_cflag &= ~CSIZE;
690 termios->c_cflag |= old_csize;
691 old_csize = CS8;
692 }
693
694 if ((termios->c_cflag & CSIZE) == CS8)
695 ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
696 else
697 ucr2 = UCR2_SRST | UCR2_IRTS;
698
699 if (termios->c_cflag & CRTSCTS) {
Sascha Hauer5b802342006-05-04 14:07:42 +0100700 if( sport->have_rtscts ) {
701 ucr2 &= ~UCR2_IRTS;
702 ucr2 |= UCR2_CTSC;
703 } else {
704 termios->c_cflag &= ~CRTSCTS;
705 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
707
708 if (termios->c_cflag & CSTOPB)
709 ucr2 |= UCR2_STPB;
710 if (termios->c_cflag & PARENB) {
711 ucr2 |= UCR2_PREN;
Matt Reimer3261e362006-01-13 20:51:44 +0000712 if (termios->c_cflag & PARODD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 ucr2 |= UCR2_PROE;
714 }
715
716 /*
717 * Ask the core to calculate the divisor for us.
718 */
Sascha Hauer036bb152008-07-05 10:02:44 +0200719 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 quot = uart_get_divisor(port, baud);
721
722 spin_lock_irqsave(&sport->port.lock, flags);
723
724 sport->port.read_status_mask = 0;
725 if (termios->c_iflag & INPCK)
726 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
727 if (termios->c_iflag & (BRKINT | PARMRK))
728 sport->port.read_status_mask |= URXD_BRK;
729
730 /*
731 * Characters to ignore
732 */
733 sport->port.ignore_status_mask = 0;
734 if (termios->c_iflag & IGNPAR)
735 sport->port.ignore_status_mask |= URXD_PRERR;
736 if (termios->c_iflag & IGNBRK) {
737 sport->port.ignore_status_mask |= URXD_BRK;
738 /*
739 * If we're ignoring parity and break indicators,
740 * ignore overruns too (for real raw support).
741 */
742 if (termios->c_iflag & IGNPAR)
743 sport->port.ignore_status_mask |= URXD_OVRRUN;
744 }
745
746 del_timer_sync(&sport->timer);
747
748 /*
749 * Update the per-port timeout.
750 */
751 uart_update_timeout(port, termios->c_cflag, baud);
752
753 /*
754 * disable interrupts and drain transmitter
755 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100756 old_ucr1 = readl(sport->port.membase + UCR1);
757 writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
758 sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100760 while ( !(readl(sport->port.membase + USR2) & USR2_TXDC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 barrier();
762
763 /* then, disable everything */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100764 old_txrxen = readl(sport->port.membase + UCR2);
765 writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN),
766 sport->port.membase + UCR2);
767 old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Sascha Hauer036bb152008-07-05 10:02:44 +0200769 div = sport->port.uartclk / (baud * 16);
770 if (div > 7)
771 div = 7;
772 if (!div)
773 div = 1;
774
775 num = baud;
776 denom = port->uartclk / div / 16;
777
778 /* shift num and denom right until they fit into 16 bits */
779 while (num > 0x10000 || denom > 0x10000) {
780 num >>= 1;
781 denom >>= 1;
782 }
783 if (num > 0)
784 num -= 1;
785 if (denom > 0)
786 denom -= 1;
787
788 writel(num, sport->port.membase + UBIR);
789 writel(denom, sport->port.membase + UBMR);
790
791 if (div == 7)
792 div = 6; /* 6 in RFDIV means divide by 7 */
793 else
794 div = 6 - div;
795
796 ufcr = readl(sport->port.membase + UFCR);
797 ufcr = (ufcr & (~UFCR_RFDIV)) |
798 (div << 7);
799 writel(ufcr, sport->port.membase + UFCR);
800
801#ifdef ONEMS
802 writel(sport->port.uartclk / div / 1000, sport->port.membase + ONEMS);
803#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100805 writel(old_ucr1, sport->port.membase + UCR1);
806
807 /* set the parity, stop bits and data size */
808 writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
811 imx_enable_ms(&sport->port);
812
813 spin_unlock_irqrestore(&sport->port.lock, flags);
814}
815
816static const char *imx_type(struct uart_port *port)
817{
818 struct imx_port *sport = (struct imx_port *)port;
819
820 return sport->port.type == PORT_IMX ? "IMX" : NULL;
821}
822
823/*
824 * Release the memory region(s) being used by 'port'.
825 */
826static void imx_release_port(struct uart_port *port)
827{
Sascha Hauer3d454442008-04-17 08:47:32 +0100828 struct platform_device *pdev = to_platform_device(port->dev);
829 struct resource *mmres;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Sascha Hauer3d454442008-04-17 08:47:32 +0100831 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
832 release_mem_region(mmres->start, mmres->end - mmres->start + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835/*
836 * Request the memory region(s) being used by 'port'.
837 */
838static int imx_request_port(struct uart_port *port)
839{
Sascha Hauer3d454442008-04-17 08:47:32 +0100840 struct platform_device *pdev = to_platform_device(port->dev);
841 struct resource *mmres;
842 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Sascha Hauer3d454442008-04-17 08:47:32 +0100844 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
845 if (!mmres)
846 return -ENODEV;
847
848 ret = request_mem_region(mmres->start, mmres->end - mmres->start + 1,
849 "imx-uart");
850
851 return ret ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
854/*
855 * Configure/autoconfigure the port.
856 */
857static void imx_config_port(struct uart_port *port, int flags)
858{
859 struct imx_port *sport = (struct imx_port *)port;
860
861 if (flags & UART_CONFIG_TYPE &&
862 imx_request_port(&sport->port) == 0)
863 sport->port.type = PORT_IMX;
864}
865
866/*
867 * Verify the new serial_struct (for TIOCSSERIAL).
868 * The only change we allow are to the flags and type, and
869 * even then only between PORT_IMX and PORT_UNKNOWN
870 */
871static int
872imx_verify_port(struct uart_port *port, struct serial_struct *ser)
873{
874 struct imx_port *sport = (struct imx_port *)port;
875 int ret = 0;
876
877 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
878 ret = -EINVAL;
879 if (sport->port.irq != ser->irq)
880 ret = -EINVAL;
881 if (ser->io_type != UPIO_MEM)
882 ret = -EINVAL;
883 if (sport->port.uartclk / 16 != ser->baud_base)
884 ret = -EINVAL;
885 if ((void *)sport->port.mapbase != ser->iomem_base)
886 ret = -EINVAL;
887 if (sport->port.iobase != ser->port)
888 ret = -EINVAL;
889 if (ser->hub6 != 0)
890 ret = -EINVAL;
891 return ret;
892}
893
894static struct uart_ops imx_pops = {
895 .tx_empty = imx_tx_empty,
896 .set_mctrl = imx_set_mctrl,
897 .get_mctrl = imx_get_mctrl,
898 .stop_tx = imx_stop_tx,
899 .start_tx = imx_start_tx,
900 .stop_rx = imx_stop_rx,
901 .enable_ms = imx_enable_ms,
902 .break_ctl = imx_break_ctl,
903 .startup = imx_startup,
904 .shutdown = imx_shutdown,
905 .set_termios = imx_set_termios,
906 .type = imx_type,
907 .release_port = imx_release_port,
908 .request_port = imx_request_port,
909 .config_port = imx_config_port,
910 .verify_port = imx_verify_port,
911};
912
Sascha Hauerdbff4e92008-07-05 10:02:45 +0200913static struct imx_port *imx_ports[UART_NR];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915#ifdef CONFIG_SERIAL_IMX_CONSOLE
Russell Kingd3587882006-03-20 20:00:09 +0000916static void imx_console_putchar(struct uart_port *port, int ch)
917{
918 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100919
920 while (readl(sport->port.membase + UTS) & UTS_TXFULL)
Russell Kingd3587882006-03-20 20:00:09 +0000921 barrier();
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100922
923 writel(ch, sport->port.membase + URTX0);
Russell Kingd3587882006-03-20 20:00:09 +0000924}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926/*
927 * Interrupts are disabled on entering
928 */
929static void
930imx_console_write(struct console *co, const char *s, unsigned int count)
931{
Sascha Hauerdbff4e92008-07-05 10:02:45 +0200932 struct imx_port *sport = imx_ports[co->index];
Russell Kingd3587882006-03-20 20:00:09 +0000933 unsigned int old_ucr1, old_ucr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /*
936 * First, save UCR1/2 and then disable interrupts
937 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100938 old_ucr1 = readl(sport->port.membase + UCR1);
939 old_ucr2 = readl(sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100941 writel((old_ucr1 | UCR1_UARTCLKEN | UCR1_UARTEN) &
942 ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
943 sport->port.membase + UCR1);
944
945 writel(old_ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Russell Kingd3587882006-03-20 20:00:09 +0000947 uart_console_write(&sport->port, s, count, imx_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 /*
950 * Finally, wait for transmitter to become empty
951 * and restore UCR1/2
952 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100953 while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100955 writel(old_ucr1, sport->port.membase + UCR1);
956 writel(old_ucr2, sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
959/*
960 * If the port was already initialised (eg, by a boot loader),
961 * try to determine the current setup.
962 */
963static void __init
964imx_console_get_options(struct imx_port *sport, int *baud,
965 int *parity, int *bits)
966{
Sascha Hauer587897f2005-04-29 22:46:40 +0100967
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100968 if ( readl(sport->port.membase + UCR1) | UCR1_UARTEN ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 /* ok, the port was enabled */
970 unsigned int ucr2, ubir,ubmr, uartclk;
Sascha Hauer587897f2005-04-29 22:46:40 +0100971 unsigned int baud_raw;
972 unsigned int ucfr_rfdiv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100974 ucr2 = readl(sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 *parity = 'n';
977 if (ucr2 & UCR2_PREN) {
978 if (ucr2 & UCR2_PROE)
979 *parity = 'o';
980 else
981 *parity = 'e';
982 }
983
984 if (ucr2 & UCR2_WS)
985 *bits = 8;
986 else
987 *bits = 7;
988
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100989 ubir = readl(sport->port.membase + UBIR) & 0xffff;
990 ubmr = readl(sport->port.membase + UBMR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100992 ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
Sascha Hauer587897f2005-04-29 22:46:40 +0100993 if (ucfr_rfdiv == 6)
994 ucfr_rfdiv = 7;
995 else
996 ucfr_rfdiv = 6 - ucfr_rfdiv;
997
Sascha Hauer38a41fd2008-07-05 10:02:46 +0200998 uartclk = clk_get_rate(sport->clk);
Sascha Hauer587897f2005-04-29 22:46:40 +0100999 uartclk /= ucfr_rfdiv;
1000
1001 { /*
1002 * The next code provides exact computation of
1003 * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
1004 * without need of float support or long long division,
1005 * which would be required to prevent 32bit arithmetic overflow
1006 */
1007 unsigned int mul = ubir + 1;
1008 unsigned int div = 16 * (ubmr + 1);
1009 unsigned int rem = uartclk % div;
1010
1011 baud_raw = (uartclk / div) * mul;
1012 baud_raw += (rem * mul + div / 2) / div;
1013 *baud = (baud_raw + 50) / 100 * 100;
1014 }
1015
1016 if(*baud != baud_raw)
1017 printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
1018 baud_raw, *baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020}
1021
1022static int __init
1023imx_console_setup(struct console *co, char *options)
1024{
1025 struct imx_port *sport;
1026 int baud = 9600;
1027 int bits = 8;
1028 int parity = 'n';
1029 int flow = 'n';
1030
1031 /*
1032 * Check whether an invalid uart number has been specified, and
1033 * if so, search for the first available port that does have
1034 * console support.
1035 */
1036 if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
1037 co->index = 0;
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001038 sport = imx_ports[co->index];
Eric Lammertse76afc42009-05-19 20:53:20 -04001039 if(sport == NULL)
1040 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
1042 if (options)
1043 uart_parse_options(options, &baud, &parity, &bits, &flow);
1044 else
1045 imx_console_get_options(sport, &baud, &parity, &bits);
1046
Sascha Hauer587897f2005-04-29 22:46:40 +01001047 imx_setup_ufcr(sport, 0);
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
1050}
1051
Vincent Sanders9f4426d2005-10-01 22:56:34 +01001052static struct uart_driver imx_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053static struct console imx_console = {
Sascha Hauere3d13ff2008-07-05 10:02:48 +02001054 .name = DEV_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 .write = imx_console_write,
1056 .device = uart_console_device,
1057 .setup = imx_console_setup,
1058 .flags = CON_PRINTBUFFER,
1059 .index = -1,
1060 .data = &imx_reg,
1061};
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063#define IMX_CONSOLE &imx_console
1064#else
1065#define IMX_CONSOLE NULL
1066#endif
1067
1068static struct uart_driver imx_reg = {
1069 .owner = THIS_MODULE,
1070 .driver_name = DRIVER_NAME,
Sascha Hauere3d13ff2008-07-05 10:02:48 +02001071 .dev_name = DEV_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 .major = SERIAL_IMX_MAJOR,
1073 .minor = MINOR_START,
1074 .nr = ARRAY_SIZE(imx_ports),
1075 .cons = IMX_CONSOLE,
1076};
1077
Russell King3ae5eae2005-11-09 22:32:44 +00001078static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Oskar Schirmerd3810cd2009-06-11 14:35:01 +01001080 struct imx_port *sport = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Oskar Schirmerd3810cd2009-06-11 14:35:01 +01001082 if (sport)
1083 uart_suspend_port(&imx_reg, &sport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Oskar Schirmerd3810cd2009-06-11 14:35:01 +01001085 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086}
1087
Russell King3ae5eae2005-11-09 22:32:44 +00001088static int serial_imx_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Oskar Schirmerd3810cd2009-06-11 14:35:01 +01001090 struct imx_port *sport = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Oskar Schirmerd3810cd2009-06-11 14:35:01 +01001092 if (sport)
1093 uart_resume_port(&imx_reg, &sport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Oskar Schirmerd3810cd2009-06-11 14:35:01 +01001095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096}
1097
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001098static int serial_imx_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099{
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001100 struct imx_port *sport;
Sascha Hauer5b802342006-05-04 14:07:42 +01001101 struct imxuart_platform_data *pdata;
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001102 void __iomem *base;
1103 int ret = 0;
1104 struct resource *res;
Sascha Hauer5b802342006-05-04 14:07:42 +01001105
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001106 sport = kzalloc(sizeof(*sport), GFP_KERNEL);
1107 if (!sport)
1108 return -ENOMEM;
1109
1110 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1111 if (!res) {
1112 ret = -ENODEV;
1113 goto free;
1114 }
1115
1116 base = ioremap(res->start, PAGE_SIZE);
1117 if (!base) {
1118 ret = -ENOMEM;
1119 goto free;
1120 }
1121
1122 sport->port.dev = &pdev->dev;
1123 sport->port.mapbase = res->start;
1124 sport->port.membase = base;
1125 sport->port.type = PORT_IMX,
1126 sport->port.iotype = UPIO_MEM;
1127 sport->port.irq = platform_get_irq(pdev, 0);
1128 sport->rxirq = platform_get_irq(pdev, 0);
1129 sport->txirq = platform_get_irq(pdev, 1);
1130 sport->rtsirq = platform_get_irq(pdev, 2);
1131 sport->port.fifosize = 32;
1132 sport->port.ops = &imx_pops;
1133 sport->port.flags = UPF_BOOT_AUTOCONF;
1134 sport->port.line = pdev->id;
1135 init_timer(&sport->timer);
1136 sport->timer.function = imx_timeout;
1137 sport->timer.data = (unsigned long)sport;
Sascha Hauer38a41fd2008-07-05 10:02:46 +02001138
Sascha Hauere65fb002009-02-16 14:29:10 +01001139 sport->clk = clk_get(&pdev->dev, "uart");
Sascha Hauer38a41fd2008-07-05 10:02:46 +02001140 if (IS_ERR(sport->clk)) {
1141 ret = PTR_ERR(sport->clk);
1142 goto unmap;
1143 }
1144 clk_enable(sport->clk);
1145
1146 sport->port.uartclk = clk_get_rate(sport->clk);
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001147
1148 imx_ports[pdev->id] = sport;
Sascha Hauer5b802342006-05-04 14:07:42 +01001149
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001150 pdata = pdev->dev.platform_data;
Oskar Schirmerd3810cd2009-06-11 14:35:01 +01001151 if (pdata && (pdata->flags & IMXUART_HAVE_RTSCTS))
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001152 sport->have_rtscts = 1;
Sascha Hauer5b802342006-05-04 14:07:42 +01001153
Darius Augulisc45e7d72008-09-02 10:19:29 +02001154 if (pdata->init) {
1155 ret = pdata->init(pdev);
1156 if (ret)
1157 goto clkput;
1158 }
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001159
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001160 uart_add_one_port(&imx_reg, &sport->port);
1161 platform_set_drvdata(pdev, &sport->port);
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return 0;
Darius Augulisc45e7d72008-09-02 10:19:29 +02001164clkput:
1165 clk_put(sport->clk);
1166 clk_disable(sport->clk);
Sascha Hauer38a41fd2008-07-05 10:02:46 +02001167unmap:
1168 iounmap(sport->port.membase);
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001169free:
1170 kfree(sport);
1171
1172 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001175static int serial_imx_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001177 struct imxuart_platform_data *pdata;
1178 struct imx_port *sport = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001180 pdata = pdev->dev.platform_data;
1181
1182 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Sascha Hauer38a41fd2008-07-05 10:02:46 +02001184 if (sport) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 uart_remove_one_port(&imx_reg, &sport->port);
Sascha Hauer38a41fd2008-07-05 10:02:46 +02001186 clk_put(sport->clk);
1187 }
1188
1189 clk_disable(sport->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001191 if (pdata->exit)
1192 pdata->exit(pdev);
1193
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001194 iounmap(sport->port.membase);
1195 kfree(sport);
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return 0;
1198}
1199
Russell King3ae5eae2005-11-09 22:32:44 +00001200static struct platform_driver serial_imx_driver = {
Oskar Schirmerd3810cd2009-06-11 14:35:01 +01001201 .probe = serial_imx_probe,
1202 .remove = serial_imx_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 .suspend = serial_imx_suspend,
1205 .resume = serial_imx_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001206 .driver = {
Oskar Schirmerd3810cd2009-06-11 14:35:01 +01001207 .name = "imx-uart",
Kay Sieverse169c132008-04-15 14:34:35 -07001208 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001209 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210};
1211
1212static int __init imx_serial_init(void)
1213{
1214 int ret;
1215
1216 printk(KERN_INFO "Serial: IMX driver\n");
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 ret = uart_register_driver(&imx_reg);
1219 if (ret)
1220 return ret;
1221
Russell King3ae5eae2005-11-09 22:32:44 +00001222 ret = platform_driver_register(&serial_imx_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (ret != 0)
1224 uart_unregister_driver(&imx_reg);
1225
1226 return 0;
1227}
1228
1229static void __exit imx_serial_exit(void)
1230{
Russell Kingc889b892005-11-21 17:05:21 +00001231 platform_driver_unregister(&serial_imx_driver);
Sascha Hauer4b300c32007-07-17 13:35:46 +01001232 uart_unregister_driver(&imx_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
1235module_init(imx_serial_init);
1236module_exit(imx_serial_exit);
1237
1238MODULE_AUTHOR("Sascha Hauer");
1239MODULE_DESCRIPTION("IMX generic serial port driver");
1240MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001241MODULE_ALIAS("platform:imx-uart");