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