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