[CPUFREQ] Make acpi-cpufreq 'sticky'.
[linux-2.6/openmoko-kernel/knife-kernel.git] / drivers / serial / mcfserial.c
blob8cbbb954df2cbdf4b4d360f469f7a5408ffbfa15
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_HW_FEITH)
61 #define CONSOLE_BAUD_RATE 38400
62 #define DEFAULT_CBAUD B38400
63 #elif defined(CONFIG_MOD5272) || defined(CONFIG_M5208EVB)
64 #define CONSOLE_BAUD_RATE 115200
65 #define DEFAULT_CBAUD B115200
66 #elif defined(CONFIG_ARNEWSH) || defined(CONFIG_FREESCALE) || \
67 defined(CONFIG_senTec) || defined(CONFIG_SNEHA)
68 #define CONSOLE_BAUD_RATE 19200
69 #define DEFAULT_CBAUD B19200
70 #endif
72 #ifndef CONSOLE_BAUD_RATE
73 #define CONSOLE_BAUD_RATE 9600
74 #define DEFAULT_CBAUD B9600
75 #endif
77 int mcfrs_console_inited = 0;
78 int mcfrs_console_port = -1;
79 int mcfrs_console_baud = CONSOLE_BAUD_RATE;
80 int mcfrs_console_cbaud = DEFAULT_CBAUD;
83 * Driver data structures.
85 static struct tty_driver *mcfrs_serial_driver;
87 /* number of characters left in xmit buffer before we ask for more */
88 #define WAKEUP_CHARS 256
90 /* Debugging...
92 #undef SERIAL_DEBUG_OPEN
93 #undef SERIAL_DEBUG_FLOW
95 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
96 defined(CONFIG_M520x)
97 #define IRQBASE (MCFINT_VECBASE+MCFINT_UART0)
98 #else
99 #define IRQBASE 73
100 #endif
103 * Configuration table, UARTs to look for at startup.
105 static struct mcf_serial mcfrs_table[] = {
106 { /* ttyS0 */
107 .magic = 0,
108 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE1),
109 .irq = IRQBASE,
110 .flags = ASYNC_BOOT_AUTOCONF,
112 { /* ttyS1 */
113 .magic = 0,
114 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE2),
115 .irq = IRQBASE+1,
116 .flags = ASYNC_BOOT_AUTOCONF,
121 #define NR_PORTS (sizeof(mcfrs_table) / sizeof(struct mcf_serial))
124 * This is used to figure out the divisor speeds and the timeouts.
126 static int mcfrs_baud_table[] = {
127 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
128 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0
130 #define MCFRS_BAUD_TABLE_SIZE \
131 (sizeof(mcfrs_baud_table)/sizeof(mcfrs_baud_table[0]))
134 #ifdef CONFIG_MAGIC_SYSRQ
136 * Magic system request keys. Used for debugging...
138 extern int magic_sysrq_key(int ch);
139 #endif
143 * Forware declarations...
145 static void mcfrs_change_speed(struct mcf_serial *info);
146 static void mcfrs_wait_until_sent(struct tty_struct *tty, int timeout);
149 static inline int serial_paranoia_check(struct mcf_serial *info,
150 char *name, const char *routine)
152 #ifdef SERIAL_PARANOIA_CHECK
153 static const char badmagic[] =
154 "MCFRS(warning): bad magic number for serial struct %s in %s\n";
155 static const char badinfo[] =
156 "MCFRS(warning): null mcf_serial for %s in %s\n";
158 if (!info) {
159 printk(badinfo, name, routine);
160 return 1;
162 if (info->magic != SERIAL_MAGIC) {
163 printk(badmagic, name, routine);
164 return 1;
166 #endif
167 return 0;
171 * Sets or clears DTR and RTS on the requested line.
173 static void mcfrs_setsignals(struct mcf_serial *info, int dtr, int rts)
175 volatile unsigned char *uartp;
176 unsigned long flags;
178 #if 0
179 printk("%s(%d): mcfrs_setsignals(info=%x,dtr=%d,rts=%d)\n",
180 __FILE__, __LINE__, info, dtr, rts);
181 #endif
183 local_irq_save(flags);
184 if (dtr >= 0) {
185 #ifdef MCFPP_DTR0
186 if (info->line)
187 mcf_setppdata(MCFPP_DTR1, (dtr ? 0 : MCFPP_DTR1));
188 else
189 mcf_setppdata(MCFPP_DTR0, (dtr ? 0 : MCFPP_DTR0));
190 #endif
192 if (rts >= 0) {
193 uartp = info->addr;
194 if (rts) {
195 info->sigs |= TIOCM_RTS;
196 uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
197 } else {
198 info->sigs &= ~TIOCM_RTS;
199 uartp[MCFUART_UOP0] = MCFUART_UOP_RTS;
202 local_irq_restore(flags);
203 return;
207 * Gets values of serial signals.
209 static int mcfrs_getsignals(struct mcf_serial *info)
211 volatile unsigned char *uartp;
212 unsigned long flags;
213 int sigs;
214 #if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
215 unsigned short ppdata;
216 #endif
218 #if 0
219 printk("%s(%d): mcfrs_getsignals(info=%x)\n", __FILE__, __LINE__);
220 #endif
222 local_irq_save(flags);
223 uartp = info->addr;
224 sigs = (uartp[MCFUART_UIPR] & MCFUART_UIPR_CTS) ? 0 : TIOCM_CTS;
225 sigs |= (info->sigs & TIOCM_RTS);
227 #ifdef MCFPP_DCD0
229 unsigned int ppdata;
230 ppdata = mcf_getppdata();
231 if (info->line == 0) {
232 sigs |= (ppdata & MCFPP_DCD0) ? 0 : TIOCM_CD;
233 sigs |= (ppdata & MCFPP_DTR0) ? 0 : TIOCM_DTR;
234 } else if (info->line == 1) {
235 sigs |= (ppdata & MCFPP_DCD1) ? 0 : TIOCM_CD;
236 sigs |= (ppdata & MCFPP_DTR1) ? 0 : TIOCM_DTR;
239 #endif
241 local_irq_restore(flags);
242 return(sigs);
246 * ------------------------------------------------------------
247 * mcfrs_stop() and mcfrs_start()
249 * This routines are called before setting or resetting tty->stopped.
250 * They enable or disable transmitter interrupts, as necessary.
251 * ------------------------------------------------------------
253 static void mcfrs_stop(struct tty_struct *tty)
255 volatile unsigned char *uartp;
256 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
257 unsigned long flags;
259 if (serial_paranoia_check(info, tty->name, "mcfrs_stop"))
260 return;
262 local_irq_save(flags);
263 uartp = info->addr;
264 info->imr &= ~MCFUART_UIR_TXREADY;
265 uartp[MCFUART_UIMR] = info->imr;
266 local_irq_restore(flags);
269 static void mcfrs_start(struct tty_struct *tty)
271 volatile unsigned char *uartp;
272 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
273 unsigned long flags;
275 if (serial_paranoia_check(info, tty->name, "mcfrs_start"))
276 return;
278 local_irq_save(flags);
279 if (info->xmit_cnt && info->xmit_buf) {
280 uartp = info->addr;
281 info->imr |= MCFUART_UIR_TXREADY;
282 uartp[MCFUART_UIMR] = info->imr;
284 local_irq_restore(flags);
288 * ----------------------------------------------------------------------
290 * Here starts the interrupt handling routines. All of the following
291 * subroutines are declared as inline and are folded into
292 * mcfrs_interrupt(). They were separated out for readability's sake.
294 * Note: mcfrs_interrupt() is a "fast" interrupt, which means that it
295 * runs with interrupts turned off. People who may want to modify
296 * mcfrs_interrupt() should try to keep the interrupt handler as fast as
297 * possible. After you are done making modifications, it is not a bad
298 * idea to do:
300 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
302 * and look at the resulting assemble code in serial.s.
304 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
305 * -----------------------------------------------------------------------
308 static inline void receive_chars(struct mcf_serial *info)
310 volatile unsigned char *uartp;
311 struct tty_struct *tty = info->tty;
312 unsigned char status, ch, flag;
314 if (!tty)
315 return;
317 uartp = info->addr;
319 while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
320 ch = uartp[MCFUART_URB];
321 info->stats.rx++;
323 #ifdef CONFIG_MAGIC_SYSRQ
324 if (mcfrs_console_inited && (info->line == mcfrs_console_port)) {
325 if (magic_sysrq_key(ch))
326 continue;
328 #endif
330 flag = TTY_NORMAL;
331 if (status & MCFUART_USR_RXERR) {
332 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
333 if (status & MCFUART_USR_RXBREAK) {
334 info->stats.rxbreak++;
335 flag = TTY_BREAK;
336 } else if (status & MCFUART_USR_RXPARITY) {
337 info->stats.rxparity++;
338 flag = TTY_PARITY;
339 } else if (status & MCFUART_USR_RXOVERRUN) {
340 info->stats.rxoverrun++;
341 flag = TTY_OVERRUN;
342 } else if (status & MCFUART_USR_RXFRAMING) {
343 info->stats.rxframing++;
344 flag = TTY_FRAME;
347 tty_insert_flip_char(tty, ch, flag);
349 tty_schedule_flip(tty);
350 return;
353 static inline void transmit_chars(struct mcf_serial *info)
355 volatile unsigned char *uartp;
357 uartp = info->addr;
359 if (info->x_char) {
360 /* Send special char - probably flow control */
361 uartp[MCFUART_UTB] = info->x_char;
362 info->x_char = 0;
363 info->stats.tx++;
366 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
367 info->imr &= ~MCFUART_UIR_TXREADY;
368 uartp[MCFUART_UIMR] = info->imr;
369 return;
372 while (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) {
373 uartp[MCFUART_UTB] = info->xmit_buf[info->xmit_tail++];
374 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
375 info->stats.tx++;
376 if (--info->xmit_cnt <= 0)
377 break;
380 if (info->xmit_cnt < WAKEUP_CHARS)
381 schedule_work(&info->tqueue);
382 return;
386 * This is the serial driver's generic interrupt routine
388 irqreturn_t mcfrs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
390 struct mcf_serial *info;
391 unsigned char isr;
393 info = &mcfrs_table[(irq - IRQBASE)];
394 isr = info->addr[MCFUART_UISR] & info->imr;
396 if (isr & MCFUART_UIR_RXREADY)
397 receive_chars(info);
398 if (isr & MCFUART_UIR_TXREADY)
399 transmit_chars(info);
400 return IRQ_HANDLED;
404 * -------------------------------------------------------------------
405 * Here ends the serial interrupt routines.
406 * -------------------------------------------------------------------
409 static void mcfrs_offintr(void *private)
411 struct mcf_serial *info = (struct mcf_serial *) private;
412 struct tty_struct *tty;
414 tty = info->tty;
415 if (!tty)
416 return;
417 tty_wakeup(tty);
422 * Change of state on a DCD line.
424 void mcfrs_modem_change(struct mcf_serial *info, int dcd)
426 if (info->count == 0)
427 return;
429 if (info->flags & ASYNC_CHECK_CD) {
430 if (dcd)
431 wake_up_interruptible(&info->open_wait);
432 else
433 schedule_work(&info->tqueue_hangup);
438 #ifdef MCFPP_DCD0
440 unsigned short mcfrs_ppstatus;
443 * This subroutine is called when the RS_TIMER goes off. It is used
444 * to monitor the state of the DCD lines - since they have no edge
445 * sensors and interrupt generators.
447 static void mcfrs_timer(void)
449 unsigned int ppstatus, dcdval, i;
451 ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
453 if (ppstatus != mcfrs_ppstatus) {
454 for (i = 0; (i < 2); i++) {
455 dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
456 if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
457 mcfrs_modem_change(&mcfrs_table[i],
458 ((ppstatus & dcdval) ? 0 : 1));
462 mcfrs_ppstatus = ppstatus;
464 /* Re-arm timer */
465 mcfrs_timer_struct.expires = jiffies + HZ/25;
466 add_timer(&mcfrs_timer_struct);
469 #endif /* MCFPP_DCD0 */
473 * This routine is called from the scheduler tqueue when the interrupt
474 * routine has signalled that a hangup has occurred. The path of
475 * hangup processing is:
477 * serial interrupt routine -> (scheduler tqueue) ->
478 * do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
481 static void do_serial_hangup(void *private)
483 struct mcf_serial *info = (struct mcf_serial *) private;
484 struct tty_struct *tty;
486 tty = info->tty;
487 if (!tty)
488 return;
490 tty_hangup(tty);
493 static int startup(struct mcf_serial * info)
495 volatile unsigned char *uartp;
496 unsigned long flags;
498 if (info->flags & ASYNC_INITIALIZED)
499 return 0;
501 if (!info->xmit_buf) {
502 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
503 if (!info->xmit_buf)
504 return -ENOMEM;
507 local_irq_save(flags);
509 #ifdef SERIAL_DEBUG_OPEN
510 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
511 #endif
514 * Reset UART, get it into known state...
516 uartp = info->addr;
517 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
518 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
519 mcfrs_setsignals(info, 1, 1);
521 if (info->tty)
522 clear_bit(TTY_IO_ERROR, &info->tty->flags);
523 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
526 * and set the speed of the serial port
528 mcfrs_change_speed(info);
531 * Lastly enable the UART transmitter and receiver, and
532 * interrupt enables.
534 info->imr = MCFUART_UIR_RXREADY;
535 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
536 uartp[MCFUART_UIMR] = info->imr;
538 info->flags |= ASYNC_INITIALIZED;
539 local_irq_restore(flags);
540 return 0;
544 * This routine will shutdown a serial port; interrupts are disabled, and
545 * DTR is dropped if the hangup on close termio flag is on.
547 static void shutdown(struct mcf_serial * info)
549 volatile unsigned char *uartp;
550 unsigned long flags;
552 if (!(info->flags & ASYNC_INITIALIZED))
553 return;
555 #ifdef SERIAL_DEBUG_OPEN
556 printk("Shutting down serial port %d (irq %d)....\n", info->line,
557 info->irq);
558 #endif
560 local_irq_save(flags);
562 uartp = info->addr;
563 uartp[MCFUART_UIMR] = 0; /* mask all interrupts */
564 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
565 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
567 if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
568 mcfrs_setsignals(info, 0, 0);
570 if (info->xmit_buf) {
571 free_page((unsigned long) info->xmit_buf);
572 info->xmit_buf = 0;
575 if (info->tty)
576 set_bit(TTY_IO_ERROR, &info->tty->flags);
578 info->flags &= ~ASYNC_INITIALIZED;
579 local_irq_restore(flags);
584 * This routine is called to set the UART divisor registers to match
585 * the specified baud rate for a serial port.
587 static void mcfrs_change_speed(struct mcf_serial *info)
589 volatile unsigned char *uartp;
590 unsigned int baudclk, cflag;
591 unsigned long flags;
592 unsigned char mr1, mr2;
593 int i;
594 #ifdef CONFIG_M5272
595 unsigned int fraction;
596 #endif
598 if (!info->tty || !info->tty->termios)
599 return;
600 cflag = info->tty->termios->c_cflag;
601 if (info->addr == 0)
602 return;
604 #if 0
605 printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
606 #endif
608 i = cflag & CBAUD;
609 if (i & CBAUDEX) {
610 i &= ~CBAUDEX;
611 if (i < 1 || i > 4)
612 info->tty->termios->c_cflag &= ~CBAUDEX;
613 else
614 i += 15;
616 if (i == 0) {
617 mcfrs_setsignals(info, 0, -1);
618 return;
621 /* compute the baudrate clock */
622 #ifdef CONFIG_M5272
624 * For the MCF5272, also compute the baudrate fraction.
626 baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
627 fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
628 fraction *= 16;
629 fraction /= (32 * mcfrs_baud_table[i]);
630 #else
631 baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
632 #endif
634 info->baud = mcfrs_baud_table[i];
636 mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
637 mr2 = 0;
639 switch (cflag & CSIZE) {
640 case CS5: mr1 |= MCFUART_MR1_CS5; break;
641 case CS6: mr1 |= MCFUART_MR1_CS6; break;
642 case CS7: mr1 |= MCFUART_MR1_CS7; break;
643 case CS8:
644 default: mr1 |= MCFUART_MR1_CS8; break;
647 if (cflag & PARENB) {
648 if (cflag & CMSPAR) {
649 if (cflag & PARODD)
650 mr1 |= MCFUART_MR1_PARITYMARK;
651 else
652 mr1 |= MCFUART_MR1_PARITYSPACE;
653 } else {
654 if (cflag & PARODD)
655 mr1 |= MCFUART_MR1_PARITYODD;
656 else
657 mr1 |= MCFUART_MR1_PARITYEVEN;
659 } else {
660 mr1 |= MCFUART_MR1_PARITYNONE;
663 if (cflag & CSTOPB)
664 mr2 |= MCFUART_MR2_STOP2;
665 else
666 mr2 |= MCFUART_MR2_STOP1;
668 if (cflag & CRTSCTS) {
669 mr1 |= MCFUART_MR1_RXRTS;
670 mr2 |= MCFUART_MR2_TXCTS;
673 if (cflag & CLOCAL)
674 info->flags &= ~ASYNC_CHECK_CD;
675 else
676 info->flags |= ASYNC_CHECK_CD;
678 uartp = info->addr;
680 local_irq_save(flags);
681 #if 0
682 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
683 mr1, mr2, baudclk);
684 #endif
686 Note: pg 12-16 of MCF5206e User's Manual states that a
687 software reset should be performed prior to changing
688 UMR1,2, UCSR, UACR, bit 7
690 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
691 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
692 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
693 uartp[MCFUART_UMR] = mr1;
694 uartp[MCFUART_UMR] = mr2;
695 uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8; /* set msb byte */
696 uartp[MCFUART_UBG2] = (baudclk & 0xff); /* set lsb byte */
697 #ifdef CONFIG_M5272
698 uartp[MCFUART_UFPD] = (fraction & 0xf); /* set fraction */
699 #endif
700 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
701 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
702 mcfrs_setsignals(info, 1, -1);
703 local_irq_restore(flags);
704 return;
707 static void mcfrs_flush_chars(struct tty_struct *tty)
709 volatile unsigned char *uartp;
710 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
711 unsigned long flags;
713 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
714 return;
716 uartp = (volatile unsigned char *) info->addr;
719 * re-enable receiver interrupt
721 local_irq_save(flags);
722 if ((!(info->imr & MCFUART_UIR_RXREADY)) &&
723 (info->flags & ASYNC_INITIALIZED) ) {
724 info->imr |= MCFUART_UIR_RXREADY;
725 uartp[MCFUART_UIMR] = info->imr;
727 local_irq_restore(flags);
729 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
730 !info->xmit_buf)
731 return;
733 /* Enable transmitter */
734 local_irq_save(flags);
735 info->imr |= MCFUART_UIR_TXREADY;
736 uartp[MCFUART_UIMR] = info->imr;
737 local_irq_restore(flags);
740 static int mcfrs_write(struct tty_struct * tty,
741 const unsigned char *buf, int count)
743 volatile unsigned char *uartp;
744 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
745 unsigned long flags;
746 int c, total = 0;
748 #if 0
749 printk("%s(%d): mcfrs_write(tty=%x,buf=%x,count=%d)\n",
750 __FILE__, __LINE__, (int)tty, (int)buf, count);
751 #endif
753 if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
754 return 0;
756 if (!tty || !info->xmit_buf)
757 return 0;
759 local_save_flags(flags);
760 while (1) {
761 local_irq_disable();
762 c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
763 ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
764 local_irq_restore(flags);
766 if (c <= 0)
767 break;
769 memcpy(info->xmit_buf + info->xmit_head, buf, c);
771 local_irq_disable();
772 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
773 info->xmit_cnt += c;
774 local_irq_restore(flags);
776 buf += c;
777 count -= c;
778 total += c;
781 local_irq_disable();
782 uartp = info->addr;
783 info->imr |= MCFUART_UIR_TXREADY;
784 uartp[MCFUART_UIMR] = info->imr;
785 local_irq_restore(flags);
787 return total;
790 static int mcfrs_write_room(struct tty_struct *tty)
792 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
793 int ret;
795 if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
796 return 0;
797 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
798 if (ret < 0)
799 ret = 0;
800 return ret;
803 static int mcfrs_chars_in_buffer(struct tty_struct *tty)
805 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
807 if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
808 return 0;
809 return info->xmit_cnt;
812 static void mcfrs_flush_buffer(struct tty_struct *tty)
814 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
815 unsigned long flags;
817 if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
818 return;
820 local_irq_save(flags);
821 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
822 local_irq_restore(flags);
824 tty_wakeup(tty);
828 * ------------------------------------------------------------
829 * mcfrs_throttle()
831 * This routine is called by the upper-layer tty layer to signal that
832 * incoming characters should be throttled.
833 * ------------------------------------------------------------
835 static void mcfrs_throttle(struct tty_struct * tty)
837 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
838 #ifdef SERIAL_DEBUG_THROTTLE
839 char buf[64];
841 printk("throttle %s: %d....\n", _tty_name(tty, buf),
842 tty->ldisc.chars_in_buffer(tty));
843 #endif
845 if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
846 return;
848 if (I_IXOFF(tty))
849 info->x_char = STOP_CHAR(tty);
851 /* Turn off RTS line (do this atomic) */
854 static void mcfrs_unthrottle(struct tty_struct * tty)
856 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
857 #ifdef SERIAL_DEBUG_THROTTLE
858 char buf[64];
860 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
861 tty->ldisc.chars_in_buffer(tty));
862 #endif
864 if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
865 return;
867 if (I_IXOFF(tty)) {
868 if (info->x_char)
869 info->x_char = 0;
870 else
871 info->x_char = START_CHAR(tty);
874 /* Assert RTS line (do this atomic) */
878 * ------------------------------------------------------------
879 * mcfrs_ioctl() and friends
880 * ------------------------------------------------------------
883 static int get_serial_info(struct mcf_serial * info,
884 struct serial_struct * retinfo)
886 struct serial_struct tmp;
888 if (!retinfo)
889 return -EFAULT;
890 memset(&tmp, 0, sizeof(tmp));
891 tmp.type = info->type;
892 tmp.line = info->line;
893 tmp.port = (unsigned int) info->addr;
894 tmp.irq = info->irq;
895 tmp.flags = info->flags;
896 tmp.baud_base = info->baud_base;
897 tmp.close_delay = info->close_delay;
898 tmp.closing_wait = info->closing_wait;
899 tmp.custom_divisor = info->custom_divisor;
900 return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
903 static int set_serial_info(struct mcf_serial * info,
904 struct serial_struct * new_info)
906 struct serial_struct new_serial;
907 struct mcf_serial old_info;
908 int retval = 0;
910 if (!new_info)
911 return -EFAULT;
912 if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
913 return -EFAULT;
914 old_info = *info;
916 if (!capable(CAP_SYS_ADMIN)) {
917 if ((new_serial.baud_base != info->baud_base) ||
918 (new_serial.type != info->type) ||
919 (new_serial.close_delay != info->close_delay) ||
920 ((new_serial.flags & ~ASYNC_USR_MASK) !=
921 (info->flags & ~ASYNC_USR_MASK)))
922 return -EPERM;
923 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
924 (new_serial.flags & ASYNC_USR_MASK));
925 info->custom_divisor = new_serial.custom_divisor;
926 goto check_and_exit;
929 if (info->count > 1)
930 return -EBUSY;
933 * OK, past this point, all the error checking has been done.
934 * At this point, we start making changes.....
937 info->baud_base = new_serial.baud_base;
938 info->flags = ((info->flags & ~ASYNC_FLAGS) |
939 (new_serial.flags & ASYNC_FLAGS));
940 info->type = new_serial.type;
941 info->close_delay = new_serial.close_delay;
942 info->closing_wait = new_serial.closing_wait;
944 check_and_exit:
945 retval = startup(info);
946 return retval;
950 * get_lsr_info - get line status register info
952 * Purpose: Let user call ioctl() to get info when the UART physically
953 * is emptied. On bus types like RS485, the transmitter must
954 * release the bus after transmitting. This must be done when
955 * the transmit shift register is empty, not be done when the
956 * transmit holding register is empty. This functionality
957 * allows an RS485 driver to be written in user space.
959 static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
961 volatile unsigned char *uartp;
962 unsigned long flags;
963 unsigned char status;
965 local_irq_save(flags);
966 uartp = info->addr;
967 status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
968 local_irq_restore(flags);
970 return put_user(status,value);
974 * This routine sends a break character out the serial port.
976 static void send_break( struct mcf_serial * info, int duration)
978 volatile unsigned char *uartp;
979 unsigned long flags;
981 if (!info->addr)
982 return;
983 set_current_state(TASK_INTERRUPTIBLE);
984 uartp = info->addr;
986 local_irq_save(flags);
987 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
988 schedule_timeout(duration);
989 uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
990 local_irq_restore(flags);
993 static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
995 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
997 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
998 return -ENODEV;
999 if (tty->flags & (1 << TTY_IO_ERROR))
1000 return -EIO;
1002 return mcfrs_getsignals(info);
1005 static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
1006 unsigned int set, unsigned int clear)
1008 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1009 int rts = -1, dtr = -1;
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 if (set & TIOCM_RTS)
1017 rts = 1;
1018 if (set & TIOCM_DTR)
1019 dtr = 1;
1020 if (clear & TIOCM_RTS)
1021 rts = 0;
1022 if (clear & TIOCM_DTR)
1023 dtr = 0;
1025 mcfrs_setsignals(info, dtr, rts);
1027 return 0;
1030 static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
1031 unsigned int cmd, unsigned long arg)
1033 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1034 int retval, error;
1036 if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1037 return -ENODEV;
1039 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1040 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1041 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1042 if (tty->flags & (1 << TTY_IO_ERROR))
1043 return -EIO;
1046 switch (cmd) {
1047 case TCSBRK: /* SVID version: non-zero arg --> no break */
1048 retval = tty_check_change(tty);
1049 if (retval)
1050 return retval;
1051 tty_wait_until_sent(tty, 0);
1052 if (!arg)
1053 send_break(info, HZ/4); /* 1/4 second */
1054 return 0;
1055 case TCSBRKP: /* support for POSIX tcsendbreak() */
1056 retval = tty_check_change(tty);
1057 if (retval)
1058 return retval;
1059 tty_wait_until_sent(tty, 0);
1060 send_break(info, arg ? arg*(HZ/10) : HZ/4);
1061 return 0;
1062 case TIOCGSOFTCAR:
1063 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1064 (unsigned long *) arg);
1065 if (error)
1066 return error;
1067 return 0;
1068 case TIOCSSOFTCAR:
1069 get_user(arg, (unsigned long *) arg);
1070 tty->termios->c_cflag =
1071 ((tty->termios->c_cflag & ~CLOCAL) |
1072 (arg ? CLOCAL : 0));
1073 return 0;
1074 case TIOCGSERIAL:
1075 if (access_ok(VERIFY_WRITE, (void *) arg,
1076 sizeof(struct serial_struct)))
1077 return get_serial_info(info,
1078 (struct serial_struct *) arg);
1079 return -EFAULT;
1080 case TIOCSSERIAL:
1081 return set_serial_info(info,
1082 (struct serial_struct *) arg);
1083 case TIOCSERGETLSR: /* Get line status register */
1084 if (access_ok(VERIFY_WRITE, (void *) arg,
1085 sizeof(unsigned int)))
1086 return get_lsr_info(info, (unsigned int *) arg);
1087 return -EFAULT;
1088 case TIOCSERGSTRUCT:
1089 error = copy_to_user((struct mcf_serial *) arg,
1090 info, sizeof(struct mcf_serial));
1091 if (error)
1092 return -EFAULT;
1093 return 0;
1095 #ifdef TIOCSET422
1096 case TIOCSET422: {
1097 unsigned int val;
1098 get_user(val, (unsigned int *) arg);
1099 mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1100 break;
1102 case TIOCGET422: {
1103 unsigned int val;
1104 val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1105 put_user(val, (unsigned int *) arg);
1106 break;
1108 #endif
1110 default:
1111 return -ENOIOCTLCMD;
1113 return 0;
1116 static void mcfrs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1118 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1120 if (tty->termios->c_cflag == old_termios->c_cflag)
1121 return;
1123 mcfrs_change_speed(info);
1125 if ((old_termios->c_cflag & CRTSCTS) &&
1126 !(tty->termios->c_cflag & CRTSCTS)) {
1127 tty->hw_stopped = 0;
1128 mcfrs_setsignals(info, -1, 1);
1129 #if 0
1130 mcfrs_start(tty);
1131 #endif
1136 * ------------------------------------------------------------
1137 * mcfrs_close()
1139 * This routine is called when the serial port gets closed. First, we
1140 * wait for the last remaining data to be sent. Then, we unlink its
1141 * S structure from the interrupt chain if necessary, and we free
1142 * that IRQ if nothing is left in the chain.
1143 * ------------------------------------------------------------
1145 static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1147 volatile unsigned char *uartp;
1148 struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1149 unsigned long flags;
1151 if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1152 return;
1154 local_irq_save(flags);
1156 if (tty_hung_up_p(filp)) {
1157 local_irq_restore(flags);
1158 return;
1161 #ifdef SERIAL_DEBUG_OPEN
1162 printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1163 #endif
1164 if ((tty->count == 1) && (info->count != 1)) {
1166 * Uh, oh. tty->count is 1, which means that the tty
1167 * structure will be freed. Info->count should always
1168 * be one in these conditions. If it's greater than
1169 * one, we've got real problems, since it means the
1170 * serial port won't be shutdown.
1172 printk("MCFRS: bad serial port count; tty->count is 1, "
1173 "info->count is %d\n", info->count);
1174 info->count = 1;
1176 if (--info->count < 0) {
1177 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1178 info->line, info->count);
1179 info->count = 0;
1181 if (info->count) {
1182 local_irq_restore(flags);
1183 return;
1185 info->flags |= ASYNC_CLOSING;
1188 * Now we wait for the transmit buffer to clear; and we notify
1189 * the line discipline to only process XON/XOFF characters.
1191 tty->closing = 1;
1192 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1193 tty_wait_until_sent(tty, info->closing_wait);
1196 * At this point we stop accepting input. To do this, we
1197 * disable the receive line status interrupts, and tell the
1198 * interrupt driver to stop checking the data ready bit in the
1199 * line status register.
1201 info->imr &= ~MCFUART_UIR_RXREADY;
1202 uartp = info->addr;
1203 uartp[MCFUART_UIMR] = info->imr;
1205 #if 0
1206 /* FIXME: do we need to keep this enabled for console?? */
1207 if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
1208 /* Do not disable the UART */ ;
1209 } else
1210 #endif
1211 shutdown(info);
1212 if (tty->driver->flush_buffer)
1213 tty->driver->flush_buffer(tty);
1214 tty_ldisc_flush(tty);
1216 tty->closing = 0;
1217 info->event = 0;
1218 info->tty = 0;
1219 #if 0
1220 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1221 if (tty->ldisc.close)
1222 (tty->ldisc.close)(tty);
1223 tty->ldisc = ldiscs[N_TTY];
1224 tty->termios->c_line = N_TTY;
1225 if (tty->ldisc.open)
1226 (tty->ldisc.open)(tty);
1228 #endif
1229 if (info->blocked_open) {
1230 if (info->close_delay) {
1231 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1233 wake_up_interruptible(&info->open_wait);
1235 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1236 wake_up_interruptible(&info->close_wait);
1237 local_irq_restore(flags);
1241 * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1243 static void
1244 mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1246 #ifdef CONFIG_M5272
1247 #define MCF5272_FIFO_SIZE 25 /* fifo size + shift reg */
1249 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1250 volatile unsigned char *uartp;
1251 unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1253 if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1254 return;
1256 orig_jiffies = jiffies;
1259 * Set the check interval to be 1/5 of the approximate time
1260 * to send the entire fifo, and make it at least 1. The check
1261 * interval should also be less than the timeout.
1263 * Note: we have to use pretty tight timings here to satisfy
1264 * the NIST-PCTS.
1266 fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1267 char_time = fifo_time / 5;
1268 if (char_time == 0)
1269 char_time = 1;
1270 if (timeout && timeout < char_time)
1271 char_time = timeout;
1274 * Clamp the timeout period at 2 * the time to empty the
1275 * fifo. Just to be safe, set the minimum at .5 seconds.
1277 fifo_time *= 2;
1278 if (fifo_time < (HZ/2))
1279 fifo_time = HZ/2;
1280 if (!timeout || timeout > fifo_time)
1281 timeout = fifo_time;
1284 * Account for the number of bytes in the UART
1285 * transmitter FIFO plus any byte being shifted out.
1287 uartp = (volatile unsigned char *) info->addr;
1288 for (;;) {
1289 fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1290 if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1291 MCFUART_USR_TXEMPTY)) ==
1292 MCFUART_USR_TXREADY)
1293 fifo_cnt++;
1294 if (fifo_cnt == 0)
1295 break;
1296 msleep_interruptible(jiffies_to_msecs(char_time));
1297 if (signal_pending(current))
1298 break;
1299 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1300 break;
1302 #else
1304 * For the other coldfire models, assume all data has been sent
1306 #endif
1310 * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1312 void mcfrs_hangup(struct tty_struct *tty)
1314 struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1316 if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
1317 return;
1319 mcfrs_flush_buffer(tty);
1320 shutdown(info);
1321 info->event = 0;
1322 info->count = 0;
1323 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1324 info->tty = 0;
1325 wake_up_interruptible(&info->open_wait);
1329 * ------------------------------------------------------------
1330 * mcfrs_open() and friends
1331 * ------------------------------------------------------------
1333 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1334 struct mcf_serial *info)
1336 DECLARE_WAITQUEUE(wait, current);
1337 int retval;
1338 int do_clocal = 0;
1341 * If the device is in the middle of being closed, then block
1342 * until it's done, and then try again.
1344 if (info->flags & ASYNC_CLOSING) {
1345 interruptible_sleep_on(&info->close_wait);
1346 #ifdef SERIAL_DO_RESTART
1347 if (info->flags & ASYNC_HUP_NOTIFY)
1348 return -EAGAIN;
1349 else
1350 return -ERESTARTSYS;
1351 #else
1352 return -EAGAIN;
1353 #endif
1357 * If non-blocking mode is set, or the port is not enabled,
1358 * then make the check up front and then exit.
1360 if ((filp->f_flags & O_NONBLOCK) ||
1361 (tty->flags & (1 << TTY_IO_ERROR))) {
1362 info->flags |= ASYNC_NORMAL_ACTIVE;
1363 return 0;
1366 if (tty->termios->c_cflag & CLOCAL)
1367 do_clocal = 1;
1370 * Block waiting for the carrier detect and the line to become
1371 * free (i.e., not in use by the callout). While we are in
1372 * this loop, info->count is dropped by one, so that
1373 * mcfrs_close() knows when to free things. We restore it upon
1374 * exit, either normal or abnormal.
1376 retval = 0;
1377 add_wait_queue(&info->open_wait, &wait);
1378 #ifdef SERIAL_DEBUG_OPEN
1379 printk("block_til_ready before block: ttyS%d, count = %d\n",
1380 info->line, info->count);
1381 #endif
1382 info->count--;
1383 info->blocked_open++;
1384 while (1) {
1385 local_irq_disable();
1386 mcfrs_setsignals(info, 1, 1);
1387 local_irq_enable();
1388 current->state = TASK_INTERRUPTIBLE;
1389 if (tty_hung_up_p(filp) ||
1390 !(info->flags & ASYNC_INITIALIZED)) {
1391 #ifdef SERIAL_DO_RESTART
1392 if (info->flags & ASYNC_HUP_NOTIFY)
1393 retval = -EAGAIN;
1394 else
1395 retval = -ERESTARTSYS;
1396 #else
1397 retval = -EAGAIN;
1398 #endif
1399 break;
1401 if (!(info->flags & ASYNC_CLOSING) &&
1402 (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1403 break;
1404 if (signal_pending(current)) {
1405 retval = -ERESTARTSYS;
1406 break;
1408 #ifdef SERIAL_DEBUG_OPEN
1409 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1410 info->line, info->count);
1411 #endif
1412 schedule();
1414 current->state = TASK_RUNNING;
1415 remove_wait_queue(&info->open_wait, &wait);
1416 if (!tty_hung_up_p(filp))
1417 info->count++;
1418 info->blocked_open--;
1419 #ifdef SERIAL_DEBUG_OPEN
1420 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1421 info->line, info->count);
1422 #endif
1423 if (retval)
1424 return retval;
1425 info->flags |= ASYNC_NORMAL_ACTIVE;
1426 return 0;
1430 * This routine is called whenever a serial port is opened. It
1431 * enables interrupts for a serial port, linking in its structure into
1432 * the IRQ chain. It also performs the serial-specific
1433 * initialization for the tty structure.
1435 int mcfrs_open(struct tty_struct *tty, struct file * filp)
1437 struct mcf_serial *info;
1438 int retval, line;
1440 line = tty->index;
1441 if ((line < 0) || (line >= NR_PORTS))
1442 return -ENODEV;
1443 info = mcfrs_table + line;
1444 if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1445 return -ENODEV;
1446 #ifdef SERIAL_DEBUG_OPEN
1447 printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1448 #endif
1449 info->count++;
1450 tty->driver_data = info;
1451 info->tty = tty;
1454 * Start up serial port
1456 retval = startup(info);
1457 if (retval)
1458 return retval;
1460 retval = block_til_ready(tty, filp, info);
1461 if (retval) {
1462 #ifdef SERIAL_DEBUG_OPEN
1463 printk("mcfrs_open returning after block_til_ready with %d\n",
1464 retval);
1465 #endif
1466 return retval;
1469 #ifdef SERIAL_DEBUG_OPEN
1470 printk("mcfrs_open %s successful...\n", tty->name);
1471 #endif
1472 return 0;
1476 * Based on the line number set up the internal interrupt stuff.
1478 static void mcfrs_irqinit(struct mcf_serial *info)
1480 #if defined(CONFIG_M5272)
1481 volatile unsigned long *icrp;
1482 volatile unsigned long *portp;
1483 volatile unsigned char *uartp;
1485 uartp = info->addr;
1486 icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1488 switch (info->line) {
1489 case 0:
1490 *icrp = 0xe0000000;
1491 break;
1492 case 1:
1493 *icrp = 0x0e000000;
1494 break;
1495 default:
1496 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1497 info->line);
1498 return;
1501 /* Enable the output lines for the serial ports */
1502 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
1503 *portp = (*portp & ~0x000000ff) | 0x00000055;
1504 portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
1505 *portp = (*portp & ~0x000003fc) | 0x000002a8;
1506 #elif defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x)
1507 volatile unsigned char *icrp, *uartp;
1508 volatile unsigned long *imrp;
1510 uartp = info->addr;
1512 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1513 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1514 *icrp = 0x30 + info->line; /* level 6, line based priority */
1516 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1517 MCFINTC_IMRL);
1518 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1519 #elif defined(CONFIG_M520x)
1520 volatile unsigned char *icrp, *uartp;
1521 volatile unsigned long *imrp;
1523 uartp = info->addr;
1525 icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1526 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1527 *icrp = 0x03;
1529 imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1530 MCFINTC_IMRL);
1531 *imrp &= ~((1 << (info->irq - MCFINT_VECBASE)) | 1);
1532 if (info->line < 2) {
1533 unsigned short *uart_par;
1534 uart_par = (unsigned short *)(MCF_IPSBAR + MCF_GPIO_PAR_UART);
1535 if (info->line == 0)
1536 *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD0
1537 | MCF_GPIO_PAR_UART_PAR_URXD0;
1538 else if (info->line == 1)
1539 *uart_par |= MCF_GPIO_PAR_UART_PAR_UTXD1
1540 | MCF_GPIO_PAR_UART_PAR_URXD1;
1541 } else if (info->line == 2) {
1542 unsigned char *feci2c_par;
1543 feci2c_par = (unsigned char *)(MCF_IPSBAR + MCF_GPIO_PAR_FECI2C);
1544 *feci2c_par &= ~0x0F;
1545 *feci2c_par |= MCF_GPIO_PAR_FECI2C_PAR_SCL_UTXD2
1546 | MCF_GPIO_PAR_FECI2C_PAR_SDA_URXD2;
1548 #else
1549 volatile unsigned char *icrp, *uartp;
1551 switch (info->line) {
1552 case 0:
1553 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1554 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1555 MCFSIM_ICR_PRI1;
1556 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1557 break;
1558 case 1:
1559 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1560 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1561 MCFSIM_ICR_PRI2;
1562 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1563 break;
1564 default:
1565 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1566 info->line);
1567 return;
1570 uartp = info->addr;
1571 uartp[MCFUART_UIVR] = info->irq;
1572 #endif
1574 /* Clear mask, so no surprise interrupts. */
1575 uartp[MCFUART_UIMR] = 0;
1577 if (request_irq(info->irq, mcfrs_interrupt, SA_INTERRUPT,
1578 "ColdFire UART", NULL)) {
1579 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1580 "vector=%d\n", info->line, info->irq);
1583 return;
1587 char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1591 * Serial stats reporting...
1593 int mcfrs_readproc(char *page, char **start, off_t off, int count,
1594 int *eof, void *data)
1596 struct mcf_serial *info;
1597 char str[20];
1598 int len, sigs, i;
1600 len = sprintf(page, mcfrs_drivername);
1601 for (i = 0; (i < NR_PORTS); i++) {
1602 info = &mcfrs_table[i];
1603 len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1604 i, (unsigned int) info->addr, info->irq, info->baud);
1605 if (info->stats.rx || info->stats.tx)
1606 len += sprintf((page + len), "tx:%d rx:%d ",
1607 info->stats.tx, info->stats.rx);
1608 if (info->stats.rxframing)
1609 len += sprintf((page + len), "fe:%d ",
1610 info->stats.rxframing);
1611 if (info->stats.rxparity)
1612 len += sprintf((page + len), "pe:%d ",
1613 info->stats.rxparity);
1614 if (info->stats.rxbreak)
1615 len += sprintf((page + len), "brk:%d ",
1616 info->stats.rxbreak);
1617 if (info->stats.rxoverrun)
1618 len += sprintf((page + len), "oe:%d ",
1619 info->stats.rxoverrun);
1621 str[0] = str[1] = 0;
1622 if ((sigs = mcfrs_getsignals(info))) {
1623 if (sigs & TIOCM_RTS)
1624 strcat(str, "|RTS");
1625 if (sigs & TIOCM_CTS)
1626 strcat(str, "|CTS");
1627 if (sigs & TIOCM_DTR)
1628 strcat(str, "|DTR");
1629 if (sigs & TIOCM_CD)
1630 strcat(str, "|CD");
1633 len += sprintf((page + len), "%s\n", &str[1]);
1636 return(len);
1640 /* Finally, routines used to initialize the serial driver. */
1642 static void show_serial_version(void)
1644 printk(mcfrs_drivername);
1647 static struct tty_operations mcfrs_ops = {
1648 .open = mcfrs_open,
1649 .close = mcfrs_close,
1650 .write = mcfrs_write,
1651 .flush_chars = mcfrs_flush_chars,
1652 .write_room = mcfrs_write_room,
1653 .chars_in_buffer = mcfrs_chars_in_buffer,
1654 .flush_buffer = mcfrs_flush_buffer,
1655 .ioctl = mcfrs_ioctl,
1656 .throttle = mcfrs_throttle,
1657 .unthrottle = mcfrs_unthrottle,
1658 .set_termios = mcfrs_set_termios,
1659 .stop = mcfrs_stop,
1660 .start = mcfrs_start,
1661 .hangup = mcfrs_hangup,
1662 .read_proc = mcfrs_readproc,
1663 .wait_until_sent = mcfrs_wait_until_sent,
1664 .tiocmget = mcfrs_tiocmget,
1665 .tiocmset = mcfrs_tiocmset,
1668 /* mcfrs_init inits the driver */
1669 static int __init
1670 mcfrs_init(void)
1672 struct mcf_serial *info;
1673 unsigned long flags;
1674 int i;
1676 /* Setup base handler, and timer table. */
1677 #ifdef MCFPP_DCD0
1678 init_timer(&mcfrs_timer_struct);
1679 mcfrs_timer_struct.function = mcfrs_timer;
1680 mcfrs_timer_struct.data = 0;
1681 mcfrs_timer_struct.expires = jiffies + HZ/25;
1682 add_timer(&mcfrs_timer_struct);
1683 mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1684 #endif
1685 mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1686 if (!mcfrs_serial_driver)
1687 return -ENOMEM;
1689 show_serial_version();
1691 /* Initialize the tty_driver structure */
1692 mcfrs_serial_driver->owner = THIS_MODULE;
1693 mcfrs_serial_driver->name = "ttyS";
1694 mcfrs_serial_driver->devfs_name = "ttys/";
1695 mcfrs_serial_driver->driver_name = "serial";
1696 mcfrs_serial_driver->major = TTY_MAJOR;
1697 mcfrs_serial_driver->minor_start = 64;
1698 mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1699 mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1700 mcfrs_serial_driver->init_termios = tty_std_termios;
1702 mcfrs_serial_driver->init_termios.c_cflag =
1703 mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1704 mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1706 tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1708 if (tty_register_driver(mcfrs_serial_driver)) {
1709 printk("MCFRS: Couldn't register serial driver\n");
1710 put_tty_driver(mcfrs_serial_driver);
1711 return(-EBUSY);
1714 local_irq_save(flags);
1717 * Configure all the attached serial ports.
1719 for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1720 info->magic = SERIAL_MAGIC;
1721 info->line = i;
1722 info->tty = 0;
1723 info->custom_divisor = 16;
1724 info->close_delay = 50;
1725 info->closing_wait = 3000;
1726 info->x_char = 0;
1727 info->event = 0;
1728 info->count = 0;
1729 info->blocked_open = 0;
1730 INIT_WORK(&info->tqueue, mcfrs_offintr, info);
1731 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1732 init_waitqueue_head(&info->open_wait);
1733 init_waitqueue_head(&info->close_wait);
1735 info->imr = 0;
1736 mcfrs_setsignals(info, 0, 0);
1737 mcfrs_irqinit(info);
1739 printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1740 (unsigned int) info->addr, info->irq);
1741 printk(" is a builtin ColdFire UART\n");
1744 local_irq_restore(flags);
1745 return 0;
1748 module_init(mcfrs_init);
1750 /****************************************************************************/
1751 /* Serial Console */
1752 /****************************************************************************/
1755 * Quick and dirty UART initialization, for console output.
1758 void mcfrs_init_console(void)
1760 volatile unsigned char *uartp;
1761 unsigned int clk;
1764 * Reset UART, get it into known state...
1766 uartp = (volatile unsigned char *) (MCF_MBAR +
1767 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1769 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX; /* reset RX */
1770 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX; /* reset TX */
1771 uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
1774 * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1776 uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1777 uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1779 clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1780 uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8; /* set msb baud */
1781 uartp[MCFUART_UBG2] = (clk & 0xff); /* set lsb baud */
1783 uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1784 uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1786 mcfrs_console_inited++;
1787 return;
1792 * Setup for console. Argument comes from the boot command line.
1795 int mcfrs_console_setup(struct console *cp, char *arg)
1797 int i, n = CONSOLE_BAUD_RATE;
1799 if (!cp)
1800 return(-1);
1802 if (!strncmp(cp->name, "ttyS", 4))
1803 mcfrs_console_port = cp->index;
1804 else if (!strncmp(cp->name, "cua", 3))
1805 mcfrs_console_port = cp->index;
1806 else
1807 return(-1);
1809 if (arg)
1810 n = simple_strtoul(arg,NULL,0);
1811 for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1812 if (mcfrs_baud_table[i] == n)
1813 break;
1814 if (i < MCFRS_BAUD_TABLE_SIZE) {
1815 mcfrs_console_baud = n;
1816 mcfrs_console_cbaud = 0;
1817 if (i > 15) {
1818 mcfrs_console_cbaud |= CBAUDEX;
1819 i -= 15;
1821 mcfrs_console_cbaud |= i;
1823 mcfrs_init_console(); /* make sure baud rate changes */
1824 return(0);
1828 static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1830 *index = c->index;
1831 return mcfrs_serial_driver;
1836 * Output a single character, using UART polled mode.
1837 * This is used for console output.
1840 void mcfrs_put_char(char ch)
1842 volatile unsigned char *uartp;
1843 unsigned long flags;
1844 int i;
1846 uartp = (volatile unsigned char *) (MCF_MBAR +
1847 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1849 local_irq_save(flags);
1850 for (i = 0; (i < 0x10000); i++) {
1851 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1852 break;
1854 if (i < 0x10000) {
1855 uartp[MCFUART_UTB] = ch;
1856 for (i = 0; (i < 0x10000); i++)
1857 if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1858 break;
1860 if (i >= 0x10000)
1861 mcfrs_init_console(); /* try and get it back */
1862 local_irq_restore(flags);
1864 return;
1869 * rs_console_write is registered for printk output.
1872 void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1874 if (!mcfrs_console_inited)
1875 mcfrs_init_console();
1876 while (len-- > 0) {
1877 if (*p == '\n')
1878 mcfrs_put_char('\r');
1879 mcfrs_put_char(*p++);
1884 * declare our consoles
1887 struct console mcfrs_console = {
1888 .name = "ttyS",
1889 .write = mcfrs_console_write,
1890 .device = mcfrs_console_device,
1891 .setup = mcfrs_console_setup,
1892 .flags = CON_PRINTBUFFER,
1893 .index = -1,
1896 static int __init mcfrs_console_init(void)
1898 register_console(&mcfrs_console);
1899 return 0;
1902 console_initcall(mcfrs_console_init);
1904 /****************************************************************************/