vserver 1.9.3
[linux-2.6.git] / drivers / serial / mcfserial.c
1 /*
2  * mcfserial.c -- serial driver for ColdFire internal UARTS.
3  *
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> 
7  *
8  * Based on code from 68332serial.c which was:
9  *
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>
13  *
14  * Changes:
15  * 08/07/2003    Daniele Bellucci <bellucda@tiscali.it>
16  *               some cleanups in mcfrs_write.
17  *
18  */
19  
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
38 #include <asm/io.h>
39 #include <asm/irq.h>
40 #include <asm/system.h>
41 #include <asm/segment.h>
42 #include <asm/semaphore.h>
43 #include <asm/bitops.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"
51
52 struct timer_list mcfrs_timer_struct;
53
54 /*
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.
59  */
60 #if defined(CONFIG_ARNEWSH) || defined(CONFIG_MOTOROLA) || defined(CONFIG_senTec)
61 #define CONSOLE_BAUD_RATE       19200
62 #define DEFAULT_CBAUD           B19200
63 #endif
64
65 #ifndef CONSOLE_BAUD_RATE
66 #define CONSOLE_BAUD_RATE       9600
67 #define DEFAULT_CBAUD           B9600
68 #endif
69
70 int mcfrs_console_inited = 0;
71 int mcfrs_console_port = -1;
72 int mcfrs_console_baud = CONSOLE_BAUD_RATE;
73 int mcfrs_console_cbaud = DEFAULT_CBAUD;
74
75 /*
76  *      Driver data structures.
77  */
78 static struct tty_driver *mcfrs_serial_driver;
79
80 /* number of characters left in xmit buffer before we ask for more */
81 #define WAKEUP_CHARS 256
82
83 /* Debugging...
84  */
85 #undef SERIAL_DEBUG_OPEN
86 #undef SERIAL_DEBUG_FLOW
87
88 #ifdef CONFIG_M5282
89 #define IRQBASE 77
90 #else
91 #define IRQBASE 73
92 #endif
93
94 /*
95  *      Configuration table, UARTs to look for at startup.
96  */
97 static struct mcf_serial mcfrs_table[] = {
98         {  /* ttyS0 */
99                 .magic = 0,
100                 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE1),
101                 .irq = IRQBASE,
102                 .flags = ASYNC_BOOT_AUTOCONF,
103         },
104         {  /* ttyS1 */
105                 .magic = 0,
106                 .addr = (volatile unsigned char *) (MCF_MBAR+MCFUART_BASE2),
107                 .irq = IRQBASE+1,
108                 .flags = ASYNC_BOOT_AUTOCONF,
109         },
110 };
111
112
113 #define NR_PORTS        (sizeof(mcfrs_table) / sizeof(struct mcf_serial))
114
115 /*
116  * This is used to figure out the divisor speeds and the timeouts.
117  */
118 static int mcfrs_baud_table[] = {
119         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
120         9600, 19200, 38400, 57600, 115200, 230400, 460800, 0
121 };
122 #define MCFRS_BAUD_TABLE_SIZE \
123                         (sizeof(mcfrs_baud_table)/sizeof(mcfrs_baud_table[0]))
124
125
126 #ifdef CONFIG_MAGIC_SYSRQ
127 /*
128  *      Magic system request keys. Used for debugging...
129  */
130 extern int      magic_sysrq_key(int ch);
131 #endif
132
133
134 /*
135  * tmp_buf is used as a temporary buffer by serial_write.  We need to
136  * lock it in case the copy_from_user blocks while swapping in a page,
137  * and some other program tries to do a serial write at the same time.
138  * Since the lock will only come under contention when the system is
139  * swapping and available memory is low, it makes sense to share one
140  * buffer across all the serial ports, since it significantly saves
141  * memory if large numbers of serial ports are open.
142  */
143 static unsigned char mcfrs_tmp_buf[4096]; /* This is cheating */
144 static DECLARE_MUTEX(mcfrs_tmp_buf_sem);
145
146 /*
147  *      Forware declarations...
148  */
149 static void     mcfrs_change_speed(struct mcf_serial *info);
150 static void     mcfrs_wait_until_sent(struct tty_struct *tty, int timeout);
151
152
153 static inline int serial_paranoia_check(struct mcf_serial *info,
154                                         char *name, const char *routine)
155 {
156 #ifdef SERIAL_PARANOIA_CHECK
157         static const char badmagic[] =
158                 "MCFRS(warning): bad magic number for serial struct %s in %s\n";
159         static const char badinfo[] =
160                 "MCFRS(warning): null mcf_serial for %s in %s\n";
161
162         if (!info) {
163                 printk(badinfo, name, routine);
164                 return 1;
165         }
166         if (info->magic != SERIAL_MAGIC) {
167                 printk(badmagic, name, routine);
168                 return 1;
169         }
170 #endif
171         return 0;
172 }
173
174 /*
175  *      Sets or clears DTR and RTS on the requested line.
176  */
177 static void mcfrs_setsignals(struct mcf_serial *info, int dtr, int rts)
178 {
179         volatile unsigned char  *uartp;
180         unsigned long           flags;
181         
182 #if 0
183         printk("%s(%d): mcfrs_setsignals(info=%x,dtr=%d,rts=%d)\n",
184                 __FILE__, __LINE__, info, dtr, rts);
185 #endif
186
187         local_irq_save(flags);
188         if (dtr >= 0) {
189 #ifdef MCFPP_DTR0
190                 if (info->line)
191                         mcf_setppdata(MCFPP_DTR1, (dtr ? 0 : MCFPP_DTR1));
192                 else
193                         mcf_setppdata(MCFPP_DTR0, (dtr ? 0 : MCFPP_DTR0));
194 #endif
195         }
196         if (rts >= 0) {
197                 uartp = info->addr;
198                 if (rts) {
199                         info->sigs |= TIOCM_RTS;
200                         uartp[MCFUART_UOP1] = MCFUART_UOP_RTS;
201                 } else {
202                         info->sigs &= ~TIOCM_RTS;
203                         uartp[MCFUART_UOP0] = MCFUART_UOP_RTS;
204                 }
205         }
206         local_irq_restore(flags);
207         return;
208 }
209
210 /*
211  *      Gets values of serial signals.
212  */
213 static int mcfrs_getsignals(struct mcf_serial *info)
214 {
215         volatile unsigned char  *uartp;
216         unsigned long           flags;
217         int                     sigs;
218 #if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
219         unsigned short          ppdata;
220 #endif
221
222 #if 0
223         printk("%s(%d): mcfrs_getsignals(info=%x)\n", __FILE__, __LINE__);
224 #endif
225
226         local_irq_save(flags);
227         uartp = info->addr;
228         sigs = (uartp[MCFUART_UIPR] & MCFUART_UIPR_CTS) ? 0 : TIOCM_CTS;
229         sigs |= (info->sigs & TIOCM_RTS);
230
231 #ifdef MCFPP_DCD0
232 {
233         unsigned int ppdata;
234         ppdata = mcf_getppdata();
235         if (info->line == 0) {
236                 sigs |= (ppdata & MCFPP_DCD0) ? 0 : TIOCM_CD;
237                 sigs |= (ppdata & MCFPP_DTR0) ? 0 : TIOCM_DTR;
238         } else if (info->line == 1) {
239                 sigs |= (ppdata & MCFPP_DCD1) ? 0 : TIOCM_CD;
240                 sigs |= (ppdata & MCFPP_DTR1) ? 0 : TIOCM_DTR;
241         }
242 }
243 #endif
244
245         local_irq_restore(flags);
246         return(sigs);
247 }
248
249 /*
250  * ------------------------------------------------------------
251  * mcfrs_stop() and mcfrs_start()
252  *
253  * This routines are called before setting or resetting tty->stopped.
254  * They enable or disable transmitter interrupts, as necessary.
255  * ------------------------------------------------------------
256  */
257 static void mcfrs_stop(struct tty_struct *tty)
258 {
259         volatile unsigned char  *uartp;
260         struct mcf_serial       *info = (struct mcf_serial *)tty->driver_data;
261         unsigned long           flags;
262
263         if (serial_paranoia_check(info, tty->name, "mcfrs_stop"))
264                 return;
265         
266         local_irq_save(flags);
267         uartp = info->addr;
268         info->imr &= ~MCFUART_UIR_TXREADY;
269         uartp[MCFUART_UIMR] = info->imr;
270         local_irq_restore(flags);
271 }
272
273 static void mcfrs_start(struct tty_struct *tty)
274 {
275         volatile unsigned char  *uartp;
276         struct mcf_serial       *info = (struct mcf_serial *)tty->driver_data;
277         unsigned long           flags;
278         
279         if (serial_paranoia_check(info, tty->name, "mcfrs_start"))
280                 return;
281
282         local_irq_save(flags);
283         if (info->xmit_cnt && info->xmit_buf) {
284                 uartp = info->addr;
285                 info->imr |= MCFUART_UIR_TXREADY;
286                 uartp[MCFUART_UIMR] = info->imr;
287         }
288         local_irq_restore(flags);
289 }
290
291 /*
292  * ----------------------------------------------------------------------
293  *
294  * Here starts the interrupt handling routines.  All of the following
295  * subroutines are declared as inline and are folded into
296  * mcfrs_interrupt().  They were separated out for readability's sake.
297  *
298  * Note: mcfrs_interrupt() is a "fast" interrupt, which means that it
299  * runs with interrupts turned off.  People who may want to modify
300  * mcfrs_interrupt() should try to keep the interrupt handler as fast as
301  * possible.  After you are done making modifications, it is not a bad
302  * idea to do:
303  * 
304  * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
305  *
306  * and look at the resulting assemble code in serial.s.
307  *
308  *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
309  * -----------------------------------------------------------------------
310  */
311
312 static inline void receive_chars(struct mcf_serial *info)
313 {
314         volatile unsigned char  *uartp;
315         struct tty_struct       *tty = info->tty;
316         unsigned char           status, ch;
317
318         if (!tty)
319                 return;
320
321         uartp = info->addr;
322
323         while ((status = uartp[MCFUART_USR]) & MCFUART_USR_RXREADY) {
324
325                 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
326                         break;
327
328                 ch = uartp[MCFUART_URB];
329                 info->stats.rx++;
330
331 #ifdef CONFIG_MAGIC_SYSRQ
332                 if (mcfrs_console_inited && (info->line == mcfrs_console_port)) {
333                         if (magic_sysrq_key(ch))
334                                 continue;
335                 }
336 #endif
337
338                 tty->flip.count++;
339                 if (status & MCFUART_USR_RXERR)
340                         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETERR;
341                 if (status & MCFUART_USR_RXBREAK) {
342                         info->stats.rxbreak++;
343                         *tty->flip.flag_buf_ptr++ = TTY_BREAK;
344                 } else if (status & MCFUART_USR_RXPARITY) {
345                         info->stats.rxparity++;
346                         *tty->flip.flag_buf_ptr++ = TTY_PARITY;
347                 } else if (status & MCFUART_USR_RXOVERRUN) {
348                         info->stats.rxoverrun++;
349                         *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
350                 } else if (status & MCFUART_USR_RXFRAMING) {
351                         info->stats.rxframing++;
352                         *tty->flip.flag_buf_ptr++ = TTY_FRAME;
353                 } else {
354                         *tty->flip.flag_buf_ptr++ = 0;
355                 }
356                 *tty->flip.char_buf_ptr++ = ch;
357         }
358
359         schedule_work(&tty->flip.work);
360         return;
361 }
362
363 static inline void transmit_chars(struct mcf_serial *info)
364 {
365         volatile unsigned char  *uartp;
366
367         uartp = info->addr;
368
369         if (info->x_char) {
370                 /* Send special char - probably flow control */
371                 uartp[MCFUART_UTB] = info->x_char;
372                 info->x_char = 0;
373                 info->stats.tx++;
374         }
375
376         if ((info->xmit_cnt <= 0) || info->tty->stopped) {
377                 info->imr &= ~MCFUART_UIR_TXREADY;
378                 uartp[MCFUART_UIMR] = info->imr;
379                 return;
380         }
381
382         while (uartp[MCFUART_USR] & MCFUART_USR_TXREADY) {
383                 uartp[MCFUART_UTB] = info->xmit_buf[info->xmit_tail++];
384                 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
385                 info->stats.tx++;
386                 if (--info->xmit_cnt <= 0)
387                         break;
388         }
389
390         if (info->xmit_cnt < WAKEUP_CHARS)
391                 schedule_work(&info->tqueue);
392         return;
393 }
394
395 /*
396  * This is the serial driver's generic interrupt routine
397  */
398 irqreturn_t mcfrs_interrupt(int irq, void *dev_id, struct pt_regs *regs)
399 {
400         struct mcf_serial       *info;
401         unsigned char           isr;
402
403         info = &mcfrs_table[(irq - IRQBASE)];
404         isr = info->addr[MCFUART_UISR] & info->imr;
405
406         if (isr & MCFUART_UIR_RXREADY)
407                 receive_chars(info);
408         if (isr & MCFUART_UIR_TXREADY)
409                 transmit_chars(info);
410         return IRQ_HANDLED;
411 }
412
413 /*
414  * -------------------------------------------------------------------
415  * Here ends the serial interrupt routines.
416  * -------------------------------------------------------------------
417  */
418
419 static void mcfrs_offintr(void *private)
420 {
421         struct mcf_serial       *info = (struct mcf_serial *) private;
422         struct tty_struct       *tty;
423         
424         tty = info->tty;
425         if (!tty)
426                 return;
427         tty_wakeup(tty);
428 }
429
430
431 /*
432  *      Change of state on a DCD line.
433  */
434 void mcfrs_modem_change(struct mcf_serial *info, int dcd)
435 {
436         if (info->count == 0)
437                 return;
438
439         if (info->flags & ASYNC_CHECK_CD) {
440                 if (dcd)
441                         wake_up_interruptible(&info->open_wait);
442                 else 
443                         schedule_work(&info->tqueue_hangup);
444         }
445 }
446
447
448 #ifdef MCFPP_DCD0
449
450 unsigned short  mcfrs_ppstatus;
451
452 /*
453  * This subroutine is called when the RS_TIMER goes off. It is used
454  * to monitor the state of the DCD lines - since they have no edge
455  * sensors and interrupt generators.
456  */
457 static void mcfrs_timer(void)
458 {
459         unsigned int    ppstatus, dcdval, i;
460
461         ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
462
463         if (ppstatus != mcfrs_ppstatus) {
464                 for (i = 0; (i < 2); i++) {
465                         dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
466                         if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
467                                 mcfrs_modem_change(&mcfrs_table[i],
468                                         ((ppstatus & dcdval) ? 0 : 1));
469                         }
470                 }
471         }
472         mcfrs_ppstatus = ppstatus;
473
474         /* Re-arm timer */
475         mcfrs_timer_struct.expires = jiffies + HZ/25;
476         add_timer(&mcfrs_timer_struct);
477 }
478
479 #endif  /* MCFPP_DCD0 */
480
481
482 /*
483  * This routine is called from the scheduler tqueue when the interrupt
484  * routine has signalled that a hangup has occurred. The path of
485  * hangup processing is:
486  *
487  *      serial interrupt routine -> (scheduler tqueue) ->
488  *      do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
489  * 
490  */
491 static void do_serial_hangup(void *private)
492 {
493         struct mcf_serial       *info = (struct mcf_serial *) private;
494         struct tty_struct       *tty;
495         
496         tty = info->tty;
497         if (!tty)
498                 return;
499
500         tty_hangup(tty);
501 }
502
503 static int startup(struct mcf_serial * info)
504 {
505         volatile unsigned char  *uartp;
506         unsigned long           flags;
507         
508         if (info->flags & ASYNC_INITIALIZED)
509                 return 0;
510
511         if (!info->xmit_buf) {
512                 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
513                 if (!info->xmit_buf)
514                         return -ENOMEM;
515         }
516
517         local_irq_save(flags);
518
519 #ifdef SERIAL_DEBUG_OPEN
520         printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
521 #endif
522
523         /*
524          *      Reset UART, get it into known state...
525          */
526         uartp = info->addr;
527         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX;  /* reset RX */
528         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX;  /* reset TX */
529         mcfrs_setsignals(info, 1, 1);
530
531         if (info->tty)
532                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
533         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
534
535         /*
536          * and set the speed of the serial port
537          */
538         mcfrs_change_speed(info);
539
540         /*
541          * Lastly enable the UART transmitter and receiver, and
542          * interrupt enables.
543          */
544         info->imr = MCFUART_UIR_RXREADY;
545         uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
546         uartp[MCFUART_UIMR] = info->imr;
547
548         info->flags |= ASYNC_INITIALIZED;
549         local_irq_restore(flags);
550         return 0;
551 }
552
553 /*
554  * This routine will shutdown a serial port; interrupts are disabled, and
555  * DTR is dropped if the hangup on close termio flag is on.
556  */
557 static void shutdown(struct mcf_serial * info)
558 {
559         volatile unsigned char  *uartp;
560         unsigned long           flags;
561
562         if (!(info->flags & ASYNC_INITIALIZED))
563                 return;
564
565 #ifdef SERIAL_DEBUG_OPEN
566         printk("Shutting down serial port %d (irq %d)....\n", info->line,
567                info->irq);
568 #endif
569         
570         local_irq_save(flags);
571
572         uartp = info->addr;
573         uartp[MCFUART_UIMR] = 0;  /* mask all interrupts */
574         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX;  /* reset RX */
575         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX;  /* reset TX */
576
577         if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
578                 mcfrs_setsignals(info, 0, 0);
579
580         if (info->xmit_buf) {
581                 free_page((unsigned long) info->xmit_buf);
582                 info->xmit_buf = 0;
583         }
584
585         if (info->tty)
586                 set_bit(TTY_IO_ERROR, &info->tty->flags);
587         
588         info->flags &= ~ASYNC_INITIALIZED;
589         local_irq_restore(flags);
590 }
591
592
593 /*
594  * This routine is called to set the UART divisor registers to match
595  * the specified baud rate for a serial port.
596  */
597 static void mcfrs_change_speed(struct mcf_serial *info)
598 {
599         volatile unsigned char  *uartp;
600         unsigned int            baudclk, cflag;
601         unsigned long           flags;
602         unsigned char           mr1, mr2;
603         int                     i;
604 #ifdef  CONFIG_M5272
605         unsigned int            fraction;
606 #endif
607
608         if (!info->tty || !info->tty->termios)
609                 return;
610         cflag = info->tty->termios->c_cflag;
611         if (info->addr == 0)
612                 return;
613
614 #if 0
615         printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
616 #endif
617
618         i = cflag & CBAUD;
619         if (i & CBAUDEX) {
620                 i &= ~CBAUDEX;
621                 if (i < 1 || i > 4)
622                         info->tty->termios->c_cflag &= ~CBAUDEX;
623                 else
624                         i += 15;
625         }
626         if (i == 0) {
627                 mcfrs_setsignals(info, 0, -1);
628                 return;
629         }
630
631         /* compute the baudrate clock */
632 #ifdef  CONFIG_M5272
633         /*
634          * For the MCF5272, also compute the baudrate fraction.
635          */
636         baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
637         fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
638         fraction *= 16;
639         fraction /= (32 * mcfrs_baud_table[i]);
640 #else
641         baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
642 #endif
643
644         info->baud = mcfrs_baud_table[i];
645
646         mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
647         mr2 = 0;
648
649         switch (cflag & CSIZE) {
650         case CS5:       mr1 |= MCFUART_MR1_CS5; break;
651         case CS6:       mr1 |= MCFUART_MR1_CS6; break;
652         case CS7:       mr1 |= MCFUART_MR1_CS7; break;
653         case CS8:
654         default:        mr1 |= MCFUART_MR1_CS8; break;
655         }
656
657         if (cflag & PARENB) {
658                 if (cflag & CMSPAR) {
659                         if (cflag & PARODD)
660                                 mr1 |= MCFUART_MR1_PARITYMARK;
661                         else
662                                 mr1 |= MCFUART_MR1_PARITYSPACE;
663                 } else {
664                         if (cflag & PARODD)
665                                 mr1 |= MCFUART_MR1_PARITYODD;
666                         else
667                                 mr1 |= MCFUART_MR1_PARITYEVEN;
668                 }
669         } else {
670                 mr1 |= MCFUART_MR1_PARITYNONE;
671         }
672
673         if (cflag & CSTOPB)
674                 mr2 |= MCFUART_MR2_STOP2;
675         else
676                 mr2 |= MCFUART_MR2_STOP1;
677
678         if (cflag & CRTSCTS) {
679                 mr1 |= MCFUART_MR1_RXRTS;
680                 mr2 |= MCFUART_MR2_TXCTS;
681         }
682
683         if (cflag & CLOCAL)
684                 info->flags &= ~ASYNC_CHECK_CD;
685         else
686                 info->flags |= ASYNC_CHECK_CD;
687
688         uartp = info->addr;
689
690         local_irq_save(flags);
691 #if 0
692         printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
693                 mr1, mr2, baudclk);
694 #endif
695         /*
696           Note: pg 12-16 of MCF5206e User's Manual states that a
697           software reset should be performed prior to changing
698           UMR1,2, UCSR, UACR, bit 7
699         */
700         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX;    /* reset RX */
701         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX;    /* reset TX */
702         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
703         uartp[MCFUART_UMR] = mr1;
704         uartp[MCFUART_UMR] = mr2;
705         uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8;  /* set msb byte */
706         uartp[MCFUART_UBG2] = (baudclk & 0xff);         /* set lsb byte */
707 #ifdef  CONFIG_M5272
708         uartp[MCFUART_UFPD] = (fraction & 0xf);         /* set fraction */
709 #endif
710         uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
711         uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
712         mcfrs_setsignals(info, 1, -1);
713         local_irq_restore(flags);
714         return;
715 }
716
717 static void mcfrs_flush_chars(struct tty_struct *tty)
718 {
719         volatile unsigned char  *uartp;
720         struct mcf_serial       *info = (struct mcf_serial *)tty->driver_data;
721         unsigned long           flags;
722
723         if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
724                 return;
725
726         if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
727             !info->xmit_buf)
728                 return;
729
730         /* Enable transmitter */
731         local_irq_save(flags);
732         uartp = info->addr;
733         info->imr |= MCFUART_UIR_TXREADY;
734         uartp[MCFUART_UIMR] = info->imr;
735         local_irq_restore(flags);
736 }
737
738 static int mcfrs_write(struct tty_struct * tty, int from_user,
739                     const unsigned char *buf, int count)
740 {
741         volatile unsigned char  *uartp;
742         struct mcf_serial       *info = (struct mcf_serial *)tty->driver_data;
743         unsigned long           flags;
744         int                     c, total = 0;
745
746 #if 0
747         printk("%s(%d): mcfrs_write(tty=%x,from_user=%d,buf=%x,count=%d)\n",
748                 __FILE__, __LINE__, (int)tty, from_user, (int)buf, count);
749 #endif
750
751         if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
752                 return 0;
753
754         if (!tty || !info->xmit_buf)
755                 return 0;
756         
757         local_save_flags(flags);
758         while (1) {
759                 local_irq_disable();            
760                 c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
761                         ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
762                 local_irq_restore(flags);
763
764                 if (c <= 0)
765                         break;
766
767                 if (from_user) {
768                         down(&mcfrs_tmp_buf_sem);
769                         if (copy_from_user(mcfrs_tmp_buf, buf, c))
770                                 return -EFAULT;
771
772                         local_irq_disable();
773                         c = min(c, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
774                                        ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
775                         local_irq_restore(flags);
776                         memcpy(info->xmit_buf + info->xmit_head, mcfrs_tmp_buf, c);
777                         up(&mcfrs_tmp_buf_sem);
778                 } else
779                         memcpy(info->xmit_buf + info->xmit_head, buf, c);
780
781                 local_irq_disable();
782                 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
783                 info->xmit_cnt += c;
784                 local_irq_restore(flags);
785
786                 buf += c;
787                 count -= c;
788                 total += c;
789         }
790
791         local_irq_disable();
792         uartp = info->addr;
793         info->imr |= MCFUART_UIR_TXREADY;
794         uartp[MCFUART_UIMR] = info->imr;
795         local_irq_restore(flags);
796
797         return total;
798 }
799
800 static int mcfrs_write_room(struct tty_struct *tty)
801 {
802         struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
803         int     ret;
804
805         if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
806                 return 0;
807         ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
808         if (ret < 0)
809                 ret = 0;
810         return ret;
811 }
812
813 static int mcfrs_chars_in_buffer(struct tty_struct *tty)
814 {
815         struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
816
817         if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
818                 return 0;
819         return info->xmit_cnt;
820 }
821
822 static void mcfrs_flush_buffer(struct tty_struct *tty)
823 {
824         struct mcf_serial       *info = (struct mcf_serial *)tty->driver_data;
825         unsigned long           flags;
826
827         if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
828                 return;
829
830         local_irq_save(flags);
831         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
832         local_irq_restore(flags);
833
834         tty_wakeup(tty);
835 }
836
837 /*
838  * ------------------------------------------------------------
839  * mcfrs_throttle()
840  * 
841  * This routine is called by the upper-layer tty layer to signal that
842  * incoming characters should be throttled.
843  * ------------------------------------------------------------
844  */
845 static void mcfrs_throttle(struct tty_struct * tty)
846 {
847         struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
848 #ifdef SERIAL_DEBUG_THROTTLE
849         char    buf[64];
850         
851         printk("throttle %s: %d....\n", _tty_name(tty, buf),
852                tty->ldisc.chars_in_buffer(tty));
853 #endif
854
855         if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
856                 return;
857         
858         if (I_IXOFF(tty))
859                 info->x_char = STOP_CHAR(tty);
860
861         /* Turn off RTS line (do this atomic) */
862 }
863
864 static void mcfrs_unthrottle(struct tty_struct * tty)
865 {
866         struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
867 #ifdef SERIAL_DEBUG_THROTTLE
868         char    buf[64];
869         
870         printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
871                tty->ldisc.chars_in_buffer(tty));
872 #endif
873
874         if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
875                 return;
876         
877         if (I_IXOFF(tty)) {
878                 if (info->x_char)
879                         info->x_char = 0;
880                 else
881                         info->x_char = START_CHAR(tty);
882         }
883
884         /* Assert RTS line (do this atomic) */
885 }
886
887 /*
888  * ------------------------------------------------------------
889  * mcfrs_ioctl() and friends
890  * ------------------------------------------------------------
891  */
892
893 static int get_serial_info(struct mcf_serial * info,
894                            struct serial_struct * retinfo)
895 {
896         struct serial_struct tmp;
897   
898         if (!retinfo)
899                 return -EFAULT;
900         memset(&tmp, 0, sizeof(tmp));
901         tmp.type = info->type;
902         tmp.line = info->line;
903         tmp.port = (unsigned int) info->addr;
904         tmp.irq = info->irq;
905         tmp.flags = info->flags;
906         tmp.baud_base = info->baud_base;
907         tmp.close_delay = info->close_delay;
908         tmp.closing_wait = info->closing_wait;
909         tmp.custom_divisor = info->custom_divisor;
910         return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
911 }
912
913 static int set_serial_info(struct mcf_serial * info,
914                            struct serial_struct * new_info)
915 {
916         struct serial_struct new_serial;
917         struct mcf_serial old_info;
918         int     retval = 0;
919
920         if (!new_info)
921                 return -EFAULT;
922         if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
923                 return -EFAULT;
924         old_info = *info;
925
926         if (!capable(CAP_SYS_ADMIN)) {
927                 if ((new_serial.baud_base != info->baud_base) ||
928                     (new_serial.type != info->type) ||
929                     (new_serial.close_delay != info->close_delay) ||
930                     ((new_serial.flags & ~ASYNC_USR_MASK) !=
931                      (info->flags & ~ASYNC_USR_MASK)))
932                         return -EPERM;
933                 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
934                                (new_serial.flags & ASYNC_USR_MASK));
935                 info->custom_divisor = new_serial.custom_divisor;
936                 goto check_and_exit;
937         }
938
939         if (info->count > 1)
940                 return -EBUSY;
941
942         /*
943          * OK, past this point, all the error checking has been done.
944          * At this point, we start making changes.....
945          */
946
947         info->baud_base = new_serial.baud_base;
948         info->flags = ((info->flags & ~ASYNC_FLAGS) |
949                         (new_serial.flags & ASYNC_FLAGS));
950         info->type = new_serial.type;
951         info->close_delay = new_serial.close_delay;
952         info->closing_wait = new_serial.closing_wait;
953
954 check_and_exit:
955         retval = startup(info);
956         return retval;
957 }
958
959 /*
960  * get_lsr_info - get line status register info
961  *
962  * Purpose: Let user call ioctl() to get info when the UART physically
963  *          is emptied.  On bus types like RS485, the transmitter must
964  *          release the bus after transmitting. This must be done when
965  *          the transmit shift register is empty, not be done when the
966  *          transmit holding register is empty.  This functionality
967  *          allows an RS485 driver to be written in user space. 
968  */
969 static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
970 {
971         volatile unsigned char  *uartp;
972         unsigned long           flags;
973         unsigned char           status;
974
975         local_irq_save(flags);
976         uartp = info->addr;
977         status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
978         local_irq_restore(flags);
979
980         return put_user(status,value);
981 }
982
983 /*
984  * This routine sends a break character out the serial port.
985  */
986 static void send_break( struct mcf_serial * info, int duration)
987 {
988         volatile unsigned char  *uartp;
989         unsigned long           flags;
990
991         if (!info->addr)
992                 return;
993         current->state = TASK_INTERRUPTIBLE;
994         uartp = info->addr;
995
996         local_irq_save(flags);
997         uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
998         schedule_timeout(jiffies + duration);
999         uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
1000         local_irq_restore(flags);
1001 }
1002
1003 static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
1004 {
1005         struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1006
1007         if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1008                 return -ENODEV;
1009         if (tty->flags & (1 << TTY_IO_ERROR))
1010                 return -EIO;
1011
1012         return mcfrs_getsignals(info);
1013 }
1014
1015 static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
1016                           unsigned int set, unsigned int clear)
1017 {
1018         struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1019         int rts = -1, dtr = -1;
1020
1021         if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1022                 return -ENODEV;
1023         if (tty->flags & (1 << TTY_IO_ERROR))
1024                 return -EIO;
1025
1026         if (set & TIOCM_RTS)
1027                 rts = 1;
1028         if (set & TIOCM_DTR)
1029                 dtr = 1;
1030         if (clear & TIOCM_RTS)
1031                 rts = 0;
1032         if (clear & TIOCM_DTR)
1033                 dtr = 0;
1034
1035         mcfrs_setsignals(info, dtr, rts);
1036
1037         return 0;
1038 }
1039
1040 static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
1041                     unsigned int cmd, unsigned long arg)
1042 {
1043         struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1044         int retval, error;
1045
1046         if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1047                 return -ENODEV;
1048
1049         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1050             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1051             (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1052                 if (tty->flags & (1 << TTY_IO_ERROR))
1053                     return -EIO;
1054         }
1055         
1056         switch (cmd) {
1057                 case TCSBRK:    /* SVID version: non-zero arg --> no break */
1058                         retval = tty_check_change(tty);
1059                         if (retval)
1060                                 return retval;
1061                         tty_wait_until_sent(tty, 0);
1062                         if (!arg)
1063                                 send_break(info, HZ/4); /* 1/4 second */
1064                         return 0;
1065                 case TCSBRKP:   /* support for POSIX tcsendbreak() */
1066                         retval = tty_check_change(tty);
1067                         if (retval)
1068                                 return retval;
1069                         tty_wait_until_sent(tty, 0);
1070                         send_break(info, arg ? arg*(HZ/10) : HZ/4);
1071                         return 0;
1072                 case TIOCGSOFTCAR:
1073                         error = put_user(C_CLOCAL(tty) ? 1 : 0,
1074                                     (unsigned long *) arg);
1075                         if (error)
1076                                 return error;
1077                         return 0;
1078                 case TIOCSSOFTCAR:
1079                         get_user(arg, (unsigned long *) arg);
1080                         tty->termios->c_cflag =
1081                                 ((tty->termios->c_cflag & ~CLOCAL) |
1082                                  (arg ? CLOCAL : 0));
1083                         return 0;
1084                 case TIOCGSERIAL:
1085                         error = verify_area(VERIFY_WRITE, (void *) arg,
1086                                                 sizeof(struct serial_struct));
1087                         if (error)
1088                                 return error;
1089                         return get_serial_info(info,
1090                                                (struct serial_struct *) arg);
1091                 case TIOCSSERIAL:
1092                         return set_serial_info(info,
1093                                                (struct serial_struct *) arg);
1094                 case TIOCSERGETLSR: /* Get line status register */
1095                         error = verify_area(VERIFY_WRITE, (void *) arg,
1096                                 sizeof(unsigned int));
1097                         if (error)
1098                                 return error;
1099                         else
1100                             return get_lsr_info(info, (unsigned int *) arg);
1101
1102                 case TIOCSERGSTRUCT:
1103                         error = copy_to_user((struct mcf_serial *) arg,
1104                                     info, sizeof(struct mcf_serial));
1105                         if (error)
1106                                 return -EFAULT;
1107                         return 0;
1108                         
1109 #ifdef TIOCSET422
1110                 case TIOCSET422: {
1111                         unsigned int val;
1112                         get_user(val, (unsigned int *) arg);
1113                         mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1114                         break;
1115                 }
1116                 case TIOCGET422: {
1117                         unsigned int val;
1118                         val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1119                         put_user(val, (unsigned int *) arg);
1120                         break;
1121                 }
1122 #endif
1123
1124                 default:
1125                         return -ENOIOCTLCMD;
1126                 }
1127         return 0;
1128 }
1129
1130 static void mcfrs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1131 {
1132         struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1133
1134         if (tty->termios->c_cflag == old_termios->c_cflag)
1135                 return;
1136
1137         mcfrs_change_speed(info);
1138
1139         if ((old_termios->c_cflag & CRTSCTS) &&
1140             !(tty->termios->c_cflag & CRTSCTS)) {
1141                 tty->hw_stopped = 0;
1142                 mcfrs_setsignals(info, -1, 1);
1143 #if 0
1144                 mcfrs_start(tty);
1145 #endif
1146         }
1147 }
1148
1149 /*
1150  * ------------------------------------------------------------
1151  * mcfrs_close()
1152  * 
1153  * This routine is called when the serial port gets closed.  First, we
1154  * wait for the last remaining data to be sent.  Then, we unlink its
1155  * S structure from the interrupt chain if necessary, and we free
1156  * that IRQ if nothing is left in the chain.
1157  * ------------------------------------------------------------
1158  */
1159 static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1160 {
1161         volatile unsigned char  *uartp;
1162         struct mcf_serial       *info = (struct mcf_serial *)tty->driver_data;
1163         unsigned long           flags;
1164
1165         if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1166                 return;
1167         
1168         local_irq_save(flags);
1169         
1170         if (tty_hung_up_p(filp)) {
1171                 local_irq_restore(flags);
1172                 return;
1173         }
1174         
1175 #ifdef SERIAL_DEBUG_OPEN
1176         printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1177 #endif
1178         if ((tty->count == 1) && (info->count != 1)) {
1179                 /*
1180                  * Uh, oh.  tty->count is 1, which means that the tty
1181                  * structure will be freed.  Info->count should always
1182                  * be one in these conditions.  If it's greater than
1183                  * one, we've got real problems, since it means the
1184                  * serial port won't be shutdown.
1185                  */
1186                 printk("MCFRS: bad serial port count; tty->count is 1, "
1187                        "info->count is %d\n", info->count);
1188                 info->count = 1;
1189         }
1190         if (--info->count < 0) {
1191                 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1192                        info->line, info->count);
1193                 info->count = 0;
1194         }
1195         if (info->count) {
1196                 local_irq_restore(flags);
1197                 return;
1198         }
1199         info->flags |= ASYNC_CLOSING;
1200
1201         /*
1202          * Now we wait for the transmit buffer to clear; and we notify 
1203          * the line discipline to only process XON/XOFF characters.
1204          */
1205         tty->closing = 1;
1206         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1207                 tty_wait_until_sent(tty, info->closing_wait);
1208
1209         /*
1210          * At this point we stop accepting input.  To do this, we
1211          * disable the receive line status interrupts, and tell the
1212          * interrupt driver to stop checking the data ready bit in the
1213          * line status register.
1214          */
1215         info->imr &= ~MCFUART_UIR_RXREADY;
1216         uartp = info->addr;
1217         uartp[MCFUART_UIMR] = info->imr;
1218
1219 #if 0
1220         /* FIXME: do we need to keep this enabled for console?? */
1221         if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
1222                 /* Do not disable the UART */ ;
1223         } else
1224 #endif
1225         shutdown(info);
1226         if (tty->driver->flush_buffer)
1227                 tty->driver->flush_buffer(tty);
1228         tty_ldisc_flush(tty);
1229         
1230         tty->closing = 0;
1231         info->event = 0;
1232         info->tty = 0;
1233 #if 0   
1234         if (tty->ldisc.num != ldiscs[N_TTY].num) {
1235                 if (tty->ldisc.close)
1236                         (tty->ldisc.close)(tty);
1237                 tty->ldisc = ldiscs[N_TTY];
1238                 tty->termios->c_line = N_TTY;
1239                 if (tty->ldisc.open)
1240                         (tty->ldisc.open)(tty);
1241         }
1242 #endif  
1243         if (info->blocked_open) {
1244                 if (info->close_delay) {
1245                         current->state = TASK_INTERRUPTIBLE;
1246                         schedule_timeout(info->close_delay);
1247                 }
1248                 wake_up_interruptible(&info->open_wait);
1249         }
1250         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1251         wake_up_interruptible(&info->close_wait);
1252         local_irq_restore(flags);
1253 }
1254
1255 /*
1256  * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1257  */
1258 static void
1259 mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1260 {
1261 #ifdef  CONFIG_M5272
1262 #define MCF5272_FIFO_SIZE       25              /* fifo size + shift reg */
1263
1264         struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1265         volatile unsigned char *uartp;
1266         unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1267
1268         if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1269                 return;
1270
1271         orig_jiffies = jiffies;
1272
1273         /*
1274          * Set the check interval to be 1/5 of the approximate time
1275          * to send the entire fifo, and make it at least 1.  The check
1276          * interval should also be less than the timeout.
1277          *
1278          * Note: we have to use pretty tight timings here to satisfy
1279          * the NIST-PCTS.
1280          */
1281         fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1282         char_time = fifo_time / 5;
1283         if (char_time == 0)
1284                 char_time = 1;
1285         if (timeout && timeout < char_time)
1286                 char_time = timeout;
1287
1288         /*
1289          * Clamp the timeout period at 2 * the time to empty the
1290          * fifo.  Just to be safe, set the minimum at .5 seconds.
1291          */
1292         fifo_time *= 2;
1293         if (fifo_time < (HZ/2))
1294                 fifo_time = HZ/2;
1295         if (!timeout || timeout > fifo_time)
1296                 timeout = fifo_time;
1297
1298         /*
1299          * Account for the number of bytes in the UART
1300          * transmitter FIFO plus any byte being shifted out.
1301          */
1302         uartp = (volatile unsigned char *) info->addr;
1303         for (;;) {
1304                 fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1305                 if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1306                                 MCFUART_USR_TXEMPTY)) ==
1307                         MCFUART_USR_TXREADY)
1308                         fifo_cnt++;
1309                 if (fifo_cnt == 0)
1310                         break;
1311                 set_current_state(TASK_INTERRUPTIBLE);
1312                 schedule_timeout(char_time);
1313                 if (signal_pending(current))
1314                         break;
1315                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1316                         break;
1317         }
1318 #else
1319         /*
1320          * For the other coldfire models, assume all data has been sent
1321          */
1322 #endif
1323 }
1324
1325 /*
1326  * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1327  */
1328 void mcfrs_hangup(struct tty_struct *tty)
1329 {
1330         struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1331         
1332         if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
1333                 return;
1334         
1335         mcfrs_flush_buffer(tty);
1336         shutdown(info);
1337         info->event = 0;
1338         info->count = 0;
1339         info->flags &= ~ASYNC_NORMAL_ACTIVE;
1340         info->tty = 0;
1341         wake_up_interruptible(&info->open_wait);
1342 }
1343
1344 /*
1345  * ------------------------------------------------------------
1346  * mcfrs_open() and friends
1347  * ------------------------------------------------------------
1348  */
1349 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1350                            struct mcf_serial *info)
1351 {
1352         DECLARE_WAITQUEUE(wait, current);
1353         int     retval;
1354         int     do_clocal = 0;
1355
1356         /*
1357          * If the device is in the middle of being closed, then block
1358          * until it's done, and then try again.
1359          */
1360         if (info->flags & ASYNC_CLOSING) {
1361                 interruptible_sleep_on(&info->close_wait);
1362 #ifdef SERIAL_DO_RESTART
1363                 if (info->flags & ASYNC_HUP_NOTIFY)
1364                         return -EAGAIN;
1365                 else
1366                         return -ERESTARTSYS;
1367 #else
1368                 return -EAGAIN;
1369 #endif
1370         }
1371         
1372         /*
1373          * If non-blocking mode is set, or the port is not enabled,
1374          * then make the check up front and then exit.
1375          */
1376         if ((filp->f_flags & O_NONBLOCK) ||
1377             (tty->flags & (1 << TTY_IO_ERROR))) {
1378                 info->flags |= ASYNC_NORMAL_ACTIVE;
1379                 return 0;
1380         }
1381
1382         if (tty->termios->c_cflag & CLOCAL)
1383                 do_clocal = 1;
1384
1385         /*
1386          * Block waiting for the carrier detect and the line to become
1387          * free (i.e., not in use by the callout).  While we are in
1388          * this loop, info->count is dropped by one, so that
1389          * mcfrs_close() knows when to free things.  We restore it upon
1390          * exit, either normal or abnormal.
1391          */
1392         retval = 0;
1393         add_wait_queue(&info->open_wait, &wait);
1394 #ifdef SERIAL_DEBUG_OPEN
1395         printk("block_til_ready before block: ttyS%d, count = %d\n",
1396                info->line, info->count);
1397 #endif
1398         info->count--;
1399         info->blocked_open++;
1400         while (1) {
1401                 local_irq_disable();
1402                 mcfrs_setsignals(info, 1, 1);
1403                 local_irq_enable();
1404                 current->state = TASK_INTERRUPTIBLE;
1405                 if (tty_hung_up_p(filp) ||
1406                     !(info->flags & ASYNC_INITIALIZED)) {
1407 #ifdef SERIAL_DO_RESTART
1408                         if (info->flags & ASYNC_HUP_NOTIFY)
1409                                 retval = -EAGAIN;
1410                         else
1411                                 retval = -ERESTARTSYS;  
1412 #else
1413                         retval = -EAGAIN;
1414 #endif
1415                         break;
1416                 }
1417                 if (!(info->flags & ASYNC_CLOSING) &&
1418                     (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1419                         break;
1420                 if (signal_pending(current)) {
1421                         retval = -ERESTARTSYS;
1422                         break;
1423                 }
1424 #ifdef SERIAL_DEBUG_OPEN
1425                 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1426                        info->line, info->count);
1427 #endif
1428                 schedule();
1429         }
1430         current->state = TASK_RUNNING;
1431         remove_wait_queue(&info->open_wait, &wait);
1432         if (!tty_hung_up_p(filp))
1433                 info->count++;
1434         info->blocked_open--;
1435 #ifdef SERIAL_DEBUG_OPEN
1436         printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1437                info->line, info->count);
1438 #endif
1439         if (retval)
1440                 return retval;
1441         info->flags |= ASYNC_NORMAL_ACTIVE;
1442         return 0;
1443 }       
1444
1445 /*
1446  * This routine is called whenever a serial port is opened. It
1447  * enables interrupts for a serial port, linking in its structure into
1448  * the IRQ chain.   It also performs the serial-specific
1449  * initialization for the tty structure.
1450  */
1451 int mcfrs_open(struct tty_struct *tty, struct file * filp)
1452 {
1453         struct mcf_serial       *info;
1454         int                     retval, line;
1455
1456         line = tty->index;
1457         if ((line < 0) || (line >= NR_PORTS))
1458                 return -ENODEV;
1459         info = mcfrs_table + line;
1460         if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1461                 return -ENODEV;
1462 #ifdef SERIAL_DEBUG_OPEN
1463         printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1464 #endif
1465         info->count++;
1466         tty->driver_data = info;
1467         info->tty = tty;
1468
1469         /*
1470          * Start up serial port
1471          */
1472         retval = startup(info);
1473         if (retval)
1474                 return retval;
1475
1476         retval = block_til_ready(tty, filp, info);
1477         if (retval) {
1478 #ifdef SERIAL_DEBUG_OPEN
1479                 printk("mcfrs_open returning after block_til_ready with %d\n",
1480                        retval);
1481 #endif
1482                 return retval;
1483         }
1484
1485 #ifdef SERIAL_DEBUG_OPEN
1486         printk("mcfrs_open %s successful...\n", tty->name);
1487 #endif
1488         return 0;
1489 }
1490
1491 /*
1492  *      Based on the line number set up the internal interrupt stuff.
1493  */
1494 static void mcfrs_irqinit(struct mcf_serial *info)
1495 {
1496 #if defined(CONFIG_M5272)
1497         volatile unsigned long  *icrp;
1498         volatile unsigned long  *portp;
1499         volatile unsigned char  *uartp;
1500
1501         uartp = info->addr;
1502         icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1503
1504         switch (info->line) {
1505         case 0:
1506                 *icrp = 0xe0000000;
1507                 break;
1508         case 1:
1509                 *icrp = 0x0e000000;
1510                 break;
1511         default:
1512                 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1513                         info->line);
1514                 return;
1515         }
1516
1517         /* Enable the output lines for the serial ports */
1518         portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
1519         *portp = (*portp & ~0x000000ff) | 0x00000055;
1520         portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
1521         *portp = (*portp & ~0x000003fc) | 0x000002a8;
1522 #elif defined(CONFIG_M5282)
1523         volatile unsigned char *icrp, *uartp;
1524         volatile unsigned long *imrp;
1525
1526         uartp = info->addr;
1527
1528         icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1529                 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1530         *icrp = 0x33; /* UART0 with level 6, priority 3 */
1531
1532         imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1533                 MCFINTC_IMRL);
1534         *imrp &= ~((1 << (info->irq - 64)) | 1);
1535 #else
1536         volatile unsigned char  *icrp, *uartp;
1537
1538         switch (info->line) {
1539         case 0:
1540                 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1541                 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1542                         MCFSIM_ICR_PRI1;
1543                 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1544                 break;
1545         case 1:
1546                 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1547                 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1548                         MCFSIM_ICR_PRI2;
1549                 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1550                 break;
1551         default:
1552                 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1553                         info->line);
1554                 return;
1555         }
1556
1557         uartp = info->addr;
1558         uartp[MCFUART_UIVR] = info->irq;
1559 #endif
1560
1561         /* Clear mask, so no surprise interrupts. */
1562         uartp[MCFUART_UIMR] = 0;
1563
1564         if (request_irq(info->irq, mcfrs_interrupt, SA_INTERRUPT,
1565             "ColdFire UART", NULL)) {
1566                 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1567                         "vector=%d\n", info->line, info->irq);
1568         }
1569
1570         return;
1571 }
1572
1573
1574 char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1575
1576
1577 /*
1578  * Serial stats reporting...
1579  */
1580 int mcfrs_readproc(char *page, char **start, off_t off, int count,
1581                          int *eof, void *data)
1582 {
1583         struct mcf_serial       *info;
1584         char                    str[20];
1585         int                     len, sigs, i;
1586
1587         len = sprintf(page, mcfrs_drivername);
1588         for (i = 0; (i < NR_PORTS); i++) {
1589                 info = &mcfrs_table[i];
1590                 len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1591                         i, (unsigned int) info->addr, info->irq, info->baud);
1592                 if (info->stats.rx || info->stats.tx)
1593                         len += sprintf((page + len), "tx:%d rx:%d ",
1594                         info->stats.tx, info->stats.rx);
1595                 if (info->stats.rxframing)
1596                         len += sprintf((page + len), "fe:%d ",
1597                         info->stats.rxframing);
1598                 if (info->stats.rxparity)
1599                         len += sprintf((page + len), "pe:%d ",
1600                         info->stats.rxparity);
1601                 if (info->stats.rxbreak)
1602                         len += sprintf((page + len), "brk:%d ",
1603                         info->stats.rxbreak);
1604                 if (info->stats.rxoverrun)
1605                         len += sprintf((page + len), "oe:%d ",
1606                         info->stats.rxoverrun);
1607
1608                 str[0] = str[1] = 0;
1609                 if ((sigs = mcfrs_getsignals(info))) {
1610                         if (sigs & TIOCM_RTS)
1611                                 strcat(str, "|RTS");
1612                         if (sigs & TIOCM_CTS)
1613                                 strcat(str, "|CTS");
1614                         if (sigs & TIOCM_DTR)
1615                                 strcat(str, "|DTR");
1616                         if (sigs & TIOCM_CD)
1617                                 strcat(str, "|CD");
1618                 }
1619
1620                 len += sprintf((page + len), "%s\n", &str[1]);
1621         }
1622
1623         return(len);
1624 }
1625
1626
1627 /* Finally, routines used to initialize the serial driver. */
1628
1629 static void show_serial_version(void)
1630 {
1631         printk(mcfrs_drivername);
1632 }
1633
1634 static struct tty_operations mcfrs_ops = {
1635         .open = mcfrs_open,
1636         .close = mcfrs_close,
1637         .write = mcfrs_write,
1638         .flush_chars = mcfrs_flush_chars,
1639         .write_room = mcfrs_write_room,
1640         .chars_in_buffer = mcfrs_chars_in_buffer,
1641         .flush_buffer = mcfrs_flush_buffer,
1642         .ioctl = mcfrs_ioctl,
1643         .throttle = mcfrs_throttle,
1644         .unthrottle = mcfrs_unthrottle,
1645         .set_termios = mcfrs_set_termios,
1646         .stop = mcfrs_stop,
1647         .start = mcfrs_start,
1648         .hangup = mcfrs_hangup,
1649         .read_proc = mcfrs_readproc,
1650         .wait_until_sent = mcfrs_wait_until_sent,
1651         .tiocmget = mcfrs_tiocmget,
1652         .tiocmset = mcfrs_tiocmset,
1653 };
1654
1655 /* mcfrs_init inits the driver */
1656 static int __init
1657 mcfrs_init(void)
1658 {
1659         struct mcf_serial       *info;
1660         unsigned long           flags;
1661         int                     i;
1662
1663         /* Setup base handler, and timer table. */
1664 #ifdef MCFPP_DCD0
1665         init_timer(&mcfrs_timer_struct);
1666         mcfrs_timer_struct.function = mcfrs_timer;
1667         mcfrs_timer_struct.data = 0;
1668         mcfrs_timer_struct.expires = jiffies + HZ/25;
1669         add_timer(&mcfrs_timer_struct);
1670         mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1671 #endif
1672         mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1673         if (!mcfrs_serial_driver)
1674                 return -ENOMEM;
1675
1676         show_serial_version();
1677
1678         /* Initialize the tty_driver structure */
1679         mcfrs_serial_driver->owner = THIS_MODULE;
1680         mcfrs_serial_driver->name = "ttyS";
1681         mcfrs_serial_driver->devfs_name = "ttys/";
1682         mcfrs_serial_driver->driver_name = "serial";
1683         mcfrs_serial_driver->major = TTY_MAJOR;
1684         mcfrs_serial_driver->minor_start = 64;
1685         mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1686         mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1687         mcfrs_serial_driver->init_termios = tty_std_termios;
1688
1689         mcfrs_serial_driver->init_termios.c_cflag =
1690                 mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1691         mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1692
1693         tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1694
1695         if (tty_register_driver(mcfrs_serial_driver)) {
1696                 printk("MCFRS: Couldn't register serial driver\n");
1697                 put_tty_driver(mcfrs_serial_driver);
1698                 return(-EBUSY);
1699         }
1700
1701         local_irq_save(flags);
1702
1703         /*
1704          *      Configure all the attached serial ports.
1705          */
1706         for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1707                 info->magic = SERIAL_MAGIC;
1708                 info->line = i;
1709                 info->tty = 0;
1710                 info->custom_divisor = 16;
1711                 info->close_delay = 50;
1712                 info->closing_wait = 3000;
1713                 info->x_char = 0;
1714                 info->event = 0;
1715                 info->count = 0;
1716                 info->blocked_open = 0;
1717                 INIT_WORK(&info->tqueue, mcfrs_offintr, info);
1718                 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1719                 init_waitqueue_head(&info->open_wait);
1720                 init_waitqueue_head(&info->close_wait);
1721
1722                 info->imr = 0;
1723                 mcfrs_setsignals(info, 0, 0);
1724                 mcfrs_irqinit(info);
1725
1726                 printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1727                         (unsigned int) info->addr, info->irq);
1728                 printk(" is a builtin ColdFire UART\n");
1729         }
1730
1731         local_irq_restore(flags);
1732         return 0;
1733 }
1734
1735 module_init(mcfrs_init);
1736
1737 /****************************************************************************/
1738 /*                          Serial Console                                  */
1739 /****************************************************************************/
1740
1741 /*
1742  *      Quick and dirty UART initialization, for console output.
1743  */
1744
1745 void mcfrs_init_console(void)
1746 {
1747         volatile unsigned char  *uartp;
1748         unsigned int            clk;
1749
1750         /*
1751          *      Reset UART, get it into known state...
1752          */
1753         uartp = (volatile unsigned char *) (MCF_MBAR +
1754                 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1755
1756         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX;  /* reset RX */
1757         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX;  /* reset TX */
1758         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR;  /* reset MR pointer */
1759
1760         /*
1761          * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1762          */
1763         uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1764         uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1765
1766         clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1767         uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8;  /* set msb baud */
1768         uartp[MCFUART_UBG2] = (clk & 0xff);  /* set lsb baud */
1769
1770         uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1771         uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1772
1773         mcfrs_console_inited++;
1774         return;
1775 }
1776
1777
1778 /*
1779  *      Setup for console. Argument comes from the boot command line.
1780  */
1781
1782 int mcfrs_console_setup(struct console *cp, char *arg)
1783 {
1784         int             i, n = CONSOLE_BAUD_RATE;
1785
1786         if (!cp)
1787                 return(-1);
1788
1789         if (!strncmp(cp->name, "ttyS", 4))
1790                 mcfrs_console_port = cp->index;
1791         else if (!strncmp(cp->name, "cua", 3))
1792                 mcfrs_console_port = cp->index;
1793         else
1794                 return(-1);
1795
1796         if (arg)
1797                 n = simple_strtoul(arg,NULL,0);
1798         for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1799                 if (mcfrs_baud_table[i] == n)
1800                         break;
1801         if (i < MCFRS_BAUD_TABLE_SIZE) {
1802                 mcfrs_console_baud = n;
1803                 mcfrs_console_cbaud = 0;
1804                 if (i > 15) {
1805                         mcfrs_console_cbaud |= CBAUDEX;
1806                         i -= 15;
1807                 }
1808                 mcfrs_console_cbaud |= i;
1809         }
1810         mcfrs_init_console(); /* make sure baud rate changes */
1811         return(0);
1812 }
1813
1814
1815 static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1816 {
1817         *index = c->index;
1818         return mcfrs_serial_driver;
1819 }
1820
1821
1822 /*
1823  *      Output a single character, using UART polled mode.
1824  *      This is used for console output.
1825  */
1826
1827 void mcfrs_put_char(char ch)
1828 {
1829         volatile unsigned char  *uartp;
1830         unsigned long           flags;
1831         int                     i;
1832
1833         uartp = (volatile unsigned char *) (MCF_MBAR +
1834                 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1835
1836         local_irq_save(flags);
1837         for (i = 0; (i < 0x10000); i++) {
1838                 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1839                         break;
1840         }
1841         if (i < 0x10000) {
1842                 uartp[MCFUART_UTB] = ch;
1843                 for (i = 0; (i < 0x10000); i++)
1844                         if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1845                                 break;
1846         }
1847         if (i >= 0x10000)
1848                 mcfrs_init_console(); /* try and get it back */
1849         local_irq_restore(flags);
1850
1851         return;
1852 }
1853
1854
1855 /*
1856  * rs_console_write is registered for printk output.
1857  */
1858
1859 void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1860 {
1861         if (!mcfrs_console_inited)
1862                 mcfrs_init_console();
1863         while (len-- > 0) {
1864                 if (*p == '\n')
1865                         mcfrs_put_char('\r');
1866                 mcfrs_put_char(*p++);
1867         }
1868 }
1869
1870 /*
1871  * declare our consoles
1872  */
1873
1874 struct console mcfrs_console = {
1875         .name           = "ttyS",
1876         .write          = mcfrs_console_write,
1877         .device         = mcfrs_console_device,
1878         .setup          = mcfrs_console_setup,
1879         .flags          = CON_PRINTBUFFER,
1880         .index          = -1,
1881 };
1882
1883 static int __init mcfrs_console_init(void)
1884 {
1885         register_console(&mcfrs_console);
1886         return 0;
1887 }
1888
1889 console_initcall(mcfrs_console_init);
1890
1891 /****************************************************************************/