blob: 8d6cb745bd93cf236980dbdeee0ae859232329af [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>
43
44#include <asm/io.h>
45#include <asm/irq.h>
46#include <asm/hardware.h>
Sascha Hauer5b802342006-05-04 14:07:42 +010047#include <asm/arch/imx-uart.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Sascha Hauerff4bfb22007-04-26 08:26:13 +010049/* Register definitions */
50#define URXD0 0x0 /* Receiver Register */
51#define URTX0 0x40 /* Transmitter Register */
52#define UCR1 0x80 /* Control Register 1 */
53#define UCR2 0x84 /* Control Register 2 */
54#define UCR3 0x88 /* Control Register 3 */
55#define UCR4 0x8c /* Control Register 4 */
56#define UFCR 0x90 /* FIFO Control Register */
57#define USR1 0x94 /* Status Register 1 */
58#define USR2 0x98 /* Status Register 2 */
59#define UESC 0x9c /* Escape Character Register */
60#define UTIM 0xa0 /* Escape Timer Register */
61#define UBIR 0xa4 /* BRM Incremental Register */
62#define UBMR 0xa8 /* BRM Modulator Register */
63#define UBRC 0xac /* Baud Rate Count Register */
64#define BIPR1 0xb0 /* Incremental Preset Register 1 */
65#define BIPR2 0xb4 /* Incremental Preset Register 2 */
66#define BIPR3 0xb8 /* Incremental Preset Register 3 */
67#define BIPR4 0xbc /* Incremental Preset Register 4 */
68#define BMPR1 0xc0 /* BRM Modulator Register 1 */
69#define BMPR2 0xc4 /* BRM Modulator Register 2 */
70#define BMPR3 0xc8 /* BRM Modulator Register 3 */
71#define BMPR4 0xcc /* BRM Modulator Register 4 */
72#define UTS 0xd0 /* UART Test Register */
73
74/* UART Control Register Bit Fields.*/
75#define URXD_CHARRDY (1<<15)
76#define URXD_ERR (1<<14)
77#define URXD_OVRRUN (1<<13)
78#define URXD_FRMERR (1<<12)
79#define URXD_BRK (1<<11)
80#define URXD_PRERR (1<<10)
81#define UCR1_ADEN (1<<15) /* Auto dectect interrupt */
82#define UCR1_ADBR (1<<14) /* Auto detect baud rate */
83#define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
84#define UCR1_IDEN (1<<12) /* Idle condition interrupt */
85#define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
86#define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
87#define UCR1_IREN (1<<7) /* Infrared interface enable */
88#define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
89#define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
90#define UCR1_SNDBRK (1<<4) /* Send break */
91#define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
92#define UCR1_UARTCLKEN (1<<2) /* UART clock enabled */
93#define UCR1_DOZE (1<<1) /* Doze */
94#define UCR1_UARTEN (1<<0) /* UART enabled */
95#define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
96#define UCR2_IRTS (1<<14) /* Ignore RTS pin */
97#define UCR2_CTSC (1<<13) /* CTS pin control */
98#define UCR2_CTS (1<<12) /* Clear to send */
99#define UCR2_ESCEN (1<<11) /* Escape enable */
100#define UCR2_PREN (1<<8) /* Parity enable */
101#define UCR2_PROE (1<<7) /* Parity odd/even */
102#define UCR2_STPB (1<<6) /* Stop */
103#define UCR2_WS (1<<5) /* Word size */
104#define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
105#define UCR2_TXEN (1<<2) /* Transmitter enabled */
106#define UCR2_RXEN (1<<1) /* Receiver enabled */
107#define UCR2_SRST (1<<0) /* SW reset */
108#define UCR3_DTREN (1<<13) /* DTR interrupt enable */
109#define UCR3_PARERREN (1<<12) /* Parity enable */
110#define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
111#define UCR3_DSR (1<<10) /* Data set ready */
112#define UCR3_DCD (1<<9) /* Data carrier detect */
113#define UCR3_RI (1<<8) /* Ring indicator */
114#define UCR3_TIMEOUTEN (1<<7) /* Timeout interrupt enable */
115#define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
116#define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
117#define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
118#define UCR3_REF25 (1<<3) /* Ref freq 25 MHz */
119#define UCR3_REF30 (1<<2) /* Ref Freq 30 MHz */
120#define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
121#define UCR3_BPEN (1<<0) /* Preset registers enable */
122#define UCR4_CTSTL_32 (32<<10) /* CTS trigger level (32 chars) */
123#define UCR4_INVR (1<<9) /* Inverted infrared reception */
124#define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
125#define UCR4_WKEN (1<<7) /* Wake interrupt enable */
126#define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
127#define UCR4_IRSC (1<<5) /* IR special case */
128#define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
129#define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
130#define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
131#define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
132#define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
133#define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
134#define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
135#define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
136#define USR1_RTSS (1<<14) /* RTS pin status */
137#define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
138#define USR1_RTSD (1<<12) /* RTS delta */
139#define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
140#define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
141#define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
142#define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */
143#define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
144#define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
145#define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
146#define USR2_ADET (1<<15) /* Auto baud rate detect complete */
147#define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
148#define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
149#define USR2_IDLE (1<<12) /* Idle condition */
150#define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
151#define USR2_WAKE (1<<7) /* Wake */
152#define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
153#define USR2_TXDC (1<<3) /* Transmitter complete */
154#define USR2_BRCD (1<<2) /* Break condition */
155#define USR2_ORE (1<<1) /* Overrun error */
156#define USR2_RDR (1<<0) /* Recv data ready */
157#define UTS_FRCPERR (1<<13) /* Force parity error */
158#define UTS_LOOP (1<<12) /* Loop tx and rx */
159#define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
160#define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
161#define UTS_TXFULL (1<<4) /* TxFIFO full */
162#define UTS_RXFULL (1<<3) /* RxFIFO full */
163#define UTS_SOFTRST (1<<0) /* Software reset */
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/* We've been assigned a range on the "Low-density serial ports" major */
166#define SERIAL_IMX_MAJOR 204
167#define MINOR_START 41
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * This determines how often we check the modem status signals
171 * for any change. They generally aren't connected to an IRQ
172 * so we have to poll them. We also check immediately before
173 * filling the TX fifo incase CTS has been dropped.
174 */
175#define MCTRL_TIMEOUT (250*HZ/1000)
176
177#define DRIVER_NAME "IMX-uart"
178
Sascha Hauerdbff4e92008-07-05 10:02:45 +0200179#define UART_NR 8
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181struct imx_port {
182 struct uart_port port;
183 struct timer_list timer;
184 unsigned int old_status;
Sascha Hauer5b802342006-05-04 14:07:42 +0100185 int txirq,rxirq,rtsirq;
186 int have_rtscts:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187};
188
189/*
190 * Handle any change of modem status signal since we were last called.
191 */
192static void imx_mctrl_check(struct imx_port *sport)
193{
194 unsigned int status, changed;
195
196 status = sport->port.ops->get_mctrl(&sport->port);
197 changed = status ^ sport->old_status;
198
199 if (changed == 0)
200 return;
201
202 sport->old_status = status;
203
204 if (changed & TIOCM_RI)
205 sport->port.icount.rng++;
206 if (changed & TIOCM_DSR)
207 sport->port.icount.dsr++;
208 if (changed & TIOCM_CAR)
209 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
210 if (changed & TIOCM_CTS)
211 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
212
213 wake_up_interruptible(&sport->port.info->delta_msr_wait);
214}
215
216/*
217 * This is our per-port timeout handler, for checking the
218 * modem status signals.
219 */
220static void imx_timeout(unsigned long data)
221{
222 struct imx_port *sport = (struct imx_port *)data;
223 unsigned long flags;
224
225 if (sport->port.info) {
226 spin_lock_irqsave(&sport->port.lock, flags);
227 imx_mctrl_check(sport);
228 spin_unlock_irqrestore(&sport->port.lock, flags);
229
230 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
231 }
232}
233
234/*
235 * interrupts disabled on entry
236 */
Russell Kingb129a8c2005-08-31 10:12:14 +0100237static void imx_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100240 unsigned long temp;
241
242 temp = readl(sport->port.membase + UCR1);
243 writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246/*
247 * interrupts disabled on entry
248 */
249static void imx_stop_rx(struct uart_port *port)
250{
251 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100252 unsigned long temp;
253
254 temp = readl(sport->port.membase + UCR2);
255 writel(temp &~ UCR2_RXEN, sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/*
259 * Set the modem control timer to fire immediately.
260 */
261static void imx_enable_ms(struct uart_port *port)
262{
263 struct imx_port *sport = (struct imx_port *)port;
264
265 mod_timer(&sport->timer, jiffies);
266}
267
268static inline void imx_transmit_buffer(struct imx_port *sport)
269{
270 struct circ_buf *xmit = &sport->port.info->xmit;
271
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100272 while (!(readl(sport->port.membase + UTS) & UTS_TXFULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* send xmit->buf[xmit->tail]
274 * out the port here */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100275 writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 xmit->tail = (xmit->tail + 1) &
277 (UART_XMIT_SIZE - 1);
278 sport->port.icount.tx++;
279 if (uart_circ_empty(xmit))
280 break;
Sascha Hauer8c0b2542007-02-05 16:10:16 -0800281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100284 imx_stop_tx(&sport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287/*
288 * interrupts disabled on entry
289 */
Russell Kingb129a8c2005-08-31 10:12:14 +0100290static void imx_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100293 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100295 temp = readl(sport->port.membase + UCR1);
296 writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100298 if (readl(sport->port.membase + UTS) & UTS_TXEMPTY)
299 imx_transmit_buffer(sport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300}
301
David Howells7d12e782006-10-05 14:55:46 +0100302static irqreturn_t imx_rtsint(int irq, void *dev_id)
Sascha Hauerceca6292005-10-12 19:58:08 +0100303{
Jeff Garzik15aafa22008-02-06 01:36:20 -0800304 struct imx_port *sport = dev_id;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100305 unsigned int val = readl(sport->port.membase + USR1) & USR1_RTSS;
Sascha Hauerceca6292005-10-12 19:58:08 +0100306 unsigned long flags;
307
308 spin_lock_irqsave(&sport->port.lock, flags);
309
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100310 writel(USR1_RTSD, sport->port.membase + USR1);
Sascha Hauerceca6292005-10-12 19:58:08 +0100311 uart_handle_cts_change(&sport->port, !!val);
312 wake_up_interruptible(&sport->port.info->delta_msr_wait);
313
314 spin_unlock_irqrestore(&sport->port.lock, flags);
315 return IRQ_HANDLED;
316}
317
David Howells7d12e782006-10-05 14:55:46 +0100318static irqreturn_t imx_txint(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319{
Jeff Garzik15aafa22008-02-06 01:36:20 -0800320 struct imx_port *sport = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 struct circ_buf *xmit = &sport->port.info->xmit;
322 unsigned long flags;
323
324 spin_lock_irqsave(&sport->port.lock,flags);
325 if (sport->port.x_char)
326 {
327 /* Send next char */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100328 writel(sport->port.x_char, sport->port.membase + URTX0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 goto out;
330 }
331
332 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100333 imx_stop_tx(&sport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 goto out;
335 }
336
337 imx_transmit_buffer(sport);
338
339 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
340 uart_write_wakeup(&sport->port);
341
342out:
343 spin_unlock_irqrestore(&sport->port.lock,flags);
344 return IRQ_HANDLED;
345}
346
David Howells7d12e782006-10-05 14:55:46 +0100347static irqreturn_t imx_rxint(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct imx_port *sport = dev_id;
350 unsigned int rx,flg,ignored = 0;
351 struct tty_struct *tty = sport->port.info->tty;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100352 unsigned long flags, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 spin_lock_irqsave(&sport->port.lock,flags);
355
Sascha Hauer0d3c3932008-04-17 08:43:14 +0100356 while (readl(sport->port.membase + USR2) & USR2_RDR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 flg = TTY_NORMAL;
358 sport->port.icount.rx++;
359
Sascha Hauer0d3c3932008-04-17 08:43:14 +0100360 rx = readl(sport->port.membase + URXD0);
361
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100362 temp = readl(sport->port.membase + USR2);
Sascha Hauer864eeed2008-04-17 08:39:22 +0100363 if (temp & USR2_BRCD) {
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100364 writel(temp | USR2_BRCD, sport->port.membase + USR2);
Sascha Hauer864eeed2008-04-17 08:39:22 +0100365 if (uart_handle_break(&sport->port))
366 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
369 if (uart_handle_sysrq_char
David Howells7d12e782006-10-05 14:55:46 +0100370 (&sport->port, (unsigned char)rx))
Sascha Hauer864eeed2008-04-17 08:39:22 +0100371 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Sascha Hauer864eeed2008-04-17 08:39:22 +0100373 if (rx & (URXD_PRERR | URXD_OVRRUN | URXD_FRMERR) ) {
374 if (rx & URXD_PRERR)
375 sport->port.icount.parity++;
376 else if (rx & URXD_FRMERR)
377 sport->port.icount.frame++;
378 if (rx & URXD_OVRRUN)
379 sport->port.icount.overrun++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Sascha Hauer864eeed2008-04-17 08:39:22 +0100381 if (rx & sport->port.ignore_status_mask) {
382 if (++ignored > 100)
383 goto out;
384 continue;
385 }
386
387 rx &= sport->port.read_status_mask;
388
389 if (rx & URXD_PRERR)
390 flg = TTY_PARITY;
391 else if (rx & URXD_FRMERR)
392 flg = TTY_FRAME;
393 if (rx & URXD_OVRRUN)
394 flg = TTY_OVERRUN;
395
396#ifdef SUPPORT_SYSRQ
397 sport->port.sysrq = 0;
398#endif
399 }
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 tty_insert_flip_char(tty, rx, flg);
Sascha Hauer864eeed2008-04-17 08:39:22 +0100402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404out:
405 spin_unlock_irqrestore(&sport->port.lock,flags);
406 tty_flip_buffer_push(tty);
407 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410/*
411 * Return TIOCSER_TEMT when transmitter is not busy.
412 */
413static unsigned int imx_tx_empty(struct uart_port *port)
414{
415 struct imx_port *sport = (struct imx_port *)port;
416
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100417 return (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100420/*
421 * We have a modem side uart, so the meanings of RTS and CTS are inverted.
422 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423static unsigned int imx_get_mctrl(struct uart_port *port)
424{
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100425 struct imx_port *sport = (struct imx_port *)port;
426 unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
427
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100428 if (readl(sport->port.membase + USR1) & USR1_RTSS)
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100429 tmp |= TIOCM_CTS;
430
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100431 if (readl(sport->port.membase + UCR2) & UCR2_CTS)
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100432 tmp |= TIOCM_RTS;
433
434 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435}
436
437static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
438{
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100439 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100440 unsigned long temp;
441
442 temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS;
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100443
444 if (mctrl & TIOCM_RTS)
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100445 temp |= UCR2_CTS;
446
447 writel(temp, sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450/*
451 * Interrupts always disabled.
452 */
453static void imx_break_ctl(struct uart_port *port, int break_state)
454{
455 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100456 unsigned long flags, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 spin_lock_irqsave(&sport->port.lock, flags);
459
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100460 temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if ( break_state != 0 )
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100463 temp |= UCR1_SNDBRK;
464
465 writel(temp, sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 spin_unlock_irqrestore(&sport->port.lock, flags);
468}
469
470#define TXTL 2 /* reset default */
471#define RXTL 1 /* reset default */
472
Sascha Hauer587897f2005-04-29 22:46:40 +0100473static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
474{
475 unsigned int val;
476 unsigned int ufcr_rfdiv;
477
478 /* set receiver / transmitter trigger level.
479 * RFDIV is set such way to satisfy requested uartclk value
480 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100481 val = TXTL << 10 | RXTL;
Sascha Hauer587897f2005-04-29 22:46:40 +0100482 ufcr_rfdiv = (imx_get_perclk1() + sport->port.uartclk / 2) / sport->port.uartclk;
483
484 if(!ufcr_rfdiv)
485 ufcr_rfdiv = 1;
486
487 if(ufcr_rfdiv >= 7)
488 ufcr_rfdiv = 6;
489 else
490 ufcr_rfdiv = 6 - ufcr_rfdiv;
491
492 val |= UFCR_RFDIV & (ufcr_rfdiv << 7);
493
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100494 writel(val, sport->port.membase + UFCR);
Sascha Hauer587897f2005-04-29 22:46:40 +0100495
496 return 0;
497}
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499static int imx_startup(struct uart_port *port)
500{
501 struct imx_port *sport = (struct imx_port *)port;
502 int retval;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100503 unsigned long flags, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Sascha Hauer587897f2005-04-29 22:46:40 +0100505 imx_setup_ufcr(sport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 /* disable the DREN bit (Data Ready interrupt enable) before
508 * requesting IRQs
509 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100510 temp = readl(sport->port.membase + UCR4);
511 writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 /*
514 * Allocate the IRQ
515 */
516 retval = request_irq(sport->rxirq, imx_rxint, 0,
517 DRIVER_NAME, sport);
Sascha Hauer86371d02005-10-10 10:17:42 +0100518 if (retval) goto error_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 retval = request_irq(sport->txirq, imx_txint, 0,
Sascha Hauerceca6292005-10-12 19:58:08 +0100521 DRIVER_NAME, sport);
Sascha Hauer86371d02005-10-10 10:17:42 +0100522 if (retval) goto error_out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Russell Kingf43aaba2006-01-19 12:26:57 +0000524 retval = request_irq(sport->rtsirq, imx_rtsint,
Pavel Pisad7ea10d2007-02-05 16:10:20 -0800525 (sport->rtsirq < IMX_IRQS) ? 0 :
526 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
Sascha Hauerceca6292005-10-12 19:58:08 +0100527 DRIVER_NAME, sport);
528 if (retval) goto error_out3;
Sascha Hauerceca6292005-10-12 19:58:08 +0100529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /*
531 * Finally, clear and enable interrupts
532 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100533 writel(USR1_RTSD, sport->port.membase + USR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100535 temp = readl(sport->port.membase + UCR1);
Sascha Hauer789d5252008-04-17 08:44:47 +0100536 temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100537 writel(temp, sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100539 temp = readl(sport->port.membase + UCR2);
540 temp |= (UCR2_RXEN | UCR2_TXEN);
541 writel(temp, sport->port.membase + UCR2);
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 /*
544 * Enable modem status interrupts
545 */
546 spin_lock_irqsave(&sport->port.lock,flags);
547 imx_enable_ms(&sport->port);
548 spin_unlock_irqrestore(&sport->port.lock,flags);
549
550 return 0;
551
Sascha Hauerceca6292005-10-12 19:58:08 +0100552error_out3:
553 free_irq(sport->txirq, sport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554error_out2:
Sascha Hauer86371d02005-10-10 10:17:42 +0100555 free_irq(sport->rxirq, sport);
556error_out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return retval;
558}
559
560static void imx_shutdown(struct uart_port *port)
561{
562 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100563 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 /*
566 * Stop our timer.
567 */
568 del_timer_sync(&sport->timer);
569
570 /*
571 * Free the interrupts
572 */
Sascha Hauerceca6292005-10-12 19:58:08 +0100573 free_irq(sport->rtsirq, sport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 free_irq(sport->txirq, sport);
575 free_irq(sport->rxirq, sport);
576
577 /*
578 * Disable all interrupts, port and break condition.
579 */
580
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100581 temp = readl(sport->port.membase + UCR1);
582 temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
583 writel(temp, sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
586static void
Alan Cox606d0992006-12-08 02:38:45 -0800587imx_set_termios(struct uart_port *port, struct ktermios *termios,
588 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct imx_port *sport = (struct imx_port *)port;
591 unsigned long flags;
592 unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
593 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
Sascha Hauer036bb152008-07-05 10:02:44 +0200594 unsigned int div, num, denom, ufcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 /*
597 * If we don't support modem control lines, don't allow
598 * these to be set.
599 */
600 if (0) {
601 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
602 termios->c_cflag |= CLOCAL;
603 }
604
605 /*
606 * We only support CS7 and CS8.
607 */
608 while ((termios->c_cflag & CSIZE) != CS7 &&
609 (termios->c_cflag & CSIZE) != CS8) {
610 termios->c_cflag &= ~CSIZE;
611 termios->c_cflag |= old_csize;
612 old_csize = CS8;
613 }
614
615 if ((termios->c_cflag & CSIZE) == CS8)
616 ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
617 else
618 ucr2 = UCR2_SRST | UCR2_IRTS;
619
620 if (termios->c_cflag & CRTSCTS) {
Sascha Hauer5b802342006-05-04 14:07:42 +0100621 if( sport->have_rtscts ) {
622 ucr2 &= ~UCR2_IRTS;
623 ucr2 |= UCR2_CTSC;
624 } else {
625 termios->c_cflag &= ~CRTSCTS;
626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
629 if (termios->c_cflag & CSTOPB)
630 ucr2 |= UCR2_STPB;
631 if (termios->c_cflag & PARENB) {
632 ucr2 |= UCR2_PREN;
Matt Reimer3261e362006-01-13 20:51:44 +0000633 if (termios->c_cflag & PARODD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 ucr2 |= UCR2_PROE;
635 }
636
637 /*
638 * Ask the core to calculate the divisor for us.
639 */
Sascha Hauer036bb152008-07-05 10:02:44 +0200640 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 quot = uart_get_divisor(port, baud);
642
643 spin_lock_irqsave(&sport->port.lock, flags);
644
645 sport->port.read_status_mask = 0;
646 if (termios->c_iflag & INPCK)
647 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
648 if (termios->c_iflag & (BRKINT | PARMRK))
649 sport->port.read_status_mask |= URXD_BRK;
650
651 /*
652 * Characters to ignore
653 */
654 sport->port.ignore_status_mask = 0;
655 if (termios->c_iflag & IGNPAR)
656 sport->port.ignore_status_mask |= URXD_PRERR;
657 if (termios->c_iflag & IGNBRK) {
658 sport->port.ignore_status_mask |= URXD_BRK;
659 /*
660 * If we're ignoring parity and break indicators,
661 * ignore overruns too (for real raw support).
662 */
663 if (termios->c_iflag & IGNPAR)
664 sport->port.ignore_status_mask |= URXD_OVRRUN;
665 }
666
667 del_timer_sync(&sport->timer);
668
669 /*
670 * Update the per-port timeout.
671 */
672 uart_update_timeout(port, termios->c_cflag, baud);
673
674 /*
675 * disable interrupts and drain transmitter
676 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100677 old_ucr1 = readl(sport->port.membase + UCR1);
678 writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
679 sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100681 while ( !(readl(sport->port.membase + USR2) & USR2_TXDC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 barrier();
683
684 /* then, disable everything */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100685 old_txrxen = readl(sport->port.membase + UCR2);
686 writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN),
687 sport->port.membase + UCR2);
688 old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Sascha Hauer036bb152008-07-05 10:02:44 +0200690 div = sport->port.uartclk / (baud * 16);
691 if (div > 7)
692 div = 7;
693 if (!div)
694 div = 1;
695
696 num = baud;
697 denom = port->uartclk / div / 16;
698
699 /* shift num and denom right until they fit into 16 bits */
700 while (num > 0x10000 || denom > 0x10000) {
701 num >>= 1;
702 denom >>= 1;
703 }
704 if (num > 0)
705 num -= 1;
706 if (denom > 0)
707 denom -= 1;
708
709 writel(num, sport->port.membase + UBIR);
710 writel(denom, sport->port.membase + UBMR);
711
712 if (div == 7)
713 div = 6; /* 6 in RFDIV means divide by 7 */
714 else
715 div = 6 - div;
716
717 ufcr = readl(sport->port.membase + UFCR);
718 ufcr = (ufcr & (~UFCR_RFDIV)) |
719 (div << 7);
720 writel(ufcr, sport->port.membase + UFCR);
721
722#ifdef ONEMS
723 writel(sport->port.uartclk / div / 1000, sport->port.membase + ONEMS);
724#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100726 writel(old_ucr1, sport->port.membase + UCR1);
727
728 /* set the parity, stop bits and data size */
729 writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
732 imx_enable_ms(&sport->port);
733
734 spin_unlock_irqrestore(&sport->port.lock, flags);
735}
736
737static const char *imx_type(struct uart_port *port)
738{
739 struct imx_port *sport = (struct imx_port *)port;
740
741 return sport->port.type == PORT_IMX ? "IMX" : NULL;
742}
743
744/*
745 * Release the memory region(s) being used by 'port'.
746 */
747static void imx_release_port(struct uart_port *port)
748{
Sascha Hauer3d454442008-04-17 08:47:32 +0100749 struct platform_device *pdev = to_platform_device(port->dev);
750 struct resource *mmres;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Sascha Hauer3d454442008-04-17 08:47:32 +0100752 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
753 release_mem_region(mmres->start, mmres->end - mmres->start + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
756/*
757 * Request the memory region(s) being used by 'port'.
758 */
759static int imx_request_port(struct uart_port *port)
760{
Sascha Hauer3d454442008-04-17 08:47:32 +0100761 struct platform_device *pdev = to_platform_device(port->dev);
762 struct resource *mmres;
763 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Sascha Hauer3d454442008-04-17 08:47:32 +0100765 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
766 if (!mmres)
767 return -ENODEV;
768
769 ret = request_mem_region(mmres->start, mmres->end - mmres->start + 1,
770 "imx-uart");
771
772 return ret ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
775/*
776 * Configure/autoconfigure the port.
777 */
778static void imx_config_port(struct uart_port *port, int flags)
779{
780 struct imx_port *sport = (struct imx_port *)port;
781
782 if (flags & UART_CONFIG_TYPE &&
783 imx_request_port(&sport->port) == 0)
784 sport->port.type = PORT_IMX;
785}
786
787/*
788 * Verify the new serial_struct (for TIOCSSERIAL).
789 * The only change we allow are to the flags and type, and
790 * even then only between PORT_IMX and PORT_UNKNOWN
791 */
792static int
793imx_verify_port(struct uart_port *port, struct serial_struct *ser)
794{
795 struct imx_port *sport = (struct imx_port *)port;
796 int ret = 0;
797
798 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
799 ret = -EINVAL;
800 if (sport->port.irq != ser->irq)
801 ret = -EINVAL;
802 if (ser->io_type != UPIO_MEM)
803 ret = -EINVAL;
804 if (sport->port.uartclk / 16 != ser->baud_base)
805 ret = -EINVAL;
806 if ((void *)sport->port.mapbase != ser->iomem_base)
807 ret = -EINVAL;
808 if (sport->port.iobase != ser->port)
809 ret = -EINVAL;
810 if (ser->hub6 != 0)
811 ret = -EINVAL;
812 return ret;
813}
814
815static struct uart_ops imx_pops = {
816 .tx_empty = imx_tx_empty,
817 .set_mctrl = imx_set_mctrl,
818 .get_mctrl = imx_get_mctrl,
819 .stop_tx = imx_stop_tx,
820 .start_tx = imx_start_tx,
821 .stop_rx = imx_stop_rx,
822 .enable_ms = imx_enable_ms,
823 .break_ctl = imx_break_ctl,
824 .startup = imx_startup,
825 .shutdown = imx_shutdown,
826 .set_termios = imx_set_termios,
827 .type = imx_type,
828 .release_port = imx_release_port,
829 .request_port = imx_request_port,
830 .config_port = imx_config_port,
831 .verify_port = imx_verify_port,
832};
833
Sascha Hauerdbff4e92008-07-05 10:02:45 +0200834static struct imx_port *imx_ports[UART_NR];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836#ifdef CONFIG_SERIAL_IMX_CONSOLE
Russell Kingd3587882006-03-20 20:00:09 +0000837static void imx_console_putchar(struct uart_port *port, int ch)
838{
839 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100840
841 while (readl(sport->port.membase + UTS) & UTS_TXFULL)
Russell Kingd3587882006-03-20 20:00:09 +0000842 barrier();
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100843
844 writel(ch, sport->port.membase + URTX0);
Russell Kingd3587882006-03-20 20:00:09 +0000845}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847/*
848 * Interrupts are disabled on entering
849 */
850static void
851imx_console_write(struct console *co, const char *s, unsigned int count)
852{
Sascha Hauerdbff4e92008-07-05 10:02:45 +0200853 struct imx_port *sport = imx_ports[co->index];
Russell Kingd3587882006-03-20 20:00:09 +0000854 unsigned int old_ucr1, old_ucr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 /*
857 * First, save UCR1/2 and then disable interrupts
858 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100859 old_ucr1 = readl(sport->port.membase + UCR1);
860 old_ucr2 = readl(sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100862 writel((old_ucr1 | UCR1_UARTCLKEN | UCR1_UARTEN) &
863 ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
864 sport->port.membase + UCR1);
865
866 writel(old_ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Russell Kingd3587882006-03-20 20:00:09 +0000868 uart_console_write(&sport->port, s, count, imx_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 /*
871 * Finally, wait for transmitter to become empty
872 * and restore UCR1/2
873 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100874 while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100876 writel(old_ucr1, sport->port.membase + UCR1);
877 writel(old_ucr2, sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
880/*
881 * If the port was already initialised (eg, by a boot loader),
882 * try to determine the current setup.
883 */
884static void __init
885imx_console_get_options(struct imx_port *sport, int *baud,
886 int *parity, int *bits)
887{
Sascha Hauer587897f2005-04-29 22:46:40 +0100888
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100889 if ( readl(sport->port.membase + UCR1) | UCR1_UARTEN ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 /* ok, the port was enabled */
891 unsigned int ucr2, ubir,ubmr, uartclk;
Sascha Hauer587897f2005-04-29 22:46:40 +0100892 unsigned int baud_raw;
893 unsigned int ucfr_rfdiv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100895 ucr2 = readl(sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 *parity = 'n';
898 if (ucr2 & UCR2_PREN) {
899 if (ucr2 & UCR2_PROE)
900 *parity = 'o';
901 else
902 *parity = 'e';
903 }
904
905 if (ucr2 & UCR2_WS)
906 *bits = 8;
907 else
908 *bits = 7;
909
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100910 ubir = readl(sport->port.membase + UBIR) & 0xffff;
911 ubmr = readl(sport->port.membase + UBMR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100913 ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
Sascha Hauer587897f2005-04-29 22:46:40 +0100914 if (ucfr_rfdiv == 6)
915 ucfr_rfdiv = 7;
916 else
917 ucfr_rfdiv = 6 - ucfr_rfdiv;
918
919 uartclk = imx_get_perclk1();
920 uartclk /= ucfr_rfdiv;
921
922 { /*
923 * The next code provides exact computation of
924 * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
925 * without need of float support or long long division,
926 * which would be required to prevent 32bit arithmetic overflow
927 */
928 unsigned int mul = ubir + 1;
929 unsigned int div = 16 * (ubmr + 1);
930 unsigned int rem = uartclk % div;
931
932 baud_raw = (uartclk / div) * mul;
933 baud_raw += (rem * mul + div / 2) / div;
934 *baud = (baud_raw + 50) / 100 * 100;
935 }
936
937 if(*baud != baud_raw)
938 printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
939 baud_raw, *baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941}
942
943static int __init
944imx_console_setup(struct console *co, char *options)
945{
946 struct imx_port *sport;
947 int baud = 9600;
948 int bits = 8;
949 int parity = 'n';
950 int flow = 'n';
951
952 /*
953 * Check whether an invalid uart number has been specified, and
954 * if so, search for the first available port that does have
955 * console support.
956 */
957 if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
958 co->index = 0;
Sascha Hauerdbff4e92008-07-05 10:02:45 +0200959 sport = imx_ports[co->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 if (options)
962 uart_parse_options(options, &baud, &parity, &bits, &flow);
963 else
964 imx_console_get_options(sport, &baud, &parity, &bits);
965
Sascha Hauer587897f2005-04-29 22:46:40 +0100966 imx_setup_ufcr(sport, 0);
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
969}
970
Vincent Sanders9f4426d2005-10-01 22:56:34 +0100971static struct uart_driver imx_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972static struct console imx_console = {
973 .name = "ttySMX",
974 .write = imx_console_write,
975 .device = uart_console_device,
976 .setup = imx_console_setup,
977 .flags = CON_PRINTBUFFER,
978 .index = -1,
979 .data = &imx_reg,
980};
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982#define IMX_CONSOLE &imx_console
983#else
984#define IMX_CONSOLE NULL
985#endif
986
987static struct uart_driver imx_reg = {
988 .owner = THIS_MODULE,
989 .driver_name = DRIVER_NAME,
990 .dev_name = "ttySMX",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 .major = SERIAL_IMX_MAJOR,
992 .minor = MINOR_START,
993 .nr = ARRAY_SIZE(imx_ports),
994 .cons = IMX_CONSOLE,
995};
996
Russell King3ae5eae2005-11-09 22:32:44 +0000997static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Russell King3ae5eae2005-11-09 22:32:44 +0000999 struct imx_port *sport = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Russell King9480e302005-10-28 09:52:56 -07001001 if (sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 uart_suspend_port(&imx_reg, &sport->port);
1003
1004 return 0;
1005}
1006
Russell King3ae5eae2005-11-09 22:32:44 +00001007static int serial_imx_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008{
Russell King3ae5eae2005-11-09 22:32:44 +00001009 struct imx_port *sport = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Russell King9480e302005-10-28 09:52:56 -07001011 if (sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 uart_resume_port(&imx_reg, &sport->port);
1013
1014 return 0;
1015}
1016
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001017static int serial_imx_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001019 struct imx_port *sport;
Sascha Hauer5b802342006-05-04 14:07:42 +01001020 struct imxuart_platform_data *pdata;
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001021 void __iomem *base;
1022 int ret = 0;
1023 struct resource *res;
Sascha Hauer5b802342006-05-04 14:07:42 +01001024
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001025 sport = kzalloc(sizeof(*sport), GFP_KERNEL);
1026 if (!sport)
1027 return -ENOMEM;
1028
1029 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1030 if (!res) {
1031 ret = -ENODEV;
1032 goto free;
1033 }
1034
1035 base = ioremap(res->start, PAGE_SIZE);
1036 if (!base) {
1037 ret = -ENOMEM;
1038 goto free;
1039 }
1040
1041 sport->port.dev = &pdev->dev;
1042 sport->port.mapbase = res->start;
1043 sport->port.membase = base;
1044 sport->port.type = PORT_IMX,
1045 sport->port.iotype = UPIO_MEM;
1046 sport->port.irq = platform_get_irq(pdev, 0);
1047 sport->rxirq = platform_get_irq(pdev, 0);
1048 sport->txirq = platform_get_irq(pdev, 1);
1049 sport->rtsirq = platform_get_irq(pdev, 2);
1050 sport->port.fifosize = 32;
1051 sport->port.ops = &imx_pops;
1052 sport->port.flags = UPF_BOOT_AUTOCONF;
1053 sport->port.line = pdev->id;
1054 init_timer(&sport->timer);
1055 sport->timer.function = imx_timeout;
1056 sport->timer.data = (unsigned long)sport;
1057 sport->port.uartclk = imx_get_perclk1();
1058
1059 imx_ports[pdev->id] = sport;
Sascha Hauer5b802342006-05-04 14:07:42 +01001060
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001061 pdata = pdev->dev.platform_data;
Sascha Hauer5b802342006-05-04 14:07:42 +01001062 if(pdata && (pdata->flags & IMXUART_HAVE_RTSCTS))
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001063 sport->have_rtscts = 1;
Sascha Hauer5b802342006-05-04 14:07:42 +01001064
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001065 if (pdata->init)
1066 pdata->init(pdev);
1067
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001068 uart_add_one_port(&imx_reg, &sport->port);
1069 platform_set_drvdata(pdev, &sport->port);
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 return 0;
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001072free:
1073 kfree(sport);
1074
1075 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001078static int serial_imx_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001080 struct imxuart_platform_data *pdata;
1081 struct imx_port *sport = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001083 pdata = pdev->dev.platform_data;
1084
1085 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 if (sport)
1088 uart_remove_one_port(&imx_reg, &sport->port);
1089
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001090 if (pdata->exit)
1091 pdata->exit(pdev);
1092
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001093 iounmap(sport->port.membase);
1094 kfree(sport);
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return 0;
1097}
1098
Russell King3ae5eae2005-11-09 22:32:44 +00001099static struct platform_driver serial_imx_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 .probe = serial_imx_probe,
1101 .remove = serial_imx_remove,
1102
1103 .suspend = serial_imx_suspend,
1104 .resume = serial_imx_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001105 .driver = {
1106 .name = "imx-uart",
Kay Sieverse169c132008-04-15 14:34:35 -07001107 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001108 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109};
1110
1111static int __init imx_serial_init(void)
1112{
1113 int ret;
1114
1115 printk(KERN_INFO "Serial: IMX driver\n");
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 ret = uart_register_driver(&imx_reg);
1118 if (ret)
1119 return ret;
1120
Russell King3ae5eae2005-11-09 22:32:44 +00001121 ret = platform_driver_register(&serial_imx_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (ret != 0)
1123 uart_unregister_driver(&imx_reg);
1124
1125 return 0;
1126}
1127
1128static void __exit imx_serial_exit(void)
1129{
Russell Kingc889b892005-11-21 17:05:21 +00001130 platform_driver_unregister(&serial_imx_driver);
Sascha Hauer4b300c32007-07-17 13:35:46 +01001131 uart_unregister_driver(&imx_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132}
1133
1134module_init(imx_serial_init);
1135module_exit(imx_serial_exit);
1136
1137MODULE_AUTHOR("Sascha Hauer");
1138MODULE_DESCRIPTION("IMX generic serial port driver");
1139MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001140MODULE_ALIAS("platform:imx-uart");