ext4: Cache the correct extent length for uninit extents
[linux-2.6/lfs.git] / drivers / serial / mcfserial.c
blobddd3aa50d4adef5de5e136df8fbd5fa3a8738ad8
1 /*
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>
14 * Changes:
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>
31 #include <linux/mm.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>
40 #include <asm/io.h>
41 #include <asm/irq.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
70 #endif
72 #ifndef CONSOLE_BAUD_RATE
73 #define CONSOLE_BAUD_RATE 9600
74 #define DEFAULT_CBAUD B9600
75 #endif
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
90 /* Debugging...
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)
98 #else
99 #define IRQBASE 73
100 #endif
103 * Configuration table, UARTs to look for at startup.
105 static struct mcf_serial mcfrs_table[] = {
106 { /* ttyS0 */
107 .magic = 0,
108 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE1),
109 .irq = IRQBASE,
110 .flags = ASYNC_BOOT_AUTOCONF,
112 #ifdef MCFUART_BASE2
113 { /* ttyS1 */
114 .magic = 0,
115 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE2),
116 .irq = IRQBASE+1,
117 .flags = ASYNC_BOOT_AUTOCONF,
119 #endif
120 #ifdef MCFUART_BASE3
121 { /* ttyS2 */
122 .magic = 0,
123 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE3),
124 .irq = IRQBASE+2,
125 .flags = ASYNC_BOOT_AUTOCONF,
127 #endif
128 #ifdef MCFUART_BASE4
129 { /* ttyS3 */
130 .magic = 0,
131 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE4),
132 .irq = IRQBASE+3,
133 .flags = ASYNC_BOOT_AUTOCONF,
135 #endif
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);
157 #endif
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";
176 if (!info) {
177 printk(badinfo, name, routine);
178 return 1;
180 if (info->magic != SERIAL_MAGIC) {
181 printk(badmagic, name, routine);
182 return 1;
184 #endif
185 return 0;
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;
194 unsigned long flags;
196 #if 0
197 printk("%s(%d): mcfrs_setsignals(info=%x,dtr=%d,rts=%d)\n",
198 __FILE__, __LINE__, info, dtr, rts);
199 #endif
201 local_irq_save(flags);
202 if (dtr >= 0) {
203 #ifdef MCFPP_DTR0
204 if (info->line)
205 mcf_setppdata(MCFPP_DTR1, (dtr ? 0 : MCFPP_DTR1));
206 else
207 mcf_setppdata(MCFPP_DTR0, (dtr ? 0 : MCFPP_DTR0));
208 #endif
210 if (rts >= 0) {
211 uartp = info->addr;
212 if (rts) {
213 info->sigs |= TIOCM_RTS;
214 uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
215 } else {
216 info->sigs &= ~TIOCM_RTS;
217 uartp[MCFUART_UOP0] = MCFUART_UOP_RTS;
220 local_irq_restore(flags);
221 return;
225 * Gets values of serial signals.
227 static int mcfrs_getsignals(struct mcf_serial *info)
229 volatile unsigned char *uartp;
230 unsigned long flags;
231 int sigs;
232 #if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
233 unsigned short ppdata;
234 #endif
236 #if 0
237 printk("%s(%d): mcfrs_getsignals(info=%x)\n", __FILE__, __LINE__);
238 #endif
240 local_irq_save(flags);
241 uartp = info->addr;
242 sigs = (uartp[MCFUART_UIPR] & MCFUART_UIPR_CTS) ? 0 : TIOCM_CTS;
243 sigs |= (info->sigs & TIOCM_RTS);
245 #ifdef MCFPP_DCD0
247 unsigned int ppdata;
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;
257 #endif
259 local_irq_restore(flags);
260 return(sigs);
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;
275 unsigned long flags;
277 if (serial_paranoia_check(info, tty->name, "mcfrs_stop"))
278 return;
280 local_irq_save(flags);
281 uartp = info->addr;
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;
291 unsigned long flags;
293 if (serial_paranoia_check(info, tty->name, "mcfrs_start"))
294 return;
296 local_irq_save(flags);
297 if (info->xmit_cnt && info->xmit_buf) {
298 uartp = info->addr;
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
316 * idea to do:
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;
332 if (!tty)
333 return;
335 uartp = info->addr;
337 while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
338 ch = uartp[MCFUART_URB];
339 info->stats.rx++;
341 #ifdef CONFIG_MAGIC_SYSRQ
342 if (mcfrs_console_inited && (info->line == mcfrs_console_port)) {
343 if (magic_sysrq_key(ch))
344 continue;
346 #endif
348 flag = TTY_NORMAL;
349 if (status & MCFUART_USR_RXERR) {
350 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
351 if (status & MCFUART_USR_RXBREAK) {
352 info->stats.rxbreak++;
353 flag = TTY_BREAK;
354 } else if (status & MCFUART_USR_RXPARITY) {
355 info->stats.rxparity++;
356 flag = TTY_PARITY;
357 } else if (status & MCFUART_USR_RXOVERRUN) {
358 info->stats.rxoverrun++;
359 flag = TTY_OVERRUN;
360 } else if (status & MCFUART_USR_RXFRAMING) {
361 info->stats.rxframing++;
362 flag = TTY_FRAME;
365 tty_insert_flip_char(tty, ch, flag);
367 tty_schedule_flip(tty);
368 return;
371 static inline void transmit_chars(struct mcf_serial *info)
373 volatile unsigned char *uartp;
375 uartp = info->addr;
377 if (info->x_char) {
378 /* Send special char - probably flow control */
379 uartp[MCFUART_UTB] = info->x_char;
380 info->x_char = 0;
381 info->stats.tx++;
384 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
385 info->imr &= ~MCFUART_UIR_TXREADY;
386 uartp[MCFUART_UIMR] = info->imr;
387 return;
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);
393 info->stats.tx++;
394 if (--info->xmit_cnt <= 0)
395 break;
398 if (info->xmit_cnt < WAKEUP_CHARS)
399 schedule_work(&info->tqueue);
400 return;
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;
409 unsigned char isr;
411 info = &mcfrs_table[(irq - IRQBASE)];
412 isr = info->addr[MCFUART_UISR] & info->imr;
414 if (isr & MCFUART_UIR_RXREADY)
415 receive_chars(info);
416 if (isr & MCFUART_UIR_TXREADY)
417 transmit_chars(info);
418 return IRQ_HANDLED;
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;
432 if (tty)
433 tty_wakeup(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)
443 return;
445 if (info->flags & ASYNC_CHECK_CD) {
446 if (dcd)
447 wake_up_interruptible(&info->open_wait);
448 else
449 schedule_work(&info->tqueue_hangup);
454 #ifdef MCFPP_DCD0
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;
480 /* Re-arm timer */
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;
502 if (tty)
503 tty_hangup(tty);
506 static int startup(struct mcf_serial * info)
508 volatile unsigned char *uartp;
509 unsigned long flags;
511 if (info->flags & ASYNC_INITIALIZED)
512 return 0;
514 if (!info->xmit_buf) {
515 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
516 if (!info->xmit_buf)
517 return -ENOMEM;
520 local_irq_save(flags);
522 #ifdef SERIAL_DEBUG_OPEN
523 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
524 #endif
527 * Reset UART, get it into known state...
529 uartp = info->addr;
530 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
531 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
532 mcfrs_setsignals(info, 1, 1);
534 if (info->tty)
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
545 * interrupt enables.
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);
553 return 0;
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;
563 unsigned long flags;
565 if (!(info->flags & ASYNC_INITIALIZED))
566 return;
568 #ifdef SERIAL_DEBUG_OPEN
569 printk("Shutting down serial port %d (irq %d)....\n", info->line,
570 info->irq);
571 #endif
573 local_irq_save(flags);
575 uartp = info->addr;
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);
585 info->xmit_buf = 0;
588 if (info->tty)
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;
604 unsigned long flags;
605 unsigned char mr1, mr2;
606 int i;
607 #ifdef CONFIG_M5272
608 unsigned int fraction;
609 #endif
611 if (!info->tty || !info->tty->termios)
612 return;
613 cflag = info->tty->termios->c_cflag;
614 if (info->addr == 0)
615 return;
617 #if 0
618 printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
619 #endif
621 i = cflag & CBAUD;
622 if (i & CBAUDEX) {
623 i &= ~CBAUDEX;
624 if (i < 1 || i > 4)
625 info->tty->termios->c_cflag &= ~CBAUDEX;
626 else
627 i += 15;
629 if (i == 0) {
630 mcfrs_setsignals(info, 0, -1);
631 return;
634 /* compute the baudrate clock */
635 #ifdef CONFIG_M5272
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]);
641 fraction *= 16;
642 fraction /= (32 * mcfrs_baud_table[i]);
643 #else
644 baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
645 #endif
647 info->baud = mcfrs_baud_table[i];
649 mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
650 mr2 = 0;
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;
656 case CS8:
657 default: mr1 |= MCFUART_MR1_CS8; break;
660 if (cflag & PARENB) {
661 if (cflag & CMSPAR) {
662 if (cflag & PARODD)
663 mr1 |= MCFUART_MR1_PARITYMARK;
664 else
665 mr1 |= MCFUART_MR1_PARITYSPACE;
666 } else {
667 if (cflag & PARODD)
668 mr1 |= MCFUART_MR1_PARITYODD;
669 else
670 mr1 |= MCFUART_MR1_PARITYEVEN;
672 } else {
673 mr1 |= MCFUART_MR1_PARITYNONE;
676 if (cflag & CSTOPB)
677 mr2 |= MCFUART_MR2_STOP2;
678 else
679 mr2 |= MCFUART_MR2_STOP1;
681 if (cflag & CRTSCTS) {
682 mr1 |= MCFUART_MR1_RXRTS;
683 mr2 |= MCFUART_MR2_TXCTS;
686 if (cflag & CLOCAL)
687 info->flags &= ~ASYNC_CHECK_CD;
688 else
689 info->flags |= ASYNC_CHECK_CD;
691 uartp = info->addr;
693 local_irq_save(flags);
694 #if 0
695 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
696 mr1, mr2, baudclk);
697 #endif
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 */
710 #ifdef CONFIG_M5272
711 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
712 #endif
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);
717 return;
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;
724 unsigned long flags;
726 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
727 return;
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 ||
743 !info->xmit_buf)
744 return;
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;
758 unsigned long flags;
759 int c, total = 0;
761 #if 0
762 printk("%s(%d): mcfrs_write(tty=%x,buf=%x,count=%d)\n",
763 __FILE__, __LINE__, (int)tty, (int)buf, count);
764 #endif
766 if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
767 return 0;
769 if (!tty || !info->xmit_buf)
770 return 0;
772 local_save_flags(flags);
773 while (1) {
774 local_irq_disable();
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);
779 if (c <= 0)
780 break;
782 memcpy(info->xmit_buf + info->xmit_head, buf, c);
784 local_irq_disable();
785 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
786 info->xmit_cnt += c;
787 local_irq_restore(flags);
789 buf += c;
790 count -= c;
791 total += c;
794 local_irq_disable();
795 uartp = info->addr;
796 info->imr |= MCFUART_UIR_TXREADY;
797 uartp[MCFUART_UIMR] = info->imr;
798 local_irq_restore(flags);
800 return total;
803 static int mcfrs_write_room(struct tty_struct *tty)
805 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
806 int ret;
808 if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
809 return 0;
810 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
811 if (ret < 0)
812 ret = 0;
813 return ret;
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"))
821 return 0;
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;
828 unsigned long flags;
830 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
831 return;
833 local_irq_save(flags);
834 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
835 local_irq_restore(flags);
837 tty_wakeup(tty);
841 * ------------------------------------------------------------
842 * mcfrs_throttle()
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
852 char buf[64];
854 printk("throttle %s: %d....\n", tty_name(tty, buf),
855 tty->ldisc.chars_in_buffer(tty));
856 #endif
858 if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
859 return;
861 if (I_IXOFF(tty))
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
871 char buf[64];
873 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
874 tty->ldisc.chars_in_buffer(tty));
875 #endif
877 if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
878 return;
880 if (I_IXOFF(tty)) {
881 if (info->x_char)
882 info->x_char = 0;
883 else
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;
901 if (!retinfo)
902 return -EFAULT;
903 memset(&tmp, 0, sizeof(tmp));
904 tmp.type = info->type;
905 tmp.line = info->line;
906 tmp.port = (unsigned int) info->addr;
907 tmp.irq = info->irq;
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;
921 int retval = 0;
923 if (!new_info)
924 return -EFAULT;
925 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
926 return -EFAULT;
927 old_info = *info;
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)))
935 return -EPERM;
936 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
937 (new_serial.flags & ASYNC_USR_MASK));
938 info->custom_divisor = new_serial.custom_divisor;
939 goto check_and_exit;
942 if (info->count > 1)
943 return -EBUSY;
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;
957 check_and_exit:
958 retval = startup(info);
959 return retval;
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;
975 unsigned long flags;
976 unsigned char status;
978 local_irq_save(flags);
979 uartp = info->addr;
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;
992 unsigned long flags;
994 if (!info->addr)
995 return;
996 set_current_state(TASK_INTERRUPTIBLE);
997 uartp = info->addr;
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"))
1011 return -ENODEV;
1012 if (tty->flags & (1 << TTY_IO_ERROR))
1013 return -EIO;
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"))
1025 return -ENODEV;
1026 if (tty->flags & (1 << TTY_IO_ERROR))
1027 return -EIO;
1029 if (set & TIOCM_RTS)
1030 rts = 1;
1031 if (set & TIOCM_DTR)
1032 dtr = 1;
1033 if (clear & TIOCM_RTS)
1034 rts = 0;
1035 if (clear & TIOCM_DTR)
1036 dtr = 0;
1038 mcfrs_setsignals(info, dtr, rts);
1040 return 0;
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;
1047 int retval, error;
1049 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1050 return -ENODEV;
1052 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1053 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1054 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1055 if (tty->flags & (1 << TTY_IO_ERROR))
1056 return -EIO;
1059 switch (cmd) {
1060 case TCSBRK: /* SVID version: non-zero arg --> no break */
1061 retval = tty_check_change(tty);
1062 if (retval)
1063 return retval;
1064 tty_wait_until_sent(tty, 0);
1065 if (!arg)
1066 send_break(info, HZ/4); /* 1/4 second */
1067 return 0;
1068 case TCSBRKP: /* support for POSIX tcsendbreak() */
1069 retval = tty_check_change(tty);
1070 if (retval)
1071 return retval;
1072 tty_wait_until_sent(tty, 0);
1073 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1074 return 0;
1075 case TIOCGSOFTCAR:
1076 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1077 (unsigned long *) arg);
1078 if (error)
1079 return error;
1080 return 0;
1081 case TIOCSSOFTCAR:
1082 get_user(arg, (unsigned long *) arg);
1083 tty->termios->c_cflag =
1084 ((tty->termios->c_cflag & ~CLOCAL) |
1085 (arg ? CLOCAL : 0));
1086 return 0;
1087 case TIOCGSERIAL:
1088 if (access_ok(VERIFY_WRITE, (void *) arg,
1089 sizeof(struct serial_struct)))
1090 return get_serial_info(info,
1091 (struct serial_struct *) arg);
1092 return -EFAULT;
1093 case TIOCSSERIAL:
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);
1100 return -EFAULT;
1101 case TIOCSERGSTRUCT:
1102 error = copy_to_user((struct mcf_serial *) arg,
1103 info, sizeof(struct mcf_serial));
1104 if (error)
1105 return -EFAULT;
1106 return 0;
1108 #ifdef TIOCSET422
1109 case TIOCSET422: {
1110 unsigned int val;
1111 get_user(val, (unsigned int *) arg);
1112 mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1113 break;
1115 case TIOCGET422: {
1116 unsigned int val;
1117 val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1118 put_user(val, (unsigned int *) arg);
1119 break;
1121 #endif
1123 default:
1124 return -ENOIOCTLCMD;
1126 return 0;
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)
1134 return;
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);
1142 #if 0
1143 mcfrs_start(tty);
1144 #endif
1149 * ------------------------------------------------------------
1150 * mcfrs_close()
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"))
1165 return;
1167 local_irq_save(flags);
1169 if (tty_hung_up_p(filp)) {
1170 local_irq_restore(flags);
1171 return;
1174 #ifdef SERIAL_DEBUG_OPEN
1175 printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1176 #endif
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);
1187 info->count = 1;
1189 if (--info->count < 0) {
1190 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1191 info->line, info->count);
1192 info->count = 0;
1194 if (info->count) {
1195 local_irq_restore(flags);
1196 return;
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.
1204 tty->closing = 1;
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;
1215 uartp = info->addr;
1216 uartp[MCFUART_UIMR] = info->imr;
1218 #if 0
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 */ ;
1222 } else
1223 #endif
1224 shutdown(info);
1225 if (tty->driver->flush_buffer)
1226 tty->driver->flush_buffer(tty);
1227 tty_ldisc_flush(tty);
1229 tty->closing = 0;
1230 info->event = 0;
1231 info->tty = 0;
1232 #if 0
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);
1241 #endif
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
1256 static void
1257 mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1259 #ifdef CONFIG_M5272
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"))
1267 return;
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
1277 * the NIST-PCTS.
1279 fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1280 char_time = fifo_time / 5;
1281 if (char_time == 0)
1282 char_time = 1;
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.
1290 fifo_time *= 2;
1291 if (fifo_time < (HZ/2))
1292 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;
1301 for (;;) {
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)
1306 fifo_cnt++;
1307 if (fifo_cnt == 0)
1308 break;
1309 msleep_interruptible(jiffies_to_msecs(char_time));
1310 if (signal_pending(current))
1311 break;
1312 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1313 break;
1315 #else
1317 * For the other coldfire models, assume all data has been sent
1319 #endif
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"))
1330 return;
1332 mcfrs_flush_buffer(tty);
1333 shutdown(info);
1334 info->event = 0;
1335 info->count = 0;
1336 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1337 info->tty = 0;
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);
1350 int retval;
1351 int do_clocal = 0;
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)
1361 return -EAGAIN;
1362 else
1363 return -ERESTARTSYS;
1364 #else
1365 return -EAGAIN;
1366 #endif
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;
1376 return 0;
1379 if (tty->termios->c_cflag & CLOCAL)
1380 do_clocal = 1;
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.
1389 retval = 0;
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);
1394 #endif
1395 info->count--;
1396 info->blocked_open++;
1397 while (1) {
1398 local_irq_disable();
1399 mcfrs_setsignals(info, 1, 1);
1400 local_irq_enable();
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)
1406 retval = -EAGAIN;
1407 else
1408 retval = -ERESTARTSYS;
1409 #else
1410 retval = -EAGAIN;
1411 #endif
1412 break;
1414 if (!(info->flags & ASYNC_CLOSING) &&
1415 (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1416 break;
1417 if (signal_pending(current)) {
1418 retval = -ERESTARTSYS;
1419 break;
1421 #ifdef SERIAL_DEBUG_OPEN
1422 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1423 info->line, info->count);
1424 #endif
1425 schedule();
1427 current->state = TASK_RUNNING;
1428 remove_wait_queue(&info->open_wait, &wait);
1429 if (!tty_hung_up_p(filp))
1430 info->count++;
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);
1435 #endif
1436 if (retval)
1437 return retval;
1438 info->flags |= ASYNC_NORMAL_ACTIVE;
1439 return 0;
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;
1451 int retval, line;
1453 line = tty->index;
1454 if ((line < 0) || (line >= NR_PORTS))
1455 return -ENODEV;
1456 info = mcfrs_table + line;
1457 if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1458 return -ENODEV;
1459 #ifdef SERIAL_DEBUG_OPEN
1460 printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1461 #endif
1462 info->count++;
1463 tty->driver_data = info;
1464 info->tty = tty;
1467 * Start up serial port
1469 retval = startup(info);
1470 if (retval)
1471 return retval;
1473 retval = block_til_ready(tty, filp, info);
1474 if (retval) {
1475 #ifdef SERIAL_DEBUG_OPEN
1476 printk("mcfrs_open returning after block_til_ready with %d\n",
1477 retval);
1478 #endif
1479 return retval;
1482 #ifdef SERIAL_DEBUG_OPEN
1483 printk("mcfrs_open %s successful...\n", tty->name);
1484 #endif
1485 return 0;
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;
1498 uartp = info->addr;
1499 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1501 switch (info->line) {
1502 case 0:
1503 *icrp = 0xe0000000;
1504 break;
1505 case 1:
1506 *icrp = 0x0e000000;
1507 break;
1508 default:
1509 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1510 info->line);
1511 return;
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;
1523 uartp = info->addr;
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 +
1530 MCFINTC_IMRL);
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;
1547 #endif
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));
1554 #endif
1555 #elif defined(CONFIG_M520x)
1556 volatile unsigned char *icrp, *uartp;
1557 volatile unsigned long *imrp;
1559 uartp = info->addr;
1561 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1562 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1563 *icrp = 0x03;
1565 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1566 MCFINTC_IMRL);
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;
1586 uartp = info->addr;
1587 switch (info->line) {
1588 case 0:
1589 MCF_INTC0_ICR26 = 0x3;
1590 MCF_INTC0_CIMR = 26;
1591 /* GPIO initialization */
1592 MCF_GPIO_PAR_UART |= 0x000F;
1593 break;
1594 case 1:
1595 MCF_INTC0_ICR27 = 0x3;
1596 MCF_INTC0_CIMR = 27;
1597 /* GPIO initialization */
1598 MCF_GPIO_PAR_UART |= 0x0FF0;
1599 break;
1600 case 2:
1601 MCF_INTC0_ICR28 = 0x3;
1602 MCF_INTC0_CIMR = 28;
1603 /* GPIOs also must be initalized, depends on board */
1604 break;
1606 #else
1607 volatile unsigned char *icrp, *uartp;
1609 switch (info->line) {
1610 case 0:
1611 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1612 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1613 MCFSIM_ICR_PRI1;
1614 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1615 break;
1616 case 1:
1617 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1618 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1619 MCFSIM_ICR_PRI2;
1620 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1621 break;
1622 default:
1623 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1624 info->line);
1625 return;
1628 uartp = info->addr;
1629 uartp[MCFUART_UIVR] = info->irq;
1630 #endif
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);
1641 return;
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;
1655 char str[20];
1656 int len, sigs, i;
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)
1688 strcat(str, "|CD");
1691 len += sprintf((page + len), "%s\n", &str[1]);
1694 return(len);
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 = {
1706 .open = mcfrs_open,
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,
1717 .stop = mcfrs_stop,
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 */
1727 static int __init
1728 mcfrs_init(void)
1730 struct mcf_serial *info;
1731 unsigned long flags;
1732 int i;
1734 /* Setup base handler, and timer table. */
1735 #ifdef MCFPP_DCD0
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);
1742 #endif
1743 mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1744 if (!mcfrs_serial_driver)
1745 return -ENOMEM;
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);
1768 return(-EBUSY);
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;
1778 info->line = i;
1779 info->tty = 0;
1780 info->custom_divisor = 16;
1781 info->close_delay = 50;
1782 info->closing_wait = 3000;
1783 info->x_char = 0;
1784 info->event = 0;
1785 info->count = 0;
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);
1792 info->imr = 0;
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);
1802 return 0;
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;
1818 unsigned int clk;
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;
1836 #ifdef CONFIG_M5272
1839 * For the MCF5272, also compute the baudrate fraction.
1841 int fraction = MCF_BUSCLK - (clk * 32 * mcfrs_console_baud);
1842 fraction *= 16;
1843 fraction /= (32 * mcfrs_console_baud);
1844 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
1845 clk = (MCF_BUSCLK / mcfrs_console_baud) / 32;
1847 #else
1848 clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1849 #endif
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++;
1857 return;
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;
1869 if (!cp)
1870 return(-1);
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;
1876 else
1877 return(-1);
1879 if (arg)
1880 n = simple_strtoul(arg,NULL,0);
1881 for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1882 if (mcfrs_baud_table[i] == n)
1883 break;
1884 if (i < MCFRS_BAUD_TABLE_SIZE) {
1885 mcfrs_console_baud = n;
1886 mcfrs_console_cbaud = 0;
1887 if (i > 15) {
1888 mcfrs_console_cbaud |= CBAUDEX;
1889 i -= 15;
1891 mcfrs_console_cbaud |= i;
1893 mcfrs_init_console(); /* make sure baud rate changes */
1894 return(0);
1898 static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1900 *index = c->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;
1914 int i;
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)
1922 break;
1924 if (i < 0x10000) {
1925 uartp[MCFUART_UTB] = ch;
1926 for (i = 0; (i < 0x10000); i++)
1927 if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1928 break;
1930 if (i >= 0x10000)
1931 mcfrs_init_console(); /* try and get it back */
1932 local_irq_restore(flags);
1934 return;
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();
1946 while (len-- > 0) {
1947 if (*p == '\n')
1948 mcfrs_put_char('\r');
1949 mcfrs_put_char(*p++);
1954 * declare our consoles
1957 struct console mcfrs_console = {
1958 .name = "ttyS",
1959 .write = mcfrs_console_write,
1960 .device = mcfrs_console_device,
1961 .setup = mcfrs_console_setup,
1962 .flags = CON_PRINTBUFFER,
1963 .index = -1,
1966 static int __init mcfrs_console_init(void)
1968 register_console(&mcfrs_console);
1969 return 0;
1972 console_initcall(mcfrs_console_init);
1974 /****************************************************************************/