[PATCH] W1: w1_netlink: New init/fini netlink callbacks.
[linux-2.6/verdex.git] / drivers / serial / mcfserial.c
blob43b03c55f453859a2902c7dc50d72f741b508e20
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/semaphore.h>
44 #include <asm/delay.h>
45 #include <asm/coldfire.h>
46 #include <asm/mcfsim.h>
47 #include <asm/mcfuart.h>
48 #include <asm/nettel.h>
49 #include <asm/uaccess.h>
50 #include "mcfserial.h"
52 struct timer_list mcfrs_timer_struct;
55 * Default console baud rate, we use this as the default
56 * for all ports so init can just open /dev/console and
57 * keep going. Perhaps one day the cflag settings for the
58 * console can be used instead.
60 #if defined(CONFIG_ARNEWSH) || defined(CONFIG_MOTOROLA) || defined(CONFIG_senTec) || defined(CONFIG_SNEHA)
61 #define CONSOLE_BAUD_RATE 19200
62 #define DEFAULT_CBAUD B19200
63 #endif
65 #if defined(CONFIG_HW_FEITH)
66 #define CONSOLE_BAUD_RATE 38400
67 #define DEFAULT_CBAUD B38400
68 #endif
70 #ifndef CONSOLE_BAUD_RATE
71 #define CONSOLE_BAUD_RATE 9600
72 #define DEFAULT_CBAUD B9600
73 #endif
75 int mcfrs_console_inited = 0;
76 int mcfrs_console_port = -1;
77 int mcfrs_console_baud = CONSOLE_BAUD_RATE;
78 int mcfrs_console_cbaud = DEFAULT_CBAUD;
81 * Driver data structures.
83 static struct tty_driver *mcfrs_serial_driver;
85 /* number of characters left in xmit buffer before we ask for more */
86 #define WAKEUP_CHARS 256
88 /* Debugging...
90 #undef SERIAL_DEBUG_OPEN
91 #undef SERIAL_DEBUG_FLOW
93 #if defined(CONFIG_M527x) || defined(CONFIG_M528x)
94 #define IRQBASE (MCFINT_VECBASE+MCFINT_UART0)
95 #else
96 #define IRQBASE 73
97 #endif
100 * Configuration table, UARTs to look for at startup.
102 static struct mcf_serial mcfrs_table[] = {
103 { /* ttyS0 */
104 .magic = 0,
105 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE1),
106 .irq = IRQBASE,
107 .flags = ASYNC_BOOT_AUTOCONF,
109 { /* ttyS1 */
110 .magic = 0,
111 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE2),
112 .irq = IRQBASE+1,
113 .flags = ASYNC_BOOT_AUTOCONF,
118 #define NR_PORTS (sizeof(mcfrs_table) / sizeof(struct mcf_serial))
121 * This is used to figure out the divisor speeds and the timeouts.
123 static int mcfrs_baud_table[] = {
124 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
125 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0
127 #define MCFRS_BAUD_TABLE_SIZE \
128 (sizeof(mcfrs_baud_table)/sizeof(mcfrs_baud_table[0]))
131 #ifdef CONFIG_MAGIC_SYSRQ
133 * Magic system request keys. Used for debugging...
135 extern int magic_sysrq_key(int ch);
136 #endif
140 * Forware declarations...
142 static void mcfrs_change_speed(struct mcf_serial *info);
143 static void mcfrs_wait_until_sent(struct tty_struct *tty, int timeout);
146 static inline int serial_paranoia_check(struct mcf_serial *info,
147 char *name, const char *routine)
149 #ifdef SERIAL_PARANOIA_CHECK
150 static const char badmagic[] =
151 "MCFRS(warning): bad magic number for serial struct %s in %s\n";
152 static const char badinfo[] =
153 "MCFRS(warning): null mcf_serial for %s in %s\n";
155 if (!info) {
156 printk(badinfo, name, routine);
157 return 1;
159 if (info->magic != SERIAL_MAGIC) {
160 printk(badmagic, name, routine);
161 return 1;
163 #endif
164 return 0;
168 * Sets or clears DTR and RTS on the requested line.
170 static void mcfrs_setsignals(struct mcf_serial *info, int dtr, int rts)
172 volatile unsigned char *uartp;
173 unsigned long flags;
175 #if 0
176 printk("%s(%d): mcfrs_setsignals(info=%x,dtr=%d,rts=%d)\n",
177 __FILE__, __LINE__, info, dtr, rts);
178 #endif
180 local_irq_save(flags);
181 if (dtr >= 0) {
182 #ifdef MCFPP_DTR0
183 if (info->line)
184 mcf_setppdata(MCFPP_DTR1, (dtr ? 0 : MCFPP_DTR1));
185 else
186 mcf_setppdata(MCFPP_DTR0, (dtr ? 0 : MCFPP_DTR0));
187 #endif
189 if (rts >= 0) {
190 uartp = info->addr;
191 if (rts) {
192 info->sigs |= TIOCM_RTS;
193 uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
194 } else {
195 info->sigs &= ~TIOCM_RTS;
196 uartp[MCFUART_UOP0] = MCFUART_UOP_RTS;
199 local_irq_restore(flags);
200 return;
204 * Gets values of serial signals.
206 static int mcfrs_getsignals(struct mcf_serial *info)
208 volatile unsigned char *uartp;
209 unsigned long flags;
210 int sigs;
211 #if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
212 unsigned short ppdata;
213 #endif
215 #if 0
216 printk("%s(%d): mcfrs_getsignals(info=%x)\n", __FILE__, __LINE__);
217 #endif
219 local_irq_save(flags);
220 uartp = info->addr;
221 sigs = (uartp[MCFUART_UIPR] & MCFUART_UIPR_CTS) ? 0 : TIOCM_CTS;
222 sigs |= (info->sigs & TIOCM_RTS);
224 #ifdef MCFPP_DCD0
226 unsigned int ppdata;
227 ppdata = mcf_getppdata();
228 if (info->line == 0) {
229 sigs |= (ppdata & MCFPP_DCD0) ? 0 : TIOCM_CD;
230 sigs |= (ppdata & MCFPP_DTR0) ? 0 : TIOCM_DTR;
231 } else if (info->line == 1) {
232 sigs |= (ppdata & MCFPP_DCD1) ? 0 : TIOCM_CD;
233 sigs |= (ppdata & MCFPP_DTR1) ? 0 : TIOCM_DTR;
236 #endif
238 local_irq_restore(flags);
239 return(sigs);
243 * ------------------------------------------------------------
244 * mcfrs_stop() and mcfrs_start()
246 * This routines are called before setting or resetting tty->stopped.
247 * They enable or disable transmitter interrupts, as necessary.
248 * ------------------------------------------------------------
250 static void mcfrs_stop(struct tty_struct *tty)
252 volatile unsigned char *uartp;
253 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
254 unsigned long flags;
256 if (serial_paranoia_check(info, tty->name, "mcfrs_stop"))
257 return;
259 local_irq_save(flags);
260 uartp = info->addr;
261 info->imr &= ~MCFUART_UIR_TXREADY;
262 uartp[MCFUART_UIMR] = info->imr;
263 local_irq_restore(flags);
266 static void mcfrs_start(struct tty_struct *tty)
268 volatile unsigned char *uartp;
269 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
270 unsigned long flags;
272 if (serial_paranoia_check(info, tty->name, "mcfrs_start"))
273 return;
275 local_irq_save(flags);
276 if (info->xmit_cnt && info->xmit_buf) {
277 uartp = info->addr;
278 info->imr |= MCFUART_UIR_TXREADY;
279 uartp[MCFUART_UIMR] = info->imr;
281 local_irq_restore(flags);
285 * ----------------------------------------------------------------------
287 * Here starts the interrupt handling routines. All of the following
288 * subroutines are declared as inline and are folded into
289 * mcfrs_interrupt(). They were separated out for readability's sake.
291 * Note: mcfrs_interrupt() is a "fast" interrupt, which means that it
292 * runs with interrupts turned off. People who may want to modify
293 * mcfrs_interrupt() should try to keep the interrupt handler as fast as
294 * possible. After you are done making modifications, it is not a bad
295 * idea to do:
297 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
299 * and look at the resulting assemble code in serial.s.
301 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
302 * -----------------------------------------------------------------------
305 static inline void receive_chars(struct mcf_serial *info)
307 volatile unsigned char *uartp;
308 struct tty_struct *tty = info->tty;
309 unsigned char status, ch;
311 if (!tty)
312 return;
314 uartp = info->addr;
316 while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
318 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
319 break;
321 ch = uartp[MCFUART_URB];
322 info->stats.rx++;
324 #ifdef CONFIG_MAGIC_SYSRQ
325 if (mcfrs_console_inited && (info->line == mcfrs_console_port)) {
326 if (magic_sysrq_key(ch))
327 continue;
329 #endif
331 tty->flip.count++;
332 if (status & MCFUART_USR_RXERR) {
333 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
334 if (status & MCFUART_USR_RXBREAK) {
335 info->stats.rxbreak++;
336 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
337 } else if (status & MCFUART_USR_RXPARITY) {
338 info->stats.rxparity++;
339 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
340 } else if (status & MCFUART_USR_RXOVERRUN) {
341 info->stats.rxoverrun++;
342 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
343 } else if (status & MCFUART_USR_RXFRAMING) {
344 info->stats.rxframing++;
345 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
346 } else {
347 /* This should never happen... */
348 *tty->flip.flag_buf_ptr++ = 0;
350 } else {
351 *tty->flip.flag_buf_ptr++ = 0;
353 *tty->flip.char_buf_ptr++ = ch;
356 schedule_work(&tty->flip.work);
357 return;
360 static inline void transmit_chars(struct mcf_serial *info)
362 volatile unsigned char *uartp;
364 uartp = info->addr;
366 if (info->x_char) {
367 /* Send special char - probably flow control */
368 uartp[MCFUART_UTB] = info->x_char;
369 info->x_char = 0;
370 info->stats.tx++;
373 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
374 info->imr &= ~MCFUART_UIR_TXREADY;
375 uartp[MCFUART_UIMR] = info->imr;
376 return;
379 while (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) {
380 uartp[MCFUART_UTB] = info->xmit_buf[info->xmit_tail++];
381 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
382 info->stats.tx++;
383 if (--info->xmit_cnt <= 0)
384 break;
387 if (info->xmit_cnt < WAKEUP_CHARS)
388 schedule_work(&info->tqueue);
389 return;
393 * This is the serial driver's generic interrupt routine
395 irqreturn_t mcfrs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
397 struct mcf_serial *info;
398 unsigned char isr;
400 info = &mcfrs_table[(irq - IRQBASE)];
401 isr = info->addr[MCFUART_UISR] & info->imr;
403 if (isr & MCFUART_UIR_RXREADY)
404 receive_chars(info);
405 if (isr & MCFUART_UIR_TXREADY)
406 transmit_chars(info);
407 return IRQ_HANDLED;
411 * -------------------------------------------------------------------
412 * Here ends the serial interrupt routines.
413 * -------------------------------------------------------------------
416 static void mcfrs_offintr(void *private)
418 struct mcf_serial *info = (struct mcf_serial *) private;
419 struct tty_struct *tty;
421 tty = info->tty;
422 if (!tty)
423 return;
424 tty_wakeup(tty);
429 * Change of state on a DCD line.
431 void mcfrs_modem_change(struct mcf_serial *info, int dcd)
433 if (info->count == 0)
434 return;
436 if (info->flags & ASYNC_CHECK_CD) {
437 if (dcd)
438 wake_up_interruptible(&info->open_wait);
439 else
440 schedule_work(&info->tqueue_hangup);
445 #ifdef MCFPP_DCD0
447 unsigned short mcfrs_ppstatus;
450 * This subroutine is called when the RS_TIMER goes off. It is used
451 * to monitor the state of the DCD lines - since they have no edge
452 * sensors and interrupt generators.
454 static void mcfrs_timer(void)
456 unsigned int ppstatus, dcdval, i;
458 ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
460 if (ppstatus != mcfrs_ppstatus) {
461 for (i = 0; (i < 2); i++) {
462 dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
463 if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
464 mcfrs_modem_change(&mcfrs_table[i],
465 ((ppstatus & dcdval) ? 0 : 1));
469 mcfrs_ppstatus = ppstatus;
471 /* Re-arm timer */
472 mcfrs_timer_struct.expires = jiffies + HZ/25;
473 add_timer(&mcfrs_timer_struct);
476 #endif /* MCFPP_DCD0 */
480 * This routine is called from the scheduler tqueue when the interrupt
481 * routine has signalled that a hangup has occurred. The path of
482 * hangup processing is:
484 * serial interrupt routine -> (scheduler tqueue) ->
485 * do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
488 static void do_serial_hangup(void *private)
490 struct mcf_serial *info = (struct mcf_serial *) private;
491 struct tty_struct *tty;
493 tty = info->tty;
494 if (!tty)
495 return;
497 tty_hangup(tty);
500 static int startup(struct mcf_serial * info)
502 volatile unsigned char *uartp;
503 unsigned long flags;
505 if (info->flags & ASYNC_INITIALIZED)
506 return 0;
508 if (!info->xmit_buf) {
509 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
510 if (!info->xmit_buf)
511 return -ENOMEM;
514 local_irq_save(flags);
516 #ifdef SERIAL_DEBUG_OPEN
517 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
518 #endif
521 * Reset UART, get it into known state...
523 uartp = info->addr;
524 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
525 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
526 mcfrs_setsignals(info, 1, 1);
528 if (info->tty)
529 clear_bit(TTY_IO_ERROR, &info->tty->flags);
530 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
533 * and set the speed of the serial port
535 mcfrs_change_speed(info);
538 * Lastly enable the UART transmitter and receiver, and
539 * interrupt enables.
541 info->imr = MCFUART_UIR_RXREADY;
542 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
543 uartp[MCFUART_UIMR] = info->imr;
545 info->flags |= ASYNC_INITIALIZED;
546 local_irq_restore(flags);
547 return 0;
551 * This routine will shutdown a serial port; interrupts are disabled, and
552 * DTR is dropped if the hangup on close termio flag is on.
554 static void shutdown(struct mcf_serial * info)
556 volatile unsigned char *uartp;
557 unsigned long flags;
559 if (!(info->flags & ASYNC_INITIALIZED))
560 return;
562 #ifdef SERIAL_DEBUG_OPEN
563 printk("Shutting down serial port %d (irq %d)....\n", info->line,
564 info->irq);
565 #endif
567 local_irq_save(flags);
569 uartp = info->addr;
570 uartp[MCFUART_UIMR] = 0; /* mask all interrupts */
571 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
572 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
574 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
575 mcfrs_setsignals(info, 0, 0);
577 if (info->xmit_buf) {
578 free_page((unsigned long) info->xmit_buf);
579 info->xmit_buf = 0;
582 if (info->tty)
583 set_bit(TTY_IO_ERROR, &info->tty->flags);
585 info->flags &= ~ASYNC_INITIALIZED;
586 local_irq_restore(flags);
591 * This routine is called to set the UART divisor registers to match
592 * the specified baud rate for a serial port.
594 static void mcfrs_change_speed(struct mcf_serial *info)
596 volatile unsigned char *uartp;
597 unsigned int baudclk, cflag;
598 unsigned long flags;
599 unsigned char mr1, mr2;
600 int i;
601 #ifdef CONFIG_M5272
602 unsigned int fraction;
603 #endif
605 if (!info->tty || !info->tty->termios)
606 return;
607 cflag = info->tty->termios->c_cflag;
608 if (info->addr == 0)
609 return;
611 #if 0
612 printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
613 #endif
615 i = cflag & CBAUD;
616 if (i & CBAUDEX) {
617 i &= ~CBAUDEX;
618 if (i < 1 || i > 4)
619 info->tty->termios->c_cflag &= ~CBAUDEX;
620 else
621 i += 15;
623 if (i == 0) {
624 mcfrs_setsignals(info, 0, -1);
625 return;
628 /* compute the baudrate clock */
629 #ifdef CONFIG_M5272
631 * For the MCF5272, also compute the baudrate fraction.
633 baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
634 fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
635 fraction *= 16;
636 fraction /= (32 * mcfrs_baud_table[i]);
637 #else
638 baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
639 #endif
641 info->baud = mcfrs_baud_table[i];
643 mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
644 mr2 = 0;
646 switch (cflag & CSIZE) {
647 case CS5: mr1 |= MCFUART_MR1_CS5; break;
648 case CS6: mr1 |= MCFUART_MR1_CS6; break;
649 case CS7: mr1 |= MCFUART_MR1_CS7; break;
650 case CS8:
651 default: mr1 |= MCFUART_MR1_CS8; break;
654 if (cflag & PARENB) {
655 if (cflag & CMSPAR) {
656 if (cflag & PARODD)
657 mr1 |= MCFUART_MR1_PARITYMARK;
658 else
659 mr1 |= MCFUART_MR1_PARITYSPACE;
660 } else {
661 if (cflag & PARODD)
662 mr1 |= MCFUART_MR1_PARITYODD;
663 else
664 mr1 |= MCFUART_MR1_PARITYEVEN;
666 } else {
667 mr1 |= MCFUART_MR1_PARITYNONE;
670 if (cflag & CSTOPB)
671 mr2 |= MCFUART_MR2_STOP2;
672 else
673 mr2 |= MCFUART_MR2_STOP1;
675 if (cflag & CRTSCTS) {
676 mr1 |= MCFUART_MR1_RXRTS;
677 mr2 |= MCFUART_MR2_TXCTS;
680 if (cflag & CLOCAL)
681 info->flags &= ~ASYNC_CHECK_CD;
682 else
683 info->flags |= ASYNC_CHECK_CD;
685 uartp = info->addr;
687 local_irq_save(flags);
688 #if 0
689 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
690 mr1, mr2, baudclk);
691 #endif
693 Note: pg 12-16 of MCF5206e User's Manual states that a
694 software reset should be performed prior to changing
695 UMR1,2, UCSR, UACR, bit 7
697 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
698 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
699 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
700 uartp[MCFUART_UMR] = mr1;
701 uartp[MCFUART_UMR] = mr2;
702 uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8; /* set msb byte */
703 uartp[MCFUART_UBG2] = (baudclk & 0xff); /* set lsb byte */
704 #ifdef CONFIG_M5272
705 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
706 #endif
707 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
708 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
709 mcfrs_setsignals(info, 1, -1);
710 local_irq_restore(flags);
711 return;
714 static void mcfrs_flush_chars(struct tty_struct *tty)
716 volatile unsigned char *uartp;
717 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
718 unsigned long flags;
720 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
721 return;
723 uartp = (volatile unsigned char *) info->addr;
726 * re-enable receiver interrupt
728 local_irq_save(flags);
729 if ((!(info->imr & MCFUART_UIR_RXREADY)) &&
730 (info->flags & ASYNC_INITIALIZED) ) {
731 info->imr |= MCFUART_UIR_RXREADY;
732 uartp[MCFUART_UIMR] = info->imr;
734 local_irq_restore(flags);
736 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
737 !info->xmit_buf)
738 return;
740 /* Enable transmitter */
741 local_irq_save(flags);
742 info->imr |= MCFUART_UIR_TXREADY;
743 uartp[MCFUART_UIMR] = info->imr;
744 local_irq_restore(flags);
747 static int mcfrs_write(struct tty_struct * tty,
748 const unsigned char *buf, int count)
750 volatile unsigned char *uartp;
751 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
752 unsigned long flags;
753 int c, total = 0;
755 #if 0
756 printk("%s(%d): mcfrs_write(tty=%x,buf=%x,count=%d)\n",
757 __FILE__, __LINE__, (int)tty, (int)buf, count);
758 #endif
760 if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
761 return 0;
763 if (!tty || !info->xmit_buf)
764 return 0;
766 local_save_flags(flags);
767 while (1) {
768 local_irq_disable();
769 c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
770 ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
771 local_irq_restore(flags);
773 if (c <= 0)
774 break;
776 memcpy(info->xmit_buf + info->xmit_head, buf, c);
778 local_irq_disable();
779 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
780 info->xmit_cnt += c;
781 local_irq_restore(flags);
783 buf += c;
784 count -= c;
785 total += c;
788 local_irq_disable();
789 uartp = info->addr;
790 info->imr |= MCFUART_UIR_TXREADY;
791 uartp[MCFUART_UIMR] = info->imr;
792 local_irq_restore(flags);
794 return total;
797 static int mcfrs_write_room(struct tty_struct *tty)
799 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
800 int ret;
802 if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
803 return 0;
804 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
805 if (ret < 0)
806 ret = 0;
807 return ret;
810 static int mcfrs_chars_in_buffer(struct tty_struct *tty)
812 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
814 if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
815 return 0;
816 return info->xmit_cnt;
819 static void mcfrs_flush_buffer(struct tty_struct *tty)
821 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
822 unsigned long flags;
824 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
825 return;
827 local_irq_save(flags);
828 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
829 local_irq_restore(flags);
831 tty_wakeup(tty);
835 * ------------------------------------------------------------
836 * mcfrs_throttle()
838 * This routine is called by the upper-layer tty layer to signal that
839 * incoming characters should be throttled.
840 * ------------------------------------------------------------
842 static void mcfrs_throttle(struct tty_struct * tty)
844 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
845 #ifdef SERIAL_DEBUG_THROTTLE
846 char buf[64];
848 printk("throttle %s: %d....\n", _tty_name(tty, buf),
849 tty->ldisc.chars_in_buffer(tty));
850 #endif
852 if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
853 return;
855 if (I_IXOFF(tty))
856 info->x_char = STOP_CHAR(tty);
858 /* Turn off RTS line (do this atomic) */
861 static void mcfrs_unthrottle(struct tty_struct * tty)
863 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
864 #ifdef SERIAL_DEBUG_THROTTLE
865 char buf[64];
867 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
868 tty->ldisc.chars_in_buffer(tty));
869 #endif
871 if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
872 return;
874 if (I_IXOFF(tty)) {
875 if (info->x_char)
876 info->x_char = 0;
877 else
878 info->x_char = START_CHAR(tty);
881 /* Assert RTS line (do this atomic) */
885 * ------------------------------------------------------------
886 * mcfrs_ioctl() and friends
887 * ------------------------------------------------------------
890 static int get_serial_info(struct mcf_serial * info,
891 struct serial_struct * retinfo)
893 struct serial_struct tmp;
895 if (!retinfo)
896 return -EFAULT;
897 memset(&tmp, 0, sizeof(tmp));
898 tmp.type = info->type;
899 tmp.line = info->line;
900 tmp.port = (unsigned int) info->addr;
901 tmp.irq = info->irq;
902 tmp.flags = info->flags;
903 tmp.baud_base = info->baud_base;
904 tmp.close_delay = info->close_delay;
905 tmp.closing_wait = info->closing_wait;
906 tmp.custom_divisor = info->custom_divisor;
907 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
910 static int set_serial_info(struct mcf_serial * info,
911 struct serial_struct * new_info)
913 struct serial_struct new_serial;
914 struct mcf_serial old_info;
915 int retval = 0;
917 if (!new_info)
918 return -EFAULT;
919 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
920 return -EFAULT;
921 old_info = *info;
923 if (!capable(CAP_SYS_ADMIN)) {
924 if ((new_serial.baud_base != info->baud_base) ||
925 (new_serial.type != info->type) ||
926 (new_serial.close_delay != info->close_delay) ||
927 ((new_serial.flags & ~ASYNC_USR_MASK) !=
928 (info->flags & ~ASYNC_USR_MASK)))
929 return -EPERM;
930 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
931 (new_serial.flags & ASYNC_USR_MASK));
932 info->custom_divisor = new_serial.custom_divisor;
933 goto check_and_exit;
936 if (info->count > 1)
937 return -EBUSY;
940 * OK, past this point, all the error checking has been done.
941 * At this point, we start making changes.....
944 info->baud_base = new_serial.baud_base;
945 info->flags = ((info->flags & ~ASYNC_FLAGS) |
946 (new_serial.flags & ASYNC_FLAGS));
947 info->type = new_serial.type;
948 info->close_delay = new_serial.close_delay;
949 info->closing_wait = new_serial.closing_wait;
951 check_and_exit:
952 retval = startup(info);
953 return retval;
957 * get_lsr_info - get line status register info
959 * Purpose: Let user call ioctl() to get info when the UART physically
960 * is emptied. On bus types like RS485, the transmitter must
961 * release the bus after transmitting. This must be done when
962 * the transmit shift register is empty, not be done when the
963 * transmit holding register is empty. This functionality
964 * allows an RS485 driver to be written in user space.
966 static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
968 volatile unsigned char *uartp;
969 unsigned long flags;
970 unsigned char status;
972 local_irq_save(flags);
973 uartp = info->addr;
974 status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
975 local_irq_restore(flags);
977 return put_user(status,value);
981 * This routine sends a break character out the serial port.
983 static void send_break( struct mcf_serial * info, int duration)
985 volatile unsigned char *uartp;
986 unsigned long flags;
988 if (!info->addr)
989 return;
990 set_current_state(TASK_INTERRUPTIBLE);
991 uartp = info->addr;
993 local_irq_save(flags);
994 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
995 schedule_timeout(duration);
996 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
997 local_irq_restore(flags);
1000 static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
1002 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1004 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1005 return -ENODEV;
1006 if (tty->flags & (1 << TTY_IO_ERROR))
1007 return -EIO;
1009 return mcfrs_getsignals(info);
1012 static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
1013 unsigned int set, unsigned int clear)
1015 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1016 int rts = -1, dtr = -1;
1018 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1019 return -ENODEV;
1020 if (tty->flags & (1 << TTY_IO_ERROR))
1021 return -EIO;
1023 if (set & TIOCM_RTS)
1024 rts = 1;
1025 if (set & TIOCM_DTR)
1026 dtr = 1;
1027 if (clear & TIOCM_RTS)
1028 rts = 0;
1029 if (clear & TIOCM_DTR)
1030 dtr = 0;
1032 mcfrs_setsignals(info, dtr, rts);
1034 return 0;
1037 static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
1038 unsigned int cmd, unsigned long arg)
1040 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1041 int retval, error;
1043 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1044 return -ENODEV;
1046 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1047 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1048 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1049 if (tty->flags & (1 << TTY_IO_ERROR))
1050 return -EIO;
1053 switch (cmd) {
1054 case TCSBRK: /* SVID version: non-zero arg --> no break */
1055 retval = tty_check_change(tty);
1056 if (retval)
1057 return retval;
1058 tty_wait_until_sent(tty, 0);
1059 if (!arg)
1060 send_break(info, HZ/4); /* 1/4 second */
1061 return 0;
1062 case TCSBRKP: /* support for POSIX tcsendbreak() */
1063 retval = tty_check_change(tty);
1064 if (retval)
1065 return retval;
1066 tty_wait_until_sent(tty, 0);
1067 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1068 return 0;
1069 case TIOCGSOFTCAR:
1070 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1071 (unsigned long *) arg);
1072 if (error)
1073 return error;
1074 return 0;
1075 case TIOCSSOFTCAR:
1076 get_user(arg, (unsigned long *) arg);
1077 tty->termios->c_cflag =
1078 ((tty->termios->c_cflag & ~CLOCAL) |
1079 (arg ? CLOCAL : 0));
1080 return 0;
1081 case TIOCGSERIAL:
1082 if (access_ok(VERIFY_WRITE, (void *) arg,
1083 sizeof(struct serial_struct)))
1084 return get_serial_info(info,
1085 (struct serial_struct *) arg);
1086 return -EFAULT;
1087 case TIOCSSERIAL:
1088 return set_serial_info(info,
1089 (struct serial_struct *) arg);
1090 case TIOCSERGETLSR: /* Get line status register */
1091 if (access_ok(VERIFY_WRITE, (void *) arg,
1092 sizeof(unsigned int)))
1093 return get_lsr_info(info, (unsigned int *) arg);
1094 return -EFAULT;
1095 case TIOCSERGSTRUCT:
1096 error = copy_to_user((struct mcf_serial *) arg,
1097 info, sizeof(struct mcf_serial));
1098 if (error)
1099 return -EFAULT;
1100 return 0;
1102 #ifdef TIOCSET422
1103 case TIOCSET422: {
1104 unsigned int val;
1105 get_user(val, (unsigned int *) arg);
1106 mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1107 break;
1109 case TIOCGET422: {
1110 unsigned int val;
1111 val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1112 put_user(val, (unsigned int *) arg);
1113 break;
1115 #endif
1117 default:
1118 return -ENOIOCTLCMD;
1120 return 0;
1123 static void mcfrs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1125 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1127 if (tty->termios->c_cflag == old_termios->c_cflag)
1128 return;
1130 mcfrs_change_speed(info);
1132 if ((old_termios->c_cflag & CRTSCTS) &&
1133 !(tty->termios->c_cflag & CRTSCTS)) {
1134 tty->hw_stopped = 0;
1135 mcfrs_setsignals(info, -1, 1);
1136 #if 0
1137 mcfrs_start(tty);
1138 #endif
1143 * ------------------------------------------------------------
1144 * mcfrs_close()
1146 * This routine is called when the serial port gets closed. First, we
1147 * wait for the last remaining data to be sent. Then, we unlink its
1148 * S structure from the interrupt chain if necessary, and we free
1149 * that IRQ if nothing is left in the chain.
1150 * ------------------------------------------------------------
1152 static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1154 volatile unsigned char *uartp;
1155 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1156 unsigned long flags;
1158 if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1159 return;
1161 local_irq_save(flags);
1163 if (tty_hung_up_p(filp)) {
1164 local_irq_restore(flags);
1165 return;
1168 #ifdef SERIAL_DEBUG_OPEN
1169 printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1170 #endif
1171 if ((tty->count == 1) && (info->count != 1)) {
1173 * Uh, oh. tty->count is 1, which means that the tty
1174 * structure will be freed. Info->count should always
1175 * be one in these conditions. If it's greater than
1176 * one, we've got real problems, since it means the
1177 * serial port won't be shutdown.
1179 printk("MCFRS: bad serial port count; tty->count is 1, "
1180 "info->count is %d\n", info->count);
1181 info->count = 1;
1183 if (--info->count < 0) {
1184 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1185 info->line, info->count);
1186 info->count = 0;
1188 if (info->count) {
1189 local_irq_restore(flags);
1190 return;
1192 info->flags |= ASYNC_CLOSING;
1195 * Now we wait for the transmit buffer to clear; and we notify
1196 * the line discipline to only process XON/XOFF characters.
1198 tty->closing = 1;
1199 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1200 tty_wait_until_sent(tty, info->closing_wait);
1203 * At this point we stop accepting input. To do this, we
1204 * disable the receive line status interrupts, and tell the
1205 * interrupt driver to stop checking the data ready bit in the
1206 * line status register.
1208 info->imr &= ~MCFUART_UIR_RXREADY;
1209 uartp = info->addr;
1210 uartp[MCFUART_UIMR] = info->imr;
1212 #if 0
1213 /* FIXME: do we need to keep this enabled for console?? */
1214 if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
1215 /* Do not disable the UART */ ;
1216 } else
1217 #endif
1218 shutdown(info);
1219 if (tty->driver->flush_buffer)
1220 tty->driver->flush_buffer(tty);
1221 tty_ldisc_flush(tty);
1223 tty->closing = 0;
1224 info->event = 0;
1225 info->tty = 0;
1226 #if 0
1227 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1228 if (tty->ldisc.close)
1229 (tty->ldisc.close)(tty);
1230 tty->ldisc = ldiscs[N_TTY];
1231 tty->termios->c_line = N_TTY;
1232 if (tty->ldisc.open)
1233 (tty->ldisc.open)(tty);
1235 #endif
1236 if (info->blocked_open) {
1237 if (info->close_delay) {
1238 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1240 wake_up_interruptible(&info->open_wait);
1242 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1243 wake_up_interruptible(&info->close_wait);
1244 local_irq_restore(flags);
1248 * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1250 static void
1251 mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1253 #ifdef CONFIG_M5272
1254 #define MCF5272_FIFO_SIZE 25 /* fifo size + shift reg */
1256 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1257 volatile unsigned char *uartp;
1258 unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1260 if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1261 return;
1263 orig_jiffies = jiffies;
1266 * Set the check interval to be 1/5 of the approximate time
1267 * to send the entire fifo, and make it at least 1. The check
1268 * interval should also be less than the timeout.
1270 * Note: we have to use pretty tight timings here to satisfy
1271 * the NIST-PCTS.
1273 fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1274 char_time = fifo_time / 5;
1275 if (char_time == 0)
1276 char_time = 1;
1277 if (timeout && timeout < char_time)
1278 char_time = timeout;
1281 * Clamp the timeout period at 2 * the time to empty the
1282 * fifo. Just to be safe, set the minimum at .5 seconds.
1284 fifo_time *= 2;
1285 if (fifo_time < (HZ/2))
1286 fifo_time = HZ/2;
1287 if (!timeout || timeout > fifo_time)
1288 timeout = fifo_time;
1291 * Account for the number of bytes in the UART
1292 * transmitter FIFO plus any byte being shifted out.
1294 uartp = (volatile unsigned char *) info->addr;
1295 for (;;) {
1296 fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1297 if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1298 MCFUART_USR_TXEMPTY)) ==
1299 MCFUART_USR_TXREADY)
1300 fifo_cnt++;
1301 if (fifo_cnt == 0)
1302 break;
1303 msleep_interruptible(jiffies_to_msecs(char_time));
1304 if (signal_pending(current))
1305 break;
1306 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1307 break;
1309 #else
1311 * For the other coldfire models, assume all data has been sent
1313 #endif
1317 * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1319 void mcfrs_hangup(struct tty_struct *tty)
1321 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1323 if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
1324 return;
1326 mcfrs_flush_buffer(tty);
1327 shutdown(info);
1328 info->event = 0;
1329 info->count = 0;
1330 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1331 info->tty = 0;
1332 wake_up_interruptible(&info->open_wait);
1336 * ------------------------------------------------------------
1337 * mcfrs_open() and friends
1338 * ------------------------------------------------------------
1340 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1341 struct mcf_serial *info)
1343 DECLARE_WAITQUEUE(wait, current);
1344 int retval;
1345 int do_clocal = 0;
1348 * If the device is in the middle of being closed, then block
1349 * until it's done, and then try again.
1351 if (info->flags & ASYNC_CLOSING) {
1352 interruptible_sleep_on(&info->close_wait);
1353 #ifdef SERIAL_DO_RESTART
1354 if (info->flags & ASYNC_HUP_NOTIFY)
1355 return -EAGAIN;
1356 else
1357 return -ERESTARTSYS;
1358 #else
1359 return -EAGAIN;
1360 #endif
1364 * If non-blocking mode is set, or the port is not enabled,
1365 * then make the check up front and then exit.
1367 if ((filp->f_flags & O_NONBLOCK) ||
1368 (tty->flags & (1 << TTY_IO_ERROR))) {
1369 info->flags |= ASYNC_NORMAL_ACTIVE;
1370 return 0;
1373 if (tty->termios->c_cflag & CLOCAL)
1374 do_clocal = 1;
1377 * Block waiting for the carrier detect and the line to become
1378 * free (i.e., not in use by the callout). While we are in
1379 * this loop, info->count is dropped by one, so that
1380 * mcfrs_close() knows when to free things. We restore it upon
1381 * exit, either normal or abnormal.
1383 retval = 0;
1384 add_wait_queue(&info->open_wait, &wait);
1385 #ifdef SERIAL_DEBUG_OPEN
1386 printk("block_til_ready before block: ttyS%d, count = %d\n",
1387 info->line, info->count);
1388 #endif
1389 info->count--;
1390 info->blocked_open++;
1391 while (1) {
1392 local_irq_disable();
1393 mcfrs_setsignals(info, 1, 1);
1394 local_irq_enable();
1395 current->state = TASK_INTERRUPTIBLE;
1396 if (tty_hung_up_p(filp) ||
1397 !(info->flags & ASYNC_INITIALIZED)) {
1398 #ifdef SERIAL_DO_RESTART
1399 if (info->flags & ASYNC_HUP_NOTIFY)
1400 retval = -EAGAIN;
1401 else
1402 retval = -ERESTARTSYS;
1403 #else
1404 retval = -EAGAIN;
1405 #endif
1406 break;
1408 if (!(info->flags & ASYNC_CLOSING) &&
1409 (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1410 break;
1411 if (signal_pending(current)) {
1412 retval = -ERESTARTSYS;
1413 break;
1415 #ifdef SERIAL_DEBUG_OPEN
1416 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1417 info->line, info->count);
1418 #endif
1419 schedule();
1421 current->state = TASK_RUNNING;
1422 remove_wait_queue(&info->open_wait, &wait);
1423 if (!tty_hung_up_p(filp))
1424 info->count++;
1425 info->blocked_open--;
1426 #ifdef SERIAL_DEBUG_OPEN
1427 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1428 info->line, info->count);
1429 #endif
1430 if (retval)
1431 return retval;
1432 info->flags |= ASYNC_NORMAL_ACTIVE;
1433 return 0;
1437 * This routine is called whenever a serial port is opened. It
1438 * enables interrupts for a serial port, linking in its structure into
1439 * the IRQ chain. It also performs the serial-specific
1440 * initialization for the tty structure.
1442 int mcfrs_open(struct tty_struct *tty, struct file * filp)
1444 struct mcf_serial *info;
1445 int retval, line;
1447 line = tty->index;
1448 if ((line < 0) || (line >= NR_PORTS))
1449 return -ENODEV;
1450 info = mcfrs_table + line;
1451 if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1452 return -ENODEV;
1453 #ifdef SERIAL_DEBUG_OPEN
1454 printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1455 #endif
1456 info->count++;
1457 tty->driver_data = info;
1458 info->tty = tty;
1461 * Start up serial port
1463 retval = startup(info);
1464 if (retval)
1465 return retval;
1467 retval = block_til_ready(tty, filp, info);
1468 if (retval) {
1469 #ifdef SERIAL_DEBUG_OPEN
1470 printk("mcfrs_open returning after block_til_ready with %d\n",
1471 retval);
1472 #endif
1473 return retval;
1476 #ifdef SERIAL_DEBUG_OPEN
1477 printk("mcfrs_open %s successful...\n", tty->name);
1478 #endif
1479 return 0;
1483 * Based on the line number set up the internal interrupt stuff.
1485 static void mcfrs_irqinit(struct mcf_serial *info)
1487 #if defined(CONFIG_M5272)
1488 volatile unsigned long *icrp;
1489 volatile unsigned long *portp;
1490 volatile unsigned char *uartp;
1492 uartp = info->addr;
1493 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1495 switch (info->line) {
1496 case 0:
1497 *icrp = 0xe0000000;
1498 break;
1499 case 1:
1500 *icrp = 0x0e000000;
1501 break;
1502 default:
1503 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1504 info->line);
1505 return;
1508 /* Enable the output lines for the serial ports */
1509 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
1510 *portp = (*portp & ~0x000000ff) | 0x00000055;
1511 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
1512 *portp = (*portp & ~0x000003fc) | 0x000002a8;
1513 #elif defined(CONFIG_M527x) || defined(CONFIG_M528x)
1514 volatile unsigned char *icrp, *uartp;
1515 volatile unsigned long *imrp;
1517 uartp = info->addr;
1519 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1520 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1521 *icrp = 0x33; /* UART0 with level 6, priority 3 */
1523 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1524 MCFINTC_IMRL);
1525 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1526 #else
1527 volatile unsigned char *icrp, *uartp;
1529 switch (info->line) {
1530 case 0:
1531 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1532 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1533 MCFSIM_ICR_PRI1;
1534 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1535 break;
1536 case 1:
1537 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1538 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1539 MCFSIM_ICR_PRI2;
1540 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1541 break;
1542 default:
1543 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1544 info->line);
1545 return;
1548 uartp = info->addr;
1549 uartp[MCFUART_UIVR] = info->irq;
1550 #endif
1552 /* Clear mask, so no surprise interrupts. */
1553 uartp[MCFUART_UIMR] = 0;
1555 if (request_irq(info->irq, mcfrs_interrupt, SA_INTERRUPT,
1556 "ColdFire UART", NULL)) {
1557 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1558 "vector=%d\n", info->line, info->irq);
1561 return;
1565 char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1569 * Serial stats reporting...
1571 int mcfrs_readproc(char *page, char **start, off_t off, int count,
1572 int *eof, void *data)
1574 struct mcf_serial *info;
1575 char str[20];
1576 int len, sigs, i;
1578 len = sprintf(page, mcfrs_drivername);
1579 for (i = 0; (i < NR_PORTS); i++) {
1580 info = &mcfrs_table[i];
1581 len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1582 i, (unsigned int) info->addr, info->irq, info->baud);
1583 if (info->stats.rx || info->stats.tx)
1584 len += sprintf((page + len), "tx:%d rx:%d ",
1585 info->stats.tx, info->stats.rx);
1586 if (info->stats.rxframing)
1587 len += sprintf((page + len), "fe:%d ",
1588 info->stats.rxframing);
1589 if (info->stats.rxparity)
1590 len += sprintf((page + len), "pe:%d ",
1591 info->stats.rxparity);
1592 if (info->stats.rxbreak)
1593 len += sprintf((page + len), "brk:%d ",
1594 info->stats.rxbreak);
1595 if (info->stats.rxoverrun)
1596 len += sprintf((page + len), "oe:%d ",
1597 info->stats.rxoverrun);
1599 str[0] = str[1] = 0;
1600 if ((sigs = mcfrs_getsignals(info))) {
1601 if (sigs & TIOCM_RTS)
1602 strcat(str, "|RTS");
1603 if (sigs & TIOCM_CTS)
1604 strcat(str, "|CTS");
1605 if (sigs & TIOCM_DTR)
1606 strcat(str, "|DTR");
1607 if (sigs & TIOCM_CD)
1608 strcat(str, "|CD");
1611 len += sprintf((page + len), "%s\n", &str[1]);
1614 return(len);
1618 /* Finally, routines used to initialize the serial driver. */
1620 static void show_serial_version(void)
1622 printk(mcfrs_drivername);
1625 static struct tty_operations mcfrs_ops = {
1626 .open = mcfrs_open,
1627 .close = mcfrs_close,
1628 .write = mcfrs_write,
1629 .flush_chars = mcfrs_flush_chars,
1630 .write_room = mcfrs_write_room,
1631 .chars_in_buffer = mcfrs_chars_in_buffer,
1632 .flush_buffer = mcfrs_flush_buffer,
1633 .ioctl = mcfrs_ioctl,
1634 .throttle = mcfrs_throttle,
1635 .unthrottle = mcfrs_unthrottle,
1636 .set_termios = mcfrs_set_termios,
1637 .stop = mcfrs_stop,
1638 .start = mcfrs_start,
1639 .hangup = mcfrs_hangup,
1640 .read_proc = mcfrs_readproc,
1641 .wait_until_sent = mcfrs_wait_until_sent,
1642 .tiocmget = mcfrs_tiocmget,
1643 .tiocmset = mcfrs_tiocmset,
1646 /* mcfrs_init inits the driver */
1647 static int __init
1648 mcfrs_init(void)
1650 struct mcf_serial *info;
1651 unsigned long flags;
1652 int i;
1654 /* Setup base handler, and timer table. */
1655 #ifdef MCFPP_DCD0
1656 init_timer(&mcfrs_timer_struct);
1657 mcfrs_timer_struct.function = mcfrs_timer;
1658 mcfrs_timer_struct.data = 0;
1659 mcfrs_timer_struct.expires = jiffies + HZ/25;
1660 add_timer(&mcfrs_timer_struct);
1661 mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1662 #endif
1663 mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1664 if (!mcfrs_serial_driver)
1665 return -ENOMEM;
1667 show_serial_version();
1669 /* Initialize the tty_driver structure */
1670 mcfrs_serial_driver->owner = THIS_MODULE;
1671 mcfrs_serial_driver->name = "ttyS";
1672 mcfrs_serial_driver->devfs_name = "ttys/";
1673 mcfrs_serial_driver->driver_name = "serial";
1674 mcfrs_serial_driver->major = TTY_MAJOR;
1675 mcfrs_serial_driver->minor_start = 64;
1676 mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1677 mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1678 mcfrs_serial_driver->init_termios = tty_std_termios;
1680 mcfrs_serial_driver->init_termios.c_cflag =
1681 mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1682 mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1684 tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1686 if (tty_register_driver(mcfrs_serial_driver)) {
1687 printk("MCFRS: Couldn't register serial driver\n");
1688 put_tty_driver(mcfrs_serial_driver);
1689 return(-EBUSY);
1692 local_irq_save(flags);
1695 * Configure all the attached serial ports.
1697 for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1698 info->magic = SERIAL_MAGIC;
1699 info->line = i;
1700 info->tty = 0;
1701 info->custom_divisor = 16;
1702 info->close_delay = 50;
1703 info->closing_wait = 3000;
1704 info->x_char = 0;
1705 info->event = 0;
1706 info->count = 0;
1707 info->blocked_open = 0;
1708 INIT_WORK(&info->tqueue, mcfrs_offintr, info);
1709 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1710 init_waitqueue_head(&info->open_wait);
1711 init_waitqueue_head(&info->close_wait);
1713 info->imr = 0;
1714 mcfrs_setsignals(info, 0, 0);
1715 mcfrs_irqinit(info);
1717 printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1718 (unsigned int) info->addr, info->irq);
1719 printk(" is a builtin ColdFire UART\n");
1722 local_irq_restore(flags);
1723 return 0;
1726 module_init(mcfrs_init);
1728 /****************************************************************************/
1729 /* Serial Console */
1730 /****************************************************************************/
1733 * Quick and dirty UART initialization, for console output.
1736 void mcfrs_init_console(void)
1738 volatile unsigned char *uartp;
1739 unsigned int clk;
1742 * Reset UART, get it into known state...
1744 uartp = (volatile unsigned char *) (MCF_MBAR +
1745 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1747 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
1748 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
1749 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
1752 * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1754 uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1755 uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1757 clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1758 uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8; /* set msb baud */
1759 uartp[MCFUART_UBG2] = (clk & 0xff); /* set lsb baud */
1761 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1762 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1764 mcfrs_console_inited++;
1765 return;
1770 * Setup for console. Argument comes from the boot command line.
1773 int mcfrs_console_setup(struct console *cp, char *arg)
1775 int i, n = CONSOLE_BAUD_RATE;
1777 if (!cp)
1778 return(-1);
1780 if (!strncmp(cp->name, "ttyS", 4))
1781 mcfrs_console_port = cp->index;
1782 else if (!strncmp(cp->name, "cua", 3))
1783 mcfrs_console_port = cp->index;
1784 else
1785 return(-1);
1787 if (arg)
1788 n = simple_strtoul(arg,NULL,0);
1789 for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1790 if (mcfrs_baud_table[i] == n)
1791 break;
1792 if (i < MCFRS_BAUD_TABLE_SIZE) {
1793 mcfrs_console_baud = n;
1794 mcfrs_console_cbaud = 0;
1795 if (i > 15) {
1796 mcfrs_console_cbaud |= CBAUDEX;
1797 i -= 15;
1799 mcfrs_console_cbaud |= i;
1801 mcfrs_init_console(); /* make sure baud rate changes */
1802 return(0);
1806 static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1808 *index = c->index;
1809 return mcfrs_serial_driver;
1814 * Output a single character, using UART polled mode.
1815 * This is used for console output.
1818 void mcfrs_put_char(char ch)
1820 volatile unsigned char *uartp;
1821 unsigned long flags;
1822 int i;
1824 uartp = (volatile unsigned char *) (MCF_MBAR +
1825 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1827 local_irq_save(flags);
1828 for (i = 0; (i < 0x10000); i++) {
1829 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1830 break;
1832 if (i < 0x10000) {
1833 uartp[MCFUART_UTB] = ch;
1834 for (i = 0; (i < 0x10000); i++)
1835 if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1836 break;
1838 if (i >= 0x10000)
1839 mcfrs_init_console(); /* try and get it back */
1840 local_irq_restore(flags);
1842 return;
1847 * rs_console_write is registered for printk output.
1850 void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1852 if (!mcfrs_console_inited)
1853 mcfrs_init_console();
1854 while (len-- > 0) {
1855 if (*p == '\n')
1856 mcfrs_put_char('\r');
1857 mcfrs_put_char(*p++);
1862 * declare our consoles
1865 struct console mcfrs_console = {
1866 .name = "ttyS",
1867 .write = mcfrs_console_write,
1868 .device = mcfrs_console_device,
1869 .setup = mcfrs_console_setup,
1870 .flags = CON_PRINTBUFFER,
1871 .index = -1,
1874 static int __init mcfrs_console_init(void)
1876 register_console(&mcfrs_console);
1877 return 0;
1880 console_initcall(mcfrs_console_init);
1882 /****************************************************************************/