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);
354 schedule_work(&tty->flip.work);
358 static inline void transmit_chars(struct mcf_serial *info)
360 volatile unsigned char *uartp;
365 /* Send special char - probably flow control */
366 uartp[MCFUART_UTB] = info->x_char;
371 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
372 info->imr &= ~MCFUART_UIR_TXREADY;
373 uartp[MCFUART_UIMR] = info->imr;
377 while (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) {
378 uartp[MCFUART_UTB] = info->xmit_buf[info->xmit_tail++];
379 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
381 if (--info->xmit_cnt <= 0)
385 if (info->xmit_cnt < WAKEUP_CHARS)
386 schedule_work(&info->tqueue);
391 * This is the serial driver's generic interrupt routine
393 irqreturn_t mcfrs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
395 struct mcf_serial *info;
398 info = &mcfrs_table[(irq - IRQBASE)];
399 isr = info->addr[MCFUART_UISR] & info->imr;
401 if (isr & MCFUART_UIR_RXREADY)
403 if (isr & MCFUART_UIR_TXREADY)
404 transmit_chars(info);
409 * -------------------------------------------------------------------
410 * Here ends the serial interrupt routines.
411 * -------------------------------------------------------------------
414 static void mcfrs_offintr(void *private)
416 struct mcf_serial *info = (struct mcf_serial *) private;
417 struct tty_struct *tty;
427 * Change of state on a DCD line.
429 void mcfrs_modem_change(struct mcf_serial *info, int dcd)
431 if (info->count == 0)
434 if (info->flags & ASYNC_CHECK_CD) {
436 wake_up_interruptible(&info->open_wait);
438 schedule_work(&info->tqueue_hangup);
445 unsigned short mcfrs_ppstatus;
448 * This subroutine is called when the RS_TIMER goes off. It is used
449 * to monitor the state of the DCD lines - since they have no edge
450 * sensors and interrupt generators.
452 static void mcfrs_timer(void)
454 unsigned int ppstatus, dcdval, i;
456 ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
458 if (ppstatus != mcfrs_ppstatus) {
459 for (i = 0; (i < 2); i++) {
460 dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
461 if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
462 mcfrs_modem_change(&mcfrs_table[i],
463 ((ppstatus & dcdval) ? 0 : 1));
467 mcfrs_ppstatus = ppstatus;
470 mcfrs_timer_struct.expires = jiffies + HZ/25;
471 add_timer(&mcfrs_timer_struct);
474 #endif /* MCFPP_DCD0 */
478 * This routine is called from the scheduler tqueue when the interrupt
479 * routine has signalled that a hangup has occurred. The path of
480 * hangup processing is:
482 * serial interrupt routine -> (scheduler tqueue) ->
483 * do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
486 static void do_serial_hangup(void *private)
488 struct mcf_serial *info = (struct mcf_serial *) private;
489 struct tty_struct *tty;
498 static int startup(struct mcf_serial * info)
500 volatile unsigned char *uartp;
503 if (info->flags & ASYNC_INITIALIZED)
506 if (!info->xmit_buf) {
507 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
512 local_irq_save(flags);
514 #ifdef SERIAL_DEBUG_OPEN
515 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
519 * Reset UART, get it into known state...
522 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
523 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
524 mcfrs_setsignals(info, 1, 1);
527 clear_bit(TTY_IO_ERROR, &info->tty->flags);
528 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
531 * and set the speed of the serial port
533 mcfrs_change_speed(info);
536 * Lastly enable the UART transmitter and receiver, and
539 info->imr = MCFUART_UIR_RXREADY;
540 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
541 uartp[MCFUART_UIMR] = info->imr;
543 info->flags |= ASYNC_INITIALIZED;
544 local_irq_restore(flags);
549 * This routine will shutdown a serial port; interrupts are disabled, and
550 * DTR is dropped if the hangup on close termio flag is on.
552 static void shutdown(struct mcf_serial * info)
554 volatile unsigned char *uartp;
557 if (!(info->flags & ASYNC_INITIALIZED))
560 #ifdef SERIAL_DEBUG_OPEN
561 printk("Shutting down serial port %d (irq %d)....\n", info->line,
565 local_irq_save(flags);
568 uartp[MCFUART_UIMR] = 0; /* mask all interrupts */
569 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
570 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
572 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
573 mcfrs_setsignals(info, 0, 0);
575 if (info->xmit_buf) {
576 free_page((unsigned long) info->xmit_buf);
581 set_bit(TTY_IO_ERROR, &info->tty->flags);
583 info->flags &= ~ASYNC_INITIALIZED;
584 local_irq_restore(flags);
589 * This routine is called to set the UART divisor registers to match
590 * the specified baud rate for a serial port.
592 static void mcfrs_change_speed(struct mcf_serial *info)
594 volatile unsigned char *uartp;
595 unsigned int baudclk, cflag;
597 unsigned char mr1, mr2;
600 unsigned int fraction;
603 if (!info->tty || !info->tty->termios)
605 cflag = info->tty->termios->c_cflag;
610 printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
617 info->tty->termios->c_cflag &= ~CBAUDEX;
622 mcfrs_setsignals(info, 0, -1);
626 /* compute the baudrate clock */
629 * For the MCF5272, also compute the baudrate fraction.
631 baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
632 fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
634 fraction /= (32 * mcfrs_baud_table[i]);
636 baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
639 info->baud = mcfrs_baud_table[i];
641 mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
644 switch (cflag & CSIZE) {
645 case CS5: mr1 |= MCFUART_MR1_CS5; break;
646 case CS6: mr1 |= MCFUART_MR1_CS6; break;
647 case CS7: mr1 |= MCFUART_MR1_CS7; break;
649 default: mr1 |= MCFUART_MR1_CS8; break;
652 if (cflag & PARENB) {
653 if (cflag & CMSPAR) {
655 mr1 |= MCFUART_MR1_PARITYMARK;
657 mr1 |= MCFUART_MR1_PARITYSPACE;
660 mr1 |= MCFUART_MR1_PARITYODD;
662 mr1 |= MCFUART_MR1_PARITYEVEN;
665 mr1 |= MCFUART_MR1_PARITYNONE;
669 mr2 |= MCFUART_MR2_STOP2;
671 mr2 |= MCFUART_MR2_STOP1;
673 if (cflag & CRTSCTS) {
674 mr1 |= MCFUART_MR1_RXRTS;
675 mr2 |= MCFUART_MR2_TXCTS;
679 info->flags &= ~ASYNC_CHECK_CD;
681 info->flags |= ASYNC_CHECK_CD;
685 local_irq_save(flags);
687 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
691 Note: pg 12-16 of MCF5206e User's Manual states that a
692 software reset should be performed prior to changing
693 UMR1,2, UCSR, UACR, bit 7
695 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
696 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
697 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
698 uartp[MCFUART_UMR] = mr1;
699 uartp[MCFUART_UMR] = mr2;
700 uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8; /* set msb byte */
701 uartp[MCFUART_UBG2] = (baudclk & 0xff); /* set lsb byte */
703 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
705 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
706 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
707 mcfrs_setsignals(info, 1, -1);
708 local_irq_restore(flags);
712 static void mcfrs_flush_chars(struct tty_struct *tty)
714 volatile unsigned char *uartp;
715 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
718 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
721 uartp = (volatile unsigned char *) info->addr;
724 * re-enable receiver interrupt
726 local_irq_save(flags);
727 if ((!(info->imr & MCFUART_UIR_RXREADY)) &&
728 (info->flags & ASYNC_INITIALIZED) ) {
729 info->imr |= MCFUART_UIR_RXREADY;
730 uartp[MCFUART_UIMR] = info->imr;
732 local_irq_restore(flags);
734 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
738 /* Enable transmitter */
739 local_irq_save(flags);
740 info->imr |= MCFUART_UIR_TXREADY;
741 uartp[MCFUART_UIMR] = info->imr;
742 local_irq_restore(flags);
745 static int mcfrs_write(struct tty_struct * tty,
746 const unsigned char *buf, int count)
748 volatile unsigned char *uartp;
749 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
754 printk("%s(%d): mcfrs_write(tty=%x,buf=%x,count=%d)\n",
755 __FILE__, __LINE__, (int)tty, (int)buf, count);
758 if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
761 if (!tty || !info->xmit_buf)
764 local_save_flags(flags);
767 c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
768 ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
769 local_irq_restore(flags);
774 memcpy(info->xmit_buf + info->xmit_head, buf, c);
777 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
779 local_irq_restore(flags);
788 info->imr |= MCFUART_UIR_TXREADY;
789 uartp[MCFUART_UIMR] = info->imr;
790 local_irq_restore(flags);
795 static int mcfrs_write_room(struct tty_struct *tty)
797 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
800 if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
802 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
808 static int mcfrs_chars_in_buffer(struct tty_struct *tty)
810 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
812 if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
814 return info->xmit_cnt;
817 static void mcfrs_flush_buffer(struct tty_struct *tty)
819 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
822 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
825 local_irq_save(flags);
826 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
827 local_irq_restore(flags);
833 * ------------------------------------------------------------
836 * This routine is called by the upper-layer tty layer to signal that
837 * incoming characters should be throttled.
838 * ------------------------------------------------------------
840 static void mcfrs_throttle(struct tty_struct * tty)
842 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
843 #ifdef SERIAL_DEBUG_THROTTLE
846 printk("throttle %s: %d....\n", _tty_name(tty, buf),
847 tty->ldisc.chars_in_buffer(tty));
850 if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
854 info->x_char = STOP_CHAR(tty);
856 /* Turn off RTS line (do this atomic) */
859 static void mcfrs_unthrottle(struct tty_struct * tty)
861 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
862 #ifdef SERIAL_DEBUG_THROTTLE
865 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
866 tty->ldisc.chars_in_buffer(tty));
869 if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
876 info->x_char = START_CHAR(tty);
879 /* Assert RTS line (do this atomic) */
883 * ------------------------------------------------------------
884 * mcfrs_ioctl() and friends
885 * ------------------------------------------------------------
888 static int get_serial_info(struct mcf_serial * info,
889 struct serial_struct * retinfo)
891 struct serial_struct tmp;
895 memset(&tmp, 0, sizeof(tmp));
896 tmp.type = info->type;
897 tmp.line = info->line;
898 tmp.port = (unsigned int) info->addr;
900 tmp.flags = info->flags;
901 tmp.baud_base = info->baud_base;
902 tmp.close_delay = info->close_delay;
903 tmp.closing_wait = info->closing_wait;
904 tmp.custom_divisor = info->custom_divisor;
905 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
908 static int set_serial_info(struct mcf_serial * info,
909 struct serial_struct * new_info)
911 struct serial_struct new_serial;
912 struct mcf_serial old_info;
917 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
921 if (!capable(CAP_SYS_ADMIN)) {
922 if ((new_serial.baud_base != info->baud_base) ||
923 (new_serial.type != info->type) ||
924 (new_serial.close_delay != info->close_delay) ||
925 ((new_serial.flags & ~ASYNC_USR_MASK) !=
926 (info->flags & ~ASYNC_USR_MASK)))
928 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
929 (new_serial.flags & ASYNC_USR_MASK));
930 info->custom_divisor = new_serial.custom_divisor;
938 * OK, past this point, all the error checking has been done.
939 * At this point, we start making changes.....
942 info->baud_base = new_serial.baud_base;
943 info->flags = ((info->flags & ~ASYNC_FLAGS) |
944 (new_serial.flags & ASYNC_FLAGS));
945 info->type = new_serial.type;
946 info->close_delay = new_serial.close_delay;
947 info->closing_wait = new_serial.closing_wait;
950 retval = startup(info);
955 * get_lsr_info - get line status register info
957 * Purpose: Let user call ioctl() to get info when the UART physically
958 * is emptied. On bus types like RS485, the transmitter must
959 * release the bus after transmitting. This must be done when
960 * the transmit shift register is empty, not be done when the
961 * transmit holding register is empty. This functionality
962 * allows an RS485 driver to be written in user space.
964 static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
966 volatile unsigned char *uartp;
968 unsigned char status;
970 local_irq_save(flags);
972 status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
973 local_irq_restore(flags);
975 return put_user(status,value);
979 * This routine sends a break character out the serial port.
981 static void send_break( struct mcf_serial * info, int duration)
983 volatile unsigned char *uartp;
988 set_current_state(TASK_INTERRUPTIBLE);
991 local_irq_save(flags);
992 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
993 schedule_timeout(duration);
994 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
995 local_irq_restore(flags);
998 static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
1000 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1002 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1004 if (tty->flags & (1 << TTY_IO_ERROR))
1007 return mcfrs_getsignals(info);
1010 static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
1011 unsigned int set, unsigned int clear)
1013 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1014 int rts = -1, dtr = -1;
1016 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1018 if (tty->flags & (1 << TTY_IO_ERROR))
1021 if (set & TIOCM_RTS)
1023 if (set & TIOCM_DTR)
1025 if (clear & TIOCM_RTS)
1027 if (clear & TIOCM_DTR)
1030 mcfrs_setsignals(info, dtr, rts);
1035 static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
1036 unsigned int cmd, unsigned long arg)
1038 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1041 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1044 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1045 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1046 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1047 if (tty->flags & (1 << TTY_IO_ERROR))
1052 case TCSBRK: /* SVID version: non-zero arg --> no break */
1053 retval = tty_check_change(tty);
1056 tty_wait_until_sent(tty, 0);
1058 send_break(info, HZ/4); /* 1/4 second */
1060 case TCSBRKP: /* support for POSIX tcsendbreak() */
1061 retval = tty_check_change(tty);
1064 tty_wait_until_sent(tty, 0);
1065 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1068 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1069 (unsigned long *) arg);
1074 get_user(arg, (unsigned long *) arg);
1075 tty->termios->c_cflag =
1076 ((tty->termios->c_cflag & ~CLOCAL) |
1077 (arg ? CLOCAL : 0));
1080 if (access_ok(VERIFY_WRITE, (void *) arg,
1081 sizeof(struct serial_struct)))
1082 return get_serial_info(info,
1083 (struct serial_struct *) arg);
1086 return set_serial_info(info,
1087 (struct serial_struct *) arg);
1088 case TIOCSERGETLSR: /* Get line status register */
1089 if (access_ok(VERIFY_WRITE, (void *) arg,
1090 sizeof(unsigned int)))
1091 return get_lsr_info(info, (unsigned int *) arg);
1093 case TIOCSERGSTRUCT:
1094 error = copy_to_user((struct mcf_serial *) arg,
1095 info, sizeof(struct mcf_serial));
1103 get_user(val, (unsigned int *) arg);
1104 mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1109 val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1110 put_user(val, (unsigned int *) arg);
1116 return -ENOIOCTLCMD;
1121 static void mcfrs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1123 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1125 if (tty->termios->c_cflag == old_termios->c_cflag)
1128 mcfrs_change_speed(info);
1130 if ((old_termios->c_cflag & CRTSCTS) &&
1131 !(tty->termios->c_cflag & CRTSCTS)) {
1132 tty->hw_stopped = 0;
1133 mcfrs_setsignals(info, -1, 1);
1141 * ------------------------------------------------------------
1144 * This routine is called when the serial port gets closed. First, we
1145 * wait for the last remaining data to be sent. Then, we unlink its
1146 * S structure from the interrupt chain if necessary, and we free
1147 * that IRQ if nothing is left in the chain.
1148 * ------------------------------------------------------------
1150 static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1152 volatile unsigned char *uartp;
1153 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1154 unsigned long flags;
1156 if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1159 local_irq_save(flags);
1161 if (tty_hung_up_p(filp)) {
1162 local_irq_restore(flags);
1166 #ifdef SERIAL_DEBUG_OPEN
1167 printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1169 if ((tty->count == 1) && (info->count != 1)) {
1171 * Uh, oh. tty->count is 1, which means that the tty
1172 * structure will be freed. Info->count should always
1173 * be one in these conditions. If it's greater than
1174 * one, we've got real problems, since it means the
1175 * serial port won't be shutdown.
1177 printk("MCFRS: bad serial port count; tty->count is 1, "
1178 "info->count is %d\n", info->count);
1181 if (--info->count < 0) {
1182 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1183 info->line, info->count);
1187 local_irq_restore(flags);
1190 info->flags |= ASYNC_CLOSING;
1193 * Now we wait for the transmit buffer to clear; and we notify
1194 * the line discipline to only process XON/XOFF characters.
1197 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1198 tty_wait_until_sent(tty, info->closing_wait);
1201 * At this point we stop accepting input. To do this, we
1202 * disable the receive line status interrupts, and tell the
1203 * interrupt driver to stop checking the data ready bit in the
1204 * line status register.
1206 info->imr &= ~MCFUART_UIR_RXREADY;
1208 uartp[MCFUART_UIMR] = info->imr;
1211 /* FIXME: do we need to keep this enabled for console?? */
1212 if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
1213 /* Do not disable the UART */ ;
1217 if (tty->driver->flush_buffer)
1218 tty->driver->flush_buffer(tty);
1219 tty_ldisc_flush(tty);
1225 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1226 if (tty->ldisc.close)
1227 (tty->ldisc.close)(tty);
1228 tty->ldisc = ldiscs[N_TTY];
1229 tty->termios->c_line = N_TTY;
1230 if (tty->ldisc.open)
1231 (tty->ldisc.open)(tty);
1234 if (info->blocked_open) {
1235 if (info->close_delay) {
1236 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1238 wake_up_interruptible(&info->open_wait);
1240 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1241 wake_up_interruptible(&info->close_wait);
1242 local_irq_restore(flags);
1246 * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1249 mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1252 #define MCF5272_FIFO_SIZE 25 /* fifo size + shift reg */
1254 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1255 volatile unsigned char *uartp;
1256 unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1258 if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1261 orig_jiffies = jiffies;
1264 * Set the check interval to be 1/5 of the approximate time
1265 * to send the entire fifo, and make it at least 1. The check
1266 * interval should also be less than the timeout.
1268 * Note: we have to use pretty tight timings here to satisfy
1271 fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1272 char_time = fifo_time / 5;
1275 if (timeout && timeout < char_time)
1276 char_time = timeout;
1279 * Clamp the timeout period at 2 * the time to empty the
1280 * fifo. Just to be safe, set the minimum at .5 seconds.
1283 if (fifo_time < (HZ/2))
1285 if (!timeout || timeout > fifo_time)
1286 timeout = fifo_time;
1289 * Account for the number of bytes in the UART
1290 * transmitter FIFO plus any byte being shifted out.
1292 uartp = (volatile unsigned char *) info->addr;
1294 fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1295 if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1296 MCFUART_USR_TXEMPTY)) ==
1297 MCFUART_USR_TXREADY)
1301 msleep_interruptible(jiffies_to_msecs(char_time));
1302 if (signal_pending(current))
1304 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1309 * For the other coldfire models, assume all data has been sent
1315 * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1317 void mcfrs_hangup(struct tty_struct *tty)
1319 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1321 if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
1324 mcfrs_flush_buffer(tty);
1328 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1330 wake_up_interruptible(&info->open_wait);
1334 * ------------------------------------------------------------
1335 * mcfrs_open() and friends
1336 * ------------------------------------------------------------
1338 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1339 struct mcf_serial *info)
1341 DECLARE_WAITQUEUE(wait, current);
1346 * If the device is in the middle of being closed, then block
1347 * until it's done, and then try again.
1349 if (info->flags & ASYNC_CLOSING) {
1350 interruptible_sleep_on(&info->close_wait);
1351 #ifdef SERIAL_DO_RESTART
1352 if (info->flags & ASYNC_HUP_NOTIFY)
1355 return -ERESTARTSYS;
1362 * If non-blocking mode is set, or the port is not enabled,
1363 * then make the check up front and then exit.
1365 if ((filp->f_flags & O_NONBLOCK) ||
1366 (tty->flags & (1 << TTY_IO_ERROR))) {
1367 info->flags |= ASYNC_NORMAL_ACTIVE;
1371 if (tty->termios->c_cflag & CLOCAL)
1375 * Block waiting for the carrier detect and the line to become
1376 * free (i.e., not in use by the callout). While we are in
1377 * this loop, info->count is dropped by one, so that
1378 * mcfrs_close() knows when to free things. We restore it upon
1379 * exit, either normal or abnormal.
1382 add_wait_queue(&info->open_wait, &wait);
1383 #ifdef SERIAL_DEBUG_OPEN
1384 printk("block_til_ready before block: ttyS%d, count = %d\n",
1385 info->line, info->count);
1388 info->blocked_open++;
1390 local_irq_disable();
1391 mcfrs_setsignals(info, 1, 1);
1393 current->state = TASK_INTERRUPTIBLE;
1394 if (tty_hung_up_p(filp) ||
1395 !(info->flags & ASYNC_INITIALIZED)) {
1396 #ifdef SERIAL_DO_RESTART
1397 if (info->flags & ASYNC_HUP_NOTIFY)
1400 retval = -ERESTARTSYS;
1406 if (!(info->flags & ASYNC_CLOSING) &&
1407 (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1409 if (signal_pending(current)) {
1410 retval = -ERESTARTSYS;
1413 #ifdef SERIAL_DEBUG_OPEN
1414 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1415 info->line, info->count);
1419 current->state = TASK_RUNNING;
1420 remove_wait_queue(&info->open_wait, &wait);
1421 if (!tty_hung_up_p(filp))
1423 info->blocked_open--;
1424 #ifdef SERIAL_DEBUG_OPEN
1425 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1426 info->line, info->count);
1430 info->flags |= ASYNC_NORMAL_ACTIVE;
1435 * This routine is called whenever a serial port is opened. It
1436 * enables interrupts for a serial port, linking in its structure into
1437 * the IRQ chain. It also performs the serial-specific
1438 * initialization for the tty structure.
1440 int mcfrs_open(struct tty_struct *tty, struct file * filp)
1442 struct mcf_serial *info;
1446 if ((line < 0) || (line >= NR_PORTS))
1448 info = mcfrs_table + line;
1449 if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1451 #ifdef SERIAL_DEBUG_OPEN
1452 printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1455 tty->driver_data = info;
1459 * Start up serial port
1461 retval = startup(info);
1465 retval = block_til_ready(tty, filp, info);
1467 #ifdef SERIAL_DEBUG_OPEN
1468 printk("mcfrs_open returning after block_til_ready with %d\n",
1474 #ifdef SERIAL_DEBUG_OPEN
1475 printk("mcfrs_open %s successful...\n", tty->name);
1481 * Based on the line number set up the internal interrupt stuff.
1483 static void mcfrs_irqinit(struct mcf_serial *info)
1485 #if defined(CONFIG_M5272)
1486 volatile unsigned long *icrp;
1487 volatile unsigned long *portp;
1488 volatile unsigned char *uartp;
1491 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1493 switch (info->line) {
1501 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1506 /* Enable the output lines for the serial ports */
1507 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
1508 *portp = (*portp & ~0x000000ff) | 0x00000055;
1509 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
1510 *portp = (*portp & ~0x000003fc) | 0x000002a8;
1511 #elif defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x)
1512 volatile unsigned char *icrp, *uartp;
1513 volatile unsigned long *imrp;
1517 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1518 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1519 *icrp = 0x30 + info->line; /* level 6, line based priority */
1521 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1523 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1524 #elif defined(CONFIG_M520x)
1525 volatile unsigned char *icrp, *uartp;
1526 volatile unsigned long *imrp;
1530 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1531 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1534 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1536 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1537 if (info->line < 2) {
1538 unsigned short *uart_par;
1539 uart_par = (unsigned short *)(MCF_IPSBAR + MCF_GPIO_PAR_UART);
1540 if (info->line == 0)
1541 *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD0
1542 | MCF_GPIO_PAR_UART_PAR_URXD0;
1543 else if (info->line == 1)
1544 *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD1
1545 | MCF_GPIO_PAR_UART_PAR_URXD1;
1546 } else if (info->line == 2) {
1547 unsigned char *feci2c_par;
1548 feci2c_par = (unsigned char *)(MCF_IPSBAR + MCF_GPIO_PAR_FECI2C);
1549 *feci2c_par &= ~0x0F;
1550 *feci2c_par |= MCF_GPIO_PAR_FECI2C_PAR_SCL_UTXD2
1551 | MCF_GPIO_PAR_FECI2C_PAR_SDA_URXD2;
1554 volatile unsigned char *icrp, *uartp;
1556 switch (info->line) {
1558 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1559 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1561 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1564 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1565 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1567 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1570 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1576 uartp[MCFUART_UIVR] = info->irq;
1579 /* Clear mask, so no surprise interrupts. */
1580 uartp[MCFUART_UIMR] = 0;
1582 if (request_irq(info->irq, mcfrs_interrupt, SA_INTERRUPT,
1583 "ColdFire UART", NULL)) {
1584 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1585 "vector=%d\n", info->line, info->irq);
1592 char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1596 * Serial stats reporting...
1598 int mcfrs_readproc(char *page, char **start, off_t off, int count,
1599 int *eof, void *data)
1601 struct mcf_serial *info;
1605 len = sprintf(page, mcfrs_drivername);
1606 for (i = 0; (i < NR_PORTS); i++) {
1607 info = &mcfrs_table[i];
1608 len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1609 i, (unsigned int) info->addr, info->irq, info->baud);
1610 if (info->stats.rx || info->stats.tx)
1611 len += sprintf((page + len), "tx:%d rx:%d ",
1612 info->stats.tx, info->stats.rx);
1613 if (info->stats.rxframing)
1614 len += sprintf((page + len), "fe:%d ",
1615 info->stats.rxframing);
1616 if (info->stats.rxparity)
1617 len += sprintf((page + len), "pe:%d ",
1618 info->stats.rxparity);
1619 if (info->stats.rxbreak)
1620 len += sprintf((page + len), "brk:%d ",
1621 info->stats.rxbreak);
1622 if (info->stats.rxoverrun)
1623 len += sprintf((page + len), "oe:%d ",
1624 info->stats.rxoverrun);
1626 str[0] = str[1] = 0;
1627 if ((sigs = mcfrs_getsignals(info))) {
1628 if (sigs & TIOCM_RTS)
1629 strcat(str, "|RTS");
1630 if (sigs & TIOCM_CTS)
1631 strcat(str, "|CTS");
1632 if (sigs & TIOCM_DTR)
1633 strcat(str, "|DTR");
1634 if (sigs & TIOCM_CD)
1638 len += sprintf((page + len), "%s\n", &str[1]);
1645 /* Finally, routines used to initialize the serial driver. */
1647 static void show_serial_version(void)
1649 printk(mcfrs_drivername);
1652 static struct tty_operations mcfrs_ops = {
1654 .close = mcfrs_close,
1655 .write = mcfrs_write,
1656 .flush_chars = mcfrs_flush_chars,
1657 .write_room = mcfrs_write_room,
1658 .chars_in_buffer = mcfrs_chars_in_buffer,
1659 .flush_buffer = mcfrs_flush_buffer,
1660 .ioctl = mcfrs_ioctl,
1661 .throttle = mcfrs_throttle,
1662 .unthrottle = mcfrs_unthrottle,
1663 .set_termios = mcfrs_set_termios,
1665 .start = mcfrs_start,
1666 .hangup = mcfrs_hangup,
1667 .read_proc = mcfrs_readproc,
1668 .wait_until_sent = mcfrs_wait_until_sent,
1669 .tiocmget = mcfrs_tiocmget,
1670 .tiocmset = mcfrs_tiocmset,
1673 /* mcfrs_init inits the driver */
1677 struct mcf_serial *info;
1678 unsigned long flags;
1681 /* Setup base handler, and timer table. */
1683 init_timer(&mcfrs_timer_struct);
1684 mcfrs_timer_struct.function = mcfrs_timer;
1685 mcfrs_timer_struct.data = 0;
1686 mcfrs_timer_struct.expires = jiffies + HZ/25;
1687 add_timer(&mcfrs_timer_struct);
1688 mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1690 mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1691 if (!mcfrs_serial_driver)
1694 show_serial_version();
1696 /* Initialize the tty_driver structure */
1697 mcfrs_serial_driver->owner = THIS_MODULE;
1698 mcfrs_serial_driver->name = "ttyS";
1699 mcfrs_serial_driver->devfs_name = "ttys/";
1700 mcfrs_serial_driver->driver_name = "serial";
1701 mcfrs_serial_driver->major = TTY_MAJOR;
1702 mcfrs_serial_driver->minor_start = 64;
1703 mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1704 mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1705 mcfrs_serial_driver->init_termios = tty_std_termios;
1707 mcfrs_serial_driver->init_termios.c_cflag =
1708 mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1709 mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1711 tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1713 if (tty_register_driver(mcfrs_serial_driver)) {
1714 printk("MCFRS: Couldn't register serial driver\n");
1715 put_tty_driver(mcfrs_serial_driver);
1719 local_irq_save(flags);
1722 * Configure all the attached serial ports.
1724 for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1725 info->magic = SERIAL_MAGIC;
1728 info->custom_divisor = 16;
1729 info->close_delay = 50;
1730 info->closing_wait = 3000;
1734 info->blocked_open = 0;
1735 INIT_WORK(&info->tqueue, mcfrs_offintr, info);
1736 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1737 init_waitqueue_head(&info->open_wait);
1738 init_waitqueue_head(&info->close_wait);
1741 mcfrs_setsignals(info, 0, 0);
1742 mcfrs_irqinit(info);
1744 printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1745 (unsigned int) info->addr, info->irq);
1746 printk(" is a builtin ColdFire UART\n");
1749 local_irq_restore(flags);
1753 module_init(mcfrs_init);
1755 /****************************************************************************/
1756 /* Serial Console */
1757 /****************************************************************************/
1760 * Quick and dirty UART initialization, for console output.
1763 void mcfrs_init_console(void)
1765 volatile unsigned char *uartp;
1769 * Reset UART, get it into known state...
1771 uartp = (volatile unsigned char *) (MCF_MBAR +
1772 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1774 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
1775 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
1776 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
1779 * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1781 uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1782 uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1784 clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1785 uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8; /* set msb baud */
1786 uartp[MCFUART_UBG2] = (clk & 0xff); /* set lsb baud */
1788 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1789 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1791 mcfrs_console_inited++;
1797 * Setup for console. Argument comes from the boot command line.
1800 int mcfrs_console_setup(struct console *cp, char *arg)
1802 int i, n = CONSOLE_BAUD_RATE;
1807 if (!strncmp(cp->name, "ttyS", 4))
1808 mcfrs_console_port = cp->index;
1809 else if (!strncmp(cp->name, "cua", 3))
1810 mcfrs_console_port = cp->index;
1815 n = simple_strtoul(arg,NULL,0);
1816 for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1817 if (mcfrs_baud_table[i] == n)
1819 if (i < MCFRS_BAUD_TABLE_SIZE) {
1820 mcfrs_console_baud = n;
1821 mcfrs_console_cbaud = 0;
1823 mcfrs_console_cbaud |= CBAUDEX;
1826 mcfrs_console_cbaud |= i;
1828 mcfrs_init_console(); /* make sure baud rate changes */
1833 static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1836 return mcfrs_serial_driver;
1841 * Output a single character, using UART polled mode.
1842 * This is used for console output.
1845 void mcfrs_put_char(char ch)
1847 volatile unsigned char *uartp;
1848 unsigned long flags;
1851 uartp = (volatile unsigned char *) (MCF_MBAR +
1852 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1854 local_irq_save(flags);
1855 for (i = 0; (i < 0x10000); i++) {
1856 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1860 uartp[MCFUART_UTB] = ch;
1861 for (i = 0; (i < 0x10000); i++)
1862 if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1866 mcfrs_init_console(); /* try and get it back */
1867 local_irq_restore(flags);
1874 * rs_console_write is registered for printk output.
1877 void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1879 if (!mcfrs_console_inited)
1880 mcfrs_init_console();
1883 mcfrs_put_char('\r');
1884 mcfrs_put_char(*p++);
1889 * declare our consoles
1892 struct console mcfrs_console = {
1894 .write = mcfrs_console_write,
1895 .device = mcfrs_console_device,
1896 .setup = mcfrs_console_setup,
1897 .flags = CON_PRINTBUFFER,
1901 static int __init mcfrs_console_init(void)
1903 register_console(&mcfrs_console);
1907 console_initcall(mcfrs_console_init);
1909 /****************************************************************************/