x86, io-apic: use predefined names instead of numeric constants
[linux-2.6/mini2440.git] / drivers / serial / mcfserial.c
blob56007cc8a9b31c24cd75390a932c4d426efec9f8
1 #warning This driver is deprecated. Check Kconfig for details.
2 /*
3 * mcfserial.c -- serial driver for ColdFire internal UARTS.
5 * Copyright (C) 1999-2003 Greg Ungerer <gerg@snapgear.com>
6 * Copyright (c) 2000-2001 Lineo, Inc. <www.lineo.com>
7 * Copyright (C) 2001-2002 SnapGear Inc. <www.snapgear.com>
9 * Based on code from 68332serial.c which was:
11 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
12 * Copyright (C) 1998 TSHG
13 * Copyright (c) 1999 Rt-Control Inc. <jeff@uclinux.org>
15 * Changes:
16 * 08/07/2003 Daniele Bellucci <bellucda@tiscali.it>
17 * some cleanups in mcfrs_write.
21 #include <linux/module.h>
22 #include <linux/errno.h>
23 #include <linux/signal.h>
24 #include <linux/sched.h>
25 #include <linux/timer.h>
26 #include <linux/wait.h>
27 #include <linux/interrupt.h>
28 #include <linux/tty.h>
29 #include <linux/tty_flip.h>
30 #include <linux/string.h>
31 #include <linux/fcntl.h>
32 #include <linux/mm.h>
33 #include <linux/kernel.h>
34 #include <linux/serial.h>
35 #include <linux/serialP.h>
36 #include <linux/console.h>
37 #include <linux/init.h>
38 #include <linux/bitops.h>
39 #include <linux/delay.h>
41 #include <asm/io.h>
42 #include <asm/irq.h>
43 #include <asm/system.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_HW_FEITH)
61 #define CONSOLE_BAUD_RATE 38400
62 #define DEFAULT_CBAUD B38400
63 #elif defined(CONFIG_MOD5272) || defined(CONFIG_M5208EVB) || \
64 defined(CONFIG_M5329EVB) || defined(CONFIG_GILBARCO)
65 #define CONSOLE_BAUD_RATE 115200
66 #define DEFAULT_CBAUD B115200
67 #elif defined(CONFIG_ARNEWSH) || defined(CONFIG_FREESCALE) || \
68 defined(CONFIG_senTec) || defined(CONFIG_SNEHA) || defined(CONFIG_AVNET)
69 #define CONSOLE_BAUD_RATE 19200
70 #define DEFAULT_CBAUD B19200
71 #endif
73 #ifndef CONSOLE_BAUD_RATE
74 #define CONSOLE_BAUD_RATE 9600
75 #define DEFAULT_CBAUD B9600
76 #endif
78 int mcfrs_console_inited = 0;
79 int mcfrs_console_port = -1;
80 int mcfrs_console_baud = CONSOLE_BAUD_RATE;
81 int mcfrs_console_cbaud = DEFAULT_CBAUD;
84 * Driver data structures.
86 static struct tty_driver *mcfrs_serial_driver;
88 /* number of characters left in xmit buffer before we ask for more */
89 #define WAKEUP_CHARS 256
91 /* Debugging...
93 #undef SERIAL_DEBUG_OPEN
94 #undef SERIAL_DEBUG_FLOW
96 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
97 defined(CONFIG_M520x) || defined(CONFIG_M532x)
98 #define IRQBASE (MCFINT_VECBASE+MCFINT_UART0)
99 #else
100 #define IRQBASE 73
101 #endif
104 * Configuration table, UARTs to look for at startup.
106 static struct mcf_serial mcfrs_table[] = {
107 { /* ttyS0 */
108 .magic = 0,
109 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE1),
110 .irq = IRQBASE,
111 .flags = ASYNC_BOOT_AUTOCONF,
113 #ifdef MCFUART_BASE2
114 { /* ttyS1 */
115 .magic = 0,
116 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE2),
117 .irq = IRQBASE+1,
118 .flags = ASYNC_BOOT_AUTOCONF,
120 #endif
121 #ifdef MCFUART_BASE3
122 { /* ttyS2 */
123 .magic = 0,
124 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE3),
125 .irq = IRQBASE+2,
126 .flags = ASYNC_BOOT_AUTOCONF,
128 #endif
129 #ifdef MCFUART_BASE4
130 { /* ttyS3 */
131 .magic = 0,
132 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE4),
133 .irq = IRQBASE+3,
134 .flags = ASYNC_BOOT_AUTOCONF,
136 #endif
140 #define NR_PORTS (sizeof(mcfrs_table) / sizeof(struct mcf_serial))
143 * This is used to figure out the divisor speeds and the timeouts.
145 static int mcfrs_baud_table[] = {
146 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
147 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0
149 #define MCFRS_BAUD_TABLE_SIZE \
150 (sizeof(mcfrs_baud_table)/sizeof(mcfrs_baud_table[0]))
153 #ifdef CONFIG_MAGIC_SYSRQ
155 * Magic system request keys. Used for debugging...
157 extern int magic_sysrq_key(int ch);
158 #endif
162 * Forware declarations...
164 static void mcfrs_change_speed(struct mcf_serial *info);
165 static void mcfrs_wait_until_sent(struct tty_struct *tty, int timeout);
168 static inline int serial_paranoia_check(struct mcf_serial *info,
169 char *name, const char *routine)
171 #ifdef SERIAL_PARANOIA_CHECK
172 static const char badmagic[] =
173 "MCFRS(warning): bad magic number for serial struct %s in %s\n";
174 static const char badinfo[] =
175 "MCFRS(warning): null mcf_serial for %s in %s\n";
177 if (!info) {
178 printk(badinfo, name, routine);
179 return 1;
181 if (info->magic != SERIAL_MAGIC) {
182 printk(badmagic, name, routine);
183 return 1;
185 #endif
186 return 0;
190 * Sets or clears DTR and RTS on the requested line.
192 static void mcfrs_setsignals(struct mcf_serial *info, int dtr, int rts)
194 volatile unsigned char *uartp;
195 unsigned long flags;
197 #if 0
198 printk("%s(%d): mcfrs_setsignals(info=%x,dtr=%d,rts=%d)\n",
199 __FILE__, __LINE__, info, dtr, rts);
200 #endif
202 local_irq_save(flags);
203 if (dtr >= 0) {
204 #ifdef MCFPP_DTR0
205 if (info->line)
206 mcf_setppdata(MCFPP_DTR1, (dtr ? 0 : MCFPP_DTR1));
207 else
208 mcf_setppdata(MCFPP_DTR0, (dtr ? 0 : MCFPP_DTR0));
209 #endif
211 if (rts >= 0) {
212 uartp = info->addr;
213 if (rts) {
214 info->sigs |= TIOCM_RTS;
215 uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
216 } else {
217 info->sigs &= ~TIOCM_RTS;
218 uartp[MCFUART_UOP0] = MCFUART_UOP_RTS;
221 local_irq_restore(flags);
222 return;
226 * Gets values of serial signals.
228 static int mcfrs_getsignals(struct mcf_serial *info)
230 volatile unsigned char *uartp;
231 unsigned long flags;
232 int sigs;
233 #if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
234 unsigned short ppdata;
235 #endif
237 #if 0
238 printk("%s(%d): mcfrs_getsignals(info=%x)\n", __FILE__, __LINE__);
239 #endif
241 local_irq_save(flags);
242 uartp = info->addr;
243 sigs = (uartp[MCFUART_UIPR] & MCFUART_UIPR_CTS) ? 0 : TIOCM_CTS;
244 sigs |= (info->sigs & TIOCM_RTS);
246 #ifdef MCFPP_DCD0
248 unsigned int ppdata;
249 ppdata = mcf_getppdata();
250 if (info->line == 0) {
251 sigs |= (ppdata & MCFPP_DCD0) ? 0 : TIOCM_CD;
252 sigs |= (ppdata & MCFPP_DTR0) ? 0 : TIOCM_DTR;
253 } else if (info->line == 1) {
254 sigs |= (ppdata & MCFPP_DCD1) ? 0 : TIOCM_CD;
255 sigs |= (ppdata & MCFPP_DTR1) ? 0 : TIOCM_DTR;
258 #endif
260 local_irq_restore(flags);
261 return(sigs);
265 * ------------------------------------------------------------
266 * mcfrs_stop() and mcfrs_start()
268 * This routines are called before setting or resetting tty->stopped.
269 * They enable or disable transmitter interrupts, as necessary.
270 * ------------------------------------------------------------
272 static void mcfrs_stop(struct tty_struct *tty)
274 volatile unsigned char *uartp;
275 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
276 unsigned long flags;
278 if (serial_paranoia_check(info, tty->name, "mcfrs_stop"))
279 return;
281 local_irq_save(flags);
282 uartp = info->addr;
283 info->imr &= ~MCFUART_UIR_TXREADY;
284 uartp[MCFUART_UIMR] = info->imr;
285 local_irq_restore(flags);
288 static void mcfrs_start(struct tty_struct *tty)
290 volatile unsigned char *uartp;
291 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
292 unsigned long flags;
294 if (serial_paranoia_check(info, tty->name, "mcfrs_start"))
295 return;
297 local_irq_save(flags);
298 if (info->xmit_cnt && info->xmit_buf) {
299 uartp = info->addr;
300 info->imr |= MCFUART_UIR_TXREADY;
301 uartp[MCFUART_UIMR] = info->imr;
303 local_irq_restore(flags);
307 * ----------------------------------------------------------------------
309 * Here starts the interrupt handling routines. All of the following
310 * subroutines are declared as inline and are folded into
311 * mcfrs_interrupt(). They were separated out for readability's sake.
313 * Note: mcfrs_interrupt() is a "fast" interrupt, which means that it
314 * runs with interrupts turned off. People who may want to modify
315 * mcfrs_interrupt() should try to keep the interrupt handler as fast as
316 * possible. After you are done making modifications, it is not a bad
317 * idea to do:
319 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
321 * and look at the resulting assemble code in serial.s.
323 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
324 * -----------------------------------------------------------------------
327 static inline void receive_chars(struct mcf_serial *info)
329 volatile unsigned char *uartp;
330 struct tty_struct *tty = info->tty;
331 unsigned char status, ch, flag;
333 if (!tty)
334 return;
336 uartp = info->addr;
338 while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
339 ch = uartp[MCFUART_URB];
340 info->stats.rx++;
342 #ifdef CONFIG_MAGIC_SYSRQ
343 if (mcfrs_console_inited && (info->line == mcfrs_console_port)) {
344 if (magic_sysrq_key(ch))
345 continue;
347 #endif
349 flag = TTY_NORMAL;
350 if (status & MCFUART_USR_RXERR) {
351 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
352 if (status & MCFUART_USR_RXBREAK) {
353 info->stats.rxbreak++;
354 flag = TTY_BREAK;
355 } else if (status & MCFUART_USR_RXPARITY) {
356 info->stats.rxparity++;
357 flag = TTY_PARITY;
358 } else if (status & MCFUART_USR_RXOVERRUN) {
359 info->stats.rxoverrun++;
360 flag = TTY_OVERRUN;
361 } else if (status & MCFUART_USR_RXFRAMING) {
362 info->stats.rxframing++;
363 flag = TTY_FRAME;
366 tty_insert_flip_char(tty, ch, flag);
368 tty_schedule_flip(tty);
369 return;
372 static inline void transmit_chars(struct mcf_serial *info)
374 volatile unsigned char *uartp;
376 uartp = info->addr;
378 if (info->x_char) {
379 /* Send special char - probably flow control */
380 uartp[MCFUART_UTB] = info->x_char;
381 info->x_char = 0;
382 info->stats.tx++;
385 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
386 info->imr &= ~MCFUART_UIR_TXREADY;
387 uartp[MCFUART_UIMR] = info->imr;
388 return;
391 while (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) {
392 uartp[MCFUART_UTB] = info->xmit_buf[info->xmit_tail++];
393 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
394 info->stats.tx++;
395 if (--info->xmit_cnt <= 0)
396 break;
399 if (info->xmit_cnt < WAKEUP_CHARS)
400 schedule_work(&info->tqueue);
401 return;
405 * This is the serial driver's generic interrupt routine
407 irqreturn_t mcfrs_interrupt(int irq, void *dev_id)
409 struct mcf_serial *info;
410 unsigned char isr;
412 info = &mcfrs_table[(irq - IRQBASE)];
413 isr = info->addr[MCFUART_UISR] & info->imr;
415 if (isr & MCFUART_UIR_RXREADY)
416 receive_chars(info);
417 if (isr & MCFUART_UIR_TXREADY)
418 transmit_chars(info);
419 return IRQ_HANDLED;
423 * -------------------------------------------------------------------
424 * Here ends the serial interrupt routines.
425 * -------------------------------------------------------------------
428 static void mcfrs_offintr(struct work_struct *work)
430 struct mcf_serial *info = container_of(work, struct mcf_serial, tqueue);
431 struct tty_struct *tty = info->tty;
433 if (tty)
434 tty_wakeup(tty);
439 * Change of state on a DCD line.
441 void mcfrs_modem_change(struct mcf_serial *info, int dcd)
443 if (info->count == 0)
444 return;
446 if (info->flags & ASYNC_CHECK_CD) {
447 if (dcd)
448 wake_up_interruptible(&info->open_wait);
449 else
450 schedule_work(&info->tqueue_hangup);
455 #ifdef MCFPP_DCD0
457 unsigned short mcfrs_ppstatus;
460 * This subroutine is called when the RS_TIMER goes off. It is used
461 * to monitor the state of the DCD lines - since they have no edge
462 * sensors and interrupt generators.
464 static void mcfrs_timer(void)
466 unsigned int ppstatus, dcdval, i;
468 ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
470 if (ppstatus != mcfrs_ppstatus) {
471 for (i = 0; (i < 2); i++) {
472 dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
473 if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
474 mcfrs_modem_change(&mcfrs_table[i],
475 ((ppstatus & dcdval) ? 0 : 1));
479 mcfrs_ppstatus = ppstatus;
481 /* Re-arm timer */
482 mcfrs_timer_struct.expires = jiffies + HZ/25;
483 add_timer(&mcfrs_timer_struct);
486 #endif /* MCFPP_DCD0 */
490 * This routine is called from the scheduler tqueue when the interrupt
491 * routine has signalled that a hangup has occurred. The path of
492 * hangup processing is:
494 * serial interrupt routine -> (scheduler tqueue) ->
495 * do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
498 static void do_serial_hangup(struct work_struct *work)
500 struct mcf_serial *info = container_of(work, struct mcf_serial, tqueue_hangup);
501 struct tty_struct *tty = info->tty;
503 if (tty)
504 tty_hangup(tty);
507 static int startup(struct mcf_serial * info)
509 volatile unsigned char *uartp;
510 unsigned long flags;
512 if (info->flags & ASYNC_INITIALIZED)
513 return 0;
515 if (!info->xmit_buf) {
516 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
517 if (!info->xmit_buf)
518 return -ENOMEM;
521 local_irq_save(flags);
523 #ifdef SERIAL_DEBUG_OPEN
524 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
525 #endif
528 * Reset UART, get it into known state...
530 uartp = info->addr;
531 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
532 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
533 mcfrs_setsignals(info, 1, 1);
535 if (info->tty)
536 clear_bit(TTY_IO_ERROR, &info->tty->flags);
537 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
540 * and set the speed of the serial port
542 mcfrs_change_speed(info);
545 * Lastly enable the UART transmitter and receiver, and
546 * interrupt enables.
548 info->imr = MCFUART_UIR_RXREADY;
549 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
550 uartp[MCFUART_UIMR] = info->imr;
552 info->flags |= ASYNC_INITIALIZED;
553 local_irq_restore(flags);
554 return 0;
558 * This routine will shutdown a serial port; interrupts are disabled, and
559 * DTR is dropped if the hangup on close termio flag is on.
561 static void shutdown(struct mcf_serial * info)
563 volatile unsigned char *uartp;
564 unsigned long flags;
566 if (!(info->flags & ASYNC_INITIALIZED))
567 return;
569 #ifdef SERIAL_DEBUG_OPEN
570 printk("Shutting down serial port %d (irq %d)....\n", info->line,
571 info->irq);
572 #endif
574 local_irq_save(flags);
576 uartp = info->addr;
577 uartp[MCFUART_UIMR] = 0; /* mask all interrupts */
578 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
579 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
581 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
582 mcfrs_setsignals(info, 0, 0);
584 if (info->xmit_buf) {
585 free_page((unsigned long) info->xmit_buf);
586 info->xmit_buf = 0;
589 if (info->tty)
590 set_bit(TTY_IO_ERROR, &info->tty->flags);
592 info->flags &= ~ASYNC_INITIALIZED;
593 local_irq_restore(flags);
598 * This routine is called to set the UART divisor registers to match
599 * the specified baud rate for a serial port.
601 static void mcfrs_change_speed(struct mcf_serial *info)
603 volatile unsigned char *uartp;
604 unsigned int baudclk, cflag;
605 unsigned long flags;
606 unsigned char mr1, mr2;
607 int i;
608 #ifdef CONFIG_M5272
609 unsigned int fraction;
610 #endif
612 if (!info->tty || !info->tty->termios)
613 return;
614 cflag = info->tty->termios->c_cflag;
615 if (info->addr == 0)
616 return;
618 #if 0
619 printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
620 #endif
622 i = cflag & CBAUD;
623 if (i & CBAUDEX) {
624 i &= ~CBAUDEX;
625 if (i < 1 || i > 4)
626 info->tty->termios->c_cflag &= ~CBAUDEX;
627 else
628 i += 15;
630 if (i == 0) {
631 mcfrs_setsignals(info, 0, -1);
632 return;
635 /* compute the baudrate clock */
636 #ifdef CONFIG_M5272
638 * For the MCF5272, also compute the baudrate fraction.
640 baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
641 fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
642 fraction *= 16;
643 fraction /= (32 * mcfrs_baud_table[i]);
644 #else
645 baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
646 #endif
648 info->baud = mcfrs_baud_table[i];
650 mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
651 mr2 = 0;
653 switch (cflag & CSIZE) {
654 case CS5: mr1 |= MCFUART_MR1_CS5; break;
655 case CS6: mr1 |= MCFUART_MR1_CS6; break;
656 case CS7: mr1 |= MCFUART_MR1_CS7; break;
657 case CS8:
658 default: mr1 |= MCFUART_MR1_CS8; break;
661 if (cflag & PARENB) {
662 if (cflag & CMSPAR) {
663 if (cflag & PARODD)
664 mr1 |= MCFUART_MR1_PARITYMARK;
665 else
666 mr1 |= MCFUART_MR1_PARITYSPACE;
667 } else {
668 if (cflag & PARODD)
669 mr1 |= MCFUART_MR1_PARITYODD;
670 else
671 mr1 |= MCFUART_MR1_PARITYEVEN;
673 } else {
674 mr1 |= MCFUART_MR1_PARITYNONE;
677 if (cflag & CSTOPB)
678 mr2 |= MCFUART_MR2_STOP2;
679 else
680 mr2 |= MCFUART_MR2_STOP1;
682 if (cflag & CRTSCTS) {
683 mr1 |= MCFUART_MR1_RXRTS;
684 mr2 |= MCFUART_MR2_TXCTS;
687 if (cflag & CLOCAL)
688 info->flags &= ~ASYNC_CHECK_CD;
689 else
690 info->flags |= ASYNC_CHECK_CD;
692 uartp = info->addr;
694 local_irq_save(flags);
695 #if 0
696 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
697 mr1, mr2, baudclk);
698 #endif
700 Note: pg 12-16 of MCF5206e User's Manual states that a
701 software reset should be performed prior to changing
702 UMR1,2, UCSR, UACR, bit 7
704 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
705 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
706 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
707 uartp[MCFUART_UMR] = mr1;
708 uartp[MCFUART_UMR] = mr2;
709 uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8; /* set msb byte */
710 uartp[MCFUART_UBG2] = (baudclk & 0xff); /* set lsb byte */
711 #ifdef CONFIG_M5272
712 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
713 #endif
714 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
715 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
716 mcfrs_setsignals(info, 1, -1);
717 local_irq_restore(flags);
718 return;
721 static void mcfrs_flush_chars(struct tty_struct *tty)
723 volatile unsigned char *uartp;
724 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
725 unsigned long flags;
727 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
728 return;
730 uartp = (volatile unsigned char *) info->addr;
733 * re-enable receiver interrupt
735 local_irq_save(flags);
736 if ((!(info->imr & MCFUART_UIR_RXREADY)) &&
737 (info->flags & ASYNC_INITIALIZED) ) {
738 info->imr |= MCFUART_UIR_RXREADY;
739 uartp[MCFUART_UIMR] = info->imr;
741 local_irq_restore(flags);
743 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
744 !info->xmit_buf)
745 return;
747 /* Enable transmitter */
748 local_irq_save(flags);
749 info->imr |= MCFUART_UIR_TXREADY;
750 uartp[MCFUART_UIMR] = info->imr;
751 local_irq_restore(flags);
754 static int mcfrs_write(struct tty_struct * tty,
755 const unsigned char *buf, int count)
757 volatile unsigned char *uartp;
758 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
759 unsigned long flags;
760 int c, total = 0;
762 #if 0
763 printk("%s(%d): mcfrs_write(tty=%x,buf=%x,count=%d)\n",
764 __FILE__, __LINE__, (int)tty, (int)buf, count);
765 #endif
767 if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
768 return 0;
770 if (!tty || !info->xmit_buf)
771 return 0;
773 local_save_flags(flags);
774 while (1) {
775 local_irq_disable();
776 c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
777 ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
778 local_irq_restore(flags);
780 if (c <= 0)
781 break;
783 memcpy(info->xmit_buf + info->xmit_head, buf, c);
785 local_irq_disable();
786 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
787 info->xmit_cnt += c;
788 local_irq_restore(flags);
790 buf += c;
791 count -= c;
792 total += c;
795 local_irq_disable();
796 uartp = info->addr;
797 info->imr |= MCFUART_UIR_TXREADY;
798 uartp[MCFUART_UIMR] = info->imr;
799 local_irq_restore(flags);
801 return total;
804 static int mcfrs_write_room(struct tty_struct *tty)
806 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
807 int ret;
809 if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
810 return 0;
811 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
812 if (ret < 0)
813 ret = 0;
814 return ret;
817 static int mcfrs_chars_in_buffer(struct tty_struct *tty)
819 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
821 if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
822 return 0;
823 return info->xmit_cnt;
826 static void mcfrs_flush_buffer(struct tty_struct *tty)
828 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
829 unsigned long flags;
831 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
832 return;
834 local_irq_save(flags);
835 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
836 local_irq_restore(flags);
838 tty_wakeup(tty);
842 * ------------------------------------------------------------
843 * mcfrs_throttle()
845 * This routine is called by the upper-layer tty layer to signal that
846 * incoming characters should be throttled.
847 * ------------------------------------------------------------
849 static void mcfrs_throttle(struct tty_struct * tty)
851 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
852 #ifdef SERIAL_DEBUG_THROTTLE
853 char buf[64];
855 printk("throttle %s: %d....\n", tty_name(tty, buf),
856 tty->ldisc.chars_in_buffer(tty));
857 #endif
859 if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
860 return;
862 if (I_IXOFF(tty))
863 info->x_char = STOP_CHAR(tty);
865 /* Turn off RTS line (do this atomic) */
868 static void mcfrs_unthrottle(struct tty_struct * tty)
870 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
871 #ifdef SERIAL_DEBUG_THROTTLE
872 char buf[64];
874 printk("unthrottle %s: %d....\n", tty_name(tty, buf),
875 tty->ldisc.chars_in_buffer(tty));
876 #endif
878 if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
879 return;
881 if (I_IXOFF(tty)) {
882 if (info->x_char)
883 info->x_char = 0;
884 else
885 info->x_char = START_CHAR(tty);
888 /* Assert RTS line (do this atomic) */
892 * ------------------------------------------------------------
893 * mcfrs_ioctl() and friends
894 * ------------------------------------------------------------
897 static int get_serial_info(struct mcf_serial * info,
898 struct serial_struct * retinfo)
900 struct serial_struct tmp;
902 if (!retinfo)
903 return -EFAULT;
904 memset(&tmp, 0, sizeof(tmp));
905 tmp.type = info->type;
906 tmp.line = info->line;
907 tmp.port = (unsigned int) info->addr;
908 tmp.irq = info->irq;
909 tmp.flags = info->flags;
910 tmp.baud_base = info->baud_base;
911 tmp.close_delay = info->close_delay;
912 tmp.closing_wait = info->closing_wait;
913 tmp.custom_divisor = info->custom_divisor;
914 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
917 static int set_serial_info(struct mcf_serial * info,
918 struct serial_struct * new_info)
920 struct serial_struct new_serial;
921 struct mcf_serial old_info;
922 int retval = 0;
924 if (!new_info)
925 return -EFAULT;
926 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
927 return -EFAULT;
928 old_info = *info;
930 if (!capable(CAP_SYS_ADMIN)) {
931 if ((new_serial.baud_base != info->baud_base) ||
932 (new_serial.type != info->type) ||
933 (new_serial.close_delay != info->close_delay) ||
934 ((new_serial.flags & ~ASYNC_USR_MASK) !=
935 (info->flags & ~ASYNC_USR_MASK)))
936 return -EPERM;
937 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
938 (new_serial.flags & ASYNC_USR_MASK));
939 info->custom_divisor = new_serial.custom_divisor;
940 goto check_and_exit;
943 if (info->count > 1)
944 return -EBUSY;
947 * OK, past this point, all the error checking has been done.
948 * At this point, we start making changes.....
951 info->baud_base = new_serial.baud_base;
952 info->flags = ((info->flags & ~ASYNC_FLAGS) |
953 (new_serial.flags & ASYNC_FLAGS));
954 info->type = new_serial.type;
955 info->close_delay = new_serial.close_delay;
956 info->closing_wait = new_serial.closing_wait;
958 check_and_exit:
959 retval = startup(info);
960 return retval;
964 * get_lsr_info - get line status register info
966 * Purpose: Let user call ioctl() to get info when the UART physically
967 * is emptied. On bus types like RS485, the transmitter must
968 * release the bus after transmitting. This must be done when
969 * the transmit shift register is empty, not be done when the
970 * transmit holding register is empty. This functionality
971 * allows an RS485 driver to be written in user space.
973 static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
975 volatile unsigned char *uartp;
976 unsigned long flags;
977 unsigned char status;
979 local_irq_save(flags);
980 uartp = info->addr;
981 status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
982 local_irq_restore(flags);
984 return put_user(status,value);
988 * This routine sends a break character out the serial port.
990 static void send_break( struct mcf_serial * info, int duration)
992 volatile unsigned char *uartp;
993 unsigned long flags;
995 if (!info->addr)
996 return;
997 set_current_state(TASK_INTERRUPTIBLE);
998 uartp = info->addr;
1000 local_irq_save(flags);
1001 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
1002 schedule_timeout(duration);
1003 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
1004 local_irq_restore(flags);
1007 static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
1009 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1011 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1012 return -ENODEV;
1013 if (tty->flags & (1 << TTY_IO_ERROR))
1014 return -EIO;
1016 return mcfrs_getsignals(info);
1019 static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
1020 unsigned int set, unsigned int clear)
1022 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1023 int rts = -1, dtr = -1;
1025 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1026 return -ENODEV;
1027 if (tty->flags & (1 << TTY_IO_ERROR))
1028 return -EIO;
1030 if (set & TIOCM_RTS)
1031 rts = 1;
1032 if (set & TIOCM_DTR)
1033 dtr = 1;
1034 if (clear & TIOCM_RTS)
1035 rts = 0;
1036 if (clear & TIOCM_DTR)
1037 dtr = 0;
1039 mcfrs_setsignals(info, dtr, rts);
1041 return 0;
1044 static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
1045 unsigned int cmd, unsigned long arg)
1047 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1048 int retval, error;
1050 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1051 return -ENODEV;
1053 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1054 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1055 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1056 if (tty->flags & (1 << TTY_IO_ERROR))
1057 return -EIO;
1060 switch (cmd) {
1061 case TCSBRK: /* SVID version: non-zero arg --> no break */
1062 retval = tty_check_change(tty);
1063 if (retval)
1064 return retval;
1065 tty_wait_until_sent(tty, 0);
1066 if (!arg)
1067 send_break(info, HZ/4); /* 1/4 second */
1068 return 0;
1069 case TCSBRKP: /* support for POSIX tcsendbreak() */
1070 retval = tty_check_change(tty);
1071 if (retval)
1072 return retval;
1073 tty_wait_until_sent(tty, 0);
1074 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1075 return 0;
1076 case TIOCGSERIAL:
1077 if (access_ok(VERIFY_WRITE, (void *) arg,
1078 sizeof(struct serial_struct)))
1079 return get_serial_info(info,
1080 (struct serial_struct *) arg);
1081 return -EFAULT;
1082 case TIOCSSERIAL:
1083 return set_serial_info(info,
1084 (struct serial_struct *) arg);
1085 case TIOCSERGETLSR: /* Get line status register */
1086 if (access_ok(VERIFY_WRITE, (void *) arg,
1087 sizeof(unsigned int)))
1088 return get_lsr_info(info, (unsigned int *) arg);
1089 return -EFAULT;
1090 case TIOCSERGSTRUCT:
1091 error = copy_to_user((struct mcf_serial *) arg,
1092 info, sizeof(struct mcf_serial));
1093 if (error)
1094 return -EFAULT;
1095 return 0;
1097 #ifdef TIOCSET422
1098 case TIOCSET422: {
1099 unsigned int val;
1100 get_user(val, (unsigned int *) arg);
1101 mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1102 break;
1104 case TIOCGET422: {
1105 unsigned int val;
1106 val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1107 put_user(val, (unsigned int *) arg);
1108 break;
1110 #endif
1112 default:
1113 return -ENOIOCTLCMD;
1115 return 0;
1118 static void mcfrs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1120 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1122 if (tty->termios->c_cflag == old_termios->c_cflag)
1123 return;
1125 mcfrs_change_speed(info);
1127 if ((old_termios->c_cflag & CRTSCTS) &&
1128 !(tty->termios->c_cflag & CRTSCTS)) {
1129 tty->hw_stopped = 0;
1130 mcfrs_setsignals(info, -1, 1);
1131 #if 0
1132 mcfrs_start(tty);
1133 #endif
1138 * ------------------------------------------------------------
1139 * mcfrs_close()
1141 * This routine is called when the serial port gets closed. First, we
1142 * wait for the last remaining data to be sent. Then, we unlink its
1143 * S structure from the interrupt chain if necessary, and we free
1144 * that IRQ if nothing is left in the chain.
1145 * ------------------------------------------------------------
1147 static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1149 volatile unsigned char *uartp;
1150 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1151 unsigned long flags;
1153 if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1154 return;
1156 local_irq_save(flags);
1158 if (tty_hung_up_p(filp)) {
1159 local_irq_restore(flags);
1160 return;
1163 #ifdef SERIAL_DEBUG_OPEN
1164 printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1165 #endif
1166 if ((tty->count == 1) && (info->count != 1)) {
1168 * Uh, oh. tty->count is 1, which means that the tty
1169 * structure will be freed. Info->count should always
1170 * be one in these conditions. If it's greater than
1171 * one, we've got real problems, since it means the
1172 * serial port won't be shutdown.
1174 printk("MCFRS: bad serial port count; tty->count is 1, "
1175 "info->count is %d\n", info->count);
1176 info->count = 1;
1178 if (--info->count < 0) {
1179 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1180 info->line, info->count);
1181 info->count = 0;
1183 if (info->count) {
1184 local_irq_restore(flags);
1185 return;
1187 info->flags |= ASYNC_CLOSING;
1190 * Now we wait for the transmit buffer to clear; and we notify
1191 * the line discipline to only process XON/XOFF characters.
1193 tty->closing = 1;
1194 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1195 tty_wait_until_sent(tty, info->closing_wait);
1198 * At this point we stop accepting input. To do this, we
1199 * disable the receive line status interrupts, and tell the
1200 * interrupt driver to stop checking the data ready bit in the
1201 * line status register.
1203 info->imr &= ~MCFUART_UIR_RXREADY;
1204 uartp = info->addr;
1205 uartp[MCFUART_UIMR] = info->imr;
1207 #if 0
1208 /* FIXME: do we need to keep this enabled for console?? */
1209 if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
1210 /* Do not disable the UART */ ;
1211 } else
1212 #endif
1213 shutdown(info);
1214 mcfrs_flush_buffer(tty);
1215 tty_ldisc_flush(tty);
1217 tty->closing = 0;
1218 info->event = 0;
1219 info->tty = 0;
1220 #if 0
1221 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1222 if (tty->ldisc.close)
1223 (tty->ldisc.close)(tty);
1224 tty->ldisc = ldiscs[N_TTY];
1225 tty->termios->c_line = N_TTY;
1226 if (tty->ldisc.open)
1227 (tty->ldisc.open)(tty);
1229 #endif
1230 if (info->blocked_open) {
1231 if (info->close_delay) {
1232 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1234 wake_up_interruptible(&info->open_wait);
1236 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1237 wake_up_interruptible(&info->close_wait);
1238 local_irq_restore(flags);
1242 * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1244 static void
1245 mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1247 #ifdef CONFIG_M5272
1248 #define MCF5272_FIFO_SIZE 25 /* fifo size + shift reg */
1250 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1251 volatile unsigned char *uartp;
1252 unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1254 if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1255 return;
1257 orig_jiffies = jiffies;
1260 * Set the check interval to be 1/5 of the approximate time
1261 * to send the entire fifo, and make it at least 1. The check
1262 * interval should also be less than the timeout.
1264 * Note: we have to use pretty tight timings here to satisfy
1265 * the NIST-PCTS.
1267 lock_kernel();
1269 fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1270 char_time = fifo_time / 5;
1271 if (char_time == 0)
1272 char_time = 1;
1273 if (timeout && timeout < char_time)
1274 char_time = timeout;
1277 * Clamp the timeout period at 2 * the time to empty the
1278 * fifo. Just to be safe, set the minimum at .5 seconds.
1280 fifo_time *= 2;
1281 if (fifo_time < (HZ/2))
1282 fifo_time = HZ/2;
1283 if (!timeout || timeout > fifo_time)
1284 timeout = fifo_time;
1287 * Account for the number of bytes in the UART
1288 * transmitter FIFO plus any byte being shifted out.
1290 uartp = (volatile unsigned char *) info->addr;
1291 for (;;) {
1292 fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1293 if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1294 MCFUART_USR_TXEMPTY)) ==
1295 MCFUART_USR_TXREADY)
1296 fifo_cnt++;
1297 if (fifo_cnt == 0)
1298 break;
1299 msleep_interruptible(jiffies_to_msecs(char_time));
1300 if (signal_pending(current))
1301 break;
1302 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1303 break;
1305 unlock_kernel();
1306 #else
1308 * For the other coldfire models, assume all data has been sent
1310 #endif
1314 * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1316 void mcfrs_hangup(struct tty_struct *tty)
1318 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1320 if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
1321 return;
1323 mcfrs_flush_buffer(tty);
1324 shutdown(info);
1325 info->event = 0;
1326 info->count = 0;
1327 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1328 info->tty = 0;
1329 wake_up_interruptible(&info->open_wait);
1333 * ------------------------------------------------------------
1334 * mcfrs_open() and friends
1335 * ------------------------------------------------------------
1337 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1338 struct mcf_serial *info)
1340 DECLARE_WAITQUEUE(wait, current);
1341 int retval;
1342 int do_clocal = 0;
1345 * If the device is in the middle of being closed, then block
1346 * until it's done, and then try again.
1348 if (info->flags & ASYNC_CLOSING) {
1349 interruptible_sleep_on(&info->close_wait);
1350 #ifdef SERIAL_DO_RESTART
1351 if (info->flags & ASYNC_HUP_NOTIFY)
1352 return -EAGAIN;
1353 else
1354 return -ERESTARTSYS;
1355 #else
1356 return -EAGAIN;
1357 #endif
1361 * If non-blocking mode is set, or the port is not enabled,
1362 * then make the check up front and then exit.
1364 if ((filp->f_flags & O_NONBLOCK) ||
1365 (tty->flags & (1 << TTY_IO_ERROR))) {
1366 info->flags |= ASYNC_NORMAL_ACTIVE;
1367 return 0;
1370 if (tty->termios->c_cflag & CLOCAL)
1371 do_clocal = 1;
1374 * Block waiting for the carrier detect and the line to become
1375 * free (i.e., not in use by the callout). While we are in
1376 * this loop, info->count is dropped by one, so that
1377 * mcfrs_close() knows when to free things. We restore it upon
1378 * exit, either normal or abnormal.
1380 retval = 0;
1381 add_wait_queue(&info->open_wait, &wait);
1382 #ifdef SERIAL_DEBUG_OPEN
1383 printk("block_til_ready before block: ttyS%d, count = %d\n",
1384 info->line, info->count);
1385 #endif
1386 info->count--;
1387 info->blocked_open++;
1388 while (1) {
1389 local_irq_disable();
1390 mcfrs_setsignals(info, 1, 1);
1391 local_irq_enable();
1392 current->state = TASK_INTERRUPTIBLE;
1393 if (tty_hung_up_p(filp) ||
1394 !(info->flags & ASYNC_INITIALIZED)) {
1395 #ifdef SERIAL_DO_RESTART
1396 if (info->flags & ASYNC_HUP_NOTIFY)
1397 retval = -EAGAIN;
1398 else
1399 retval = -ERESTARTSYS;
1400 #else
1401 retval = -EAGAIN;
1402 #endif
1403 break;
1405 if (!(info->flags & ASYNC_CLOSING) &&
1406 (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1407 break;
1408 if (signal_pending(current)) {
1409 retval = -ERESTARTSYS;
1410 break;
1412 #ifdef SERIAL_DEBUG_OPEN
1413 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1414 info->line, info->count);
1415 #endif
1416 schedule();
1418 current->state = TASK_RUNNING;
1419 remove_wait_queue(&info->open_wait, &wait);
1420 if (!tty_hung_up_p(filp))
1421 info->count++;
1422 info->blocked_open--;
1423 #ifdef SERIAL_DEBUG_OPEN
1424 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1425 info->line, info->count);
1426 #endif
1427 if (retval)
1428 return retval;
1429 info->flags |= ASYNC_NORMAL_ACTIVE;
1430 return 0;
1434 * This routine is called whenever a serial port is opened. It
1435 * enables interrupts for a serial port, linking in its structure into
1436 * the IRQ chain. It also performs the serial-specific
1437 * initialization for the tty structure.
1439 int mcfrs_open(struct tty_struct *tty, struct file * filp)
1441 struct mcf_serial *info;
1442 int retval, line;
1444 line = tty->index;
1445 if ((line < 0) || (line >= NR_PORTS))
1446 return -ENODEV;
1447 info = mcfrs_table + line;
1448 if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1449 return -ENODEV;
1450 #ifdef SERIAL_DEBUG_OPEN
1451 printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1452 #endif
1453 info->count++;
1454 tty->driver_data = info;
1455 info->tty = tty;
1458 * Start up serial port
1460 retval = startup(info);
1461 if (retval)
1462 return retval;
1464 retval = block_til_ready(tty, filp, info);
1465 if (retval) {
1466 #ifdef SERIAL_DEBUG_OPEN
1467 printk("mcfrs_open returning after block_til_ready with %d\n",
1468 retval);
1469 #endif
1470 return retval;
1473 #ifdef SERIAL_DEBUG_OPEN
1474 printk("mcfrs_open %s successful...\n", tty->name);
1475 #endif
1476 return 0;
1480 * Based on the line number set up the internal interrupt stuff.
1482 static void mcfrs_irqinit(struct mcf_serial *info)
1484 #if defined(CONFIG_M5272)
1485 volatile unsigned long *icrp;
1486 volatile unsigned long *portp;
1487 volatile unsigned char *uartp;
1489 uartp = info->addr;
1490 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1492 switch (info->line) {
1493 case 0:
1494 *icrp = 0xe0000000;
1495 break;
1496 case 1:
1497 *icrp = 0x0e000000;
1498 break;
1499 default:
1500 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1501 info->line);
1502 return;
1505 /* Enable the output lines for the serial ports */
1506 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
1507 *portp = (*portp & ~0x000000ff) | 0x00000055;
1508 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
1509 *portp = (*portp & ~0x000003fc) | 0x000002a8;
1510 #elif defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x)
1511 volatile unsigned char *icrp, *uartp;
1512 volatile unsigned long *imrp;
1514 uartp = info->addr;
1516 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1517 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1518 *icrp = 0x30 + info->line; /* level 6, line based priority */
1520 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1521 MCFINTC_IMRL);
1522 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1523 #if defined(CONFIG_M527x)
1526 * External Pin Mask Setting & Enable External Pin for Interface
1527 * mrcbis@aliceposta.it
1529 u16 *serpin_enable_mask;
1530 serpin_enable_mask = (u16 *) (MCF_IPSBAR + MCF_GPIO_PAR_UART);
1531 if (info->line == 0)
1532 *serpin_enable_mask |= UART0_ENABLE_MASK;
1533 else if (info->line == 1)
1534 *serpin_enable_mask |= UART1_ENABLE_MASK;
1535 else if (info->line == 2)
1536 *serpin_enable_mask |= UART2_ENABLE_MASK;
1538 #endif
1539 #if defined(CONFIG_M528x)
1540 /* make sure PUAPAR is set for UART0 and UART1 */
1541 if (info->line < 2) {
1542 volatile unsigned char *portp = (volatile unsigned char *) (MCF_MBAR + MCF5282_GPIO_PUAPAR);
1543 *portp |= (0x03 << (info->line * 2));
1545 #endif
1546 #elif defined(CONFIG_M520x)
1547 volatile unsigned char *icrp, *uartp;
1548 volatile unsigned long *imrp;
1550 uartp = info->addr;
1552 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1553 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1554 *icrp = 0x03;
1556 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1557 MCFINTC_IMRL);
1558 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1559 if (info->line < 2) {
1560 unsigned short *uart_par;
1561 uart_par = (unsigned short *)(MCF_IPSBAR + MCF_GPIO_PAR_UART);
1562 if (info->line == 0)
1563 *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD0
1564 | MCF_GPIO_PAR_UART_PAR_URXD0;
1565 else if (info->line == 1)
1566 *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD1
1567 | MCF_GPIO_PAR_UART_PAR_URXD1;
1568 } else if (info->line == 2) {
1569 unsigned char *feci2c_par;
1570 feci2c_par = (unsigned char *)(MCF_IPSBAR + MCF_GPIO_PAR_FECI2C);
1571 *feci2c_par &= ~0x0F;
1572 *feci2c_par |= MCF_GPIO_PAR_FECI2C_PAR_SCL_UTXD2
1573 | MCF_GPIO_PAR_FECI2C_PAR_SDA_URXD2;
1575 #elif defined(CONFIG_M532x)
1576 volatile unsigned char *uartp;
1577 uartp = info->addr;
1578 switch (info->line) {
1579 case 0:
1580 MCF_INTC0_ICR26 = 0x3;
1581 MCF_INTC0_CIMR = 26;
1582 /* GPIO initialization */
1583 MCF_GPIO_PAR_UART |= 0x000F;
1584 break;
1585 case 1:
1586 MCF_INTC0_ICR27 = 0x3;
1587 MCF_INTC0_CIMR = 27;
1588 /* GPIO initialization */
1589 MCF_GPIO_PAR_UART |= 0x0FF0;
1590 break;
1591 case 2:
1592 MCF_INTC0_ICR28 = 0x3;
1593 MCF_INTC0_CIMR = 28;
1594 /* GPIOs also must be initalized, depends on board */
1595 break;
1597 #else
1598 volatile unsigned char *icrp, *uartp;
1600 switch (info->line) {
1601 case 0:
1602 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1603 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1604 MCFSIM_ICR_PRI1;
1605 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1606 break;
1607 case 1:
1608 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1609 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1610 MCFSIM_ICR_PRI2;
1611 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1612 break;
1613 default:
1614 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1615 info->line);
1616 return;
1619 uartp = info->addr;
1620 uartp[MCFUART_UIVR] = info->irq;
1621 #endif
1623 /* Clear mask, so no surprise interrupts. */
1624 uartp[MCFUART_UIMR] = 0;
1626 if (request_irq(info->irq, mcfrs_interrupt, IRQF_DISABLED,
1627 "ColdFire UART", NULL)) {
1628 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1629 "vector=%d\n", info->line, info->irq);
1632 return;
1636 char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1640 * Serial stats reporting...
1642 int mcfrs_readproc(char *page, char **start, off_t off, int count,
1643 int *eof, void *data)
1645 struct mcf_serial *info;
1646 char str[20];
1647 int len, sigs, i;
1649 len = sprintf(page, mcfrs_drivername);
1650 for (i = 0; (i < NR_PORTS); i++) {
1651 info = &mcfrs_table[i];
1652 len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1653 i, (unsigned int) info->addr, info->irq, info->baud);
1654 if (info->stats.rx || info->stats.tx)
1655 len += sprintf((page + len), "tx:%d rx:%d ",
1656 info->stats.tx, info->stats.rx);
1657 if (info->stats.rxframing)
1658 len += sprintf((page + len), "fe:%d ",
1659 info->stats.rxframing);
1660 if (info->stats.rxparity)
1661 len += sprintf((page + len), "pe:%d ",
1662 info->stats.rxparity);
1663 if (info->stats.rxbreak)
1664 len += sprintf((page + len), "brk:%d ",
1665 info->stats.rxbreak);
1666 if (info->stats.rxoverrun)
1667 len += sprintf((page + len), "oe:%d ",
1668 info->stats.rxoverrun);
1670 str[0] = str[1] = 0;
1671 if ((sigs = mcfrs_getsignals(info))) {
1672 if (sigs & TIOCM_RTS)
1673 strcat(str, "|RTS");
1674 if (sigs & TIOCM_CTS)
1675 strcat(str, "|CTS");
1676 if (sigs & TIOCM_DTR)
1677 strcat(str, "|DTR");
1678 if (sigs & TIOCM_CD)
1679 strcat(str, "|CD");
1682 len += sprintf((page + len), "%s\n", &str[1]);
1685 return(len);
1689 /* Finally, routines used to initialize the serial driver. */
1691 static void show_serial_version(void)
1693 printk(mcfrs_drivername);
1696 static const struct tty_operations mcfrs_ops = {
1697 .open = mcfrs_open,
1698 .close = mcfrs_close,
1699 .write = mcfrs_write,
1700 .flush_chars = mcfrs_flush_chars,
1701 .write_room = mcfrs_write_room,
1702 .chars_in_buffer = mcfrs_chars_in_buffer,
1703 .flush_buffer = mcfrs_flush_buffer,
1704 .ioctl = mcfrs_ioctl,
1705 .throttle = mcfrs_throttle,
1706 .unthrottle = mcfrs_unthrottle,
1707 .set_termios = mcfrs_set_termios,
1708 .stop = mcfrs_stop,
1709 .start = mcfrs_start,
1710 .hangup = mcfrs_hangup,
1711 .read_proc = mcfrs_readproc,
1712 .wait_until_sent = mcfrs_wait_until_sent,
1713 .tiocmget = mcfrs_tiocmget,
1714 .tiocmset = mcfrs_tiocmset,
1717 /* mcfrs_init inits the driver */
1718 static int __init
1719 mcfrs_init(void)
1721 struct mcf_serial *info;
1722 unsigned long flags;
1723 int i;
1725 /* Setup base handler, and timer table. */
1726 #ifdef MCFPP_DCD0
1727 init_timer(&mcfrs_timer_struct);
1728 mcfrs_timer_struct.function = mcfrs_timer;
1729 mcfrs_timer_struct.data = 0;
1730 mcfrs_timer_struct.expires = jiffies + HZ/25;
1731 add_timer(&mcfrs_timer_struct);
1732 mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1733 #endif
1734 mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1735 if (!mcfrs_serial_driver)
1736 return -ENOMEM;
1738 show_serial_version();
1740 /* Initialize the tty_driver structure */
1741 mcfrs_serial_driver->owner = THIS_MODULE;
1742 mcfrs_serial_driver->name = "ttyS";
1743 mcfrs_serial_driver->driver_name = "mcfserial";
1744 mcfrs_serial_driver->major = TTY_MAJOR;
1745 mcfrs_serial_driver->minor_start = 64;
1746 mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1747 mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1748 mcfrs_serial_driver->init_termios = tty_std_termios;
1750 mcfrs_serial_driver->init_termios.c_cflag =
1751 mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1752 mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1754 tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1756 if (tty_register_driver(mcfrs_serial_driver)) {
1757 printk("MCFRS: Couldn't register serial driver\n");
1758 put_tty_driver(mcfrs_serial_driver);
1759 return(-EBUSY);
1762 local_irq_save(flags);
1765 * Configure all the attached serial ports.
1767 for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1768 info->magic = SERIAL_MAGIC;
1769 info->line = i;
1770 info->tty = 0;
1771 info->custom_divisor = 16;
1772 info->close_delay = 50;
1773 info->closing_wait = 3000;
1774 info->x_char = 0;
1775 info->event = 0;
1776 info->count = 0;
1777 info->blocked_open = 0;
1778 INIT_WORK(&info->tqueue, mcfrs_offintr);
1779 INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
1780 init_waitqueue_head(&info->open_wait);
1781 init_waitqueue_head(&info->close_wait);
1783 info->imr = 0;
1784 mcfrs_setsignals(info, 0, 0);
1785 mcfrs_irqinit(info);
1787 printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1788 (unsigned int) info->addr, info->irq);
1789 printk(" is a builtin ColdFire UART\n");
1792 local_irq_restore(flags);
1793 return 0;
1796 module_init(mcfrs_init);
1798 /****************************************************************************/
1799 /* Serial Console */
1800 /****************************************************************************/
1803 * Quick and dirty UART initialization, for console output.
1806 void mcfrs_init_console(void)
1808 volatile unsigned char *uartp;
1809 unsigned int clk;
1812 * Reset UART, get it into known state...
1814 uartp = (volatile unsigned char *) (MCF_MBAR +
1815 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1817 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
1818 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
1819 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
1822 * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1824 uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1825 uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1827 #ifdef CONFIG_M5272
1830 * For the MCF5272, also compute the baudrate fraction.
1832 int fraction = MCF_BUSCLK - (clk * 32 * mcfrs_console_baud);
1833 fraction *= 16;
1834 fraction /= (32 * mcfrs_console_baud);
1835 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
1836 clk = (MCF_BUSCLK / mcfrs_console_baud) / 32;
1838 #else
1839 clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1840 #endif
1842 uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8; /* set msb baud */
1843 uartp[MCFUART_UBG2] = (clk & 0xff); /* set lsb baud */
1844 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1845 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1847 mcfrs_console_inited++;
1848 return;
1853 * Setup for console. Argument comes from the boot command line.
1856 int mcfrs_console_setup(struct console *cp, char *arg)
1858 int i, n = CONSOLE_BAUD_RATE;
1860 if (!cp)
1861 return(-1);
1863 if (!strncmp(cp->name, "ttyS", 4))
1864 mcfrs_console_port = cp->index;
1865 else if (!strncmp(cp->name, "cua", 3))
1866 mcfrs_console_port = cp->index;
1867 else
1868 return(-1);
1870 if (arg)
1871 n = simple_strtoul(arg,NULL,0);
1872 for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1873 if (mcfrs_baud_table[i] == n)
1874 break;
1875 if (i < MCFRS_BAUD_TABLE_SIZE) {
1876 mcfrs_console_baud = n;
1877 mcfrs_console_cbaud = 0;
1878 if (i > 15) {
1879 mcfrs_console_cbaud |= CBAUDEX;
1880 i -= 15;
1882 mcfrs_console_cbaud |= i;
1884 mcfrs_init_console(); /* make sure baud rate changes */
1885 return(0);
1889 static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1891 *index = c->index;
1892 return mcfrs_serial_driver;
1897 * Output a single character, using UART polled mode.
1898 * This is used for console output.
1901 int mcfrs_put_char(char ch)
1903 volatile unsigned char *uartp;
1904 unsigned long flags;
1905 int i;
1907 uartp = (volatile unsigned char *) (MCF_MBAR +
1908 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1910 local_irq_save(flags);
1911 for (i = 0; (i < 0x10000); i++) {
1912 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1913 break;
1915 if (i < 0x10000) {
1916 uartp[MCFUART_UTB] = ch;
1917 for (i = 0; (i < 0x10000); i++)
1918 if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1919 break;
1921 if (i >= 0x10000)
1922 mcfrs_init_console(); /* try and get it back */
1923 local_irq_restore(flags);
1925 return 1;
1930 * rs_console_write is registered for printk output.
1933 void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1935 if (!mcfrs_console_inited)
1936 mcfrs_init_console();
1937 while (len-- > 0) {
1938 if (*p == '\n')
1939 mcfrs_put_char('\r');
1940 mcfrs_put_char(*p++);
1945 * declare our consoles
1948 struct console mcfrs_console = {
1949 .name = "ttyS",
1950 .write = mcfrs_console_write,
1951 .device = mcfrs_console_device,
1952 .setup = mcfrs_console_setup,
1953 .flags = CON_PRINTBUFFER,
1954 .index = -1,
1957 static int __init mcfrs_console_init(void)
1959 register_console(&mcfrs_console);
1960 return 0;
1963 console_initcall(mcfrs_console_init);
1965 /****************************************************************************/