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