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_MOTOROLA) || defined(CONFIG_senTec) || defined(CONFIG_SNEHA)
61 #define CONSOLE_BAUD_RATE 19200
62 #define DEFAULT_CBAUD B19200
65 #if defined(CONFIG_HW_FEITH)
66 #define CONSOLE_BAUD_RATE 38400
67 #define DEFAULT_CBAUD B38400
70 #ifndef CONSOLE_BAUD_RATE
71 #define CONSOLE_BAUD_RATE 9600
72 #define DEFAULT_CBAUD B9600
75 int mcfrs_console_inited = 0;
76 int mcfrs_console_port = -1;
77 int mcfrs_console_baud = CONSOLE_BAUD_RATE;
78 int mcfrs_console_cbaud = DEFAULT_CBAUD;
81 * Driver data structures.
83 static struct tty_driver *mcfrs_serial_driver;
85 /* number of characters left in xmit buffer before we ask for more */
86 #define WAKEUP_CHARS 256
90 #undef SERIAL_DEBUG_OPEN
91 #undef SERIAL_DEBUG_FLOW
93 #if defined(CONFIG_M527x) || defined(CONFIG_M528x)
94 #define IRQBASE (MCFINT_VECBASE+MCFINT_UART0)
100 * Configuration table, UARTs to look for at startup.
102 static struct mcf_serial mcfrs_table[] = {
105 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE1),
107 .flags = ASYNC_BOOT_AUTOCONF,
111 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE2),
113 .flags = ASYNC_BOOT_AUTOCONF,
118 #define NR_PORTS (sizeof(mcfrs_table) / sizeof(struct mcf_serial))
121 * This is used to figure out the divisor speeds and the timeouts.
123 static int mcfrs_baud_table[] = {
124 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
125 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0
127 #define MCFRS_BAUD_TABLE_SIZE \
128 (sizeof(mcfrs_baud_table)/sizeof(mcfrs_baud_table[0]))
131 #ifdef CONFIG_MAGIC_SYSRQ
133 * Magic system request keys. Used for debugging...
135 extern int magic_sysrq_key(int ch);
140 * Forware declarations...
142 static void mcfrs_change_speed(struct mcf_serial *info);
143 static void mcfrs_wait_until_sent(struct tty_struct *tty, int timeout);
146 static inline int serial_paranoia_check(struct mcf_serial *info,
147 char *name, const char *routine)
149 #ifdef SERIAL_PARANOIA_CHECK
150 static const char badmagic[] =
151 "MCFRS(warning): bad magic number for serial struct %s in %s\n";
152 static const char badinfo[] =
153 "MCFRS(warning): null mcf_serial for %s in %s\n";
156 printk(badinfo, name, routine);
159 if (info->magic != SERIAL_MAGIC) {
160 printk(badmagic, name, routine);
168 * Sets or clears DTR and RTS on the requested line.
170 static void mcfrs_setsignals(struct mcf_serial *info, int dtr, int rts)
172 volatile unsigned char *uartp;
176 printk("%s(%d): mcfrs_setsignals(info=%x,dtr=%d,rts=%d)\n",
177 __FILE__, __LINE__, info, dtr, rts);
180 local_irq_save(flags);
184 mcf_setppdata(MCFPP_DTR1, (dtr ? 0 : MCFPP_DTR1));
186 mcf_setppdata(MCFPP_DTR0, (dtr ? 0 : MCFPP_DTR0));
192 info->sigs |= TIOCM_RTS;
193 uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
195 info->sigs &= ~TIOCM_RTS;
196 uartp[MCFUART_UOP0] = MCFUART_UOP_RTS;
199 local_irq_restore(flags);
204 * Gets values of serial signals.
206 static int mcfrs_getsignals(struct mcf_serial *info)
208 volatile unsigned char *uartp;
211 #if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
212 unsigned short ppdata;
216 printk("%s(%d): mcfrs_getsignals(info=%x)\n", __FILE__, __LINE__);
219 local_irq_save(flags);
221 sigs = (uartp[MCFUART_UIPR] & MCFUART_UIPR_CTS) ? 0 : TIOCM_CTS;
222 sigs |= (info->sigs & TIOCM_RTS);
227 ppdata = mcf_getppdata();
228 if (info->line == 0) {
229 sigs |= (ppdata & MCFPP_DCD0) ? 0 : TIOCM_CD;
230 sigs |= (ppdata & MCFPP_DTR0) ? 0 : TIOCM_DTR;
231 } else if (info->line == 1) {
232 sigs |= (ppdata & MCFPP_DCD1) ? 0 : TIOCM_CD;
233 sigs |= (ppdata & MCFPP_DTR1) ? 0 : TIOCM_DTR;
238 local_irq_restore(flags);
243 * ------------------------------------------------------------
244 * mcfrs_stop() and mcfrs_start()
246 * This routines are called before setting or resetting tty->stopped.
247 * They enable or disable transmitter interrupts, as necessary.
248 * ------------------------------------------------------------
250 static void mcfrs_stop(struct tty_struct *tty)
252 volatile unsigned char *uartp;
253 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
256 if (serial_paranoia_check(info, tty->name, "mcfrs_stop"))
259 local_irq_save(flags);
261 info->imr &= ~MCFUART_UIR_TXREADY;
262 uartp[MCFUART_UIMR] = info->imr;
263 local_irq_restore(flags);
266 static void mcfrs_start(struct tty_struct *tty)
268 volatile unsigned char *uartp;
269 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
272 if (serial_paranoia_check(info, tty->name, "mcfrs_start"))
275 local_irq_save(flags);
276 if (info->xmit_cnt && info->xmit_buf) {
278 info->imr |= MCFUART_UIR_TXREADY;
279 uartp[MCFUART_UIMR] = info->imr;
281 local_irq_restore(flags);
285 * ----------------------------------------------------------------------
287 * Here starts the interrupt handling routines. All of the following
288 * subroutines are declared as inline and are folded into
289 * mcfrs_interrupt(). They were separated out for readability's sake.
291 * Note: mcfrs_interrupt() is a "fast" interrupt, which means that it
292 * runs with interrupts turned off. People who may want to modify
293 * mcfrs_interrupt() should try to keep the interrupt handler as fast as
294 * possible. After you are done making modifications, it is not a bad
297 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
299 * and look at the resulting assemble code in serial.s.
301 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
302 * -----------------------------------------------------------------------
305 static inline void receive_chars(struct mcf_serial *info)
307 volatile unsigned char *uartp;
308 struct tty_struct *tty = info->tty;
309 unsigned char status, ch;
316 while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
318 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
321 ch = uartp[MCFUART_URB];
324 #ifdef CONFIG_MAGIC_SYSRQ
325 if (mcfrs_console_inited && (info->line == mcfrs_console_port)) {
326 if (magic_sysrq_key(ch))
332 if (status & MCFUART_USR_RXERR) {
333 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
334 if (status & MCFUART_USR_RXBREAK) {
335 info->stats.rxbreak++;
336 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
337 } else if (status & MCFUART_USR_RXPARITY) {
338 info->stats.rxparity++;
339 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
340 } else if (status & MCFUART_USR_RXOVERRUN) {
341 info->stats.rxoverrun++;
342 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
343 } else if (status & MCFUART_USR_RXFRAMING) {
344 info->stats.rxframing++;
345 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
347 /* This should never happen... */
348 *tty->flip.flag_buf_ptr++ = 0;
351 *tty->flip.flag_buf_ptr++ = 0;
353 *tty->flip.char_buf_ptr++ = ch;
356 schedule_work(&tty->flip.work);
360 static inline void transmit_chars(struct mcf_serial *info)
362 volatile unsigned char *uartp;
367 /* Send special char - probably flow control */
368 uartp[MCFUART_UTB] = info->x_char;
373 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
374 info->imr &= ~MCFUART_UIR_TXREADY;
375 uartp[MCFUART_UIMR] = info->imr;
379 while (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) {
380 uartp[MCFUART_UTB] = info->xmit_buf[info->xmit_tail++];
381 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
383 if (--info->xmit_cnt <= 0)
387 if (info->xmit_cnt < WAKEUP_CHARS)
388 schedule_work(&info->tqueue);
393 * This is the serial driver's generic interrupt routine
395 irqreturn_t mcfrs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
397 struct mcf_serial *info;
400 info = &mcfrs_table[(irq - IRQBASE)];
401 isr = info->addr[MCFUART_UISR] & info->imr;
403 if (isr & MCFUART_UIR_RXREADY)
405 if (isr & MCFUART_UIR_TXREADY)
406 transmit_chars(info);
411 * -------------------------------------------------------------------
412 * Here ends the serial interrupt routines.
413 * -------------------------------------------------------------------
416 static void mcfrs_offintr(void *private)
418 struct mcf_serial *info = (struct mcf_serial *) private;
419 struct tty_struct *tty;
429 * Change of state on a DCD line.
431 void mcfrs_modem_change(struct mcf_serial *info, int dcd)
433 if (info->count == 0)
436 if (info->flags & ASYNC_CHECK_CD) {
438 wake_up_interruptible(&info->open_wait);
440 schedule_work(&info->tqueue_hangup);
447 unsigned short mcfrs_ppstatus;
450 * This subroutine is called when the RS_TIMER goes off. It is used
451 * to monitor the state of the DCD lines - since they have no edge
452 * sensors and interrupt generators.
454 static void mcfrs_timer(void)
456 unsigned int ppstatus, dcdval, i;
458 ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
460 if (ppstatus != mcfrs_ppstatus) {
461 for (i = 0; (i < 2); i++) {
462 dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
463 if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
464 mcfrs_modem_change(&mcfrs_table[i],
465 ((ppstatus & dcdval) ? 0 : 1));
469 mcfrs_ppstatus = ppstatus;
472 mcfrs_timer_struct.expires = jiffies + HZ/25;
473 add_timer(&mcfrs_timer_struct);
476 #endif /* MCFPP_DCD0 */
480 * This routine is called from the scheduler tqueue when the interrupt
481 * routine has signalled that a hangup has occurred. The path of
482 * hangup processing is:
484 * serial interrupt routine -> (scheduler tqueue) ->
485 * do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
488 static void do_serial_hangup(void *private)
490 struct mcf_serial *info = (struct mcf_serial *) private;
491 struct tty_struct *tty;
500 static int startup(struct mcf_serial * info)
502 volatile unsigned char *uartp;
505 if (info->flags & ASYNC_INITIALIZED)
508 if (!info->xmit_buf) {
509 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
514 local_irq_save(flags);
516 #ifdef SERIAL_DEBUG_OPEN
517 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
521 * Reset UART, get it into known state...
524 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
525 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
526 mcfrs_setsignals(info, 1, 1);
529 clear_bit(TTY_IO_ERROR, &info->tty->flags);
530 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
533 * and set the speed of the serial port
535 mcfrs_change_speed(info);
538 * Lastly enable the UART transmitter and receiver, and
541 info->imr = MCFUART_UIR_RXREADY;
542 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
543 uartp[MCFUART_UIMR] = info->imr;
545 info->flags |= ASYNC_INITIALIZED;
546 local_irq_restore(flags);
551 * This routine will shutdown a serial port; interrupts are disabled, and
552 * DTR is dropped if the hangup on close termio flag is on.
554 static void shutdown(struct mcf_serial * info)
556 volatile unsigned char *uartp;
559 if (!(info->flags & ASYNC_INITIALIZED))
562 #ifdef SERIAL_DEBUG_OPEN
563 printk("Shutting down serial port %d (irq %d)....\n", info->line,
567 local_irq_save(flags);
570 uartp[MCFUART_UIMR] = 0; /* mask all interrupts */
571 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
572 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
574 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
575 mcfrs_setsignals(info, 0, 0);
577 if (info->xmit_buf) {
578 free_page((unsigned long) info->xmit_buf);
583 set_bit(TTY_IO_ERROR, &info->tty->flags);
585 info->flags &= ~ASYNC_INITIALIZED;
586 local_irq_restore(flags);
591 * This routine is called to set the UART divisor registers to match
592 * the specified baud rate for a serial port.
594 static void mcfrs_change_speed(struct mcf_serial *info)
596 volatile unsigned char *uartp;
597 unsigned int baudclk, cflag;
599 unsigned char mr1, mr2;
602 unsigned int fraction;
605 if (!info->tty || !info->tty->termios)
607 cflag = info->tty->termios->c_cflag;
612 printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
619 info->tty->termios->c_cflag &= ~CBAUDEX;
624 mcfrs_setsignals(info, 0, -1);
628 /* compute the baudrate clock */
631 * For the MCF5272, also compute the baudrate fraction.
633 baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
634 fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
636 fraction /= (32 * mcfrs_baud_table[i]);
638 baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
641 info->baud = mcfrs_baud_table[i];
643 mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
646 switch (cflag & CSIZE) {
647 case CS5: mr1 |= MCFUART_MR1_CS5; break;
648 case CS6: mr1 |= MCFUART_MR1_CS6; break;
649 case CS7: mr1 |= MCFUART_MR1_CS7; break;
651 default: mr1 |= MCFUART_MR1_CS8; break;
654 if (cflag & PARENB) {
655 if (cflag & CMSPAR) {
657 mr1 |= MCFUART_MR1_PARITYMARK;
659 mr1 |= MCFUART_MR1_PARITYSPACE;
662 mr1 |= MCFUART_MR1_PARITYODD;
664 mr1 |= MCFUART_MR1_PARITYEVEN;
667 mr1 |= MCFUART_MR1_PARITYNONE;
671 mr2 |= MCFUART_MR2_STOP2;
673 mr2 |= MCFUART_MR2_STOP1;
675 if (cflag & CRTSCTS) {
676 mr1 |= MCFUART_MR1_RXRTS;
677 mr2 |= MCFUART_MR2_TXCTS;
681 info->flags &= ~ASYNC_CHECK_CD;
683 info->flags |= ASYNC_CHECK_CD;
687 local_irq_save(flags);
689 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
693 Note: pg 12-16 of MCF5206e User's Manual states that a
694 software reset should be performed prior to changing
695 UMR1,2, UCSR, UACR, bit 7
697 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
698 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
699 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
700 uartp[MCFUART_UMR] = mr1;
701 uartp[MCFUART_UMR] = mr2;
702 uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8; /* set msb byte */
703 uartp[MCFUART_UBG2] = (baudclk & 0xff); /* set lsb byte */
705 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
707 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
708 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
709 mcfrs_setsignals(info, 1, -1);
710 local_irq_restore(flags);
714 static void mcfrs_flush_chars(struct tty_struct *tty)
716 volatile unsigned char *uartp;
717 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
720 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
723 uartp = (volatile unsigned char *) info->addr;
726 * re-enable receiver interrupt
728 local_irq_save(flags);
729 if ((!(info->imr & MCFUART_UIR_RXREADY)) &&
730 (info->flags & ASYNC_INITIALIZED) ) {
731 info->imr |= MCFUART_UIR_RXREADY;
732 uartp[MCFUART_UIMR] = info->imr;
734 local_irq_restore(flags);
736 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
740 /* Enable transmitter */
741 local_irq_save(flags);
742 info->imr |= MCFUART_UIR_TXREADY;
743 uartp[MCFUART_UIMR] = info->imr;
744 local_irq_restore(flags);
747 static int mcfrs_write(struct tty_struct * tty,
748 const unsigned char *buf, int count)
750 volatile unsigned char *uartp;
751 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
756 printk("%s(%d): mcfrs_write(tty=%x,buf=%x,count=%d)\n",
757 __FILE__, __LINE__, (int)tty, (int)buf, count);
760 if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
763 if (!tty || !info->xmit_buf)
766 local_save_flags(flags);
769 c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
770 ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
771 local_irq_restore(flags);
776 memcpy(info->xmit_buf + info->xmit_head, buf, c);
779 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
781 local_irq_restore(flags);
790 info->imr |= MCFUART_UIR_TXREADY;
791 uartp[MCFUART_UIMR] = info->imr;
792 local_irq_restore(flags);
797 static int mcfrs_write_room(struct tty_struct *tty)
799 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
802 if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
804 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
810 static int mcfrs_chars_in_buffer(struct tty_struct *tty)
812 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
814 if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
816 return info->xmit_cnt;
819 static void mcfrs_flush_buffer(struct tty_struct *tty)
821 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
824 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
827 local_irq_save(flags);
828 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
829 local_irq_restore(flags);
835 * ------------------------------------------------------------
838 * This routine is called by the upper-layer tty layer to signal that
839 * incoming characters should be throttled.
840 * ------------------------------------------------------------
842 static void mcfrs_throttle(struct tty_struct * tty)
844 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
845 #ifdef SERIAL_DEBUG_THROTTLE
848 printk("throttle %s: %d....\n", _tty_name(tty, buf),
849 tty->ldisc.chars_in_buffer(tty));
852 if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
856 info->x_char = STOP_CHAR(tty);
858 /* Turn off RTS line (do this atomic) */
861 static void mcfrs_unthrottle(struct tty_struct * tty)
863 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
864 #ifdef SERIAL_DEBUG_THROTTLE
867 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
868 tty->ldisc.chars_in_buffer(tty));
871 if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
878 info->x_char = START_CHAR(tty);
881 /* Assert RTS line (do this atomic) */
885 * ------------------------------------------------------------
886 * mcfrs_ioctl() and friends
887 * ------------------------------------------------------------
890 static int get_serial_info(struct mcf_serial * info,
891 struct serial_struct * retinfo)
893 struct serial_struct tmp;
897 memset(&tmp, 0, sizeof(tmp));
898 tmp.type = info->type;
899 tmp.line = info->line;
900 tmp.port = (unsigned int) info->addr;
902 tmp.flags = info->flags;
903 tmp.baud_base = info->baud_base;
904 tmp.close_delay = info->close_delay;
905 tmp.closing_wait = info->closing_wait;
906 tmp.custom_divisor = info->custom_divisor;
907 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
910 static int set_serial_info(struct mcf_serial * info,
911 struct serial_struct * new_info)
913 struct serial_struct new_serial;
914 struct mcf_serial old_info;
919 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
923 if (!capable(CAP_SYS_ADMIN)) {
924 if ((new_serial.baud_base != info->baud_base) ||
925 (new_serial.type != info->type) ||
926 (new_serial.close_delay != info->close_delay) ||
927 ((new_serial.flags & ~ASYNC_USR_MASK) !=
928 (info->flags & ~ASYNC_USR_MASK)))
930 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
931 (new_serial.flags & ASYNC_USR_MASK));
932 info->custom_divisor = new_serial.custom_divisor;
940 * OK, past this point, all the error checking has been done.
941 * At this point, we start making changes.....
944 info->baud_base = new_serial.baud_base;
945 info->flags = ((info->flags & ~ASYNC_FLAGS) |
946 (new_serial.flags & ASYNC_FLAGS));
947 info->type = new_serial.type;
948 info->close_delay = new_serial.close_delay;
949 info->closing_wait = new_serial.closing_wait;
952 retval = startup(info);
957 * get_lsr_info - get line status register info
959 * Purpose: Let user call ioctl() to get info when the UART physically
960 * is emptied. On bus types like RS485, the transmitter must
961 * release the bus after transmitting. This must be done when
962 * the transmit shift register is empty, not be done when the
963 * transmit holding register is empty. This functionality
964 * allows an RS485 driver to be written in user space.
966 static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
968 volatile unsigned char *uartp;
970 unsigned char status;
972 local_irq_save(flags);
974 status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
975 local_irq_restore(flags);
977 return put_user(status,value);
981 * This routine sends a break character out the serial port.
983 static void send_break( struct mcf_serial * info, int duration)
985 volatile unsigned char *uartp;
990 set_current_state(TASK_INTERRUPTIBLE);
993 local_irq_save(flags);
994 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
995 schedule_timeout(duration);
996 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
997 local_irq_restore(flags);
1000 static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
1002 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1004 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1006 if (tty->flags & (1 << TTY_IO_ERROR))
1009 return mcfrs_getsignals(info);
1012 static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
1013 unsigned int set, unsigned int clear)
1015 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1016 int rts = -1, dtr = -1;
1018 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1020 if (tty->flags & (1 << TTY_IO_ERROR))
1023 if (set & TIOCM_RTS)
1025 if (set & TIOCM_DTR)
1027 if (clear & TIOCM_RTS)
1029 if (clear & TIOCM_DTR)
1032 mcfrs_setsignals(info, dtr, rts);
1037 static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
1038 unsigned int cmd, unsigned long arg)
1040 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1043 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1046 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1047 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1048 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1049 if (tty->flags & (1 << TTY_IO_ERROR))
1054 case TCSBRK: /* SVID version: non-zero arg --> no break */
1055 retval = tty_check_change(tty);
1058 tty_wait_until_sent(tty, 0);
1060 send_break(info, HZ/4); /* 1/4 second */
1062 case TCSBRKP: /* support for POSIX tcsendbreak() */
1063 retval = tty_check_change(tty);
1066 tty_wait_until_sent(tty, 0);
1067 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1070 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1071 (unsigned long *) arg);
1076 get_user(arg, (unsigned long *) arg);
1077 tty->termios->c_cflag =
1078 ((tty->termios->c_cflag & ~CLOCAL) |
1079 (arg ? CLOCAL : 0));
1082 if (access_ok(VERIFY_WRITE, (void *) arg,
1083 sizeof(struct serial_struct)))
1084 return get_serial_info(info,
1085 (struct serial_struct *) arg);
1088 return set_serial_info(info,
1089 (struct serial_struct *) arg);
1090 case TIOCSERGETLSR: /* Get line status register */
1091 if (access_ok(VERIFY_WRITE, (void *) arg,
1092 sizeof(unsigned int)))
1093 return get_lsr_info(info, (unsigned int *) arg);
1095 case TIOCSERGSTRUCT:
1096 error = copy_to_user((struct mcf_serial *) arg,
1097 info, sizeof(struct mcf_serial));
1105 get_user(val, (unsigned int *) arg);
1106 mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1111 val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1112 put_user(val, (unsigned int *) arg);
1118 return -ENOIOCTLCMD;
1123 static void mcfrs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1125 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1127 if (tty->termios->c_cflag == old_termios->c_cflag)
1130 mcfrs_change_speed(info);
1132 if ((old_termios->c_cflag & CRTSCTS) &&
1133 !(tty->termios->c_cflag & CRTSCTS)) {
1134 tty->hw_stopped = 0;
1135 mcfrs_setsignals(info, -1, 1);
1143 * ------------------------------------------------------------
1146 * This routine is called when the serial port gets closed. First, we
1147 * wait for the last remaining data to be sent. Then, we unlink its
1148 * S structure from the interrupt chain if necessary, and we free
1149 * that IRQ if nothing is left in the chain.
1150 * ------------------------------------------------------------
1152 static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1154 volatile unsigned char *uartp;
1155 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1156 unsigned long flags;
1158 if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1161 local_irq_save(flags);
1163 if (tty_hung_up_p(filp)) {
1164 local_irq_restore(flags);
1168 #ifdef SERIAL_DEBUG_OPEN
1169 printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1171 if ((tty->count == 1) && (info->count != 1)) {
1173 * Uh, oh. tty->count is 1, which means that the tty
1174 * structure will be freed. Info->count should always
1175 * be one in these conditions. If it's greater than
1176 * one, we've got real problems, since it means the
1177 * serial port won't be shutdown.
1179 printk("MCFRS: bad serial port count; tty->count is 1, "
1180 "info->count is %d\n", info->count);
1183 if (--info->count < 0) {
1184 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1185 info->line, info->count);
1189 local_irq_restore(flags);
1192 info->flags |= ASYNC_CLOSING;
1195 * Now we wait for the transmit buffer to clear; and we notify
1196 * the line discipline to only process XON/XOFF characters.
1199 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1200 tty_wait_until_sent(tty, info->closing_wait);
1203 * At this point we stop accepting input. To do this, we
1204 * disable the receive line status interrupts, and tell the
1205 * interrupt driver to stop checking the data ready bit in the
1206 * line status register.
1208 info->imr &= ~MCFUART_UIR_RXREADY;
1210 uartp[MCFUART_UIMR] = info->imr;
1213 /* FIXME: do we need to keep this enabled for console?? */
1214 if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
1215 /* Do not disable the UART */ ;
1219 if (tty->driver->flush_buffer)
1220 tty->driver->flush_buffer(tty);
1221 tty_ldisc_flush(tty);
1227 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1228 if (tty->ldisc.close)
1229 (tty->ldisc.close)(tty);
1230 tty->ldisc = ldiscs[N_TTY];
1231 tty->termios->c_line = N_TTY;
1232 if (tty->ldisc.open)
1233 (tty->ldisc.open)(tty);
1236 if (info->blocked_open) {
1237 if (info->close_delay) {
1238 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1240 wake_up_interruptible(&info->open_wait);
1242 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1243 wake_up_interruptible(&info->close_wait);
1244 local_irq_restore(flags);
1248 * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1251 mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1254 #define MCF5272_FIFO_SIZE 25 /* fifo size + shift reg */
1256 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1257 volatile unsigned char *uartp;
1258 unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1260 if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1263 orig_jiffies = jiffies;
1266 * Set the check interval to be 1/5 of the approximate time
1267 * to send the entire fifo, and make it at least 1. The check
1268 * interval should also be less than the timeout.
1270 * Note: we have to use pretty tight timings here to satisfy
1273 fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1274 char_time = fifo_time / 5;
1277 if (timeout && timeout < char_time)
1278 char_time = timeout;
1281 * Clamp the timeout period at 2 * the time to empty the
1282 * fifo. Just to be safe, set the minimum at .5 seconds.
1285 if (fifo_time < (HZ/2))
1287 if (!timeout || timeout > fifo_time)
1288 timeout = fifo_time;
1291 * Account for the number of bytes in the UART
1292 * transmitter FIFO plus any byte being shifted out.
1294 uartp = (volatile unsigned char *) info->addr;
1296 fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1297 if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1298 MCFUART_USR_TXEMPTY)) ==
1299 MCFUART_USR_TXREADY)
1303 msleep_interruptible(jiffies_to_msecs(char_time));
1304 if (signal_pending(current))
1306 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1311 * For the other coldfire models, assume all data has been sent
1317 * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1319 void mcfrs_hangup(struct tty_struct *tty)
1321 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1323 if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
1326 mcfrs_flush_buffer(tty);
1330 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1332 wake_up_interruptible(&info->open_wait);
1336 * ------------------------------------------------------------
1337 * mcfrs_open() and friends
1338 * ------------------------------------------------------------
1340 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1341 struct mcf_serial *info)
1343 DECLARE_WAITQUEUE(wait, current);
1348 * If the device is in the middle of being closed, then block
1349 * until it's done, and then try again.
1351 if (info->flags & ASYNC_CLOSING) {
1352 interruptible_sleep_on(&info->close_wait);
1353 #ifdef SERIAL_DO_RESTART
1354 if (info->flags & ASYNC_HUP_NOTIFY)
1357 return -ERESTARTSYS;
1364 * If non-blocking mode is set, or the port is not enabled,
1365 * then make the check up front and then exit.
1367 if ((filp->f_flags & O_NONBLOCK) ||
1368 (tty->flags & (1 << TTY_IO_ERROR))) {
1369 info->flags |= ASYNC_NORMAL_ACTIVE;
1373 if (tty->termios->c_cflag & CLOCAL)
1377 * Block waiting for the carrier detect and the line to become
1378 * free (i.e., not in use by the callout). While we are in
1379 * this loop, info->count is dropped by one, so that
1380 * mcfrs_close() knows when to free things. We restore it upon
1381 * exit, either normal or abnormal.
1384 add_wait_queue(&info->open_wait, &wait);
1385 #ifdef SERIAL_DEBUG_OPEN
1386 printk("block_til_ready before block: ttyS%d, count = %d\n",
1387 info->line, info->count);
1390 info->blocked_open++;
1392 local_irq_disable();
1393 mcfrs_setsignals(info, 1, 1);
1395 current->state = TASK_INTERRUPTIBLE;
1396 if (tty_hung_up_p(filp) ||
1397 !(info->flags & ASYNC_INITIALIZED)) {
1398 #ifdef SERIAL_DO_RESTART
1399 if (info->flags & ASYNC_HUP_NOTIFY)
1402 retval = -ERESTARTSYS;
1408 if (!(info->flags & ASYNC_CLOSING) &&
1409 (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1411 if (signal_pending(current)) {
1412 retval = -ERESTARTSYS;
1415 #ifdef SERIAL_DEBUG_OPEN
1416 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1417 info->line, info->count);
1421 current->state = TASK_RUNNING;
1422 remove_wait_queue(&info->open_wait, &wait);
1423 if (!tty_hung_up_p(filp))
1425 info->blocked_open--;
1426 #ifdef SERIAL_DEBUG_OPEN
1427 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1428 info->line, info->count);
1432 info->flags |= ASYNC_NORMAL_ACTIVE;
1437 * This routine is called whenever a serial port is opened. It
1438 * enables interrupts for a serial port, linking in its structure into
1439 * the IRQ chain. It also performs the serial-specific
1440 * initialization for the tty structure.
1442 int mcfrs_open(struct tty_struct *tty, struct file * filp)
1444 struct mcf_serial *info;
1448 if ((line < 0) || (line >= NR_PORTS))
1450 info = mcfrs_table + line;
1451 if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1453 #ifdef SERIAL_DEBUG_OPEN
1454 printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1457 tty->driver_data = info;
1461 * Start up serial port
1463 retval = startup(info);
1467 retval = block_til_ready(tty, filp, info);
1469 #ifdef SERIAL_DEBUG_OPEN
1470 printk("mcfrs_open returning after block_til_ready with %d\n",
1476 #ifdef SERIAL_DEBUG_OPEN
1477 printk("mcfrs_open %s successful...\n", tty->name);
1483 * Based on the line number set up the internal interrupt stuff.
1485 static void mcfrs_irqinit(struct mcf_serial *info)
1487 #if defined(CONFIG_M5272)
1488 volatile unsigned long *icrp;
1489 volatile unsigned long *portp;
1490 volatile unsigned char *uartp;
1493 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1495 switch (info->line) {
1503 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1508 /* Enable the output lines for the serial ports */
1509 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
1510 *portp = (*portp & ~0x000000ff) | 0x00000055;
1511 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
1512 *portp = (*portp & ~0x000003fc) | 0x000002a8;
1513 #elif defined(CONFIG_M527x) || defined(CONFIG_M528x)
1514 volatile unsigned char *icrp, *uartp;
1515 volatile unsigned long *imrp;
1519 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1520 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1521 *icrp = 0x33; /* UART0 with level 6, priority 3 */
1523 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1525 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1527 volatile unsigned char *icrp, *uartp;
1529 switch (info->line) {
1531 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1532 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1534 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1537 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1538 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1540 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1543 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1549 uartp[MCFUART_UIVR] = info->irq;
1552 /* Clear mask, so no surprise interrupts. */
1553 uartp[MCFUART_UIMR] = 0;
1555 if (request_irq(info->irq, mcfrs_interrupt, SA_INTERRUPT,
1556 "ColdFire UART", NULL)) {
1557 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1558 "vector=%d\n", info->line, info->irq);
1565 char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1569 * Serial stats reporting...
1571 int mcfrs_readproc(char *page, char **start, off_t off, int count,
1572 int *eof, void *data)
1574 struct mcf_serial *info;
1578 len = sprintf(page, mcfrs_drivername);
1579 for (i = 0; (i < NR_PORTS); i++) {
1580 info = &mcfrs_table[i];
1581 len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1582 i, (unsigned int) info->addr, info->irq, info->baud);
1583 if (info->stats.rx || info->stats.tx)
1584 len += sprintf((page + len), "tx:%d rx:%d ",
1585 info->stats.tx, info->stats.rx);
1586 if (info->stats.rxframing)
1587 len += sprintf((page + len), "fe:%d ",
1588 info->stats.rxframing);
1589 if (info->stats.rxparity)
1590 len += sprintf((page + len), "pe:%d ",
1591 info->stats.rxparity);
1592 if (info->stats.rxbreak)
1593 len += sprintf((page + len), "brk:%d ",
1594 info->stats.rxbreak);
1595 if (info->stats.rxoverrun)
1596 len += sprintf((page + len), "oe:%d ",
1597 info->stats.rxoverrun);
1599 str[0] = str[1] = 0;
1600 if ((sigs = mcfrs_getsignals(info))) {
1601 if (sigs & TIOCM_RTS)
1602 strcat(str, "|RTS");
1603 if (sigs & TIOCM_CTS)
1604 strcat(str, "|CTS");
1605 if (sigs & TIOCM_DTR)
1606 strcat(str, "|DTR");
1607 if (sigs & TIOCM_CD)
1611 len += sprintf((page + len), "%s\n", &str[1]);
1618 /* Finally, routines used to initialize the serial driver. */
1620 static void show_serial_version(void)
1622 printk(mcfrs_drivername);
1625 static struct tty_operations mcfrs_ops = {
1627 .close = mcfrs_close,
1628 .write = mcfrs_write,
1629 .flush_chars = mcfrs_flush_chars,
1630 .write_room = mcfrs_write_room,
1631 .chars_in_buffer = mcfrs_chars_in_buffer,
1632 .flush_buffer = mcfrs_flush_buffer,
1633 .ioctl = mcfrs_ioctl,
1634 .throttle = mcfrs_throttle,
1635 .unthrottle = mcfrs_unthrottle,
1636 .set_termios = mcfrs_set_termios,
1638 .start = mcfrs_start,
1639 .hangup = mcfrs_hangup,
1640 .read_proc = mcfrs_readproc,
1641 .wait_until_sent = mcfrs_wait_until_sent,
1642 .tiocmget = mcfrs_tiocmget,
1643 .tiocmset = mcfrs_tiocmset,
1646 /* mcfrs_init inits the driver */
1650 struct mcf_serial *info;
1651 unsigned long flags;
1654 /* Setup base handler, and timer table. */
1656 init_timer(&mcfrs_timer_struct);
1657 mcfrs_timer_struct.function = mcfrs_timer;
1658 mcfrs_timer_struct.data = 0;
1659 mcfrs_timer_struct.expires = jiffies + HZ/25;
1660 add_timer(&mcfrs_timer_struct);
1661 mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1663 mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1664 if (!mcfrs_serial_driver)
1667 show_serial_version();
1669 /* Initialize the tty_driver structure */
1670 mcfrs_serial_driver->owner = THIS_MODULE;
1671 mcfrs_serial_driver->name = "ttyS";
1672 mcfrs_serial_driver->devfs_name = "ttys/";
1673 mcfrs_serial_driver->driver_name = "serial";
1674 mcfrs_serial_driver->major = TTY_MAJOR;
1675 mcfrs_serial_driver->minor_start = 64;
1676 mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1677 mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1678 mcfrs_serial_driver->init_termios = tty_std_termios;
1680 mcfrs_serial_driver->init_termios.c_cflag =
1681 mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1682 mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1684 tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1686 if (tty_register_driver(mcfrs_serial_driver)) {
1687 printk("MCFRS: Couldn't register serial driver\n");
1688 put_tty_driver(mcfrs_serial_driver);
1692 local_irq_save(flags);
1695 * Configure all the attached serial ports.
1697 for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1698 info->magic = SERIAL_MAGIC;
1701 info->custom_divisor = 16;
1702 info->close_delay = 50;
1703 info->closing_wait = 3000;
1707 info->blocked_open = 0;
1708 INIT_WORK(&info->tqueue, mcfrs_offintr, info);
1709 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1710 init_waitqueue_head(&info->open_wait);
1711 init_waitqueue_head(&info->close_wait);
1714 mcfrs_setsignals(info, 0, 0);
1715 mcfrs_irqinit(info);
1717 printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1718 (unsigned int) info->addr, info->irq);
1719 printk(" is a builtin ColdFire UART\n");
1722 local_irq_restore(flags);
1726 module_init(mcfrs_init);
1728 /****************************************************************************/
1729 /* Serial Console */
1730 /****************************************************************************/
1733 * Quick and dirty UART initialization, for console output.
1736 void mcfrs_init_console(void)
1738 volatile unsigned char *uartp;
1742 * Reset UART, get it into known state...
1744 uartp = (volatile unsigned char *) (MCF_MBAR +
1745 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1747 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
1748 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
1749 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
1752 * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1754 uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1755 uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1757 clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1758 uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8; /* set msb baud */
1759 uartp[MCFUART_UBG2] = (clk & 0xff); /* set lsb baud */
1761 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1762 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1764 mcfrs_console_inited++;
1770 * Setup for console. Argument comes from the boot command line.
1773 int mcfrs_console_setup(struct console *cp, char *arg)
1775 int i, n = CONSOLE_BAUD_RATE;
1780 if (!strncmp(cp->name, "ttyS", 4))
1781 mcfrs_console_port = cp->index;
1782 else if (!strncmp(cp->name, "cua", 3))
1783 mcfrs_console_port = cp->index;
1788 n = simple_strtoul(arg,NULL,0);
1789 for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1790 if (mcfrs_baud_table[i] == n)
1792 if (i < MCFRS_BAUD_TABLE_SIZE) {
1793 mcfrs_console_baud = n;
1794 mcfrs_console_cbaud = 0;
1796 mcfrs_console_cbaud |= CBAUDEX;
1799 mcfrs_console_cbaud |= i;
1801 mcfrs_init_console(); /* make sure baud rate changes */
1806 static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1809 return mcfrs_serial_driver;
1814 * Output a single character, using UART polled mode.
1815 * This is used for console output.
1818 void mcfrs_put_char(char ch)
1820 volatile unsigned char *uartp;
1821 unsigned long flags;
1824 uartp = (volatile unsigned char *) (MCF_MBAR +
1825 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1827 local_irq_save(flags);
1828 for (i = 0; (i < 0x10000); i++) {
1829 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1833 uartp[MCFUART_UTB] = ch;
1834 for (i = 0; (i < 0x10000); i++)
1835 if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1839 mcfrs_init_console(); /* try and get it back */
1840 local_irq_restore(flags);
1847 * rs_console_write is registered for printk output.
1850 void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1852 if (!mcfrs_console_inited)
1853 mcfrs_init_console();
1856 mcfrs_put_char('\r');
1857 mcfrs_put_char(*p++);
1862 * declare our consoles
1865 struct console mcfrs_console = {
1867 .write = mcfrs_console_write,
1868 .device = mcfrs_console_device,
1869 .setup = mcfrs_console_setup,
1870 .flags = CON_PRINTBUFFER,
1874 static int __init mcfrs_console_init(void)
1876 register_console(&mcfrs_console);
1880 console_initcall(mcfrs_console_init);
1882 /****************************************************************************/