blob: 04cc88cc528cec7eb742eee9e5e952e3662be502 [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
49/* We've been assigned a range on the "Low-density serial ports" major */
50#define SERIAL_IMX_MAJOR 204
51#define MINOR_START 41
52
53#define NR_PORTS 2
54
55#define IMX_ISR_PASS_LIMIT 256
56
57/*
58 * This is the size of our serial port register set.
59 */
60#define UART_PORT_SIZE 0x100
61
62/*
63 * This determines how often we check the modem status signals
64 * for any change. They generally aren't connected to an IRQ
65 * so we have to poll them. We also check immediately before
66 * filling the TX fifo incase CTS has been dropped.
67 */
68#define MCTRL_TIMEOUT (250*HZ/1000)
69
70#define DRIVER_NAME "IMX-uart"
71
72struct imx_port {
73 struct uart_port port;
74 struct timer_list timer;
75 unsigned int old_status;
Sascha Hauer5b802342006-05-04 14:07:42 +010076 int txirq,rxirq,rtsirq;
77 int have_rtscts:1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
80/*
81 * Handle any change of modem status signal since we were last called.
82 */
83static void imx_mctrl_check(struct imx_port *sport)
84{
85 unsigned int status, changed;
86
87 status = sport->port.ops->get_mctrl(&sport->port);
88 changed = status ^ sport->old_status;
89
90 if (changed == 0)
91 return;
92
93 sport->old_status = status;
94
95 if (changed & TIOCM_RI)
96 sport->port.icount.rng++;
97 if (changed & TIOCM_DSR)
98 sport->port.icount.dsr++;
99 if (changed & TIOCM_CAR)
100 uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
101 if (changed & TIOCM_CTS)
102 uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
103
104 wake_up_interruptible(&sport->port.info->delta_msr_wait);
105}
106
107/*
108 * This is our per-port timeout handler, for checking the
109 * modem status signals.
110 */
111static void imx_timeout(unsigned long data)
112{
113 struct imx_port *sport = (struct imx_port *)data;
114 unsigned long flags;
115
116 if (sport->port.info) {
117 spin_lock_irqsave(&sport->port.lock, flags);
118 imx_mctrl_check(sport);
119 spin_unlock_irqrestore(&sport->port.lock, flags);
120
121 mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
122 }
123}
124
125/*
126 * interrupts disabled on entry
127 */
Russell Kingb129a8c2005-08-31 10:12:14 +0100128static void imx_stop_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct imx_port *sport = (struct imx_port *)port;
131 UCR1((u32)sport->port.membase) &= ~UCR1_TXMPTYEN;
132}
133
134/*
135 * interrupts disabled on entry
136 */
137static void imx_stop_rx(struct uart_port *port)
138{
139 struct imx_port *sport = (struct imx_port *)port;
140 UCR2((u32)sport->port.membase) &= ~UCR2_RXEN;
141}
142
143/*
144 * Set the modem control timer to fire immediately.
145 */
146static void imx_enable_ms(struct uart_port *port)
147{
148 struct imx_port *sport = (struct imx_port *)port;
149
150 mod_timer(&sport->timer, jiffies);
151}
152
153static inline void imx_transmit_buffer(struct imx_port *sport)
154{
155 struct circ_buf *xmit = &sport->port.info->xmit;
156
Sascha Hauer8c0b2542007-02-05 16:10:16 -0800157 while (!(UTS((u32)sport->port.membase) & UTS_TXFULL)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 /* send xmit->buf[xmit->tail]
159 * out the port here */
160 URTX0((u32)sport->port.membase) = xmit->buf[xmit->tail];
161 xmit->tail = (xmit->tail + 1) &
162 (UART_XMIT_SIZE - 1);
163 sport->port.icount.tx++;
164 if (uart_circ_empty(xmit))
165 break;
Sascha Hauer8c0b2542007-02-05 16:10:16 -0800166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 if (uart_circ_empty(xmit))
Russell Kingb129a8c2005-08-31 10:12:14 +0100169 imx_stop_tx(&sport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172/*
173 * interrupts disabled on entry
174 */
Russell Kingb129a8c2005-08-31 10:12:14 +0100175static void imx_start_tx(struct uart_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct imx_port *sport = (struct imx_port *)port;
178
179 UCR1((u32)sport->port.membase) |= UCR1_TXMPTYEN;
180
Sascha Hauer8c0b2542007-02-05 16:10:16 -0800181 imx_transmit_buffer(sport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
David Howells7d12e782006-10-05 14:55:46 +0100184static irqreturn_t imx_rtsint(int irq, void *dev_id)
Sascha Hauerceca6292005-10-12 19:58:08 +0100185{
186 struct imx_port *sport = (struct imx_port *)dev_id;
187 unsigned int val = USR1((u32)sport->port.membase)&USR1_RTSS;
188 unsigned long flags;
189
190 spin_lock_irqsave(&sport->port.lock, flags);
191
192 USR1((u32)sport->port.membase) = USR1_RTSD;
193 uart_handle_cts_change(&sport->port, !!val);
194 wake_up_interruptible(&sport->port.info->delta_msr_wait);
195
196 spin_unlock_irqrestore(&sport->port.lock, flags);
197 return IRQ_HANDLED;
198}
199
David Howells7d12e782006-10-05 14:55:46 +0100200static irqreturn_t imx_txint(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 struct imx_port *sport = (struct imx_port *)dev_id;
203 struct circ_buf *xmit = &sport->port.info->xmit;
204 unsigned long flags;
205
206 spin_lock_irqsave(&sport->port.lock,flags);
207 if (sport->port.x_char)
208 {
209 /* Send next char */
210 URTX0((u32)sport->port.membase) = sport->port.x_char;
211 goto out;
212 }
213
214 if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
Russell Kingb129a8c2005-08-31 10:12:14 +0100215 imx_stop_tx(&sport->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 goto out;
217 }
218
219 imx_transmit_buffer(sport);
220
221 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
222 uart_write_wakeup(&sport->port);
223
224out:
225 spin_unlock_irqrestore(&sport->port.lock,flags);
226 return IRQ_HANDLED;
227}
228
David Howells7d12e782006-10-05 14:55:46 +0100229static irqreturn_t imx_rxint(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 struct imx_port *sport = dev_id;
232 unsigned int rx,flg,ignored = 0;
233 struct tty_struct *tty = sport->port.info->tty;
234 unsigned long flags;
235
236 rx = URXD0((u32)sport->port.membase);
237 spin_lock_irqsave(&sport->port.lock,flags);
238
239 do {
240 flg = TTY_NORMAL;
241 sport->port.icount.rx++;
242
243 if( USR2((u32)sport->port.membase) & USR2_BRCD ) {
244 USR2((u32)sport->port.membase) |= USR2_BRCD;
245 if(uart_handle_break(&sport->port))
246 goto ignore_char;
247 }
248
249 if (uart_handle_sysrq_char
David Howells7d12e782006-10-05 14:55:46 +0100250 (&sport->port, (unsigned char)rx))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 goto ignore_char;
252
253 if( rx & (URXD_PRERR | URXD_OVRRUN | URXD_FRMERR) )
254 goto handle_error;
255
256 error_return:
257 tty_insert_flip_char(tty, rx, flg);
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 ignore_char:
260 rx = URXD0((u32)sport->port.membase);
261 } while(rx & URXD_CHARRDY);
262
263out:
264 spin_unlock_irqrestore(&sport->port.lock,flags);
265 tty_flip_buffer_push(tty);
266 return IRQ_HANDLED;
267
268handle_error:
269 if (rx & URXD_PRERR)
270 sport->port.icount.parity++;
271 else if (rx & URXD_FRMERR)
272 sport->port.icount.frame++;
273 if (rx & URXD_OVRRUN)
274 sport->port.icount.overrun++;
275
276 if (rx & sport->port.ignore_status_mask) {
277 if (++ignored > 100)
278 goto out;
279 goto ignore_char;
280 }
281
282 rx &= sport->port.read_status_mask;
283
284 if (rx & URXD_PRERR)
285 flg = TTY_PARITY;
286 else if (rx & URXD_FRMERR)
287 flg = TTY_FRAME;
288 if (rx & URXD_OVRRUN)
289 flg = TTY_OVERRUN;
290
291#ifdef SUPPORT_SYSRQ
292 sport->port.sysrq = 0;
293#endif
294 goto error_return;
295}
296
297/*
298 * Return TIOCSER_TEMT when transmitter is not busy.
299 */
300static unsigned int imx_tx_empty(struct uart_port *port)
301{
302 struct imx_port *sport = (struct imx_port *)port;
303
304 return USR2((u32)sport->port.membase) & USR2_TXDC ? TIOCSER_TEMT : 0;
305}
306
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100307/*
308 * We have a modem side uart, so the meanings of RTS and CTS are inverted.
309 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310static unsigned int imx_get_mctrl(struct uart_port *port)
311{
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100312 struct imx_port *sport = (struct imx_port *)port;
313 unsigned int tmp = TIOCM_DSR | TIOCM_CAR;
314
315 if (USR1((u32)sport->port.membase) & USR1_RTSS)
316 tmp |= TIOCM_CTS;
317
318 if (UCR2((u32)sport->port.membase) & UCR2_CTS)
319 tmp |= TIOCM_RTS;
320
321 return tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324static void imx_set_mctrl(struct uart_port *port, unsigned int mctrl)
325{
Sascha Hauer0f302dc2005-08-31 21:48:47 +0100326 struct imx_port *sport = (struct imx_port *)port;
327
328 if (mctrl & TIOCM_RTS)
329 UCR2((u32)sport->port.membase) |= UCR2_CTS;
330 else
331 UCR2((u32)sport->port.membase) &= ~UCR2_CTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
334/*
335 * Interrupts always disabled.
336 */
337static void imx_break_ctl(struct uart_port *port, int break_state)
338{
339 struct imx_port *sport = (struct imx_port *)port;
340 unsigned long flags;
341
342 spin_lock_irqsave(&sport->port.lock, flags);
343
344 if ( break_state != 0 )
345 UCR1((u32)sport->port.membase) |= UCR1_SNDBRK;
346 else
347 UCR1((u32)sport->port.membase) &= ~UCR1_SNDBRK;
348
349 spin_unlock_irqrestore(&sport->port.lock, flags);
350}
351
352#define TXTL 2 /* reset default */
353#define RXTL 1 /* reset default */
354
Sascha Hauer587897f2005-04-29 22:46:40 +0100355static int imx_setup_ufcr(struct imx_port *sport, unsigned int mode)
356{
357 unsigned int val;
358 unsigned int ufcr_rfdiv;
359
360 /* set receiver / transmitter trigger level.
361 * RFDIV is set such way to satisfy requested uartclk value
362 */
363 val = TXTL<<10 | RXTL;
364 ufcr_rfdiv = (imx_get_perclk1() + sport->port.uartclk / 2) / sport->port.uartclk;
365
366 if(!ufcr_rfdiv)
367 ufcr_rfdiv = 1;
368
369 if(ufcr_rfdiv >= 7)
370 ufcr_rfdiv = 6;
371 else
372 ufcr_rfdiv = 6 - ufcr_rfdiv;
373
374 val |= UFCR_RFDIV & (ufcr_rfdiv << 7);
375
376 UFCR((u32)sport->port.membase) = val;
377
378 return 0;
379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381static int imx_startup(struct uart_port *port)
382{
383 struct imx_port *sport = (struct imx_port *)port;
384 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 unsigned long flags;
386
Sascha Hauer587897f2005-04-29 22:46:40 +0100387 imx_setup_ufcr(sport, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 /* disable the DREN bit (Data Ready interrupt enable) before
390 * requesting IRQs
391 */
392 UCR4((u32)sport->port.membase) &= ~UCR4_DREN;
393
394 /*
395 * Allocate the IRQ
396 */
397 retval = request_irq(sport->rxirq, imx_rxint, 0,
398 DRIVER_NAME, sport);
Sascha Hauer86371d02005-10-10 10:17:42 +0100399 if (retval) goto error_out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 retval = request_irq(sport->txirq, imx_txint, 0,
Sascha Hauerceca6292005-10-12 19:58:08 +0100402 DRIVER_NAME, sport);
Sascha Hauer86371d02005-10-10 10:17:42 +0100403 if (retval) goto error_out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Russell Kingf43aaba2006-01-19 12:26:57 +0000405 retval = request_irq(sport->rtsirq, imx_rtsint,
Pavel Pisad7ea10d2007-02-05 16:10:20 -0800406 (sport->rtsirq < IMX_IRQS) ? 0 :
407 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
Sascha Hauerceca6292005-10-12 19:58:08 +0100408 DRIVER_NAME, sport);
409 if (retval) goto error_out3;
Sascha Hauerceca6292005-10-12 19:58:08 +0100410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /*
412 * Finally, clear and enable interrupts
413 */
414
Sascha Hauerceca6292005-10-12 19:58:08 +0100415 USR1((u32)sport->port.membase) = USR1_RTSD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 UCR1((u32)sport->port.membase) |=
Sascha Hauerceca6292005-10-12 19:58:08 +0100417 (UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 UCR2((u32)sport->port.membase) |= (UCR2_RXEN | UCR2_TXEN);
420 /*
421 * Enable modem status interrupts
422 */
423 spin_lock_irqsave(&sport->port.lock,flags);
424 imx_enable_ms(&sport->port);
425 spin_unlock_irqrestore(&sport->port.lock,flags);
426
427 return 0;
428
Sascha Hauerceca6292005-10-12 19:58:08 +0100429error_out3:
430 free_irq(sport->txirq, sport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431error_out2:
Sascha Hauer86371d02005-10-10 10:17:42 +0100432 free_irq(sport->rxirq, sport);
433error_out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return retval;
435}
436
437static void imx_shutdown(struct uart_port *port)
438{
439 struct imx_port *sport = (struct imx_port *)port;
440
441 /*
442 * Stop our timer.
443 */
444 del_timer_sync(&sport->timer);
445
446 /*
447 * Free the interrupts
448 */
Sascha Hauerceca6292005-10-12 19:58:08 +0100449 free_irq(sport->rtsirq, sport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 free_irq(sport->txirq, sport);
451 free_irq(sport->rxirq, sport);
452
453 /*
454 * Disable all interrupts, port and break condition.
455 */
456
457 UCR1((u32)sport->port.membase) &=
Sascha Hauerceca6292005-10-12 19:58:08 +0100458 ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_UARTEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461static void
Alan Cox606d0992006-12-08 02:38:45 -0800462imx_set_termios(struct uart_port *port, struct ktermios *termios,
463 struct ktermios *old)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464{
465 struct imx_port *sport = (struct imx_port *)port;
466 unsigned long flags;
467 unsigned int ucr2, old_ucr1, old_txrxen, baud, quot;
468 unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
469
470 /*
471 * If we don't support modem control lines, don't allow
472 * these to be set.
473 */
474 if (0) {
475 termios->c_cflag &= ~(HUPCL | CRTSCTS | CMSPAR);
476 termios->c_cflag |= CLOCAL;
477 }
478
479 /*
480 * We only support CS7 and CS8.
481 */
482 while ((termios->c_cflag & CSIZE) != CS7 &&
483 (termios->c_cflag & CSIZE) != CS8) {
484 termios->c_cflag &= ~CSIZE;
485 termios->c_cflag |= old_csize;
486 old_csize = CS8;
487 }
488
489 if ((termios->c_cflag & CSIZE) == CS8)
490 ucr2 = UCR2_WS | UCR2_SRST | UCR2_IRTS;
491 else
492 ucr2 = UCR2_SRST | UCR2_IRTS;
493
494 if (termios->c_cflag & CRTSCTS) {
Sascha Hauer5b802342006-05-04 14:07:42 +0100495 if( sport->have_rtscts ) {
496 ucr2 &= ~UCR2_IRTS;
497 ucr2 |= UCR2_CTSC;
498 } else {
499 termios->c_cflag &= ~CRTSCTS;
500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502
503 if (termios->c_cflag & CSTOPB)
504 ucr2 |= UCR2_STPB;
505 if (termios->c_cflag & PARENB) {
506 ucr2 |= UCR2_PREN;
Matt Reimer3261e362006-01-13 20:51:44 +0000507 if (termios->c_cflag & PARODD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 ucr2 |= UCR2_PROE;
509 }
510
511 /*
512 * Ask the core to calculate the divisor for us.
513 */
514 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
515 quot = uart_get_divisor(port, baud);
516
517 spin_lock_irqsave(&sport->port.lock, flags);
518
519 sport->port.read_status_mask = 0;
520 if (termios->c_iflag & INPCK)
521 sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
522 if (termios->c_iflag & (BRKINT | PARMRK))
523 sport->port.read_status_mask |= URXD_BRK;
524
525 /*
526 * Characters to ignore
527 */
528 sport->port.ignore_status_mask = 0;
529 if (termios->c_iflag & IGNPAR)
530 sport->port.ignore_status_mask |= URXD_PRERR;
531 if (termios->c_iflag & IGNBRK) {
532 sport->port.ignore_status_mask |= URXD_BRK;
533 /*
534 * If we're ignoring parity and break indicators,
535 * ignore overruns too (for real raw support).
536 */
537 if (termios->c_iflag & IGNPAR)
538 sport->port.ignore_status_mask |= URXD_OVRRUN;
539 }
540
541 del_timer_sync(&sport->timer);
542
543 /*
544 * Update the per-port timeout.
545 */
546 uart_update_timeout(port, termios->c_cflag, baud);
547
548 /*
549 * disable interrupts and drain transmitter
550 */
551 old_ucr1 = UCR1((u32)sport->port.membase);
Sascha Hauerceca6292005-10-12 19:58:08 +0100552 UCR1((u32)sport->port.membase) &= ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 while ( !(USR2((u32)sport->port.membase) & USR2_TXDC))
555 barrier();
556
557 /* then, disable everything */
558 old_txrxen = UCR2((u32)sport->port.membase) & ( UCR2_TXEN | UCR2_RXEN );
559 UCR2((u32)sport->port.membase) &= ~( UCR2_TXEN | UCR2_RXEN);
560
561 /* set the parity, stop bits and data size */
562 UCR2((u32)sport->port.membase) = ucr2;
563
564 /* set the baud rate. We assume uartclk = 16 MHz
565 *
566 * baud * 16 UBIR - 1
567 * --------- = --------
568 * uartclk UBMR - 1
569 */
570 UBIR((u32)sport->port.membase) = (baud / 100) - 1;
571 UBMR((u32)sport->port.membase) = 10000 - 1;
572
573 UCR1((u32)sport->port.membase) = old_ucr1;
574 UCR2((u32)sport->port.membase) |= old_txrxen;
575
576 if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
577 imx_enable_ms(&sport->port);
578
579 spin_unlock_irqrestore(&sport->port.lock, flags);
580}
581
582static const char *imx_type(struct uart_port *port)
583{
584 struct imx_port *sport = (struct imx_port *)port;
585
586 return sport->port.type == PORT_IMX ? "IMX" : NULL;
587}
588
589/*
590 * Release the memory region(s) being used by 'port'.
591 */
592static void imx_release_port(struct uart_port *port)
593{
594 struct imx_port *sport = (struct imx_port *)port;
595
596 release_mem_region(sport->port.mapbase, UART_PORT_SIZE);
597}
598
599/*
600 * Request the memory region(s) being used by 'port'.
601 */
602static int imx_request_port(struct uart_port *port)
603{
604 struct imx_port *sport = (struct imx_port *)port;
605
606 return request_mem_region(sport->port.mapbase, UART_PORT_SIZE,
607 "imx-uart") != NULL ? 0 : -EBUSY;
608}
609
610/*
611 * Configure/autoconfigure the port.
612 */
613static void imx_config_port(struct uart_port *port, int flags)
614{
615 struct imx_port *sport = (struct imx_port *)port;
616
617 if (flags & UART_CONFIG_TYPE &&
618 imx_request_port(&sport->port) == 0)
619 sport->port.type = PORT_IMX;
620}
621
622/*
623 * Verify the new serial_struct (for TIOCSSERIAL).
624 * The only change we allow are to the flags and type, and
625 * even then only between PORT_IMX and PORT_UNKNOWN
626 */
627static int
628imx_verify_port(struct uart_port *port, struct serial_struct *ser)
629{
630 struct imx_port *sport = (struct imx_port *)port;
631 int ret = 0;
632
633 if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
634 ret = -EINVAL;
635 if (sport->port.irq != ser->irq)
636 ret = -EINVAL;
637 if (ser->io_type != UPIO_MEM)
638 ret = -EINVAL;
639 if (sport->port.uartclk / 16 != ser->baud_base)
640 ret = -EINVAL;
641 if ((void *)sport->port.mapbase != ser->iomem_base)
642 ret = -EINVAL;
643 if (sport->port.iobase != ser->port)
644 ret = -EINVAL;
645 if (ser->hub6 != 0)
646 ret = -EINVAL;
647 return ret;
648}
649
650static struct uart_ops imx_pops = {
651 .tx_empty = imx_tx_empty,
652 .set_mctrl = imx_set_mctrl,
653 .get_mctrl = imx_get_mctrl,
654 .stop_tx = imx_stop_tx,
655 .start_tx = imx_start_tx,
656 .stop_rx = imx_stop_rx,
657 .enable_ms = imx_enable_ms,
658 .break_ctl = imx_break_ctl,
659 .startup = imx_startup,
660 .shutdown = imx_shutdown,
661 .set_termios = imx_set_termios,
662 .type = imx_type,
663 .release_port = imx_release_port,
664 .request_port = imx_request_port,
665 .config_port = imx_config_port,
666 .verify_port = imx_verify_port,
667};
668
669static struct imx_port imx_ports[] = {
670 {
671 .txirq = UART1_MINT_TX,
672 .rxirq = UART1_MINT_RX,
Sascha Hauerceca6292005-10-12 19:58:08 +0100673 .rtsirq = UART1_MINT_RTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 .port = {
675 .type = PORT_IMX,
Russell King9b4a1612006-02-05 10:48:10 +0000676 .iotype = UPIO_MEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 .membase = (void *)IMX_UART1_BASE,
678 .mapbase = IMX_UART1_BASE, /* FIXME */
679 .irq = UART1_MINT_RX,
680 .uartclk = 16000000,
Sascha Hauer8c0b2542007-02-05 16:10:16 -0800681 .fifosize = 32,
Russell Kingce8337c2006-01-21 19:28:15 +0000682 .flags = UPF_BOOT_AUTOCONF,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 .ops = &imx_pops,
684 .line = 0,
685 },
686 }, {
687 .txirq = UART2_MINT_TX,
688 .rxirq = UART2_MINT_RX,
Sascha Hauerceca6292005-10-12 19:58:08 +0100689 .rtsirq = UART2_MINT_RTS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 .port = {
691 .type = PORT_IMX,
Russell King9b4a1612006-02-05 10:48:10 +0000692 .iotype = UPIO_MEM,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 .membase = (void *)IMX_UART2_BASE,
694 .mapbase = IMX_UART2_BASE, /* FIXME */
695 .irq = UART2_MINT_RX,
696 .uartclk = 16000000,
Sascha Hauer8c0b2542007-02-05 16:10:16 -0800697 .fifosize = 32,
Russell Kingce8337c2006-01-21 19:28:15 +0000698 .flags = UPF_BOOT_AUTOCONF,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 .ops = &imx_pops,
700 .line = 1,
701 },
702 }
703};
704
705/*
706 * Setup the IMX serial ports.
707 * Note also that we support "console=ttySMXx" where "x" is either 0 or 1.
708 * Which serial port this ends up being depends on the machine you're
709 * running this kernel on. I'm not convinced that this is a good idea,
710 * but that's the way it traditionally works.
711 *
712 */
713static void __init imx_init_ports(void)
714{
715 static int first = 1;
716 int i;
717
718 if (!first)
719 return;
720 first = 0;
721
722 for (i = 0; i < ARRAY_SIZE(imx_ports); i++) {
723 init_timer(&imx_ports[i].timer);
724 imx_ports[i].timer.function = imx_timeout;
725 imx_ports[i].timer.data = (unsigned long)&imx_ports[i];
726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729#ifdef CONFIG_SERIAL_IMX_CONSOLE
Russell Kingd3587882006-03-20 20:00:09 +0000730static void imx_console_putchar(struct uart_port *port, int ch)
731{
732 struct imx_port *sport = (struct imx_port *)port;
733 while ((UTS((u32)sport->port.membase) & UTS_TXFULL))
734 barrier();
735 URTX0((u32)sport->port.membase) = ch;
736}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738/*
739 * Interrupts are disabled on entering
740 */
741static void
742imx_console_write(struct console *co, const char *s, unsigned int count)
743{
744 struct imx_port *sport = &imx_ports[co->index];
Russell Kingd3587882006-03-20 20:00:09 +0000745 unsigned int old_ucr1, old_ucr2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 /*
748 * First, save UCR1/2 and then disable interrupts
749 */
750 old_ucr1 = UCR1((u32)sport->port.membase);
751 old_ucr2 = UCR2((u32)sport->port.membase);
752
753 UCR1((u32)sport->port.membase) =
754 (old_ucr1 | UCR1_UARTCLKEN | UCR1_UARTEN)
Sascha Hauerceca6292005-10-12 19:58:08 +0100755 & ~(UCR1_TXMPTYEN | UCR1_RRDYEN | UCR1_RTSDEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 UCR2((u32)sport->port.membase) = old_ucr2 | UCR2_TXEN;
757
Russell Kingd3587882006-03-20 20:00:09 +0000758 uart_console_write(&sport->port, s, count, imx_console_putchar);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760 /*
761 * Finally, wait for transmitter to become empty
762 * and restore UCR1/2
763 */
764 while (!(USR2((u32)sport->port.membase) & USR2_TXDC));
765
766 UCR1((u32)sport->port.membase) = old_ucr1;
767 UCR2((u32)sport->port.membase) = old_ucr2;
768}
769
770/*
771 * If the port was already initialised (eg, by a boot loader),
772 * try to determine the current setup.
773 */
774static void __init
775imx_console_get_options(struct imx_port *sport, int *baud,
776 int *parity, int *bits)
777{
Sascha Hauer587897f2005-04-29 22:46:40 +0100778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if ( UCR1((u32)sport->port.membase) | UCR1_UARTEN ) {
780 /* ok, the port was enabled */
781 unsigned int ucr2, ubir,ubmr, uartclk;
Sascha Hauer587897f2005-04-29 22:46:40 +0100782 unsigned int baud_raw;
783 unsigned int ucfr_rfdiv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 ucr2 = UCR2((u32)sport->port.membase);
786
787 *parity = 'n';
788 if (ucr2 & UCR2_PREN) {
789 if (ucr2 & UCR2_PROE)
790 *parity = 'o';
791 else
792 *parity = 'e';
793 }
794
795 if (ucr2 & UCR2_WS)
796 *bits = 8;
797 else
798 *bits = 7;
799
800 ubir = UBIR((u32)sport->port.membase) & 0xffff;
801 ubmr = UBMR((u32)sport->port.membase) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Sascha Hauer587897f2005-04-29 22:46:40 +0100803
804 ucfr_rfdiv = (UFCR((u32)sport->port.membase) & UFCR_RFDIV) >> 7;
805 if (ucfr_rfdiv == 6)
806 ucfr_rfdiv = 7;
807 else
808 ucfr_rfdiv = 6 - ucfr_rfdiv;
809
810 uartclk = imx_get_perclk1();
811 uartclk /= ucfr_rfdiv;
812
813 { /*
814 * The next code provides exact computation of
815 * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
816 * without need of float support or long long division,
817 * which would be required to prevent 32bit arithmetic overflow
818 */
819 unsigned int mul = ubir + 1;
820 unsigned int div = 16 * (ubmr + 1);
821 unsigned int rem = uartclk % div;
822
823 baud_raw = (uartclk / div) * mul;
824 baud_raw += (rem * mul + div / 2) / div;
825 *baud = (baud_raw + 50) / 100 * 100;
826 }
827
828 if(*baud != baud_raw)
829 printk(KERN_INFO "Serial: Console IMX rounded baud rate from %d to %d\n",
830 baud_raw, *baud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832}
833
834static int __init
835imx_console_setup(struct console *co, char *options)
836{
837 struct imx_port *sport;
838 int baud = 9600;
839 int bits = 8;
840 int parity = 'n';
841 int flow = 'n';
842
843 /*
844 * Check whether an invalid uart number has been specified, and
845 * if so, search for the first available port that does have
846 * console support.
847 */
848 if (co->index == -1 || co->index >= ARRAY_SIZE(imx_ports))
849 co->index = 0;
850 sport = &imx_ports[co->index];
851
852 if (options)
853 uart_parse_options(options, &baud, &parity, &bits, &flow);
854 else
855 imx_console_get_options(sport, &baud, &parity, &bits);
856
Sascha Hauer587897f2005-04-29 22:46:40 +0100857 imx_setup_ufcr(sport, 0);
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
860}
861
Vincent Sanders9f4426d2005-10-01 22:56:34 +0100862static struct uart_driver imx_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863static struct console imx_console = {
864 .name = "ttySMX",
865 .write = imx_console_write,
866 .device = uart_console_device,
867 .setup = imx_console_setup,
868 .flags = CON_PRINTBUFFER,
869 .index = -1,
870 .data = &imx_reg,
871};
872
873static int __init imx_rs_console_init(void)
874{
875 imx_init_ports();
876 register_console(&imx_console);
877 return 0;
878}
879console_initcall(imx_rs_console_init);
880
881#define IMX_CONSOLE &imx_console
882#else
883#define IMX_CONSOLE NULL
884#endif
885
886static struct uart_driver imx_reg = {
887 .owner = THIS_MODULE,
888 .driver_name = DRIVER_NAME,
889 .dev_name = "ttySMX",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 .major = SERIAL_IMX_MAJOR,
891 .minor = MINOR_START,
892 .nr = ARRAY_SIZE(imx_ports),
893 .cons = IMX_CONSOLE,
894};
895
Russell King3ae5eae2005-11-09 22:32:44 +0000896static int serial_imx_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Russell King3ae5eae2005-11-09 22:32:44 +0000898 struct imx_port *sport = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Russell King9480e302005-10-28 09:52:56 -0700900 if (sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 uart_suspend_port(&imx_reg, &sport->port);
902
903 return 0;
904}
905
Russell King3ae5eae2005-11-09 22:32:44 +0000906static int serial_imx_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Russell King3ae5eae2005-11-09 22:32:44 +0000908 struct imx_port *sport = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Russell King9480e302005-10-28 09:52:56 -0700910 if (sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 uart_resume_port(&imx_reg, &sport->port);
912
913 return 0;
914}
915
Russell King3ae5eae2005-11-09 22:32:44 +0000916static int serial_imx_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Sascha Hauer5b802342006-05-04 14:07:42 +0100918 struct imxuart_platform_data *pdata;
919
Russell King3ae5eae2005-11-09 22:32:44 +0000920 imx_ports[dev->id].port.dev = &dev->dev;
Sascha Hauer5b802342006-05-04 14:07:42 +0100921
922 pdata = (struct imxuart_platform_data *)dev->dev.platform_data;
923 if(pdata && (pdata->flags & IMXUART_HAVE_RTSCTS))
924 imx_ports[dev->id].have_rtscts = 1;
925
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 uart_add_one_port(&imx_reg, &imx_ports[dev->id].port);
Russell King3ae5eae2005-11-09 22:32:44 +0000927 platform_set_drvdata(dev, &imx_ports[dev->id]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return 0;
929}
930
Russell King3ae5eae2005-11-09 22:32:44 +0000931static int serial_imx_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Russell King3ae5eae2005-11-09 22:32:44 +0000933 struct imx_port *sport = platform_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Russell King3ae5eae2005-11-09 22:32:44 +0000935 platform_set_drvdata(dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 if (sport)
938 uart_remove_one_port(&imx_reg, &sport->port);
939
940 return 0;
941}
942
Russell King3ae5eae2005-11-09 22:32:44 +0000943static struct platform_driver serial_imx_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 .probe = serial_imx_probe,
945 .remove = serial_imx_remove,
946
947 .suspend = serial_imx_suspend,
948 .resume = serial_imx_resume,
Russell King3ae5eae2005-11-09 22:32:44 +0000949 .driver = {
950 .name = "imx-uart",
951 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952};
953
954static int __init imx_serial_init(void)
955{
956 int ret;
957
958 printk(KERN_INFO "Serial: IMX driver\n");
959
960 imx_init_ports();
961
962 ret = uart_register_driver(&imx_reg);
963 if (ret)
964 return ret;
965
Russell King3ae5eae2005-11-09 22:32:44 +0000966 ret = platform_driver_register(&serial_imx_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (ret != 0)
968 uart_unregister_driver(&imx_reg);
969
970 return 0;
971}
972
973static void __exit imx_serial_exit(void)
974{
975 uart_unregister_driver(&imx_reg);
Russell Kingc889b892005-11-21 17:05:21 +0000976 platform_driver_unregister(&serial_imx_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
979module_init(imx_serial_init);
980module_exit(imx_serial_exit);
981
982MODULE_AUTHOR("Sascha Hauer");
983MODULE_DESCRIPTION("IMX generic serial port driver");
984MODULE_LICENSE("GPL");