Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / serial / mcfserial.c
blob8c40167778dea949e077954a10445736f9f6ea21
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/segment.h>
44 #include <asm/semaphore.h>
45 #include <asm/delay.h>
46 #include <asm/coldfire.h>
47 #include <asm/mcfsim.h>
48 #include <asm/mcfuart.h>
49 #include <asm/nettel.h>
50 #include <asm/uaccess.h>
51 #include "mcfserial.h"
53 struct timer_list mcfrs_timer_struct;
56 * Default console baud rate, we use this as the default
57 * for all ports so init can just open /dev/console and
58 * keep going. Perhaps one day the cflag settings for the
59 * console can be used instead.
61 #if defined(CONFIG_ARNEWSH) || defined(CONFIG_MOTOROLA) || defined(CONFIG_senTec) || defined(CONFIG_SNEHA)
62 #define CONSOLE_BAUD_RATE 19200
63 #define DEFAULT_CBAUD B19200
64 #endif
66 #if defined(CONFIG_HW_FEITH)
67 #define CONSOLE_BAUD_RATE 38400
68 #define DEFAULT_CBAUD B38400
69 #endif
71 #ifndef CONSOLE_BAUD_RATE
72 #define CONSOLE_BAUD_RATE 9600
73 #define DEFAULT_CBAUD B9600
74 #endif
76 int mcfrs_console_inited = 0;
77 int mcfrs_console_port = -1;
78 int mcfrs_console_baud = CONSOLE_BAUD_RATE;
79 int mcfrs_console_cbaud = DEFAULT_CBAUD;
82 * Driver data structures.
84 static struct tty_driver *mcfrs_serial_driver;
86 /* number of characters left in xmit buffer before we ask for more */
87 #define WAKEUP_CHARS 256
89 /* Debugging...
91 #undef SERIAL_DEBUG_OPEN
92 #undef SERIAL_DEBUG_FLOW
94 #if defined(CONFIG_M527x) || defined(CONFIG_M528x)
95 #define IRQBASE (MCFINT_VECBASE+MCFINT_UART0)
96 #else
97 #define IRQBASE 73
98 #endif
101 * Configuration table, UARTs to look for at startup.
103 static struct mcf_serial mcfrs_table[] = {
104 { /* ttyS0 */
105 .magic = 0,
106 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE1),
107 .irq = IRQBASE,
108 .flags = ASYNC_BOOT_AUTOCONF,
110 { /* ttyS1 */
111 .magic = 0,
112 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE2),
113 .irq = IRQBASE+1,
114 .flags = ASYNC_BOOT_AUTOCONF,
119 #define NR_PORTS (sizeof(mcfrs_table) / sizeof(struct mcf_serial))
122 * This is used to figure out the divisor speeds and the timeouts.
124 static int mcfrs_baud_table[] = {
125 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
126 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0
128 #define MCFRS_BAUD_TABLE_SIZE \
129 (sizeof(mcfrs_baud_table)/sizeof(mcfrs_baud_table[0]))
132 #ifdef CONFIG_MAGIC_SYSRQ
134 * Magic system request keys. Used for debugging...
136 extern int magic_sysrq_key(int ch);
137 #endif
141 * Forware declarations...
143 static void mcfrs_change_speed(struct mcf_serial *info);
144 static void mcfrs_wait_until_sent(struct tty_struct *tty, int timeout);
147 static inline int serial_paranoia_check(struct mcf_serial *info,
148 char *name, const char *routine)
150 #ifdef SERIAL_PARANOIA_CHECK
151 static const char badmagic[] =
152 "MCFRS(warning): bad magic number for serial struct %s in %s\n";
153 static const char badinfo[] =
154 "MCFRS(warning): null mcf_serial for %s in %s\n";
156 if (!info) {
157 printk(badinfo, name, routine);
158 return 1;
160 if (info->magic != SERIAL_MAGIC) {
161 printk(badmagic, name, routine);
162 return 1;
164 #endif
165 return 0;
169 * Sets or clears DTR and RTS on the requested line.
171 static void mcfrs_setsignals(struct mcf_serial *info, int dtr, int rts)
173 volatile unsigned char *uartp;
174 unsigned long flags;
176 #if 0
177 printk("%s(%d): mcfrs_setsignals(info=%x,dtr=%d,rts=%d)\n",
178 __FILE__, __LINE__, info, dtr, rts);
179 #endif
181 local_irq_save(flags);
182 if (dtr >= 0) {
183 #ifdef MCFPP_DTR0
184 if (info->line)
185 mcf_setppdata(MCFPP_DTR1, (dtr ? 0 : MCFPP_DTR1));
186 else
187 mcf_setppdata(MCFPP_DTR0, (dtr ? 0 : MCFPP_DTR0));
188 #endif
190 if (rts >= 0) {
191 uartp = info->addr;
192 if (rts) {
193 info->sigs |= TIOCM_RTS;
194 uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
195 } else {
196 info->sigs &= ~TIOCM_RTS;
197 uartp[MCFUART_UOP0] = MCFUART_UOP_RTS;
200 local_irq_restore(flags);
201 return;
205 * Gets values of serial signals.
207 static int mcfrs_getsignals(struct mcf_serial *info)
209 volatile unsigned char *uartp;
210 unsigned long flags;
211 int sigs;
212 #if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
213 unsigned short ppdata;
214 #endif
216 #if 0
217 printk("%s(%d): mcfrs_getsignals(info=%x)\n", __FILE__, __LINE__);
218 #endif
220 local_irq_save(flags);
221 uartp = info->addr;
222 sigs = (uartp[MCFUART_UIPR] & MCFUART_UIPR_CTS) ? 0 : TIOCM_CTS;
223 sigs |= (info->sigs & TIOCM_RTS);
225 #ifdef MCFPP_DCD0
227 unsigned int ppdata;
228 ppdata = mcf_getppdata();
229 if (info->line == 0) {
230 sigs |= (ppdata & MCFPP_DCD0) ? 0 : TIOCM_CD;
231 sigs |= (ppdata & MCFPP_DTR0) ? 0 : TIOCM_DTR;
232 } else if (info->line == 1) {
233 sigs |= (ppdata & MCFPP_DCD1) ? 0 : TIOCM_CD;
234 sigs |= (ppdata & MCFPP_DTR1) ? 0 : TIOCM_DTR;
237 #endif
239 local_irq_restore(flags);
240 return(sigs);
244 * ------------------------------------------------------------
245 * mcfrs_stop() and mcfrs_start()
247 * This routines are called before setting or resetting tty->stopped.
248 * They enable or disable transmitter interrupts, as necessary.
249 * ------------------------------------------------------------
251 static void mcfrs_stop(struct tty_struct *tty)
253 volatile unsigned char *uartp;
254 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
255 unsigned long flags;
257 if (serial_paranoia_check(info, tty->name, "mcfrs_stop"))
258 return;
260 local_irq_save(flags);
261 uartp = info->addr;
262 info->imr &= ~MCFUART_UIR_TXREADY;
263 uartp[MCFUART_UIMR] = info->imr;
264 local_irq_restore(flags);
267 static void mcfrs_start(struct tty_struct *tty)
269 volatile unsigned char *uartp;
270 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
271 unsigned long flags;
273 if (serial_paranoia_check(info, tty->name, "mcfrs_start"))
274 return;
276 local_irq_save(flags);
277 if (info->xmit_cnt && info->xmit_buf) {
278 uartp = info->addr;
279 info->imr |= MCFUART_UIR_TXREADY;
280 uartp[MCFUART_UIMR] = info->imr;
282 local_irq_restore(flags);
286 * ----------------------------------------------------------------------
288 * Here starts the interrupt handling routines. All of the following
289 * subroutines are declared as inline and are folded into
290 * mcfrs_interrupt(). They were separated out for readability's sake.
292 * Note: mcfrs_interrupt() is a "fast" interrupt, which means that it
293 * runs with interrupts turned off. People who may want to modify
294 * mcfrs_interrupt() should try to keep the interrupt handler as fast as
295 * possible. After you are done making modifications, it is not a bad
296 * idea to do:
298 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
300 * and look at the resulting assemble code in serial.s.
302 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
303 * -----------------------------------------------------------------------
306 static inline void receive_chars(struct mcf_serial *info)
308 volatile unsigned char *uartp;
309 struct tty_struct *tty = info->tty;
310 unsigned char status, ch;
312 if (!tty)
313 return;
315 uartp = info->addr;
317 while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
319 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
320 break;
322 ch = uartp[MCFUART_URB];
323 info->stats.rx++;
325 #ifdef CONFIG_MAGIC_SYSRQ
326 if (mcfrs_console_inited && (info->line == mcfrs_console_port)) {
327 if (magic_sysrq_key(ch))
328 continue;
330 #endif
332 tty->flip.count++;
333 if (status & MCFUART_USR_RXERR) {
334 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
335 if (status & MCFUART_USR_RXBREAK) {
336 info->stats.rxbreak++;
337 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
338 } else if (status & MCFUART_USR_RXPARITY) {
339 info->stats.rxparity++;
340 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
341 } else if (status & MCFUART_USR_RXOVERRUN) {
342 info->stats.rxoverrun++;
343 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
344 } else if (status & MCFUART_USR_RXFRAMING) {
345 info->stats.rxframing++;
346 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
347 } else {
348 /* This should never happen... */
349 *tty->flip.flag_buf_ptr++ = 0;
351 } else {
352 *tty->flip.flag_buf_ptr++ = 0;
354 *tty->flip.char_buf_ptr++ = ch;
357 schedule_work(&tty->flip.work);
358 return;
361 static inline void transmit_chars(struct mcf_serial *info)
363 volatile unsigned char *uartp;
365 uartp = info->addr;
367 if (info->x_char) {
368 /* Send special char - probably flow control */
369 uartp[MCFUART_UTB] = info->x_char;
370 info->x_char = 0;
371 info->stats.tx++;
374 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
375 info->imr &= ~MCFUART_UIR_TXREADY;
376 uartp[MCFUART_UIMR] = info->imr;
377 return;
380 while (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) {
381 uartp[MCFUART_UTB] = info->xmit_buf[info->xmit_tail++];
382 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
383 info->stats.tx++;
384 if (--info->xmit_cnt <= 0)
385 break;
388 if (info->xmit_cnt < WAKEUP_CHARS)
389 schedule_work(&info->tqueue);
390 return;
394 * This is the serial driver's generic interrupt routine
396 irqreturn_t mcfrs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
398 struct mcf_serial *info;
399 unsigned char isr;
401 info = &mcfrs_table[(irq - IRQBASE)];
402 isr = info->addr[MCFUART_UISR] & info->imr;
404 if (isr & MCFUART_UIR_RXREADY)
405 receive_chars(info);
406 if (isr & MCFUART_UIR_TXREADY)
407 transmit_chars(info);
408 return IRQ_HANDLED;
412 * -------------------------------------------------------------------
413 * Here ends the serial interrupt routines.
414 * -------------------------------------------------------------------
417 static void mcfrs_offintr(void *private)
419 struct mcf_serial *info = (struct mcf_serial *) private;
420 struct tty_struct *tty;
422 tty = info->tty;
423 if (!tty)
424 return;
425 tty_wakeup(tty);
430 * Change of state on a DCD line.
432 void mcfrs_modem_change(struct mcf_serial *info, int dcd)
434 if (info->count == 0)
435 return;
437 if (info->flags & ASYNC_CHECK_CD) {
438 if (dcd)
439 wake_up_interruptible(&info->open_wait);
440 else
441 schedule_work(&info->tqueue_hangup);
446 #ifdef MCFPP_DCD0
448 unsigned short mcfrs_ppstatus;
451 * This subroutine is called when the RS_TIMER goes off. It is used
452 * to monitor the state of the DCD lines - since they have no edge
453 * sensors and interrupt generators.
455 static void mcfrs_timer(void)
457 unsigned int ppstatus, dcdval, i;
459 ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
461 if (ppstatus != mcfrs_ppstatus) {
462 for (i = 0; (i < 2); i++) {
463 dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
464 if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
465 mcfrs_modem_change(&mcfrs_table[i],
466 ((ppstatus & dcdval) ? 0 : 1));
470 mcfrs_ppstatus = ppstatus;
472 /* Re-arm timer */
473 mcfrs_timer_struct.expires = jiffies + HZ/25;
474 add_timer(&mcfrs_timer_struct);
477 #endif /* MCFPP_DCD0 */
481 * This routine is called from the scheduler tqueue when the interrupt
482 * routine has signalled that a hangup has occurred. The path of
483 * hangup processing is:
485 * serial interrupt routine -> (scheduler tqueue) ->
486 * do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
489 static void do_serial_hangup(void *private)
491 struct mcf_serial *info = (struct mcf_serial *) private;
492 struct tty_struct *tty;
494 tty = info->tty;
495 if (!tty)
496 return;
498 tty_hangup(tty);
501 static int startup(struct mcf_serial * info)
503 volatile unsigned char *uartp;
504 unsigned long flags;
506 if (info->flags & ASYNC_INITIALIZED)
507 return 0;
509 if (!info->xmit_buf) {
510 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
511 if (!info->xmit_buf)
512 return -ENOMEM;
515 local_irq_save(flags);
517 #ifdef SERIAL_DEBUG_OPEN
518 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
519 #endif
522 * Reset UART, get it into known state...
524 uartp = info->addr;
525 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
526 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
527 mcfrs_setsignals(info, 1, 1);
529 if (info->tty)
530 clear_bit(TTY_IO_ERROR, &info->tty->flags);
531 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
534 * and set the speed of the serial port
536 mcfrs_change_speed(info);
539 * Lastly enable the UART transmitter and receiver, and
540 * interrupt enables.
542 info->imr = MCFUART_UIR_RXREADY;
543 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
544 uartp[MCFUART_UIMR] = info->imr;
546 info->flags |= ASYNC_INITIALIZED;
547 local_irq_restore(flags);
548 return 0;
552 * This routine will shutdown a serial port; interrupts are disabled, and
553 * DTR is dropped if the hangup on close termio flag is on.
555 static void shutdown(struct mcf_serial * info)
557 volatile unsigned char *uartp;
558 unsigned long flags;
560 if (!(info->flags & ASYNC_INITIALIZED))
561 return;
563 #ifdef SERIAL_DEBUG_OPEN
564 printk("Shutting down serial port %d (irq %d)....\n", info->line,
565 info->irq);
566 #endif
568 local_irq_save(flags);
570 uartp = info->addr;
571 uartp[MCFUART_UIMR] = 0; /* mask all interrupts */
572 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
573 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
575 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
576 mcfrs_setsignals(info, 0, 0);
578 if (info->xmit_buf) {
579 free_page((unsigned long) info->xmit_buf);
580 info->xmit_buf = 0;
583 if (info->tty)
584 set_bit(TTY_IO_ERROR, &info->tty->flags);
586 info->flags &= ~ASYNC_INITIALIZED;
587 local_irq_restore(flags);
592 * This routine is called to set the UART divisor registers to match
593 * the specified baud rate for a serial port.
595 static void mcfrs_change_speed(struct mcf_serial *info)
597 volatile unsigned char *uartp;
598 unsigned int baudclk, cflag;
599 unsigned long flags;
600 unsigned char mr1, mr2;
601 int i;
602 #ifdef CONFIG_M5272
603 unsigned int fraction;
604 #endif
606 if (!info->tty || !info->tty->termios)
607 return;
608 cflag = info->tty->termios->c_cflag;
609 if (info->addr == 0)
610 return;
612 #if 0
613 printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
614 #endif
616 i = cflag & CBAUD;
617 if (i & CBAUDEX) {
618 i &= ~CBAUDEX;
619 if (i < 1 || i > 4)
620 info->tty->termios->c_cflag &= ~CBAUDEX;
621 else
622 i += 15;
624 if (i == 0) {
625 mcfrs_setsignals(info, 0, -1);
626 return;
629 /* compute the baudrate clock */
630 #ifdef CONFIG_M5272
632 * For the MCF5272, also compute the baudrate fraction.
634 baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
635 fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
636 fraction *= 16;
637 fraction /= (32 * mcfrs_baud_table[i]);
638 #else
639 baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
640 #endif
642 info->baud = mcfrs_baud_table[i];
644 mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
645 mr2 = 0;
647 switch (cflag & CSIZE) {
648 case CS5: mr1 |= MCFUART_MR1_CS5; break;
649 case CS6: mr1 |= MCFUART_MR1_CS6; break;
650 case CS7: mr1 |= MCFUART_MR1_CS7; break;
651 case CS8:
652 default: mr1 |= MCFUART_MR1_CS8; break;
655 if (cflag & PARENB) {
656 if (cflag & CMSPAR) {
657 if (cflag & PARODD)
658 mr1 |= MCFUART_MR1_PARITYMARK;
659 else
660 mr1 |= MCFUART_MR1_PARITYSPACE;
661 } else {
662 if (cflag & PARODD)
663 mr1 |= MCFUART_MR1_PARITYODD;
664 else
665 mr1 |= MCFUART_MR1_PARITYEVEN;
667 } else {
668 mr1 |= MCFUART_MR1_PARITYNONE;
671 if (cflag & CSTOPB)
672 mr2 |= MCFUART_MR2_STOP2;
673 else
674 mr2 |= MCFUART_MR2_STOP1;
676 if (cflag & CRTSCTS) {
677 mr1 |= MCFUART_MR1_RXRTS;
678 mr2 |= MCFUART_MR2_TXCTS;
681 if (cflag & CLOCAL)
682 info->flags &= ~ASYNC_CHECK_CD;
683 else
684 info->flags |= ASYNC_CHECK_CD;
686 uartp = info->addr;
688 local_irq_save(flags);
689 #if 0
690 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
691 mr1, mr2, baudclk);
692 #endif
694 Note: pg 12-16 of MCF5206e User's Manual states that a
695 software reset should be performed prior to changing
696 UMR1,2, UCSR, UACR, bit 7
698 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
699 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
700 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
701 uartp[MCFUART_UMR] = mr1;
702 uartp[MCFUART_UMR] = mr2;
703 uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8; /* set msb byte */
704 uartp[MCFUART_UBG2] = (baudclk & 0xff); /* set lsb byte */
705 #ifdef CONFIG_M5272
706 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
707 #endif
708 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
709 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
710 mcfrs_setsignals(info, 1, -1);
711 local_irq_restore(flags);
712 return;
715 static void mcfrs_flush_chars(struct tty_struct *tty)
717 volatile unsigned char *uartp;
718 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
719 unsigned long flags;
721 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
722 return;
724 uartp = (volatile unsigned char *) info->addr;
727 * re-enable receiver interrupt
729 local_irq_save(flags);
730 if ((!(info->imr & MCFUART_UIR_RXREADY)) &&
731 (info->flags & ASYNC_INITIALIZED) ) {
732 info->imr |= MCFUART_UIR_RXREADY;
733 uartp[MCFUART_UIMR] = info->imr;
735 local_irq_restore(flags);
737 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
738 !info->xmit_buf)
739 return;
741 /* Enable transmitter */
742 local_irq_save(flags);
743 info->imr |= MCFUART_UIR_TXREADY;
744 uartp[MCFUART_UIMR] = info->imr;
745 local_irq_restore(flags);
748 static int mcfrs_write(struct tty_struct * tty,
749 const unsigned char *buf, int count)
751 volatile unsigned char *uartp;
752 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
753 unsigned long flags;
754 int c, total = 0;
756 #if 0
757 printk("%s(%d): mcfrs_write(tty=%x,buf=%x,count=%d)\n",
758 __FILE__, __LINE__, (int)tty, (int)buf, count);
759 #endif
761 if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
762 return 0;
764 if (!tty || !info->xmit_buf)
765 return 0;
767 local_save_flags(flags);
768 while (1) {
769 local_irq_disable();
770 c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
771 ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
772 local_irq_restore(flags);
774 if (c <= 0)
775 break;
777 memcpy(info->xmit_buf + info->xmit_head, buf, c);
779 local_irq_disable();
780 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
781 info->xmit_cnt += c;
782 local_irq_restore(flags);
784 buf += c;
785 count -= c;
786 total += c;
789 local_irq_disable();
790 uartp = info->addr;
791 info->imr |= MCFUART_UIR_TXREADY;
792 uartp[MCFUART_UIMR] = info->imr;
793 local_irq_restore(flags);
795 return total;
798 static int mcfrs_write_room(struct tty_struct *tty)
800 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
801 int ret;
803 if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
804 return 0;
805 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
806 if (ret < 0)
807 ret = 0;
808 return ret;
811 static int mcfrs_chars_in_buffer(struct tty_struct *tty)
813 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
815 if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
816 return 0;
817 return info->xmit_cnt;
820 static void mcfrs_flush_buffer(struct tty_struct *tty)
822 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
823 unsigned long flags;
825 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
826 return;
828 local_irq_save(flags);
829 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
830 local_irq_restore(flags);
832 tty_wakeup(tty);
836 * ------------------------------------------------------------
837 * mcfrs_throttle()
839 * This routine is called by the upper-layer tty layer to signal that
840 * incoming characters should be throttled.
841 * ------------------------------------------------------------
843 static void mcfrs_throttle(struct tty_struct * tty)
845 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
846 #ifdef SERIAL_DEBUG_THROTTLE
847 char buf[64];
849 printk("throttle %s: %d....\n", _tty_name(tty, buf),
850 tty->ldisc.chars_in_buffer(tty));
851 #endif
853 if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
854 return;
856 if (I_IXOFF(tty))
857 info->x_char = STOP_CHAR(tty);
859 /* Turn off RTS line (do this atomic) */
862 static void mcfrs_unthrottle(struct tty_struct * tty)
864 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
865 #ifdef SERIAL_DEBUG_THROTTLE
866 char buf[64];
868 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
869 tty->ldisc.chars_in_buffer(tty));
870 #endif
872 if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
873 return;
875 if (I_IXOFF(tty)) {
876 if (info->x_char)
877 info->x_char = 0;
878 else
879 info->x_char = START_CHAR(tty);
882 /* Assert RTS line (do this atomic) */
886 * ------------------------------------------------------------
887 * mcfrs_ioctl() and friends
888 * ------------------------------------------------------------
891 static int get_serial_info(struct mcf_serial * info,
892 struct serial_struct * retinfo)
894 struct serial_struct tmp;
896 if (!retinfo)
897 return -EFAULT;
898 memset(&tmp, 0, sizeof(tmp));
899 tmp.type = info->type;
900 tmp.line = info->line;
901 tmp.port = (unsigned int) info->addr;
902 tmp.irq = info->irq;
903 tmp.flags = info->flags;
904 tmp.baud_base = info->baud_base;
905 tmp.close_delay = info->close_delay;
906 tmp.closing_wait = info->closing_wait;
907 tmp.custom_divisor = info->custom_divisor;
908 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
911 static int set_serial_info(struct mcf_serial * info,
912 struct serial_struct * new_info)
914 struct serial_struct new_serial;
915 struct mcf_serial old_info;
916 int retval = 0;
918 if (!new_info)
919 return -EFAULT;
920 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
921 return -EFAULT;
922 old_info = *info;
924 if (!capable(CAP_SYS_ADMIN)) {
925 if ((new_serial.baud_base != info->baud_base) ||
926 (new_serial.type != info->type) ||
927 (new_serial.close_delay != info->close_delay) ||
928 ((new_serial.flags & ~ASYNC_USR_MASK) !=
929 (info->flags & ~ASYNC_USR_MASK)))
930 return -EPERM;
931 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
932 (new_serial.flags & ASYNC_USR_MASK));
933 info->custom_divisor = new_serial.custom_divisor;
934 goto check_and_exit;
937 if (info->count > 1)
938 return -EBUSY;
941 * OK, past this point, all the error checking has been done.
942 * At this point, we start making changes.....
945 info->baud_base = new_serial.baud_base;
946 info->flags = ((info->flags & ~ASYNC_FLAGS) |
947 (new_serial.flags & ASYNC_FLAGS));
948 info->type = new_serial.type;
949 info->close_delay = new_serial.close_delay;
950 info->closing_wait = new_serial.closing_wait;
952 check_and_exit:
953 retval = startup(info);
954 return retval;
958 * get_lsr_info - get line status register info
960 * Purpose: Let user call ioctl() to get info when the UART physically
961 * is emptied. On bus types like RS485, the transmitter must
962 * release the bus after transmitting. This must be done when
963 * the transmit shift register is empty, not be done when the
964 * transmit holding register is empty. This functionality
965 * allows an RS485 driver to be written in user space.
967 static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
969 volatile unsigned char *uartp;
970 unsigned long flags;
971 unsigned char status;
973 local_irq_save(flags);
974 uartp = info->addr;
975 status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
976 local_irq_restore(flags);
978 return put_user(status,value);
982 * This routine sends a break character out the serial port.
984 static void send_break( struct mcf_serial * info, int duration)
986 volatile unsigned char *uartp;
987 unsigned long flags;
989 if (!info->addr)
990 return;
991 set_current_state(TASK_INTERRUPTIBLE);
992 uartp = info->addr;
994 local_irq_save(flags);
995 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
996 schedule_timeout(duration);
997 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
998 local_irq_restore(flags);
1001 static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
1003 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1005 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1006 return -ENODEV;
1007 if (tty->flags & (1 << TTY_IO_ERROR))
1008 return -EIO;
1010 return mcfrs_getsignals(info);
1013 static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
1014 unsigned int set, unsigned int clear)
1016 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1017 int rts = -1, dtr = -1;
1019 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1020 return -ENODEV;
1021 if (tty->flags & (1 << TTY_IO_ERROR))
1022 return -EIO;
1024 if (set & TIOCM_RTS)
1025 rts = 1;
1026 if (set & TIOCM_DTR)
1027 dtr = 1;
1028 if (clear & TIOCM_RTS)
1029 rts = 0;
1030 if (clear & TIOCM_DTR)
1031 dtr = 0;
1033 mcfrs_setsignals(info, dtr, rts);
1035 return 0;
1038 static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
1039 unsigned int cmd, unsigned long arg)
1041 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1042 int retval, error;
1044 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1045 return -ENODEV;
1047 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1048 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1049 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1050 if (tty->flags & (1 << TTY_IO_ERROR))
1051 return -EIO;
1054 switch (cmd) {
1055 case TCSBRK: /* SVID version: non-zero arg --> no break */
1056 retval = tty_check_change(tty);
1057 if (retval)
1058 return retval;
1059 tty_wait_until_sent(tty, 0);
1060 if (!arg)
1061 send_break(info, HZ/4); /* 1/4 second */
1062 return 0;
1063 case TCSBRKP: /* support for POSIX tcsendbreak() */
1064 retval = tty_check_change(tty);
1065 if (retval)
1066 return retval;
1067 tty_wait_until_sent(tty, 0);
1068 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1069 return 0;
1070 case TIOCGSOFTCAR:
1071 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1072 (unsigned long *) arg);
1073 if (error)
1074 return error;
1075 return 0;
1076 case TIOCSSOFTCAR:
1077 get_user(arg, (unsigned long *) arg);
1078 tty->termios->c_cflag =
1079 ((tty->termios->c_cflag & ~CLOCAL) |
1080 (arg ? CLOCAL : 0));
1081 return 0;
1082 case TIOCGSERIAL:
1083 if (access_ok(VERIFY_WRITE, (void *) arg,
1084 sizeof(struct serial_struct)))
1085 return get_serial_info(info,
1086 (struct serial_struct *) arg);
1087 return -EFAULT;
1088 case TIOCSSERIAL:
1089 return set_serial_info(info,
1090 (struct serial_struct *) arg);
1091 case TIOCSERGETLSR: /* Get line status register */
1092 if (access_ok(VERIFY_WRITE, (void *) arg,
1093 sizeof(unsigned int)))
1094 return get_lsr_info(info, (unsigned int *) arg);
1095 return -EFAULT;
1096 case TIOCSERGSTRUCT:
1097 error = copy_to_user((struct mcf_serial *) arg,
1098 info, sizeof(struct mcf_serial));
1099 if (error)
1100 return -EFAULT;
1101 return 0;
1103 #ifdef TIOCSET422
1104 case TIOCSET422: {
1105 unsigned int val;
1106 get_user(val, (unsigned int *) arg);
1107 mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1108 break;
1110 case TIOCGET422: {
1111 unsigned int val;
1112 val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1113 put_user(val, (unsigned int *) arg);
1114 break;
1116 #endif
1118 default:
1119 return -ENOIOCTLCMD;
1121 return 0;
1124 static void mcfrs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1126 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1128 if (tty->termios->c_cflag == old_termios->c_cflag)
1129 return;
1131 mcfrs_change_speed(info);
1133 if ((old_termios->c_cflag & CRTSCTS) &&
1134 !(tty->termios->c_cflag & CRTSCTS)) {
1135 tty->hw_stopped = 0;
1136 mcfrs_setsignals(info, -1, 1);
1137 #if 0
1138 mcfrs_start(tty);
1139 #endif
1144 * ------------------------------------------------------------
1145 * mcfrs_close()
1147 * This routine is called when the serial port gets closed. First, we
1148 * wait for the last remaining data to be sent. Then, we unlink its
1149 * S structure from the interrupt chain if necessary, and we free
1150 * that IRQ if nothing is left in the chain.
1151 * ------------------------------------------------------------
1153 static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1155 volatile unsigned char *uartp;
1156 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1157 unsigned long flags;
1159 if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1160 return;
1162 local_irq_save(flags);
1164 if (tty_hung_up_p(filp)) {
1165 local_irq_restore(flags);
1166 return;
1169 #ifdef SERIAL_DEBUG_OPEN
1170 printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1171 #endif
1172 if ((tty->count == 1) && (info->count != 1)) {
1174 * Uh, oh. tty->count is 1, which means that the tty
1175 * structure will be freed. Info->count should always
1176 * be one in these conditions. If it's greater than
1177 * one, we've got real problems, since it means the
1178 * serial port won't be shutdown.
1180 printk("MCFRS: bad serial port count; tty->count is 1, "
1181 "info->count is %d\n", info->count);
1182 info->count = 1;
1184 if (--info->count < 0) {
1185 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1186 info->line, info->count);
1187 info->count = 0;
1189 if (info->count) {
1190 local_irq_restore(flags);
1191 return;
1193 info->flags |= ASYNC_CLOSING;
1196 * Now we wait for the transmit buffer to clear; and we notify
1197 * the line discipline to only process XON/XOFF characters.
1199 tty->closing = 1;
1200 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1201 tty_wait_until_sent(tty, info->closing_wait);
1204 * At this point we stop accepting input. To do this, we
1205 * disable the receive line status interrupts, and tell the
1206 * interrupt driver to stop checking the data ready bit in the
1207 * line status register.
1209 info->imr &= ~MCFUART_UIR_RXREADY;
1210 uartp = info->addr;
1211 uartp[MCFUART_UIMR] = info->imr;
1213 #if 0
1214 /* FIXME: do we need to keep this enabled for console?? */
1215 if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
1216 /* Do not disable the UART */ ;
1217 } else
1218 #endif
1219 shutdown(info);
1220 if (tty->driver->flush_buffer)
1221 tty->driver->flush_buffer(tty);
1222 tty_ldisc_flush(tty);
1224 tty->closing = 0;
1225 info->event = 0;
1226 info->tty = 0;
1227 #if 0
1228 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1229 if (tty->ldisc.close)
1230 (tty->ldisc.close)(tty);
1231 tty->ldisc = ldiscs[N_TTY];
1232 tty->termios->c_line = N_TTY;
1233 if (tty->ldisc.open)
1234 (tty->ldisc.open)(tty);
1236 #endif
1237 if (info->blocked_open) {
1238 if (info->close_delay) {
1239 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1241 wake_up_interruptible(&info->open_wait);
1243 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1244 wake_up_interruptible(&info->close_wait);
1245 local_irq_restore(flags);
1249 * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1251 static void
1252 mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1254 #ifdef CONFIG_M5272
1255 #define MCF5272_FIFO_SIZE 25 /* fifo size + shift reg */
1257 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1258 volatile unsigned char *uartp;
1259 unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1261 if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1262 return;
1264 orig_jiffies = jiffies;
1267 * Set the check interval to be 1/5 of the approximate time
1268 * to send the entire fifo, and make it at least 1. The check
1269 * interval should also be less than the timeout.
1271 * Note: we have to use pretty tight timings here to satisfy
1272 * the NIST-PCTS.
1274 fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1275 char_time = fifo_time / 5;
1276 if (char_time == 0)
1277 char_time = 1;
1278 if (timeout && timeout < char_time)
1279 char_time = timeout;
1282 * Clamp the timeout period at 2 * the time to empty the
1283 * fifo. Just to be safe, set the minimum at .5 seconds.
1285 fifo_time *= 2;
1286 if (fifo_time < (HZ/2))
1287 fifo_time = HZ/2;
1288 if (!timeout || timeout > fifo_time)
1289 timeout = fifo_time;
1292 * Account for the number of bytes in the UART
1293 * transmitter FIFO plus any byte being shifted out.
1295 uartp = (volatile unsigned char *) info->addr;
1296 for (;;) {
1297 fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1298 if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1299 MCFUART_USR_TXEMPTY)) ==
1300 MCFUART_USR_TXREADY)
1301 fifo_cnt++;
1302 if (fifo_cnt == 0)
1303 break;
1304 msleep_interruptible(jiffies_to_msecs(char_time));
1305 if (signal_pending(current))
1306 break;
1307 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1308 break;
1310 #else
1312 * For the other coldfire models, assume all data has been sent
1314 #endif
1318 * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1320 void mcfrs_hangup(struct tty_struct *tty)
1322 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1324 if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
1325 return;
1327 mcfrs_flush_buffer(tty);
1328 shutdown(info);
1329 info->event = 0;
1330 info->count = 0;
1331 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1332 info->tty = 0;
1333 wake_up_interruptible(&info->open_wait);
1337 * ------------------------------------------------------------
1338 * mcfrs_open() and friends
1339 * ------------------------------------------------------------
1341 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1342 struct mcf_serial *info)
1344 DECLARE_WAITQUEUE(wait, current);
1345 int retval;
1346 int do_clocal = 0;
1349 * If the device is in the middle of being closed, then block
1350 * until it's done, and then try again.
1352 if (info->flags & ASYNC_CLOSING) {
1353 interruptible_sleep_on(&info->close_wait);
1354 #ifdef SERIAL_DO_RESTART
1355 if (info->flags & ASYNC_HUP_NOTIFY)
1356 return -EAGAIN;
1357 else
1358 return -ERESTARTSYS;
1359 #else
1360 return -EAGAIN;
1361 #endif
1365 * If non-blocking mode is set, or the port is not enabled,
1366 * then make the check up front and then exit.
1368 if ((filp->f_flags & O_NONBLOCK) ||
1369 (tty->flags & (1 << TTY_IO_ERROR))) {
1370 info->flags |= ASYNC_NORMAL_ACTIVE;
1371 return 0;
1374 if (tty->termios->c_cflag & CLOCAL)
1375 do_clocal = 1;
1378 * Block waiting for the carrier detect and the line to become
1379 * free (i.e., not in use by the callout). While we are in
1380 * this loop, info->count is dropped by one, so that
1381 * mcfrs_close() knows when to free things. We restore it upon
1382 * exit, either normal or abnormal.
1384 retval = 0;
1385 add_wait_queue(&info->open_wait, &wait);
1386 #ifdef SERIAL_DEBUG_OPEN
1387 printk("block_til_ready before block: ttyS%d, count = %d\n",
1388 info->line, info->count);
1389 #endif
1390 info->count--;
1391 info->blocked_open++;
1392 while (1) {
1393 local_irq_disable();
1394 mcfrs_setsignals(info, 1, 1);
1395 local_irq_enable();
1396 current->state = TASK_INTERRUPTIBLE;
1397 if (tty_hung_up_p(filp) ||
1398 !(info->flags & ASYNC_INITIALIZED)) {
1399 #ifdef SERIAL_DO_RESTART
1400 if (info->flags & ASYNC_HUP_NOTIFY)
1401 retval = -EAGAIN;
1402 else
1403 retval = -ERESTARTSYS;
1404 #else
1405 retval = -EAGAIN;
1406 #endif
1407 break;
1409 if (!(info->flags & ASYNC_CLOSING) &&
1410 (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1411 break;
1412 if (signal_pending(current)) {
1413 retval = -ERESTARTSYS;
1414 break;
1416 #ifdef SERIAL_DEBUG_OPEN
1417 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1418 info->line, info->count);
1419 #endif
1420 schedule();
1422 current->state = TASK_RUNNING;
1423 remove_wait_queue(&info->open_wait, &wait);
1424 if (!tty_hung_up_p(filp))
1425 info->count++;
1426 info->blocked_open--;
1427 #ifdef SERIAL_DEBUG_OPEN
1428 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1429 info->line, info->count);
1430 #endif
1431 if (retval)
1432 return retval;
1433 info->flags |= ASYNC_NORMAL_ACTIVE;
1434 return 0;
1438 * This routine is called whenever a serial port is opened. It
1439 * enables interrupts for a serial port, linking in its structure into
1440 * the IRQ chain. It also performs the serial-specific
1441 * initialization for the tty structure.
1443 int mcfrs_open(struct tty_struct *tty, struct file * filp)
1445 struct mcf_serial *info;
1446 int retval, line;
1448 line = tty->index;
1449 if ((line < 0) || (line >= NR_PORTS))
1450 return -ENODEV;
1451 info = mcfrs_table + line;
1452 if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1453 return -ENODEV;
1454 #ifdef SERIAL_DEBUG_OPEN
1455 printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1456 #endif
1457 info->count++;
1458 tty->driver_data = info;
1459 info->tty = tty;
1462 * Start up serial port
1464 retval = startup(info);
1465 if (retval)
1466 return retval;
1468 retval = block_til_ready(tty, filp, info);
1469 if (retval) {
1470 #ifdef SERIAL_DEBUG_OPEN
1471 printk("mcfrs_open returning after block_til_ready with %d\n",
1472 retval);
1473 #endif
1474 return retval;
1477 #ifdef SERIAL_DEBUG_OPEN
1478 printk("mcfrs_open %s successful...\n", tty->name);
1479 #endif
1480 return 0;
1484 * Based on the line number set up the internal interrupt stuff.
1486 static void mcfrs_irqinit(struct mcf_serial *info)
1488 #if defined(CONFIG_M5272)
1489 volatile unsigned long *icrp;
1490 volatile unsigned long *portp;
1491 volatile unsigned char *uartp;
1493 uartp = info->addr;
1494 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1496 switch (info->line) {
1497 case 0:
1498 *icrp = 0xe0000000;
1499 break;
1500 case 1:
1501 *icrp = 0x0e000000;
1502 break;
1503 default:
1504 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1505 info->line);
1506 return;
1509 /* Enable the output lines for the serial ports */
1510 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
1511 *portp = (*portp & ~0x000000ff) | 0x00000055;
1512 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
1513 *portp = (*portp & ~0x000003fc) | 0x000002a8;
1514 #elif defined(CONFIG_M527x) || defined(CONFIG_M528x)
1515 volatile unsigned char *icrp, *uartp;
1516 volatile unsigned long *imrp;
1518 uartp = info->addr;
1520 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1521 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1522 *icrp = 0x33; /* UART0 with level 6, priority 3 */
1524 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1525 MCFINTC_IMRL);
1526 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1527 #else
1528 volatile unsigned char *icrp, *uartp;
1530 switch (info->line) {
1531 case 0:
1532 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1533 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1534 MCFSIM_ICR_PRI1;
1535 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1536 break;
1537 case 1:
1538 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1539 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1540 MCFSIM_ICR_PRI2;
1541 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1542 break;
1543 default:
1544 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1545 info->line);
1546 return;
1549 uartp = info->addr;
1550 uartp[MCFUART_UIVR] = info->irq;
1551 #endif
1553 /* Clear mask, so no surprise interrupts. */
1554 uartp[MCFUART_UIMR] = 0;
1556 if (request_irq(info->irq, mcfrs_interrupt, SA_INTERRUPT,
1557 "ColdFire UART", NULL)) {
1558 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1559 "vector=%d\n", info->line, info->irq);
1562 return;
1566 char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1570 * Serial stats reporting...
1572 int mcfrs_readproc(char *page, char **start, off_t off, int count,
1573 int *eof, void *data)
1575 struct mcf_serial *info;
1576 char str[20];
1577 int len, sigs, i;
1579 len = sprintf(page, mcfrs_drivername);
1580 for (i = 0; (i < NR_PORTS); i++) {
1581 info = &mcfrs_table[i];
1582 len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1583 i, (unsigned int) info->addr, info->irq, info->baud);
1584 if (info->stats.rx || info->stats.tx)
1585 len += sprintf((page + len), "tx:%d rx:%d ",
1586 info->stats.tx, info->stats.rx);
1587 if (info->stats.rxframing)
1588 len += sprintf((page + len), "fe:%d ",
1589 info->stats.rxframing);
1590 if (info->stats.rxparity)
1591 len += sprintf((page + len), "pe:%d ",
1592 info->stats.rxparity);
1593 if (info->stats.rxbreak)
1594 len += sprintf((page + len), "brk:%d ",
1595 info->stats.rxbreak);
1596 if (info->stats.rxoverrun)
1597 len += sprintf((page + len), "oe:%d ",
1598 info->stats.rxoverrun);
1600 str[0] = str[1] = 0;
1601 if ((sigs = mcfrs_getsignals(info))) {
1602 if (sigs & TIOCM_RTS)
1603 strcat(str, "|RTS");
1604 if (sigs & TIOCM_CTS)
1605 strcat(str, "|CTS");
1606 if (sigs & TIOCM_DTR)
1607 strcat(str, "|DTR");
1608 if (sigs & TIOCM_CD)
1609 strcat(str, "|CD");
1612 len += sprintf((page + len), "%s\n", &str[1]);
1615 return(len);
1619 /* Finally, routines used to initialize the serial driver. */
1621 static void show_serial_version(void)
1623 printk(mcfrs_drivername);
1626 static struct tty_operations mcfrs_ops = {
1627 .open = mcfrs_open,
1628 .close = mcfrs_close,
1629 .write = mcfrs_write,
1630 .flush_chars = mcfrs_flush_chars,
1631 .write_room = mcfrs_write_room,
1632 .chars_in_buffer = mcfrs_chars_in_buffer,
1633 .flush_buffer = mcfrs_flush_buffer,
1634 .ioctl = mcfrs_ioctl,
1635 .throttle = mcfrs_throttle,
1636 .unthrottle = mcfrs_unthrottle,
1637 .set_termios = mcfrs_set_termios,
1638 .stop = mcfrs_stop,
1639 .start = mcfrs_start,
1640 .hangup = mcfrs_hangup,
1641 .read_proc = mcfrs_readproc,
1642 .wait_until_sent = mcfrs_wait_until_sent,
1643 .tiocmget = mcfrs_tiocmget,
1644 .tiocmset = mcfrs_tiocmset,
1647 /* mcfrs_init inits the driver */
1648 static int __init
1649 mcfrs_init(void)
1651 struct mcf_serial *info;
1652 unsigned long flags;
1653 int i;
1655 /* Setup base handler, and timer table. */
1656 #ifdef MCFPP_DCD0
1657 init_timer(&mcfrs_timer_struct);
1658 mcfrs_timer_struct.function = mcfrs_timer;
1659 mcfrs_timer_struct.data = 0;
1660 mcfrs_timer_struct.expires = jiffies + HZ/25;
1661 add_timer(&mcfrs_timer_struct);
1662 mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1663 #endif
1664 mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1665 if (!mcfrs_serial_driver)
1666 return -ENOMEM;
1668 show_serial_version();
1670 /* Initialize the tty_driver structure */
1671 mcfrs_serial_driver->owner = THIS_MODULE;
1672 mcfrs_serial_driver->name = "ttyS";
1673 mcfrs_serial_driver->devfs_name = "ttys/";
1674 mcfrs_serial_driver->driver_name = "serial";
1675 mcfrs_serial_driver->major = TTY_MAJOR;
1676 mcfrs_serial_driver->minor_start = 64;
1677 mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1678 mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1679 mcfrs_serial_driver->init_termios = tty_std_termios;
1681 mcfrs_serial_driver->init_termios.c_cflag =
1682 mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1683 mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1685 tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1687 if (tty_register_driver(mcfrs_serial_driver)) {
1688 printk("MCFRS: Couldn't register serial driver\n");
1689 put_tty_driver(mcfrs_serial_driver);
1690 return(-EBUSY);
1693 local_irq_save(flags);
1696 * Configure all the attached serial ports.
1698 for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1699 info->magic = SERIAL_MAGIC;
1700 info->line = i;
1701 info->tty = 0;
1702 info->custom_divisor = 16;
1703 info->close_delay = 50;
1704 info->closing_wait = 3000;
1705 info->x_char = 0;
1706 info->event = 0;
1707 info->count = 0;
1708 info->blocked_open = 0;
1709 INIT_WORK(&info->tqueue, mcfrs_offintr, info);
1710 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1711 init_waitqueue_head(&info->open_wait);
1712 init_waitqueue_head(&info->close_wait);
1714 info->imr = 0;
1715 mcfrs_setsignals(info, 0, 0);
1716 mcfrs_irqinit(info);
1718 printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1719 (unsigned int) info->addr, info->irq);
1720 printk(" is a builtin ColdFire UART\n");
1723 local_irq_restore(flags);
1724 return 0;
1727 module_init(mcfrs_init);
1729 /****************************************************************************/
1730 /* Serial Console */
1731 /****************************************************************************/
1734 * Quick and dirty UART initialization, for console output.
1737 void mcfrs_init_console(void)
1739 volatile unsigned char *uartp;
1740 unsigned int clk;
1743 * Reset UART, get it into known state...
1745 uartp = (volatile unsigned char *) (MCF_MBAR +
1746 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1748 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
1749 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
1750 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
1753 * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1755 uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1756 uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1758 clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1759 uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8; /* set msb baud */
1760 uartp[MCFUART_UBG2] = (clk & 0xff); /* set lsb baud */
1762 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1763 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1765 mcfrs_console_inited++;
1766 return;
1771 * Setup for console. Argument comes from the boot command line.
1774 int mcfrs_console_setup(struct console *cp, char *arg)
1776 int i, n = CONSOLE_BAUD_RATE;
1778 if (!cp)
1779 return(-1);
1781 if (!strncmp(cp->name, "ttyS", 4))
1782 mcfrs_console_port = cp->index;
1783 else if (!strncmp(cp->name, "cua", 3))
1784 mcfrs_console_port = cp->index;
1785 else
1786 return(-1);
1788 if (arg)
1789 n = simple_strtoul(arg,NULL,0);
1790 for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1791 if (mcfrs_baud_table[i] == n)
1792 break;
1793 if (i < MCFRS_BAUD_TABLE_SIZE) {
1794 mcfrs_console_baud = n;
1795 mcfrs_console_cbaud = 0;
1796 if (i > 15) {
1797 mcfrs_console_cbaud |= CBAUDEX;
1798 i -= 15;
1800 mcfrs_console_cbaud |= i;
1802 mcfrs_init_console(); /* make sure baud rate changes */
1803 return(0);
1807 static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1809 *index = c->index;
1810 return mcfrs_serial_driver;
1815 * Output a single character, using UART polled mode.
1816 * This is used for console output.
1819 void mcfrs_put_char(char ch)
1821 volatile unsigned char *uartp;
1822 unsigned long flags;
1823 int i;
1825 uartp = (volatile unsigned char *) (MCF_MBAR +
1826 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1828 local_irq_save(flags);
1829 for (i = 0; (i < 0x10000); i++) {
1830 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1831 break;
1833 if (i < 0x10000) {
1834 uartp[MCFUART_UTB] = ch;
1835 for (i = 0; (i < 0x10000); i++)
1836 if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1837 break;
1839 if (i >= 0x10000)
1840 mcfrs_init_console(); /* try and get it back */
1841 local_irq_restore(flags);
1843 return;
1848 * rs_console_write is registered for printk output.
1851 void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1853 if (!mcfrs_console_inited)
1854 mcfrs_init_console();
1855 while (len-- > 0) {
1856 if (*p == '\n')
1857 mcfrs_put_char('\r');
1858 mcfrs_put_char(*p++);
1863 * declare our consoles
1866 struct console mcfrs_console = {
1867 .name = "ttyS",
1868 .write = mcfrs_console_write,
1869 .device = mcfrs_console_device,
1870 .setup = mcfrs_console_setup,
1871 .flags = CON_PRINTBUFFER,
1872 .index = -1,
1875 static int __init mcfrs_console_init(void)
1877 register_console(&mcfrs_console);
1878 return 0;
1881 console_initcall(mcfrs_console_init);
1883 /****************************************************************************/