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/delay.h>
44 #include <asm/coldfire.h>
45 #include <asm/mcfsim.h>
46 #include <asm/mcfuart.h>
47 #include <asm/nettel.h>
48 #include <asm/uaccess.h>
49 #include "mcfserial.h"
51 struct timer_list mcfrs_timer_struct;
54 * Default console baud rate, we use this as the default
55 * for all ports so init can just open /dev/console and
56 * keep going. Perhaps one day the cflag settings for the
57 * console can be used instead.
59 #if defined(CONFIG_HW_FEITH)
60 #define CONSOLE_BAUD_RATE 38400
61 #define DEFAULT_CBAUD B38400
62 #elif defined(CONFIG_MOD5272) || defined(CONFIG_M5208EVB) || \
63 defined(CONFIG_M5329EVB) || defined(CONFIG_GILBARCO)
64 #define CONSOLE_BAUD_RATE 115200
65 #define DEFAULT_CBAUD B115200
66 #elif defined(CONFIG_ARNEWSH) || defined(CONFIG_FREESCALE) || \
67 defined(CONFIG_senTec) || defined(CONFIG_SNEHA) || defined(CONFIG_AVNET)
68 #define CONSOLE_BAUD_RATE 19200
69 #define DEFAULT_CBAUD B19200
72 #ifndef CONSOLE_BAUD_RATE
73 #define CONSOLE_BAUD_RATE 9600
74 #define DEFAULT_CBAUD B9600
77 int mcfrs_console_inited = 0;
78 int mcfrs_console_port = -1;
79 int mcfrs_console_baud = CONSOLE_BAUD_RATE;
80 int mcfrs_console_cbaud = DEFAULT_CBAUD;
83 * Driver data structures.
85 static struct tty_driver *mcfrs_serial_driver;
87 /* number of characters left in xmit buffer before we ask for more */
88 #define WAKEUP_CHARS 256
92 #undef SERIAL_DEBUG_OPEN
93 #undef SERIAL_DEBUG_FLOW
95 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
96 defined(CONFIG_M520x) || defined(CONFIG_M532x)
97 #define IRQBASE (MCFINT_VECBASE+MCFINT_UART0)
103 * Configuration table, UARTs to look for at startup.
105 static struct mcf_serial mcfrs_table[] = {
108 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE1),
110 .flags = ASYNC_BOOT_AUTOCONF,
115 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE2),
117 .flags = ASYNC_BOOT_AUTOCONF,
123 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE3),
125 .flags = ASYNC_BOOT_AUTOCONF,
131 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE4),
133 .flags = ASYNC_BOOT_AUTOCONF,
139 #define NR_PORTS (sizeof(mcfrs_table) / sizeof(struct mcf_serial))
142 * This is used to figure out the divisor speeds and the timeouts.
144 static int mcfrs_baud_table[] = {
145 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
146 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0
148 #define MCFRS_BAUD_TABLE_SIZE \
149 (sizeof(mcfrs_baud_table)/sizeof(mcfrs_baud_table[0]))
152 #ifdef CONFIG_MAGIC_SYSRQ
154 * Magic system request keys. Used for debugging...
156 extern int magic_sysrq_key(int ch);
161 * Forware declarations...
163 static void mcfrs_change_speed(struct mcf_serial *info);
164 static void mcfrs_wait_until_sent(struct tty_struct *tty, int timeout);
167 static inline int serial_paranoia_check(struct mcf_serial *info,
168 char *name, const char *routine)
170 #ifdef SERIAL_PARANOIA_CHECK
171 static const char badmagic[] =
172 "MCFRS(warning): bad magic number for serial struct %s in %s\n";
173 static const char badinfo[] =
174 "MCFRS(warning): null mcf_serial for %s in %s\n";
177 printk(badinfo, name, routine);
180 if (info->magic != SERIAL_MAGIC) {
181 printk(badmagic, name, routine);
189 * Sets or clears DTR and RTS on the requested line.
191 static void mcfrs_setsignals(struct mcf_serial *info, int dtr, int rts)
193 volatile unsigned char *uartp;
197 printk("%s(%d): mcfrs_setsignals(info=%x,dtr=%d,rts=%d)\n",
198 __FILE__, __LINE__, info, dtr, rts);
201 local_irq_save(flags);
205 mcf_setppdata(MCFPP_DTR1, (dtr ? 0 : MCFPP_DTR1));
207 mcf_setppdata(MCFPP_DTR0, (dtr ? 0 : MCFPP_DTR0));
213 info->sigs |= TIOCM_RTS;
214 uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
216 info->sigs &= ~TIOCM_RTS;
217 uartp[MCFUART_UOP0] = MCFUART_UOP_RTS;
220 local_irq_restore(flags);
225 * Gets values of serial signals.
227 static int mcfrs_getsignals(struct mcf_serial *info)
229 volatile unsigned char *uartp;
232 #if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
233 unsigned short ppdata;
237 printk("%s(%d): mcfrs_getsignals(info=%x)\n", __FILE__, __LINE__);
240 local_irq_save(flags);
242 sigs = (uartp[MCFUART_UIPR] & MCFUART_UIPR_CTS) ? 0 : TIOCM_CTS;
243 sigs |= (info->sigs & TIOCM_RTS);
248 ppdata = mcf_getppdata();
249 if (info->line == 0) {
250 sigs |= (ppdata & MCFPP_DCD0) ? 0 : TIOCM_CD;
251 sigs |= (ppdata & MCFPP_DTR0) ? 0 : TIOCM_DTR;
252 } else if (info->line == 1) {
253 sigs |= (ppdata & MCFPP_DCD1) ? 0 : TIOCM_CD;
254 sigs |= (ppdata & MCFPP_DTR1) ? 0 : TIOCM_DTR;
259 local_irq_restore(flags);
264 * ------------------------------------------------------------
265 * mcfrs_stop() and mcfrs_start()
267 * This routines are called before setting or resetting tty->stopped.
268 * They enable or disable transmitter interrupts, as necessary.
269 * ------------------------------------------------------------
271 static void mcfrs_stop(struct tty_struct *tty)
273 volatile unsigned char *uartp;
274 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
277 if (serial_paranoia_check(info, tty->name, "mcfrs_stop"))
280 local_irq_save(flags);
282 info->imr &= ~MCFUART_UIR_TXREADY;
283 uartp[MCFUART_UIMR] = info->imr;
284 local_irq_restore(flags);
287 static void mcfrs_start(struct tty_struct *tty)
289 volatile unsigned char *uartp;
290 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
293 if (serial_paranoia_check(info, tty->name, "mcfrs_start"))
296 local_irq_save(flags);
297 if (info->xmit_cnt && info->xmit_buf) {
299 info->imr |= MCFUART_UIR_TXREADY;
300 uartp[MCFUART_UIMR] = info->imr;
302 local_irq_restore(flags);
306 * ----------------------------------------------------------------------
308 * Here starts the interrupt handling routines. All of the following
309 * subroutines are declared as inline and are folded into
310 * mcfrs_interrupt(). They were separated out for readability's sake.
312 * Note: mcfrs_interrupt() is a "fast" interrupt, which means that it
313 * runs with interrupts turned off. People who may want to modify
314 * mcfrs_interrupt() should try to keep the interrupt handler as fast as
315 * possible. After you are done making modifications, it is not a bad
318 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
320 * and look at the resulting assemble code in serial.s.
322 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
323 * -----------------------------------------------------------------------
326 static inline void receive_chars(struct mcf_serial *info)
328 volatile unsigned char *uartp;
329 struct tty_struct *tty = info->tty;
330 unsigned char status, ch, flag;
337 while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
338 ch = uartp[MCFUART_URB];
341 #ifdef CONFIG_MAGIC_SYSRQ
342 if (mcfrs_console_inited && (info->line == mcfrs_console_port)) {
343 if (magic_sysrq_key(ch))
349 if (status & MCFUART_USR_RXERR) {
350 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
351 if (status & MCFUART_USR_RXBREAK) {
352 info->stats.rxbreak++;
354 } else if (status & MCFUART_USR_RXPARITY) {
355 info->stats.rxparity++;
357 } else if (status & MCFUART_USR_RXOVERRUN) {
358 info->stats.rxoverrun++;
360 } else if (status & MCFUART_USR_RXFRAMING) {
361 info->stats.rxframing++;
365 tty_insert_flip_char(tty, ch, flag);
367 tty_schedule_flip(tty);
371 static inline void transmit_chars(struct mcf_serial *info)
373 volatile unsigned char *uartp;
378 /* Send special char - probably flow control */
379 uartp[MCFUART_UTB] = info->x_char;
384 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
385 info->imr &= ~MCFUART_UIR_TXREADY;
386 uartp[MCFUART_UIMR] = info->imr;
390 while (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) {
391 uartp[MCFUART_UTB] = info->xmit_buf[info->xmit_tail++];
392 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
394 if (--info->xmit_cnt <= 0)
398 if (info->xmit_cnt < WAKEUP_CHARS)
399 schedule_work(&info->tqueue);
404 * This is the serial driver's generic interrupt routine
406 irqreturn_t mcfrs_interrupt(int irq, void *dev_id)
408 struct mcf_serial *info;
411 info = &mcfrs_table[(irq - IRQBASE)];
412 isr = info->addr[MCFUART_UISR] & info->imr;
414 if (isr & MCFUART_UIR_RXREADY)
416 if (isr & MCFUART_UIR_TXREADY)
417 transmit_chars(info);
422 * -------------------------------------------------------------------
423 * Here ends the serial interrupt routines.
424 * -------------------------------------------------------------------
427 static void mcfrs_offintr(struct work_struct *work)
429 struct mcf_serial *info = container_of(work, struct mcf_serial, tqueue);
430 struct tty_struct *tty = info->tty;
438 * Change of state on a DCD line.
440 void mcfrs_modem_change(struct mcf_serial *info, int dcd)
442 if (info->count == 0)
445 if (info->flags & ASYNC_CHECK_CD) {
447 wake_up_interruptible(&info->open_wait);
449 schedule_work(&info->tqueue_hangup);
456 unsigned short mcfrs_ppstatus;
459 * This subroutine is called when the RS_TIMER goes off. It is used
460 * to monitor the state of the DCD lines - since they have no edge
461 * sensors and interrupt generators.
463 static void mcfrs_timer(void)
465 unsigned int ppstatus, dcdval, i;
467 ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
469 if (ppstatus != mcfrs_ppstatus) {
470 for (i = 0; (i < 2); i++) {
471 dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
472 if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
473 mcfrs_modem_change(&mcfrs_table[i],
474 ((ppstatus & dcdval) ? 0 : 1));
478 mcfrs_ppstatus = ppstatus;
481 mcfrs_timer_struct.expires = jiffies + HZ/25;
482 add_timer(&mcfrs_timer_struct);
485 #endif /* MCFPP_DCD0 */
489 * This routine is called from the scheduler tqueue when the interrupt
490 * routine has signalled that a hangup has occurred. The path of
491 * hangup processing is:
493 * serial interrupt routine -> (scheduler tqueue) ->
494 * do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
497 static void do_serial_hangup(struct work_struct *work)
499 struct mcf_serial *info = container_of(work, struct mcf_serial, tqueue_hangup);
500 struct tty_struct *tty = info->tty;
506 static int startup(struct mcf_serial * info)
508 volatile unsigned char *uartp;
511 if (info->flags & ASYNC_INITIALIZED)
514 if (!info->xmit_buf) {
515 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
520 local_irq_save(flags);
522 #ifdef SERIAL_DEBUG_OPEN
523 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
527 * Reset UART, get it into known state...
530 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
531 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
532 mcfrs_setsignals(info, 1, 1);
535 clear_bit(TTY_IO_ERROR, &info->tty->flags);
536 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
539 * and set the speed of the serial port
541 mcfrs_change_speed(info);
544 * Lastly enable the UART transmitter and receiver, and
547 info->imr = MCFUART_UIR_RXREADY;
548 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
549 uartp[MCFUART_UIMR] = info->imr;
551 info->flags |= ASYNC_INITIALIZED;
552 local_irq_restore(flags);
557 * This routine will shutdown a serial port; interrupts are disabled, and
558 * DTR is dropped if the hangup on close termio flag is on.
560 static void shutdown(struct mcf_serial * info)
562 volatile unsigned char *uartp;
565 if (!(info->flags & ASYNC_INITIALIZED))
568 #ifdef SERIAL_DEBUG_OPEN
569 printk("Shutting down serial port %d (irq %d)....\n", info->line,
573 local_irq_save(flags);
576 uartp[MCFUART_UIMR] = 0; /* mask all interrupts */
577 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
578 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
580 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
581 mcfrs_setsignals(info, 0, 0);
583 if (info->xmit_buf) {
584 free_page((unsigned long) info->xmit_buf);
589 set_bit(TTY_IO_ERROR, &info->tty->flags);
591 info->flags &= ~ASYNC_INITIALIZED;
592 local_irq_restore(flags);
597 * This routine is called to set the UART divisor registers to match
598 * the specified baud rate for a serial port.
600 static void mcfrs_change_speed(struct mcf_serial *info)
602 volatile unsigned char *uartp;
603 unsigned int baudclk, cflag;
605 unsigned char mr1, mr2;
608 unsigned int fraction;
611 if (!info->tty || !info->tty->termios)
613 cflag = info->tty->termios->c_cflag;
618 printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
625 info->tty->termios->c_cflag &= ~CBAUDEX;
630 mcfrs_setsignals(info, 0, -1);
634 /* compute the baudrate clock */
637 * For the MCF5272, also compute the baudrate fraction.
639 baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
640 fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
642 fraction /= (32 * mcfrs_baud_table[i]);
644 baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
647 info->baud = mcfrs_baud_table[i];
649 mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
652 switch (cflag & CSIZE) {
653 case CS5: mr1 |= MCFUART_MR1_CS5; break;
654 case CS6: mr1 |= MCFUART_MR1_CS6; break;
655 case CS7: mr1 |= MCFUART_MR1_CS7; break;
657 default: mr1 |= MCFUART_MR1_CS8; break;
660 if (cflag & PARENB) {
661 if (cflag & CMSPAR) {
663 mr1 |= MCFUART_MR1_PARITYMARK;
665 mr1 |= MCFUART_MR1_PARITYSPACE;
668 mr1 |= MCFUART_MR1_PARITYODD;
670 mr1 |= MCFUART_MR1_PARITYEVEN;
673 mr1 |= MCFUART_MR1_PARITYNONE;
677 mr2 |= MCFUART_MR2_STOP2;
679 mr2 |= MCFUART_MR2_STOP1;
681 if (cflag & CRTSCTS) {
682 mr1 |= MCFUART_MR1_RXRTS;
683 mr2 |= MCFUART_MR2_TXCTS;
687 info->flags &= ~ASYNC_CHECK_CD;
689 info->flags |= ASYNC_CHECK_CD;
693 local_irq_save(flags);
695 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
699 Note: pg 12-16 of MCF5206e User's Manual states that a
700 software reset should be performed prior to changing
701 UMR1,2, UCSR, UACR, bit 7
703 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
704 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
705 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
706 uartp[MCFUART_UMR] = mr1;
707 uartp[MCFUART_UMR] = mr2;
708 uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8; /* set msb byte */
709 uartp[MCFUART_UBG2] = (baudclk & 0xff); /* set lsb byte */
711 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
713 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
714 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
715 mcfrs_setsignals(info, 1, -1);
716 local_irq_restore(flags);
720 static void mcfrs_flush_chars(struct tty_struct *tty)
722 volatile unsigned char *uartp;
723 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
726 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
729 uartp = (volatile unsigned char *) info->addr;
732 * re-enable receiver interrupt
734 local_irq_save(flags);
735 if ((!(info->imr & MCFUART_UIR_RXREADY)) &&
736 (info->flags & ASYNC_INITIALIZED) ) {
737 info->imr |= MCFUART_UIR_RXREADY;
738 uartp[MCFUART_UIMR] = info->imr;
740 local_irq_restore(flags);
742 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
746 /* Enable transmitter */
747 local_irq_save(flags);
748 info->imr |= MCFUART_UIR_TXREADY;
749 uartp[MCFUART_UIMR] = info->imr;
750 local_irq_restore(flags);
753 static int mcfrs_write(struct tty_struct * tty,
754 const unsigned char *buf, int count)
756 volatile unsigned char *uartp;
757 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
762 printk("%s(%d): mcfrs_write(tty=%x,buf=%x,count=%d)\n",
763 __FILE__, __LINE__, (int)tty, (int)buf, count);
766 if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
769 if (!tty || !info->xmit_buf)
772 local_save_flags(flags);
775 c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
776 ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
777 local_irq_restore(flags);
782 memcpy(info->xmit_buf + info->xmit_head, buf, c);
785 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
787 local_irq_restore(flags);
796 info->imr |= MCFUART_UIR_TXREADY;
797 uartp[MCFUART_UIMR] = info->imr;
798 local_irq_restore(flags);
803 static int mcfrs_write_room(struct tty_struct *tty)
805 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
808 if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
810 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
816 static int mcfrs_chars_in_buffer(struct tty_struct *tty)
818 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
820 if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
822 return info->xmit_cnt;
825 static void mcfrs_flush_buffer(struct tty_struct *tty)
827 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
830 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
833 local_irq_save(flags);
834 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
835 local_irq_restore(flags);
841 * ------------------------------------------------------------
844 * This routine is called by the upper-layer tty layer to signal that
845 * incoming characters should be throttled.
846 * ------------------------------------------------------------
848 static void mcfrs_throttle(struct tty_struct * tty)
850 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
851 #ifdef SERIAL_DEBUG_THROTTLE
854 printk("throttle %s: %d....\n", tty_name(tty, buf),
855 tty->ldisc.chars_in_buffer(tty));
858 if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
862 info->x_char = STOP_CHAR(tty);
864 /* Turn off RTS line (do this atomic) */
867 static void mcfrs_unthrottle(struct tty_struct * tty)
869 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
870 #ifdef SERIAL_DEBUG_THROTTLE
873 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
874 tty->ldisc.chars_in_buffer(tty));
877 if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
884 info->x_char = START_CHAR(tty);
887 /* Assert RTS line (do this atomic) */
891 * ------------------------------------------------------------
892 * mcfrs_ioctl() and friends
893 * ------------------------------------------------------------
896 static int get_serial_info(struct mcf_serial * info,
897 struct serial_struct * retinfo)
899 struct serial_struct tmp;
903 memset(&tmp, 0, sizeof(tmp));
904 tmp.type = info->type;
905 tmp.line = info->line;
906 tmp.port = (unsigned int) info->addr;
908 tmp.flags = info->flags;
909 tmp.baud_base = info->baud_base;
910 tmp.close_delay = info->close_delay;
911 tmp.closing_wait = info->closing_wait;
912 tmp.custom_divisor = info->custom_divisor;
913 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
916 static int set_serial_info(struct mcf_serial * info,
917 struct serial_struct * new_info)
919 struct serial_struct new_serial;
920 struct mcf_serial old_info;
925 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
929 if (!capable(CAP_SYS_ADMIN)) {
930 if ((new_serial.baud_base != info->baud_base) ||
931 (new_serial.type != info->type) ||
932 (new_serial.close_delay != info->close_delay) ||
933 ((new_serial.flags & ~ASYNC_USR_MASK) !=
934 (info->flags & ~ASYNC_USR_MASK)))
936 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
937 (new_serial.flags & ASYNC_USR_MASK));
938 info->custom_divisor = new_serial.custom_divisor;
946 * OK, past this point, all the error checking has been done.
947 * At this point, we start making changes.....
950 info->baud_base = new_serial.baud_base;
951 info->flags = ((info->flags & ~ASYNC_FLAGS) |
952 (new_serial.flags & ASYNC_FLAGS));
953 info->type = new_serial.type;
954 info->close_delay = new_serial.close_delay;
955 info->closing_wait = new_serial.closing_wait;
958 retval = startup(info);
963 * get_lsr_info - get line status register info
965 * Purpose: Let user call ioctl() to get info when the UART physically
966 * is emptied. On bus types like RS485, the transmitter must
967 * release the bus after transmitting. This must be done when
968 * the transmit shift register is empty, not be done when the
969 * transmit holding register is empty. This functionality
970 * allows an RS485 driver to be written in user space.
972 static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
974 volatile unsigned char *uartp;
976 unsigned char status;
978 local_irq_save(flags);
980 status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
981 local_irq_restore(flags);
983 return put_user(status,value);
987 * This routine sends a break character out the serial port.
989 static void send_break( struct mcf_serial * info, int duration)
991 volatile unsigned char *uartp;
996 set_current_state(TASK_INTERRUPTIBLE);
999 local_irq_save(flags);
1000 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
1001 schedule_timeout(duration);
1002 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
1003 local_irq_restore(flags);
1006 static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
1008 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1010 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1012 if (tty->flags & (1 << TTY_IO_ERROR))
1015 return mcfrs_getsignals(info);
1018 static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
1019 unsigned int set, unsigned int clear)
1021 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1022 int rts = -1, dtr = -1;
1024 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1026 if (tty->flags & (1 << TTY_IO_ERROR))
1029 if (set & TIOCM_RTS)
1031 if (set & TIOCM_DTR)
1033 if (clear & TIOCM_RTS)
1035 if (clear & TIOCM_DTR)
1038 mcfrs_setsignals(info, dtr, rts);
1043 static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
1044 unsigned int cmd, unsigned long arg)
1046 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1049 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1052 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1053 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1054 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1055 if (tty->flags & (1 << TTY_IO_ERROR))
1060 case TCSBRK: /* SVID version: non-zero arg --> no break */
1061 retval = tty_check_change(tty);
1064 tty_wait_until_sent(tty, 0);
1066 send_break(info, HZ/4); /* 1/4 second */
1068 case TCSBRKP: /* support for POSIX tcsendbreak() */
1069 retval = tty_check_change(tty);
1072 tty_wait_until_sent(tty, 0);
1073 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1076 if (access_ok(VERIFY_WRITE, (void *) arg,
1077 sizeof(struct serial_struct)))
1078 return get_serial_info(info,
1079 (struct serial_struct *) arg);
1082 return set_serial_info(info,
1083 (struct serial_struct *) arg);
1084 case TIOCSERGETLSR: /* Get line status register */
1085 if (access_ok(VERIFY_WRITE, (void *) arg,
1086 sizeof(unsigned int)))
1087 return get_lsr_info(info, (unsigned int *) arg);
1089 case TIOCSERGSTRUCT:
1090 error = copy_to_user((struct mcf_serial *) arg,
1091 info, sizeof(struct mcf_serial));
1099 get_user(val, (unsigned int *) arg);
1100 mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1105 val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1106 put_user(val, (unsigned int *) arg);
1112 return -ENOIOCTLCMD;
1117 static void mcfrs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1119 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1121 if (tty->termios->c_cflag == old_termios->c_cflag)
1124 mcfrs_change_speed(info);
1126 if ((old_termios->c_cflag & CRTSCTS) &&
1127 !(tty->termios->c_cflag & CRTSCTS)) {
1128 tty->hw_stopped = 0;
1129 mcfrs_setsignals(info, -1, 1);
1137 * ------------------------------------------------------------
1140 * This routine is called when the serial port gets closed. First, we
1141 * wait for the last remaining data to be sent. Then, we unlink its
1142 * S structure from the interrupt chain if necessary, and we free
1143 * that IRQ if nothing is left in the chain.
1144 * ------------------------------------------------------------
1146 static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1148 volatile unsigned char *uartp;
1149 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1150 unsigned long flags;
1152 if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1155 local_irq_save(flags);
1157 if (tty_hung_up_p(filp)) {
1158 local_irq_restore(flags);
1162 #ifdef SERIAL_DEBUG_OPEN
1163 printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1165 if ((tty->count == 1) && (info->count != 1)) {
1167 * Uh, oh. tty->count is 1, which means that the tty
1168 * structure will be freed. Info->count should always
1169 * be one in these conditions. If it's greater than
1170 * one, we've got real problems, since it means the
1171 * serial port won't be shutdown.
1173 printk("MCFRS: bad serial port count; tty->count is 1, "
1174 "info->count is %d\n", info->count);
1177 if (--info->count < 0) {
1178 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1179 info->line, info->count);
1183 local_irq_restore(flags);
1186 info->flags |= ASYNC_CLOSING;
1189 * Now we wait for the transmit buffer to clear; and we notify
1190 * the line discipline to only process XON/XOFF characters.
1193 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1194 tty_wait_until_sent(tty, info->closing_wait);
1197 * At this point we stop accepting input. To do this, we
1198 * disable the receive line status interrupts, and tell the
1199 * interrupt driver to stop checking the data ready bit in the
1200 * line status register.
1202 info->imr &= ~MCFUART_UIR_RXREADY;
1204 uartp[MCFUART_UIMR] = info->imr;
1207 /* FIXME: do we need to keep this enabled for console?? */
1208 if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
1209 /* Do not disable the UART */ ;
1213 mcfrs_flush_buffer(tty);
1214 tty_ldisc_flush(tty);
1220 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1221 if (tty->ldisc.close)
1222 (tty->ldisc.close)(tty);
1223 tty->ldisc = ldiscs[N_TTY];
1224 tty->termios->c_line = N_TTY;
1225 if (tty->ldisc.open)
1226 (tty->ldisc.open)(tty);
1229 if (info->blocked_open) {
1230 if (info->close_delay) {
1231 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1233 wake_up_interruptible(&info->open_wait);
1235 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1236 wake_up_interruptible(&info->close_wait);
1237 local_irq_restore(flags);
1241 * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1244 mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1247 #define MCF5272_FIFO_SIZE 25 /* fifo size + shift reg */
1249 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1250 volatile unsigned char *uartp;
1251 unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1253 if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1256 orig_jiffies = jiffies;
1259 * Set the check interval to be 1/5 of the approximate time
1260 * to send the entire fifo, and make it at least 1. The check
1261 * interval should also be less than the timeout.
1263 * Note: we have to use pretty tight timings here to satisfy
1268 fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1269 char_time = fifo_time / 5;
1272 if (timeout && timeout < char_time)
1273 char_time = timeout;
1276 * Clamp the timeout period at 2 * the time to empty the
1277 * fifo. Just to be safe, set the minimum at .5 seconds.
1280 if (fifo_time < (HZ/2))
1282 if (!timeout || timeout > fifo_time)
1283 timeout = fifo_time;
1286 * Account for the number of bytes in the UART
1287 * transmitter FIFO plus any byte being shifted out.
1289 uartp = (volatile unsigned char *) info->addr;
1291 fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1292 if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1293 MCFUART_USR_TXEMPTY)) ==
1294 MCFUART_USR_TXREADY)
1298 msleep_interruptible(jiffies_to_msecs(char_time));
1299 if (signal_pending(current))
1301 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_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x)
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 = 0x30 + info->line; /* level 6, line based priority */
1519 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1521 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1522 #if defined(CONFIG_M527x)
1525 * External Pin Mask Setting & Enable External Pin for Interface
1526 * mrcbis@aliceposta.it
1528 u16 *serpin_enable_mask;
1529 serpin_enable_mask = (u16 *) (MCF_IPSBAR + MCF_GPIO_PAR_UART);
1530 if (info->line == 0)
1531 *serpin_enable_mask |= UART0_ENABLE_MASK;
1532 else if (info->line == 1)
1533 *serpin_enable_mask |= UART1_ENABLE_MASK;
1534 else if (info->line == 2)
1535 *serpin_enable_mask |= UART2_ENABLE_MASK;
1538 #if defined(CONFIG_M528x)
1539 /* make sure PUAPAR is set for UART0 and UART1 */
1540 if (info->line < 2) {
1541 volatile unsigned char *portp = (volatile unsigned char *) (MCF_MBAR + MCF5282_GPIO_PUAPAR);
1542 *portp |= (0x03 << (info->line * 2));
1545 #elif defined(CONFIG_M520x)
1546 volatile unsigned char *icrp, *uartp;
1547 volatile unsigned long *imrp;
1551 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1552 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1555 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1557 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1558 if (info->line < 2) {
1559 unsigned short *uart_par;
1560 uart_par = (unsigned short *)(MCF_IPSBAR + MCF_GPIO_PAR_UART);
1561 if (info->line == 0)
1562 *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD0
1563 | MCF_GPIO_PAR_UART_PAR_URXD0;
1564 else if (info->line == 1)
1565 *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD1
1566 | MCF_GPIO_PAR_UART_PAR_URXD1;
1567 } else if (info->line == 2) {
1568 unsigned char *feci2c_par;
1569 feci2c_par = (unsigned char *)(MCF_IPSBAR + MCF_GPIO_PAR_FECI2C);
1570 *feci2c_par &= ~0x0F;
1571 *feci2c_par |= MCF_GPIO_PAR_FECI2C_PAR_SCL_UTXD2
1572 | MCF_GPIO_PAR_FECI2C_PAR_SDA_URXD2;
1574 #elif defined(CONFIG_M532x)
1575 volatile unsigned char *uartp;
1577 switch (info->line) {
1579 MCF_INTC0_ICR26 = 0x3;
1580 MCF_INTC0_CIMR = 26;
1581 /* GPIO initialization */
1582 MCF_GPIO_PAR_UART |= 0x000F;
1585 MCF_INTC0_ICR27 = 0x3;
1586 MCF_INTC0_CIMR = 27;
1587 /* GPIO initialization */
1588 MCF_GPIO_PAR_UART |= 0x0FF0;
1591 MCF_INTC0_ICR28 = 0x3;
1592 MCF_INTC0_CIMR = 28;
1593 /* GPIOs also must be initalized, depends on board */
1597 volatile unsigned char *icrp, *uartp;
1599 switch (info->line) {
1601 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1602 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1604 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1607 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1608 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1610 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1613 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1619 uartp[MCFUART_UIVR] = info->irq;
1622 /* Clear mask, so no surprise interrupts. */
1623 uartp[MCFUART_UIMR] = 0;
1625 if (request_irq(info->irq, mcfrs_interrupt, IRQF_DISABLED,
1626 "ColdFire UART", NULL)) {
1627 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1628 "vector=%d\n", info->line, info->irq);
1635 char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1639 * Serial stats reporting...
1641 int mcfrs_readproc(char *page, char **start, off_t off, int count,
1642 int *eof, void *data)
1644 struct mcf_serial *info;
1648 len = sprintf(page, mcfrs_drivername);
1649 for (i = 0; (i < NR_PORTS); i++) {
1650 info = &mcfrs_table[i];
1651 len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1652 i, (unsigned int) info->addr, info->irq, info->baud);
1653 if (info->stats.rx || info->stats.tx)
1654 len += sprintf((page + len), "tx:%d rx:%d ",
1655 info->stats.tx, info->stats.rx);
1656 if (info->stats.rxframing)
1657 len += sprintf((page + len), "fe:%d ",
1658 info->stats.rxframing);
1659 if (info->stats.rxparity)
1660 len += sprintf((page + len), "pe:%d ",
1661 info->stats.rxparity);
1662 if (info->stats.rxbreak)
1663 len += sprintf((page + len), "brk:%d ",
1664 info->stats.rxbreak);
1665 if (info->stats.rxoverrun)
1666 len += sprintf((page + len), "oe:%d ",
1667 info->stats.rxoverrun);
1669 str[0] = str[1] = 0;
1670 if ((sigs = mcfrs_getsignals(info))) {
1671 if (sigs & TIOCM_RTS)
1672 strcat(str, "|RTS");
1673 if (sigs & TIOCM_CTS)
1674 strcat(str, "|CTS");
1675 if (sigs & TIOCM_DTR)
1676 strcat(str, "|DTR");
1677 if (sigs & TIOCM_CD)
1681 len += sprintf((page + len), "%s\n", &str[1]);
1688 /* Finally, routines used to initialize the serial driver. */
1690 static void show_serial_version(void)
1692 printk(mcfrs_drivername);
1695 static const struct tty_operations mcfrs_ops = {
1697 .close = mcfrs_close,
1698 .write = mcfrs_write,
1699 .flush_chars = mcfrs_flush_chars,
1700 .write_room = mcfrs_write_room,
1701 .chars_in_buffer = mcfrs_chars_in_buffer,
1702 .flush_buffer = mcfrs_flush_buffer,
1703 .ioctl = mcfrs_ioctl,
1704 .throttle = mcfrs_throttle,
1705 .unthrottle = mcfrs_unthrottle,
1706 .set_termios = mcfrs_set_termios,
1708 .start = mcfrs_start,
1709 .hangup = mcfrs_hangup,
1710 .read_proc = mcfrs_readproc,
1711 .wait_until_sent = mcfrs_wait_until_sent,
1712 .tiocmget = mcfrs_tiocmget,
1713 .tiocmset = mcfrs_tiocmset,
1716 /* mcfrs_init inits the driver */
1720 struct mcf_serial *info;
1721 unsigned long flags;
1724 /* Setup base handler, and timer table. */
1726 init_timer(&mcfrs_timer_struct);
1727 mcfrs_timer_struct.function = mcfrs_timer;
1728 mcfrs_timer_struct.data = 0;
1729 mcfrs_timer_struct.expires = jiffies + HZ/25;
1730 add_timer(&mcfrs_timer_struct);
1731 mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1733 mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1734 if (!mcfrs_serial_driver)
1737 show_serial_version();
1739 /* Initialize the tty_driver structure */
1740 mcfrs_serial_driver->owner = THIS_MODULE;
1741 mcfrs_serial_driver->name = "ttyS";
1742 mcfrs_serial_driver->driver_name = "mcfserial";
1743 mcfrs_serial_driver->major = TTY_MAJOR;
1744 mcfrs_serial_driver->minor_start = 64;
1745 mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1746 mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1747 mcfrs_serial_driver->init_termios = tty_std_termios;
1749 mcfrs_serial_driver->init_termios.c_cflag =
1750 mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1751 mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1753 tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1755 if (tty_register_driver(mcfrs_serial_driver)) {
1756 printk("MCFRS: Couldn't register serial driver\n");
1757 put_tty_driver(mcfrs_serial_driver);
1761 local_irq_save(flags);
1764 * Configure all the attached serial ports.
1766 for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1767 info->magic = SERIAL_MAGIC;
1770 info->custom_divisor = 16;
1771 info->close_delay = 50;
1772 info->closing_wait = 3000;
1776 info->blocked_open = 0;
1777 INIT_WORK(&info->tqueue, mcfrs_offintr);
1778 INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
1779 init_waitqueue_head(&info->open_wait);
1780 init_waitqueue_head(&info->close_wait);
1783 mcfrs_setsignals(info, 0, 0);
1784 mcfrs_irqinit(info);
1786 printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1787 (unsigned int) info->addr, info->irq);
1788 printk(" is a builtin ColdFire UART\n");
1791 local_irq_restore(flags);
1795 module_init(mcfrs_init);
1797 /****************************************************************************/
1798 /* Serial Console */
1799 /****************************************************************************/
1802 * Quick and dirty UART initialization, for console output.
1805 void mcfrs_init_console(void)
1807 volatile unsigned char *uartp;
1811 * Reset UART, get it into known state...
1813 uartp = (volatile unsigned char *) (MCF_MBAR +
1814 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1816 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
1817 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
1818 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
1821 * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1823 uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1824 uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1829 * For the MCF5272, also compute the baudrate fraction.
1831 int fraction = MCF_BUSCLK - (clk * 32 * mcfrs_console_baud);
1833 fraction /= (32 * mcfrs_console_baud);
1834 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
1835 clk = (MCF_BUSCLK / mcfrs_console_baud) / 32;
1838 clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1841 uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8; /* set msb baud */
1842 uartp[MCFUART_UBG2] = (clk & 0xff); /* set lsb baud */
1843 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1844 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1846 mcfrs_console_inited++;
1852 * Setup for console. Argument comes from the boot command line.
1855 int mcfrs_console_setup(struct console *cp, char *arg)
1857 int i, n = CONSOLE_BAUD_RATE;
1862 if (!strncmp(cp->name, "ttyS", 4))
1863 mcfrs_console_port = cp->index;
1864 else if (!strncmp(cp->name, "cua", 3))
1865 mcfrs_console_port = cp->index;
1870 n = simple_strtoul(arg,NULL,0);
1871 for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1872 if (mcfrs_baud_table[i] == n)
1874 if (i < MCFRS_BAUD_TABLE_SIZE) {
1875 mcfrs_console_baud = n;
1876 mcfrs_console_cbaud = 0;
1878 mcfrs_console_cbaud |= CBAUDEX;
1881 mcfrs_console_cbaud |= i;
1883 mcfrs_init_console(); /* make sure baud rate changes */
1888 static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1891 return mcfrs_serial_driver;
1896 * Output a single character, using UART polled mode.
1897 * This is used for console output.
1900 int mcfrs_put_char(char ch)
1902 volatile unsigned char *uartp;
1903 unsigned long flags;
1906 uartp = (volatile unsigned char *) (MCF_MBAR +
1907 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1909 local_irq_save(flags);
1910 for (i = 0; (i < 0x10000); i++) {
1911 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1915 uartp[MCFUART_UTB] = ch;
1916 for (i = 0; (i < 0x10000); i++)
1917 if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1921 mcfrs_init_console(); /* try and get it back */
1922 local_irq_restore(flags);
1929 * rs_console_write is registered for printk output.
1932 void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1934 if (!mcfrs_console_inited)
1935 mcfrs_init_console();
1938 mcfrs_put_char('\r');
1939 mcfrs_put_char(*p++);
1944 * declare our consoles
1947 struct console mcfrs_console = {
1949 .write = mcfrs_console_write,
1950 .device = mcfrs_console_device,
1951 .setup = mcfrs_console_setup,
1952 .flags = CON_PRINTBUFFER,
1956 static int __init mcfrs_console_init(void)
1958 register_console(&mcfrs_console);
1962 console_initcall(mcfrs_console_init);
1964 /****************************************************************************/