blob: d957a3a9edf1f52bc0ebdeeb6f662ed9cfef7a47 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * mcfserial.c -- serial driver for ColdFire internal UARTS.
3 *
4 * Copyright (C) 1999-2003 Greg Ungerer <gerg@snapgear.com>
5 * Copyright (c) 2000-2001 Lineo, Inc. <www.lineo.com>
6 * Copyright (C) 2001-2002 SnapGear Inc. <www.snapgear.com>
7 *
8 * Based on code from 68332serial.c which was:
9 *
10 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
11 * Copyright (C) 1998 TSHG
12 * Copyright (c) 1999 Rt-Control Inc. <jeff@uclinux.org>
13 *
14 * Changes:
15 * 08/07/2003 Daniele Bellucci <bellucda@tiscali.it>
16 * some cleanups in mcfrs_write.
17 *
18 */
19
20#include <linux/module.h>
21#include <linux/errno.h>
22#include <linux/signal.h>
23#include <linux/sched.h>
24#include <linux/timer.h>
25#include <linux/wait.h>
26#include <linux/interrupt.h>
27#include <linux/tty.h>
28#include <linux/tty_flip.h>
29#include <linux/string.h>
30#include <linux/fcntl.h>
31#include <linux/mm.h>
32#include <linux/kernel.h>
33#include <linux/serial.h>
34#include <linux/serialP.h>
35#include <linux/console.h>
36#include <linux/init.h>
37#include <linux/bitops.h>
38#include <linux/delay.h>
39
40#include <asm/io.h>
41#include <asm/irq.h>
42#include <asm/system.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/semaphore.h>
44#include <asm/delay.h>
45#include <asm/coldfire.h>
46#include <asm/mcfsim.h>
47#include <asm/mcfuart.h>
48#include <asm/nettel.h>
49#include <asm/uaccess.h>
50#include "mcfserial.h"
51
52struct timer_list mcfrs_timer_struct;
53
54/*
55 * Default console baud rate, we use this as the default
56 * for all ports so init can just open /dev/console and
57 * keep going. Perhaps one day the cflag settings for the
58 * console can be used instead.
59 */
Greg Ungerer7f04d622005-11-07 14:09:50 +100060#if defined(CONFIG_ARNEWSH) || defined(CONFIG_FREESCALE) || \
61 defined(CONFIG_senTec) || defined(CONFIG_SNEHA)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define CONSOLE_BAUD_RATE 19200
63#define DEFAULT_CBAUD B19200
64#endif
65
66#if defined(CONFIG_HW_FEITH)
Greg Ungererb0433b92005-09-12 11:18:10 +100067#define CONSOLE_BAUD_RATE 38400
68#define DEFAULT_CBAUD B38400
69#endif
70
Greg Ungerer7f04d622005-11-07 14:09:50 +100071#if defined(CONFIG_MOD5272) || defined(CONFIG_M5208EVB)
Greg Ungererb0433b92005-09-12 11:18:10 +100072#define CONSOLE_BAUD_RATE 115200
73#define DEFAULT_CBAUD B115200
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#endif
75
76#ifndef CONSOLE_BAUD_RATE
77#define CONSOLE_BAUD_RATE 9600
78#define DEFAULT_CBAUD B9600
79#endif
80
81int mcfrs_console_inited = 0;
82int mcfrs_console_port = -1;
83int mcfrs_console_baud = CONSOLE_BAUD_RATE;
84int mcfrs_console_cbaud = DEFAULT_CBAUD;
85
86/*
87 * Driver data structures.
88 */
89static struct tty_driver *mcfrs_serial_driver;
90
91/* number of characters left in xmit buffer before we ask for more */
92#define WAKEUP_CHARS 256
93
94/* Debugging...
95 */
96#undef SERIAL_DEBUG_OPEN
97#undef SERIAL_DEBUG_FLOW
98
Greg Ungerer7f04d622005-11-07 14:09:50 +100099#if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
100 defined(CONFIG_M520x)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#define IRQBASE (MCFINT_VECBASE+MCFINT_UART0)
102#else
103#define IRQBASE 73
104#endif
105
106/*
107 * Configuration table, UARTs to look for at startup.
108 */
109static struct mcf_serial mcfrs_table[] = {
110 { /* ttyS0 */
111 .magic = 0,
112 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE1),
113 .irq = IRQBASE,
114 .flags = ASYNC_BOOT_AUTOCONF,
115 },
116 { /* ttyS1 */
117 .magic = 0,
118 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE2),
119 .irq = IRQBASE+1,
120 .flags = ASYNC_BOOT_AUTOCONF,
121 },
122};
123
124
125#define NR_PORTS (sizeof(mcfrs_table) / sizeof(struct mcf_serial))
126
127/*
128 * This is used to figure out the divisor speeds and the timeouts.
129 */
130static int mcfrs_baud_table[] = {
131 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
132 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0
133};
134#define MCFRS_BAUD_TABLE_SIZE \
135 (sizeof(mcfrs_baud_table)/sizeof(mcfrs_baud_table[0]))
136
137
138#ifdef CONFIG_MAGIC_SYSRQ
139/*
140 * Magic system request keys. Used for debugging...
141 */
142extern int magic_sysrq_key(int ch);
143#endif
144
145
146/*
147 * Forware declarations...
148 */
149static void mcfrs_change_speed(struct mcf_serial *info);
150static void mcfrs_wait_until_sent(struct tty_struct *tty, int timeout);
151
152
153static inline int serial_paranoia_check(struct mcf_serial *info,
154 char *name, const char *routine)
155{
156#ifdef SERIAL_PARANOIA_CHECK
157 static const char badmagic[] =
158 "MCFRS(warning): bad magic number for serial struct %s in %s\n";
159 static const char badinfo[] =
160 "MCFRS(warning): null mcf_serial for %s in %s\n";
161
162 if (!info) {
163 printk(badinfo, name, routine);
164 return 1;
165 }
166 if (info->magic != SERIAL_MAGIC) {
167 printk(badmagic, name, routine);
168 return 1;
169 }
170#endif
171 return 0;
172}
173
174/*
175 * Sets or clears DTR and RTS on the requested line.
176 */
177static void mcfrs_setsignals(struct mcf_serial *info, int dtr, int rts)
178{
179 volatile unsigned char *uartp;
180 unsigned long flags;
181
182#if 0
183 printk("%s(%d): mcfrs_setsignals(info=%x,dtr=%d,rts=%d)\n",
184 __FILE__, __LINE__, info, dtr, rts);
185#endif
186
187 local_irq_save(flags);
188 if (dtr >= 0) {
189#ifdef MCFPP_DTR0
190 if (info->line)
191 mcf_setppdata(MCFPP_DTR1, (dtr ? 0 : MCFPP_DTR1));
192 else
193 mcf_setppdata(MCFPP_DTR0, (dtr ? 0 : MCFPP_DTR0));
194#endif
195 }
196 if (rts >= 0) {
197 uartp = info->addr;
198 if (rts) {
199 info->sigs |= TIOCM_RTS;
200 uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
201 } else {
202 info->sigs &= ~TIOCM_RTS;
203 uartp[MCFUART_UOP0] = MCFUART_UOP_RTS;
204 }
205 }
206 local_irq_restore(flags);
207 return;
208}
209
210/*
211 * Gets values of serial signals.
212 */
213static int mcfrs_getsignals(struct mcf_serial *info)
214{
215 volatile unsigned char *uartp;
216 unsigned long flags;
217 int sigs;
218#if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
219 unsigned short ppdata;
220#endif
221
222#if 0
223 printk("%s(%d): mcfrs_getsignals(info=%x)\n", __FILE__, __LINE__);
224#endif
225
226 local_irq_save(flags);
227 uartp = info->addr;
228 sigs = (uartp[MCFUART_UIPR] & MCFUART_UIPR_CTS) ? 0 : TIOCM_CTS;
229 sigs |= (info->sigs & TIOCM_RTS);
230
231#ifdef MCFPP_DCD0
232{
233 unsigned int ppdata;
234 ppdata = mcf_getppdata();
235 if (info->line == 0) {
236 sigs |= (ppdata & MCFPP_DCD0) ? 0 : TIOCM_CD;
237 sigs |= (ppdata & MCFPP_DTR0) ? 0 : TIOCM_DTR;
238 } else if (info->line == 1) {
239 sigs |= (ppdata & MCFPP_DCD1) ? 0 : TIOCM_CD;
240 sigs |= (ppdata & MCFPP_DTR1) ? 0 : TIOCM_DTR;
241 }
242}
243#endif
244
245 local_irq_restore(flags);
246 return(sigs);
247}
248
249/*
250 * ------------------------------------------------------------
251 * mcfrs_stop() and mcfrs_start()
252 *
253 * This routines are called before setting or resetting tty->stopped.
254 * They enable or disable transmitter interrupts, as necessary.
255 * ------------------------------------------------------------
256 */
257static void mcfrs_stop(struct tty_struct *tty)
258{
259 volatile unsigned char *uartp;
260 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
261 unsigned long flags;
262
263 if (serial_paranoia_check(info, tty->name, "mcfrs_stop"))
264 return;
265
266 local_irq_save(flags);
267 uartp = info->addr;
268 info->imr &= ~MCFUART_UIR_TXREADY;
269 uartp[MCFUART_UIMR] = info->imr;
270 local_irq_restore(flags);
271}
272
273static void mcfrs_start(struct tty_struct *tty)
274{
275 volatile unsigned char *uartp;
276 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
277 unsigned long flags;
278
279 if (serial_paranoia_check(info, tty->name, "mcfrs_start"))
280 return;
281
282 local_irq_save(flags);
283 if (info->xmit_cnt && info->xmit_buf) {
284 uartp = info->addr;
285 info->imr |= MCFUART_UIR_TXREADY;
286 uartp[MCFUART_UIMR] = info->imr;
287 }
288 local_irq_restore(flags);
289}
290
291/*
292 * ----------------------------------------------------------------------
293 *
294 * Here starts the interrupt handling routines. All of the following
295 * subroutines are declared as inline and are folded into
296 * mcfrs_interrupt(). They were separated out for readability's sake.
297 *
298 * Note: mcfrs_interrupt() is a "fast" interrupt, which means that it
299 * runs with interrupts turned off. People who may want to modify
300 * mcfrs_interrupt() should try to keep the interrupt handler as fast as
301 * possible. After you are done making modifications, it is not a bad
302 * idea to do:
303 *
304 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
305 *
306 * and look at the resulting assemble code in serial.s.
307 *
308 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
309 * -----------------------------------------------------------------------
310 */
311
312static inline void receive_chars(struct mcf_serial *info)
313{
314 volatile unsigned char *uartp;
315 struct tty_struct *tty = info->tty;
Alan Cox33f0f882006-01-09 20:54:13 -0800316 unsigned char status, ch, flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 if (!tty)
319 return;
320
321 uartp = info->addr;
322
323 while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 ch = uartp[MCFUART_URB];
325 info->stats.rx++;
326
327#ifdef CONFIG_MAGIC_SYSRQ
328 if (mcfrs_console_inited && (info->line == mcfrs_console_port)) {
329 if (magic_sysrq_key(ch))
330 continue;
331 }
332#endif
333
Alan Cox33f0f882006-01-09 20:54:13 -0800334 flag = TTY_NORMAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (status & MCFUART_USR_RXERR) {
336 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
337 if (status & MCFUART_USR_RXBREAK) {
338 info->stats.rxbreak++;
Alan Cox33f0f882006-01-09 20:54:13 -0800339 flag = TTY_BREAK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 } else if (status & MCFUART_USR_RXPARITY) {
341 info->stats.rxparity++;
Alan Cox33f0f882006-01-09 20:54:13 -0800342 flag = TTY_PARITY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 } else if (status & MCFUART_USR_RXOVERRUN) {
344 info->stats.rxoverrun++;
Alan Cox33f0f882006-01-09 20:54:13 -0800345 flag = TTY_OVERRUN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 } else if (status & MCFUART_USR_RXFRAMING) {
347 info->stats.rxframing++;
Alan Cox33f0f882006-01-09 20:54:13 -0800348 flag = TTY_FRAME;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Alan Cox33f0f882006-01-09 20:54:13 -0800351 tty_insert_flip_char(tty, ch, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
354 schedule_work(&tty->flip.work);
355 return;
356}
357
358static inline void transmit_chars(struct mcf_serial *info)
359{
360 volatile unsigned char *uartp;
361
362 uartp = info->addr;
363
364 if (info->x_char) {
365 /* Send special char - probably flow control */
366 uartp[MCFUART_UTB] = info->x_char;
367 info->x_char = 0;
368 info->stats.tx++;
369 }
370
371 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
372 info->imr &= ~MCFUART_UIR_TXREADY;
373 uartp[MCFUART_UIMR] = info->imr;
374 return;
375 }
376
377 while (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) {
378 uartp[MCFUART_UTB] = info->xmit_buf[info->xmit_tail++];
379 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
380 info->stats.tx++;
381 if (--info->xmit_cnt <= 0)
382 break;
383 }
384
385 if (info->xmit_cnt < WAKEUP_CHARS)
386 schedule_work(&info->tqueue);
387 return;
388}
389
390/*
391 * This is the serial driver's generic interrupt routine
392 */
393irqreturn_t mcfrs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
394{
395 struct mcf_serial *info;
396 unsigned char isr;
397
398 info = &mcfrs_table[(irq - IRQBASE)];
399 isr = info->addr[MCFUART_UISR] & info->imr;
400
401 if (isr & MCFUART_UIR_RXREADY)
402 receive_chars(info);
403 if (isr & MCFUART_UIR_TXREADY)
404 transmit_chars(info);
405 return IRQ_HANDLED;
406}
407
408/*
409 * -------------------------------------------------------------------
410 * Here ends the serial interrupt routines.
411 * -------------------------------------------------------------------
412 */
413
414static void mcfrs_offintr(void *private)
415{
416 struct mcf_serial *info = (struct mcf_serial *) private;
417 struct tty_struct *tty;
418
419 tty = info->tty;
420 if (!tty)
421 return;
422 tty_wakeup(tty);
423}
424
425
426/*
427 * Change of state on a DCD line.
428 */
429void mcfrs_modem_change(struct mcf_serial *info, int dcd)
430{
431 if (info->count == 0)
432 return;
433
434 if (info->flags & ASYNC_CHECK_CD) {
435 if (dcd)
436 wake_up_interruptible(&info->open_wait);
437 else
438 schedule_work(&info->tqueue_hangup);
439 }
440}
441
442
443#ifdef MCFPP_DCD0
444
445unsigned short mcfrs_ppstatus;
446
447/*
448 * This subroutine is called when the RS_TIMER goes off. It is used
449 * to monitor the state of the DCD lines - since they have no edge
450 * sensors and interrupt generators.
451 */
452static void mcfrs_timer(void)
453{
454 unsigned int ppstatus, dcdval, i;
455
456 ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
457
458 if (ppstatus != mcfrs_ppstatus) {
459 for (i = 0; (i < 2); i++) {
460 dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
461 if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
462 mcfrs_modem_change(&mcfrs_table[i],
463 ((ppstatus & dcdval) ? 0 : 1));
464 }
465 }
466 }
467 mcfrs_ppstatus = ppstatus;
468
469 /* Re-arm timer */
470 mcfrs_timer_struct.expires = jiffies + HZ/25;
471 add_timer(&mcfrs_timer_struct);
472}
473
474#endif /* MCFPP_DCD0 */
475
476
477/*
478 * This routine is called from the scheduler tqueue when the interrupt
479 * routine has signalled that a hangup has occurred. The path of
480 * hangup processing is:
481 *
482 * serial interrupt routine -> (scheduler tqueue) ->
483 * do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
484 *
485 */
486static void do_serial_hangup(void *private)
487{
488 struct mcf_serial *info = (struct mcf_serial *) private;
489 struct tty_struct *tty;
490
491 tty = info->tty;
492 if (!tty)
493 return;
494
495 tty_hangup(tty);
496}
497
498static int startup(struct mcf_serial * info)
499{
500 volatile unsigned char *uartp;
501 unsigned long flags;
502
503 if (info->flags & ASYNC_INITIALIZED)
504 return 0;
505
506 if (!info->xmit_buf) {
507 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
508 if (!info->xmit_buf)
509 return -ENOMEM;
510 }
511
512 local_irq_save(flags);
513
514#ifdef SERIAL_DEBUG_OPEN
515 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
516#endif
517
518 /*
519 * Reset UART, get it into known state...
520 */
521 uartp = info->addr;
522 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
523 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
524 mcfrs_setsignals(info, 1, 1);
525
526 if (info->tty)
527 clear_bit(TTY_IO_ERROR, &info->tty->flags);
528 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
529
530 /*
531 * and set the speed of the serial port
532 */
533 mcfrs_change_speed(info);
534
535 /*
536 * Lastly enable the UART transmitter and receiver, and
537 * interrupt enables.
538 */
539 info->imr = MCFUART_UIR_RXREADY;
540 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
541 uartp[MCFUART_UIMR] = info->imr;
542
543 info->flags |= ASYNC_INITIALIZED;
544 local_irq_restore(flags);
545 return 0;
546}
547
548/*
549 * This routine will shutdown a serial port; interrupts are disabled, and
550 * DTR is dropped if the hangup on close termio flag is on.
551 */
552static void shutdown(struct mcf_serial * info)
553{
554 volatile unsigned char *uartp;
555 unsigned long flags;
556
557 if (!(info->flags & ASYNC_INITIALIZED))
558 return;
559
560#ifdef SERIAL_DEBUG_OPEN
561 printk("Shutting down serial port %d (irq %d)....\n", info->line,
562 info->irq);
563#endif
564
565 local_irq_save(flags);
566
567 uartp = info->addr;
568 uartp[MCFUART_UIMR] = 0; /* mask all interrupts */
569 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
570 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
571
572 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
573 mcfrs_setsignals(info, 0, 0);
574
575 if (info->xmit_buf) {
576 free_page((unsigned long) info->xmit_buf);
577 info->xmit_buf = 0;
578 }
579
580 if (info->tty)
581 set_bit(TTY_IO_ERROR, &info->tty->flags);
582
583 info->flags &= ~ASYNC_INITIALIZED;
584 local_irq_restore(flags);
585}
586
587
588/*
589 * This routine is called to set the UART divisor registers to match
590 * the specified baud rate for a serial port.
591 */
592static void mcfrs_change_speed(struct mcf_serial *info)
593{
594 volatile unsigned char *uartp;
595 unsigned int baudclk, cflag;
596 unsigned long flags;
597 unsigned char mr1, mr2;
598 int i;
599#ifdef CONFIG_M5272
600 unsigned int fraction;
601#endif
602
603 if (!info->tty || !info->tty->termios)
604 return;
605 cflag = info->tty->termios->c_cflag;
606 if (info->addr == 0)
607 return;
608
609#if 0
610 printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
611#endif
612
613 i = cflag & CBAUD;
614 if (i & CBAUDEX) {
615 i &= ~CBAUDEX;
616 if (i < 1 || i > 4)
617 info->tty->termios->c_cflag &= ~CBAUDEX;
618 else
619 i += 15;
620 }
621 if (i == 0) {
622 mcfrs_setsignals(info, 0, -1);
623 return;
624 }
625
626 /* compute the baudrate clock */
627#ifdef CONFIG_M5272
628 /*
629 * For the MCF5272, also compute the baudrate fraction.
630 */
631 baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
632 fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
633 fraction *= 16;
634 fraction /= (32 * mcfrs_baud_table[i]);
635#else
636 baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
637#endif
638
639 info->baud = mcfrs_baud_table[i];
640
641 mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
642 mr2 = 0;
643
644 switch (cflag & CSIZE) {
645 case CS5: mr1 |= MCFUART_MR1_CS5; break;
646 case CS6: mr1 |= MCFUART_MR1_CS6; break;
647 case CS7: mr1 |= MCFUART_MR1_CS7; break;
648 case CS8:
649 default: mr1 |= MCFUART_MR1_CS8; break;
650 }
651
652 if (cflag & PARENB) {
653 if (cflag & CMSPAR) {
654 if (cflag & PARODD)
655 mr1 |= MCFUART_MR1_PARITYMARK;
656 else
657 mr1 |= MCFUART_MR1_PARITYSPACE;
658 } else {
659 if (cflag & PARODD)
660 mr1 |= MCFUART_MR1_PARITYODD;
661 else
662 mr1 |= MCFUART_MR1_PARITYEVEN;
663 }
664 } else {
665 mr1 |= MCFUART_MR1_PARITYNONE;
666 }
667
668 if (cflag & CSTOPB)
669 mr2 |= MCFUART_MR2_STOP2;
670 else
671 mr2 |= MCFUART_MR2_STOP1;
672
673 if (cflag & CRTSCTS) {
674 mr1 |= MCFUART_MR1_RXRTS;
675 mr2 |= MCFUART_MR2_TXCTS;
676 }
677
678 if (cflag & CLOCAL)
679 info->flags &= ~ASYNC_CHECK_CD;
680 else
681 info->flags |= ASYNC_CHECK_CD;
682
683 uartp = info->addr;
684
685 local_irq_save(flags);
686#if 0
687 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
688 mr1, mr2, baudclk);
689#endif
690 /*
691 Note: pg 12-16 of MCF5206e User's Manual states that a
692 software reset should be performed prior to changing
693 UMR1,2, UCSR, UACR, bit 7
694 */
695 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
696 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
697 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
698 uartp[MCFUART_UMR] = mr1;
699 uartp[MCFUART_UMR] = mr2;
700 uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8; /* set msb byte */
701 uartp[MCFUART_UBG2] = (baudclk & 0xff); /* set lsb byte */
702#ifdef CONFIG_M5272
703 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
704#endif
705 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
706 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
707 mcfrs_setsignals(info, 1, -1);
708 local_irq_restore(flags);
709 return;
710}
711
712static void mcfrs_flush_chars(struct tty_struct *tty)
713{
714 volatile unsigned char *uartp;
715 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
716 unsigned long flags;
717
718 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
719 return;
720
721 uartp = (volatile unsigned char *) info->addr;
722
723 /*
724 * re-enable receiver interrupt
725 */
726 local_irq_save(flags);
727 if ((!(info->imr & MCFUART_UIR_RXREADY)) &&
728 (info->flags & ASYNC_INITIALIZED) ) {
729 info->imr |= MCFUART_UIR_RXREADY;
730 uartp[MCFUART_UIMR] = info->imr;
731 }
732 local_irq_restore(flags);
733
734 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
735 !info->xmit_buf)
736 return;
737
738 /* Enable transmitter */
739 local_irq_save(flags);
740 info->imr |= MCFUART_UIR_TXREADY;
741 uartp[MCFUART_UIMR] = info->imr;
742 local_irq_restore(flags);
743}
744
745static int mcfrs_write(struct tty_struct * tty,
746 const unsigned char *buf, int count)
747{
748 volatile unsigned char *uartp;
749 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
750 unsigned long flags;
751 int c, total = 0;
752
753#if 0
754 printk("%s(%d): mcfrs_write(tty=%x,buf=%x,count=%d)\n",
755 __FILE__, __LINE__, (int)tty, (int)buf, count);
756#endif
757
758 if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
759 return 0;
760
761 if (!tty || !info->xmit_buf)
762 return 0;
763
764 local_save_flags(flags);
765 while (1) {
766 local_irq_disable();
767 c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
768 ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
769 local_irq_restore(flags);
770
771 if (c <= 0)
772 break;
773
774 memcpy(info->xmit_buf + info->xmit_head, buf, c);
775
776 local_irq_disable();
777 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
778 info->xmit_cnt += c;
779 local_irq_restore(flags);
780
781 buf += c;
782 count -= c;
783 total += c;
784 }
785
786 local_irq_disable();
787 uartp = info->addr;
788 info->imr |= MCFUART_UIR_TXREADY;
789 uartp[MCFUART_UIMR] = info->imr;
790 local_irq_restore(flags);
791
792 return total;
793}
794
795static int mcfrs_write_room(struct tty_struct *tty)
796{
797 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
798 int ret;
799
800 if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
801 return 0;
802 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
803 if (ret < 0)
804 ret = 0;
805 return ret;
806}
807
808static int mcfrs_chars_in_buffer(struct tty_struct *tty)
809{
810 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
811
812 if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
813 return 0;
814 return info->xmit_cnt;
815}
816
817static void mcfrs_flush_buffer(struct tty_struct *tty)
818{
819 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
820 unsigned long flags;
821
822 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
823 return;
824
825 local_irq_save(flags);
826 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
827 local_irq_restore(flags);
828
829 tty_wakeup(tty);
830}
831
832/*
833 * ------------------------------------------------------------
834 * mcfrs_throttle()
835 *
836 * This routine is called by the upper-layer tty layer to signal that
837 * incoming characters should be throttled.
838 * ------------------------------------------------------------
839 */
840static void mcfrs_throttle(struct tty_struct * tty)
841{
842 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
843#ifdef SERIAL_DEBUG_THROTTLE
844 char buf[64];
845
846 printk("throttle %s: %d....\n", _tty_name(tty, buf),
847 tty->ldisc.chars_in_buffer(tty));
848#endif
849
850 if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
851 return;
852
853 if (I_IXOFF(tty))
854 info->x_char = STOP_CHAR(tty);
855
856 /* Turn off RTS line (do this atomic) */
857}
858
859static void mcfrs_unthrottle(struct tty_struct * tty)
860{
861 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
862#ifdef SERIAL_DEBUG_THROTTLE
863 char buf[64];
864
865 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
866 tty->ldisc.chars_in_buffer(tty));
867#endif
868
869 if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
870 return;
871
872 if (I_IXOFF(tty)) {
873 if (info->x_char)
874 info->x_char = 0;
875 else
876 info->x_char = START_CHAR(tty);
877 }
878
879 /* Assert RTS line (do this atomic) */
880}
881
882/*
883 * ------------------------------------------------------------
884 * mcfrs_ioctl() and friends
885 * ------------------------------------------------------------
886 */
887
888static int get_serial_info(struct mcf_serial * info,
889 struct serial_struct * retinfo)
890{
891 struct serial_struct tmp;
892
893 if (!retinfo)
894 return -EFAULT;
895 memset(&tmp, 0, sizeof(tmp));
896 tmp.type = info->type;
897 tmp.line = info->line;
898 tmp.port = (unsigned int) info->addr;
899 tmp.irq = info->irq;
900 tmp.flags = info->flags;
901 tmp.baud_base = info->baud_base;
902 tmp.close_delay = info->close_delay;
903 tmp.closing_wait = info->closing_wait;
904 tmp.custom_divisor = info->custom_divisor;
905 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
906}
907
908static int set_serial_info(struct mcf_serial * info,
909 struct serial_struct * new_info)
910{
911 struct serial_struct new_serial;
912 struct mcf_serial old_info;
913 int retval = 0;
914
915 if (!new_info)
916 return -EFAULT;
917 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
918 return -EFAULT;
919 old_info = *info;
920
921 if (!capable(CAP_SYS_ADMIN)) {
922 if ((new_serial.baud_base != info->baud_base) ||
923 (new_serial.type != info->type) ||
924 (new_serial.close_delay != info->close_delay) ||
925 ((new_serial.flags & ~ASYNC_USR_MASK) !=
926 (info->flags & ~ASYNC_USR_MASK)))
927 return -EPERM;
928 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
929 (new_serial.flags & ASYNC_USR_MASK));
930 info->custom_divisor = new_serial.custom_divisor;
931 goto check_and_exit;
932 }
933
934 if (info->count > 1)
935 return -EBUSY;
936
937 /*
938 * OK, past this point, all the error checking has been done.
939 * At this point, we start making changes.....
940 */
941
942 info->baud_base = new_serial.baud_base;
943 info->flags = ((info->flags & ~ASYNC_FLAGS) |
944 (new_serial.flags & ASYNC_FLAGS));
945 info->type = new_serial.type;
946 info->close_delay = new_serial.close_delay;
947 info->closing_wait = new_serial.closing_wait;
948
949check_and_exit:
950 retval = startup(info);
951 return retval;
952}
953
954/*
955 * get_lsr_info - get line status register info
956 *
957 * Purpose: Let user call ioctl() to get info when the UART physically
958 * is emptied. On bus types like RS485, the transmitter must
959 * release the bus after transmitting. This must be done when
960 * the transmit shift register is empty, not be done when the
961 * transmit holding register is empty. This functionality
962 * allows an RS485 driver to be written in user space.
963 */
964static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
965{
966 volatile unsigned char *uartp;
967 unsigned long flags;
968 unsigned char status;
969
970 local_irq_save(flags);
971 uartp = info->addr;
972 status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
973 local_irq_restore(flags);
974
975 return put_user(status,value);
976}
977
978/*
979 * This routine sends a break character out the serial port.
980 */
981static void send_break( struct mcf_serial * info, int duration)
982{
983 volatile unsigned char *uartp;
984 unsigned long flags;
985
986 if (!info->addr)
987 return;
988 set_current_state(TASK_INTERRUPTIBLE);
989 uartp = info->addr;
990
991 local_irq_save(flags);
992 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
993 schedule_timeout(duration);
994 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
995 local_irq_restore(flags);
996}
997
998static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
999{
1000 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1001
1002 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1003 return -ENODEV;
1004 if (tty->flags & (1 << TTY_IO_ERROR))
1005 return -EIO;
1006
1007 return mcfrs_getsignals(info);
1008}
1009
1010static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
1011 unsigned int set, unsigned int clear)
1012{
1013 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1014 int rts = -1, dtr = -1;
1015
1016 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1017 return -ENODEV;
1018 if (tty->flags & (1 << TTY_IO_ERROR))
1019 return -EIO;
1020
1021 if (set & TIOCM_RTS)
1022 rts = 1;
1023 if (set & TIOCM_DTR)
1024 dtr = 1;
1025 if (clear & TIOCM_RTS)
1026 rts = 0;
1027 if (clear & TIOCM_DTR)
1028 dtr = 0;
1029
1030 mcfrs_setsignals(info, dtr, rts);
1031
1032 return 0;
1033}
1034
1035static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
1036 unsigned int cmd, unsigned long arg)
1037{
1038 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1039 int retval, error;
1040
1041 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1042 return -ENODEV;
1043
1044 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1045 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1046 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1047 if (tty->flags & (1 << TTY_IO_ERROR))
1048 return -EIO;
1049 }
1050
1051 switch (cmd) {
1052 case TCSBRK: /* SVID version: non-zero arg --> no break */
1053 retval = tty_check_change(tty);
1054 if (retval)
1055 return retval;
1056 tty_wait_until_sent(tty, 0);
1057 if (!arg)
1058 send_break(info, HZ/4); /* 1/4 second */
1059 return 0;
1060 case TCSBRKP: /* support for POSIX tcsendbreak() */
1061 retval = tty_check_change(tty);
1062 if (retval)
1063 return retval;
1064 tty_wait_until_sent(tty, 0);
1065 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1066 return 0;
1067 case TIOCGSOFTCAR:
1068 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1069 (unsigned long *) arg);
1070 if (error)
1071 return error;
1072 return 0;
1073 case TIOCSSOFTCAR:
1074 get_user(arg, (unsigned long *) arg);
1075 tty->termios->c_cflag =
1076 ((tty->termios->c_cflag & ~CLOCAL) |
1077 (arg ? CLOCAL : 0));
1078 return 0;
1079 case TIOCGSERIAL:
1080 if (access_ok(VERIFY_WRITE, (void *) arg,
1081 sizeof(struct serial_struct)))
1082 return get_serial_info(info,
1083 (struct serial_struct *) arg);
1084 return -EFAULT;
1085 case TIOCSSERIAL:
1086 return set_serial_info(info,
1087 (struct serial_struct *) arg);
1088 case TIOCSERGETLSR: /* Get line status register */
1089 if (access_ok(VERIFY_WRITE, (void *) arg,
1090 sizeof(unsigned int)))
1091 return get_lsr_info(info, (unsigned int *) arg);
1092 return -EFAULT;
1093 case TIOCSERGSTRUCT:
1094 error = copy_to_user((struct mcf_serial *) arg,
1095 info, sizeof(struct mcf_serial));
1096 if (error)
1097 return -EFAULT;
1098 return 0;
1099
1100#ifdef TIOCSET422
1101 case TIOCSET422: {
1102 unsigned int val;
1103 get_user(val, (unsigned int *) arg);
1104 mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1105 break;
1106 }
1107 case TIOCGET422: {
1108 unsigned int val;
1109 val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1110 put_user(val, (unsigned int *) arg);
1111 break;
1112 }
1113#endif
1114
1115 default:
1116 return -ENOIOCTLCMD;
1117 }
1118 return 0;
1119}
1120
1121static void mcfrs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1122{
1123 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1124
1125 if (tty->termios->c_cflag == old_termios->c_cflag)
1126 return;
1127
1128 mcfrs_change_speed(info);
1129
1130 if ((old_termios->c_cflag & CRTSCTS) &&
1131 !(tty->termios->c_cflag & CRTSCTS)) {
1132 tty->hw_stopped = 0;
1133 mcfrs_setsignals(info, -1, 1);
1134#if 0
1135 mcfrs_start(tty);
1136#endif
1137 }
1138}
1139
1140/*
1141 * ------------------------------------------------------------
1142 * mcfrs_close()
1143 *
1144 * This routine is called when the serial port gets closed. First, we
1145 * wait for the last remaining data to be sent. Then, we unlink its
1146 * S structure from the interrupt chain if necessary, and we free
1147 * that IRQ if nothing is left in the chain.
1148 * ------------------------------------------------------------
1149 */
1150static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1151{
1152 volatile unsigned char *uartp;
1153 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1154 unsigned long flags;
1155
1156 if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1157 return;
1158
1159 local_irq_save(flags);
1160
1161 if (tty_hung_up_p(filp)) {
1162 local_irq_restore(flags);
1163 return;
1164 }
1165
1166#ifdef SERIAL_DEBUG_OPEN
1167 printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1168#endif
1169 if ((tty->count == 1) && (info->count != 1)) {
1170 /*
1171 * Uh, oh. tty->count is 1, which means that the tty
1172 * structure will be freed. Info->count should always
1173 * be one in these conditions. If it's greater than
1174 * one, we've got real problems, since it means the
1175 * serial port won't be shutdown.
1176 */
1177 printk("MCFRS: bad serial port count; tty->count is 1, "
1178 "info->count is %d\n", info->count);
1179 info->count = 1;
1180 }
1181 if (--info->count < 0) {
1182 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1183 info->line, info->count);
1184 info->count = 0;
1185 }
1186 if (info->count) {
1187 local_irq_restore(flags);
1188 return;
1189 }
1190 info->flags |= ASYNC_CLOSING;
1191
1192 /*
1193 * Now we wait for the transmit buffer to clear; and we notify
1194 * the line discipline to only process XON/XOFF characters.
1195 */
1196 tty->closing = 1;
1197 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1198 tty_wait_until_sent(tty, info->closing_wait);
1199
1200 /*
1201 * At this point we stop accepting input. To do this, we
1202 * disable the receive line status interrupts, and tell the
1203 * interrupt driver to stop checking the data ready bit in the
1204 * line status register.
1205 */
1206 info->imr &= ~MCFUART_UIR_RXREADY;
1207 uartp = info->addr;
1208 uartp[MCFUART_UIMR] = info->imr;
1209
1210#if 0
1211 /* FIXME: do we need to keep this enabled for console?? */
1212 if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
1213 /* Do not disable the UART */ ;
1214 } else
1215#endif
1216 shutdown(info);
1217 if (tty->driver->flush_buffer)
1218 tty->driver->flush_buffer(tty);
1219 tty_ldisc_flush(tty);
1220
1221 tty->closing = 0;
1222 info->event = 0;
1223 info->tty = 0;
1224#if 0
1225 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1226 if (tty->ldisc.close)
1227 (tty->ldisc.close)(tty);
1228 tty->ldisc = ldiscs[N_TTY];
1229 tty->termios->c_line = N_TTY;
1230 if (tty->ldisc.open)
1231 (tty->ldisc.open)(tty);
1232 }
1233#endif
1234 if (info->blocked_open) {
1235 if (info->close_delay) {
1236 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1237 }
1238 wake_up_interruptible(&info->open_wait);
1239 }
1240 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1241 wake_up_interruptible(&info->close_wait);
1242 local_irq_restore(flags);
1243}
1244
1245/*
1246 * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1247 */
1248static void
1249mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1250{
1251#ifdef CONFIG_M5272
1252#define MCF5272_FIFO_SIZE 25 /* fifo size + shift reg */
1253
1254 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1255 volatile unsigned char *uartp;
1256 unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1257
1258 if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1259 return;
1260
1261 orig_jiffies = jiffies;
1262
1263 /*
1264 * Set the check interval to be 1/5 of the approximate time
1265 * to send the entire fifo, and make it at least 1. The check
1266 * interval should also be less than the timeout.
1267 *
1268 * Note: we have to use pretty tight timings here to satisfy
1269 * the NIST-PCTS.
1270 */
1271 fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1272 char_time = fifo_time / 5;
1273 if (char_time == 0)
1274 char_time = 1;
1275 if (timeout && timeout < char_time)
1276 char_time = timeout;
1277
1278 /*
1279 * Clamp the timeout period at 2 * the time to empty the
1280 * fifo. Just to be safe, set the minimum at .5 seconds.
1281 */
1282 fifo_time *= 2;
1283 if (fifo_time < (HZ/2))
1284 fifo_time = HZ/2;
1285 if (!timeout || timeout > fifo_time)
1286 timeout = fifo_time;
1287
1288 /*
1289 * Account for the number of bytes in the UART
1290 * transmitter FIFO plus any byte being shifted out.
1291 */
1292 uartp = (volatile unsigned char *) info->addr;
1293 for (;;) {
1294 fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1295 if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1296 MCFUART_USR_TXEMPTY)) ==
1297 MCFUART_USR_TXREADY)
1298 fifo_cnt++;
1299 if (fifo_cnt == 0)
1300 break;
1301 msleep_interruptible(jiffies_to_msecs(char_time));
1302 if (signal_pending(current))
1303 break;
1304 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1305 break;
1306 }
1307#else
1308 /*
1309 * For the other coldfire models, assume all data has been sent
1310 */
1311#endif
1312}
1313
1314/*
1315 * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1316 */
1317void mcfrs_hangup(struct tty_struct *tty)
1318{
1319 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1320
1321 if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
1322 return;
1323
1324 mcfrs_flush_buffer(tty);
1325 shutdown(info);
1326 info->event = 0;
1327 info->count = 0;
1328 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1329 info->tty = 0;
1330 wake_up_interruptible(&info->open_wait);
1331}
1332
1333/*
1334 * ------------------------------------------------------------
1335 * mcfrs_open() and friends
1336 * ------------------------------------------------------------
1337 */
1338static int block_til_ready(struct tty_struct *tty, struct file * filp,
1339 struct mcf_serial *info)
1340{
1341 DECLARE_WAITQUEUE(wait, current);
1342 int retval;
1343 int do_clocal = 0;
1344
1345 /*
1346 * If the device is in the middle of being closed, then block
1347 * until it's done, and then try again.
1348 */
1349 if (info->flags & ASYNC_CLOSING) {
1350 interruptible_sleep_on(&info->close_wait);
1351#ifdef SERIAL_DO_RESTART
1352 if (info->flags & ASYNC_HUP_NOTIFY)
1353 return -EAGAIN;
1354 else
1355 return -ERESTARTSYS;
1356#else
1357 return -EAGAIN;
1358#endif
1359 }
1360
1361 /*
1362 * If non-blocking mode is set, or the port is not enabled,
1363 * then make the check up front and then exit.
1364 */
1365 if ((filp->f_flags & O_NONBLOCK) ||
1366 (tty->flags & (1 << TTY_IO_ERROR))) {
1367 info->flags |= ASYNC_NORMAL_ACTIVE;
1368 return 0;
1369 }
1370
1371 if (tty->termios->c_cflag & CLOCAL)
1372 do_clocal = 1;
1373
1374 /*
1375 * Block waiting for the carrier detect and the line to become
1376 * free (i.e., not in use by the callout). While we are in
1377 * this loop, info->count is dropped by one, so that
1378 * mcfrs_close() knows when to free things. We restore it upon
1379 * exit, either normal or abnormal.
1380 */
1381 retval = 0;
1382 add_wait_queue(&info->open_wait, &wait);
1383#ifdef SERIAL_DEBUG_OPEN
1384 printk("block_til_ready before block: ttyS%d, count = %d\n",
1385 info->line, info->count);
1386#endif
1387 info->count--;
1388 info->blocked_open++;
1389 while (1) {
1390 local_irq_disable();
1391 mcfrs_setsignals(info, 1, 1);
1392 local_irq_enable();
1393 current->state = TASK_INTERRUPTIBLE;
1394 if (tty_hung_up_p(filp) ||
1395 !(info->flags & ASYNC_INITIALIZED)) {
1396#ifdef SERIAL_DO_RESTART
1397 if (info->flags & ASYNC_HUP_NOTIFY)
1398 retval = -EAGAIN;
1399 else
1400 retval = -ERESTARTSYS;
1401#else
1402 retval = -EAGAIN;
1403#endif
1404 break;
1405 }
1406 if (!(info->flags & ASYNC_CLOSING) &&
1407 (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1408 break;
1409 if (signal_pending(current)) {
1410 retval = -ERESTARTSYS;
1411 break;
1412 }
1413#ifdef SERIAL_DEBUG_OPEN
1414 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1415 info->line, info->count);
1416#endif
1417 schedule();
1418 }
1419 current->state = TASK_RUNNING;
1420 remove_wait_queue(&info->open_wait, &wait);
1421 if (!tty_hung_up_p(filp))
1422 info->count++;
1423 info->blocked_open--;
1424#ifdef SERIAL_DEBUG_OPEN
1425 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1426 info->line, info->count);
1427#endif
1428 if (retval)
1429 return retval;
1430 info->flags |= ASYNC_NORMAL_ACTIVE;
1431 return 0;
1432}
1433
1434/*
1435 * This routine is called whenever a serial port is opened. It
1436 * enables interrupts for a serial port, linking in its structure into
1437 * the IRQ chain. It also performs the serial-specific
1438 * initialization for the tty structure.
1439 */
1440int mcfrs_open(struct tty_struct *tty, struct file * filp)
1441{
1442 struct mcf_serial *info;
1443 int retval, line;
1444
1445 line = tty->index;
1446 if ((line < 0) || (line >= NR_PORTS))
1447 return -ENODEV;
1448 info = mcfrs_table + line;
1449 if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1450 return -ENODEV;
1451#ifdef SERIAL_DEBUG_OPEN
1452 printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1453#endif
1454 info->count++;
1455 tty->driver_data = info;
1456 info->tty = tty;
1457
1458 /*
1459 * Start up serial port
1460 */
1461 retval = startup(info);
1462 if (retval)
1463 return retval;
1464
1465 retval = block_til_ready(tty, filp, info);
1466 if (retval) {
1467#ifdef SERIAL_DEBUG_OPEN
1468 printk("mcfrs_open returning after block_til_ready with %d\n",
1469 retval);
1470#endif
1471 return retval;
1472 }
1473
1474#ifdef SERIAL_DEBUG_OPEN
1475 printk("mcfrs_open %s successful...\n", tty->name);
1476#endif
1477 return 0;
1478}
1479
1480/*
1481 * Based on the line number set up the internal interrupt stuff.
1482 */
1483static void mcfrs_irqinit(struct mcf_serial *info)
1484{
1485#if defined(CONFIG_M5272)
1486 volatile unsigned long *icrp;
1487 volatile unsigned long *portp;
1488 volatile unsigned char *uartp;
1489
1490 uartp = info->addr;
1491 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1492
1493 switch (info->line) {
1494 case 0:
1495 *icrp = 0xe0000000;
1496 break;
1497 case 1:
1498 *icrp = 0x0e000000;
1499 break;
1500 default:
1501 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1502 info->line);
1503 return;
1504 }
1505
1506 /* Enable the output lines for the serial ports */
1507 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
1508 *portp = (*portp & ~0x000000ff) | 0x00000055;
1509 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
1510 *portp = (*portp & ~0x000003fc) | 0x000002a8;
Greg Ungererb0433b92005-09-12 11:18:10 +10001511#elif defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 volatile unsigned char *icrp, *uartp;
1513 volatile unsigned long *imrp;
1514
1515 uartp = info->addr;
1516
1517 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1518 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
Greg Ungerer082f2c12006-01-10 16:59:04 +10001519 *icrp = 0x30 + info->line; /* level 6, line based priority */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1522 MCFINTC_IMRL);
1523 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
Greg Ungerer7f04d622005-11-07 14:09:50 +10001524#elif defined(CONFIG_M520x)
1525 volatile unsigned char *icrp, *uartp;
1526 volatile unsigned long *imrp;
1527
1528 uartp = info->addr;
1529
1530 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1531 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1532 *icrp = 0x03;
1533
1534 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1535 MCFINTC_IMRL);
1536 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1537 if (info->line < 2) {
1538 unsigned short *uart_par;
1539 uart_par = (unsigned short *)(MCF_IPSBAR + MCF_GPIO_PAR_UART);
1540 if (info->line == 0)
1541 *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD0
1542 | MCF_GPIO_PAR_UART_PAR_URXD0;
1543 else if (info->line == 1)
1544 *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD1
1545 | MCF_GPIO_PAR_UART_PAR_URXD1;
1546 } else if (info->line == 2) {
1547 unsigned char *feci2c_par;
1548 feci2c_par = (unsigned char *)(MCF_IPSBAR + MCF_GPIO_PAR_FECI2C);
1549 *feci2c_par &= ~0x0F;
1550 *feci2c_par |= MCF_GPIO_PAR_FECI2C_PAR_SCL_UTXD2
1551 | MCF_GPIO_PAR_FECI2C_PAR_SDA_URXD2;
1552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553#else
1554 volatile unsigned char *icrp, *uartp;
1555
1556 switch (info->line) {
1557 case 0:
1558 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1559 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1560 MCFSIM_ICR_PRI1;
1561 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1562 break;
1563 case 1:
1564 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1565 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1566 MCFSIM_ICR_PRI2;
1567 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1568 break;
1569 default:
1570 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1571 info->line);
1572 return;
1573 }
1574
1575 uartp = info->addr;
1576 uartp[MCFUART_UIVR] = info->irq;
1577#endif
1578
1579 /* Clear mask, so no surprise interrupts. */
1580 uartp[MCFUART_UIMR] = 0;
1581
1582 if (request_irq(info->irq, mcfrs_interrupt, SA_INTERRUPT,
1583 "ColdFire UART", NULL)) {
1584 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1585 "vector=%d\n", info->line, info->irq);
1586 }
1587
1588 return;
1589}
1590
1591
1592char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1593
1594
1595/*
1596 * Serial stats reporting...
1597 */
1598int mcfrs_readproc(char *page, char **start, off_t off, int count,
1599 int *eof, void *data)
1600{
1601 struct mcf_serial *info;
1602 char str[20];
1603 int len, sigs, i;
1604
1605 len = sprintf(page, mcfrs_drivername);
1606 for (i = 0; (i < NR_PORTS); i++) {
1607 info = &mcfrs_table[i];
1608 len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1609 i, (unsigned int) info->addr, info->irq, info->baud);
1610 if (info->stats.rx || info->stats.tx)
1611 len += sprintf((page + len), "tx:%d rx:%d ",
1612 info->stats.tx, info->stats.rx);
1613 if (info->stats.rxframing)
1614 len += sprintf((page + len), "fe:%d ",
1615 info->stats.rxframing);
1616 if (info->stats.rxparity)
1617 len += sprintf((page + len), "pe:%d ",
1618 info->stats.rxparity);
1619 if (info->stats.rxbreak)
1620 len += sprintf((page + len), "brk:%d ",
1621 info->stats.rxbreak);
1622 if (info->stats.rxoverrun)
1623 len += sprintf((page + len), "oe:%d ",
1624 info->stats.rxoverrun);
1625
1626 str[0] = str[1] = 0;
1627 if ((sigs = mcfrs_getsignals(info))) {
1628 if (sigs & TIOCM_RTS)
1629 strcat(str, "|RTS");
1630 if (sigs & TIOCM_CTS)
1631 strcat(str, "|CTS");
1632 if (sigs & TIOCM_DTR)
1633 strcat(str, "|DTR");
1634 if (sigs & TIOCM_CD)
1635 strcat(str, "|CD");
1636 }
1637
1638 len += sprintf((page + len), "%s\n", &str[1]);
1639 }
1640
1641 return(len);
1642}
1643
1644
1645/* Finally, routines used to initialize the serial driver. */
1646
1647static void show_serial_version(void)
1648{
1649 printk(mcfrs_drivername);
1650}
1651
1652static struct tty_operations mcfrs_ops = {
1653 .open = mcfrs_open,
1654 .close = mcfrs_close,
1655 .write = mcfrs_write,
1656 .flush_chars = mcfrs_flush_chars,
1657 .write_room = mcfrs_write_room,
1658 .chars_in_buffer = mcfrs_chars_in_buffer,
1659 .flush_buffer = mcfrs_flush_buffer,
1660 .ioctl = mcfrs_ioctl,
1661 .throttle = mcfrs_throttle,
1662 .unthrottle = mcfrs_unthrottle,
1663 .set_termios = mcfrs_set_termios,
1664 .stop = mcfrs_stop,
1665 .start = mcfrs_start,
1666 .hangup = mcfrs_hangup,
1667 .read_proc = mcfrs_readproc,
1668 .wait_until_sent = mcfrs_wait_until_sent,
1669 .tiocmget = mcfrs_tiocmget,
1670 .tiocmset = mcfrs_tiocmset,
1671};
1672
1673/* mcfrs_init inits the driver */
1674static int __init
1675mcfrs_init(void)
1676{
1677 struct mcf_serial *info;
1678 unsigned long flags;
1679 int i;
1680
1681 /* Setup base handler, and timer table. */
1682#ifdef MCFPP_DCD0
1683 init_timer(&mcfrs_timer_struct);
1684 mcfrs_timer_struct.function = mcfrs_timer;
1685 mcfrs_timer_struct.data = 0;
1686 mcfrs_timer_struct.expires = jiffies + HZ/25;
1687 add_timer(&mcfrs_timer_struct);
1688 mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1689#endif
1690 mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1691 if (!mcfrs_serial_driver)
1692 return -ENOMEM;
1693
1694 show_serial_version();
1695
1696 /* Initialize the tty_driver structure */
1697 mcfrs_serial_driver->owner = THIS_MODULE;
1698 mcfrs_serial_driver->name = "ttyS";
1699 mcfrs_serial_driver->devfs_name = "ttys/";
1700 mcfrs_serial_driver->driver_name = "serial";
1701 mcfrs_serial_driver->major = TTY_MAJOR;
1702 mcfrs_serial_driver->minor_start = 64;
1703 mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1704 mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1705 mcfrs_serial_driver->init_termios = tty_std_termios;
1706
1707 mcfrs_serial_driver->init_termios.c_cflag =
1708 mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1709 mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1710
1711 tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1712
1713 if (tty_register_driver(mcfrs_serial_driver)) {
1714 printk("MCFRS: Couldn't register serial driver\n");
1715 put_tty_driver(mcfrs_serial_driver);
1716 return(-EBUSY);
1717 }
1718
1719 local_irq_save(flags);
1720
1721 /*
1722 * Configure all the attached serial ports.
1723 */
1724 for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1725 info->magic = SERIAL_MAGIC;
1726 info->line = i;
1727 info->tty = 0;
1728 info->custom_divisor = 16;
1729 info->close_delay = 50;
1730 info->closing_wait = 3000;
1731 info->x_char = 0;
1732 info->event = 0;
1733 info->count = 0;
1734 info->blocked_open = 0;
1735 INIT_WORK(&info->tqueue, mcfrs_offintr, info);
1736 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1737 init_waitqueue_head(&info->open_wait);
1738 init_waitqueue_head(&info->close_wait);
1739
1740 info->imr = 0;
1741 mcfrs_setsignals(info, 0, 0);
1742 mcfrs_irqinit(info);
1743
1744 printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1745 (unsigned int) info->addr, info->irq);
1746 printk(" is a builtin ColdFire UART\n");
1747 }
1748
1749 local_irq_restore(flags);
1750 return 0;
1751}
1752
1753module_init(mcfrs_init);
1754
1755/****************************************************************************/
1756/* Serial Console */
1757/****************************************************************************/
1758
1759/*
1760 * Quick and dirty UART initialization, for console output.
1761 */
1762
1763void mcfrs_init_console(void)
1764{
1765 volatile unsigned char *uartp;
1766 unsigned int clk;
1767
1768 /*
1769 * Reset UART, get it into known state...
1770 */
1771 uartp = (volatile unsigned char *) (MCF_MBAR +
1772 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1773
1774 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
1775 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
1776 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
1777
1778 /*
1779 * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1780 */
1781 uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1782 uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1783
1784 clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1785 uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8; /* set msb baud */
1786 uartp[MCFUART_UBG2] = (clk & 0xff); /* set lsb baud */
1787
1788 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1789 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1790
1791 mcfrs_console_inited++;
1792 return;
1793}
1794
1795
1796/*
1797 * Setup for console. Argument comes from the boot command line.
1798 */
1799
1800int mcfrs_console_setup(struct console *cp, char *arg)
1801{
1802 int i, n = CONSOLE_BAUD_RATE;
1803
1804 if (!cp)
1805 return(-1);
1806
1807 if (!strncmp(cp->name, "ttyS", 4))
1808 mcfrs_console_port = cp->index;
1809 else if (!strncmp(cp->name, "cua", 3))
1810 mcfrs_console_port = cp->index;
1811 else
1812 return(-1);
1813
1814 if (arg)
1815 n = simple_strtoul(arg,NULL,0);
1816 for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1817 if (mcfrs_baud_table[i] == n)
1818 break;
1819 if (i < MCFRS_BAUD_TABLE_SIZE) {
1820 mcfrs_console_baud = n;
1821 mcfrs_console_cbaud = 0;
1822 if (i > 15) {
1823 mcfrs_console_cbaud |= CBAUDEX;
1824 i -= 15;
1825 }
1826 mcfrs_console_cbaud |= i;
1827 }
1828 mcfrs_init_console(); /* make sure baud rate changes */
1829 return(0);
1830}
1831
1832
1833static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1834{
1835 *index = c->index;
1836 return mcfrs_serial_driver;
1837}
1838
1839
1840/*
1841 * Output a single character, using UART polled mode.
1842 * This is used for console output.
1843 */
1844
1845void mcfrs_put_char(char ch)
1846{
1847 volatile unsigned char *uartp;
1848 unsigned long flags;
1849 int i;
1850
1851 uartp = (volatile unsigned char *) (MCF_MBAR +
1852 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1853
1854 local_irq_save(flags);
1855 for (i = 0; (i < 0x10000); i++) {
1856 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1857 break;
1858 }
1859 if (i < 0x10000) {
1860 uartp[MCFUART_UTB] = ch;
1861 for (i = 0; (i < 0x10000); i++)
1862 if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1863 break;
1864 }
1865 if (i >= 0x10000)
1866 mcfrs_init_console(); /* try and get it back */
1867 local_irq_restore(flags);
1868
1869 return;
1870}
1871
1872
1873/*
1874 * rs_console_write is registered for printk output.
1875 */
1876
1877void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1878{
1879 if (!mcfrs_console_inited)
1880 mcfrs_init_console();
1881 while (len-- > 0) {
1882 if (*p == '\n')
1883 mcfrs_put_char('\r');
1884 mcfrs_put_char(*p++);
1885 }
1886}
1887
1888/*
1889 * declare our consoles
1890 */
1891
1892struct console mcfrs_console = {
1893 .name = "ttyS",
1894 .write = mcfrs_console_write,
1895 .device = mcfrs_console_device,
1896 .setup = mcfrs_console_setup,
1897 .flags = CON_PRINTBUFFER,
1898 .index = -1,
1899};
1900
1901static int __init mcfrs_console_init(void)
1902{
1903 register_console(&mcfrs_console);
1904 return 0;
1905}
1906
1907console_initcall(mcfrs_console_init);
1908
1909/****************************************************************************/