ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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
428         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
429             tty->ldisc.write_wakeup)
430                 (tty->ldisc.write_wakeup)(tty);
431         wake_up_interruptible(&tty->write_wait);
432 }
433
434
435 /*
436  *      Change of state on a DCD line.
437  */
438 void mcfrs_modem_change(struct mcf_serial *info, int dcd)
439 {
440         if (info->count == 0)
441                 return;
442
443         if (info->flags & ASYNC_CHECK_CD) {
444                 if (dcd)
445                         wake_up_interruptible(&info->open_wait);
446                 else 
447                         schedule_work(&info->tqueue_hangup);
448         }
449 }
450
451
452 #ifdef MCFPP_DCD0
453
454 unsigned short  mcfrs_ppstatus;
455
456 /*
457  * This subroutine is called when the RS_TIMER goes off. It is used
458  * to monitor the state of the DCD lines - since they have no edge
459  * sensors and interrupt generators.
460  */
461 static void mcfrs_timer(void)
462 {
463         unsigned int    ppstatus, dcdval, i;
464
465         ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
466
467         if (ppstatus != mcfrs_ppstatus) {
468                 for (i = 0; (i < 2); i++) {
469                         dcdval = (i ? MCFPP_DCD1 : MCFPP_DCD0);
470                         if ((ppstatus & dcdval) != (mcfrs_ppstatus & dcdval)) {
471                                 mcfrs_modem_change(&mcfrs_table[i],
472                                         ((ppstatus & dcdval) ? 0 : 1));
473                         }
474                 }
475         }
476         mcfrs_ppstatus = ppstatus;
477
478         /* Re-arm timer */
479         mcfrs_timer_struct.expires = jiffies + HZ/25;
480         add_timer(&mcfrs_timer_struct);
481 }
482
483 #endif  /* MCFPP_DCD0 */
484
485
486 /*
487  * This routine is called from the scheduler tqueue when the interrupt
488  * routine has signalled that a hangup has occurred. The path of
489  * hangup processing is:
490  *
491  *      serial interrupt routine -> (scheduler tqueue) ->
492  *      do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
493  * 
494  */
495 static void do_serial_hangup(void *private)
496 {
497         struct mcf_serial       *info = (struct mcf_serial *) private;
498         struct tty_struct       *tty;
499         
500         tty = info->tty;
501         if (!tty)
502                 return;
503
504         tty_hangup(tty);
505 }
506
507 static int startup(struct mcf_serial * info)
508 {
509         volatile unsigned char  *uartp;
510         unsigned long           flags;
511         
512         if (info->flags & ASYNC_INITIALIZED)
513                 return 0;
514
515         if (!info->xmit_buf) {
516                 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
517                 if (!info->xmit_buf)
518                         return -ENOMEM;
519         }
520
521         local_irq_save(flags);
522
523 #ifdef SERIAL_DEBUG_OPEN
524         printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
525 #endif
526
527         /*
528          *      Reset UART, get it into known state...
529          */
530         uartp = info->addr;
531         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX;  /* reset RX */
532         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX;  /* reset TX */
533         mcfrs_setsignals(info, 1, 1);
534
535         if (info->tty)
536                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
537         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
538
539         /*
540          * and set the speed of the serial port
541          */
542         mcfrs_change_speed(info);
543
544         /*
545          * Lastly enable the UART transmitter and receiver, and
546          * interrupt enables.
547          */
548         info->imr = MCFUART_UIR_RXREADY;
549         uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
550         uartp[MCFUART_UIMR] = info->imr;
551
552         info->flags |= ASYNC_INITIALIZED;
553         local_irq_restore(flags);
554         return 0;
555 }
556
557 /*
558  * This routine will shutdown a serial port; interrupts are disabled, and
559  * DTR is dropped if the hangup on close termio flag is on.
560  */
561 static void shutdown(struct mcf_serial * info)
562 {
563         volatile unsigned char  *uartp;
564         unsigned long           flags;
565
566         if (!(info->flags & ASYNC_INITIALIZED))
567                 return;
568
569 #ifdef SERIAL_DEBUG_OPEN
570         printk("Shutting down serial port %d (irq %d)....\n", info->line,
571                info->irq);
572 #endif
573         
574         local_irq_save(flags);
575
576         uartp = info->addr;
577         uartp[MCFUART_UIMR] = 0;  /* mask all interrupts */
578         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX;  /* reset RX */
579         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX;  /* reset TX */
580
581         if (!info->tty || (info->tty->termios->c_cflag & HUPCL))
582                 mcfrs_setsignals(info, 0, 0);
583
584         if (info->xmit_buf) {
585                 free_page((unsigned long) info->xmit_buf);
586                 info->xmit_buf = 0;
587         }
588
589         if (info->tty)
590                 set_bit(TTY_IO_ERROR, &info->tty->flags);
591         
592         info->flags &= ~ASYNC_INITIALIZED;
593         local_irq_restore(flags);
594 }
595
596
597 /*
598  * This routine is called to set the UART divisor registers to match
599  * the specified baud rate for a serial port.
600  */
601 static void mcfrs_change_speed(struct mcf_serial *info)
602 {
603         volatile unsigned char  *uartp;
604         unsigned int            baudclk, cflag;
605         unsigned long           flags;
606         unsigned char           mr1, mr2;
607         int                     i;
608 #ifdef  CONFIG_M5272
609         unsigned int            fraction;
610 #endif
611
612         if (!info->tty || !info->tty->termios)
613                 return;
614         cflag = info->tty->termios->c_cflag;
615         if (info->addr == 0)
616                 return;
617
618 #if 0
619         printk("%s(%d): mcfrs_change_speed()\n", __FILE__, __LINE__);
620 #endif
621
622         i = cflag & CBAUD;
623         if (i & CBAUDEX) {
624                 i &= ~CBAUDEX;
625                 if (i < 1 || i > 4)
626                         info->tty->termios->c_cflag &= ~CBAUDEX;
627                 else
628                         i += 15;
629         }
630         if (i == 0) {
631                 mcfrs_setsignals(info, 0, -1);
632                 return;
633         }
634
635         /* compute the baudrate clock */
636 #ifdef  CONFIG_M5272
637         /*
638          * For the MCF5272, also compute the baudrate fraction.
639          */
640         baudclk = (MCF_BUSCLK / mcfrs_baud_table[i]) / 32;
641         fraction = MCF_BUSCLK - (baudclk * 32 * mcfrs_baud_table[i]);
642         fraction *= 16;
643         fraction /= (32 * mcfrs_baud_table[i]);
644 #else
645         baudclk = ((MCF_BUSCLK / mcfrs_baud_table[i]) + 16) / 32;
646 #endif
647
648         info->baud = mcfrs_baud_table[i];
649
650         mr1 = MCFUART_MR1_RXIRQRDY | MCFUART_MR1_RXERRCHAR;
651         mr2 = 0;
652
653         switch (cflag & CSIZE) {
654         case CS5:       mr1 |= MCFUART_MR1_CS5; break;
655         case CS6:       mr1 |= MCFUART_MR1_CS6; break;
656         case CS7:       mr1 |= MCFUART_MR1_CS7; break;
657         case CS8:
658         default:        mr1 |= MCFUART_MR1_CS8; break;
659         }
660
661         if (cflag & PARENB) {
662                 if (cflag & CMSPAR) {
663                         if (cflag & PARODD)
664                                 mr1 |= MCFUART_MR1_PARITYMARK;
665                         else
666                                 mr1 |= MCFUART_MR1_PARITYSPACE;
667                 } else {
668                         if (cflag & PARODD)
669                                 mr1 |= MCFUART_MR1_PARITYODD;
670                         else
671                                 mr1 |= MCFUART_MR1_PARITYEVEN;
672                 }
673         } else {
674                 mr1 |= MCFUART_MR1_PARITYNONE;
675         }
676
677         if (cflag & CSTOPB)
678                 mr2 |= MCFUART_MR2_STOP2;
679         else
680                 mr2 |= MCFUART_MR2_STOP1;
681
682         if (cflag & CRTSCTS) {
683                 mr1 |= MCFUART_MR1_RXRTS;
684                 mr2 |= MCFUART_MR2_TXCTS;
685         }
686
687         if (cflag & CLOCAL)
688                 info->flags &= ~ASYNC_CHECK_CD;
689         else
690                 info->flags |= ASYNC_CHECK_CD;
691
692         uartp = info->addr;
693
694         local_irq_save(flags);
695 #if 0
696         printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__, __LINE__,
697                 mr1, mr2, baudclk);
698 #endif
699         /*
700           Note: pg 12-16 of MCF5206e User's Manual states that a
701           software reset should be performed prior to changing
702           UMR1,2, UCSR, UACR, bit 7
703         */
704         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX;    /* reset RX */
705         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX;    /* reset TX */
706         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR; /* reset MR pointer */
707         uartp[MCFUART_UMR] = mr1;
708         uartp[MCFUART_UMR] = mr2;
709         uartp[MCFUART_UBG1] = (baudclk & 0xff00) >> 8;  /* set msb byte */
710         uartp[MCFUART_UBG2] = (baudclk & 0xff);         /* set lsb byte */
711 #ifdef  CONFIG_M5272
712         uartp[MCFUART_UFPD] = (fraction & 0xf);         /* set fraction */
713 #endif
714         uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
715         uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
716         mcfrs_setsignals(info, 1, -1);
717         local_irq_restore(flags);
718         return;
719 }
720
721 static void mcfrs_flush_chars(struct tty_struct *tty)
722 {
723         volatile unsigned char  *uartp;
724         struct mcf_serial       *info = (struct mcf_serial *)tty->driver_data;
725         unsigned long           flags;
726
727         if (serial_paranoia_check(info, tty->name, "mcfrs_flush_chars"))
728                 return;
729
730         if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
731             !info->xmit_buf)
732                 return;
733
734         /* Enable transmitter */
735         local_irq_save(flags);
736         uartp = info->addr;
737         info->imr |= MCFUART_UIR_TXREADY;
738         uartp[MCFUART_UIMR] = info->imr;
739         local_irq_restore(flags);
740 }
741
742 static int mcfrs_write(struct tty_struct * tty, int from_user,
743                     const unsigned char *buf, int count)
744 {
745         volatile unsigned char  *uartp;
746         struct mcf_serial       *info = (struct mcf_serial *)tty->driver_data;
747         unsigned long           flags;
748         int                     c, total = 0;
749
750 #if 0
751         printk("%s(%d): mcfrs_write(tty=%x,from_user=%d,buf=%x,count=%d)\n",
752                 __FILE__, __LINE__, (int)tty, from_user, (int)buf, count);
753 #endif
754
755         if (serial_paranoia_check(info, tty->name, "mcfrs_write"))
756                 return 0;
757
758         if (!tty || !info->xmit_buf)
759                 return 0;
760         
761         local_save_flags(flags);
762         while (1) {
763                 local_irq_disable();            
764                 c = min(count, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
765                         ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
766                 local_irq_restore(flags);
767
768                 if (c <= 0)
769                         break;
770
771                 if (from_user) {
772                         down(&mcfrs_tmp_buf_sem);
773                         if (copy_from_user(mcfrs_tmp_buf, buf, c))
774                                 return -EFAULT;
775
776                         local_irq_disable();
777                         c = min(c, (int) min(((int)SERIAL_XMIT_SIZE) - info->xmit_cnt - 1,
778                                        ((int)SERIAL_XMIT_SIZE) - info->xmit_head));
779                         local_irq_restore(flags);
780                         memcpy(info->xmit_buf + info->xmit_head, mcfrs_tmp_buf, c);
781                         up(&mcfrs_tmp_buf_sem);
782                 } else
783                         memcpy(info->xmit_buf + info->xmit_head, buf, c);
784
785                 local_irq_disable();
786                 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
787                 info->xmit_cnt += c;
788                 local_irq_restore(flags);
789
790                 buf += c;
791                 count -= c;
792                 total += c;
793         }
794
795         local_irq_disable();
796         uartp = info->addr;
797         info->imr |= MCFUART_UIR_TXREADY;
798         uartp[MCFUART_UIMR] = info->imr;
799         local_irq_restore(flags);
800
801         return total;
802 }
803
804 static int mcfrs_write_room(struct tty_struct *tty)
805 {
806         struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
807         int     ret;
808
809         if (serial_paranoia_check(info, tty->name, "mcfrs_write_room"))
810                 return 0;
811         ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
812         if (ret < 0)
813                 ret = 0;
814         return ret;
815 }
816
817 static int mcfrs_chars_in_buffer(struct tty_struct *tty)
818 {
819         struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
820
821         if (serial_paranoia_check(info, tty->name, "mcfrs_chars_in_buffer"))
822                 return 0;
823         return info->xmit_cnt;
824 }
825
826 static void mcfrs_flush_buffer(struct tty_struct *tty)
827 {
828         struct mcf_serial       *info = (struct mcf_serial *)tty->driver_data;
829         unsigned long           flags;
830
831         if (serial_paranoia_check(info, tty->name, "mcfrs_flush_buffer"))
832                 return;
833
834         local_irq_save(flags);
835         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
836         local_irq_restore(flags);
837
838         wake_up_interruptible(&tty->write_wait);
839         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
840             tty->ldisc.write_wakeup)
841                 (tty->ldisc.write_wakeup)(tty);
842 }
843
844 /*
845  * ------------------------------------------------------------
846  * mcfrs_throttle()
847  * 
848  * This routine is called by the upper-layer tty layer to signal that
849  * incoming characters should be throttled.
850  * ------------------------------------------------------------
851  */
852 static void mcfrs_throttle(struct tty_struct * tty)
853 {
854         struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
855 #ifdef SERIAL_DEBUG_THROTTLE
856         char    buf[64];
857         
858         printk("throttle %s: %d....\n", _tty_name(tty, buf),
859                tty->ldisc.chars_in_buffer(tty));
860 #endif
861
862         if (serial_paranoia_check(info, tty->name, "mcfrs_throttle"))
863                 return;
864         
865         if (I_IXOFF(tty))
866                 info->x_char = STOP_CHAR(tty);
867
868         /* Turn off RTS line (do this atomic) */
869 }
870
871 static void mcfrs_unthrottle(struct tty_struct * tty)
872 {
873         struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
874 #ifdef SERIAL_DEBUG_THROTTLE
875         char    buf[64];
876         
877         printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
878                tty->ldisc.chars_in_buffer(tty));
879 #endif
880
881         if (serial_paranoia_check(info, tty->name, "mcfrs_unthrottle"))
882                 return;
883         
884         if (I_IXOFF(tty)) {
885                 if (info->x_char)
886                         info->x_char = 0;
887                 else
888                         info->x_char = START_CHAR(tty);
889         }
890
891         /* Assert RTS line (do this atomic) */
892 }
893
894 /*
895  * ------------------------------------------------------------
896  * mcfrs_ioctl() and friends
897  * ------------------------------------------------------------
898  */
899
900 static int get_serial_info(struct mcf_serial * info,
901                            struct serial_struct * retinfo)
902 {
903         struct serial_struct tmp;
904   
905         if (!retinfo)
906                 return -EFAULT;
907         memset(&tmp, 0, sizeof(tmp));
908         tmp.type = info->type;
909         tmp.line = info->line;
910         tmp.port = (unsigned int) info->addr;
911         tmp.irq = info->irq;
912         tmp.flags = info->flags;
913         tmp.baud_base = info->baud_base;
914         tmp.close_delay = info->close_delay;
915         tmp.closing_wait = info->closing_wait;
916         tmp.custom_divisor = info->custom_divisor;
917         return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
918 }
919
920 static int set_serial_info(struct mcf_serial * info,
921                            struct serial_struct * new_info)
922 {
923         struct serial_struct new_serial;
924         struct mcf_serial old_info;
925         int     retval = 0;
926
927         if (!new_info)
928                 return -EFAULT;
929         if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
930                 return -EFAULT;
931         old_info = *info;
932
933         if (!capable(CAP_SYS_ADMIN)) {
934                 if ((new_serial.baud_base != info->baud_base) ||
935                     (new_serial.type != info->type) ||
936                     (new_serial.close_delay != info->close_delay) ||
937                     ((new_serial.flags & ~ASYNC_USR_MASK) !=
938                      (info->flags & ~ASYNC_USR_MASK)))
939                         return -EPERM;
940                 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
941                                (new_serial.flags & ASYNC_USR_MASK));
942                 info->custom_divisor = new_serial.custom_divisor;
943                 goto check_and_exit;
944         }
945
946         if (info->count > 1)
947                 return -EBUSY;
948
949         /*
950          * OK, past this point, all the error checking has been done.
951          * At this point, we start making changes.....
952          */
953
954         info->baud_base = new_serial.baud_base;
955         info->flags = ((info->flags & ~ASYNC_FLAGS) |
956                         (new_serial.flags & ASYNC_FLAGS));
957         info->type = new_serial.type;
958         info->close_delay = new_serial.close_delay;
959         info->closing_wait = new_serial.closing_wait;
960
961 check_and_exit:
962         retval = startup(info);
963         return retval;
964 }
965
966 /*
967  * get_lsr_info - get line status register info
968  *
969  * Purpose: Let user call ioctl() to get info when the UART physically
970  *          is emptied.  On bus types like RS485, the transmitter must
971  *          release the bus after transmitting. This must be done when
972  *          the transmit shift register is empty, not be done when the
973  *          transmit holding register is empty.  This functionality
974  *          allows an RS485 driver to be written in user space. 
975  */
976 static int get_lsr_info(struct mcf_serial * info, unsigned int *value)
977 {
978         volatile unsigned char  *uartp;
979         unsigned long           flags;
980         unsigned char           status;
981
982         local_irq_save(flags);
983         uartp = info->addr;
984         status = (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY) ? TIOCSER_TEMT : 0;
985         local_irq_restore(flags);
986
987         return put_user(status,value);
988 }
989
990 /*
991  * This routine sends a break character out the serial port.
992  */
993 static void send_break( struct mcf_serial * info, int duration)
994 {
995         volatile unsigned char  *uartp;
996         unsigned long           flags;
997
998         if (!info->addr)
999                 return;
1000         current->state = TASK_INTERRUPTIBLE;
1001         uartp = info->addr;
1002
1003         local_irq_save(flags);
1004         uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTART;
1005         schedule_timeout(jiffies + duration);
1006         uartp[MCFUART_UCR] = MCFUART_UCR_CMDBREAKSTOP;
1007         local_irq_restore(flags);
1008 }
1009
1010 static int mcfrs_tiocmget(struct tty_struct *tty, struct file *file)
1011 {
1012         struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1013
1014         if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1015                 return -ENODEV;
1016         if (tty->flags & (1 << TTY_IO_ERROR))
1017                 return -EIO;
1018
1019         return mcfrs_getsignals(info);
1020 }
1021
1022 static int mcfrs_tiocmset(struct tty_struct *tty, struct file *file,
1023                           unsigned int set, unsigned int clear)
1024 {
1025         struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1026         int rts = -1, dtr = -1;
1027
1028         if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1029                 return -ENODEV;
1030         if (tty->flags & (1 << TTY_IO_ERROR))
1031                 return -EIO;
1032
1033         if (set & TIOCM_RTS)
1034                 rts = 1;
1035         if (set & TIOCM_DTR)
1036                 dtr = 1;
1037         if (clear & TIOCM_RTS)
1038                 rts = 0;
1039         if (clear & TIOCM_DTR)
1040                 dtr = 0;
1041
1042         mcfrs_setsignals(info, dtr, rts);
1043
1044         return 0;
1045 }
1046
1047 static int mcfrs_ioctl(struct tty_struct *tty, struct file * file,
1048                     unsigned int cmd, unsigned long arg)
1049 {
1050         struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1051         int retval, error;
1052
1053         if (serial_paranoia_check(info, tty->name, "mcfrs_ioctl"))
1054                 return -ENODEV;
1055
1056         if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1057             (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD)  &&
1058             (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1059                 if (tty->flags & (1 << TTY_IO_ERROR))
1060                     return -EIO;
1061         }
1062         
1063         switch (cmd) {
1064                 case TCSBRK:    /* SVID version: non-zero arg --> no break */
1065                         retval = tty_check_change(tty);
1066                         if (retval)
1067                                 return retval;
1068                         tty_wait_until_sent(tty, 0);
1069                         if (!arg)
1070                                 send_break(info, HZ/4); /* 1/4 second */
1071                         return 0;
1072                 case TCSBRKP:   /* support for POSIX tcsendbreak() */
1073                         retval = tty_check_change(tty);
1074                         if (retval)
1075                                 return retval;
1076                         tty_wait_until_sent(tty, 0);
1077                         send_break(info, arg ? arg*(HZ/10) : HZ/4);
1078                         return 0;
1079                 case TIOCGSOFTCAR:
1080                         error = put_user(C_CLOCAL(tty) ? 1 : 0,
1081                                     (unsigned long *) arg);
1082                         if (error)
1083                                 return error;
1084                         return 0;
1085                 case TIOCSSOFTCAR:
1086                         get_user(arg, (unsigned long *) arg);
1087                         tty->termios->c_cflag =
1088                                 ((tty->termios->c_cflag & ~CLOCAL) |
1089                                  (arg ? CLOCAL : 0));
1090                         return 0;
1091                 case TIOCGSERIAL:
1092                         error = verify_area(VERIFY_WRITE, (void *) arg,
1093                                                 sizeof(struct serial_struct));
1094                         if (error)
1095                                 return error;
1096                         return get_serial_info(info,
1097                                                (struct serial_struct *) arg);
1098                 case TIOCSSERIAL:
1099                         return set_serial_info(info,
1100                                                (struct serial_struct *) arg);
1101                 case TIOCSERGETLSR: /* Get line status register */
1102                         error = verify_area(VERIFY_WRITE, (void *) arg,
1103                                 sizeof(unsigned int));
1104                         if (error)
1105                                 return error;
1106                         else
1107                             return get_lsr_info(info, (unsigned int *) arg);
1108
1109                 case TIOCSERGSTRUCT:
1110                         error = copy_to_user((struct mcf_serial *) arg,
1111                                     info, sizeof(struct mcf_serial));
1112                         if (error)
1113                                 return -EFAULT;
1114                         return 0;
1115                         
1116 #ifdef TIOCSET422
1117                 case TIOCSET422: {
1118                         unsigned int val;
1119                         get_user(val, (unsigned int *) arg);
1120                         mcf_setpa(MCFPP_PA11, (val ? 0 : MCFPP_PA11));
1121                         break;
1122                 }
1123                 case TIOCGET422: {
1124                         unsigned int val;
1125                         val = (mcf_getpa() & MCFPP_PA11) ? 0 : 1;
1126                         put_user(val, (unsigned int *) arg);
1127                         break;
1128                 }
1129 #endif
1130
1131                 default:
1132                         return -ENOIOCTLCMD;
1133                 }
1134         return 0;
1135 }
1136
1137 static void mcfrs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1138 {
1139         struct mcf_serial *info = (struct mcf_serial *)tty->driver_data;
1140
1141         if (tty->termios->c_cflag == old_termios->c_cflag)
1142                 return;
1143
1144         mcfrs_change_speed(info);
1145
1146         if ((old_termios->c_cflag & CRTSCTS) &&
1147             !(tty->termios->c_cflag & CRTSCTS)) {
1148                 tty->hw_stopped = 0;
1149                 mcfrs_setsignals(info, -1, 1);
1150 #if 0
1151                 mcfrs_start(tty);
1152 #endif
1153         }
1154 }
1155
1156 /*
1157  * ------------------------------------------------------------
1158  * mcfrs_close()
1159  * 
1160  * This routine is called when the serial port gets closed.  First, we
1161  * wait for the last remaining data to be sent.  Then, we unlink its
1162  * S structure from the interrupt chain if necessary, and we free
1163  * that IRQ if nothing is left in the chain.
1164  * ------------------------------------------------------------
1165  */
1166 static void mcfrs_close(struct tty_struct *tty, struct file * filp)
1167 {
1168         volatile unsigned char  *uartp;
1169         struct mcf_serial       *info = (struct mcf_serial *)tty->driver_data;
1170         unsigned long           flags;
1171
1172         if (!info || serial_paranoia_check(info, tty->name, "mcfrs_close"))
1173                 return;
1174         
1175         local_irq_save(flags);
1176         
1177         if (tty_hung_up_p(filp)) {
1178                 local_irq_restore(flags);
1179                 return;
1180         }
1181         
1182 #ifdef SERIAL_DEBUG_OPEN
1183         printk("mcfrs_close ttyS%d, count = %d\n", info->line, info->count);
1184 #endif
1185         if ((tty->count == 1) && (info->count != 1)) {
1186                 /*
1187                  * Uh, oh.  tty->count is 1, which means that the tty
1188                  * structure will be freed.  Info->count should always
1189                  * be one in these conditions.  If it's greater than
1190                  * one, we've got real problems, since it means the
1191                  * serial port won't be shutdown.
1192                  */
1193                 printk("MCFRS: bad serial port count; tty->count is 1, "
1194                        "info->count is %d\n", info->count);
1195                 info->count = 1;
1196         }
1197         if (--info->count < 0) {
1198                 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1199                        info->line, info->count);
1200                 info->count = 0;
1201         }
1202         if (info->count) {
1203                 local_irq_restore(flags);
1204                 return;
1205         }
1206         info->flags |= ASYNC_CLOSING;
1207
1208         /*
1209          * Now we wait for the transmit buffer to clear; and we notify 
1210          * the line discipline to only process XON/XOFF characters.
1211          */
1212         tty->closing = 1;
1213         if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1214                 tty_wait_until_sent(tty, info->closing_wait);
1215
1216         /*
1217          * At this point we stop accepting input.  To do this, we
1218          * disable the receive line status interrupts, and tell the
1219          * interrupt driver to stop checking the data ready bit in the
1220          * line status register.
1221          */
1222         info->imr &= ~MCFUART_UIR_RXREADY;
1223         uartp = info->addr;
1224         uartp[MCFUART_UIMR] = info->imr;
1225
1226 #if 0
1227         /* FIXME: do we need to keep this enabled for console?? */
1228         if (mcfrs_console_inited && (mcfrs_console_port == info->line)) {
1229                 /* Do not disable the UART */ ;
1230         } else
1231 #endif
1232         shutdown(info);
1233         if (tty->driver->flush_buffer)
1234                 tty->driver->flush_buffer(tty);
1235         if (tty->ldisc.flush_buffer)
1236                 tty->ldisc.flush_buffer(tty);
1237         tty->closing = 0;
1238         info->event = 0;
1239         info->tty = 0;
1240         if (tty->ldisc.num != ldiscs[N_TTY].num) {
1241                 if (tty->ldisc.close)
1242                         (tty->ldisc.close)(tty);
1243                 tty->ldisc = ldiscs[N_TTY];
1244                 tty->termios->c_line = N_TTY;
1245                 if (tty->ldisc.open)
1246                         (tty->ldisc.open)(tty);
1247         }
1248         if (info->blocked_open) {
1249                 if (info->close_delay) {
1250                         current->state = TASK_INTERRUPTIBLE;
1251                         schedule_timeout(info->close_delay);
1252                 }
1253                 wake_up_interruptible(&info->open_wait);
1254         }
1255         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1256         wake_up_interruptible(&info->close_wait);
1257         local_irq_restore(flags);
1258 }
1259
1260 /*
1261  * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1262  */
1263 static void
1264 mcfrs_wait_until_sent(struct tty_struct *tty, int timeout)
1265 {
1266 #ifdef  CONFIG_M5272
1267 #define MCF5272_FIFO_SIZE       25              /* fifo size + shift reg */
1268
1269         struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1270         volatile unsigned char *uartp;
1271         unsigned long orig_jiffies, fifo_time, char_time, fifo_cnt;
1272
1273         if (serial_paranoia_check(info, tty->name, "mcfrs_wait_until_sent"))
1274                 return;
1275
1276         orig_jiffies = jiffies;
1277
1278         /*
1279          * Set the check interval to be 1/5 of the approximate time
1280          * to send the entire fifo, and make it at least 1.  The check
1281          * interval should also be less than the timeout.
1282          *
1283          * Note: we have to use pretty tight timings here to satisfy
1284          * the NIST-PCTS.
1285          */
1286         fifo_time = (MCF5272_FIFO_SIZE * HZ * 10) / info->baud;
1287         char_time = fifo_time / 5;
1288         if (char_time == 0)
1289                 char_time = 1;
1290         if (timeout && timeout < char_time)
1291                 char_time = timeout;
1292
1293         /*
1294          * Clamp the timeout period at 2 * the time to empty the
1295          * fifo.  Just to be safe, set the minimum at .5 seconds.
1296          */
1297         fifo_time *= 2;
1298         if (fifo_time < (HZ/2))
1299                 fifo_time = HZ/2;
1300         if (!timeout || timeout > fifo_time)
1301                 timeout = fifo_time;
1302
1303         /*
1304          * Account for the number of bytes in the UART
1305          * transmitter FIFO plus any byte being shifted out.
1306          */
1307         uartp = (volatile unsigned char *) info->addr;
1308         for (;;) {
1309                 fifo_cnt = (uartp[MCFUART_UTF] & MCFUART_UTF_TXB);
1310                 if ((uartp[MCFUART_USR] & (MCFUART_USR_TXREADY|
1311                                 MCFUART_USR_TXEMPTY)) ==
1312                         MCFUART_USR_TXREADY)
1313                         fifo_cnt++;
1314                 if (fifo_cnt == 0)
1315                         break;
1316                 set_current_state(TASK_INTERRUPTIBLE);
1317                 schedule_timeout(char_time);
1318                 if (signal_pending(current))
1319                         break;
1320                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1321                         break;
1322         }
1323 #else
1324         /*
1325          * For the other coldfire models, assume all data has been sent
1326          */
1327 #endif
1328 }
1329
1330 /*
1331  * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1332  */
1333 void mcfrs_hangup(struct tty_struct *tty)
1334 {
1335         struct mcf_serial * info = (struct mcf_serial *)tty->driver_data;
1336         
1337         if (serial_paranoia_check(info, tty->name, "mcfrs_hangup"))
1338                 return;
1339         
1340         mcfrs_flush_buffer(tty);
1341         shutdown(info);
1342         info->event = 0;
1343         info->count = 0;
1344         info->flags &= ~ASYNC_NORMAL_ACTIVE;
1345         info->tty = 0;
1346         wake_up_interruptible(&info->open_wait);
1347 }
1348
1349 /*
1350  * ------------------------------------------------------------
1351  * mcfrs_open() and friends
1352  * ------------------------------------------------------------
1353  */
1354 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1355                            struct mcf_serial *info)
1356 {
1357         DECLARE_WAITQUEUE(wait, current);
1358         int     retval;
1359         int     do_clocal = 0;
1360
1361         /*
1362          * If the device is in the middle of being closed, then block
1363          * until it's done, and then try again.
1364          */
1365         if (info->flags & ASYNC_CLOSING) {
1366                 interruptible_sleep_on(&info->close_wait);
1367 #ifdef SERIAL_DO_RESTART
1368                 if (info->flags & ASYNC_HUP_NOTIFY)
1369                         return -EAGAIN;
1370                 else
1371                         return -ERESTARTSYS;
1372 #else
1373                 return -EAGAIN;
1374 #endif
1375         }
1376         
1377         /*
1378          * If non-blocking mode is set, or the port is not enabled,
1379          * then make the check up front and then exit.
1380          */
1381         if ((filp->f_flags & O_NONBLOCK) ||
1382             (tty->flags & (1 << TTY_IO_ERROR))) {
1383                 info->flags |= ASYNC_NORMAL_ACTIVE;
1384                 return 0;
1385         }
1386
1387         if (tty->termios->c_cflag & CLOCAL)
1388                 do_clocal = 1;
1389
1390         /*
1391          * Block waiting for the carrier detect and the line to become
1392          * free (i.e., not in use by the callout).  While we are in
1393          * this loop, info->count is dropped by one, so that
1394          * mcfrs_close() knows when to free things.  We restore it upon
1395          * exit, either normal or abnormal.
1396          */
1397         retval = 0;
1398         add_wait_queue(&info->open_wait, &wait);
1399 #ifdef SERIAL_DEBUG_OPEN
1400         printk("block_til_ready before block: ttyS%d, count = %d\n",
1401                info->line, info->count);
1402 #endif
1403         info->count--;
1404         info->blocked_open++;
1405         while (1) {
1406                 local_irq_disable();
1407                 mcfrs_setsignals(info, 1, 1);
1408                 local_irq_enable();
1409                 current->state = TASK_INTERRUPTIBLE;
1410                 if (tty_hung_up_p(filp) ||
1411                     !(info->flags & ASYNC_INITIALIZED)) {
1412 #ifdef SERIAL_DO_RESTART
1413                         if (info->flags & ASYNC_HUP_NOTIFY)
1414                                 retval = -EAGAIN;
1415                         else
1416                                 retval = -ERESTARTSYS;  
1417 #else
1418                         retval = -EAGAIN;
1419 #endif
1420                         break;
1421                 }
1422                 if (!(info->flags & ASYNC_CLOSING) &&
1423                     (do_clocal || (mcfrs_getsignals(info) & TIOCM_CD)))
1424                         break;
1425                 if (signal_pending(current)) {
1426                         retval = -ERESTARTSYS;
1427                         break;
1428                 }
1429 #ifdef SERIAL_DEBUG_OPEN
1430                 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1431                        info->line, info->count);
1432 #endif
1433                 schedule();
1434         }
1435         current->state = TASK_RUNNING;
1436         remove_wait_queue(&info->open_wait, &wait);
1437         if (!tty_hung_up_p(filp))
1438                 info->count++;
1439         info->blocked_open--;
1440 #ifdef SERIAL_DEBUG_OPEN
1441         printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1442                info->line, info->count);
1443 #endif
1444         if (retval)
1445                 return retval;
1446         info->flags |= ASYNC_NORMAL_ACTIVE;
1447         return 0;
1448 }       
1449
1450 /*
1451  * This routine is called whenever a serial port is opened. It
1452  * enables interrupts for a serial port, linking in its structure into
1453  * the IRQ chain.   It also performs the serial-specific
1454  * initialization for the tty structure.
1455  */
1456 int mcfrs_open(struct tty_struct *tty, struct file * filp)
1457 {
1458         struct mcf_serial       *info;
1459         int                     retval, line;
1460
1461         line = tty->index;
1462         if ((line < 0) || (line >= NR_PORTS))
1463                 return -ENODEV;
1464         info = mcfrs_table + line;
1465         if (serial_paranoia_check(info, tty->name, "mcfrs_open"))
1466                 return -ENODEV;
1467 #ifdef SERIAL_DEBUG_OPEN
1468         printk("mcfrs_open %s, count = %d\n", tty->name, info->count);
1469 #endif
1470         info->count++;
1471         tty->driver_data = info;
1472         info->tty = tty;
1473
1474         /*
1475          * Start up serial port
1476          */
1477         retval = startup(info);
1478         if (retval)
1479                 return retval;
1480
1481         retval = block_til_ready(tty, filp, info);
1482         if (retval) {
1483 #ifdef SERIAL_DEBUG_OPEN
1484                 printk("mcfrs_open returning after block_til_ready with %d\n",
1485                        retval);
1486 #endif
1487                 return retval;
1488         }
1489
1490 #ifdef SERIAL_DEBUG_OPEN
1491         printk("mcfrs_open %s successful...\n", tty->name);
1492 #endif
1493         return 0;
1494 }
1495
1496 /*
1497  *      Based on the line number set up the internal interrupt stuff.
1498  */
1499 static void mcfrs_irqinit(struct mcf_serial *info)
1500 {
1501 #if defined(CONFIG_M5272)
1502         volatile unsigned long  *icrp;
1503         volatile unsigned long  *portp;
1504         volatile unsigned char  *uartp;
1505
1506         uartp = info->addr;
1507         icrp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_ICR2);
1508
1509         switch (info->line) {
1510         case 0:
1511                 *icrp = 0xe0000000;
1512                 break;
1513         case 1:
1514                 *icrp = 0x0e000000;
1515                 break;
1516         default:
1517                 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1518                         info->line);
1519                 return;
1520         }
1521
1522         /* Enable the output lines for the serial ports */
1523         portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PBCNT);
1524         *portp = (*portp & ~0x000000ff) | 0x00000055;
1525         portp = (volatile unsigned long *) (MCF_MBAR + MCFSIM_PDCNT);
1526         *portp = (*portp & ~0x000003fc) | 0x000002a8;
1527 #elif defined(CONFIG_M5282)
1528         volatile unsigned char *icrp, *uartp;
1529         volatile unsigned long *imrp;
1530
1531         uartp = info->addr;
1532
1533         icrp = (volatile unsigned char *) (MCF_MBAR + MCFICM_INTC0 +
1534                 MCFINTC_ICR0 + MCFINT_UART0 + info->line);
1535         *icrp = 0x33; /* UART0 with level 6, priority 3 */
1536
1537         imrp = (volatile unsigned long *) (MCF_MBAR + MCFICM_INTC0 +
1538                 MCFINTC_IMRL);
1539         *imrp &= ~((1 << (info->irq - 64)) | 1);
1540 #else
1541         volatile unsigned char  *icrp, *uartp;
1542
1543         switch (info->line) {
1544         case 0:
1545                 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART1ICR);
1546                 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1547                         MCFSIM_ICR_PRI1;
1548                 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1);
1549                 break;
1550         case 1:
1551                 icrp = (volatile unsigned char *) (MCF_MBAR + MCFSIM_UART2ICR);
1552                 *icrp = /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6 |
1553                         MCFSIM_ICR_PRI2;
1554                 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2);
1555                 break;
1556         default:
1557                 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1558                         info->line);
1559                 return;
1560         }
1561
1562         uartp = info->addr;
1563         uartp[MCFUART_UIVR] = info->irq;
1564 #endif
1565
1566         /* Clear mask, so no surprise interrupts. */
1567         uartp[MCFUART_UIMR] = 0;
1568
1569         if (request_irq(info->irq, mcfrs_interrupt, SA_INTERRUPT,
1570             "ColdFire UART", NULL)) {
1571                 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1572                         "vector=%d\n", info->line, info->irq);
1573         }
1574
1575         return;
1576 }
1577
1578
1579 char *mcfrs_drivername = "ColdFire internal UART serial driver version 1.00\n";
1580
1581
1582 /*
1583  * Serial stats reporting...
1584  */
1585 int mcfrs_readproc(char *page, char **start, off_t off, int count,
1586                          int *eof, void *data)
1587 {
1588         struct mcf_serial       *info;
1589         char                    str[20];
1590         int                     len, sigs, i;
1591
1592         len = sprintf(page, mcfrs_drivername);
1593         for (i = 0; (i < NR_PORTS); i++) {
1594                 info = &mcfrs_table[i];
1595                 len += sprintf((page + len), "%d: port:%x irq=%d baud:%d ",
1596                         i, (unsigned int) info->addr, info->irq, info->baud);
1597                 if (info->stats.rx || info->stats.tx)
1598                         len += sprintf((page + len), "tx:%d rx:%d ",
1599                         info->stats.tx, info->stats.rx);
1600                 if (info->stats.rxframing)
1601                         len += sprintf((page + len), "fe:%d ",
1602                         info->stats.rxframing);
1603                 if (info->stats.rxparity)
1604                         len += sprintf((page + len), "pe:%d ",
1605                         info->stats.rxparity);
1606                 if (info->stats.rxbreak)
1607                         len += sprintf((page + len), "brk:%d ",
1608                         info->stats.rxbreak);
1609                 if (info->stats.rxoverrun)
1610                         len += sprintf((page + len), "oe:%d ",
1611                         info->stats.rxoverrun);
1612
1613                 str[0] = str[1] = 0;
1614                 if ((sigs = mcfrs_getsignals(info))) {
1615                         if (sigs & TIOCM_RTS)
1616                                 strcat(str, "|RTS");
1617                         if (sigs & TIOCM_CTS)
1618                                 strcat(str, "|CTS");
1619                         if (sigs & TIOCM_DTR)
1620                                 strcat(str, "|DTR");
1621                         if (sigs & TIOCM_CD)
1622                                 strcat(str, "|CD");
1623                 }
1624
1625                 len += sprintf((page + len), "%s\n", &str[1]);
1626         }
1627
1628         return(len);
1629 }
1630
1631
1632 /* Finally, routines used to initialize the serial driver. */
1633
1634 static void show_serial_version(void)
1635 {
1636         printk(mcfrs_drivername);
1637 }
1638
1639 static struct tty_operations mcfrs_ops = {
1640         .open = mcfrs_open,
1641         .close = mcfrs_close,
1642         .write = mcfrs_write,
1643         .flush_chars = mcfrs_flush_chars,
1644         .write_room = mcfrs_write_room,
1645         .chars_in_buffer = mcfrs_chars_in_buffer,
1646         .flush_buffer = mcfrs_flush_buffer,
1647         .ioctl = mcfrs_ioctl,
1648         .throttle = mcfrs_throttle,
1649         .unthrottle = mcfrs_unthrottle,
1650         .set_termios = mcfrs_set_termios,
1651         .stop = mcfrs_stop,
1652         .start = mcfrs_start,
1653         .hangup = mcfrs_hangup,
1654         .read_proc = mcfrs_readproc,
1655         .wait_until_sent = mcfrs_wait_until_sent,
1656         .tiocmget = mcfrs_tiocmget,
1657         .tiocmset = mcfrs_tiocmset,
1658 };
1659
1660 /* mcfrs_init inits the driver */
1661 static int __init
1662 mcfrs_init(void)
1663 {
1664         struct mcf_serial       *info;
1665         unsigned long           flags;
1666         int                     i;
1667
1668         /* Setup base handler, and timer table. */
1669 #ifdef MCFPP_DCD0
1670         init_timer(&mcfrs_timer_struct);
1671         mcfrs_timer_struct.function = mcfrs_timer;
1672         mcfrs_timer_struct.data = 0;
1673         mcfrs_timer_struct.expires = jiffies + HZ/25;
1674         add_timer(&mcfrs_timer_struct);
1675         mcfrs_ppstatus = mcf_getppdata() & (MCFPP_DCD0 | MCFPP_DCD1);
1676 #endif
1677         mcfrs_serial_driver = alloc_tty_driver(NR_PORTS);
1678         if (!mcfrs_serial_driver)
1679                 return -ENOMEM;
1680
1681         show_serial_version();
1682
1683         /* Initialize the tty_driver structure */
1684         mcfrs_serial_driver->owner = THIS_MODULE;
1685         mcfrs_serial_driver->name = "ttyS";
1686         mcfrs_serial_driver->devfs_name = "ttys/";
1687         mcfrs_serial_driver->driver_name = "serial";
1688         mcfrs_serial_driver->major = TTY_MAJOR;
1689         mcfrs_serial_driver->minor_start = 64;
1690         mcfrs_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1691         mcfrs_serial_driver->subtype = SERIAL_TYPE_NORMAL;
1692         mcfrs_serial_driver->init_termios = tty_std_termios;
1693
1694         mcfrs_serial_driver->init_termios.c_cflag =
1695                 mcfrs_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1696         mcfrs_serial_driver->flags = TTY_DRIVER_REAL_RAW;
1697
1698         tty_set_operations(mcfrs_serial_driver, &mcfrs_ops);
1699
1700         if (tty_register_driver(mcfrs_serial_driver)) {
1701                 printk("MCFRS: Couldn't register serial driver\n");
1702                 put_tty_driver(mcfrs_serial_driver);
1703                 return(-EBUSY);
1704         }
1705
1706         local_irq_save(flags);
1707
1708         /*
1709          *      Configure all the attached serial ports.
1710          */
1711         for (i = 0, info = mcfrs_table; (i < NR_PORTS); i++, info++) {
1712                 info->magic = SERIAL_MAGIC;
1713                 info->line = i;
1714                 info->tty = 0;
1715                 info->custom_divisor = 16;
1716                 info->close_delay = 50;
1717                 info->closing_wait = 3000;
1718                 info->x_char = 0;
1719                 info->event = 0;
1720                 info->count = 0;
1721                 info->blocked_open = 0;
1722                 INIT_WORK(&info->tqueue, mcfrs_offintr, info);
1723                 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1724                 init_waitqueue_head(&info->open_wait);
1725                 init_waitqueue_head(&info->close_wait);
1726
1727                 info->imr = 0;
1728                 mcfrs_setsignals(info, 0, 0);
1729                 mcfrs_irqinit(info);
1730
1731                 printk("ttyS%d at 0x%04x (irq = %d)", info->line,
1732                         (unsigned int) info->addr, info->irq);
1733                 printk(" is a builtin ColdFire UART\n");
1734         }
1735
1736         local_irq_restore(flags);
1737         return 0;
1738 }
1739
1740 module_init(mcfrs_init);
1741
1742 /****************************************************************************/
1743 /*                          Serial Console                                  */
1744 /****************************************************************************/
1745
1746 /*
1747  *      Quick and dirty UART initialization, for console output.
1748  */
1749
1750 void mcfrs_init_console(void)
1751 {
1752         volatile unsigned char  *uartp;
1753         unsigned int            clk;
1754
1755         /*
1756          *      Reset UART, get it into known state...
1757          */
1758         uartp = (volatile unsigned char *) (MCF_MBAR +
1759                 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1760
1761         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETRX;  /* reset RX */
1762         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETTX;  /* reset TX */
1763         uartp[MCFUART_UCR] = MCFUART_UCR_CMDRESETMRPTR;  /* reset MR pointer */
1764
1765         /*
1766          * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1767          */
1768         uartp[MCFUART_UMR] = MCFUART_MR1_PARITYNONE | MCFUART_MR1_CS8;
1769         uartp[MCFUART_UMR] = MCFUART_MR2_STOP1;
1770
1771         clk = ((MCF_BUSCLK / mcfrs_console_baud) + 16) / 32; /* set baud */
1772         uartp[MCFUART_UBG1] = (clk & 0xff00) >> 8;  /* set msb baud */
1773         uartp[MCFUART_UBG2] = (clk & 0xff);  /* set lsb baud */
1774
1775         uartp[MCFUART_UCSR] = MCFUART_UCSR_RXCLKTIMER | MCFUART_UCSR_TXCLKTIMER;
1776         uartp[MCFUART_UCR] = MCFUART_UCR_RXENABLE | MCFUART_UCR_TXENABLE;
1777
1778         mcfrs_console_inited++;
1779         return;
1780 }
1781
1782
1783 /*
1784  *      Setup for console. Argument comes from the boot command line.
1785  */
1786
1787 int mcfrs_console_setup(struct console *cp, char *arg)
1788 {
1789         int             i, n = CONSOLE_BAUD_RATE;
1790
1791         if (!cp)
1792                 return(-1);
1793
1794         if (!strncmp(cp->name, "ttyS", 4))
1795                 mcfrs_console_port = cp->index;
1796         else if (!strncmp(cp->name, "cua", 3))
1797                 mcfrs_console_port = cp->index;
1798         else
1799                 return(-1);
1800
1801         if (arg)
1802                 n = simple_strtoul(arg,NULL,0);
1803         for (i = 0; i < MCFRS_BAUD_TABLE_SIZE; i++)
1804                 if (mcfrs_baud_table[i] == n)
1805                         break;
1806         if (i < MCFRS_BAUD_TABLE_SIZE) {
1807                 mcfrs_console_baud = n;
1808                 mcfrs_console_cbaud = 0;
1809                 if (i > 15) {
1810                         mcfrs_console_cbaud |= CBAUDEX;
1811                         i -= 15;
1812                 }
1813                 mcfrs_console_cbaud |= i;
1814         }
1815         mcfrs_init_console(); /* make sure baud rate changes */
1816         return(0);
1817 }
1818
1819
1820 static struct tty_driver *mcfrs_console_device(struct console *c, int *index)
1821 {
1822         *index = c->index;
1823         return mcfrs_serial_driver;
1824 }
1825
1826
1827 /*
1828  *      Output a single character, using UART polled mode.
1829  *      This is used for console output.
1830  */
1831
1832 void mcfrs_put_char(char ch)
1833 {
1834         volatile unsigned char  *uartp;
1835         unsigned long           flags;
1836         int                     i;
1837
1838         uartp = (volatile unsigned char *) (MCF_MBAR +
1839                 (mcfrs_console_port ? MCFUART_BASE2 : MCFUART_BASE1));
1840
1841         local_irq_save(flags);
1842         for (i = 0; (i < 0x10000); i++) {
1843                 if (uartp[MCFUART_USR] & MCFUART_USR_TXREADY)
1844                         break;
1845         }
1846         if (i < 0x10000) {
1847                 uartp[MCFUART_UTB] = ch;
1848                 for (i = 0; (i < 0x10000); i++)
1849                         if (uartp[MCFUART_USR] & MCFUART_USR_TXEMPTY)
1850                                 break;
1851         }
1852         if (i >= 0x10000)
1853                 mcfrs_init_console(); /* try and get it back */
1854         local_irq_restore(flags);
1855
1856         return;
1857 }
1858
1859
1860 /*
1861  * rs_console_write is registered for printk output.
1862  */
1863
1864 void mcfrs_console_write(struct console *cp, const char *p, unsigned len)
1865 {
1866         if (!mcfrs_console_inited)
1867                 mcfrs_init_console();
1868         while (len-- > 0) {
1869                 if (*p == '\n')
1870                         mcfrs_put_char('\r');
1871                 mcfrs_put_char(*p++);
1872         }
1873 }
1874
1875 /*
1876  * declare our consoles
1877  */
1878
1879 struct console mcfrs_console = {
1880         .name           = "ttyS",
1881         .write          = mcfrs_console_write,
1882         .device         = mcfrs_console_device,
1883         .setup          = mcfrs_console_setup,
1884         .flags          = CON_PRINTBUFFER,
1885         .index          = -1,
1886 };
1887
1888 static int __init mcfrs_console_init(void)
1889 {
1890         register_console(&mcfrs_console);
1891         return 0;
1892 }
1893
1894 console_initcall(mcfrs_console_init);
1895
1896 /****************************************************************************/