blob: 9e2162ebf1bb8f1e193dcf7c23ec1957eddf4abf [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>
47#include <asm/hardware.h>
Sascha Hauer5b802342006-05-04 14:07:42 +010048#include <asm/arch/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 */
65#define BIPR1 0xb0 /* Incremental Preset Register 1 */
66#define BIPR2 0xb4 /* Incremental Preset Register 2 */
67#define BIPR3 0xb8 /* Incremental Preset Register 3 */
68#define BIPR4 0xbc /* Incremental Preset Register 4 */
69#define BMPR1 0xc0 /* BRM Modulator Register 1 */
70#define BMPR2 0xc4 /* BRM Modulator Register 2 */
71#define BMPR3 0xc8 /* BRM Modulator Register 3 */
72#define BMPR4 0xcc /* BRM Modulator Register 4 */
73#define UTS 0xd0 /* UART Test Register */
74
75/* UART Control Register Bit Fields.*/
76#define URXD_CHARRDY (1<<15)
77#define URXD_ERR (1<<14)
78#define URXD_OVRRUN (1<<13)
79#define URXD_FRMERR (1<<12)
80#define URXD_BRK (1<<11)
81#define URXD_PRERR (1<<10)
82#define UCR1_ADEN (1<<15) /* Auto dectect interrupt */
83#define UCR1_ADBR (1<<14) /* Auto detect baud rate */
84#define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
85#define UCR1_IDEN (1<<12) /* Idle condition interrupt */
86#define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
87#define UCR1_RDMAEN (1<<8) /* Recv ready DMA enable */
88#define UCR1_IREN (1<<7) /* Infrared interface enable */
89#define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
90#define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
91#define UCR1_SNDBRK (1<<4) /* Send break */
92#define UCR1_TDMAEN (1<<3) /* Transmitter ready DMA enable */
93#define UCR1_UARTCLKEN (1<<2) /* UART clock enabled */
94#define UCR1_DOZE (1<<1) /* Doze */
95#define UCR1_UARTEN (1<<0) /* UART enabled */
96#define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
97#define UCR2_IRTS (1<<14) /* Ignore RTS pin */
98#define UCR2_CTSC (1<<13) /* CTS pin control */
99#define UCR2_CTS (1<<12) /* Clear to send */
100#define UCR2_ESCEN (1<<11) /* Escape enable */
101#define UCR2_PREN (1<<8) /* Parity enable */
102#define UCR2_PROE (1<<7) /* Parity odd/even */
103#define UCR2_STPB (1<<6) /* Stop */
104#define UCR2_WS (1<<5) /* Word size */
105#define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
106#define UCR2_TXEN (1<<2) /* Transmitter enabled */
107#define UCR2_RXEN (1<<1) /* Receiver enabled */
108#define UCR2_SRST (1<<0) /* SW reset */
109#define UCR3_DTREN (1<<13) /* DTR interrupt enable */
110#define UCR3_PARERREN (1<<12) /* Parity enable */
111#define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
112#define UCR3_DSR (1<<10) /* Data set ready */
113#define UCR3_DCD (1<<9) /* Data carrier detect */
114#define UCR3_RI (1<<8) /* Ring indicator */
115#define UCR3_TIMEOUTEN (1<<7) /* Timeout interrupt enable */
116#define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
117#define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
118#define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
119#define UCR3_REF25 (1<<3) /* Ref freq 25 MHz */
120#define UCR3_REF30 (1<<2) /* Ref Freq 30 MHz */
121#define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
122#define UCR3_BPEN (1<<0) /* Preset registers enable */
123#define UCR4_CTSTL_32 (32<<10) /* CTS trigger level (32 chars) */
124#define UCR4_INVR (1<<9) /* Inverted infrared reception */
125#define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
126#define UCR4_WKEN (1<<7) /* Wake interrupt enable */
127#define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
128#define UCR4_IRSC (1<<5) /* IR special case */
129#define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
130#define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
131#define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
132#define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
133#define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
134#define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
135#define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
136#define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
137#define USR1_RTSS (1<<14) /* RTS pin status */
138#define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
139#define USR1_RTSD (1<<12) /* RTS delta */
140#define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
141#define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
142#define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
143#define USR1_TIMEOUT (1<<7) /* Receive timeout interrupt status */
144#define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
145#define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
146#define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
147#define USR2_ADET (1<<15) /* Auto baud rate detect complete */
148#define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
149#define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
150#define USR2_IDLE (1<<12) /* Idle condition */
151#define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
152#define USR2_WAKE (1<<7) /* Wake */
153#define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
154#define USR2_TXDC (1<<3) /* Transmitter complete */
155#define USR2_BRCD (1<<2) /* Break condition */
156#define USR2_ORE (1<<1) /* Overrun error */
157#define USR2_RDR (1<<0) /* Recv data ready */
158#define UTS_FRCPERR (1<<13) /* Force parity error */
159#define UTS_LOOP (1<<12) /* Loop tx and rx */
160#define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
161#define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
162#define UTS_TXFULL (1<<4) /* TxFIFO full */
163#define UTS_RXFULL (1<<3) /* RxFIFO full */
164#define UTS_SOFTRST (1<<0) /* Software reset */
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/* We've been assigned a range on the "Low-density serial ports" major */
167#define SERIAL_IMX_MAJOR 204
168#define MINOR_START 41
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * This determines how often we check the modem status signals
172 * for any change. They generally aren't connected to an IRQ
173 * so we have to poll them. We also check immediately before
174 * filling the TX fifo incase CTS has been dropped.
175 */
176#define MCTRL_TIMEOUT (250*HZ/1000)
177
178#define DRIVER_NAME "IMX-uart"
179
Sascha Hauerdbff4e92008-07-05 10:02:45 +0200180#define UART_NR 8
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182struct imx_port {
183 struct uart_port port;
184 struct timer_list timer;
185 unsigned int old_status;
Sascha Hauer5b802342006-05-04 14:07:42 +0100186 int txirq,rxirq,rtsirq;
187 int have_rtscts:1;
Sascha Hauer38a41fd2008-07-05 10:02:46 +0200188 struct clk *clk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189};
190
191/*
192 * Handle any change of modem status signal since we were last called.
193 */
194static void imx_mctrl_check(struct imx_port *sport)
195{
196 unsigned int status, changed;
197
198 status = sport->port.ops->get_mctrl(&sport->port);
199 changed = status ^ sport->old_status;
200
201 if (changed == 0)
202 return;
203
204 sport->old_status = status;
205
206 if (changed & TIOCM_RI)
207 sport->port.icount.rng++;
208 if (changed & TIOCM_DSR)
209 sport->port.icount.dsr++;
210 if (changed & TIOCM_CAR)
211 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
212 if (changed & TIOCM_CTS)
213 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
214
215 wake_up_interruptible(&sport->port.info->delta_msr_wait);
216}
217
218/*
219 * This is our per-port timeout handler, for checking the
220 * modem status signals.
221 */
222static void imx_timeout(unsigned long data)
223{
224 struct imx_port *sport = (struct imx_port *)data;
225 unsigned long flags;
226
227 if (sport->port.info) {
228 spin_lock_irqsave(&sport->port.lock, flags);
229 imx_mctrl_check(sport);
230 spin_unlock_irqrestore(&sport->port.lock, flags);
231
232 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
233 }
234}
235
236/*
237 * interrupts disabled on entry
238 */
Russell Kingb129a8c2005-08-31 10:12:14 +0100239static void imx_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100242 unsigned long temp;
243
244 temp = readl(sport->port.membase + UCR1);
245 writel(temp & ~UCR1_TXMPTYEN, sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248/*
249 * interrupts disabled on entry
250 */
251static void imx_stop_rx(struct uart_port *port)
252{
253 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100254 unsigned long temp;
255
256 temp = readl(sport->port.membase + UCR2);
257 writel(temp &~ UCR2_RXEN, sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260/*
261 * Set the modem control timer to fire immediately.
262 */
263static void imx_enable_ms(struct uart_port *port)
264{
265 struct imx_port *sport = (struct imx_port *)port;
266
267 mod_timer(&sport->timer, jiffies);
268}
269
270static inline void imx_transmit_buffer(struct imx_port *sport)
271{
272 struct circ_buf *xmit = &sport->port.info->xmit;
273
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100274 while (!(readl(sport->port.membase + UTS) & UTS_TXFULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* send xmit->buf[xmit->tail]
276 * out the port here */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100277 writel(xmit->buf[xmit->tail], sport->port.membase + URTX0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 xmit->tail = (xmit->tail + 1) &
279 (UART_XMIT_SIZE - 1);
280 sport->port.icount.tx++;
281 if (uart_circ_empty(xmit))
282 break;
Sascha Hauer8c0b2542007-02-05 16:10:16 -0800283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100286 imx_stop_tx(&sport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289/*
290 * interrupts disabled on entry
291 */
Russell Kingb129a8c2005-08-31 10:12:14 +0100292static void imx_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100295 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100297 temp = readl(sport->port.membase + UCR1);
298 writel(temp | UCR1_TXMPTYEN, sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100300 if (readl(sport->port.membase + UTS) & UTS_TXEMPTY)
301 imx_transmit_buffer(sport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
David Howells7d12e782006-10-05 14:55:46 +0100304static irqreturn_t imx_rtsint(int irq, void *dev_id)
Sascha Hauerceca6292005-10-12 19:58:08 +0100305{
Jeff Garzik15aafa22008-02-06 01:36:20 -0800306 struct imx_port *sport = dev_id;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100307 unsigned int val = readl(sport->port.membase + USR1) & USR1_RTSS;
Sascha Hauerceca6292005-10-12 19:58:08 +0100308 unsigned long flags;
309
310 spin_lock_irqsave(&sport->port.lock, flags);
311
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100312 writel(USR1_RTSD, sport->port.membase + USR1);
Sascha Hauerceca6292005-10-12 19:58:08 +0100313 uart_handle_cts_change(&sport->port, !!val);
314 wake_up_interruptible(&sport->port.info->delta_msr_wait);
315
316 spin_unlock_irqrestore(&sport->port.lock, flags);
317 return IRQ_HANDLED;
318}
319
David Howells7d12e782006-10-05 14:55:46 +0100320static irqreturn_t imx_txint(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Jeff Garzik15aafa22008-02-06 01:36:20 -0800322 struct imx_port *sport = dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 struct circ_buf *xmit = &sport->port.info->xmit;
324 unsigned long flags;
325
326 spin_lock_irqsave(&sport->port.lock,flags);
327 if (sport->port.x_char)
328 {
329 /* Send next char */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100330 writel(sport->port.x_char, sport->port.membase + URTX0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 goto out;
332 }
333
334 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100335 imx_stop_tx(&sport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 goto out;
337 }
338
339 imx_transmit_buffer(sport);
340
341 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
342 uart_write_wakeup(&sport->port);
343
344out:
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_rxint(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 struct imx_port *sport = dev_id;
352 unsigned int rx,flg,ignored = 0;
353 struct tty_struct *tty = sport->port.info->tty;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100354 unsigned long flags, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 spin_lock_irqsave(&sport->port.lock,flags);
357
Sascha Hauer0d3c3932008-04-17 08:43:14 +0100358 while (readl(sport->port.membase + USR2) & USR2_RDR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 flg = TTY_NORMAL;
360 sport->port.icount.rx++;
361
Sascha Hauer0d3c3932008-04-17 08:43:14 +0100362 rx = readl(sport->port.membase + URXD0);
363
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100364 temp = readl(sport->port.membase + USR2);
Sascha Hauer864eeed2008-04-17 08:39:22 +0100365 if (temp & USR2_BRCD) {
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100366 writel(temp | USR2_BRCD, sport->port.membase + USR2);
Sascha Hauer864eeed2008-04-17 08:39:22 +0100367 if (uart_handle_break(&sport->port))
368 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
370
371 if (uart_handle_sysrq_char
David Howells7d12e782006-10-05 14:55:46 +0100372 (&sport->port, (unsigned char)rx))
Sascha Hauer864eeed2008-04-17 08:39:22 +0100373 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Sascha Hauer864eeed2008-04-17 08:39:22 +0100375 if (rx & (URXD_PRERR | URXD_OVRRUN | URXD_FRMERR) ) {
376 if (rx & URXD_PRERR)
377 sport->port.icount.parity++;
378 else if (rx & URXD_FRMERR)
379 sport->port.icount.frame++;
380 if (rx & URXD_OVRRUN)
381 sport->port.icount.overrun++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
Sascha Hauer864eeed2008-04-17 08:39:22 +0100383 if (rx & sport->port.ignore_status_mask) {
384 if (++ignored > 100)
385 goto out;
386 continue;
387 }
388
389 rx &= sport->port.read_status_mask;
390
391 if (rx & URXD_PRERR)
392 flg = TTY_PARITY;
393 else if (rx & URXD_FRMERR)
394 flg = TTY_FRAME;
395 if (rx & URXD_OVRRUN)
396 flg = TTY_OVERRUN;
397
398#ifdef SUPPORT_SYSRQ
399 sport->port.sysrq = 0;
400#endif
401 }
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 tty_insert_flip_char(tty, rx, flg);
Sascha Hauer864eeed2008-04-17 08:39:22 +0100404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406out:
407 spin_unlock_irqrestore(&sport->port.lock,flags);
408 tty_flip_buffer_push(tty);
409 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
412/*
413 * Return TIOCSER_TEMT when transmitter is not busy.
414 */
415static unsigned int imx_tx_empty(struct uart_port *port)
416{
417 struct imx_port *sport = (struct imx_port *)port;
418
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100419 return (readl(sport->port.membase + USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100422/*
423 * We have a modem side uart, so the meanings of RTS and CTS are inverted.
424 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425static unsigned int imx_get_mctrl(struct uart_port *port)
426{
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100427 struct imx_port *sport = (struct imx_port *)port;
428 unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
429
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100430 if (readl(sport->port.membase + USR1) & USR1_RTSS)
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100431 tmp |= TIOCM_CTS;
432
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100433 if (readl(sport->port.membase + UCR2) & UCR2_CTS)
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100434 tmp |= TIOCM_RTS;
435
436 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
440{
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100441 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100442 unsigned long temp;
443
444 temp = readl(sport->port.membase + UCR2) & ~UCR2_CTS;
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100445
446 if (mctrl & TIOCM_RTS)
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100447 temp |= UCR2_CTS;
448
449 writel(temp, sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452/*
453 * Interrupts always disabled.
454 */
455static void imx_break_ctl(struct uart_port *port, int break_state)
456{
457 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100458 unsigned long flags, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460 spin_lock_irqsave(&sport->port.lock, flags);
461
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100462 temp = readl(sport->port.membase + UCR1) & ~UCR1_SNDBRK;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if ( break_state != 0 )
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100465 temp |= UCR1_SNDBRK;
466
467 writel(temp, sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
469 spin_unlock_irqrestore(&sport->port.lock, flags);
470}
471
472#define TXTL 2 /* reset default */
473#define RXTL 1 /* reset default */
474
Sascha Hauer587897f2005-04-29 22:46:40 +0100475static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
476{
477 unsigned int val;
478 unsigned int ufcr_rfdiv;
479
480 /* set receiver / transmitter trigger level.
481 * RFDIV is set such way to satisfy requested uartclk value
482 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100483 val = TXTL << 10 | RXTL;
Sascha Hauer38a41fd2008-07-05 10:02:46 +0200484 ufcr_rfdiv = (clk_get_rate(sport->clk) + sport->port.uartclk / 2)
485 / sport->port.uartclk;
Sascha Hauer587897f2005-04-29 22:46:40 +0100486
487 if(!ufcr_rfdiv)
488 ufcr_rfdiv = 1;
489
490 if(ufcr_rfdiv >= 7)
491 ufcr_rfdiv = 6;
492 else
493 ufcr_rfdiv = 6 - ufcr_rfdiv;
494
495 val |= UFCR_RFDIV & (ufcr_rfdiv << 7);
496
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100497 writel(val, sport->port.membase + UFCR);
Sascha Hauer587897f2005-04-29 22:46:40 +0100498
499 return 0;
500}
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502static int imx_startup(struct uart_port *port)
503{
504 struct imx_port *sport = (struct imx_port *)port;
505 int retval;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100506 unsigned long flags, temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Sascha Hauer587897f2005-04-29 22:46:40 +0100508 imx_setup_ufcr(sport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 /* disable the DREN bit (Data Ready interrupt enable) before
511 * requesting IRQs
512 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100513 temp = readl(sport->port.membase + UCR4);
514 writel(temp & ~UCR4_DREN, sport->port.membase + UCR4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /*
517 * Allocate the IRQ
518 */
519 retval = request_irq(sport->rxirq, imx_rxint, 0,
520 DRIVER_NAME, sport);
Sascha Hauer86371d02005-10-10 10:17:42 +0100521 if (retval) goto error_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522
523 retval = request_irq(sport->txirq, imx_txint, 0,
Sascha Hauerceca6292005-10-12 19:58:08 +0100524 DRIVER_NAME, sport);
Sascha Hauer86371d02005-10-10 10:17:42 +0100525 if (retval) goto error_out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Russell Kingf43aaba2006-01-19 12:26:57 +0000527 retval = request_irq(sport->rtsirq, imx_rtsint,
Pavel Pisad7ea10d2007-02-05 16:10:20 -0800528 (sport->rtsirq < IMX_IRQS) ? 0 :
529 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
Sascha Hauerceca6292005-10-12 19:58:08 +0100530 DRIVER_NAME, sport);
531 if (retval) goto error_out3;
Sascha Hauerceca6292005-10-12 19:58:08 +0100532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /*
534 * Finally, clear and enable interrupts
535 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100536 writel(USR1_RTSD, sport->port.membase + USR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100538 temp = readl(sport->port.membase + UCR1);
Sascha Hauer789d5252008-04-17 08:44:47 +0100539 temp |= UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100540 writel(temp, sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100542 temp = readl(sport->port.membase + UCR2);
543 temp |= (UCR2_RXEN | UCR2_TXEN);
544 writel(temp, sport->port.membase + UCR2);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /*
547 * Enable modem status interrupts
548 */
549 spin_lock_irqsave(&sport->port.lock,flags);
550 imx_enable_ms(&sport->port);
551 spin_unlock_irqrestore(&sport->port.lock,flags);
552
553 return 0;
554
Sascha Hauerceca6292005-10-12 19:58:08 +0100555error_out3:
556 free_irq(sport->txirq, sport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557error_out2:
Sascha Hauer86371d02005-10-10 10:17:42 +0100558 free_irq(sport->rxirq, sport);
559error_out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return retval;
561}
562
563static void imx_shutdown(struct uart_port *port)
564{
565 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100566 unsigned long temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /*
569 * Stop our timer.
570 */
571 del_timer_sync(&sport->timer);
572
573 /*
574 * Free the interrupts
575 */
Sascha Hauerceca6292005-10-12 19:58:08 +0100576 free_irq(sport->rtsirq, sport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 free_irq(sport->txirq, sport);
578 free_irq(sport->rxirq, sport);
579
580 /*
581 * Disable all interrupts, port and break condition.
582 */
583
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100584 temp = readl(sport->port.membase + UCR1);
585 temp &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
586 writel(temp, sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
589static void
Alan Cox606d0992006-12-08 02:38:45 -0800590imx_set_termios(struct uart_port *port, struct ktermios *termios,
591 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593 struct imx_port *sport = (struct imx_port *)port;
594 unsigned long flags;
595 unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
596 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
Sascha Hauer036bb152008-07-05 10:02:44 +0200597 unsigned int div, num, denom, ufcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 /*
600 * If we don't support modem control lines, don't allow
601 * these to be set.
602 */
603 if (0) {
604 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
605 termios->c_cflag |= CLOCAL;
606 }
607
608 /*
609 * We only support CS7 and CS8.
610 */
611 while ((termios->c_cflag & CSIZE) != CS7 &&
612 (termios->c_cflag & CSIZE) != CS8) {
613 termios->c_cflag &= ~CSIZE;
614 termios->c_cflag |= old_csize;
615 old_csize = CS8;
616 }
617
618 if ((termios->c_cflag & CSIZE) == CS8)
619 ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
620 else
621 ucr2 = UCR2_SRST | UCR2_IRTS;
622
623 if (termios->c_cflag & CRTSCTS) {
Sascha Hauer5b802342006-05-04 14:07:42 +0100624 if( sport->have_rtscts ) {
625 ucr2 &= ~UCR2_IRTS;
626 ucr2 |= UCR2_CTSC;
627 } else {
628 termios->c_cflag &= ~CRTSCTS;
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 }
631
632 if (termios->c_cflag & CSTOPB)
633 ucr2 |= UCR2_STPB;
634 if (termios->c_cflag & PARENB) {
635 ucr2 |= UCR2_PREN;
Matt Reimer3261e362006-01-13 20:51:44 +0000636 if (termios->c_cflag & PARODD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 ucr2 |= UCR2_PROE;
638 }
639
640 /*
641 * Ask the core to calculate the divisor for us.
642 */
Sascha Hauer036bb152008-07-05 10:02:44 +0200643 baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 quot = uart_get_divisor(port, baud);
645
646 spin_lock_irqsave(&sport->port.lock, flags);
647
648 sport->port.read_status_mask = 0;
649 if (termios->c_iflag & INPCK)
650 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
651 if (termios->c_iflag & (BRKINT | PARMRK))
652 sport->port.read_status_mask |= URXD_BRK;
653
654 /*
655 * Characters to ignore
656 */
657 sport->port.ignore_status_mask = 0;
658 if (termios->c_iflag & IGNPAR)
659 sport->port.ignore_status_mask |= URXD_PRERR;
660 if (termios->c_iflag & IGNBRK) {
661 sport->port.ignore_status_mask |= URXD_BRK;
662 /*
663 * If we're ignoring parity and break indicators,
664 * ignore overruns too (for real raw support).
665 */
666 if (termios->c_iflag & IGNPAR)
667 sport->port.ignore_status_mask |= URXD_OVRRUN;
668 }
669
670 del_timer_sync(&sport->timer);
671
672 /*
673 * Update the per-port timeout.
674 */
675 uart_update_timeout(port, termios->c_cflag, baud);
676
677 /*
678 * disable interrupts and drain transmitter
679 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100680 old_ucr1 = readl(sport->port.membase + UCR1);
681 writel(old_ucr1 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
682 sport->port.membase + UCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100684 while ( !(readl(sport->port.membase + USR2) & USR2_TXDC))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 barrier();
686
687 /* then, disable everything */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100688 old_txrxen = readl(sport->port.membase + UCR2);
689 writel(old_txrxen & ~( UCR2_TXEN | UCR2_RXEN),
690 sport->port.membase + UCR2);
691 old_txrxen &= (UCR2_TXEN | UCR2_RXEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Sascha Hauer036bb152008-07-05 10:02:44 +0200693 div = sport->port.uartclk / (baud * 16);
694 if (div > 7)
695 div = 7;
696 if (!div)
697 div = 1;
698
699 num = baud;
700 denom = port->uartclk / div / 16;
701
702 /* shift num and denom right until they fit into 16 bits */
703 while (num > 0x10000 || denom > 0x10000) {
704 num >>= 1;
705 denom >>= 1;
706 }
707 if (num > 0)
708 num -= 1;
709 if (denom > 0)
710 denom -= 1;
711
712 writel(num, sport->port.membase + UBIR);
713 writel(denom, sport->port.membase + UBMR);
714
715 if (div == 7)
716 div = 6; /* 6 in RFDIV means divide by 7 */
717 else
718 div = 6 - div;
719
720 ufcr = readl(sport->port.membase + UFCR);
721 ufcr = (ufcr & (~UFCR_RFDIV)) |
722 (div << 7);
723 writel(ufcr, sport->port.membase + UFCR);
724
725#ifdef ONEMS
726 writel(sport->port.uartclk / div / 1000, sport->port.membase + ONEMS);
727#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100729 writel(old_ucr1, sport->port.membase + UCR1);
730
731 /* set the parity, stop bits and data size */
732 writel(ucr2 | old_txrxen, sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
735 imx_enable_ms(&sport->port);
736
737 spin_unlock_irqrestore(&sport->port.lock, flags);
738}
739
740static const char *imx_type(struct uart_port *port)
741{
742 struct imx_port *sport = (struct imx_port *)port;
743
744 return sport->port.type == PORT_IMX ? "IMX" : NULL;
745}
746
747/*
748 * Release the memory region(s) being used by 'port'.
749 */
750static void imx_release_port(struct uart_port *port)
751{
Sascha Hauer3d454442008-04-17 08:47:32 +0100752 struct platform_device *pdev = to_platform_device(port->dev);
753 struct resource *mmres;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Sascha Hauer3d454442008-04-17 08:47:32 +0100755 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
756 release_mem_region(mmres->start, mmres->end - mmres->start + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759/*
760 * Request the memory region(s) being used by 'port'.
761 */
762static int imx_request_port(struct uart_port *port)
763{
Sascha Hauer3d454442008-04-17 08:47:32 +0100764 struct platform_device *pdev = to_platform_device(port->dev);
765 struct resource *mmres;
766 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Sascha Hauer3d454442008-04-17 08:47:32 +0100768 mmres = platform_get_resource(pdev, IORESOURCE_MEM, 0);
769 if (!mmres)
770 return -ENODEV;
771
772 ret = request_mem_region(mmres->start, mmres->end - mmres->start + 1,
773 "imx-uart");
774
775 return ret ? 0 : -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776}
777
778/*
779 * Configure/autoconfigure the port.
780 */
781static void imx_config_port(struct uart_port *port, int flags)
782{
783 struct imx_port *sport = (struct imx_port *)port;
784
785 if (flags & UART_CONFIG_TYPE &&
786 imx_request_port(&sport->port) == 0)
787 sport->port.type = PORT_IMX;
788}
789
790/*
791 * Verify the new serial_struct (for TIOCSSERIAL).
792 * The only change we allow are to the flags and type, and
793 * even then only between PORT_IMX and PORT_UNKNOWN
794 */
795static int
796imx_verify_port(struct uart_port *port, struct serial_struct *ser)
797{
798 struct imx_port *sport = (struct imx_port *)port;
799 int ret = 0;
800
801 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
802 ret = -EINVAL;
803 if (sport->port.irq != ser->irq)
804 ret = -EINVAL;
805 if (ser->io_type != UPIO_MEM)
806 ret = -EINVAL;
807 if (sport->port.uartclk / 16 != ser->baud_base)
808 ret = -EINVAL;
809 if ((void *)sport->port.mapbase != ser->iomem_base)
810 ret = -EINVAL;
811 if (sport->port.iobase != ser->port)
812 ret = -EINVAL;
813 if (ser->hub6 != 0)
814 ret = -EINVAL;
815 return ret;
816}
817
818static struct uart_ops imx_pops = {
819 .tx_empty = imx_tx_empty,
820 .set_mctrl = imx_set_mctrl,
821 .get_mctrl = imx_get_mctrl,
822 .stop_tx = imx_stop_tx,
823 .start_tx = imx_start_tx,
824 .stop_rx = imx_stop_rx,
825 .enable_ms = imx_enable_ms,
826 .break_ctl = imx_break_ctl,
827 .startup = imx_startup,
828 .shutdown = imx_shutdown,
829 .set_termios = imx_set_termios,
830 .type = imx_type,
831 .release_port = imx_release_port,
832 .request_port = imx_request_port,
833 .config_port = imx_config_port,
834 .verify_port = imx_verify_port,
835};
836
Sascha Hauerdbff4e92008-07-05 10:02:45 +0200837static struct imx_port *imx_ports[UART_NR];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839#ifdef CONFIG_SERIAL_IMX_CONSOLE
Russell Kingd3587882006-03-20 20:00:09 +0000840static void imx_console_putchar(struct uart_port *port, int ch)
841{
842 struct imx_port *sport = (struct imx_port *)port;
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100843
844 while (readl(sport->port.membase + UTS) & UTS_TXFULL)
Russell Kingd3587882006-03-20 20:00:09 +0000845 barrier();
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100846
847 writel(ch, sport->port.membase + URTX0);
Russell Kingd3587882006-03-20 20:00:09 +0000848}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
850/*
851 * Interrupts are disabled on entering
852 */
853static void
854imx_console_write(struct console *co, const char *s, unsigned int count)
855{
Sascha Hauerdbff4e92008-07-05 10:02:45 +0200856 struct imx_port *sport = imx_ports[co->index];
Russell Kingd3587882006-03-20 20:00:09 +0000857 unsigned int old_ucr1, old_ucr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 /*
860 * First, save UCR1/2 and then disable interrupts
861 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100862 old_ucr1 = readl(sport->port.membase + UCR1);
863 old_ucr2 = readl(sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100865 writel((old_ucr1 | UCR1_UARTCLKEN | UCR1_UARTEN) &
866 ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN),
867 sport->port.membase + UCR1);
868
869 writel(old_ucr2 | UCR2_TXEN, sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Russell Kingd3587882006-03-20 20:00:09 +0000871 uart_console_write(&sport->port, s, count, imx_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 /*
874 * Finally, wait for transmitter to become empty
875 * and restore UCR1/2
876 */
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100877 while (!(readl(sport->port.membase + USR2) & USR2_TXDC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100879 writel(old_ucr1, sport->port.membase + UCR1);
880 writel(old_ucr2, sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
883/*
884 * If the port was already initialised (eg, by a boot loader),
885 * try to determine the current setup.
886 */
887static void __init
888imx_console_get_options(struct imx_port *sport, int *baud,
889 int *parity, int *bits)
890{
Sascha Hauer587897f2005-04-29 22:46:40 +0100891
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100892 if ( readl(sport->port.membase + UCR1) | UCR1_UARTEN ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 /* ok, the port was enabled */
894 unsigned int ucr2, ubir,ubmr, uartclk;
Sascha Hauer587897f2005-04-29 22:46:40 +0100895 unsigned int baud_raw;
896 unsigned int ucfr_rfdiv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100898 ucr2 = readl(sport->port.membase + UCR2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 *parity = 'n';
901 if (ucr2 & UCR2_PREN) {
902 if (ucr2 & UCR2_PROE)
903 *parity = 'o';
904 else
905 *parity = 'e';
906 }
907
908 if (ucr2 & UCR2_WS)
909 *bits = 8;
910 else
911 *bits = 7;
912
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100913 ubir = readl(sport->port.membase + UBIR) & 0xffff;
914 ubmr = readl(sport->port.membase + UBMR) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Sascha Hauerff4bfb22007-04-26 08:26:13 +0100916 ucfr_rfdiv = (readl(sport->port.membase + UFCR) & UFCR_RFDIV) >> 7;
Sascha Hauer587897f2005-04-29 22:46:40 +0100917 if (ucfr_rfdiv == 6)
918 ucfr_rfdiv = 7;
919 else
920 ucfr_rfdiv = 6 - ucfr_rfdiv;
921
Sascha Hauer38a41fd2008-07-05 10:02:46 +0200922 uartclk = clk_get_rate(sport->clk);
Sascha Hauer587897f2005-04-29 22:46:40 +0100923 uartclk /= ucfr_rfdiv;
924
925 { /*
926 * The next code provides exact computation of
927 * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
928 * without need of float support or long long division,
929 * which would be required to prevent 32bit arithmetic overflow
930 */
931 unsigned int mul = ubir + 1;
932 unsigned int div = 16 * (ubmr + 1);
933 unsigned int rem = uartclk % div;
934
935 baud_raw = (uartclk / div) * mul;
936 baud_raw += (rem * mul + div / 2) / div;
937 *baud = (baud_raw + 50) / 100 * 100;
938 }
939
940 if(*baud != baud_raw)
941 printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
942 baud_raw, *baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944}
945
946static int __init
947imx_console_setup(struct console *co, char *options)
948{
949 struct imx_port *sport;
950 int baud = 9600;
951 int bits = 8;
952 int parity = 'n';
953 int flow = 'n';
954
955 /*
956 * Check whether an invalid uart number has been specified, and
957 * if so, search for the first available port that does have
958 * console support.
959 */
960 if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
961 co->index = 0;
Sascha Hauerdbff4e92008-07-05 10:02:45 +0200962 sport = imx_ports[co->index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 if (options)
965 uart_parse_options(options, &baud, &parity, &bits, &flow);
966 else
967 imx_console_get_options(sport, &baud, &parity, &bits);
968
Sascha Hauer587897f2005-04-29 22:46:40 +0100969 imx_setup_ufcr(sport, 0);
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
972}
973
Vincent Sanders9f4426d2005-10-01 22:56:34 +0100974static struct uart_driver imx_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975static struct console imx_console = {
976 .name = "ttySMX",
977 .write = imx_console_write,
978 .device = uart_console_device,
979 .setup = imx_console_setup,
980 .flags = CON_PRINTBUFFER,
981 .index = -1,
982 .data = &imx_reg,
983};
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985#define IMX_CONSOLE &imx_console
986#else
987#define IMX_CONSOLE NULL
988#endif
989
990static struct uart_driver imx_reg = {
991 .owner = THIS_MODULE,
992 .driver_name = DRIVER_NAME,
993 .dev_name = "ttySMX",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 .major = SERIAL_IMX_MAJOR,
995 .minor = MINOR_START,
996 .nr = ARRAY_SIZE(imx_ports),
997 .cons = IMX_CONSOLE,
998};
999
Russell King3ae5eae2005-11-09 22:32:44 +00001000static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Russell King3ae5eae2005-11-09 22:32:44 +00001002 struct imx_port *sport = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Russell King9480e302005-10-28 09:52:56 -07001004 if (sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 uart_suspend_port(&imx_reg, &sport->port);
1006
1007 return 0;
1008}
1009
Russell King3ae5eae2005-11-09 22:32:44 +00001010static int serial_imx_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Russell King3ae5eae2005-11-09 22:32:44 +00001012 struct imx_port *sport = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Russell King9480e302005-10-28 09:52:56 -07001014 if (sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 uart_resume_port(&imx_reg, &sport->port);
1016
1017 return 0;
1018}
1019
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001020static int serial_imx_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021{
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001022 struct imx_port *sport;
Sascha Hauer5b802342006-05-04 14:07:42 +01001023 struct imxuart_platform_data *pdata;
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001024 void __iomem *base;
1025 int ret = 0;
1026 struct resource *res;
Sascha Hauer5b802342006-05-04 14:07:42 +01001027
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001028 sport = kzalloc(sizeof(*sport), GFP_KERNEL);
1029 if (!sport)
1030 return -ENOMEM;
1031
1032 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1033 if (!res) {
1034 ret = -ENODEV;
1035 goto free;
1036 }
1037
1038 base = ioremap(res->start, PAGE_SIZE);
1039 if (!base) {
1040 ret = -ENOMEM;
1041 goto free;
1042 }
1043
1044 sport->port.dev = &pdev->dev;
1045 sport->port.mapbase = res->start;
1046 sport->port.membase = base;
1047 sport->port.type = PORT_IMX,
1048 sport->port.iotype = UPIO_MEM;
1049 sport->port.irq = platform_get_irq(pdev, 0);
1050 sport->rxirq = platform_get_irq(pdev, 0);
1051 sport->txirq = platform_get_irq(pdev, 1);
1052 sport->rtsirq = platform_get_irq(pdev, 2);
1053 sport->port.fifosize = 32;
1054 sport->port.ops = &imx_pops;
1055 sport->port.flags = UPF_BOOT_AUTOCONF;
1056 sport->port.line = pdev->id;
1057 init_timer(&sport->timer);
1058 sport->timer.function = imx_timeout;
1059 sport->timer.data = (unsigned long)sport;
Sascha Hauer38a41fd2008-07-05 10:02:46 +02001060
1061 sport->clk = clk_get(&pdev->dev, "uart_clk");
1062 if (IS_ERR(sport->clk)) {
1063 ret = PTR_ERR(sport->clk);
1064 goto unmap;
1065 }
1066 clk_enable(sport->clk);
1067
1068 sport->port.uartclk = clk_get_rate(sport->clk);
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001069
1070 imx_ports[pdev->id] = sport;
Sascha Hauer5b802342006-05-04 14:07:42 +01001071
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001072 pdata = pdev->dev.platform_data;
Sascha Hauer5b802342006-05-04 14:07:42 +01001073 if(pdata && (pdata->flags & IMXUART_HAVE_RTSCTS))
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001074 sport->have_rtscts = 1;
Sascha Hauer5b802342006-05-04 14:07:42 +01001075
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001076 if (pdata->init)
1077 pdata->init(pdev);
1078
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001079 uart_add_one_port(&imx_reg, &sport->port);
1080 platform_set_drvdata(pdev, &sport->port);
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return 0;
Sascha Hauer38a41fd2008-07-05 10:02:46 +02001083unmap:
1084 iounmap(sport->port.membase);
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001085free:
1086 kfree(sport);
1087
1088 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001091static int serial_imx_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001093 struct imxuart_platform_data *pdata;
1094 struct imx_port *sport = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001096 pdata = pdev->dev.platform_data;
1097
1098 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Sascha Hauer38a41fd2008-07-05 10:02:46 +02001100 if (sport) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 uart_remove_one_port(&imx_reg, &sport->port);
Sascha Hauer38a41fd2008-07-05 10:02:46 +02001102 clk_put(sport->clk);
1103 }
1104
1105 clk_disable(sport->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Sascha Hauer2582d8c2008-07-05 10:02:45 +02001107 if (pdata->exit)
1108 pdata->exit(pdev);
1109
Sascha Hauerdbff4e92008-07-05 10:02:45 +02001110 iounmap(sport->port.membase);
1111 kfree(sport);
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 return 0;
1114}
1115
Russell King3ae5eae2005-11-09 22:32:44 +00001116static struct platform_driver serial_imx_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 .probe = serial_imx_probe,
1118 .remove = serial_imx_remove,
1119
1120 .suspend = serial_imx_suspend,
1121 .resume = serial_imx_resume,
Russell King3ae5eae2005-11-09 22:32:44 +00001122 .driver = {
1123 .name = "imx-uart",
Kay Sieverse169c132008-04-15 14:34:35 -07001124 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00001125 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126};
1127
1128static int __init imx_serial_init(void)
1129{
1130 int ret;
1131
1132 printk(KERN_INFO "Serial: IMX driver\n");
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 ret = uart_register_driver(&imx_reg);
1135 if (ret)
1136 return ret;
1137
Russell King3ae5eae2005-11-09 22:32:44 +00001138 ret = platform_driver_register(&serial_imx_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (ret != 0)
1140 uart_unregister_driver(&imx_reg);
1141
1142 return 0;
1143}
1144
1145static void __exit imx_serial_exit(void)
1146{
Russell Kingc889b892005-11-21 17:05:21 +00001147 platform_driver_unregister(&serial_imx_driver);
Sascha Hauer4b300c32007-07-17 13:35:46 +01001148 uart_unregister_driver(&imx_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
1151module_init(imx_serial_init);
1152module_exit(imx_serial_exit);
1153
1154MODULE_AUTHOR("Sascha Hauer");
1155MODULE_DESCRIPTION("IMX generic serial port driver");
1156MODULE_LICENSE("GPL");
Kay Sieverse169c132008-04-15 14:34:35 -07001157MODULE_ALIAS("platform:imx-uart");