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 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1077 (unsigned long *) arg);
1082 get_user(arg, (unsigned long *) arg);
1083 tty->termios->c_cflag =
1084 ((tty->termios->c_cflag & ~CLOCAL) |
1085 (arg ? CLOCAL : 0));
1088 if (access_ok(VERIFY_WRITE, (void *) arg,
1089 sizeof(struct serial_struct)))
1090 return get_serial_info(info,
1091 (struct serial_struct *) arg);
1094 return set_serial_info(info,
1095 (struct serial_struct *) arg);
1096 case TIOCSERGETLSR: /* Get line status register */
1097 if (access_ok(VERIFY_WRITE, (void *) arg,
1098 sizeof(unsigned int)))
1099 return get_lsr_info(info, (unsigned int *) arg);
1101 case TIOCSERGSTRUCT:
1102 error = copy_to_user((struct mcf_serial *) arg,
1103 info, sizeof(struct mcf_serial));
1111 get_user(val, (unsigned int *) arg);
1112 mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1117 val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1118 put_user(val, (unsigned int *) arg);
1124 return -ENOIOCTLCMD;
1129 static void mcfrs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1131 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1133 if (tty->termios->c_cflag == old_termios->c_cflag)
1136 mcfrs_change_speed(info);
1138 if ((old_termios->c_cflag & CRTSCTS) &&
1139 !(tty->termios->c_cflag & CRTSCTS)) {
1140 tty->hw_stopped = 0;
1141 mcfrs_setsignals(info, -1, 1);
1149 * ------------------------------------------------------------
1152 * This routine is called when the serial port gets closed. First, we
1153 * wait for the last remaining data to be sent. Then, we unlink its
1154 * S structure from the interrupt chain if necessary, and we free
1155 * that IRQ if nothing is left in the chain.
1156 * ------------------------------------------------------------
1158 static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1160 volatile unsigned char *uartp;
1161 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1162 unsigned long flags;
1164 if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1167 local_irq_save(flags);
1169 if (tty_hung_up_p(filp)) {
1170 local_irq_restore(flags);
1174 #ifdef SERIAL_DEBUG_OPEN
1175 printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1177 if ((tty->count == 1) && (info->count != 1)) {
1179 * Uh, oh. tty->count is 1, which means that the tty
1180 * structure will be freed. Info->count should always
1181 * be one in these conditions. If it's greater than
1182 * one, we've got real problems, since it means the
1183 * serial port won't be shutdown.
1185 printk("MCFRS: bad serial port count; tty->count is 1, "
1186 "info->count is %d\n", info->count);
1189 if (--info->count < 0) {
1190 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1191 info->line, info->count);
1195 local_irq_restore(flags);
1198 info->flags |= ASYNC_CLOSING;
1201 * Now we wait for the transmit buffer to clear; and we notify
1202 * the line discipline to only process XON/XOFF characters.
1205 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1206 tty_wait_until_sent(tty, info->closing_wait);
1209 * At this point we stop accepting input. To do this, we
1210 * disable the receive line status interrupts, and tell the
1211 * interrupt driver to stop checking the data ready bit in the
1212 * line status register.
1214 info->imr &= ~MCFUART_UIR_RXREADY;
1216 uartp[MCFUART_UIMR] = info->imr;
1219 /* FIXME: do we need to keep this enabled for console?? */
1220 if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
1221 /* Do not disable the UART */ ;
1225 if (tty->driver->flush_buffer)
1226 tty->driver->flush_buffer(tty);
1227 tty_ldisc_flush(tty);
1233 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1234 if (tty->ldisc.close)
1235 (tty->ldisc.close)(tty);
1236 tty->ldisc = ldiscs[N_TTY];
1237 tty->termios->c_line = N_TTY;
1238 if (tty->ldisc.open)
1239 (tty->ldisc.open)(tty);
1242 if (info->blocked_open) {
1243 if (info->close_delay) {
1244 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1246 wake_up_interruptible(&info->open_wait);
1248 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1249 wake_up_interruptible(&info->close_wait);
1250 local_irq_restore(flags);
1254 * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1257 mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1260 #define MCF5272_FIFO_SIZE 25 /* fifo size + shift reg */
1262 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1263 volatile unsigned char *uartp;
1264 unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1266 if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1269 orig_jiffies = jiffies;
1272 * Set the check interval to be 1/5 of the approximate time
1273 * to send the entire fifo, and make it at least 1. The check
1274 * interval should also be less than the timeout.
1276 * Note: we have to use pretty tight timings here to satisfy
1279 fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1280 char_time = fifo_time / 5;
1283 if (timeout && timeout < char_time)
1284 char_time = timeout;
1287 * Clamp the timeout period at 2 * the time to empty the
1288 * fifo. Just to be safe, set the minimum at .5 seconds.
1291 if (fifo_time < (HZ/2))
1293 if (!timeout || timeout > fifo_time)
1294 timeout = fifo_time;
1297 * Account for the number of bytes in the UART
1298 * transmitter FIFO plus any byte being shifted out.
1300 uartp = (volatile unsigned char *) info->addr;
1302 fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1303 if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1304 MCFUART_USR_TXEMPTY)) ==
1305 MCFUART_USR_TXREADY)
1309 msleep_interruptible(jiffies_to_msecs(char_time));
1310 if (signal_pending(current))
1312 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1317 * For the other coldfire models, assume all data has been sent
1323 * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1325 void mcfrs_hangup(struct tty_struct *tty)
1327 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1329 if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
1332 mcfrs_flush_buffer(tty);
1336 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1338 wake_up_interruptible(&info->open_wait);
1342 * ------------------------------------------------------------
1343 * mcfrs_open() and friends
1344 * ------------------------------------------------------------
1346 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1347 struct mcf_serial *info)
1349 DECLARE_WAITQUEUE(wait, current);
1354 * If the device is in the middle of being closed, then block
1355 * until it's done, and then try again.
1357 if (info->flags & ASYNC_CLOSING) {
1358 interruptible_sleep_on(&info->close_wait);
1359 #ifdef SERIAL_DO_RESTART
1360 if (info->flags & ASYNC_HUP_NOTIFY)
1363 return -ERESTARTSYS;
1370 * If non-blocking mode is set, or the port is not enabled,
1371 * then make the check up front and then exit.
1373 if ((filp->f_flags & O_NONBLOCK) ||
1374 (tty->flags & (1 << TTY_IO_ERROR))) {
1375 info->flags |= ASYNC_NORMAL_ACTIVE;
1379 if (tty->termios->c_cflag & CLOCAL)
1383 * Block waiting for the carrier detect and the line to become
1384 * free (i.e., not in use by the callout). While we are in
1385 * this loop, info->count is dropped by one, so that
1386 * mcfrs_close() knows when to free things. We restore it upon
1387 * exit, either normal or abnormal.
1390 add_wait_queue(&info->open_wait, &wait);
1391 #ifdef SERIAL_DEBUG_OPEN
1392 printk("block_til_ready before block: ttyS%d, count = %d\n",
1393 info->line, info->count);
1396 info->blocked_open++;
1398 local_irq_disable();
1399 mcfrs_setsignals(info, 1, 1);
1401 current->state = TASK_INTERRUPTIBLE;
1402 if (tty_hung_up_p(filp) ||
1403 !(info->flags & ASYNC_INITIALIZED)) {
1404 #ifdef SERIAL_DO_RESTART
1405 if (info->flags & ASYNC_HUP_NOTIFY)
1408 retval = -ERESTARTSYS;
1414 if (!(info->flags & ASYNC_CLOSING) &&
1415 (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1417 if (signal_pending(current)) {
1418 retval = -ERESTARTSYS;
1421 #ifdef SERIAL_DEBUG_OPEN
1422 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1423 info->line, info->count);
1427 current->state = TASK_RUNNING;
1428 remove_wait_queue(&info->open_wait, &wait);
1429 if (!tty_hung_up_p(filp))
1431 info->blocked_open--;
1432 #ifdef SERIAL_DEBUG_OPEN
1433 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1434 info->line, info->count);
1438 info->flags |= ASYNC_NORMAL_ACTIVE;
1443 * This routine is called whenever a serial port is opened. It
1444 * enables interrupts for a serial port, linking in its structure into
1445 * the IRQ chain. It also performs the serial-specific
1446 * initialization for the tty structure.
1448 int mcfrs_open(struct tty_struct *tty, struct file * filp)
1450 struct mcf_serial *info;
1454 if ((line < 0) || (line >= NR_PORTS))
1456 info = mcfrs_table + line;
1457 if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1459 #ifdef SERIAL_DEBUG_OPEN
1460 printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1463 tty->driver_data = info;
1467 * Start up serial port
1469 retval = startup(info);
1473 retval = block_til_ready(tty, filp, info);
1475 #ifdef SERIAL_DEBUG_OPEN
1476 printk("mcfrs_open returning after block_til_ready with %d\n",
1482 #ifdef SERIAL_DEBUG_OPEN
1483 printk("mcfrs_open %s successful...\n", tty->name);
1489 * Based on the line number set up the internal interrupt stuff.
1491 static void mcfrs_irqinit(struct mcf_serial *info)
1493 #if defined(CONFIG_M5272)
1494 volatile unsigned long *icrp;
1495 volatile unsigned long *portp;
1496 volatile unsigned char *uartp;
1499 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1501 switch (info->line) {
1509 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1514 /* Enable the output lines for the serial ports */
1515 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
1516 *portp = (*portp & ~0x000000ff) | 0x00000055;
1517 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
1518 *portp = (*portp & ~0x000003fc) | 0x000002a8;
1519 #elif defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x)
1520 volatile unsigned char *icrp, *uartp;
1521 volatile unsigned long *imrp;
1525 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1526 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1527 *icrp = 0x30 + info->line; /* level 6, line based priority */
1529 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1531 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1532 #if defined(CONFIG_M527x)
1535 * External Pin Mask Setting & Enable External Pin for Interface
1536 * mrcbis@aliceposta.it
1538 u16 *serpin_enable_mask;
1539 serpin_enable_mask = (u16 *) (MCF_IPSBAR + MCF_GPIO_PAR_UART);
1540 if (info->line == 0)
1541 *serpin_enable_mask |= UART0_ENABLE_MASK;
1542 else if (info->line == 1)
1543 *serpin_enable_mask |= UART1_ENABLE_MASK;
1544 else if (info->line == 2)
1545 *serpin_enable_mask |= UART2_ENABLE_MASK;
1548 #if defined(CONFIG_M528x)
1549 /* make sure PUAPAR is set for UART0 and UART1 */
1550 if (info->line < 2) {
1551 volatile unsigned char *portp = (volatile unsigned char *) (MCF_MBAR + MCF5282_GPIO_PUAPAR);
1552 *portp |= (0x03 << (info->line * 2));
1555 #elif defined(CONFIG_M520x)
1556 volatile unsigned char *icrp, *uartp;
1557 volatile unsigned long *imrp;
1561 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1562 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1565 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1567 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1568 if (info->line < 2) {
1569 unsigned short *uart_par;
1570 uart_par = (unsigned short *)(MCF_IPSBAR + MCF_GPIO_PAR_UART);
1571 if (info->line == 0)
1572 *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD0
1573 | MCF_GPIO_PAR_UART_PAR_URXD0;
1574 else if (info->line == 1)
1575 *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD1
1576 | MCF_GPIO_PAR_UART_PAR_URXD1;
1577 } else if (info->line == 2) {
1578 unsigned char *feci2c_par;
1579 feci2c_par = (unsigned char *)(MCF_IPSBAR + MCF_GPIO_PAR_FECI2C);
1580 *feci2c_par &= ~0x0F;
1581 *feci2c_par |= MCF_GPIO_PAR_FECI2C_PAR_SCL_UTXD2
1582 | MCF_GPIO_PAR_FECI2C_PAR_SDA_URXD2;
1584 #elif defined(CONFIG_M532x)
1585 volatile unsigned char *uartp;
1587 switch (info->line) {
1589 MCF_INTC0_ICR26 = 0x3;
1590 MCF_INTC0_CIMR = 26;
1591 /* GPIO initialization */
1592 MCF_GPIO_PAR_UART |= 0x000F;
1595 MCF_INTC0_ICR27 = 0x3;
1596 MCF_INTC0_CIMR = 27;
1597 /* GPIO initialization */
1598 MCF_GPIO_PAR_UART |= 0x0FF0;
1601 MCF_INTC0_ICR28 = 0x3;
1602 MCF_INTC0_CIMR = 28;
1603 /* GPIOs also must be initalized, depends on board */
1607 volatile unsigned char *icrp, *uartp;
1609 switch (info->line) {
1611 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1612 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1614 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1617 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1618 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1620 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1623 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1629 uartp[MCFUART_UIVR] = info->irq;
1632 /* Clear mask, so no surprise interrupts. */
1633 uartp[MCFUART_UIMR] = 0;
1635 if (request_irq(info->irq, mcfrs_interrupt, IRQF_DISABLED,
1636 "ColdFire UART", NULL)) {
1637 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1638 "vector=%d\n", info->line, info->irq);
1645 char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1649 * Serial stats reporting...
1651 int mcfrs_readproc(char *page, char **start, off_t off, int count,
1652 int *eof, void *data)
1654 struct mcf_serial *info;
1658 len = sprintf(page, mcfrs_drivername);
1659 for (i = 0; (i < NR_PORTS); i++) {
1660 info = &mcfrs_table[i];
1661 len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1662 i, (unsigned int) info->addr, info->irq, info->baud);
1663 if (info->stats.rx || info->stats.tx)
1664 len += sprintf((page + len), "tx:%d rx:%d ",
1665 info->stats.tx, info->stats.rx);
1666 if (info->stats.rxframing)
1667 len += sprintf((page + len), "fe:%d ",
1668 info->stats.rxframing);
1669 if (info->stats.rxparity)
1670 len += sprintf((page + len), "pe:%d ",
1671 info->stats.rxparity);
1672 if (info->stats.rxbreak)
1673 len += sprintf((page + len), "brk:%d ",
1674 info->stats.rxbreak);
1675 if (info->stats.rxoverrun)
1676 len += sprintf((page + len), "oe:%d ",
1677 info->stats.rxoverrun);
1679 str[0] = str[1] = 0;
1680 if ((sigs = mcfrs_getsignals(info))) {
1681 if (sigs & TIOCM_RTS)
1682 strcat(str, "|RTS");
1683 if (sigs & TIOCM_CTS)
1684 strcat(str, "|CTS");
1685 if (sigs & TIOCM_DTR)
1686 strcat(str, "|DTR");
1687 if (sigs & TIOCM_CD)
1691 len += sprintf((page + len), "%s\n", &str[1]);
1698 /* Finally, routines used to initialize the serial driver. */
1700 static void show_serial_version(void)
1702 printk(mcfrs_drivername);
1705 static const struct tty_operations mcfrs_ops = {
1707 .close = mcfrs_close,
1708 .write = mcfrs_write,
1709 .flush_chars = mcfrs_flush_chars,
1710 .write_room = mcfrs_write_room,
1711 .chars_in_buffer = mcfrs_chars_in_buffer,
1712 .flush_buffer = mcfrs_flush_buffer,
1713 .ioctl = mcfrs_ioctl,
1714 .throttle = mcfrs_throttle,
1715 .unthrottle = mcfrs_unthrottle,
1716 .set_termios = mcfrs_set_termios,
1718 .start = mcfrs_start,
1719 .hangup = mcfrs_hangup,
1720 .read_proc = mcfrs_readproc,
1721 .wait_until_sent = mcfrs_wait_until_sent,
1722 .tiocmget = mcfrs_tiocmget,
1723 .tiocmset = mcfrs_tiocmset,
1726 /* mcfrs_init inits the driver */
1730 struct mcf_serial *info;
1731 unsigned long flags;
1734 /* Setup base handler, and timer table. */
1736 init_timer(&mcfrs_timer_struct);
1737 mcfrs_timer_struct.function = mcfrs_timer;
1738 mcfrs_timer_struct.data = 0;
1739 mcfrs_timer_struct.expires = jiffies + HZ/25;
1740 add_timer(&mcfrs_timer_struct);
1741 mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1743 mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1744 if (!mcfrs_serial_driver)
1747 show_serial_version();
1749 /* Initialize the tty_driver structure */
1750 mcfrs_serial_driver->owner = THIS_MODULE;
1751 mcfrs_serial_driver->name = "ttyS";
1752 mcfrs_serial_driver->driver_name = "mcfserial";
1753 mcfrs_serial_driver->major = TTY_MAJOR;
1754 mcfrs_serial_driver->minor_start = 64;
1755 mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1756 mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1757 mcfrs_serial_driver->init_termios = tty_std_termios;
1759 mcfrs_serial_driver->init_termios.c_cflag =
1760 mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1761 mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1763 tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1765 if (tty_register_driver(mcfrs_serial_driver)) {
1766 printk("MCFRS: Couldn't register serial driver\n");
1767 put_tty_driver(mcfrs_serial_driver);
1771 local_irq_save(flags);
1774 * Configure all the attached serial ports.
1776 for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1777 info->magic = SERIAL_MAGIC;
1780 info->custom_divisor = 16;
1781 info->close_delay = 50;
1782 info->closing_wait = 3000;
1786 info->blocked_open = 0;
1787 INIT_WORK(&info->tqueue, mcfrs_offintr);
1788 INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
1789 init_waitqueue_head(&info->open_wait);
1790 init_waitqueue_head(&info->close_wait);
1793 mcfrs_setsignals(info, 0, 0);
1794 mcfrs_irqinit(info);
1796 printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1797 (unsigned int) info->addr, info->irq);
1798 printk(" is a builtin ColdFire UART\n");
1801 local_irq_restore(flags);
1805 module_init(mcfrs_init);
1807 /****************************************************************************/
1808 /* Serial Console */
1809 /****************************************************************************/
1812 * Quick and dirty UART initialization, for console output.
1815 void mcfrs_init_console(void)
1817 volatile unsigned char *uartp;
1821 * Reset UART, get it into known state...
1823 uartp = (volatile unsigned char *) (MCF_MBAR +
1824 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1826 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
1827 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
1828 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
1831 * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1833 uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1834 uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1839 * For the MCF5272, also compute the baudrate fraction.
1841 int fraction = MCF_BUSCLK - (clk * 32 * mcfrs_console_baud);
1843 fraction /= (32 * mcfrs_console_baud);
1844 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
1845 clk = (MCF_BUSCLK / mcfrs_console_baud) / 32;
1848 clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1851 uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8; /* set msb baud */
1852 uartp[MCFUART_UBG2] = (clk & 0xff); /* set lsb baud */
1853 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1854 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1856 mcfrs_console_inited++;
1862 * Setup for console. Argument comes from the boot command line.
1865 int mcfrs_console_setup(struct console *cp, char *arg)
1867 int i, n = CONSOLE_BAUD_RATE;
1872 if (!strncmp(cp->name, "ttyS", 4))
1873 mcfrs_console_port = cp->index;
1874 else if (!strncmp(cp->name, "cua", 3))
1875 mcfrs_console_port = cp->index;
1880 n = simple_strtoul(arg,NULL,0);
1881 for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1882 if (mcfrs_baud_table[i] == n)
1884 if (i < MCFRS_BAUD_TABLE_SIZE) {
1885 mcfrs_console_baud = n;
1886 mcfrs_console_cbaud = 0;
1888 mcfrs_console_cbaud |= CBAUDEX;
1891 mcfrs_console_cbaud |= i;
1893 mcfrs_init_console(); /* make sure baud rate changes */
1898 static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1901 return mcfrs_serial_driver;
1906 * Output a single character, using UART polled mode.
1907 * This is used for console output.
1910 void mcfrs_put_char(char ch)
1912 volatile unsigned char *uartp;
1913 unsigned long flags;
1916 uartp = (volatile unsigned char *) (MCF_MBAR +
1917 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1919 local_irq_save(flags);
1920 for (i = 0; (i < 0x10000); i++) {
1921 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1925 uartp[MCFUART_UTB] = ch;
1926 for (i = 0; (i < 0x10000); i++)
1927 if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1931 mcfrs_init_console(); /* try and get it back */
1932 local_irq_restore(flags);
1939 * rs_console_write is registered for printk output.
1942 void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1944 if (!mcfrs_console_inited)
1945 mcfrs_init_console();
1948 mcfrs_put_char('\r');
1949 mcfrs_put_char(*p++);
1954 * declare our consoles
1957 struct console mcfrs_console = {
1959 .write = mcfrs_console_write,
1960 .device = mcfrs_console_device,
1961 .setup = mcfrs_console_setup,
1962 .flags = CON_PRINTBUFFER,
1966 static int __init mcfrs_console_init(void)
1968 register_console(&mcfrs_console);
1972 console_initcall(mcfrs_console_init);
1974 /****************************************************************************/