MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / serial / mcfserial.c
blobe0ca6edf35bb1df9201f5432a0f9c22ac568cde0
1 /*
2 * mcfserial.c -- serial driver for ColdFire internal UARTS.
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>
8 * Based on code from 68332serial.c which was:
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>
14 * Changes:
15 * 08/07/2003 Daniele Bellucci <bellucda@tiscali.it>
16 * some cleanups in mcfrs_write.
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>
38 #include <asm/io.h>
39 #include <asm/irq.h>
40 #include <asm/system.h>
41 #include <asm/segment.h>
42 #include <asm/semaphore.h>
43 #include <asm/bitops.h>
44 #include <asm/coldfire.h>
45 #include <asm/mcfsim.h>
46 #include <asm/mcfuart.h>
47 #include <asm/nettel.h>
48 #include <asm/uaccess.h>
49 #include "mcfserial.h"
51 struct timer_list mcfrs_timer_struct;
54 * Default console baud rate, we use this as the default
55 * for all ports so init can just open /dev/console and
56 * keep going. Perhaps one day the cflag settings for the
57 * console can be used instead.
59 #if defined(CONFIG_ARNEWSH) || defined(CONFIG_MOTOROLA) || defined(CONFIG_senTec) || defined(CONFIG_SNEHA)
60 #define CONSOLE_BAUD_RATE 19200
61 #define DEFAULT_CBAUD B19200
62 #endif
64 #if defined(CONFIG_HW_FEITH)
65 #define CONSOLE_BAUD_RATE 38400
66 #define DEFAULT_CBAUD B38400
67 #endif
69 #ifndef CONSOLE_BAUD_RATE
70 #define CONSOLE_BAUD_RATE 9600
71 #define DEFAULT_CBAUD B9600
72 #endif
74 int mcfrs_console_inited = 0;
75 int mcfrs_console_port = -1;
76 int mcfrs_console_baud = CONSOLE_BAUD_RATE;
77 int mcfrs_console_cbaud = DEFAULT_CBAUD;
80 * Driver data structures.
82 static struct tty_driver *mcfrs_serial_driver;
84 /* number of characters left in xmit buffer before we ask for more */
85 #define WAKEUP_CHARS 256
87 /* Debugging...
89 #undef SERIAL_DEBUG_OPEN
90 #undef SERIAL_DEBUG_FLOW
92 #if defined(CONFIG_M527x) || defined(CONFIG_M528x)
93 #define IRQBASE (MCFINT_VECBASE+MCFINT_UART0)
94 #else
95 #define IRQBASE 73
96 #endif
99 * Configuration table, UARTs to look for at startup.
101 static struct mcf_serial mcfrs_table[] = {
102 { /* ttyS0 */
103 .magic = 0,
104 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE1),
105 .irq = IRQBASE,
106 .flags = ASYNC_BOOT_AUTOCONF,
108 { /* ttyS1 */
109 .magic = 0,
110 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE2),
111 .irq = IRQBASE+1,
112 .flags = ASYNC_BOOT_AUTOCONF,
117 #define NR_PORTS (sizeof(mcfrs_table) / sizeof(struct mcf_serial))
120 * This is used to figure out the divisor speeds and the timeouts.
122 static int mcfrs_baud_table[] = {
123 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
124 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0
126 #define MCFRS_BAUD_TABLE_SIZE \
127 (sizeof(mcfrs_baud_table)/sizeof(mcfrs_baud_table[0]))
130 #ifdef CONFIG_MAGIC_SYSRQ
132 * Magic system request keys. Used for debugging...
134 extern int magic_sysrq_key(int ch);
135 #endif
139 * tmp_buf is used as a temporary buffer by serial_write. We need to
140 * lock it in case the copy_from_user blocks while swapping in a page,
141 * and some other program tries to do a serial write at the same time.
142 * Since the lock will only come under contention when the system is
143 * swapping and available memory is low, it makes sense to share one
144 * buffer across all the serial ports, since it significantly saves
145 * memory if large numbers of serial ports are open.
147 static unsigned char mcfrs_tmp_buf[4096]; /* This is cheating */
148 static DECLARE_MUTEX(mcfrs_tmp_buf_sem);
151 * Forware declarations...
153 static void mcfrs_change_speed(struct mcf_serial *info);
154 static void mcfrs_wait_until_sent(struct tty_struct *tty, int timeout);
157 static inline int serial_paranoia_check(struct mcf_serial *info,
158 char *name, const char *routine)
160 #ifdef SERIAL_PARANOIA_CHECK
161 static const char badmagic[] =
162 "MCFRS(warning): bad magic number for serial struct %s in %s\n";
163 static const char badinfo[] =
164 "MCFRS(warning): null mcf_serial for %s in %s\n";
166 if (!info) {
167 printk(badinfo, name, routine);
168 return 1;
170 if (info->magic != SERIAL_MAGIC) {
171 printk(badmagic, name, routine);
172 return 1;
174 #endif
175 return 0;
179 * Sets or clears DTR and RTS on the requested line.
181 static void mcfrs_setsignals(struct mcf_serial *info, int dtr, int rts)
183 volatile unsigned char *uartp;
184 unsigned long flags;
186 #if 0
187 printk("%s(%d): mcfrs_setsignals(info=%x,dtr=%d,rts=%d)\n",
188 __FILE__, __LINE__, info, dtr, rts);
189 #endif
191 local_irq_save(flags);
192 if (dtr >= 0) {
193 #ifdef MCFPP_DTR0
194 if (info->line)
195 mcf_setppdata(MCFPP_DTR1, (dtr ? 0 : MCFPP_DTR1));
196 else
197 mcf_setppdata(MCFPP_DTR0, (dtr ? 0 : MCFPP_DTR0));
198 #endif
200 if (rts >= 0) {
201 uartp = info->addr;
202 if (rts) {
203 info->sigs |= TIOCM_RTS;
204 uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
205 } else {
206 info->sigs &= ~TIOCM_RTS;
207 uartp[MCFUART_UOP0] = MCFUART_UOP_RTS;
210 local_irq_restore(flags);
211 return;
215 * Gets values of serial signals.
217 static int mcfrs_getsignals(struct mcf_serial *info)
219 volatile unsigned char *uartp;
220 unsigned long flags;
221 int sigs;
222 #if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
223 unsigned short ppdata;
224 #endif
226 #if 0
227 printk("%s(%d): mcfrs_getsignals(info=%x)\n", __FILE__, __LINE__);
228 #endif
230 local_irq_save(flags);
231 uartp = info->addr;
232 sigs = (uartp[MCFUART_UIPR] & MCFUART_UIPR_CTS) ? 0 : TIOCM_CTS;
233 sigs |= (info->sigs & TIOCM_RTS);
235 #ifdef MCFPP_DCD0
237 unsigned int ppdata;
238 ppdata = mcf_getppdata();
239 if (info->line == 0) {
240 sigs |= (ppdata & MCFPP_DCD0) ? 0 : TIOCM_CD;
241 sigs |= (ppdata & MCFPP_DTR0) ? 0 : TIOCM_DTR;
242 } else if (info->line == 1) {
243 sigs |= (ppdata & MCFPP_DCD1) ? 0 : TIOCM_CD;
244 sigs |= (ppdata & MCFPP_DTR1) ? 0 : TIOCM_DTR;
247 #endif
249 local_irq_restore(flags);
250 return(sigs);
254 * ------------------------------------------------------------
255 * mcfrs_stop() and mcfrs_start()
257 * This routines are called before setting or resetting tty->stopped.
258 * They enable or disable transmitter interrupts, as necessary.
259 * ------------------------------------------------------------
261 static void mcfrs_stop(struct tty_struct *tty)
263 volatile unsigned char *uartp;
264 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
265 unsigned long flags;
267 if (serial_paranoia_check(info, tty->name, "mcfrs_stop"))
268 return;
270 local_irq_save(flags);
271 uartp = info->addr;
272 info->imr &= ~MCFUART_UIR_TXREADY;
273 uartp[MCFUART_UIMR] = info->imr;
274 local_irq_restore(flags);
277 static void mcfrs_start(struct tty_struct *tty)
279 volatile unsigned char *uartp;
280 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
281 unsigned long flags;
283 if (serial_paranoia_check(info, tty->name, "mcfrs_start"))
284 return;
286 local_irq_save(flags);
287 if (info->xmit_cnt && info->xmit_buf) {
288 uartp = info->addr;
289 info->imr |= MCFUART_UIR_TXREADY;
290 uartp[MCFUART_UIMR] = info->imr;
292 local_irq_restore(flags);
296 * ----------------------------------------------------------------------
298 * Here starts the interrupt handling routines. All of the following
299 * subroutines are declared as inline and are folded into
300 * mcfrs_interrupt(). They were separated out for readability's sake.
302 * Note: mcfrs_interrupt() is a "fast" interrupt, which means that it
303 * runs with interrupts turned off. People who may want to modify
304 * mcfrs_interrupt() should try to keep the interrupt handler as fast as
305 * possible. After you are done making modifications, it is not a bad
306 * idea to do:
308 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
310 * and look at the resulting assemble code in serial.s.
312 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
313 * -----------------------------------------------------------------------
316 static inline void receive_chars(struct mcf_serial *info)
318 volatile unsigned char *uartp;
319 struct tty_struct *tty = info->tty;
320 unsigned char status, ch;
322 if (!tty)
323 return;
325 uartp = info->addr;
327 while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
329 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
330 break;
332 ch = uartp[MCFUART_URB];
333 info->stats.rx++;
335 #ifdef CONFIG_MAGIC_SYSRQ
336 if (mcfrs_console_inited && (info->line == mcfrs_console_port)) {
337 if (magic_sysrq_key(ch))
338 continue;
340 #endif
342 tty->flip.count++;
343 if (status & MCFUART_USR_RXERR) {
344 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
345 if (status & MCFUART_USR_RXBREAK) {
346 info->stats.rxbreak++;
347 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
348 } else if (status & MCFUART_USR_RXPARITY) {
349 info->stats.rxparity++;
350 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
351 } else if (status & MCFUART_USR_RXOVERRUN) {
352 info->stats.rxoverrun++;
353 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
354 } else if (status & MCFUART_USR_RXFRAMING) {
355 info->stats.rxframing++;
356 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
357 } else {
358 /* This should never happen... */
359 *tty->flip.flag_buf_ptr++ = 0;
361 } else {
362 *tty->flip.flag_buf_ptr++ = 0;
364 *tty->flip.char_buf_ptr++ = ch;
367 schedule_work(&tty->flip.work);
368 return;
371 static inline void transmit_chars(struct mcf_serial *info)
373 volatile unsigned char *uartp;
375 uartp = info->addr;
377 if (info->x_char) {
378 /* Send special char - probably flow control */
379 uartp[MCFUART_UTB] = info->x_char;
380 info->x_char = 0;
381 info->stats.tx++;
384 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
385 info->imr &= ~MCFUART_UIR_TXREADY;
386 uartp[MCFUART_UIMR] = info->imr;
387 return;
390 while (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) {
391 uartp[MCFUART_UTB] = info->xmit_buf[info->xmit_tail++];
392 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
393 info->stats.tx++;
394 if (--info->xmit_cnt <= 0)
395 break;
398 if (info->xmit_cnt < WAKEUP_CHARS)
399 schedule_work(&info->tqueue);
400 return;
404 * This is the serial driver's generic interrupt routine
406 irqreturn_t mcfrs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
408 struct mcf_serial *info;
409 unsigned char isr;
411 info = &mcfrs_table[(irq - IRQBASE)];
412 isr = info->addr[MCFUART_UISR] & info->imr;
414 if (isr & MCFUART_UIR_RXREADY)
415 receive_chars(info);
416 if (isr & MCFUART_UIR_TXREADY)
417 transmit_chars(info);
418 return IRQ_HANDLED;
422 * -------------------------------------------------------------------
423 * Here ends the serial interrupt routines.
424 * -------------------------------------------------------------------
427 static void mcfrs_offintr(void *private)
429 struct mcf_serial *info = (struct mcf_serial *) private;
430 struct tty_struct *tty;
432 tty = info->tty;
433 if (!tty)
434 return;
435 tty_wakeup(tty);
440 * Change of state on a DCD line.
442 void mcfrs_modem_change(struct mcf_serial *info, int dcd)
444 if (info->count == 0)
445 return;
447 if (info->flags & ASYNC_CHECK_CD) {
448 if (dcd)
449 wake_up_interruptible(&info->open_wait);
450 else
451 schedule_work(&info->tqueue_hangup);
456 #ifdef MCFPP_DCD0
458 unsigned short mcfrs_ppstatus;
461 * This subroutine is called when the RS_TIMER goes off. It is used
462 * to monitor the state of the DCD lines - since they have no edge
463 * sensors and interrupt generators.
465 static void mcfrs_timer(void)
467 unsigned int ppstatus, dcdval, i;
469 ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
471 if (ppstatus != mcfrs_ppstatus) {
472 for (i = 0; (i < 2); i++) {
473 dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
474 if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
475 mcfrs_modem_change(&mcfrs_table[i],
476 ((ppstatus & dcdval) ? 0 : 1));
480 mcfrs_ppstatus = ppstatus;
482 /* Re-arm timer */
483 mcfrs_timer_struct.expires = jiffies + HZ/25;
484 add_timer(&mcfrs_timer_struct);
487 #endif /* MCFPP_DCD0 */
491 * This routine is called from the scheduler tqueue when the interrupt
492 * routine has signalled that a hangup has occurred. The path of
493 * hangup processing is:
495 * serial interrupt routine -> (scheduler tqueue) ->
496 * do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
499 static void do_serial_hangup(void *private)
501 struct mcf_serial *info = (struct mcf_serial *) private;
502 struct tty_struct *tty;
504 tty = info->tty;
505 if (!tty)
506 return;
508 tty_hangup(tty);
511 static int startup(struct mcf_serial * info)
513 volatile unsigned char *uartp;
514 unsigned long flags;
516 if (info->flags & ASYNC_INITIALIZED)
517 return 0;
519 if (!info->xmit_buf) {
520 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
521 if (!info->xmit_buf)
522 return -ENOMEM;
525 local_irq_save(flags);
527 #ifdef SERIAL_DEBUG_OPEN
528 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
529 #endif
532 * Reset UART, get it into known state...
534 uartp = info->addr;
535 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
536 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
537 mcfrs_setsignals(info, 1, 1);
539 if (info->tty)
540 clear_bit(TTY_IO_ERROR, &info->tty->flags);
541 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
544 * and set the speed of the serial port
546 mcfrs_change_speed(info);
549 * Lastly enable the UART transmitter and receiver, and
550 * interrupt enables.
552 info->imr = MCFUART_UIR_RXREADY;
553 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
554 uartp[MCFUART_UIMR] = info->imr;
556 info->flags |= ASYNC_INITIALIZED;
557 local_irq_restore(flags);
558 return 0;
562 * This routine will shutdown a serial port; interrupts are disabled, and
563 * DTR is dropped if the hangup on close termio flag is on.
565 static void shutdown(struct mcf_serial * info)
567 volatile unsigned char *uartp;
568 unsigned long flags;
570 if (!(info->flags & ASYNC_INITIALIZED))
571 return;
573 #ifdef SERIAL_DEBUG_OPEN
574 printk("Shutting down serial port %d (irq %d)....\n", info->line,
575 info->irq);
576 #endif
578 local_irq_save(flags);
580 uartp = info->addr;
581 uartp[MCFUART_UIMR] = 0; /* mask all interrupts */
582 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
583 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
585 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
586 mcfrs_setsignals(info, 0, 0);
588 if (info->xmit_buf) {
589 free_page((unsigned long) info->xmit_buf);
590 info->xmit_buf = 0;
593 if (info->tty)
594 set_bit(TTY_IO_ERROR, &info->tty->flags);
596 info->flags &= ~ASYNC_INITIALIZED;
597 local_irq_restore(flags);
602 * This routine is called to set the UART divisor registers to match
603 * the specified baud rate for a serial port.
605 static void mcfrs_change_speed(struct mcf_serial *info)
607 volatile unsigned char *uartp;
608 unsigned int baudclk, cflag;
609 unsigned long flags;
610 unsigned char mr1, mr2;
611 int i;
612 #ifdef CONFIG_M5272
613 unsigned int fraction;
614 #endif
616 if (!info->tty || !info->tty->termios)
617 return;
618 cflag = info->tty->termios->c_cflag;
619 if (info->addr == 0)
620 return;
622 #if 0
623 printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
624 #endif
626 i = cflag & CBAUD;
627 if (i & CBAUDEX) {
628 i &= ~CBAUDEX;
629 if (i < 1 || i > 4)
630 info->tty->termios->c_cflag &= ~CBAUDEX;
631 else
632 i += 15;
634 if (i == 0) {
635 mcfrs_setsignals(info, 0, -1);
636 return;
639 /* compute the baudrate clock */
640 #ifdef CONFIG_M5272
642 * For the MCF5272, also compute the baudrate fraction.
644 baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
645 fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
646 fraction *= 16;
647 fraction /= (32 * mcfrs_baud_table[i]);
648 #else
649 baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
650 #endif
652 info->baud = mcfrs_baud_table[i];
654 mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
655 mr2 = 0;
657 switch (cflag & CSIZE) {
658 case CS5: mr1 |= MCFUART_MR1_CS5; break;
659 case CS6: mr1 |= MCFUART_MR1_CS6; break;
660 case CS7: mr1 |= MCFUART_MR1_CS7; break;
661 case CS8:
662 default: mr1 |= MCFUART_MR1_CS8; break;
665 if (cflag & PARENB) {
666 if (cflag & CMSPAR) {
667 if (cflag & PARODD)
668 mr1 |= MCFUART_MR1_PARITYMARK;
669 else
670 mr1 |= MCFUART_MR1_PARITYSPACE;
671 } else {
672 if (cflag & PARODD)
673 mr1 |= MCFUART_MR1_PARITYODD;
674 else
675 mr1 |= MCFUART_MR1_PARITYEVEN;
677 } else {
678 mr1 |= MCFUART_MR1_PARITYNONE;
681 if (cflag & CSTOPB)
682 mr2 |= MCFUART_MR2_STOP2;
683 else
684 mr2 |= MCFUART_MR2_STOP1;
686 if (cflag & CRTSCTS) {
687 mr1 |= MCFUART_MR1_RXRTS;
688 mr2 |= MCFUART_MR2_TXCTS;
691 if (cflag & CLOCAL)
692 info->flags &= ~ASYNC_CHECK_CD;
693 else
694 info->flags |= ASYNC_CHECK_CD;
696 uartp = info->addr;
698 local_irq_save(flags);
699 #if 0
700 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
701 mr1, mr2, baudclk);
702 #endif
704 Note: pg 12-16 of MCF5206e User's Manual states that a
705 software reset should be performed prior to changing
706 UMR1,2, UCSR, UACR, bit 7
708 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
709 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
710 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
711 uartp[MCFUART_UMR] = mr1;
712 uartp[MCFUART_UMR] = mr2;
713 uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8; /* set msb byte */
714 uartp[MCFUART_UBG2] = (baudclk & 0xff); /* set lsb byte */
715 #ifdef CONFIG_M5272
716 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
717 #endif
718 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
719 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
720 mcfrs_setsignals(info, 1, -1);
721 local_irq_restore(flags);
722 return;
725 static void mcfrs_flush_chars(struct tty_struct *tty)
727 volatile unsigned char *uartp;
728 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
729 unsigned long flags;
731 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
732 return;
734 uartp = (volatile unsigned char *) info->addr;
737 * re-enable receiver interrupt
739 local_irq_save(flags);
740 if ((!(info->imr & MCFUART_UIR_RXREADY)) &&
741 (info->flags & ASYNC_INITIALIZED) ) {
742 info->imr |= MCFUART_UIR_RXREADY;
743 uartp[MCFUART_UIMR] = info->imr;
745 local_irq_restore(flags);
747 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
748 !info->xmit_buf)
749 return;
751 /* Enable transmitter */
752 local_irq_save(flags);
753 info->imr |= MCFUART_UIR_TXREADY;
754 uartp[MCFUART_UIMR] = info->imr;
755 local_irq_restore(flags);
758 static int mcfrs_write(struct tty_struct * tty, int from_user,
759 const unsigned char *buf, int count)
761 volatile unsigned char *uartp;
762 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
763 unsigned long flags;
764 int c, total = 0;
766 #if 0
767 printk("%s(%d): mcfrs_write(tty=%x,from_user=%d,buf=%x,count=%d)\n",
768 __FILE__, __LINE__, (int)tty, from_user, (int)buf, count);
769 #endif
771 if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
772 return 0;
774 if (!tty || !info->xmit_buf)
775 return 0;
777 local_save_flags(flags);
778 while (1) {
779 local_irq_disable();
780 c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
781 ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
782 local_irq_restore(flags);
784 if (c <= 0)
785 break;
787 if (from_user) {
788 down(&mcfrs_tmp_buf_sem);
789 if (copy_from_user(mcfrs_tmp_buf, buf, c))
790 return -EFAULT;
792 local_irq_disable();
793 c = min(c, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
794 ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
795 local_irq_restore(flags);
796 memcpy(info->xmit_buf + info->xmit_head, mcfrs_tmp_buf, c);
797 up(&mcfrs_tmp_buf_sem);
798 } else
799 memcpy(info->xmit_buf + info->xmit_head, buf, c);
801 local_irq_disable();
802 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
803 info->xmit_cnt += c;
804 local_irq_restore(flags);
806 buf += c;
807 count -= c;
808 total += c;
811 local_irq_disable();
812 uartp = info->addr;
813 info->imr |= MCFUART_UIR_TXREADY;
814 uartp[MCFUART_UIMR] = info->imr;
815 local_irq_restore(flags);
817 return total;
820 static int mcfrs_write_room(struct tty_struct *tty)
822 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
823 int ret;
825 if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
826 return 0;
827 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
828 if (ret < 0)
829 ret = 0;
830 return ret;
833 static int mcfrs_chars_in_buffer(struct tty_struct *tty)
835 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
837 if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
838 return 0;
839 return info->xmit_cnt;
842 static void mcfrs_flush_buffer(struct tty_struct *tty)
844 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
845 unsigned long flags;
847 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
848 return;
850 local_irq_save(flags);
851 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
852 local_irq_restore(flags);
854 tty_wakeup(tty);
858 * ------------------------------------------------------------
859 * mcfrs_throttle()
861 * This routine is called by the upper-layer tty layer to signal that
862 * incoming characters should be throttled.
863 * ------------------------------------------------------------
865 static void mcfrs_throttle(struct tty_struct * tty)
867 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
868 #ifdef SERIAL_DEBUG_THROTTLE
869 char buf[64];
871 printk("throttle %s: %d....\n", _tty_name(tty, buf),
872 tty->ldisc.chars_in_buffer(tty));
873 #endif
875 if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
876 return;
878 if (I_IXOFF(tty))
879 info->x_char = STOP_CHAR(tty);
881 /* Turn off RTS line (do this atomic) */
884 static void mcfrs_unthrottle(struct tty_struct * tty)
886 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
887 #ifdef SERIAL_DEBUG_THROTTLE
888 char buf[64];
890 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
891 tty->ldisc.chars_in_buffer(tty));
892 #endif
894 if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
895 return;
897 if (I_IXOFF(tty)) {
898 if (info->x_char)
899 info->x_char = 0;
900 else
901 info->x_char = START_CHAR(tty);
904 /* Assert RTS line (do this atomic) */
908 * ------------------------------------------------------------
909 * mcfrs_ioctl() and friends
910 * ------------------------------------------------------------
913 static int get_serial_info(struct mcf_serial * info,
914 struct serial_struct * retinfo)
916 struct serial_struct tmp;
918 if (!retinfo)
919 return -EFAULT;
920 memset(&tmp, 0, sizeof(tmp));
921 tmp.type = info->type;
922 tmp.line = info->line;
923 tmp.port = (unsigned int) info->addr;
924 tmp.irq = info->irq;
925 tmp.flags = info->flags;
926 tmp.baud_base = info->baud_base;
927 tmp.close_delay = info->close_delay;
928 tmp.closing_wait = info->closing_wait;
929 tmp.custom_divisor = info->custom_divisor;
930 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
933 static int set_serial_info(struct mcf_serial * info,
934 struct serial_struct * new_info)
936 struct serial_struct new_serial;
937 struct mcf_serial old_info;
938 int retval = 0;
940 if (!new_info)
941 return -EFAULT;
942 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
943 return -EFAULT;
944 old_info = *info;
946 if (!capable(CAP_SYS_ADMIN)) {
947 if ((new_serial.baud_base != info->baud_base) ||
948 (new_serial.type != info->type) ||
949 (new_serial.close_delay != info->close_delay) ||
950 ((new_serial.flags & ~ASYNC_USR_MASK) !=
951 (info->flags & ~ASYNC_USR_MASK)))
952 return -EPERM;
953 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
954 (new_serial.flags & ASYNC_USR_MASK));
955 info->custom_divisor = new_serial.custom_divisor;
956 goto check_and_exit;
959 if (info->count > 1)
960 return -EBUSY;
963 * OK, past this point, all the error checking has been done.
964 * At this point, we start making changes.....
967 info->baud_base = new_serial.baud_base;
968 info->flags = ((info->flags & ~ASYNC_FLAGS) |
969 (new_serial.flags & ASYNC_FLAGS));
970 info->type = new_serial.type;
971 info->close_delay = new_serial.close_delay;
972 info->closing_wait = new_serial.closing_wait;
974 check_and_exit:
975 retval = startup(info);
976 return retval;
980 * get_lsr_info - get line status register info
982 * Purpose: Let user call ioctl() to get info when the UART physically
983 * is emptied. On bus types like RS485, the transmitter must
984 * release the bus after transmitting. This must be done when
985 * the transmit shift register is empty, not be done when the
986 * transmit holding register is empty. This functionality
987 * allows an RS485 driver to be written in user space.
989 static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
991 volatile unsigned char *uartp;
992 unsigned long flags;
993 unsigned char status;
995 local_irq_save(flags);
996 uartp = info->addr;
997 status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
998 local_irq_restore(flags);
1000 return put_user(status,value);
1004 * This routine sends a break character out the serial port.
1006 static void send_break( struct mcf_serial * info, int duration)
1008 volatile unsigned char *uartp;
1009 unsigned long flags;
1011 if (!info->addr)
1012 return;
1013 current->state = TASK_INTERRUPTIBLE;
1014 uartp = info->addr;
1016 local_irq_save(flags);
1017 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
1018 schedule_timeout(duration);
1019 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
1020 local_irq_restore(flags);
1023 static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
1025 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1027 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1028 return -ENODEV;
1029 if (tty->flags & (1 << TTY_IO_ERROR))
1030 return -EIO;
1032 return mcfrs_getsignals(info);
1035 static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
1036 unsigned int set, unsigned int clear)
1038 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1039 int rts = -1, dtr = -1;
1041 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1042 return -ENODEV;
1043 if (tty->flags & (1 << TTY_IO_ERROR))
1044 return -EIO;
1046 if (set & TIOCM_RTS)
1047 rts = 1;
1048 if (set & TIOCM_DTR)
1049 dtr = 1;
1050 if (clear & TIOCM_RTS)
1051 rts = 0;
1052 if (clear & TIOCM_DTR)
1053 dtr = 0;
1055 mcfrs_setsignals(info, dtr, rts);
1057 return 0;
1060 static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
1061 unsigned int cmd, unsigned long arg)
1063 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1064 int retval, error;
1066 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1067 return -ENODEV;
1069 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1070 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1071 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1072 if (tty->flags & (1 << TTY_IO_ERROR))
1073 return -EIO;
1076 switch (cmd) {
1077 case TCSBRK: /* SVID version: non-zero arg --> no break */
1078 retval = tty_check_change(tty);
1079 if (retval)
1080 return retval;
1081 tty_wait_until_sent(tty, 0);
1082 if (!arg)
1083 send_break(info, HZ/4); /* 1/4 second */
1084 return 0;
1085 case TCSBRKP: /* support for POSIX tcsendbreak() */
1086 retval = tty_check_change(tty);
1087 if (retval)
1088 return retval;
1089 tty_wait_until_sent(tty, 0);
1090 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1091 return 0;
1092 case TIOCGSOFTCAR:
1093 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1094 (unsigned long *) arg);
1095 if (error)
1096 return error;
1097 return 0;
1098 case TIOCSSOFTCAR:
1099 get_user(arg, (unsigned long *) arg);
1100 tty->termios->c_cflag =
1101 ((tty->termios->c_cflag & ~CLOCAL) |
1102 (arg ? CLOCAL : 0));
1103 return 0;
1104 case TIOCGSERIAL:
1105 error = verify_area(VERIFY_WRITE, (void *) arg,
1106 sizeof(struct serial_struct));
1107 if (error)
1108 return error;
1109 return get_serial_info(info,
1110 (struct serial_struct *) arg);
1111 case TIOCSSERIAL:
1112 return set_serial_info(info,
1113 (struct serial_struct *) arg);
1114 case TIOCSERGETLSR: /* Get line status register */
1115 error = verify_area(VERIFY_WRITE, (void *) arg,
1116 sizeof(unsigned int));
1117 if (error)
1118 return error;
1119 else
1120 return get_lsr_info(info, (unsigned int *) arg);
1122 case TIOCSERGSTRUCT:
1123 error = copy_to_user((struct mcf_serial *) arg,
1124 info, sizeof(struct mcf_serial));
1125 if (error)
1126 return -EFAULT;
1127 return 0;
1129 #ifdef TIOCSET422
1130 case TIOCSET422: {
1131 unsigned int val;
1132 get_user(val, (unsigned int *) arg);
1133 mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1134 break;
1136 case TIOCGET422: {
1137 unsigned int val;
1138 val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1139 put_user(val, (unsigned int *) arg);
1140 break;
1142 #endif
1144 default:
1145 return -ENOIOCTLCMD;
1147 return 0;
1150 static void mcfrs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1152 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1154 if (tty->termios->c_cflag == old_termios->c_cflag)
1155 return;
1157 mcfrs_change_speed(info);
1159 if ((old_termios->c_cflag & CRTSCTS) &&
1160 !(tty->termios->c_cflag & CRTSCTS)) {
1161 tty->hw_stopped = 0;
1162 mcfrs_setsignals(info, -1, 1);
1163 #if 0
1164 mcfrs_start(tty);
1165 #endif
1170 * ------------------------------------------------------------
1171 * mcfrs_close()
1173 * This routine is called when the serial port gets closed. First, we
1174 * wait for the last remaining data to be sent. Then, we unlink its
1175 * S structure from the interrupt chain if necessary, and we free
1176 * that IRQ if nothing is left in the chain.
1177 * ------------------------------------------------------------
1179 static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1181 volatile unsigned char *uartp;
1182 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1183 unsigned long flags;
1185 if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1186 return;
1188 local_irq_save(flags);
1190 if (tty_hung_up_p(filp)) {
1191 local_irq_restore(flags);
1192 return;
1195 #ifdef SERIAL_DEBUG_OPEN
1196 printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1197 #endif
1198 if ((tty->count == 1) && (info->count != 1)) {
1200 * Uh, oh. tty->count is 1, which means that the tty
1201 * structure will be freed. Info->count should always
1202 * be one in these conditions. If it's greater than
1203 * one, we've got real problems, since it means the
1204 * serial port won't be shutdown.
1206 printk("MCFRS: bad serial port count; tty->count is 1, "
1207 "info->count is %d\n", info->count);
1208 info->count = 1;
1210 if (--info->count < 0) {
1211 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1212 info->line, info->count);
1213 info->count = 0;
1215 if (info->count) {
1216 local_irq_restore(flags);
1217 return;
1219 info->flags |= ASYNC_CLOSING;
1222 * Now we wait for the transmit buffer to clear; and we notify
1223 * the line discipline to only process XON/XOFF characters.
1225 tty->closing = 1;
1226 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1227 tty_wait_until_sent(tty, info->closing_wait);
1230 * At this point we stop accepting input. To do this, we
1231 * disable the receive line status interrupts, and tell the
1232 * interrupt driver to stop checking the data ready bit in the
1233 * line status register.
1235 info->imr &= ~MCFUART_UIR_RXREADY;
1236 uartp = info->addr;
1237 uartp[MCFUART_UIMR] = info->imr;
1239 #if 0
1240 /* FIXME: do we need to keep this enabled for console?? */
1241 if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
1242 /* Do not disable the UART */ ;
1243 } else
1244 #endif
1245 shutdown(info);
1246 if (tty->driver->flush_buffer)
1247 tty->driver->flush_buffer(tty);
1248 tty_ldisc_flush(tty);
1250 tty->closing = 0;
1251 info->event = 0;
1252 info->tty = 0;
1253 #if 0
1254 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1255 if (tty->ldisc.close)
1256 (tty->ldisc.close)(tty);
1257 tty->ldisc = ldiscs[N_TTY];
1258 tty->termios->c_line = N_TTY;
1259 if (tty->ldisc.open)
1260 (tty->ldisc.open)(tty);
1262 #endif
1263 if (info->blocked_open) {
1264 if (info->close_delay) {
1265 current->state = TASK_INTERRUPTIBLE;
1266 schedule_timeout(info->close_delay);
1268 wake_up_interruptible(&info->open_wait);
1270 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1271 wake_up_interruptible(&info->close_wait);
1272 local_irq_restore(flags);
1276 * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1278 static void
1279 mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1281 #ifdef CONFIG_M5272
1282 #define MCF5272_FIFO_SIZE 25 /* fifo size + shift reg */
1284 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1285 volatile unsigned char *uartp;
1286 unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1288 if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1289 return;
1291 orig_jiffies = jiffies;
1294 * Set the check interval to be 1/5 of the approximate time
1295 * to send the entire fifo, and make it at least 1. The check
1296 * interval should also be less than the timeout.
1298 * Note: we have to use pretty tight timings here to satisfy
1299 * the NIST-PCTS.
1301 fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1302 char_time = fifo_time / 5;
1303 if (char_time == 0)
1304 char_time = 1;
1305 if (timeout && timeout < char_time)
1306 char_time = timeout;
1309 * Clamp the timeout period at 2 * the time to empty the
1310 * fifo. Just to be safe, set the minimum at .5 seconds.
1312 fifo_time *= 2;
1313 if (fifo_time < (HZ/2))
1314 fifo_time = HZ/2;
1315 if (!timeout || timeout > fifo_time)
1316 timeout = fifo_time;
1319 * Account for the number of bytes in the UART
1320 * transmitter FIFO plus any byte being shifted out.
1322 uartp = (volatile unsigned char *) info->addr;
1323 for (;;) {
1324 fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1325 if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1326 MCFUART_USR_TXEMPTY)) ==
1327 MCFUART_USR_TXREADY)
1328 fifo_cnt++;
1329 if (fifo_cnt == 0)
1330 break;
1331 set_current_state(TASK_INTERRUPTIBLE);
1332 schedule_timeout(char_time);
1333 if (signal_pending(current))
1334 break;
1335 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1336 break;
1338 #else
1340 * For the other coldfire models, assume all data has been sent
1342 #endif
1346 * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1348 void mcfrs_hangup(struct tty_struct *tty)
1350 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1352 if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
1353 return;
1355 mcfrs_flush_buffer(tty);
1356 shutdown(info);
1357 info->event = 0;
1358 info->count = 0;
1359 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1360 info->tty = 0;
1361 wake_up_interruptible(&info->open_wait);
1365 * ------------------------------------------------------------
1366 * mcfrs_open() and friends
1367 * ------------------------------------------------------------
1369 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1370 struct mcf_serial *info)
1372 DECLARE_WAITQUEUE(wait, current);
1373 int retval;
1374 int do_clocal = 0;
1377 * If the device is in the middle of being closed, then block
1378 * until it's done, and then try again.
1380 if (info->flags & ASYNC_CLOSING) {
1381 interruptible_sleep_on(&info->close_wait);
1382 #ifdef SERIAL_DO_RESTART
1383 if (info->flags & ASYNC_HUP_NOTIFY)
1384 return -EAGAIN;
1385 else
1386 return -ERESTARTSYS;
1387 #else
1388 return -EAGAIN;
1389 #endif
1393 * If non-blocking mode is set, or the port is not enabled,
1394 * then make the check up front and then exit.
1396 if ((filp->f_flags & O_NONBLOCK) ||
1397 (tty->flags & (1 << TTY_IO_ERROR))) {
1398 info->flags |= ASYNC_NORMAL_ACTIVE;
1399 return 0;
1402 if (tty->termios->c_cflag & CLOCAL)
1403 do_clocal = 1;
1406 * Block waiting for the carrier detect and the line to become
1407 * free (i.e., not in use by the callout). While we are in
1408 * this loop, info->count is dropped by one, so that
1409 * mcfrs_close() knows when to free things. We restore it upon
1410 * exit, either normal or abnormal.
1412 retval = 0;
1413 add_wait_queue(&info->open_wait, &wait);
1414 #ifdef SERIAL_DEBUG_OPEN
1415 printk("block_til_ready before block: ttyS%d, count = %d\n",
1416 info->line, info->count);
1417 #endif
1418 info->count--;
1419 info->blocked_open++;
1420 while (1) {
1421 local_irq_disable();
1422 mcfrs_setsignals(info, 1, 1);
1423 local_irq_enable();
1424 current->state = TASK_INTERRUPTIBLE;
1425 if (tty_hung_up_p(filp) ||
1426 !(info->flags & ASYNC_INITIALIZED)) {
1427 #ifdef SERIAL_DO_RESTART
1428 if (info->flags & ASYNC_HUP_NOTIFY)
1429 retval = -EAGAIN;
1430 else
1431 retval = -ERESTARTSYS;
1432 #else
1433 retval = -EAGAIN;
1434 #endif
1435 break;
1437 if (!(info->flags & ASYNC_CLOSING) &&
1438 (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1439 break;
1440 if (signal_pending(current)) {
1441 retval = -ERESTARTSYS;
1442 break;
1444 #ifdef SERIAL_DEBUG_OPEN
1445 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1446 info->line, info->count);
1447 #endif
1448 schedule();
1450 current->state = TASK_RUNNING;
1451 remove_wait_queue(&info->open_wait, &wait);
1452 if (!tty_hung_up_p(filp))
1453 info->count++;
1454 info->blocked_open--;
1455 #ifdef SERIAL_DEBUG_OPEN
1456 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1457 info->line, info->count);
1458 #endif
1459 if (retval)
1460 return retval;
1461 info->flags |= ASYNC_NORMAL_ACTIVE;
1462 return 0;
1466 * This routine is called whenever a serial port is opened. It
1467 * enables interrupts for a serial port, linking in its structure into
1468 * the IRQ chain. It also performs the serial-specific
1469 * initialization for the tty structure.
1471 int mcfrs_open(struct tty_struct *tty, struct file * filp)
1473 struct mcf_serial *info;
1474 int retval, line;
1476 line = tty->index;
1477 if ((line < 0) || (line >= NR_PORTS))
1478 return -ENODEV;
1479 info = mcfrs_table + line;
1480 if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1481 return -ENODEV;
1482 #ifdef SERIAL_DEBUG_OPEN
1483 printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1484 #endif
1485 info->count++;
1486 tty->driver_data = info;
1487 info->tty = tty;
1490 * Start up serial port
1492 retval = startup(info);
1493 if (retval)
1494 return retval;
1496 retval = block_til_ready(tty, filp, info);
1497 if (retval) {
1498 #ifdef SERIAL_DEBUG_OPEN
1499 printk("mcfrs_open returning after block_til_ready with %d\n",
1500 retval);
1501 #endif
1502 return retval;
1505 #ifdef SERIAL_DEBUG_OPEN
1506 printk("mcfrs_open %s successful...\n", tty->name);
1507 #endif
1508 return 0;
1512 * Based on the line number set up the internal interrupt stuff.
1514 static void mcfrs_irqinit(struct mcf_serial *info)
1516 #if defined(CONFIG_M5272)
1517 volatile unsigned long *icrp;
1518 volatile unsigned long *portp;
1519 volatile unsigned char *uartp;
1521 uartp = info->addr;
1522 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1524 switch (info->line) {
1525 case 0:
1526 *icrp = 0xe0000000;
1527 break;
1528 case 1:
1529 *icrp = 0x0e000000;
1530 break;
1531 default:
1532 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1533 info->line);
1534 return;
1537 /* Enable the output lines for the serial ports */
1538 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
1539 *portp = (*portp & ~0x000000ff) | 0x00000055;
1540 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
1541 *portp = (*portp & ~0x000003fc) | 0x000002a8;
1542 #elif defined(CONFIG_M527x) || defined(CONFIG_M528x)
1543 volatile unsigned char *icrp, *uartp;
1544 volatile unsigned long *imrp;
1546 uartp = info->addr;
1548 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1549 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1550 *icrp = 0x33; /* UART0 with level 6, priority 3 */
1552 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1553 MCFINTC_IMRL);
1554 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1555 #else
1556 volatile unsigned char *icrp, *uartp;
1558 switch (info->line) {
1559 case 0:
1560 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1561 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1562 MCFSIM_ICR_PRI1;
1563 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1564 break;
1565 case 1:
1566 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1567 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1568 MCFSIM_ICR_PRI2;
1569 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1570 break;
1571 default:
1572 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1573 info->line);
1574 return;
1577 uartp = info->addr;
1578 uartp[MCFUART_UIVR] = info->irq;
1579 #endif
1581 /* Clear mask, so no surprise interrupts. */
1582 uartp[MCFUART_UIMR] = 0;
1584 if (request_irq(info->irq, mcfrs_interrupt, SA_INTERRUPT,
1585 "ColdFire UART", NULL)) {
1586 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1587 "vector=%d\n", info->line, info->irq);
1590 return;
1594 char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1598 * Serial stats reporting...
1600 int mcfrs_readproc(char *page, char **start, off_t off, int count,
1601 int *eof, void *data)
1603 struct mcf_serial *info;
1604 char str[20];
1605 int len, sigs, i;
1607 len = sprintf(page, mcfrs_drivername);
1608 for (i = 0; (i < NR_PORTS); i++) {
1609 info = &mcfrs_table[i];
1610 len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1611 i, (unsigned int) info->addr, info->irq, info->baud);
1612 if (info->stats.rx || info->stats.tx)
1613 len += sprintf((page + len), "tx:%d rx:%d ",
1614 info->stats.tx, info->stats.rx);
1615 if (info->stats.rxframing)
1616 len += sprintf((page + len), "fe:%d ",
1617 info->stats.rxframing);
1618 if (info->stats.rxparity)
1619 len += sprintf((page + len), "pe:%d ",
1620 info->stats.rxparity);
1621 if (info->stats.rxbreak)
1622 len += sprintf((page + len), "brk:%d ",
1623 info->stats.rxbreak);
1624 if (info->stats.rxoverrun)
1625 len += sprintf((page + len), "oe:%d ",
1626 info->stats.rxoverrun);
1628 str[0] = str[1] = 0;
1629 if ((sigs = mcfrs_getsignals(info))) {
1630 if (sigs & TIOCM_RTS)
1631 strcat(str, "|RTS");
1632 if (sigs & TIOCM_CTS)
1633 strcat(str, "|CTS");
1634 if (sigs & TIOCM_DTR)
1635 strcat(str, "|DTR");
1636 if (sigs & TIOCM_CD)
1637 strcat(str, "|CD");
1640 len += sprintf((page + len), "%s\n", &str[1]);
1643 return(len);
1647 /* Finally, routines used to initialize the serial driver. */
1649 static void show_serial_version(void)
1651 printk(mcfrs_drivername);
1654 static struct tty_operations mcfrs_ops = {
1655 .open = mcfrs_open,
1656 .close = mcfrs_close,
1657 .write = mcfrs_write,
1658 .flush_chars = mcfrs_flush_chars,
1659 .write_room = mcfrs_write_room,
1660 .chars_in_buffer = mcfrs_chars_in_buffer,
1661 .flush_buffer = mcfrs_flush_buffer,
1662 .ioctl = mcfrs_ioctl,
1663 .throttle = mcfrs_throttle,
1664 .unthrottle = mcfrs_unthrottle,
1665 .set_termios = mcfrs_set_termios,
1666 .stop = mcfrs_stop,
1667 .start = mcfrs_start,
1668 .hangup = mcfrs_hangup,
1669 .read_proc = mcfrs_readproc,
1670 .wait_until_sent = mcfrs_wait_until_sent,
1671 .tiocmget = mcfrs_tiocmget,
1672 .tiocmset = mcfrs_tiocmset,
1675 /* mcfrs_init inits the driver */
1676 static int __init
1677 mcfrs_init(void)
1679 struct mcf_serial *info;
1680 unsigned long flags;
1681 int i;
1683 /* Setup base handler, and timer table. */
1684 #ifdef MCFPP_DCD0
1685 init_timer(&mcfrs_timer_struct);
1686 mcfrs_timer_struct.function = mcfrs_timer;
1687 mcfrs_timer_struct.data = 0;
1688 mcfrs_timer_struct.expires = jiffies + HZ/25;
1689 add_timer(&mcfrs_timer_struct);
1690 mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1691 #endif
1692 mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1693 if (!mcfrs_serial_driver)
1694 return -ENOMEM;
1696 show_serial_version();
1698 /* Initialize the tty_driver structure */
1699 mcfrs_serial_driver->owner = THIS_MODULE;
1700 mcfrs_serial_driver->name = "ttyS";
1701 mcfrs_serial_driver->devfs_name = "ttys/";
1702 mcfrs_serial_driver->driver_name = "serial";
1703 mcfrs_serial_driver->major = TTY_MAJOR;
1704 mcfrs_serial_driver->minor_start = 64;
1705 mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1706 mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1707 mcfrs_serial_driver->init_termios = tty_std_termios;
1709 mcfrs_serial_driver->init_termios.c_cflag =
1710 mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1711 mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1713 tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1715 if (tty_register_driver(mcfrs_serial_driver)) {
1716 printk("MCFRS: Couldn't register serial driver\n");
1717 put_tty_driver(mcfrs_serial_driver);
1718 return(-EBUSY);
1721 local_irq_save(flags);
1724 * Configure all the attached serial ports.
1726 for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1727 info->magic = SERIAL_MAGIC;
1728 info->line = i;
1729 info->tty = 0;
1730 info->custom_divisor = 16;
1731 info->close_delay = 50;
1732 info->closing_wait = 3000;
1733 info->x_char = 0;
1734 info->event = 0;
1735 info->count = 0;
1736 info->blocked_open = 0;
1737 INIT_WORK(&info->tqueue, mcfrs_offintr, info);
1738 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1739 init_waitqueue_head(&info->open_wait);
1740 init_waitqueue_head(&info->close_wait);
1742 info->imr = 0;
1743 mcfrs_setsignals(info, 0, 0);
1744 mcfrs_irqinit(info);
1746 printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1747 (unsigned int) info->addr, info->irq);
1748 printk(" is a builtin ColdFire UART\n");
1751 local_irq_restore(flags);
1752 return 0;
1755 module_init(mcfrs_init);
1757 /****************************************************************************/
1758 /* Serial Console */
1759 /****************************************************************************/
1762 * Quick and dirty UART initialization, for console output.
1765 void mcfrs_init_console(void)
1767 volatile unsigned char *uartp;
1768 unsigned int clk;
1771 * Reset UART, get it into known state...
1773 uartp = (volatile unsigned char *) (MCF_MBAR +
1774 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1776 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
1777 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
1778 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
1781 * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1783 uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1784 uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1786 clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1787 uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8; /* set msb baud */
1788 uartp[MCFUART_UBG2] = (clk & 0xff); /* set lsb baud */
1790 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1791 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1793 mcfrs_console_inited++;
1794 return;
1799 * Setup for console. Argument comes from the boot command line.
1802 int mcfrs_console_setup(struct console *cp, char *arg)
1804 int i, n = CONSOLE_BAUD_RATE;
1806 if (!cp)
1807 return(-1);
1809 if (!strncmp(cp->name, "ttyS", 4))
1810 mcfrs_console_port = cp->index;
1811 else if (!strncmp(cp->name, "cua", 3))
1812 mcfrs_console_port = cp->index;
1813 else
1814 return(-1);
1816 if (arg)
1817 n = simple_strtoul(arg,NULL,0);
1818 for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1819 if (mcfrs_baud_table[i] == n)
1820 break;
1821 if (i < MCFRS_BAUD_TABLE_SIZE) {
1822 mcfrs_console_baud = n;
1823 mcfrs_console_cbaud = 0;
1824 if (i > 15) {
1825 mcfrs_console_cbaud |= CBAUDEX;
1826 i -= 15;
1828 mcfrs_console_cbaud |= i;
1830 mcfrs_init_console(); /* make sure baud rate changes */
1831 return(0);
1835 static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1837 *index = c->index;
1838 return mcfrs_serial_driver;
1843 * Output a single character, using UART polled mode.
1844 * This is used for console output.
1847 void mcfrs_put_char(char ch)
1849 volatile unsigned char *uartp;
1850 unsigned long flags;
1851 int i;
1853 uartp = (volatile unsigned char *) (MCF_MBAR +
1854 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1856 local_irq_save(flags);
1857 for (i = 0; (i < 0x10000); i++) {
1858 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1859 break;
1861 if (i < 0x10000) {
1862 uartp[MCFUART_UTB] = ch;
1863 for (i = 0; (i < 0x10000); i++)
1864 if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1865 break;
1867 if (i >= 0x10000)
1868 mcfrs_init_console(); /* try and get it back */
1869 local_irq_restore(flags);
1871 return;
1876 * rs_console_write is registered for printk output.
1879 void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1881 if (!mcfrs_console_inited)
1882 mcfrs_init_console();
1883 while (len-- > 0) {
1884 if (*p == '\n')
1885 mcfrs_put_char('\r');
1886 mcfrs_put_char(*p++);
1891 * declare our consoles
1894 struct console mcfrs_console = {
1895 .name = "ttyS",
1896 .write = mcfrs_console_write,
1897 .device = mcfrs_console_device,
1898 .setup = mcfrs_console_setup,
1899 .flags = CON_PRINTBUFFER,
1900 .index = -1,
1903 static int __init mcfrs_console_init(void)
1905 register_console(&mcfrs_console);
1906 return 0;
1909 console_initcall(mcfrs_console_init);
1911 /****************************************************************************/