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/segment.h>
44 #include <asm/semaphore.h>
45 #include <asm/delay.h>
46 #include <asm/coldfire.h>
47 #include <asm/mcfsim.h>
48 #include <asm/mcfuart.h>
49 #include <asm/nettel.h>
50 #include <asm/uaccess.h>
51 #include "mcfserial.h"
53 struct timer_list mcfrs_timer_struct;
56 * Default console baud rate, we use this as the default
57 * for all ports so init can just open /dev/console and
58 * keep going. Perhaps one day the cflag settings for the
59 * console can be used instead.
61 #if defined(CONFIG_ARNEWSH) || defined(CONFIG_MOTOROLA) || defined(CONFIG_senTec)
62 #define CONSOLE_BAUD_RATE 19200
63 #define DEFAULT_CBAUD B19200
66 #ifndef CONSOLE_BAUD_RATE
67 #define CONSOLE_BAUD_RATE 9600
68 #define DEFAULT_CBAUD B9600
71 int mcfrs_console_inited = 0;
72 int mcfrs_console_port = -1;
73 int mcfrs_console_baud = CONSOLE_BAUD_RATE;
74 int mcfrs_console_cbaud = DEFAULT_CBAUD;
77 * Driver data structures.
79 static struct tty_driver *mcfrs_serial_driver;
81 /* number of characters left in xmit buffer before we ask for more */
82 #define WAKEUP_CHARS 256
86 #undef SERIAL_DEBUG_OPEN
87 #undef SERIAL_DEBUG_FLOW
96 * Configuration table, UARTs to look for at startup.
98 static struct mcf_serial mcfrs_table[] = {
101 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE1),
103 .flags = ASYNC_BOOT_AUTOCONF,
107 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE2),
109 .flags = ASYNC_BOOT_AUTOCONF,
114 #define NR_PORTS (sizeof(mcfrs_table) / sizeof(struct mcf_serial))
117 * This is used to figure out the divisor speeds and the timeouts.
119 static int mcfrs_baud_table[] = {
120 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
121 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0
123 #define MCFRS_BAUD_TABLE_SIZE \
124 (sizeof(mcfrs_baud_table)/sizeof(mcfrs_baud_table[0]))
127 #ifdef CONFIG_MAGIC_SYSRQ
129 * Magic system request keys. Used for debugging...
131 extern int magic_sysrq_key(int ch);
136 * tmp_buf is used as a temporary buffer by serial_write. We need to
137 * lock it in case the copy_from_user blocks while swapping in a page,
138 * and some other program tries to do a serial write at the same time.
139 * Since the lock will only come under contention when the system is
140 * swapping and available memory is low, it makes sense to share one
141 * buffer across all the serial ports, since it significantly saves
142 * memory if large numbers of serial ports are open.
144 static unsigned char mcfrs_tmp_buf[4096]; /* This is cheating */
145 static DECLARE_MUTEX(mcfrs_tmp_buf_sem);
148 * Forware declarations...
150 static void mcfrs_change_speed(struct mcf_serial *info);
151 static void mcfrs_wait_until_sent(struct tty_struct *tty, int timeout);
154 static inline int serial_paranoia_check(struct mcf_serial *info,
155 char *name, const char *routine)
157 #ifdef SERIAL_PARANOIA_CHECK
158 static const char badmagic[] =
159 "MCFRS(warning): bad magic number for serial struct %s in %s\n";
160 static const char badinfo[] =
161 "MCFRS(warning): null mcf_serial for %s in %s\n";
164 printk(badinfo, name, routine);
167 if (info->magic != SERIAL_MAGIC) {
168 printk(badmagic, name, routine);
176 * Sets or clears DTR and RTS on the requested line.
178 static void mcfrs_setsignals(struct mcf_serial *info, int dtr, int rts)
180 volatile unsigned char *uartp;
184 printk("%s(%d): mcfrs_setsignals(info=%x,dtr=%d,rts=%d)\n",
185 __FILE__, __LINE__, info, dtr, rts);
188 local_irq_save(flags);
192 mcf_setppdata(MCFPP_DTR1, (dtr ? 0 : MCFPP_DTR1));
194 mcf_setppdata(MCFPP_DTR0, (dtr ? 0 : MCFPP_DTR0));
200 info->sigs |= TIOCM_RTS;
201 uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
203 info->sigs &= ~TIOCM_RTS;
204 uartp[MCFUART_UOP0] = MCFUART_UOP_RTS;
207 local_irq_restore(flags);
212 * Gets values of serial signals.
214 static int mcfrs_getsignals(struct mcf_serial *info)
216 volatile unsigned char *uartp;
219 #if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
220 unsigned short ppdata;
224 printk("%s(%d): mcfrs_getsignals(info=%x)\n", __FILE__, __LINE__);
227 local_irq_save(flags);
229 sigs = (uartp[MCFUART_UIPR] & MCFUART_UIPR_CTS) ? 0 : TIOCM_CTS;
230 sigs |= (info->sigs & TIOCM_RTS);
235 ppdata = mcf_getppdata();
236 if (info->line == 0) {
237 sigs |= (ppdata & MCFPP_DCD0) ? 0 : TIOCM_CD;
238 sigs |= (ppdata & MCFPP_DTR0) ? 0 : TIOCM_DTR;
239 } else if (info->line == 1) {
240 sigs |= (ppdata & MCFPP_DCD1) ? 0 : TIOCM_CD;
241 sigs |= (ppdata & MCFPP_DTR1) ? 0 : TIOCM_DTR;
246 local_irq_restore(flags);
251 * ------------------------------------------------------------
252 * mcfrs_stop() and mcfrs_start()
254 * This routines are called before setting or resetting tty->stopped.
255 * They enable or disable transmitter interrupts, as necessary.
256 * ------------------------------------------------------------
258 static void mcfrs_stop(struct tty_struct *tty)
260 volatile unsigned char *uartp;
261 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
264 if (serial_paranoia_check(info, tty->name, "mcfrs_stop"))
267 local_irq_save(flags);
269 info->imr &= ~MCFUART_UIR_TXREADY;
270 uartp[MCFUART_UIMR] = info->imr;
271 local_irq_restore(flags);
274 static void mcfrs_start(struct tty_struct *tty)
276 volatile unsigned char *uartp;
277 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
280 if (serial_paranoia_check(info, tty->name, "mcfrs_start"))
283 local_irq_save(flags);
284 if (info->xmit_cnt && info->xmit_buf) {
286 info->imr |= MCFUART_UIR_TXREADY;
287 uartp[MCFUART_UIMR] = info->imr;
289 local_irq_restore(flags);
293 * ----------------------------------------------------------------------
295 * Here starts the interrupt handling routines. All of the following
296 * subroutines are declared as inline and are folded into
297 * mcfrs_interrupt(). They were separated out for readability's sake.
299 * Note: mcfrs_interrupt() is a "fast" interrupt, which means that it
300 * runs with interrupts turned off. People who may want to modify
301 * mcfrs_interrupt() should try to keep the interrupt handler as fast as
302 * possible. After you are done making modifications, it is not a bad
305 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
307 * and look at the resulting assemble code in serial.s.
309 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
310 * -----------------------------------------------------------------------
313 static inline void receive_chars(struct mcf_serial *info)
315 volatile unsigned char *uartp;
316 struct tty_struct *tty = info->tty;
317 unsigned char status, ch;
324 while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
326 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
329 ch = uartp[MCFUART_URB];
332 #ifdef CONFIG_MAGIC_SYSRQ
333 if (mcfrs_console_inited && (info->line == mcfrs_console_port)) {
334 if (magic_sysrq_key(ch))
340 if (status & MCFUART_USR_RXERR)
341 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
342 if (status & MCFUART_USR_RXBREAK) {
343 info->stats.rxbreak++;
344 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
345 } else if (status & MCFUART_USR_RXPARITY) {
346 info->stats.rxparity++;
347 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
348 } else if (status & MCFUART_USR_RXOVERRUN) {
349 info->stats.rxoverrun++;
350 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
351 } else if (status & MCFUART_USR_RXFRAMING) {
352 info->stats.rxframing++;
353 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
355 *tty->flip.flag_buf_ptr++ = 0;
357 *tty->flip.char_buf_ptr++ = ch;
360 schedule_work(&tty->flip.work);
364 static inline void transmit_chars(struct mcf_serial *info)
366 volatile unsigned char *uartp;
371 /* Send special char - probably flow control */
372 uartp[MCFUART_UTB] = info->x_char;
377 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
378 info->imr &= ~MCFUART_UIR_TXREADY;
379 uartp[MCFUART_UIMR] = info->imr;
383 while (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) {
384 uartp[MCFUART_UTB] = info->xmit_buf[info->xmit_tail++];
385 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
387 if (--info->xmit_cnt <= 0)
391 if (info->xmit_cnt < WAKEUP_CHARS)
392 schedule_work(&info->tqueue);
397 * This is the serial driver's generic interrupt routine
399 irqreturn_t mcfrs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
401 struct mcf_serial *info;
404 info = &mcfrs_table[(irq - IRQBASE)];
405 isr = info->addr[MCFUART_UISR] & info->imr;
407 if (isr & MCFUART_UIR_RXREADY)
409 if (isr & MCFUART_UIR_TXREADY)
410 transmit_chars(info);
415 * -------------------------------------------------------------------
416 * Here ends the serial interrupt routines.
417 * -------------------------------------------------------------------
420 static void mcfrs_offintr(void *private)
422 struct mcf_serial *info = (struct mcf_serial *) private;
423 struct tty_struct *tty;
433 * Change of state on a DCD line.
435 void mcfrs_modem_change(struct mcf_serial *info, int dcd)
437 if (info->count == 0)
440 if (info->flags & ASYNC_CHECK_CD) {
442 wake_up_interruptible(&info->open_wait);
444 schedule_work(&info->tqueue_hangup);
451 unsigned short mcfrs_ppstatus;
454 * This subroutine is called when the RS_TIMER goes off. It is used
455 * to monitor the state of the DCD lines - since they have no edge
456 * sensors and interrupt generators.
458 static void mcfrs_timer(void)
460 unsigned int ppstatus, dcdval, i;
462 ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
464 if (ppstatus != mcfrs_ppstatus) {
465 for (i = 0; (i < 2); i++) {
466 dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
467 if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
468 mcfrs_modem_change(&mcfrs_table[i],
469 ((ppstatus & dcdval) ? 0 : 1));
473 mcfrs_ppstatus = ppstatus;
476 mcfrs_timer_struct.expires = jiffies + HZ/25;
477 add_timer(&mcfrs_timer_struct);
480 #endif /* MCFPP_DCD0 */
484 * This routine is called from the scheduler tqueue when the interrupt
485 * routine has signalled that a hangup has occurred. The path of
486 * hangup processing is:
488 * serial interrupt routine -> (scheduler tqueue) ->
489 * do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
492 static void do_serial_hangup(void *private)
494 struct mcf_serial *info = (struct mcf_serial *) private;
495 struct tty_struct *tty;
504 static int startup(struct mcf_serial * info)
506 volatile unsigned char *uartp;
509 if (info->flags & ASYNC_INITIALIZED)
512 if (!info->xmit_buf) {
513 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
518 local_irq_save(flags);
520 #ifdef SERIAL_DEBUG_OPEN
521 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
525 * Reset UART, get it into known state...
528 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
529 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
530 mcfrs_setsignals(info, 1, 1);
533 clear_bit(TTY_IO_ERROR, &info->tty->flags);
534 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
537 * and set the speed of the serial port
539 mcfrs_change_speed(info);
542 * Lastly enable the UART transmitter and receiver, and
545 info->imr = MCFUART_UIR_RXREADY;
546 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
547 uartp[MCFUART_UIMR] = info->imr;
549 info->flags |= ASYNC_INITIALIZED;
550 local_irq_restore(flags);
555 * This routine will shutdown a serial port; interrupts are disabled, and
556 * DTR is dropped if the hangup on close termio flag is on.
558 static void shutdown(struct mcf_serial * info)
560 volatile unsigned char *uartp;
563 if (!(info->flags & ASYNC_INITIALIZED))
566 #ifdef SERIAL_DEBUG_OPEN
567 printk("Shutting down serial port %d (irq %d)....\n", info->line,
571 local_irq_save(flags);
574 uartp[MCFUART_UIMR] = 0; /* mask all interrupts */
575 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
576 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
578 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
579 mcfrs_setsignals(info, 0, 0);
581 if (info->xmit_buf) {
582 free_page((unsigned long) info->xmit_buf);
587 set_bit(TTY_IO_ERROR, &info->tty->flags);
589 info->flags &= ~ASYNC_INITIALIZED;
590 local_irq_restore(flags);
595 * This routine is called to set the UART divisor registers to match
596 * the specified baud rate for a serial port.
598 static void mcfrs_change_speed(struct mcf_serial *info)
600 volatile unsigned char *uartp;
601 unsigned int baudclk, cflag;
603 unsigned char mr1, mr2;
606 unsigned int fraction;
609 if (!info->tty || !info->tty->termios)
611 cflag = info->tty->termios->c_cflag;
616 printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
623 info->tty->termios->c_cflag &= ~CBAUDEX;
628 mcfrs_setsignals(info, 0, -1);
632 /* compute the baudrate clock */
635 * For the MCF5272, also compute the baudrate fraction.
637 baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
638 fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
640 fraction /= (32 * mcfrs_baud_table[i]);
642 baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
645 info->baud = mcfrs_baud_table[i];
647 mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
650 switch (cflag & CSIZE) {
651 case CS5: mr1 |= MCFUART_MR1_CS5; break;
652 case CS6: mr1 |= MCFUART_MR1_CS6; break;
653 case CS7: mr1 |= MCFUART_MR1_CS7; break;
655 default: mr1 |= MCFUART_MR1_CS8; break;
658 if (cflag & PARENB) {
659 if (cflag & CMSPAR) {
661 mr1 |= MCFUART_MR1_PARITYMARK;
663 mr1 |= MCFUART_MR1_PARITYSPACE;
666 mr1 |= MCFUART_MR1_PARITYODD;
668 mr1 |= MCFUART_MR1_PARITYEVEN;
671 mr1 |= MCFUART_MR1_PARITYNONE;
675 mr2 |= MCFUART_MR2_STOP2;
677 mr2 |= MCFUART_MR2_STOP1;
679 if (cflag & CRTSCTS) {
680 mr1 |= MCFUART_MR1_RXRTS;
681 mr2 |= MCFUART_MR2_TXCTS;
685 info->flags &= ~ASYNC_CHECK_CD;
687 info->flags |= ASYNC_CHECK_CD;
691 local_irq_save(flags);
693 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
697 Note: pg 12-16 of MCF5206e User's Manual states that a
698 software reset should be performed prior to changing
699 UMR1,2, UCSR, UACR, bit 7
701 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
702 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
703 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
704 uartp[MCFUART_UMR] = mr1;
705 uartp[MCFUART_UMR] = mr2;
706 uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8; /* set msb byte */
707 uartp[MCFUART_UBG2] = (baudclk & 0xff); /* set lsb byte */
709 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
711 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
712 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
713 mcfrs_setsignals(info, 1, -1);
714 local_irq_restore(flags);
718 static void mcfrs_flush_chars(struct tty_struct *tty)
720 volatile unsigned char *uartp;
721 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
724 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
727 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
731 /* Enable transmitter */
732 local_irq_save(flags);
734 info->imr |= MCFUART_UIR_TXREADY;
735 uartp[MCFUART_UIMR] = info->imr;
736 local_irq_restore(flags);
739 static int mcfrs_write(struct tty_struct * tty,
740 const unsigned char *buf, int count)
742 volatile unsigned char *uartp;
743 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
748 printk("%s(%d): mcfrs_write(tty=%x,buf=%x,count=%d)\n",
749 __FILE__, __LINE__, (int)tty, (int)buf, count);
752 if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
755 if (!tty || !info->xmit_buf)
758 local_save_flags(flags);
761 c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
762 ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
763 local_irq_restore(flags);
768 memcpy(info->xmit_buf + info->xmit_head, buf, c);
771 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
773 local_irq_restore(flags);
782 info->imr |= MCFUART_UIR_TXREADY;
783 uartp[MCFUART_UIMR] = info->imr;
784 local_irq_restore(flags);
789 static int mcfrs_write_room(struct tty_struct *tty)
791 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
794 if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
796 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
802 static int mcfrs_chars_in_buffer(struct tty_struct *tty)
804 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
806 if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
808 return info->xmit_cnt;
811 static void mcfrs_flush_buffer(struct tty_struct *tty)
813 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
816 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
819 local_irq_save(flags);
820 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
821 local_irq_restore(flags);
827 * ------------------------------------------------------------
830 * This routine is called by the upper-layer tty layer to signal that
831 * incoming characters should be throttled.
832 * ------------------------------------------------------------
834 static void mcfrs_throttle(struct tty_struct * tty)
836 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
837 #ifdef SERIAL_DEBUG_THROTTLE
840 printk("throttle %s: %d....\n", _tty_name(tty, buf),
841 tty->ldisc.chars_in_buffer(tty));
844 if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
848 info->x_char = STOP_CHAR(tty);
850 /* Turn off RTS line (do this atomic) */
853 static void mcfrs_unthrottle(struct tty_struct * tty)
855 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
856 #ifdef SERIAL_DEBUG_THROTTLE
859 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
860 tty->ldisc.chars_in_buffer(tty));
863 if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
870 info->x_char = START_CHAR(tty);
873 /* Assert RTS line (do this atomic) */
877 * ------------------------------------------------------------
878 * mcfrs_ioctl() and friends
879 * ------------------------------------------------------------
882 static int get_serial_info(struct mcf_serial * info,
883 struct serial_struct * retinfo)
885 struct serial_struct tmp;
889 memset(&tmp, 0, sizeof(tmp));
890 tmp.type = info->type;
891 tmp.line = info->line;
892 tmp.port = (unsigned int) info->addr;
894 tmp.flags = info->flags;
895 tmp.baud_base = info->baud_base;
896 tmp.close_delay = info->close_delay;
897 tmp.closing_wait = info->closing_wait;
898 tmp.custom_divisor = info->custom_divisor;
899 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
902 static int set_serial_info(struct mcf_serial * info,
903 struct serial_struct * new_info)
905 struct serial_struct new_serial;
906 struct mcf_serial old_info;
911 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
915 if (!capable(CAP_SYS_ADMIN)) {
916 if ((new_serial.baud_base != info->baud_base) ||
917 (new_serial.type != info->type) ||
918 (new_serial.close_delay != info->close_delay) ||
919 ((new_serial.flags & ~ASYNC_USR_MASK) !=
920 (info->flags & ~ASYNC_USR_MASK)))
922 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
923 (new_serial.flags & ASYNC_USR_MASK));
924 info->custom_divisor = new_serial.custom_divisor;
932 * OK, past this point, all the error checking has been done.
933 * At this point, we start making changes.....
936 info->baud_base = new_serial.baud_base;
937 info->flags = ((info->flags & ~ASYNC_FLAGS) |
938 (new_serial.flags & ASYNC_FLAGS));
939 info->type = new_serial.type;
940 info->close_delay = new_serial.close_delay;
941 info->closing_wait = new_serial.closing_wait;
944 retval = startup(info);
949 * get_lsr_info - get line status register info
951 * Purpose: Let user call ioctl() to get info when the UART physically
952 * is emptied. On bus types like RS485, the transmitter must
953 * release the bus after transmitting. This must be done when
954 * the transmit shift register is empty, not be done when the
955 * transmit holding register is empty. This functionality
956 * allows an RS485 driver to be written in user space.
958 static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
960 volatile unsigned char *uartp;
962 unsigned char status;
964 local_irq_save(flags);
966 status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
967 local_irq_restore(flags);
969 return put_user(status,value);
973 * This routine sends a break character out the serial port.
975 static void send_break( struct mcf_serial * info, int duration)
977 volatile unsigned char *uartp;
982 set_current_state(TASK_INTERRUPTIBLE);
985 local_irq_save(flags);
986 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
987 schedule_timeout(jiffies + duration);
988 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
989 local_irq_restore(flags);
992 static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
994 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
996 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
998 if (tty->flags & (1 << TTY_IO_ERROR))
1001 return mcfrs_getsignals(info);
1004 static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
1005 unsigned int set, unsigned int clear)
1007 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1008 int rts = -1, dtr = -1;
1010 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1012 if (tty->flags & (1 << TTY_IO_ERROR))
1015 if (set & TIOCM_RTS)
1017 if (set & TIOCM_DTR)
1019 if (clear & TIOCM_RTS)
1021 if (clear & TIOCM_DTR)
1024 mcfrs_setsignals(info, dtr, rts);
1029 static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
1030 unsigned int cmd, unsigned long arg)
1032 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1035 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1038 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1039 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1040 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1041 if (tty->flags & (1 << TTY_IO_ERROR))
1046 case TCSBRK: /* SVID version: non-zero arg --> no break */
1047 retval = tty_check_change(tty);
1050 tty_wait_until_sent(tty, 0);
1052 send_break(info, HZ/4); /* 1/4 second */
1054 case TCSBRKP: /* support for POSIX tcsendbreak() */
1055 retval = tty_check_change(tty);
1058 tty_wait_until_sent(tty, 0);
1059 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1062 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1063 (unsigned long *) arg);
1068 get_user(arg, (unsigned long *) arg);
1069 tty->termios->c_cflag =
1070 ((tty->termios->c_cflag & ~CLOCAL) |
1071 (arg ? CLOCAL : 0));
1074 error = verify_area(VERIFY_WRITE, (void *) arg,
1075 sizeof(struct serial_struct));
1078 return get_serial_info(info,
1079 (struct serial_struct *) arg);
1081 return set_serial_info(info,
1082 (struct serial_struct *) arg);
1083 case TIOCSERGETLSR: /* Get line status register */
1084 error = verify_area(VERIFY_WRITE, (void *) arg,
1085 sizeof(unsigned int));
1089 return get_lsr_info(info, (unsigned int *) arg);
1091 case TIOCSERGSTRUCT:
1092 error = copy_to_user((struct mcf_serial *) arg,
1093 info, sizeof(struct mcf_serial));
1101 get_user(val, (unsigned int *) arg);
1102 mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1107 val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1108 put_user(val, (unsigned int *) arg);
1114 return -ENOIOCTLCMD;
1119 static void mcfrs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1121 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1123 if (tty->termios->c_cflag == old_termios->c_cflag)
1126 mcfrs_change_speed(info);
1128 if ((old_termios->c_cflag & CRTSCTS) &&
1129 !(tty->termios->c_cflag & CRTSCTS)) {
1130 tty->hw_stopped = 0;
1131 mcfrs_setsignals(info, -1, 1);
1139 * ------------------------------------------------------------
1142 * This routine is called when the serial port gets closed. First, we
1143 * wait for the last remaining data to be sent. Then, we unlink its
1144 * S structure from the interrupt chain if necessary, and we free
1145 * that IRQ if nothing is left in the chain.
1146 * ------------------------------------------------------------
1148 static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1150 volatile unsigned char *uartp;
1151 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1152 unsigned long flags;
1154 if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1157 local_irq_save(flags);
1159 if (tty_hung_up_p(filp)) {
1160 local_irq_restore(flags);
1164 #ifdef SERIAL_DEBUG_OPEN
1165 printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1167 if ((tty->count == 1) && (info->count != 1)) {
1169 * Uh, oh. tty->count is 1, which means that the tty
1170 * structure will be freed. Info->count should always
1171 * be one in these conditions. If it's greater than
1172 * one, we've got real problems, since it means the
1173 * serial port won't be shutdown.
1175 printk("MCFRS: bad serial port count; tty->count is 1, "
1176 "info->count is %d\n", info->count);
1179 if (--info->count < 0) {
1180 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1181 info->line, info->count);
1185 local_irq_restore(flags);
1188 info->flags |= ASYNC_CLOSING;
1191 * Now we wait for the transmit buffer to clear; and we notify
1192 * the line discipline to only process XON/XOFF characters.
1195 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1196 tty_wait_until_sent(tty, info->closing_wait);
1199 * At this point we stop accepting input. To do this, we
1200 * disable the receive line status interrupts, and tell the
1201 * interrupt driver to stop checking the data ready bit in the
1202 * line status register.
1204 info->imr &= ~MCFUART_UIR_RXREADY;
1206 uartp[MCFUART_UIMR] = info->imr;
1209 /* FIXME: do we need to keep this enabled for console?? */
1210 if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
1211 /* Do not disable the UART */ ;
1215 if (tty->driver->flush_buffer)
1216 tty->driver->flush_buffer(tty);
1217 tty_ldisc_flush(tty);
1223 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1224 if (tty->ldisc.close)
1225 (tty->ldisc.close)(tty);
1226 tty->ldisc = ldiscs[N_TTY];
1227 tty->termios->c_line = N_TTY;
1228 if (tty->ldisc.open)
1229 (tty->ldisc.open)(tty);
1232 if (info->blocked_open) {
1233 if (info->close_delay) {
1234 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1236 wake_up_interruptible(&info->open_wait);
1238 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1239 wake_up_interruptible(&info->close_wait);
1240 local_irq_restore(flags);
1244 * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1247 mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1250 #define MCF5272_FIFO_SIZE 25 /* fifo size + shift reg */
1252 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1253 volatile unsigned char *uartp;
1254 unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1256 if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1259 orig_jiffies = jiffies;
1262 * Set the check interval to be 1/5 of the approximate time
1263 * to send the entire fifo, and make it at least 1. The check
1264 * interval should also be less than the timeout.
1266 * Note: we have to use pretty tight timings here to satisfy
1269 fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1270 char_time = fifo_time / 5;
1273 if (timeout && timeout < char_time)
1274 char_time = timeout;
1277 * Clamp the timeout period at 2 * the time to empty the
1278 * fifo. Just to be safe, set the minimum at .5 seconds.
1281 if (fifo_time < (HZ/2))
1283 if (!timeout || timeout > fifo_time)
1284 timeout = fifo_time;
1287 * Account for the number of bytes in the UART
1288 * transmitter FIFO plus any byte being shifted out.
1290 uartp = (volatile unsigned char *) info->addr;
1292 fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1293 if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1294 MCFUART_USR_TXEMPTY)) ==
1295 MCFUART_USR_TXREADY)
1299 msleep_interruptible(jiffies_to_msecs(char_time));
1300 if (signal_pending(current))
1302 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1307 * For the other coldfire models, assume all data has been sent
1313 * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1315 void mcfrs_hangup(struct tty_struct *tty)
1317 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1319 if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
1322 mcfrs_flush_buffer(tty);
1326 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1328 wake_up_interruptible(&info->open_wait);
1332 * ------------------------------------------------------------
1333 * mcfrs_open() and friends
1334 * ------------------------------------------------------------
1336 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1337 struct mcf_serial *info)
1339 DECLARE_WAITQUEUE(wait, current);
1344 * If the device is in the middle of being closed, then block
1345 * until it's done, and then try again.
1347 if (info->flags & ASYNC_CLOSING) {
1348 interruptible_sleep_on(&info->close_wait);
1349 #ifdef SERIAL_DO_RESTART
1350 if (info->flags & ASYNC_HUP_NOTIFY)
1353 return -ERESTARTSYS;
1360 * If non-blocking mode is set, or the port is not enabled,
1361 * then make the check up front and then exit.
1363 if ((filp->f_flags & O_NONBLOCK) ||
1364 (tty->flags & (1 << TTY_IO_ERROR))) {
1365 info->flags |= ASYNC_NORMAL_ACTIVE;
1369 if (tty->termios->c_cflag & CLOCAL)
1373 * Block waiting for the carrier detect and the line to become
1374 * free (i.e., not in use by the callout). While we are in
1375 * this loop, info->count is dropped by one, so that
1376 * mcfrs_close() knows when to free things. We restore it upon
1377 * exit, either normal or abnormal.
1380 add_wait_queue(&info->open_wait, &wait);
1381 #ifdef SERIAL_DEBUG_OPEN
1382 printk("block_til_ready before block: ttyS%d, count = %d\n",
1383 info->line, info->count);
1386 info->blocked_open++;
1388 local_irq_disable();
1389 mcfrs_setsignals(info, 1, 1);
1391 current->state = TASK_INTERRUPTIBLE;
1392 if (tty_hung_up_p(filp) ||
1393 !(info->flags & ASYNC_INITIALIZED)) {
1394 #ifdef SERIAL_DO_RESTART
1395 if (info->flags & ASYNC_HUP_NOTIFY)
1398 retval = -ERESTARTSYS;
1404 if (!(info->flags & ASYNC_CLOSING) &&
1405 (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1407 if (signal_pending(current)) {
1408 retval = -ERESTARTSYS;
1411 #ifdef SERIAL_DEBUG_OPEN
1412 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1413 info->line, info->count);
1417 current->state = TASK_RUNNING;
1418 remove_wait_queue(&info->open_wait, &wait);
1419 if (!tty_hung_up_p(filp))
1421 info->blocked_open--;
1422 #ifdef SERIAL_DEBUG_OPEN
1423 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1424 info->line, info->count);
1428 info->flags |= ASYNC_NORMAL_ACTIVE;
1433 * This routine is called whenever a serial port is opened. It
1434 * enables interrupts for a serial port, linking in its structure into
1435 * the IRQ chain. It also performs the serial-specific
1436 * initialization for the tty structure.
1438 int mcfrs_open(struct tty_struct *tty, struct file * filp)
1440 struct mcf_serial *info;
1444 if ((line < 0) || (line >= NR_PORTS))
1446 info = mcfrs_table + line;
1447 if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1449 #ifdef SERIAL_DEBUG_OPEN
1450 printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1453 tty->driver_data = info;
1457 * Start up serial port
1459 retval = startup(info);
1463 retval = block_til_ready(tty, filp, info);
1465 #ifdef SERIAL_DEBUG_OPEN
1466 printk("mcfrs_open returning after block_til_ready with %d\n",
1472 #ifdef SERIAL_DEBUG_OPEN
1473 printk("mcfrs_open %s successful...\n", tty->name);
1479 * Based on the line number set up the internal interrupt stuff.
1481 static void mcfrs_irqinit(struct mcf_serial *info)
1483 #if defined(CONFIG_M5272)
1484 volatile unsigned long *icrp;
1485 volatile unsigned long *portp;
1486 volatile unsigned char *uartp;
1489 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1491 switch (info->line) {
1499 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1504 /* Enable the output lines for the serial ports */
1505 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
1506 *portp = (*portp & ~0x000000ff) | 0x00000055;
1507 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
1508 *portp = (*portp & ~0x000003fc) | 0x000002a8;
1509 #elif defined(CONFIG_M5282)
1510 volatile unsigned char *icrp, *uartp;
1511 volatile unsigned long *imrp;
1515 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1516 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1517 *icrp = 0x33; /* UART0 with level 6, priority 3 */
1519 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1521 *imrp &= ~((1 << (info->irq - 64)) | 1);
1523 volatile unsigned char *icrp, *uartp;
1525 switch (info->line) {
1527 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1528 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1530 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1533 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1534 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1536 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1539 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1545 uartp[MCFUART_UIVR] = info->irq;
1548 /* Clear mask, so no surprise interrupts. */
1549 uartp[MCFUART_UIMR] = 0;
1551 if (request_irq(info->irq, mcfrs_interrupt, SA_INTERRUPT,
1552 "ColdFire UART", NULL)) {
1553 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1554 "vector=%d\n", info->line, info->irq);
1561 char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1565 * Serial stats reporting...
1567 int mcfrs_readproc(char *page, char **start, off_t off, int count,
1568 int *eof, void *data)
1570 struct mcf_serial *info;
1574 len = sprintf(page, mcfrs_drivername);
1575 for (i = 0; (i < NR_PORTS); i++) {
1576 info = &mcfrs_table[i];
1577 len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1578 i, (unsigned int) info->addr, info->irq, info->baud);
1579 if (info->stats.rx || info->stats.tx)
1580 len += sprintf((page + len), "tx:%d rx:%d ",
1581 info->stats.tx, info->stats.rx);
1582 if (info->stats.rxframing)
1583 len += sprintf((page + len), "fe:%d ",
1584 info->stats.rxframing);
1585 if (info->stats.rxparity)
1586 len += sprintf((page + len), "pe:%d ",
1587 info->stats.rxparity);
1588 if (info->stats.rxbreak)
1589 len += sprintf((page + len), "brk:%d ",
1590 info->stats.rxbreak);
1591 if (info->stats.rxoverrun)
1592 len += sprintf((page + len), "oe:%d ",
1593 info->stats.rxoverrun);
1595 str[0] = str[1] = 0;
1596 if ((sigs = mcfrs_getsignals(info))) {
1597 if (sigs & TIOCM_RTS)
1598 strcat(str, "|RTS");
1599 if (sigs & TIOCM_CTS)
1600 strcat(str, "|CTS");
1601 if (sigs & TIOCM_DTR)
1602 strcat(str, "|DTR");
1603 if (sigs & TIOCM_CD)
1607 len += sprintf((page + len), "%s\n", &str[1]);
1614 /* Finally, routines used to initialize the serial driver. */
1616 static void show_serial_version(void)
1618 printk(mcfrs_drivername);
1621 static struct tty_operations mcfrs_ops = {
1623 .close = mcfrs_close,
1624 .write = mcfrs_write,
1625 .flush_chars = mcfrs_flush_chars,
1626 .write_room = mcfrs_write_room,
1627 .chars_in_buffer = mcfrs_chars_in_buffer,
1628 .flush_buffer = mcfrs_flush_buffer,
1629 .ioctl = mcfrs_ioctl,
1630 .throttle = mcfrs_throttle,
1631 .unthrottle = mcfrs_unthrottle,
1632 .set_termios = mcfrs_set_termios,
1634 .start = mcfrs_start,
1635 .hangup = mcfrs_hangup,
1636 .read_proc = mcfrs_readproc,
1637 .wait_until_sent = mcfrs_wait_until_sent,
1638 .tiocmget = mcfrs_tiocmget,
1639 .tiocmset = mcfrs_tiocmset,
1642 /* mcfrs_init inits the driver */
1646 struct mcf_serial *info;
1647 unsigned long flags;
1650 /* Setup base handler, and timer table. */
1652 init_timer(&mcfrs_timer_struct);
1653 mcfrs_timer_struct.function = mcfrs_timer;
1654 mcfrs_timer_struct.data = 0;
1655 mcfrs_timer_struct.expires = jiffies + HZ/25;
1656 add_timer(&mcfrs_timer_struct);
1657 mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1659 mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1660 if (!mcfrs_serial_driver)
1663 show_serial_version();
1665 /* Initialize the tty_driver structure */
1666 mcfrs_serial_driver->owner = THIS_MODULE;
1667 mcfrs_serial_driver->name = "ttyS";
1668 mcfrs_serial_driver->devfs_name = "ttys/";
1669 mcfrs_serial_driver->driver_name = "serial";
1670 mcfrs_serial_driver->major = TTY_MAJOR;
1671 mcfrs_serial_driver->minor_start = 64;
1672 mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1673 mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1674 mcfrs_serial_driver->init_termios = tty_std_termios;
1676 mcfrs_serial_driver->init_termios.c_cflag =
1677 mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1678 mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1680 tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1682 if (tty_register_driver(mcfrs_serial_driver)) {
1683 printk("MCFRS: Couldn't register serial driver\n");
1684 put_tty_driver(mcfrs_serial_driver);
1688 local_irq_save(flags);
1691 * Configure all the attached serial ports.
1693 for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1694 info->magic = SERIAL_MAGIC;
1697 info->custom_divisor = 16;
1698 info->close_delay = 50;
1699 info->closing_wait = 3000;
1703 info->blocked_open = 0;
1704 INIT_WORK(&info->tqueue, mcfrs_offintr, info);
1705 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1706 init_waitqueue_head(&info->open_wait);
1707 init_waitqueue_head(&info->close_wait);
1710 mcfrs_setsignals(info, 0, 0);
1711 mcfrs_irqinit(info);
1713 printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1714 (unsigned int) info->addr, info->irq);
1715 printk(" is a builtin ColdFire UART\n");
1718 local_irq_restore(flags);
1722 module_init(mcfrs_init);
1724 /****************************************************************************/
1725 /* Serial Console */
1726 /****************************************************************************/
1729 * Quick and dirty UART initialization, for console output.
1732 void mcfrs_init_console(void)
1734 volatile unsigned char *uartp;
1738 * Reset UART, get it into known state...
1740 uartp = (volatile unsigned char *) (MCF_MBAR +
1741 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1743 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
1744 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
1745 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
1748 * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1750 uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1751 uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1753 clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1754 uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8; /* set msb baud */
1755 uartp[MCFUART_UBG2] = (clk & 0xff); /* set lsb baud */
1757 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1758 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1760 mcfrs_console_inited++;
1766 * Setup for console. Argument comes from the boot command line.
1769 int mcfrs_console_setup(struct console *cp, char *arg)
1771 int i, n = CONSOLE_BAUD_RATE;
1776 if (!strncmp(cp->name, "ttyS", 4))
1777 mcfrs_console_port = cp->index;
1778 else if (!strncmp(cp->name, "cua", 3))
1779 mcfrs_console_port = cp->index;
1784 n = simple_strtoul(arg,NULL,0);
1785 for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1786 if (mcfrs_baud_table[i] == n)
1788 if (i < MCFRS_BAUD_TABLE_SIZE) {
1789 mcfrs_console_baud = n;
1790 mcfrs_console_cbaud = 0;
1792 mcfrs_console_cbaud |= CBAUDEX;
1795 mcfrs_console_cbaud |= i;
1797 mcfrs_init_console(); /* make sure baud rate changes */
1802 static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1805 return mcfrs_serial_driver;
1810 * Output a single character, using UART polled mode.
1811 * This is used for console output.
1814 void mcfrs_put_char(char ch)
1816 volatile unsigned char *uartp;
1817 unsigned long flags;
1820 uartp = (volatile unsigned char *) (MCF_MBAR +
1821 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1823 local_irq_save(flags);
1824 for (i = 0; (i < 0x10000); i++) {
1825 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1829 uartp[MCFUART_UTB] = ch;
1830 for (i = 0; (i < 0x10000); i++)
1831 if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1835 mcfrs_init_console(); /* try and get it back */
1836 local_irq_restore(flags);
1843 * rs_console_write is registered for printk output.
1846 void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1848 if (!mcfrs_console_inited)
1849 mcfrs_init_console();
1852 mcfrs_put_char('\r');
1853 mcfrs_put_char(*p++);
1858 * declare our consoles
1861 struct console mcfrs_console = {
1863 .write = mcfrs_console_write,
1864 .device = mcfrs_console_device,
1865 .setup = mcfrs_console_setup,
1866 .flags = CON_PRINTBUFFER,
1870 static int __init mcfrs_console_init(void)
1872 register_console(&mcfrs_console);
1876 console_initcall(mcfrs_console_init);
1878 /****************************************************************************/