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