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>
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>
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>
42 #include <asm/system.h>
43 #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"
52 struct timer_list mcfrs_timer_struct;
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.
60 #if defined(CONFIG_ARNEWSH) || defined(CONFIG_FREESCALE) || \
61 defined(CONFIG_senTec) || defined(CONFIG_SNEHA)
62 #define CONSOLE_BAUD_RATE 19200
63 #define DEFAULT_CBAUD B19200
66 #if defined(CONFIG_HW_FEITH)
67 #define CONSOLE_BAUD_RATE 38400
68 #define DEFAULT_CBAUD B38400
71 #if defined(CONFIG_MOD5272) || defined(CONFIG_M5208EVB)
72 #define CONSOLE_BAUD_RATE 115200
73 #define DEFAULT_CBAUD B115200
76 #ifndef CONSOLE_BAUD_RATE
77 #define CONSOLE_BAUD_RATE 9600
78 #define DEFAULT_CBAUD B9600
81 int mcfrs_console_inited = 0;
82 int mcfrs_console_port = -1;
83 int mcfrs_console_baud = CONSOLE_BAUD_RATE;
84 int mcfrs_console_cbaud = DEFAULT_CBAUD;
87 * Driver data structures.
89 static struct tty_driver *mcfrs_serial_driver;
91 /* number of characters left in xmit buffer before we ask for more */
92 #define WAKEUP_CHARS 256
96 #undef SERIAL_DEBUG_OPEN
97 #undef SERIAL_DEBUG_FLOW
99 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
100 defined(CONFIG_M520x)
101 #define IRQBASE (MCFINT_VECBASE+MCFINT_UART0)
107 * Configuration table, UARTs to look for at startup.
109 static struct mcf_serial mcfrs_table[] = {
112 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE1),
114 .flags = ASYNC_BOOT_AUTOCONF,
118 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE2),
120 .flags = ASYNC_BOOT_AUTOCONF,
125 #define NR_PORTS (sizeof(mcfrs_table) / sizeof(struct mcf_serial))
128 * This is used to figure out the divisor speeds and the timeouts.
130 static 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
134 #define MCFRS_BAUD_TABLE_SIZE \
135 (sizeof(mcfrs_baud_table)/sizeof(mcfrs_baud_table[0]))
138 #ifdef CONFIG_MAGIC_SYSRQ
140 * Magic system request keys. Used for debugging...
142 extern int magic_sysrq_key(int ch);
147 * Forware declarations...
149 static void mcfrs_change_speed(struct mcf_serial *info);
150 static void mcfrs_wait_until_sent(struct tty_struct *tty, int timeout);
153 static inline int serial_paranoia_check(struct mcf_serial *info,
154 char *name, const char *routine)
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";
163 printk(badinfo, name, routine);
166 if (info->magic != SERIAL_MAGIC) {
167 printk(badmagic, name, routine);
175 * Sets or clears DTR and RTS on the requested line.
177 static void mcfrs_setsignals(struct mcf_serial *info, int dtr, int rts)
179 volatile unsigned char *uartp;
183 printk("%s(%d): mcfrs_setsignals(info=%x,dtr=%d,rts=%d)\n",
184 __FILE__, __LINE__, info, dtr, rts);
187 local_irq_save(flags);
191 mcf_setppdata(MCFPP_DTR1, (dtr ? 0 : MCFPP_DTR1));
193 mcf_setppdata(MCFPP_DTR0, (dtr ? 0 : MCFPP_DTR0));
199 info->sigs |= TIOCM_RTS;
200 uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
202 info->sigs &= ~TIOCM_RTS;
203 uartp[MCFUART_UOP0] = MCFUART_UOP_RTS;
206 local_irq_restore(flags);
211 * Gets values of serial signals.
213 static int mcfrs_getsignals(struct mcf_serial *info)
215 volatile unsigned char *uartp;
218 #if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
219 unsigned short ppdata;
223 printk("%s(%d): mcfrs_getsignals(info=%x)\n", __FILE__, __LINE__);
226 local_irq_save(flags);
228 sigs = (uartp[MCFUART_UIPR] & MCFUART_UIPR_CTS) ? 0 : TIOCM_CTS;
229 sigs |= (info->sigs & TIOCM_RTS);
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;
245 local_irq_restore(flags);
250 * ------------------------------------------------------------
251 * mcfrs_stop() and mcfrs_start()
253 * This routines are called before setting or resetting tty->stopped.
254 * They enable or disable transmitter interrupts, as necessary.
255 * ------------------------------------------------------------
257 static void mcfrs_stop(struct tty_struct *tty)
259 volatile unsigned char *uartp;
260 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
263 if (serial_paranoia_check(info, tty->name, "mcfrs_stop"))
266 local_irq_save(flags);
268 info->imr &= ~MCFUART_UIR_TXREADY;
269 uartp[MCFUART_UIMR] = info->imr;
270 local_irq_restore(flags);
273 static void mcfrs_start(struct tty_struct *tty)
275 volatile unsigned char *uartp;
276 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
279 if (serial_paranoia_check(info, tty->name, "mcfrs_start"))
282 local_irq_save(flags);
283 if (info->xmit_cnt && info->xmit_buf) {
285 info->imr |= MCFUART_UIR_TXREADY;
286 uartp[MCFUART_UIMR] = info->imr;
288 local_irq_restore(flags);
292 * ----------------------------------------------------------------------
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.
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
304 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
306 * and look at the resulting assemble code in serial.s.
308 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
309 * -----------------------------------------------------------------------
312 static inline void receive_chars(struct mcf_serial *info)
314 volatile unsigned char *uartp;
315 struct tty_struct *tty = info->tty;
316 unsigned char status, ch, flag;
323 while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
324 ch = uartp[MCFUART_URB];
327 #ifdef CONFIG_MAGIC_SYSRQ
328 if (mcfrs_console_inited && (info->line == mcfrs_console_port)) {
329 if (magic_sysrq_key(ch))
335 if (status & MCFUART_USR_RXERR) {
336 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
337 if (status & MCFUART_USR_RXBREAK) {
338 info->stats.rxbreak++;
340 } else if (status & MCFUART_USR_RXPARITY) {
341 info->stats.rxparity++;
343 } else if (status & MCFUART_USR_RXOVERRUN) {
344 info->stats.rxoverrun++;
346 } else if (status & MCFUART_USR_RXFRAMING) {
347 info->stats.rxframing++;
351 tty_insert_flip_char(tty, ch, flag);
353 tty_flip_buffer_push(tty);
357 static inline void transmit_chars(struct mcf_serial *info)
359 volatile unsigned char *uartp;
364 /* Send special char - probably flow control */
365 uartp[MCFUART_UTB] = info->x_char;
370 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
371 info->imr &= ~MCFUART_UIR_TXREADY;
372 uartp[MCFUART_UIMR] = info->imr;
376 while (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) {
377 uartp[MCFUART_UTB] = info->xmit_buf[info->xmit_tail++];
378 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
380 if (--info->xmit_cnt <= 0)
384 if (info->xmit_cnt < WAKEUP_CHARS)
385 schedule_work(&info->tqueue);
390 * This is the serial driver's generic interrupt routine
392 irqreturn_t mcfrs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
394 struct mcf_serial *info;
397 info = &mcfrs_table[(irq - IRQBASE)];
398 isr = info->addr[MCFUART_UISR] & info->imr;
400 if (isr & MCFUART_UIR_RXREADY)
402 if (isr & MCFUART_UIR_TXREADY)
403 transmit_chars(info);
408 * -------------------------------------------------------------------
409 * Here ends the serial interrupt routines.
410 * -------------------------------------------------------------------
413 static void mcfrs_offintr(void *private)
415 struct mcf_serial *info = (struct mcf_serial *) private;
416 struct tty_struct *tty;
426 * Change of state on a DCD line.
428 void mcfrs_modem_change(struct mcf_serial *info, int dcd)
430 if (info->count == 0)
433 if (info->flags & ASYNC_CHECK_CD) {
435 wake_up_interruptible(&info->open_wait);
437 schedule_work(&info->tqueue_hangup);
444 unsigned short mcfrs_ppstatus;
447 * This subroutine is called when the RS_TIMER goes off. It is used
448 * to monitor the state of the DCD lines - since they have no edge
449 * sensors and interrupt generators.
451 static void mcfrs_timer(void)
453 unsigned int ppstatus, dcdval, i;
455 ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
457 if (ppstatus != mcfrs_ppstatus) {
458 for (i = 0; (i < 2); i++) {
459 dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
460 if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
461 mcfrs_modem_change(&mcfrs_table[i],
462 ((ppstatus & dcdval) ? 0 : 1));
466 mcfrs_ppstatus = ppstatus;
469 mcfrs_timer_struct.expires = jiffies + HZ/25;
470 add_timer(&mcfrs_timer_struct);
473 #endif /* MCFPP_DCD0 */
477 * This routine is called from the scheduler tqueue when the interrupt
478 * routine has signalled that a hangup has occurred. The path of
479 * hangup processing is:
481 * serial interrupt routine -> (scheduler tqueue) ->
482 * do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
485 static void do_serial_hangup(void *private)
487 struct mcf_serial *info = (struct mcf_serial *) private;
488 struct tty_struct *tty;
497 static int startup(struct mcf_serial * info)
499 volatile unsigned char *uartp;
502 if (info->flags & ASYNC_INITIALIZED)
505 if (!info->xmit_buf) {
506 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
511 local_irq_save(flags);
513 #ifdef SERIAL_DEBUG_OPEN
514 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
518 * Reset UART, get it into known state...
521 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
522 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
523 mcfrs_setsignals(info, 1, 1);
526 clear_bit(TTY_IO_ERROR, &info->tty->flags);
527 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
530 * and set the speed of the serial port
532 mcfrs_change_speed(info);
535 * Lastly enable the UART transmitter and receiver, and
538 info->imr = MCFUART_UIR_RXREADY;
539 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
540 uartp[MCFUART_UIMR] = info->imr;
542 info->flags |= ASYNC_INITIALIZED;
543 local_irq_restore(flags);
548 * This routine will shutdown a serial port; interrupts are disabled, and
549 * DTR is dropped if the hangup on close termio flag is on.
551 static void shutdown(struct mcf_serial * info)
553 volatile unsigned char *uartp;
556 if (!(info->flags & ASYNC_INITIALIZED))
559 #ifdef SERIAL_DEBUG_OPEN
560 printk("Shutting down serial port %d (irq %d)....\n", info->line,
564 local_irq_save(flags);
567 uartp[MCFUART_UIMR] = 0; /* mask all interrupts */
568 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
569 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
571 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
572 mcfrs_setsignals(info, 0, 0);
574 if (info->xmit_buf) {
575 free_page((unsigned long) info->xmit_buf);
580 set_bit(TTY_IO_ERROR, &info->tty->flags);
582 info->flags &= ~ASYNC_INITIALIZED;
583 local_irq_restore(flags);
588 * This routine is called to set the UART divisor registers to match
589 * the specified baud rate for a serial port.
591 static void mcfrs_change_speed(struct mcf_serial *info)
593 volatile unsigned char *uartp;
594 unsigned int baudclk, cflag;
596 unsigned char mr1, mr2;
599 unsigned int fraction;
602 if (!info->tty || !info->tty->termios)
604 cflag = info->tty->termios->c_cflag;
609 printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
616 info->tty->termios->c_cflag &= ~CBAUDEX;
621 mcfrs_setsignals(info, 0, -1);
625 /* compute the baudrate clock */
628 * For the MCF5272, also compute the baudrate fraction.
630 baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
631 fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
633 fraction /= (32 * mcfrs_baud_table[i]);
635 baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
638 info->baud = mcfrs_baud_table[i];
640 mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
643 switch (cflag & CSIZE) {
644 case CS5: mr1 |= MCFUART_MR1_CS5; break;
645 case CS6: mr1 |= MCFUART_MR1_CS6; break;
646 case CS7: mr1 |= MCFUART_MR1_CS7; break;
648 default: mr1 |= MCFUART_MR1_CS8; break;
651 if (cflag & PARENB) {
652 if (cflag & CMSPAR) {
654 mr1 |= MCFUART_MR1_PARITYMARK;
656 mr1 |= MCFUART_MR1_PARITYSPACE;
659 mr1 |= MCFUART_MR1_PARITYODD;
661 mr1 |= MCFUART_MR1_PARITYEVEN;
664 mr1 |= MCFUART_MR1_PARITYNONE;
668 mr2 |= MCFUART_MR2_STOP2;
670 mr2 |= MCFUART_MR2_STOP1;
672 if (cflag & CRTSCTS) {
673 mr1 |= MCFUART_MR1_RXRTS;
674 mr2 |= MCFUART_MR2_TXCTS;
678 info->flags &= ~ASYNC_CHECK_CD;
680 info->flags |= ASYNC_CHECK_CD;
684 local_irq_save(flags);
686 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
690 Note: pg 12-16 of MCF5206e User's Manual states that a
691 software reset should be performed prior to changing
692 UMR1,2, UCSR, UACR, bit 7
694 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
695 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
696 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
697 uartp[MCFUART_UMR] = mr1;
698 uartp[MCFUART_UMR] = mr2;
699 uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8; /* set msb byte */
700 uartp[MCFUART_UBG2] = (baudclk & 0xff); /* set lsb byte */
702 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
704 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
705 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
706 mcfrs_setsignals(info, 1, -1);
707 local_irq_restore(flags);
711 static void mcfrs_flush_chars(struct tty_struct *tty)
713 volatile unsigned char *uartp;
714 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
717 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
720 uartp = (volatile unsigned char *) info->addr;
723 * re-enable receiver interrupt
725 local_irq_save(flags);
726 if ((!(info->imr & MCFUART_UIR_RXREADY)) &&
727 (info->flags & ASYNC_INITIALIZED) ) {
728 info->imr |= MCFUART_UIR_RXREADY;
729 uartp[MCFUART_UIMR] = info->imr;
731 local_irq_restore(flags);
733 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
737 /* Enable transmitter */
738 local_irq_save(flags);
739 info->imr |= MCFUART_UIR_TXREADY;
740 uartp[MCFUART_UIMR] = info->imr;
741 local_irq_restore(flags);
744 static int mcfrs_write(struct tty_struct * tty,
745 const unsigned char *buf, int count)
747 volatile unsigned char *uartp;
748 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
753 printk("%s(%d): mcfrs_write(tty=%x,buf=%x,count=%d)\n",
754 __FILE__, __LINE__, (int)tty, (int)buf, count);
757 if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
760 if (!tty || !info->xmit_buf)
763 local_save_flags(flags);
766 c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
767 ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
768 local_irq_restore(flags);
773 memcpy(info->xmit_buf + info->xmit_head, buf, c);
776 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
778 local_irq_restore(flags);
787 info->imr |= MCFUART_UIR_TXREADY;
788 uartp[MCFUART_UIMR] = info->imr;
789 local_irq_restore(flags);
794 static int mcfrs_write_room(struct tty_struct *tty)
796 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
799 if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
801 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
807 static int mcfrs_chars_in_buffer(struct tty_struct *tty)
809 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
811 if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
813 return info->xmit_cnt;
816 static void mcfrs_flush_buffer(struct tty_struct *tty)
818 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
821 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
824 local_irq_save(flags);
825 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
826 local_irq_restore(flags);
832 * ------------------------------------------------------------
835 * This routine is called by the upper-layer tty layer to signal that
836 * incoming characters should be throttled.
837 * ------------------------------------------------------------
839 static void mcfrs_throttle(struct tty_struct * tty)
841 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
842 #ifdef SERIAL_DEBUG_THROTTLE
845 printk("throttle %s: %d....\n", _tty_name(tty, buf),
846 tty->ldisc.chars_in_buffer(tty));
849 if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
853 info->x_char = STOP_CHAR(tty);
855 /* Turn off RTS line (do this atomic) */
858 static void mcfrs_unthrottle(struct tty_struct * tty)
860 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
861 #ifdef SERIAL_DEBUG_THROTTLE
864 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
865 tty->ldisc.chars_in_buffer(tty));
868 if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
875 info->x_char = START_CHAR(tty);
878 /* Assert RTS line (do this atomic) */
882 * ------------------------------------------------------------
883 * mcfrs_ioctl() and friends
884 * ------------------------------------------------------------
887 static int get_serial_info(struct mcf_serial * info,
888 struct serial_struct * retinfo)
890 struct serial_struct tmp;
894 memset(&tmp, 0, sizeof(tmp));
895 tmp.type = info->type;
896 tmp.line = info->line;
897 tmp.port = (unsigned int) info->addr;
899 tmp.flags = info->flags;
900 tmp.baud_base = info->baud_base;
901 tmp.close_delay = info->close_delay;
902 tmp.closing_wait = info->closing_wait;
903 tmp.custom_divisor = info->custom_divisor;
904 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
907 static int set_serial_info(struct mcf_serial * info,
908 struct serial_struct * new_info)
910 struct serial_struct new_serial;
911 struct mcf_serial old_info;
916 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
920 if (!capable(CAP_SYS_ADMIN)) {
921 if ((new_serial.baud_base != info->baud_base) ||
922 (new_serial.type != info->type) ||
923 (new_serial.close_delay != info->close_delay) ||
924 ((new_serial.flags & ~ASYNC_USR_MASK) !=
925 (info->flags & ~ASYNC_USR_MASK)))
927 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
928 (new_serial.flags & ASYNC_USR_MASK));
929 info->custom_divisor = new_serial.custom_divisor;
937 * OK, past this point, all the error checking has been done.
938 * At this point, we start making changes.....
941 info->baud_base = new_serial.baud_base;
942 info->flags = ((info->flags & ~ASYNC_FLAGS) |
943 (new_serial.flags & ASYNC_FLAGS));
944 info->type = new_serial.type;
945 info->close_delay = new_serial.close_delay;
946 info->closing_wait = new_serial.closing_wait;
949 retval = startup(info);
954 * get_lsr_info - get line status register info
956 * Purpose: Let user call ioctl() to get info when the UART physically
957 * is emptied. On bus types like RS485, the transmitter must
958 * release the bus after transmitting. This must be done when
959 * the transmit shift register is empty, not be done when the
960 * transmit holding register is empty. This functionality
961 * allows an RS485 driver to be written in user space.
963 static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
965 volatile unsigned char *uartp;
967 unsigned char status;
969 local_irq_save(flags);
971 status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
972 local_irq_restore(flags);
974 return put_user(status,value);
978 * This routine sends a break character out the serial port.
980 static void send_break( struct mcf_serial * info, int duration)
982 volatile unsigned char *uartp;
987 set_current_state(TASK_INTERRUPTIBLE);
990 local_irq_save(flags);
991 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
992 schedule_timeout(duration);
993 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
994 local_irq_restore(flags);
997 static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
999 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1001 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1003 if (tty->flags & (1 << TTY_IO_ERROR))
1006 return mcfrs_getsignals(info);
1009 static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
1010 unsigned int set, unsigned int clear)
1012 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1013 int rts = -1, dtr = -1;
1015 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1017 if (tty->flags & (1 << TTY_IO_ERROR))
1020 if (set & TIOCM_RTS)
1022 if (set & TIOCM_DTR)
1024 if (clear & TIOCM_RTS)
1026 if (clear & TIOCM_DTR)
1029 mcfrs_setsignals(info, dtr, rts);
1034 static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
1035 unsigned int cmd, unsigned long arg)
1037 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1040 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1043 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1044 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1045 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1046 if (tty->flags & (1 << TTY_IO_ERROR))
1051 case TCSBRK: /* SVID version: non-zero arg --> no break */
1052 retval = tty_check_change(tty);
1055 tty_wait_until_sent(tty, 0);
1057 send_break(info, HZ/4); /* 1/4 second */
1059 case TCSBRKP: /* support for POSIX tcsendbreak() */
1060 retval = tty_check_change(tty);
1063 tty_wait_until_sent(tty, 0);
1064 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1067 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1068 (unsigned long *) arg);
1073 get_user(arg, (unsigned long *) arg);
1074 tty->termios->c_cflag =
1075 ((tty->termios->c_cflag & ~CLOCAL) |
1076 (arg ? CLOCAL : 0));
1079 if (access_ok(VERIFY_WRITE, (void *) arg,
1080 sizeof(struct serial_struct)))
1081 return get_serial_info(info,
1082 (struct serial_struct *) arg);
1085 return set_serial_info(info,
1086 (struct serial_struct *) arg);
1087 case TIOCSERGETLSR: /* Get line status register */
1088 if (access_ok(VERIFY_WRITE, (void *) arg,
1089 sizeof(unsigned int)))
1090 return get_lsr_info(info, (unsigned int *) arg);
1092 case TIOCSERGSTRUCT:
1093 error = copy_to_user((struct mcf_serial *) arg,
1094 info, sizeof(struct mcf_serial));
1102 get_user(val, (unsigned int *) arg);
1103 mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1108 val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1109 put_user(val, (unsigned int *) arg);
1115 return -ENOIOCTLCMD;
1120 static void mcfrs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1122 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1124 if (tty->termios->c_cflag == old_termios->c_cflag)
1127 mcfrs_change_speed(info);
1129 if ((old_termios->c_cflag & CRTSCTS) &&
1130 !(tty->termios->c_cflag & CRTSCTS)) {
1131 tty->hw_stopped = 0;
1132 mcfrs_setsignals(info, -1, 1);
1140 * ------------------------------------------------------------
1143 * This routine is called when the serial port gets closed. First, we
1144 * wait for the last remaining data to be sent. Then, we unlink its
1145 * S structure from the interrupt chain if necessary, and we free
1146 * that IRQ if nothing is left in the chain.
1147 * ------------------------------------------------------------
1149 static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1151 volatile unsigned char *uartp;
1152 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1153 unsigned long flags;
1155 if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1158 local_irq_save(flags);
1160 if (tty_hung_up_p(filp)) {
1161 local_irq_restore(flags);
1165 #ifdef SERIAL_DEBUG_OPEN
1166 printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1168 if ((tty->count == 1) && (info->count != 1)) {
1170 * Uh, oh. tty->count is 1, which means that the tty
1171 * structure will be freed. Info->count should always
1172 * be one in these conditions. If it's greater than
1173 * one, we've got real problems, since it means the
1174 * serial port won't be shutdown.
1176 printk("MCFRS: bad serial port count; tty->count is 1, "
1177 "info->count is %d\n", info->count);
1180 if (--info->count < 0) {
1181 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1182 info->line, info->count);
1186 local_irq_restore(flags);
1189 info->flags |= ASYNC_CLOSING;
1192 * Now we wait for the transmit buffer to clear; and we notify
1193 * the line discipline to only process XON/XOFF characters.
1196 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1197 tty_wait_until_sent(tty, info->closing_wait);
1200 * At this point we stop accepting input. To do this, we
1201 * disable the receive line status interrupts, and tell the
1202 * interrupt driver to stop checking the data ready bit in the
1203 * line status register.
1205 info->imr &= ~MCFUART_UIR_RXREADY;
1207 uartp[MCFUART_UIMR] = info->imr;
1210 /* FIXME: do we need to keep this enabled for console?? */
1211 if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
1212 /* Do not disable the UART */ ;
1216 if (tty->driver->flush_buffer)
1217 tty->driver->flush_buffer(tty);
1218 tty_ldisc_flush(tty);
1224 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1225 if (tty->ldisc.close)
1226 (tty->ldisc.close)(tty);
1227 tty->ldisc = ldiscs[N_TTY];
1228 tty->termios->c_line = N_TTY;
1229 if (tty->ldisc.open)
1230 (tty->ldisc.open)(tty);
1233 if (info->blocked_open) {
1234 if (info->close_delay) {
1235 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1237 wake_up_interruptible(&info->open_wait);
1239 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1240 wake_up_interruptible(&info->close_wait);
1241 local_irq_restore(flags);
1245 * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1248 mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1251 #define MCF5272_FIFO_SIZE 25 /* fifo size + shift reg */
1253 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1254 volatile unsigned char *uartp;
1255 unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1257 if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1260 orig_jiffies = jiffies;
1263 * Set the check interval to be 1/5 of the approximate time
1264 * to send the entire fifo, and make it at least 1. The check
1265 * interval should also be less than the timeout.
1267 * Note: we have to use pretty tight timings here to satisfy
1270 fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1271 char_time = fifo_time / 5;
1274 if (timeout && timeout < char_time)
1275 char_time = timeout;
1278 * Clamp the timeout period at 2 * the time to empty the
1279 * fifo. Just to be safe, set the minimum at .5 seconds.
1282 if (fifo_time < (HZ/2))
1284 if (!timeout || timeout > fifo_time)
1285 timeout = fifo_time;
1288 * Account for the number of bytes in the UART
1289 * transmitter FIFO plus any byte being shifted out.
1291 uartp = (volatile unsigned char *) info->addr;
1293 fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1294 if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1295 MCFUART_USR_TXEMPTY)) ==
1296 MCFUART_USR_TXREADY)
1300 msleep_interruptible(jiffies_to_msecs(char_time));
1301 if (signal_pending(current))
1303 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1308 * For the other coldfire models, assume all data has been sent
1314 * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1316 void mcfrs_hangup(struct tty_struct *tty)
1318 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1320 if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
1323 mcfrs_flush_buffer(tty);
1327 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1329 wake_up_interruptible(&info->open_wait);
1333 * ------------------------------------------------------------
1334 * mcfrs_open() and friends
1335 * ------------------------------------------------------------
1337 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1338 struct mcf_serial *info)
1340 DECLARE_WAITQUEUE(wait, current);
1345 * If the device is in the middle of being closed, then block
1346 * until it's done, and then try again.
1348 if (info->flags & ASYNC_CLOSING) {
1349 interruptible_sleep_on(&info->close_wait);
1350 #ifdef SERIAL_DO_RESTART
1351 if (info->flags & ASYNC_HUP_NOTIFY)
1354 return -ERESTARTSYS;
1361 * If non-blocking mode is set, or the port is not enabled,
1362 * then make the check up front and then exit.
1364 if ((filp->f_flags & O_NONBLOCK) ||
1365 (tty->flags & (1 << TTY_IO_ERROR))) {
1366 info->flags |= ASYNC_NORMAL_ACTIVE;
1370 if (tty->termios->c_cflag & CLOCAL)
1374 * Block waiting for the carrier detect and the line to become
1375 * free (i.e., not in use by the callout). While we are in
1376 * this loop, info->count is dropped by one, so that
1377 * mcfrs_close() knows when to free things. We restore it upon
1378 * exit, either normal or abnormal.
1381 add_wait_queue(&info->open_wait, &wait);
1382 #ifdef SERIAL_DEBUG_OPEN
1383 printk("block_til_ready before block: ttyS%d, count = %d\n",
1384 info->line, info->count);
1387 info->blocked_open++;
1389 local_irq_disable();
1390 mcfrs_setsignals(info, 1, 1);
1392 current->state = TASK_INTERRUPTIBLE;
1393 if (tty_hung_up_p(filp) ||
1394 !(info->flags & ASYNC_INITIALIZED)) {
1395 #ifdef SERIAL_DO_RESTART
1396 if (info->flags & ASYNC_HUP_NOTIFY)
1399 retval = -ERESTARTSYS;
1405 if (!(info->flags & ASYNC_CLOSING) &&
1406 (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1408 if (signal_pending(current)) {
1409 retval = -ERESTARTSYS;
1412 #ifdef SERIAL_DEBUG_OPEN
1413 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1414 info->line, info->count);
1418 current->state = TASK_RUNNING;
1419 remove_wait_queue(&info->open_wait, &wait);
1420 if (!tty_hung_up_p(filp))
1422 info->blocked_open--;
1423 #ifdef SERIAL_DEBUG_OPEN
1424 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1425 info->line, info->count);
1429 info->flags |= ASYNC_NORMAL_ACTIVE;
1434 * This routine is called whenever a serial port is opened. It
1435 * enables interrupts for a serial port, linking in its structure into
1436 * the IRQ chain. It also performs the serial-specific
1437 * initialization for the tty structure.
1439 int mcfrs_open(struct tty_struct *tty, struct file * filp)
1441 struct mcf_serial *info;
1445 if ((line < 0) || (line >= NR_PORTS))
1447 info = mcfrs_table + line;
1448 if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1450 #ifdef SERIAL_DEBUG_OPEN
1451 printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1454 tty->driver_data = info;
1458 * Start up serial port
1460 retval = startup(info);
1464 retval = block_til_ready(tty, filp, info);
1466 #ifdef SERIAL_DEBUG_OPEN
1467 printk("mcfrs_open returning after block_til_ready with %d\n",
1473 #ifdef SERIAL_DEBUG_OPEN
1474 printk("mcfrs_open %s successful...\n", tty->name);
1480 * Based on the line number set up the internal interrupt stuff.
1482 static void mcfrs_irqinit(struct mcf_serial *info)
1484 #if defined(CONFIG_M5272)
1485 volatile unsigned long *icrp;
1486 volatile unsigned long *portp;
1487 volatile unsigned char *uartp;
1490 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1492 switch (info->line) {
1500 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1505 /* Enable the output lines for the serial ports */
1506 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
1507 *portp = (*portp & ~0x000000ff) | 0x00000055;
1508 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
1509 *portp = (*portp & ~0x000003fc) | 0x000002a8;
1510 #elif defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x)
1511 volatile unsigned char *icrp, *uartp;
1512 volatile unsigned long *imrp;
1516 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1517 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1518 *icrp = 0x30 + info->line; /* level 6, line based priority */
1520 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1522 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1523 #elif defined(CONFIG_M520x)
1524 volatile unsigned char *icrp, *uartp;
1525 volatile unsigned long *imrp;
1529 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1530 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1533 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1535 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1536 if (info->line < 2) {
1537 unsigned short *uart_par;
1538 uart_par = (unsigned short *)(MCF_IPSBAR + MCF_GPIO_PAR_UART);
1539 if (info->line == 0)
1540 *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD0
1541 | MCF_GPIO_PAR_UART_PAR_URXD0;
1542 else if (info->line == 1)
1543 *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD1
1544 | MCF_GPIO_PAR_UART_PAR_URXD1;
1545 } else if (info->line == 2) {
1546 unsigned char *feci2c_par;
1547 feci2c_par = (unsigned char *)(MCF_IPSBAR + MCF_GPIO_PAR_FECI2C);
1548 *feci2c_par &= ~0x0F;
1549 *feci2c_par |= MCF_GPIO_PAR_FECI2C_PAR_SCL_UTXD2
1550 | MCF_GPIO_PAR_FECI2C_PAR_SDA_URXD2;
1553 volatile unsigned char *icrp, *uartp;
1555 switch (info->line) {
1557 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1558 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1560 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1563 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1564 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1566 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1569 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1575 uartp[MCFUART_UIVR] = info->irq;
1578 /* Clear mask, so no surprise interrupts. */
1579 uartp[MCFUART_UIMR] = 0;
1581 if (request_irq(info->irq, mcfrs_interrupt, SA_INTERRUPT,
1582 "ColdFire UART", NULL)) {
1583 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1584 "vector=%d\n", info->line, info->irq);
1591 char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1595 * Serial stats reporting...
1597 int mcfrs_readproc(char *page, char **start, off_t off, int count,
1598 int *eof, void *data)
1600 struct mcf_serial *info;
1604 len = sprintf(page, mcfrs_drivername);
1605 for (i = 0; (i < NR_PORTS); i++) {
1606 info = &mcfrs_table[i];
1607 len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1608 i, (unsigned int) info->addr, info->irq, info->baud);
1609 if (info->stats.rx || info->stats.tx)
1610 len += sprintf((page + len), "tx:%d rx:%d ",
1611 info->stats.tx, info->stats.rx);
1612 if (info->stats.rxframing)
1613 len += sprintf((page + len), "fe:%d ",
1614 info->stats.rxframing);
1615 if (info->stats.rxparity)
1616 len += sprintf((page + len), "pe:%d ",
1617 info->stats.rxparity);
1618 if (info->stats.rxbreak)
1619 len += sprintf((page + len), "brk:%d ",
1620 info->stats.rxbreak);
1621 if (info->stats.rxoverrun)
1622 len += sprintf((page + len), "oe:%d ",
1623 info->stats.rxoverrun);
1625 str[0] = str[1] = 0;
1626 if ((sigs = mcfrs_getsignals(info))) {
1627 if (sigs & TIOCM_RTS)
1628 strcat(str, "|RTS");
1629 if (sigs & TIOCM_CTS)
1630 strcat(str, "|CTS");
1631 if (sigs & TIOCM_DTR)
1632 strcat(str, "|DTR");
1633 if (sigs & TIOCM_CD)
1637 len += sprintf((page + len), "%s\n", &str[1]);
1644 /* Finally, routines used to initialize the serial driver. */
1646 static void show_serial_version(void)
1648 printk(mcfrs_drivername);
1651 static struct tty_operations mcfrs_ops = {
1653 .close = mcfrs_close,
1654 .write = mcfrs_write,
1655 .flush_chars = mcfrs_flush_chars,
1656 .write_room = mcfrs_write_room,
1657 .chars_in_buffer = mcfrs_chars_in_buffer,
1658 .flush_buffer = mcfrs_flush_buffer,
1659 .ioctl = mcfrs_ioctl,
1660 .throttle = mcfrs_throttle,
1661 .unthrottle = mcfrs_unthrottle,
1662 .set_termios = mcfrs_set_termios,
1664 .start = mcfrs_start,
1665 .hangup = mcfrs_hangup,
1666 .read_proc = mcfrs_readproc,
1667 .wait_until_sent = mcfrs_wait_until_sent,
1668 .tiocmget = mcfrs_tiocmget,
1669 .tiocmset = mcfrs_tiocmset,
1672 /* mcfrs_init inits the driver */
1676 struct mcf_serial *info;
1677 unsigned long flags;
1680 /* Setup base handler, and timer table. */
1682 init_timer(&mcfrs_timer_struct);
1683 mcfrs_timer_struct.function = mcfrs_timer;
1684 mcfrs_timer_struct.data = 0;
1685 mcfrs_timer_struct.expires = jiffies + HZ/25;
1686 add_timer(&mcfrs_timer_struct);
1687 mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1689 mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1690 if (!mcfrs_serial_driver)
1693 show_serial_version();
1695 /* Initialize the tty_driver structure */
1696 mcfrs_serial_driver->owner = THIS_MODULE;
1697 mcfrs_serial_driver->name = "ttyS";
1698 mcfrs_serial_driver->devfs_name = "ttys/";
1699 mcfrs_serial_driver->driver_name = "serial";
1700 mcfrs_serial_driver->major = TTY_MAJOR;
1701 mcfrs_serial_driver->minor_start = 64;
1702 mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1703 mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1704 mcfrs_serial_driver->init_termios = tty_std_termios;
1706 mcfrs_serial_driver->init_termios.c_cflag =
1707 mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1708 mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1710 tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1712 if (tty_register_driver(mcfrs_serial_driver)) {
1713 printk("MCFRS: Couldn't register serial driver\n");
1714 put_tty_driver(mcfrs_serial_driver);
1718 local_irq_save(flags);
1721 * Configure all the attached serial ports.
1723 for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1724 info->magic = SERIAL_MAGIC;
1727 info->custom_divisor = 16;
1728 info->close_delay = 50;
1729 info->closing_wait = 3000;
1733 info->blocked_open = 0;
1734 INIT_WORK(&info->tqueue, mcfrs_offintr, info);
1735 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1736 init_waitqueue_head(&info->open_wait);
1737 init_waitqueue_head(&info->close_wait);
1740 mcfrs_setsignals(info, 0, 0);
1741 mcfrs_irqinit(info);
1743 printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1744 (unsigned int) info->addr, info->irq);
1745 printk(" is a builtin ColdFire UART\n");
1748 local_irq_restore(flags);
1752 module_init(mcfrs_init);
1754 /****************************************************************************/
1755 /* Serial Console */
1756 /****************************************************************************/
1759 * Quick and dirty UART initialization, for console output.
1762 void mcfrs_init_console(void)
1764 volatile unsigned char *uartp;
1768 * Reset UART, get it into known state...
1770 uartp = (volatile unsigned char *) (MCF_MBAR +
1771 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1773 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
1774 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
1775 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
1778 * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1780 uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1781 uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1783 clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1784 uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8; /* set msb baud */
1785 uartp[MCFUART_UBG2] = (clk & 0xff); /* set lsb baud */
1787 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1788 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1790 mcfrs_console_inited++;
1796 * Setup for console. Argument comes from the boot command line.
1799 int mcfrs_console_setup(struct console *cp, char *arg)
1801 int i, n = CONSOLE_BAUD_RATE;
1806 if (!strncmp(cp->name, "ttyS", 4))
1807 mcfrs_console_port = cp->index;
1808 else if (!strncmp(cp->name, "cua", 3))
1809 mcfrs_console_port = cp->index;
1814 n = simple_strtoul(arg,NULL,0);
1815 for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1816 if (mcfrs_baud_table[i] == n)
1818 if (i < MCFRS_BAUD_TABLE_SIZE) {
1819 mcfrs_console_baud = n;
1820 mcfrs_console_cbaud = 0;
1822 mcfrs_console_cbaud |= CBAUDEX;
1825 mcfrs_console_cbaud |= i;
1827 mcfrs_init_console(); /* make sure baud rate changes */
1832 static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1835 return mcfrs_serial_driver;
1840 * Output a single character, using UART polled mode.
1841 * This is used for console output.
1844 void mcfrs_put_char(char ch)
1846 volatile unsigned char *uartp;
1847 unsigned long flags;
1850 uartp = (volatile unsigned char *) (MCF_MBAR +
1851 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1853 local_irq_save(flags);
1854 for (i = 0; (i < 0x10000); i++) {
1855 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1859 uartp[MCFUART_UTB] = ch;
1860 for (i = 0; (i < 0x10000); i++)
1861 if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1865 mcfrs_init_console(); /* try and get it back */
1866 local_irq_restore(flags);
1873 * rs_console_write is registered for printk output.
1876 void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1878 if (!mcfrs_console_inited)
1879 mcfrs_init_console();
1882 mcfrs_put_char('\r');
1883 mcfrs_put_char(*p++);
1888 * declare our consoles
1891 struct console mcfrs_console = {
1893 .write = mcfrs_console_write,
1894 .device = mcfrs_console_device,
1895 .setup = mcfrs_console_setup,
1896 .flags = CON_PRINTBUFFER,
1900 static int __init mcfrs_console_init(void)
1902 register_console(&mcfrs_console);
1906 console_initcall(mcfrs_console_init);
1908 /****************************************************************************/