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