original comment: +Wilson03172004,marked due to this pci host does not support MWI
[linux-2.4.git] / arch / ppc / 8xx_io / uart.c
1 /*
2  *  UART driver for MPC860 CPM SCC or SMC
3  *  Copyright (c) 1997 Dan Malek (dmalek@jlc.net)
4  *
5  * I used the serial.c driver as the framework for this driver.
6  * Give credit to those guys.
7  * The original code was written for the MBX860 board.  I tried to make
8  * it generic, but there may be some assumptions in the structures that
9  * have to be fixed later.
10  * To save porting time, I did not bother to change any object names
11  * that are not accessed outside of this file.
12  * It still needs lots of work........When it was easy, I included code
13  * to support the SCCs, but this has never been tested, nor is it complete.
14  * Only the SCCs support modem control, so that is not complete either.
15  *
16  * This module exports the following rs232 io functions:
17  *
18  *      int rs_8xx_init(void);
19  */
20
21 #include <linux/config.h>
22 #include <linux/module.h>
23 #include <linux/errno.h>
24 #include <linux/signal.h>
25 #include <linux/sched.h>
26 #include <linux/timer.h>
27 #include <linux/interrupt.h>
28 #include <linux/tty.h>
29 #include <linux/tty_flip.h>
30 #include <linux/serial.h>
31 #include <linux/serialP.h>
32 #include <linux/major.h>
33 #include <linux/string.h>
34 #include <linux/fcntl.h>
35 #include <linux/ptrace.h>
36 #include <linux/mm.h>
37 #include <linux/slab.h>
38 #include <linux/init.h>
39 #include <linux/delay.h>
40 #include <asm/uaccess.h>
41 #include <asm/8xx_immap.h>
42 #include <asm/mpc8xx.h>
43 #include <asm/commproc.h>
44 #include <asm/irq.h>
45 #include <asm/kgdb.h>
46 #ifdef CONFIG_MAGIC_SYSRQ
47 #include <linux/sysrq.h>
48 #endif
49
50 extern int kgdb_output_string (const char* s, unsigned int count);
51
52 #ifdef CONFIG_SERIAL_CONSOLE
53 #include <linux/console.h>
54
55 /* this defines the index into rs_table for the port to use
56 */
57 # ifndef CONFIG_SERIAL_CONSOLE_PORT
58 #  ifdef CONFIG_SCC3_ENET
59 #   ifdef CONFIG_CONS_SMC2
60 #    define CONFIG_SERIAL_CONSOLE_PORT  0       /* Console on SMC2 is 1st port */
61 #   else
62 #    error "Can't use SMC1 for console with Ethernet on SCC3"
63 #   endif
64 #  else /* ! CONFIG_SCC3_ENET */
65 #   ifdef CONFIG_CONS_SMC2                      /* Console on SMC2 */
66 #    define CONFIG_SERIAL_CONSOLE_PORT  1
67 #   else                                        /* Console on SMC1 */
68 #    define CONFIG_SERIAL_CONSOLE_PORT  0
69 #   endif /* CONFIG_CONS_SMC2 */
70 #  endif  /* CONFIG_SCC3_ENET */
71 # endif   /* CONFIG_SERIAL_CONSOLE_PORT */
72 #endif    /* CONFIG_SERIAL_CONSOLE */
73
74 #if 0
75 /* SCC2 for console
76 */
77 #undef CONFIG_SERIAL_CONSOLE_PORT
78 #define CONFIG_SERIAL_CONSOLE_PORT      2
79 #endif
80
81 #define TX_WAKEUP       ASYNC_SHARE_IRQ
82
83 static char *serial_name = "CPM UART driver";
84 static char *serial_version = "0.04";
85
86 /* TX buffer length used by my_console_write.
87    Assume minumun length until it gets set by this driver */
88 static int console_tx_buf_len = 1;
89
90 static DECLARE_TASK_QUEUE(tq_serial);
91
92 static struct tty_driver serial_driver, callout_driver;
93 static int serial_refcount;
94 static int serial_console_setup(struct console *co, char *options);
95
96 static void serial_console_write(struct console *c, const char *s,
97                                 unsigned count);
98 static kdev_t serial_console_device(struct console *c);
99
100 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
101 static unsigned long break_pressed; /* break, really ... */
102 #endif
103
104 /*
105  * Serial driver configuration section.  Here are the various options:
106  */
107 #define SERIAL_PARANOIA_CHECK
108 #define CONFIG_SERIAL_NOPAUSE_IO
109 #define SERIAL_DO_RESTART
110
111 /* Set of debugging defines */
112
113 #undef SERIAL_DEBUG_INTR
114 #undef SERIAL_DEBUG_OPEN
115 #undef SERIAL_DEBUG_FLOW
116 #undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
117
118 #define _INLINE_ inline
119
120 #define DBG_CNT(s)
121
122 /* We overload some of the items in the data structure to meet our
123  * needs.  For example, the port address is the CPM parameter ram
124  * offset for the SCC or SMC.  The maximum number of ports is 4 SCCs and
125  * 2 SMCs.  The "hub6" field is used to indicate the channel number, with
126  * a flag indicating SCC or SMC, and the number is used as an index into
127  * the CPM parameter area for this device.
128  * The "type" field is currently set to 0, for PORT_UNKNOWN.  It is
129  * not currently used.  I should probably use it to indicate the port
130  * type of SMC or SCC.
131  * The SMCs do not support any modem control signals.
132  */
133 #define smc_scc_num          hub6
134 #define NUM_IS_SCC           ((int)0x000100000)
135 #define NUM_BRG              ((int)0x0000FF00)
136 #define NUM_BRG_SHIFT        8
137 #define NUM                  ((int)0x000000FF)
138 #define NUM_SHIFT            0
139 #define PORT_NUM(P)          ((P) & NUM)
140 #define PORT_NUM_SET(N)      (((N)-1) << NUM_SHIFT)
141 #define PORT_IS_SCC(P)       ((P) & NUM_IS_SCC)
142 #define PORT_BRG(P)          (((P) & NUM_BRG) >> NUM_BRG_SHIFT)
143 #define PORT_BRG_SET(P,B)    (P) = (((P) & ~NUM_BRG) | ((B) << NUM_BRG_SHIFT))
144
145 /* Short names for the ports
146 */
147 #define QUICC_CPM_SMC1  (PORT_NUM_SET(1))
148 #define QUICC_CPM_SMC2  (PORT_NUM_SET(2))
149 #define QUICC_CPM_SCC1  (PORT_NUM_SET(1)|NUM_IS_SCC)
150 #define QUICC_CPM_SCC2  (PORT_NUM_SET(2)|NUM_IS_SCC)
151 #define QUICC_CPM_SCC3  (PORT_NUM_SET(3)|NUM_IS_SCC)
152 #define QUICC_CPM_SCC4  (PORT_NUM_SET(4)|NUM_IS_SCC)
153 #define QUICC_MAX_BRG   3  /* BRG1..BRG4 */
154
155 /* The serial port to use for KGDB. */
156 #ifdef CONFIG_KGDB_TTYS1
157 #define KGDB_SER_IDX    1       /* SCC1/SMC1 */
158 #elif CONFIG_KGDB_TTYS2
159 #define KGDB_SER_IDX    2       /* SCC2/SMC2 */
160 #elif CONFIG_KGDB_TTYS3
161 #define KGDB_SER_IDX    3       /* SCC4/SMC4 (future?) */
162 #else                           /* Unset, or ttyS0, use SCC1/SMC1 */
163 #define KGDB_SER_IDX    0
164 #endif
165
166 /* Processors other than the 860 only get SMCs configured by default.
167  * Either they don't have SCCs or they are allocated somewhere else.
168  * Of course, there are now 860s without some SCCs, so we will need to
169  * address that someday.
170  * The Embedded Planet Multimedia I/O cards use TDM interfaces to the
171  * stereo codec parts, and we use SMC2 to help support that.
172  */
173 static struct serial_state rs_table[] = {
174         /* UART CLK   PORT          IRQ      FLAGS  NUM   */
175 #ifdef CONFIG_8xx_SMC1
176         { 0,     0, PROFF_SMC1, CPMVEC_SMC1,   0,    QUICC_CPM_SMC1 },    /* SMC1 ttySx */
177 #endif
178 #ifdef CONFIG_8xx_SMC2
179         { 0,     0, PROFF_SMC2, CPMVEC_SMC2,   0,    QUICC_CPM_SMC2 },    /* SMC2 ttySx */
180 #endif
181 #ifdef CONFIG_8xx_SCC1
182         { 0,     0, PROFF_SCC1, CPMVEC_SCC1,   0,    QUICC_CPM_SCC1 },    /* SCC1 ttySx */
183 #endif
184 #ifdef CONFIG_8xx_SCC2
185         { 0,     0, PROFF_SCC2, CPMVEC_SCC2,   0,    QUICC_CPM_SCC2 },    /* SCC2 ttySx */
186 #endif
187 #ifdef CONFIG_8xx_SCC3
188         { 0,     0, PROFF_SCC3, CPMVEC_SCC3,   0,    QUICC_CPM_SCC3 },    /* SCC3 ttySx */
189 #endif
190 #ifdef CONFIG_8xx_SCC4
191         { 0,     0, PROFF_SCC4, CPMVEC_SCC4,   0,    QUICC_CPM_SCC4 },    /* SCC4 ttySx */
192 #endif
193 };
194
195 #define NR_PORTS        (sizeof(rs_table)/sizeof(struct serial_state))
196
197 static struct tty_struct *serial_table[NR_PORTS];
198 static struct termios *serial_termios[NR_PORTS];
199 static struct termios *serial_termios_locked[NR_PORTS];
200
201 /* The number of buffer descriptors and their sizes.
202 */
203 #define EARLY_BUF_SIZE  4
204 #define RX_NUM_FIFO     4
205 #define RX_BUF_SIZE     32
206 #define TX_NUM_FIFO     4
207 #define TX_BUF_SIZE     32
208
209 #ifndef MIN
210 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
211 #endif
212
213 /* The async_struct in serial.h does not really give us what we
214  * need, so define our own here.
215  */
216 typedef struct serial_info {
217         int                     magic;
218         int                     flags;
219         struct serial_state     *state;
220         struct tty_struct       *tty;
221         int                     read_status_mask;
222         int                     ignore_status_mask;
223         int                     timeout;
224         int                     line;
225         int                     x_char; /* xon/xoff character */
226         int                     close_delay;
227         unsigned short          closing_wait;
228         unsigned short          closing_wait2;
229         unsigned long           event;
230         unsigned long           last_active;
231         int                     blocked_open; /* # of blocked opens */
232         long                    session; /* Session of opening process */
233         long                    pgrp; /* pgrp of opening process */
234         struct tq_struct        tqueue;
235         struct tq_struct        tqueue_hangup;
236         wait_queue_head_t       open_wait;
237         wait_queue_head_t       close_wait;
238
239         /* CPM Buffer Descriptor pointers.
240         */
241         cbd_t                   *rx_bd_base;
242         cbd_t                   *rx_cur;
243         cbd_t                   *tx_bd_base;
244         cbd_t                   *tx_cur;
245
246         /* Virtual addresses for the FIFOs because we can't __va() a
247          * physical address anymore.
248          */
249          unsigned char          *rx_va_base;
250          unsigned char          *tx_va_base;
251 } ser_info_t;
252
253 static struct console sercons = {
254         name:           "ttyS",
255         write:          serial_console_write,
256         device:         serial_console_device,
257         setup:          serial_console_setup,
258         flags:          CON_PRINTBUFFER,
259         index:          CONFIG_SERIAL_CONSOLE_PORT,
260 };
261
262 static void change_speed(ser_info_t *info);
263 static void rs_8xx_wait_until_sent(struct tty_struct *tty, int timeout);
264
265 static inline int serial_paranoia_check(ser_info_t *info,
266                                         kdev_t device, const char *routine)
267 {
268 #ifdef SERIAL_PARANOIA_CHECK
269         static const char *badmagic =
270                 "Warning: bad magic number for serial struct (%s) in %s\n";
271         static const char *badinfo =
272                 "Warning: null async_struct for (%s) in %s\n";
273
274         if (!info) {
275                 printk(badinfo, kdevname(device), routine);
276                 return 1;
277         }
278         if (info->magic != SERIAL_MAGIC) {
279                 printk(badmagic, kdevname(device), routine);
280                 return 1;
281         }
282 #endif
283         return 0;
284 }
285
286 /*
287  * This is used to figure out the divisor speeds and the timeouts,
288  * indexed by the termio value.  The generic CPM functions are responsible
289  * for setting and assigning baud rate generators for us.
290  */
291 static int baud_table[] = {
292         0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
293         9600, 19200, 38400, 57600, 115200, 230400, 460800, 0 };
294
295
296 /*
297  * ------------------------------------------------------------
298  * rs_stop() and rs_start()
299  *
300  * This routines are called before setting or resetting tty->stopped.
301  * They enable or disable transmitter interrupts, as necessary.
302  * ------------------------------------------------------------
303  */
304 static void rs_8xx_stop(struct tty_struct *tty)
305 {
306         ser_info_t *info = (ser_info_t *)tty->driver_data;
307         int     idx;
308         unsigned long flags;
309         volatile scc_t  *sccp;
310         volatile smc_t  *smcp;
311
312         if (serial_paranoia_check(info, tty->device, "rs_stop"))
313                 return;
314
315         save_flags(flags); cli();
316         idx = PORT_NUM(info->state->smc_scc_num);
317         if (PORT_IS_SCC(info->state->smc_scc_num)) {
318                 sccp = &cpmp->cp_scc[idx];
319                 sccp->scc_sccm &= ~UART_SCCM_TX;
320         }
321         else {
322                 smcp = &cpmp->cp_smc[idx];
323                 smcp->smc_smcm &= ~SMCM_TX;
324         }
325         restore_flags(flags);
326 }
327
328 static void rs_8xx_start(struct tty_struct *tty)
329 {
330         ser_info_t *info = (ser_info_t *)tty->driver_data;
331         int     idx;
332         unsigned long flags;
333         volatile scc_t  *sccp;
334         volatile smc_t  *smcp;
335
336         if (serial_paranoia_check(info, tty->device, "rs_stop"))
337                 return;
338
339         idx = PORT_NUM(info->state->smc_scc_num);
340         save_flags(flags); cli();
341         if (PORT_IS_SCC(info->state->smc_scc_num)) {
342                 sccp = &cpmp->cp_scc[idx];
343                 sccp->scc_sccm |= UART_SCCM_TX;
344         }
345         else {
346                 smcp = &cpmp->cp_smc[idx];
347                 smcp->smc_smcm |= SMCM_TX;
348         }
349         restore_flags(flags);
350 }
351
352 /*
353  * ----------------------------------------------------------------------
354  *
355  * Here starts the interrupt handling routines.  All of the following
356  * subroutines are declared as inline and are folded into
357  * rs_interrupt().  They were separated out for readability's sake.
358  *
359  * Note: rs_interrupt() is a "fast" interrupt, which means that it
360  * runs with interrupts turned off.  People who may want to modify
361  * rs_interrupt() should try to keep the interrupt handler as fast as
362  * possible.  After you are done making modifications, it is not a bad
363  * idea to do:
364  *
365  * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
366  *
367  * and look at the resulting assemble code in serial.s.
368  *
369  *                              - Ted Ts'o (tytso@mit.edu), 7-Mar-93
370  * -----------------------------------------------------------------------
371  */
372
373 /*
374  * This routine is used by the interrupt handler to schedule
375  * processing in the software interrupt portion of the driver.
376  */
377 static _INLINE_ void rs_sched_event(ser_info_t *info,
378                                   int event)
379 {
380         info->event |= 1 << event;
381         queue_task(&info->tqueue, &tq_serial);
382         mark_bh(SERIAL_BH);
383 }
384
385 static _INLINE_ void receive_chars(ser_info_t *info, struct pt_regs *regs)
386 {
387         struct tty_struct *tty = info->tty;
388         unsigned char ch, *cp;
389         /*int   ignored = 0;*/
390         int     i;
391         ushort  status;
392         struct  async_icount *icount;
393         volatile cbd_t  *bdp;
394
395         icount = &info->state->icount;
396
397         /* Just loop through the closed BDs and copy the characters into
398          * the buffer.
399          */
400         bdp = info->rx_cur;
401         for (;;) {
402                 if (bdp->cbd_sc & BD_SC_EMPTY)  /* If this one is empty */
403                         break;                  /*   we are all done */
404
405                 /* The read status mask tell us what we should do with
406                  * incoming characters, especially if errors occur.
407                  * One special case is the use of BD_SC_EMPTY.  If
408                  * this is not set, we are supposed to be ignoring
409                  * inputs.  In this case, just mark the buffer empty and
410                  * continue.
411                 if (!(info->read_status_mask & BD_SC_EMPTY)) {
412                         bdp->cbd_sc |= BD_SC_EMPTY;
413                         bdp->cbd_sc &=
414                                 ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
415
416                         if (bdp->cbd_sc & BD_SC_WRAP)
417                                 bdp = info->rx_bd_base;
418                         else
419                                 bdp++;
420                         continue;
421                 }
422                  */
423
424                 /* Get the number of characters and the buffer pointer.
425                 */
426                 i = bdp->cbd_datlen;
427                 cp = info->rx_va_base + ((bdp - info->rx_bd_base) * RX_BUF_SIZE);
428                 status = bdp->cbd_sc;
429
430 #ifdef CONFIG_KGDB
431                 if (info->state->smc_scc_num == KGDB_SER_IDX &&
432                                 (*cp == 0x03 || *cp == '$')) {
433                         breakpoint();
434                         return;
435                 }
436 #endif
437
438                 /* Check to see if there is room in the tty buffer for
439                  * the characters in our BD buffer.  If not, we exit
440                  * now, leaving the BD with the characters.  We'll pick
441                  * them up again on the next receive interrupt (which could
442                  * be a timeout).
443                  */
444                 if ((tty->flip.count + i) >= TTY_FLIPBUF_SIZE)
445                         break;
446
447                 while (i-- > 0) {
448                         ch = *cp++;
449                         *tty->flip.char_buf_ptr = ch;
450                         icount->rx++;
451
452 #ifdef SERIAL_DEBUG_INTR
453                         printk("DR%02x:%02x...", ch, status);
454 #endif
455                         *tty->flip.flag_buf_ptr = 0;
456                         if (status & (BD_SC_BR | BD_SC_FR |
457                                        BD_SC_PR | BD_SC_OV)) {
458                                 /*
459                                  * For statistics only
460                                  */
461                                 if (status & BD_SC_BR)
462                                         icount->brk++;
463                                 else if (status & BD_SC_PR)
464                                         icount->parity++;
465                                 else if (status & BD_SC_FR)
466                                         icount->frame++;
467                                 if (status & BD_SC_OV)
468                                         icount->overrun++;
469
470                                 /*
471                                  * Now check to see if character should be
472                                  * ignored, and mask off conditions which
473                                  * should be ignored.
474                                 if (status & info->ignore_status_mask) {
475                                         if (++ignored > 100)
476                                                 break;
477                                         continue;
478                                 }
479                                  */
480                                 status &= info->read_status_mask;
481
482                                 if (status & (BD_SC_BR)) {
483 #ifdef SERIAL_DEBUG_INTR
484                                         printk("handling break....");
485 #endif
486                                         *tty->flip.flag_buf_ptr = TTY_BREAK;
487                                         if (info->flags & ASYNC_SAK)
488                                                 do_SAK(tty);
489                                 } else if (status & BD_SC_PR)
490                                         *tty->flip.flag_buf_ptr = TTY_PARITY;
491                                 else if (status & BD_SC_FR)
492                                         *tty->flip.flag_buf_ptr = TTY_FRAME;
493                                 if (status & BD_SC_OV) {
494                                         /*
495                                          * Overrun is special, since it's
496                                          * reported immediately, and doesn't
497                                          * affect the current character
498                                          */
499                                         if (tty->flip.count < TTY_FLIPBUF_SIZE) {
500                                                 tty->flip.count++;
501                                                 tty->flip.flag_buf_ptr++;
502                                                 tty->flip.char_buf_ptr++;
503                                                 *tty->flip.flag_buf_ptr =
504                                                                 TTY_OVERRUN;
505                                         }
506                                 }
507                         }
508 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
509                         if (break_pressed && info->line == sercons.index) {
510                                 if (ch != 0 && time_before(jiffies,
511                                                         break_pressed + HZ*5)) {
512                                         handle_sysrq(ch, regs, NULL, NULL);
513                                         break_pressed = 0;
514                                         goto ignore_char;
515                                 } else
516                                         break_pressed = 0;
517                         }
518 #endif
519                         if (tty->flip.count >= TTY_FLIPBUF_SIZE)
520                                 break;
521
522                         tty->flip.flag_buf_ptr++;
523                         tty->flip.char_buf_ptr++;
524                         tty->flip.count++;
525                 }
526
527 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
528         ignore_char:
529 #endif
530                 /* This BD is ready to be used again.  Clear status.
531                  * Get next BD.
532                  */
533                 bdp->cbd_sc |= BD_SC_EMPTY;
534                 bdp->cbd_sc &= ~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);
535
536                 if (bdp->cbd_sc & BD_SC_WRAP)
537                         bdp = info->rx_bd_base;
538                 else
539                         bdp++;
540         }
541         info->rx_cur = (cbd_t *)bdp;
542
543         queue_task(&tty->flip.tqueue, &tq_timer);
544 }
545
546 static _INLINE_ void receive_break(ser_info_t *info, struct pt_regs *regs)
547 {
548         struct tty_struct *tty = info->tty;
549
550         info->state->icount.brk++;
551
552 #if defined(CONFIG_SERIAL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
553         if (info->line == sercons.index) {
554                 if (!break_pressed) {
555                         break_pressed = jiffies;
556                         return;
557                 } else
558                         break_pressed = 0;
559         }
560 #endif
561
562         /* Check to see if there is room in the tty buffer for
563          * the break.  If not, we exit now, losing the break.  FIXME
564          */
565         if ((tty->flip.count + 1) >= TTY_FLIPBUF_SIZE)
566                 return;
567         *(tty->flip.flag_buf_ptr++) = TTY_BREAK;
568         *(tty->flip.char_buf_ptr++) = 0;
569         tty->flip.count++;
570
571         queue_task(&tty->flip.tqueue, &tq_timer);
572 }
573
574 static _INLINE_ void transmit_chars(ser_info_t *info, struct pt_regs *regs)
575 {
576
577         if ((info->flags & TX_WAKEUP) ||
578             (info->tty->flags & (1 << TTY_DO_WRITE_WAKEUP))) {
579                 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
580         }
581
582 #ifdef SERIAL_DEBUG_INTR
583         printk("THRE...");
584 #endif
585 }
586
587 #ifdef notdef
588         /* I need to do this for the SCCs, so it is left as a reminder.
589         */
590 static _INLINE_ void check_modem_status(struct async_struct *info)
591 {
592         int     status;
593         struct  async_icount *icount;
594
595         status = serial_in(info, UART_MSR);
596
597         if (status & UART_MSR_ANY_DELTA) {
598                 icount = &info->state->icount;
599                 /* update input line counters */
600                 if (status & UART_MSR_TERI)
601                         icount->rng++;
602                 if (status & UART_MSR_DDSR)
603                         icount->dsr++;
604                 if (status & UART_MSR_DDCD) {
605                         icount->dcd++;
606 #ifdef CONFIG_HARD_PPS
607                         if ((info->flags & ASYNC_HARDPPS_CD) &&
608                             (status & UART_MSR_DCD))
609                                 hardpps();
610 #endif
611                 }
612                 if (status & UART_MSR_DCTS)
613                         icount->cts++;
614                 wake_up_interruptible(&info->delta_msr_wait);
615         }
616
617         if ((info->flags & ASYNC_CHECK_CD) && (status & UART_MSR_DDCD)) {
618 #if (defined(SERIAL_DEBUG_OPEN) || defined(SERIAL_DEBUG_INTR))
619                 printk("ttys%d CD now %s...", info->line,
620                        (status & UART_MSR_DCD) ? "on" : "off");
621 #endif
622                 if (status & UART_MSR_DCD)
623                         wake_up_interruptible(&info->open_wait);
624                 else if (!((info->flags & ASYNC_CALLOUT_ACTIVE) &&
625                            (info->flags & ASYNC_CALLOUT_NOHUP))) {
626 #ifdef SERIAL_DEBUG_OPEN
627                         printk("scheduling hangup...");
628 #endif
629                         MOD_INC_USE_COUNT;
630                         if (schedule_task(&info->tqueue_hangup) == 0)
631                                 MOD_DEC_USE_COUNT;
632                 }
633         }
634         if (info->flags & ASYNC_CTS_FLOW) {
635                 if (info->tty->hw_stopped) {
636                         if (status & UART_MSR_CTS) {
637 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
638                                 printk("CTS tx start...");
639 #endif
640                                 info->tty->hw_stopped = 0;
641                                 info->IER |= UART_IER_THRI;
642                                 serial_out(info, UART_IER, info->IER);
643                                 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP);
644                                 return;
645                         }
646                 } else {
647                         if (!(status & UART_MSR_CTS)) {
648 #if (defined(SERIAL_DEBUG_INTR) || defined(SERIAL_DEBUG_FLOW))
649                                 printk("CTS tx stop...");
650 #endif
651                                 info->tty->hw_stopped = 1;
652                                 info->IER &= ~UART_IER_THRI;
653                                 serial_out(info, UART_IER, info->IER);
654                         }
655                 }
656         }
657 }
658 #endif
659
660 /*
661  * This is the serial driver's interrupt routine for a single port
662  */
663 static void rs_8xx_interrupt(int irq, void *dev_id, struct pt_regs *regs)
664 {
665         u_char  events;
666         int     idx;
667         ser_info_t *info;
668         volatile smc_t  *smcp;
669         volatile scc_t  *sccp;
670
671         info = (ser_info_t *)dev_id;
672
673         idx = PORT_NUM(info->state->smc_scc_num);
674         if (PORT_IS_SCC(info->state->smc_scc_num)) {
675                 sccp = &cpmp->cp_scc[idx];
676                 events = sccp->scc_scce;
677                 if (events & SMCM_BRKE)
678                         receive_break(info, regs);
679                 if (events & SCCM_RX)
680                         receive_chars(info, regs);
681                 if (events & SCCM_TX)
682                         transmit_chars(info, regs);
683                 sccp->scc_scce = events;
684         }
685         else {
686                 smcp = &cpmp->cp_smc[idx];
687                 events = smcp->smc_smce;
688                 if (events & SMCM_BRKE)
689                         receive_break(info, regs);
690                 if (events & SMCM_RX)
691                         receive_chars(info, regs);
692                 if (events & SMCM_TX)
693                         transmit_chars(info, regs);
694                 smcp->smc_smce = events;
695         }
696
697 #ifdef SERIAL_DEBUG_INTR
698         printk("rs_interrupt_single(%d, %x)...",
699                                         info->state->smc_scc_num, events);
700 #endif
701 #ifdef modem_control
702         check_modem_status(info);
703 #endif
704         info->last_active = jiffies;
705 #ifdef SERIAL_DEBUG_INTR
706         printk("end.\n");
707 #endif
708 }
709
710
711 /*
712  * -------------------------------------------------------------------
713  * Here ends the serial interrupt routines.
714  * -------------------------------------------------------------------
715  */
716
717 /*
718  * This routine is used to handle the "bottom half" processing for the
719  * serial driver, known also the "software interrupt" processing.
720  * This processing is done at the kernel interrupt level, after the
721  * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
722  * is where time-consuming activities which can not be done in the
723  * interrupt driver proper are done; the interrupt driver schedules
724  * them using rs_sched_event(), and they get done here.
725  */
726 static void do_serial_bh(void)
727 {
728         run_task_queue(&tq_serial);
729 }
730
731 static void do_softint(void *private_)
732 {
733         ser_info_t      *info = (ser_info_t *) private_;
734         struct tty_struct       *tty;
735
736         tty = info->tty;
737         if (!tty)
738                 return;
739
740         if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
741                 tty_wakeup(tty);
742         }
743 }
744
745 /*
746  * This routine is called from the scheduler tqueue when the interrupt
747  * routine has signalled that a hangup has occurred.  The path of
748  * hangup processing is:
749  *
750  *      serial interrupt routine -> (scheduler tqueue) ->
751  *      do_serial_hangup() -> tty->hangup() -> rs_hangup()
752  *
753  */
754 static void do_serial_hangup(void *private_)
755 {
756         struct async_struct     *info = (struct async_struct *) private_;
757         struct tty_struct       *tty;
758
759         tty = info->tty;
760         if (tty)
761                 tty_hangup(tty);
762         MOD_DEC_USE_COUNT;
763 }
764
765 /*static void rs_8xx_timer(void)
766 {
767         printk("rs_8xx_timer\n");
768 }*/
769
770
771 static int startup(ser_info_t *info)
772 {
773         unsigned long flags;
774         int     retval=0;
775         int     idx;
776         struct serial_state *state= info->state;
777         volatile smc_t          *smcp;
778         volatile scc_t          *sccp;
779         volatile smc_uart_t     *up;
780         volatile scc_uart_t     *scup;
781
782
783         save_flags(flags); cli();
784
785         if (info->flags & ASYNC_INITIALIZED) {
786                 goto errout;
787         }
788
789 #ifdef maybe
790         if (!state->port || !state->type) {
791                 if (info->tty)
792                         set_bit(TTY_IO_ERROR, &info->tty->flags);
793                 goto errout;
794         }
795 #endif
796
797 #ifdef SERIAL_DEBUG_OPEN
798         printk("starting up ttys%d (irq %d)...", info->line, state->irq);
799 #endif
800
801
802 #ifdef modem_control
803         info->MCR = 0;
804         if (info->tty->termios->c_cflag & CBAUD)
805                 info->MCR = UART_MCR_DTR | UART_MCR_RTS;
806 #endif
807
808         if (info->tty)
809                 clear_bit(TTY_IO_ERROR, &info->tty->flags);
810
811         /*
812          * and set the speed of the serial port
813          */
814         change_speed(info);
815
816         idx = PORT_NUM(info->state->smc_scc_num);
817         if (PORT_IS_SCC(info->state->smc_scc_num)) {
818                 sccp = &cpmp->cp_scc[idx];
819                 scup = (scc_uart_t *)&cpmp->cp_dparam[state->port];
820                 scup->scc_genscc.scc_mrblr = RX_BUF_SIZE;
821                 scup->scc_maxidl = RX_BUF_SIZE;
822                 sccp->scc_sccm |= (UART_SCCM_TX | UART_SCCM_RX);
823                 sccp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
824         }
825         else {
826                 smcp = &cpmp->cp_smc[idx];
827
828                 /* Enable interrupts and I/O.
829                 */
830                 smcp->smc_smcm |= (SMCM_RX | SMCM_TX);
831                 smcp->smc_smcmr |= (SMCMR_REN | SMCMR_TEN);
832
833                 /* We can tune the buffer length and idle characters
834                  * to take advantage of the entire incoming buffer size.
835                  * If mrblr is something other than 1, maxidl has to be
836                  * non-zero or we never get an interrupt.  The maxidl
837                  * is the number of character times we wait after reception
838                  * of the last character before we decide no more characters
839                  * are coming.
840                  */
841                 up = (smc_uart_t *)&cpmp->cp_dparam[state->port];
842                 up->smc_mrblr = RX_BUF_SIZE;
843                 up->smc_maxidl = RX_BUF_SIZE;
844                 up->smc_brkcr = 1;      /* number of break chars */
845         }
846
847         info->flags |= ASYNC_INITIALIZED;
848         restore_flags(flags);
849         return 0;
850
851 errout:
852         restore_flags(flags);
853         return retval;
854 }
855
856 /*
857  * This routine will shutdown a serial port; interrupts are disabled, and
858  * DTR is dropped if the hangup on close termio flag is on.
859  */
860 static void shutdown(ser_info_t * info)
861 {
862         unsigned long   flags;
863         struct serial_state *state;
864         int             idx;
865         volatile smc_t  *smcp;
866         volatile scc_t  *sccp;
867
868         if (!(info->flags & ASYNC_INITIALIZED))
869                 return;
870
871         state = info->state;
872
873 #ifdef SERIAL_DEBUG_OPEN
874         printk("Shutting down serial port %d (irq %d)....", info->line,
875                state->irq);
876 #endif
877
878         save_flags(flags); cli(); /* Disable interrupts */
879
880         idx = PORT_NUM(state->smc_scc_num);
881         if (PORT_IS_SCC(state->smc_scc_num)) {
882                 sccp = &cpmp->cp_scc[idx];
883                 sccp->scc_gsmrl &= ~(SCC_GSMRL_ENR | SCC_GSMRL_ENT);
884 #ifdef CONFIG_SERIAL_CONSOLE
885                 /* We can't disable the transmitter if this is the
886                  * system console.
887                  */
888                 if ((state - rs_table) != CONFIG_SERIAL_CONSOLE_PORT)
889 #endif
890                         sccp->scc_sccm &= ~(UART_SCCM_TX | UART_SCCM_RX);
891         }
892         else {
893                 smcp = &cpmp->cp_smc[idx];
894
895                 /* Disable interrupts and I/O.
896                 */
897                 smcp->smc_smcm &= ~(SMCM_RX | SMCM_TX);
898 #ifdef CONFIG_SERIAL_CONSOLE
899                 /* We can't disable the transmitter if this is the
900                  * system console.
901                  */
902                 if ((state - rs_table) != CONFIG_SERIAL_CONSOLE_PORT)
903 #endif
904                         smcp->smc_smcmr &= ~(SMCMR_REN | SMCMR_TEN);
905         }
906
907         if (info->tty)
908                 set_bit(TTY_IO_ERROR, &info->tty->flags);
909
910         info->flags &= ~ASYNC_INITIALIZED;
911         restore_flags(flags);
912 }
913
914 /*
915  * This routine is called to set the UART divisor registers to match
916  * the specified baud rate for a serial port.
917  */
918 static void change_speed(ser_info_t *info)
919 {
920         int     baud_rate;
921         unsigned cflag, cval, scval, prev_mode, new_mode;
922         int     i, bits, sbits, idx;
923         unsigned long   flags;
924         struct serial_state *state;
925         volatile smc_t  *smcp;
926         volatile scc_t  *sccp;
927
928         if (!info->tty || !info->tty->termios)
929                 return;
930         cflag = info->tty->termios->c_cflag;
931
932         state = info->state;
933
934         /* Character length programmed into the mode register is the
935          * sum of: 1 start bit, number of data bits, 0 or 1 parity bit,
936          * 1 or 2 stop bits, minus 1.
937          * The value 'bits' counts this for us.
938          */
939         cval = 0;
940         scval = 0;
941
942         /* byte size and parity */
943         switch (cflag & CSIZE) {
944               case CS5: bits = 5; break;
945               case CS6: bits = 6; break;
946               case CS7: bits = 7; break;
947               case CS8: bits = 8; break;
948               /* Never happens, but GCC is too dumb to figure it out */
949               default:  bits = 8; break;
950         }
951         sbits = bits - 5;
952
953         if (cflag & CSTOPB) {
954                 cval |= SMCMR_SL;       /* Two stops */
955                 scval |= SCU_PMSR_SL;
956                 bits++;
957         }
958         if (cflag & PARENB) {
959                 cval |= SMCMR_PEN;
960                 scval |= SCU_PMSR_PEN;
961                 bits++;
962                 if (!(cflag & PARODD)) {
963                         cval |= SMCMR_PM_EVEN;
964                         scval |= (SCU_PMSR_REVP | SCU_PMSR_TEVP);
965                 }
966         }
967
968         /* Determine divisor based on baud rate */
969         i = cflag & CBAUD;
970         if (i >= (sizeof(baud_table)/sizeof(int)))
971                 baud_rate = 9600;
972         else
973                 baud_rate = baud_table[i];
974
975         info->timeout = (TX_BUF_SIZE*HZ*bits);
976         info->timeout += HZ/50;         /* Add .02 seconds of slop */
977
978 #ifdef modem_control
979         /* CTS flow control flag and modem status interrupts */
980         info->IER &= ~UART_IER_MSI;
981         if (info->flags & ASYNC_HARDPPS_CD)
982                 info->IER |= UART_IER_MSI;
983         if (cflag & CRTSCTS) {
984                 info->flags |= ASYNC_CTS_FLOW;
985                 info->IER |= UART_IER_MSI;
986         } else
987                 info->flags &= ~ASYNC_CTS_FLOW;
988         if (cflag & CLOCAL)
989                 info->flags &= ~ASYNC_CHECK_CD;
990         else {
991                 info->flags |= ASYNC_CHECK_CD;
992                 info->IER |= UART_IER_MSI;
993         }
994         serial_out(info, UART_IER, info->IER);
995 #endif
996
997         /*
998          * Set up parity check flag
999          */
1000 #define RELEVANT_IFLAG(iflag) (iflag & (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK))
1001
1002         info->read_status_mask = (BD_SC_EMPTY | BD_SC_OV);
1003         if (I_INPCK(info->tty))
1004                 info->read_status_mask |= BD_SC_FR | BD_SC_PR;
1005         if (I_BRKINT(info->tty) || I_PARMRK(info->tty))
1006                 info->read_status_mask |= BD_SC_BR;
1007
1008         /*
1009          * Characters to ignore
1010          */
1011         info->ignore_status_mask = 0;
1012         if (I_IGNPAR(info->tty))
1013                 info->ignore_status_mask |= BD_SC_PR | BD_SC_FR;
1014         if (I_IGNBRK(info->tty)) {
1015                 info->ignore_status_mask |= BD_SC_BR;
1016                 /*
1017                  * If we're ignore parity and break indicators, ignore
1018                  * overruns too.  (For real raw support).
1019                  */
1020                 if (I_IGNPAR(info->tty))
1021                         info->ignore_status_mask |= BD_SC_OV;
1022         }
1023         /*
1024          * !!! ignore all characters if CREAD is not set
1025          */
1026         if ((cflag & CREAD) == 0)
1027                 info->read_status_mask &= ~BD_SC_EMPTY;
1028         save_flags(flags); cli();
1029
1030         /* Start bit has not been added (so don't, because we would just
1031          * subtract it later), and we need to add one for the number of
1032          * stops bits (there is always at least one).
1033          */
1034         bits++;
1035         idx = PORT_NUM(state->smc_scc_num);
1036         if (PORT_IS_SCC(state->smc_scc_num)) {
1037                 sccp = &cpmp->cp_scc[idx];
1038                 new_mode = (sbits << 12) | scval;
1039                 prev_mode = sccp->scc_pmsr;
1040                 if (!(prev_mode & SCU_PMSR_PEN))
1041                         /* If parity is disabled, mask out even/odd */
1042                         prev_mode &= ~(SCU_PMSR_TPM|SCU_PMSR_RPM);
1043                 if (prev_mode != new_mode)
1044                         sccp->scc_pmsr = new_mode;
1045         }
1046         else {
1047                 smcp = &cpmp->cp_smc[idx];
1048
1049                 /* Set the mode register.  We want to keep a copy of the
1050                  * enables, because we want to put them back if they were
1051                  * present.
1052                  */
1053                 prev_mode = smcp->smc_smcmr & (SMCMR_REN | SMCMR_TEN);
1054                 new_mode = smcr_mk_clen(bits) | cval | SMCMR_SM_UART
1055                         | prev_mode;
1056                 if (!(prev_mode & SMCMR_PEN))
1057                         /* If parity is disabled, mask out even/odd */
1058                         prev_mode &= ~SMCMR_PM_EVEN;
1059                 if (prev_mode != new_mode)
1060                         smcp->smc_smcmr = new_mode;
1061         }
1062
1063         m8xx_cpm_setbrg(PORT_BRG(state->smc_scc_num), baud_rate);
1064
1065         restore_flags(flags);
1066 }
1067
1068 static void rs_8xx_put_char(struct tty_struct *tty, unsigned char ch)
1069 {
1070         ser_info_t *info = (ser_info_t *)tty->driver_data;
1071         volatile cbd_t  *bdp;
1072         unsigned char *cp;
1073         unsigned long flags;
1074
1075         if (serial_paranoia_check(info, tty->device, "rs_put_char"))
1076                 return;
1077
1078         if (!tty)
1079                 return;
1080
1081         local_irq_save(flags);
1082         bdp = info->tx_cur;
1083         /* Get next BD.
1084         */
1085         if (bdp->cbd_sc & BD_SC_WRAP)
1086                 info->tx_cur = info->tx_bd_base;
1087         else
1088                 info->tx_cur = (cbd_t *)bdp + 1;
1089         local_irq_restore(flags);
1090
1091         while (bdp->cbd_sc & BD_SC_READY);
1092
1093         cp = info->tx_va_base + ((bdp - info->tx_bd_base) * TX_BUF_SIZE);
1094         *cp = ch;
1095         bdp->cbd_datlen = 1;
1096         bdp->cbd_sc |= BD_SC_READY;
1097
1098 }
1099
1100 static int rs_8xx_write(struct tty_struct * tty, int from_user,
1101                     const unsigned char *buf, int count)
1102 {
1103         int     c, ret = 0;
1104         ser_info_t *info = (ser_info_t *)tty->driver_data;
1105         volatile cbd_t *bdp;
1106         unsigned char   *cp;
1107         unsigned long flags;
1108
1109 #ifdef CONFIG_KGDB_CONSOLE
1110         /* Try to let stub handle output. Returns true if it did. */
1111         if (kgdb_output_string(buf, count))
1112             return ret;
1113 #endif
1114
1115         if (serial_paranoia_check(info, tty->device, "rs_write"))
1116                 return 0;
1117
1118         if (!tty)
1119                 return 0;
1120
1121         while (1) {
1122                 c = MIN(count, TX_BUF_SIZE);
1123
1124                 if (c <= 0)
1125                         break;
1126
1127                 local_irq_save(flags);
1128                 bdp = info->tx_cur;
1129                 if (bdp->cbd_sc & BD_SC_READY) {
1130                         info->flags |= TX_WAKEUP;
1131                         local_irq_restore(flags);
1132                         break;
1133                 }
1134                 /* Get next BD.
1135                  */
1136                 if (bdp->cbd_sc & BD_SC_WRAP)
1137                         info->tx_cur = info->tx_bd_base;
1138                 else
1139                         info->tx_cur = (cbd_t *)bdp + 1;
1140                 local_irq_restore(flags);
1141
1142                 cp = info->tx_va_base + ((bdp - info->tx_bd_base) *
1143                                 TX_BUF_SIZE);
1144                 if (from_user)
1145                         c -= copy_from_user((void *)cp, buf, c);
1146                 else
1147                         memcpy((void *)cp, buf, c);
1148
1149                 if (c) {
1150                         bdp->cbd_datlen = c;
1151                         bdp->cbd_sc |= BD_SC_READY;
1152                 } else {
1153                         /* Need to TX at least 1 char to keep CPM sane */
1154                         bdp->cbd_datlen = 1;
1155                         *cp = 0;
1156                         bdp->cbd_sc |= BD_SC_READY;
1157
1158                         if (!ret)
1159                                 ret = -EFAULT;
1160                         break;
1161                 }
1162                 buf += c;
1163                 count -= c;
1164                 ret += c;
1165         }
1166         return ret;
1167 }
1168
1169 static int rs_8xx_write_room(struct tty_struct *tty)
1170 {
1171         ser_info_t *info = (ser_info_t *)tty->driver_data;
1172         int     ret;
1173
1174         if (serial_paranoia_check(info, tty->device, "rs_write_room"))
1175                 return 0;
1176
1177         if ((info->tx_cur->cbd_sc & BD_SC_READY) == 0) {
1178                 info->flags &= ~TX_WAKEUP;
1179                 ret = TX_BUF_SIZE;
1180         }
1181         else {
1182                 info->flags |= TX_WAKEUP;
1183                 ret = 0;
1184         }
1185         return ret;
1186 }
1187
1188 /* I could track this with transmit counters....maybe later.
1189 */
1190 static int rs_8xx_chars_in_buffer(struct tty_struct *tty)
1191 {
1192         ser_info_t *info = (ser_info_t *)tty->driver_data;
1193
1194         if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer"))
1195                 return 0;
1196         return 0;
1197 }
1198
1199 static void rs_8xx_flush_buffer(struct tty_struct *tty)
1200 {
1201         ser_info_t *info = (ser_info_t *)tty->driver_data;
1202
1203         if (serial_paranoia_check(info, tty->device, "rs_flush_buffer"))
1204                 return;
1205
1206         /* There is nothing to "flush", whatever we gave the CPM
1207          * is on its way out.
1208          */
1209         tty_wakeup(tty);
1210         info->flags &= ~TX_WAKEUP;
1211 }
1212
1213 /*
1214  * This function is used to send a high-priority XON/XOFF character to
1215  * the device
1216  */
1217 static void rs_8xx_send_xchar(struct tty_struct *tty, char ch)
1218 {
1219         volatile cbd_t  *bdp;
1220         unsigned char   *cp;
1221         unsigned long flags;
1222
1223         ser_info_t *info = (ser_info_t *)tty->driver_data;
1224
1225         if (serial_paranoia_check(info, tty->device, "rs_send_char"))
1226                 return;
1227
1228         local_irq_save(flags);
1229         bdp = info->tx_cur;
1230         /* Get next BD.
1231         */
1232         if (bdp->cbd_sc & BD_SC_WRAP)
1233                 info->tx_cur = info->tx_bd_base;
1234         else
1235                 info->tx_cur = (cbd_t *)bdp + 1;
1236         local_irq_restore(flags);
1237         while (bdp->cbd_sc & BD_SC_READY);
1238
1239         cp = info->tx_va_base + ((bdp - info->tx_bd_base) * TX_BUF_SIZE);
1240         *cp = ch;
1241         bdp->cbd_datlen = 1;
1242         bdp->cbd_sc |= BD_SC_READY;
1243 }
1244
1245 /*
1246  * ------------------------------------------------------------
1247  * rs_throttle()
1248  *
1249  * This routine is called by the upper-layer tty layer to signal that
1250  * incoming characters should be throttled.
1251  * ------------------------------------------------------------
1252  */
1253 static void rs_8xx_throttle(struct tty_struct * tty)
1254 {
1255         ser_info_t *info = (ser_info_t *)tty->driver_data;
1256 #ifdef SERIAL_DEBUG_THROTTLE
1257         char    buf[64];
1258
1259         printk("throttle %s: %d....\n", _tty_name(tty, buf),
1260                tty->ldisc.chars_in_buffer(tty));
1261 #endif
1262
1263         if (serial_paranoia_check(info, tty->device, "rs_throttle"))
1264                 return;
1265
1266         if (I_IXOFF(tty))
1267                 rs_8xx_send_xchar(tty, STOP_CHAR(tty));
1268
1269 #ifdef modem_control
1270         if (tty->termios->c_cflag & CRTSCTS)
1271                 info->MCR &= ~UART_MCR_RTS;
1272
1273         cli();
1274         serial_out(info, UART_MCR, info->MCR);
1275         sti();
1276 #endif
1277 }
1278
1279 static void rs_8xx_unthrottle(struct tty_struct * tty)
1280 {
1281         ser_info_t *info = (ser_info_t *)tty->driver_data;
1282 #ifdef SERIAL_DEBUG_THROTTLE
1283         char    buf[64];
1284
1285         printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1286                tty->ldisc.chars_in_buffer(tty));
1287 #endif
1288
1289         if (serial_paranoia_check(info, tty->device, "rs_unthrottle"))
1290                 return;
1291
1292         if (I_IXOFF(tty)) {
1293                 if (info->x_char)
1294                         info->x_char = 0;
1295                 else
1296                         rs_8xx_send_xchar(tty, START_CHAR(tty));
1297         }
1298 #ifdef modem_control
1299         if (tty->termios->c_cflag & CRTSCTS)
1300                 info->MCR |= UART_MCR_RTS;
1301         cli();
1302         serial_out(info, UART_MCR, info->MCR);
1303         sti();
1304 #endif
1305 }
1306
1307 /*
1308  * ------------------------------------------------------------
1309  * rs_ioctl() and friends
1310  * ------------------------------------------------------------
1311  */
1312
1313 #ifdef maybe
1314 /*
1315  * get_lsr_info - get line status register info
1316  *
1317  * Purpose: Let user call ioctl() to get info when the UART physically
1318  *          is emptied.  On bus types like RS485, the transmitter must
1319  *          release the bus after transmitting. This must be done when
1320  *          the transmit shift register is empty, not be done when the
1321  *          transmit holding register is empty.  This functionality
1322  *          allows an RS485 driver to be written in user space.
1323  */
1324 static int get_lsr_info(struct async_struct * info, unsigned int *value)
1325 {
1326         unsigned char status;
1327         unsigned int result;
1328
1329         cli();
1330         status = serial_in(info, UART_LSR);
1331         sti();
1332         result = ((status & UART_LSR_TEMT) ? TIOCSER_TEMT : 0);
1333         return put_user(result,value);
1334 }
1335 #endif
1336
1337 static int get_modem_info(ser_info_t *info, unsigned int *value)
1338 {
1339         unsigned int result = 0;
1340 #ifdef modem_control
1341         unsigned char control, status;
1342
1343         control = info->MCR;
1344         cli();
1345         status = serial_in(info, UART_MSR);
1346         sti();
1347         result =  ((control & UART_MCR_RTS) ? TIOCM_RTS : 0)
1348                 | ((control & UART_MCR_DTR) ? TIOCM_DTR : 0)
1349 #ifdef TIOCM_OUT1
1350                 | ((control & UART_MCR_OUT1) ? TIOCM_OUT1 : 0)
1351                 | ((control & UART_MCR_OUT2) ? TIOCM_OUT2 : 0)
1352 #endif
1353                 | ((status  & UART_MSR_DCD) ? TIOCM_CAR : 0)
1354                 | ((status  & UART_MSR_RI) ? TIOCM_RNG : 0)
1355                 | ((status  & UART_MSR_DSR) ? TIOCM_DSR : 0)
1356                 | ((status  & UART_MSR_CTS) ? TIOCM_CTS : 0);
1357 #endif
1358         return put_user(result,value);
1359 }
1360
1361 static int set_modem_info(ser_info_t *info, unsigned int cmd,
1362                           unsigned int *value)
1363 {
1364         int error;
1365         unsigned int arg;
1366
1367         error = get_user(arg, value);
1368         if (error)
1369                 return error;
1370 #ifdef modem_control
1371         switch (cmd) {
1372         case TIOCMBIS:
1373                 if (arg & TIOCM_RTS)
1374                         info->MCR |= UART_MCR_RTS;
1375                 if (arg & TIOCM_DTR)
1376                         info->MCR |= UART_MCR_DTR;
1377 #ifdef TIOCM_OUT1
1378                 if (arg & TIOCM_OUT1)
1379                         info->MCR |= UART_MCR_OUT1;
1380                 if (arg & TIOCM_OUT2)
1381                         info->MCR |= UART_MCR_OUT2;
1382 #endif
1383                 break;
1384         case TIOCMBIC:
1385                 if (arg & TIOCM_RTS)
1386                         info->MCR &= ~UART_MCR_RTS;
1387                 if (arg & TIOCM_DTR)
1388                         info->MCR &= ~UART_MCR_DTR;
1389 #ifdef TIOCM_OUT1
1390                 if (arg & TIOCM_OUT1)
1391                         info->MCR &= ~UART_MCR_OUT1;
1392                 if (arg & TIOCM_OUT2)
1393                         info->MCR &= ~UART_MCR_OUT2;
1394 #endif
1395                 break;
1396         case TIOCMSET:
1397                 info->MCR = ((info->MCR & ~(UART_MCR_RTS |
1398 #ifdef TIOCM_OUT1
1399                                             UART_MCR_OUT1 |
1400                                             UART_MCR_OUT2 |
1401 #endif
1402                                             UART_MCR_DTR))
1403                              | ((arg & TIOCM_RTS) ? UART_MCR_RTS : 0)
1404 #ifdef TIOCM_OUT1
1405                              | ((arg & TIOCM_OUT1) ? UART_MCR_OUT1 : 0)
1406                              | ((arg & TIOCM_OUT2) ? UART_MCR_OUT2 : 0)
1407 #endif
1408                              | ((arg & TIOCM_DTR) ? UART_MCR_DTR : 0));
1409                 break;
1410         default:
1411                 return -EINVAL;
1412         }
1413         cli();
1414         serial_out(info, UART_MCR, info->MCR);
1415         sti();
1416 #endif
1417         return 0;
1418 }
1419
1420 /* Sending a break is a two step process on the SMC/SCC.  It is accomplished
1421  * by sending a STOP TRANSMIT command followed by a RESTART TRANSMIT
1422  * command.  We take advantage of the begin/end functions to make this
1423  * happen.
1424  */
1425 static ushort   smc_chan_map[] = {
1426         CPM_CR_CH_SMC1,
1427         CPM_CR_CH_SMC2
1428 };
1429
1430 static ushort   scc_chan_map[] = {
1431         CPM_CR_CH_SCC1,
1432         CPM_CR_CH_SCC2,
1433         CPM_CR_CH_SCC3,
1434         CPM_CR_CH_SCC4
1435 };
1436
1437 static void begin_break(ser_info_t *info)
1438 {
1439         volatile cpm8xx_t *cp;
1440         ushort  chan;
1441         int     idx;
1442
1443         cp = cpmp;
1444
1445         idx = PORT_NUM(info->state->smc_scc_num);
1446         if (PORT_IS_SCC(info->state->smc_scc_num))
1447                 chan = scc_chan_map[idx];
1448         else
1449                 chan = smc_chan_map[idx];
1450         cp->cp_cpcr = mk_cr_cmd(chan, CPM_CR_STOP_TX) | CPM_CR_FLG;
1451         while (cp->cp_cpcr & CPM_CR_FLG);
1452 }
1453
1454 static void end_break(ser_info_t *info)
1455 {
1456         volatile cpm8xx_t *cp;
1457         ushort  chan;
1458         int     idx;
1459
1460         cp = cpmp;
1461
1462         idx = PORT_NUM(info->state->smc_scc_num);
1463         if (PORT_IS_SCC(info->state->smc_scc_num))
1464                 chan = scc_chan_map[idx];
1465         else
1466                 chan = smc_chan_map[idx];
1467         cp->cp_cpcr = mk_cr_cmd(chan, CPM_CR_RESTART_TX) | CPM_CR_FLG;
1468         while (cp->cp_cpcr & CPM_CR_FLG);
1469 }
1470
1471 /*
1472  * This routine sends a break character out the serial port.
1473  */
1474 static void send_break(ser_info_t *info, int duration)
1475 {
1476         current->state = TASK_INTERRUPTIBLE;
1477 #ifdef SERIAL_DEBUG_SEND_BREAK
1478         printk("rs_send_break(%d) jiff=%lu...", duration, jiffies);
1479 #endif
1480         begin_break(info);
1481         schedule_timeout(duration);
1482         end_break(info);
1483 #ifdef SERIAL_DEBUG_SEND_BREAK
1484         printk("done jiffies=%lu\n", jiffies);
1485 #endif
1486 }
1487
1488
1489 static int rs_8xx_ioctl(struct tty_struct *tty, struct file * file,
1490                     unsigned int cmd, unsigned long arg)
1491 {
1492         int error;
1493         ser_info_t *info = (ser_info_t *)tty->driver_data;
1494         int retval;
1495         struct async_icount cnow;       /* kernel counter temps */
1496         struct serial_icounter_struct *p_cuser; /* user space */
1497
1498         if (serial_paranoia_check(info, tty->device, "rs_ioctl"))
1499                 return -ENODEV;
1500
1501         if ((cmd != TIOCMIWAIT) && (cmd != TIOCGICOUNT)) {
1502                 if (tty->flags & (1 << TTY_IO_ERROR))
1503                     return -EIO;
1504         }
1505
1506         switch (cmd) {
1507                 case TCSBRK:    /* SVID version: non-zero arg --> no break */
1508                         retval = tty_check_change(tty);
1509                         if (retval)
1510                                 return retval;
1511                         tty_wait_until_sent(tty, 0);
1512                         if (signal_pending(current))
1513                                 return -EINTR;
1514                         if (!arg) {
1515                                 send_break(info, HZ/4); /* 1/4 second */
1516                                 if (signal_pending(current))
1517                                         return -EINTR;
1518                         }
1519                         return 0;
1520                 case TCSBRKP:   /* support for POSIX tcsendbreak() */
1521                         retval = tty_check_change(tty);
1522                         if (retval)
1523                                 return retval;
1524                         tty_wait_until_sent(tty, 0);
1525                         if (signal_pending(current))
1526                                 return -EINTR;
1527                         send_break(info, arg ? arg*(HZ/10) : HZ/4);
1528                         if (signal_pending(current))
1529                                 return -EINTR;
1530                         return 0;
1531                 case TIOCSBRK:
1532                         retval = tty_check_change(tty);
1533                         if (retval)
1534                                 return retval;
1535                         tty_wait_until_sent(tty, 0);
1536                         begin_break(info);
1537                         return 0;
1538                 case TIOCCBRK:
1539                         retval = tty_check_change(tty);
1540                         if (retval)
1541                                 return retval;
1542                         end_break(info);
1543                         return 0;
1544                 case TIOCGSOFTCAR:
1545                         return put_user(C_CLOCAL(tty) ? 1 : 0, (int *) arg);
1546                 case TIOCSSOFTCAR:
1547                         error = get_user(arg, (unsigned int *) arg);
1548                         if (error)
1549                                 return error;
1550                         tty->termios->c_cflag =
1551                                 ((tty->termios->c_cflag & ~CLOCAL) |
1552                                  (arg ? CLOCAL : 0));
1553                         return 0;
1554                 case TIOCMGET:
1555                         return get_modem_info(info, (unsigned int *) arg);
1556                 case TIOCMBIS:
1557                 case TIOCMBIC:
1558                 case TIOCMSET:
1559                         return set_modem_info(info, cmd, (unsigned int *) arg);
1560 #ifdef maybe
1561                 case TIOCSERGETLSR: /* Get line status register */
1562                         return get_lsr_info(info, (unsigned int *) arg);
1563 #endif
1564                 /*
1565                  * Wait for any of the 4 modem inputs (DCD,RI,DSR,CTS) to change
1566                  * - mask passed in arg for lines of interest
1567                  *   (use |'ed TIOCM_RNG/DSR/CD/CTS for masking)
1568                  * Caller should use TIOCGICOUNT to see which one it was
1569                  */
1570                  case TIOCMIWAIT:
1571 #ifdef modem_control
1572                         cli();
1573                         /* note the counters on entry */
1574                         cprev = info->state->icount;
1575                         sti();
1576                         while (1) {
1577                                 interruptible_sleep_on(&info->delta_msr_wait);
1578                                 /* see if a signal did it */
1579                                 if (signal_pending(current))
1580                                         return -ERESTARTSYS;
1581                                 cli();
1582                                 cnow = info->state->icount; /* atomic copy */
1583                                 sti();
1584                                 if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
1585                                     cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
1586                                         return -EIO; /* no change => error */
1587                                 if ( ((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
1588                                      ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
1589                                      ((arg & TIOCM_CD)  && (cnow.dcd != cprev.dcd)) ||
1590                                      ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts)) ) {
1591                                         return 0;
1592                                 }
1593                                 cprev = cnow;
1594                         }
1595                         /* NOTREACHED */
1596 #else
1597                         return 0;
1598 #endif
1599
1600                 /*
1601                  * Get counter of input serial line interrupts (DCD,RI,DSR,CTS)
1602                  * Return: write counters to the user passed counter struct
1603                  * NB: both 1->0 and 0->1 transitions are counted except for
1604                  *     RI where only 0->1 is counted.
1605                  */
1606                 case TIOCGICOUNT:
1607                         cli();
1608                         cnow = info->state->icount;
1609                         sti();
1610                         p_cuser = (struct serial_icounter_struct *) arg;
1611                         error = put_user(cnow.cts, &p_cuser->cts);
1612                         if (error) return error;
1613                         error = put_user(cnow.dsr, &p_cuser->dsr);
1614                         if (error) return error;
1615                         error = put_user(cnow.rng, &p_cuser->rng);
1616                         if (error) return error;
1617                         error = put_user(cnow.dcd, &p_cuser->dcd);
1618                         if (error) return error;
1619                         return 0;
1620
1621                 default:
1622                         return -ENOIOCTLCMD;
1623                 }
1624         return 0;
1625 }
1626
1627 /* FIX UP modem control here someday......
1628 */
1629 static void rs_8xx_set_termios(struct tty_struct *tty, struct termios *old_termios)
1630 {
1631         ser_info_t *info = (ser_info_t *)tty->driver_data;
1632
1633         if (   (tty->termios->c_cflag == old_termios->c_cflag)
1634             && (   RELEVANT_IFLAG(tty->termios->c_iflag)
1635                 == RELEVANT_IFLAG(old_termios->c_iflag)))
1636           return;
1637
1638         change_speed(info);
1639
1640 #ifdef modem_control
1641         /* Handle transition to B0 status */
1642         if ((old_termios->c_cflag & CBAUD) &&
1643             !(tty->termios->c_cflag & CBAUD)) {
1644                 info->MCR &= ~(UART_MCR_DTR|UART_MCR_RTS);
1645                 cli();
1646                 serial_out(info, UART_MCR, info->MCR);
1647                 sti();
1648         }
1649
1650         /* Handle transition away from B0 status */
1651         if (!(old_termios->c_cflag & CBAUD) &&
1652             (tty->termios->c_cflag & CBAUD)) {
1653                 info->MCR |= UART_MCR_DTR;
1654                 if (!tty->hw_stopped ||
1655                     !(tty->termios->c_cflag & CRTSCTS)) {
1656                         info->MCR |= UART_MCR_RTS;
1657                 }
1658                 cli();
1659                 serial_out(info, UART_MCR, info->MCR);
1660                 sti();
1661         }
1662
1663         /* Handle turning off CRTSCTS */
1664         if ((old_termios->c_cflag & CRTSCTS) &&
1665             !(tty->termios->c_cflag & CRTSCTS)) {
1666                 tty->hw_stopped = 0;
1667                 rs_8xx_start(tty);
1668         }
1669 #endif
1670
1671 #if 0
1672         /*
1673          * No need to wake up processes in open wait, since they
1674          * sample the CLOCAL flag once, and don't recheck it.
1675          * XXX  It's not clear whether the current behavior is correct
1676          * or not.  Hence, this may change.....
1677          */
1678         if (!(old_termios->c_cflag & CLOCAL) &&
1679             (tty->termios->c_cflag & CLOCAL))
1680                 wake_up_interruptible(&info->open_wait);
1681 #endif
1682 }
1683
1684 /*
1685  * ------------------------------------------------------------
1686  * rs_close()
1687  *
1688  * This routine is called when the serial port gets closed.  First, we
1689  * wait for the last remaining data to be sent.  Then, we unlink its
1690  * async structure from the interrupt chain if necessary, and we free
1691  * that IRQ if nothing is left in the chain.
1692  * ------------------------------------------------------------
1693  */
1694 static void rs_8xx_close(struct tty_struct *tty, struct file * filp)
1695 {
1696         ser_info_t *info = (ser_info_t *)tty->driver_data;
1697         struct serial_state *state;
1698         unsigned long   flags;
1699         int             idx;
1700         volatile smc_t  *smcp;
1701         volatile scc_t  *sccp;
1702
1703         if (!info || serial_paranoia_check(info, tty->device, "rs_close"))
1704                 return;
1705
1706         state = info->state;
1707
1708         save_flags(flags); cli();
1709
1710         if (tty_hung_up_p(filp)) {
1711                 DBG_CNT("before DEC-hung");
1712                 MOD_DEC_USE_COUNT;
1713                 restore_flags(flags);
1714                 return;
1715         }
1716
1717 #ifdef SERIAL_DEBUG_OPEN
1718         printk("rs_close ttys%d, count = %d\n", info->line, state->count);
1719 #endif
1720         if ((tty->count == 1) && (state->count != 1)) {
1721                 /*
1722                  * Uh, oh.  tty->count is 1, which means that the tty
1723                  * structure will be freed.  state->count should always
1724                  * be one in these conditions.  If it's greater than
1725                  * one, we've got real problems, since it means the
1726                  * serial port won't be shutdown.
1727                  */
1728                 printk("rs_close: bad serial port count; tty->count is 1, "
1729                        "state->count is %d\n", state->count);
1730                 state->count = 1;
1731         }
1732         if (--state->count < 0) {
1733                 printk("rs_close: bad serial port count for ttys%d: %d\n",
1734                        info->line, state->count);
1735                 state->count = 0;
1736         }
1737         if (state->count) {
1738                 DBG_CNT("before DEC-2");
1739                 MOD_DEC_USE_COUNT;
1740                 restore_flags(flags);
1741                 return;
1742         }
1743         info->flags |= ASYNC_CLOSING;
1744         /*
1745          * Save the termios structure, since this port may have
1746          * separate termios for callout and dialin.
1747          */
1748         if (info->flags & ASYNC_NORMAL_ACTIVE)
1749                 info->state->normal_termios = *tty->termios;
1750         if (info->flags & ASYNC_CALLOUT_ACTIVE)
1751                 info->state->callout_termios = *tty->termios;
1752         /*
1753          * Now we wait for the transmit buffer to clear; and we notify
1754          * the line discipline to only process XON/XOFF characters.
1755          */
1756         tty->closing = 1;
1757         if (state->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1758                 tty_wait_until_sent(tty, state->closing_wait);
1759         /*
1760          * At this point we stop accepting input.  To do this, we
1761          * disable the receive line status interrupts, and tell the
1762          * interrupt driver to stop checking the data ready bit in the
1763          * line status register.
1764          */
1765         info->read_status_mask &= ~BD_SC_EMPTY;
1766         if (info->flags & ASYNC_INITIALIZED) {
1767                 idx = PORT_NUM(info->state->smc_scc_num);
1768                 if (PORT_IS_SCC(info->state->smc_scc_num)) {
1769                         sccp = &cpmp->cp_scc[idx];
1770                         sccp->scc_sccm &= ~UART_SCCM_RX;
1771                         sccp->scc_gsmrl &= ~SCC_GSMRL_ENR;
1772                 }
1773                 else {
1774                         smcp = &cpmp->cp_smc[idx];
1775                         smcp->smc_smcm &= ~SMCM_RX;
1776                         smcp->smc_smcmr &= ~SMCMR_REN;
1777                 }
1778                 /*
1779                  * Before we drop DTR, make sure the UART transmitter
1780                  * has completely drained; this is especially
1781                  * important if there is a transmit FIFO!
1782                  */
1783                 rs_8xx_wait_until_sent(tty, info->timeout);
1784         }
1785         shutdown(info);
1786         if (tty->driver.flush_buffer)
1787                 tty->driver.flush_buffer(tty);
1788         tty_ldisc_flush(tty);
1789         tty->closing = 0;
1790         info->event = 0;
1791         info->tty = 0;
1792         if (info->blocked_open) {
1793                 if (state->close_delay) {
1794                         current->state = TASK_INTERRUPTIBLE;
1795                         schedule_timeout(state->close_delay);
1796                 }
1797                 wake_up_interruptible(&info->open_wait);
1798         }
1799         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE|
1800                          ASYNC_CLOSING);
1801         wake_up_interruptible(&info->close_wait);
1802         MOD_DEC_USE_COUNT;
1803         restore_flags(flags);
1804 }
1805
1806 /*
1807  * rs_wait_until_sent() --- wait until the transmitter is empty
1808  */
1809 static void rs_8xx_wait_until_sent(struct tty_struct *tty, int timeout)
1810 {
1811         ser_info_t *info = (ser_info_t *)tty->driver_data;
1812         unsigned long orig_jiffies, char_time, tst_res;
1813         /*int lsr;*/
1814         volatile cbd_t *bdp;
1815
1816         if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent"))
1817                 return;
1818
1819 #ifdef maybe
1820         if (info->state->type == PORT_UNKNOWN)
1821                 return;
1822 #endif
1823
1824         orig_jiffies = jiffies;
1825         /*
1826          * Set the check interval to be 1/5 of the estimated time to
1827          * send a single character, and make it at least 1.  The check
1828          * interval should also be less than the timeout.
1829          *
1830          * Note: we have to use pretty tight timings here to satisfy
1831          * the NIST-PCTS.
1832          */
1833         char_time = 1;
1834         if (timeout)
1835                 char_time = MIN(char_time, timeout);
1836 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1837         printk("In rs_wait_until_sent(%d) check=%lu...", timeout, char_time);
1838         printk("jiff=%lu...", jiffies);
1839 #endif
1840
1841         /* We go through the loop at least once because we can't tell
1842          * exactly when the last character exits the shifter.  There can
1843          * be at least two characters waiting to be sent after the buffers
1844          * are empty.
1845          */
1846         do {
1847                 unsigned long flags;
1848 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1849                 printk("lsr = %d (jiff=%lu)...", lsr, jiffies);
1850 #endif
1851                 current->state = TASK_INTERRUPTIBLE;
1852 /*              current->counter = 0;    make us low-priority */
1853                 schedule_timeout(char_time);
1854                 if (signal_pending(current))
1855                         break;
1856                 if (timeout && time_after(jiffies, orig_jiffies + timeout))
1857                         break;
1858
1859                 /* The 'tx_cur' is really the next buffer to send.  We
1860                  * have to back up to the previous BD and wait for it
1861                  * to go.  This isn't perfect, because all this indicates
1862                  * is the buffer is available.  There are still characters
1863                  * in the CPM FIFO.
1864                  */
1865                 local_irq_save(flags);
1866                 bdp = info->tx_cur;
1867                 if (bdp == info->tx_bd_base)
1868                         bdp += (TX_NUM_FIFO-1);
1869                 else
1870                         bdp--;
1871                 tst_res = !!(bdp->cbd_sc & BD_SC_READY);
1872                 local_irq_restore(flags);
1873         } while (tst_res);
1874         current->state = TASK_RUNNING;
1875 #ifdef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT
1876         printk("lsr = %d (jiff=%lu)...done\n", lsr, jiffies);
1877 #endif
1878 }
1879
1880 /*
1881  * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1882  */
1883 static void rs_8xx_hangup(struct tty_struct *tty)
1884 {
1885         ser_info_t *info = (ser_info_t *)tty->driver_data;
1886         struct serial_state *state = info->state;
1887
1888         if (serial_paranoia_check(info, tty->device, "rs_hangup"))
1889                 return;
1890
1891         state = info->state;
1892
1893         rs_8xx_flush_buffer(tty);
1894         shutdown(info);
1895         info->event = 0;
1896         state->count = 0;
1897         info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CALLOUT_ACTIVE);
1898         info->tty = 0;
1899         wake_up_interruptible(&info->open_wait);
1900 }
1901
1902 /*
1903  * ------------------------------------------------------------
1904  * rs_open() and friends
1905  * ------------------------------------------------------------
1906  */
1907 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1908                            ser_info_t *info)
1909 {
1910 #ifdef DO_THIS_LATER
1911         DECLARE_WAITQUEUE(wait, current);
1912 #endif
1913         struct serial_state *state = info->state;
1914         int             retval;
1915         int             do_clocal = 0;
1916
1917         /*
1918          * If the device is in the middle of being closed, then block
1919          * until it's done, and then try again.
1920          */
1921         if (tty_hung_up_p(filp) ||
1922             (info->flags & ASYNC_CLOSING)) {
1923                 if (info->flags & ASYNC_CLOSING)
1924                         interruptible_sleep_on(&info->close_wait);
1925 #ifdef SERIAL_DO_RESTART
1926                 if (info->flags & ASYNC_HUP_NOTIFY)
1927                         return -EAGAIN;
1928                 else
1929                         return -ERESTARTSYS;
1930 #else
1931                 return -EAGAIN;
1932 #endif
1933         }
1934
1935         /*
1936          * If this is a callout device, then just make sure the normal
1937          * device isn't being used.
1938          */
1939         if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) {
1940                 if (info->flags & ASYNC_NORMAL_ACTIVE)
1941                         return -EBUSY;
1942                 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1943                     (info->flags & ASYNC_SESSION_LOCKOUT) &&
1944                     (info->session != current->session))
1945                     return -EBUSY;
1946                 if ((info->flags & ASYNC_CALLOUT_ACTIVE) &&
1947                     (info->flags & ASYNC_PGRP_LOCKOUT) &&
1948                     (info->pgrp != current->pgrp))
1949                     return -EBUSY;
1950                 info->flags |= ASYNC_CALLOUT_ACTIVE;
1951                 return 0;
1952         }
1953
1954         /*
1955          * If non-blocking mode is set, or the port is not enabled,
1956          * then make the check up front and then exit.
1957          * If this is an SMC port, we don't have modem control to wait
1958          * for, so just get out here.
1959          */
1960         if ((filp->f_flags & O_NONBLOCK) ||
1961             (tty->flags & (1 << TTY_IO_ERROR)) ||
1962             !(PORT_IS_SCC(info->state->smc_scc_num))) {
1963                 if (info->flags & ASYNC_CALLOUT_ACTIVE)
1964                         return -EBUSY;
1965                 info->flags |= ASYNC_NORMAL_ACTIVE;
1966                 return 0;
1967         }
1968
1969         if (info->flags & ASYNC_CALLOUT_ACTIVE) {
1970                 if (state->normal_termios.c_cflag & CLOCAL)
1971                         do_clocal = 1;
1972         } else {
1973                 if (tty->termios->c_cflag & CLOCAL)
1974                         do_clocal = 1;
1975         }
1976
1977         /*
1978          * Block waiting for the carrier detect and the line to become
1979          * free (i.e., not in use by the callout).  While we are in
1980          * this loop, state->count is dropped by one, so that
1981          * rs_close() knows when to free things.  We restore it upon
1982          * exit, either normal or abnormal.
1983          */
1984         retval = 0;
1985 #ifdef DO_THIS_LATER
1986         add_wait_queue(&info->open_wait, &wait);
1987 #ifdef SERIAL_DEBUG_OPEN
1988         printk("block_til_ready before block: ttys%d, count = %d\n",
1989                state->line, state->count);
1990 #endif
1991         cli();
1992         if (!tty_hung_up_p(filp))
1993                 state->count--;
1994         sti();
1995         info->blocked_open++;
1996         while (1) {
1997                 cli();
1998                 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
1999                     (tty->termios->c_cflag & CBAUD))
2000                         serial_out(info, UART_MCR,
2001                                    serial_inp(info, UART_MCR) |
2002                                    (UART_MCR_DTR | UART_MCR_RTS));
2003                 sti();
2004                 set_current_state(TASK_INTERRUPTIBLE);
2005                 if (tty_hung_up_p(filp) ||
2006                     !(info->flags & ASYNC_INITIALIZED)) {
2007 #ifdef SERIAL_DO_RESTART
2008                         if (info->flags & ASYNC_HUP_NOTIFY)
2009                                 retval = -EAGAIN;
2010                         else
2011                                 retval = -ERESTARTSYS;
2012 #else
2013                         retval = -EAGAIN;
2014 #endif
2015                         break;
2016                 }
2017                 if (!(info->flags & ASYNC_CALLOUT_ACTIVE) &&
2018                     !(info->flags & ASYNC_CLOSING) &&
2019                     (do_clocal || (serial_in(info, UART_MSR) &
2020                                    UART_MSR_DCD)))
2021                         break;
2022                 if (signal_pending(current)) {
2023                         retval = -ERESTARTSYS;
2024                         break;
2025                 }
2026 #ifdef SERIAL_DEBUG_OPEN
2027                 printk("block_til_ready blocking: ttys%d, count = %d\n",
2028                        info->line, state->count);
2029 #endif
2030                 schedule();
2031         }
2032         current->state = TASK_RUNNING;
2033         remove_wait_queue(&info->open_wait, &wait);
2034         if (!tty_hung_up_p(filp))
2035                 state->count++;
2036         info->blocked_open--;
2037 #ifdef SERIAL_DEBUG_OPEN
2038         printk("block_til_ready after blocking: ttys%d, count = %d\n",
2039                info->line, state->count);
2040 #endif
2041 #endif /* DO_THIS_LATER */
2042         if (retval)
2043                 return retval;
2044         info->flags |= ASYNC_NORMAL_ACTIVE;
2045         return 0;
2046 }
2047
2048 static int get_async_struct(int line, ser_info_t **ret_info)
2049 {
2050         struct serial_state *sstate;
2051
2052         sstate = rs_table + line;
2053         if (sstate->info) {
2054                 sstate->count++;
2055                 *ret_info = (ser_info_t *)sstate->info;
2056                 return 0;
2057         }
2058         else {
2059                 return -ENOMEM;
2060         }
2061 }
2062
2063 /*
2064  * This routine is called whenever a serial port is opened.  It
2065  * enables interrupts for a serial port, linking in its async structure into
2066  * the IRQ chain.   It also performs the serial-specific
2067  * initialization for the tty structure.
2068  */
2069 static int rs_8xx_open(struct tty_struct *tty, struct file * filp)
2070 {
2071         ser_info_t      *info;
2072         int             retval, line;
2073
2074         line = MINOR(tty->device) - tty->driver.minor_start;
2075         if ((line < 0) || (line >= NR_PORTS))
2076                 return -ENODEV;
2077         retval = get_async_struct(line, &info);
2078         if (retval)
2079                 return retval;
2080         if (serial_paranoia_check(info, tty->device, "rs_open"))
2081                 return -ENODEV;
2082
2083 #ifdef SERIAL_DEBUG_OPEN
2084         printk("rs_open %s%d, count = %d\n", tty->driver.name, info->line,
2085                info->state->count);
2086 #endif
2087         tty->driver_data = info;
2088         info->tty = tty;
2089
2090         /*
2091          * Start up serial port
2092          */
2093         retval = startup(info);
2094         if (retval)
2095                 return retval;
2096
2097         MOD_INC_USE_COUNT;
2098         retval = block_til_ready(tty, filp, info);
2099         if (retval) {
2100 #ifdef SERIAL_DEBUG_OPEN
2101                 printk("rs_open returning after block_til_ready with %d\n",
2102                        retval);
2103 #endif
2104                 MOD_DEC_USE_COUNT;
2105                 return retval;
2106         }
2107
2108         if ((info->state->count == 1) &&
2109             (info->flags & ASYNC_SPLIT_TERMIOS)) {
2110                 if (tty->driver.subtype == SERIAL_TYPE_NORMAL)
2111                         *tty->termios = info->state->normal_termios;
2112                 else
2113                         *tty->termios = info->state->callout_termios;
2114                 change_speed(info);
2115         }
2116
2117         info->session = current->session;
2118         info->pgrp = current->pgrp;
2119
2120 #ifdef SERIAL_DEBUG_OPEN
2121         printk("rs_open ttys%d successful...", info->line);
2122 #endif
2123         return 0;
2124 }
2125
2126 /*
2127  * /proc fs routines....
2128  */
2129
2130 static int inline line_info(char *buf, struct serial_state *state)
2131 {
2132 #ifdef notdef
2133         struct async_struct *info = state->info, scr_info;
2134         char    stat_buf[30], control, status;
2135 #endif
2136         int     ret;
2137
2138         ret = sprintf(buf, "%d: uart:%s port:%X irq:%d",
2139                       state->line,
2140                       (PORT_IS_SCC(state->smc_scc_num)) ? "SCC" : "SMC",
2141                       (unsigned int)(state->port), state->irq);
2142
2143         if (!state->port || (state->type == PORT_UNKNOWN)) {
2144                 ret += sprintf(buf+ret, "\n");
2145                 return ret;
2146         }
2147
2148 #ifdef notdef
2149         /*
2150          * Figure out the current RS-232 lines
2151          */
2152         if (!info) {
2153                 info = &scr_info;       /* This is just for serial_{in,out} */
2154
2155                 info->magic = SERIAL_MAGIC;
2156                 info->port = state->port;
2157                 info->flags = state->flags;
2158                 info->quot = 0;
2159                 info->tty = 0;
2160         }
2161         cli();
2162         status = serial_in(info, UART_MSR);
2163         control = info ? info->MCR : serial_in(info, UART_MCR);
2164         sti();
2165
2166         stat_buf[0] = 0;
2167         stat_buf[1] = 0;
2168         if (control & UART_MCR_RTS)
2169                 strcat(stat_buf, "|RTS");
2170         if (status & UART_MSR_CTS)
2171                 strcat(stat_buf, "|CTS");
2172         if (control & UART_MCR_DTR)
2173                 strcat(stat_buf, "|DTR");
2174         if (status & UART_MSR_DSR)
2175                 strcat(stat_buf, "|DSR");
2176         if (status & UART_MSR_DCD)
2177                 strcat(stat_buf, "|CD");
2178         if (status & UART_MSR_RI)
2179                 strcat(stat_buf, "|RI");
2180
2181         if (info->quot) {
2182                 ret += sprintf(buf+ret, " baud:%d",
2183                                state->baud_base / info->quot);
2184         }
2185
2186         ret += sprintf(buf+ret, " tx:%d rx:%d",
2187                       state->icount.tx, state->icount.rx);
2188
2189         if (state->icount.frame)
2190                 ret += sprintf(buf+ret, " fe:%d", state->icount.frame);
2191
2192         if (state->icount.parity)
2193                 ret += sprintf(buf+ret, " pe:%d", state->icount.parity);
2194
2195         if (state->icount.brk)
2196                 ret += sprintf(buf+ret, " brk:%d", state->icount.brk);
2197
2198         if (state->icount.overrun)
2199                 ret += sprintf(buf+ret, " oe:%d", state->icount.overrun);
2200
2201         /*
2202          * Last thing is the RS-232 status lines
2203          */
2204         ret += sprintf(buf+ret, " %s\n", stat_buf+1);
2205 #endif
2206         return ret;
2207 }
2208
2209 int rs_8xx_read_proc(char *page, char **start, off_t off, int count,
2210                  int *eof, void *data)
2211 {
2212         int i, len = 0;
2213         off_t   begin = 0;
2214
2215         len += sprintf(page, "serinfo:1.0 driver:%s\n", serial_version);
2216         for (i = 0; i < NR_PORTS && len < 4000; i++) {
2217                 len += line_info(page + len, &rs_table[i]);
2218                 if (len+begin > off+count)
2219                         goto done;
2220                 if (len+begin < off) {
2221                         begin += len;
2222                         len = 0;
2223                 }
2224         }
2225         *eof = 1;
2226 done:
2227         if (off >= len+begin)
2228                 return 0;
2229         *start = page + (begin-off);
2230         return ((count < begin+len-off) ? count : begin+len-off);
2231 }
2232
2233 /*
2234  * ---------------------------------------------------------------------
2235  * rs_init() and friends
2236  *
2237  * rs_init() is called at boot-time to initialize the serial driver.
2238  * ---------------------------------------------------------------------
2239  */
2240
2241 /*
2242  * This routine prints out the appropriate serial driver version
2243  * number, and identifies which options were configured into this
2244  * driver.
2245  */
2246 static _INLINE_ void show_serial_version(void)
2247 {
2248         printk(KERN_INFO "%s version %s\n", serial_name, serial_version);
2249 }
2250
2251
2252 /*
2253  * The serial console driver used during boot.  Note that these names
2254  * clash with those found in "serial.c", so we currently can't support
2255  * the 16xxx uarts and these at the same time.  I will fix this to become
2256  * an indirect function call from tty_io.c (or something).
2257  */
2258
2259 #ifdef CONFIG_SERIAL_CONSOLE
2260
2261 /* I need this just so I can store the virtual addresses and have
2262  * common functions for the early console printing.
2263  */
2264 static ser_info_t consinfo;
2265
2266 /*
2267  * Print a string to the serial port trying not to disturb any possible
2268  * real use of the port...
2269  */
2270 static void my_console_write(int idx, const char *s,
2271                                 unsigned count)
2272 {
2273         struct          serial_state    *ser;
2274         ser_info_t                      *info;
2275         unsigned                        i, c, cr_missing, max_tx_size;
2276         volatile        cbd_t           *bdp, *bdbase;
2277         volatile        smc_uart_t      *up;
2278         volatile        u_char          *cp;
2279         unsigned long                   flags;
2280
2281         ser = rs_table + idx;
2282
2283         /* If the port has been initialized for general use, we have
2284          * to use the buffer descriptors allocated there.  Otherwise,
2285          * we simply use the single buffer allocated.
2286          */
2287         if ((info = (ser_info_t *)ser->info) != NULL) {
2288                 bdbase = info->tx_bd_base;
2289         } else {
2290                 /* Pointer to UART in parameter ram. */
2291                 up = (smc_uart_t *)&cpmp->cp_dparam[ser->port];
2292
2293                 /* Get the address of the host memory buffer.*/
2294                 info = &consinfo;
2295                 info->tx_bd_base = (cbd_t *)bdbase = (cbd_t *)&cpmp->cp_dpmem[up->smc_tbase];
2296                 info->tx_cur = (cbd_t *)bdbase;
2297         }
2298         max_tx_size = console_tx_buf_len;
2299         cr_missing = 0;
2300         while (1){
2301                 c = MIN(max_tx_size, count);
2302                 if (c <= 0)
2303                         break;
2304
2305                 local_irq_save(flags);
2306                 bdp = info->tx_cur;
2307                 bdbase = info->tx_bd_base;
2308                 if (bdp->cbd_sc & BD_SC_WRAP)
2309                         info->tx_cur = (cbd_t *)bdbase;
2310                 else
2311                         info->tx_cur = (cbd_t *)(bdp+1);
2312                 local_irq_restore(flags);
2313
2314                 /* Wait for transmitter fifo to empty.
2315                  * Ready indicates output is ready, and xmt is doing
2316                  * that, not that it is ready for us to send.
2317                  */
2318                 while (bdp->cbd_sc & BD_SC_READY);
2319
2320                 /* Send the characters out.
2321                  * If the buffer address is in the CPM DPRAM, don't
2322                  * convert it.
2323                  */
2324                 if ((uint)(bdp->cbd_bufaddr) > (uint)IMAP_ADDR)
2325                         cp = (u_char *)(bdp->cbd_bufaddr);
2326                 else
2327                         cp = info->tx_va_base + ((bdp - info->tx_bd_base) * TX_BUF_SIZE);
2328
2329                 i=1; /* Keeps track of consumed TX buffer space */
2330                 if (cr_missing) {
2331                         /* Previus loop didn't have room for the CR, insert it first in this */
2332                         *cp++ = '\r';
2333                         i++;
2334                 }
2335                 cr_missing = 0;
2336                 for (; i <= c; i++) {
2337                         if ((*cp++ = *s++) != '\n')
2338                                 continue; /* Copy bytes until a NewLine is found */
2339                         /* NewLine found, see if there is space in the TX buffer to add a CR */
2340                         if (i < max_tx_size) {
2341                                 *cp++ = '\r'; /* yes, there is space to add a CR */
2342                                 i++;
2343                         } else
2344                                 cr_missing = 1; /* No space in the TX buffer,
2345                                                    rember it so it can be inserted in the next loop */
2346                 }
2347                 count -= (c-cr_missing);
2348                 bdp->cbd_datlen = i-1;
2349                 bdp->cbd_sc |= BD_SC_READY;
2350
2351         }
2352         /* while (bdp->cbd_sc & BD_SC_READY); is this really needed? */
2353 }
2354 static void serial_console_write(struct console *c, const char *s,
2355                                 unsigned count)
2356 {
2357 #ifdef CONFIG_KGDB_CONSOLE
2358         /* Try to let stub handle output. Returns true if it did. */
2359         if (kgdb_output_string(s, count))
2360                 return;
2361 #endif
2362         my_console_write(c->index, s, count);
2363 }
2364
2365 #ifdef CONFIG_XMON
2366 int
2367 xmon_8xx_write(const char *s, unsigned count)
2368 {
2369         my_console_write(0, s, count);
2370         return(count);
2371 }
2372 #endif
2373
2374 #if defined(CONFIG_KGDB) || defined(CONFIG_XMON)
2375 /*
2376  * Receive character from the serial port.  This only works well
2377  * before the port is initialized for real use.
2378  */
2379 static int my_console_wait_key(int idx, int xmon, char *obuf)
2380 {
2381         struct serial_state             *ser;
2382         u_char                          c, *cp;
2383         ser_info_t                      *info;
2384         volatile        cbd_t           *bdp;
2385         volatile        smc_uart_t      *up;
2386         int                             i;
2387
2388         ser = rs_table + idx;
2389
2390         /* Pointer to UART in parameter ram.
2391         */
2392         up = (smc_uart_t *)&cpmp->cp_dparam[ser->port];
2393
2394         /* Get the address of the host memory buffer.
2395          * If the port has been initialized for general use, we must
2396          * use information from the port structure.
2397          */
2398         if ((info = (ser_info_t *)ser->info)) {
2399                 bdp = info->rx_cur;
2400         }
2401         else {
2402                 bdp = (cbd_t *)&cpmp->cp_dpmem[up->smc_rbase];
2403                 info = &consinfo;
2404         }
2405
2406         /*
2407          * We need to gracefully shut down the receiver, disable
2408          * interrupts, then read the input.
2409          * XMON just wants a poll.  If no character, return -1, else
2410          * return the character.
2411          */
2412         if (!xmon) {
2413                 while (bdp->cbd_sc & BD_SC_EMPTY);
2414         }
2415         else {
2416                 if (bdp->cbd_sc & BD_SC_EMPTY)
2417                         return -1;
2418         }
2419
2420         /* If the buffer address is in the CPM DPRAM, don't
2421          * convert it.
2422          */
2423         if ((uint)(bdp->cbd_bufaddr) > (uint)IMAP_ADDR)
2424                 cp = (u_char *)(bdp->cbd_bufaddr);
2425         else
2426                 cp = info->rx_va_base + ((bdp - info->rx_bd_base) * RX_BUF_SIZE);
2427
2428         if (obuf) {
2429                 i = c = bdp->cbd_datlen;
2430                 while (i-- > 0)
2431                         *obuf++ = *cp++;
2432         }
2433         else {
2434                 c = *cp;
2435         }
2436         bdp->cbd_sc |= BD_SC_EMPTY;
2437
2438         if (info) {
2439                 if (bdp->cbd_sc & BD_SC_WRAP) {
2440                         bdp = info->rx_bd_base;
2441                 }
2442                 else {
2443                         bdp++;
2444                 }
2445                 info->rx_cur = (cbd_t *)bdp;
2446         }
2447
2448         return((int)c);
2449 }
2450 #endif /* CONFIG_KGDB || CONFIG_XMON */
2451
2452 #ifdef CONFIG_XMON
2453 int
2454 xmon_8xx_read_poll(void)
2455 {
2456         return(my_console_wait_key(0, 1, NULL));
2457 }
2458
2459 int
2460 xmon_8xx_read_char(void)
2461 {
2462         return(my_console_wait_key(0, 0, NULL));
2463 }
2464 #endif
2465
2466 #ifdef CONFIG_KGDB
2467 static char kgdb_buf[RX_BUF_SIZE], *kgdp;
2468 static int kgdb_chars;
2469
2470 void
2471 putDebugChar(char ch)
2472 {
2473         my_console_write(0, &ch, 1);
2474 }
2475
2476 char
2477 getDebugChar(void)
2478 {
2479         if (kgdb_chars <= 0) {
2480                 kgdb_chars = my_console_wait_key(0, 0, kgdb_buf);
2481                 kgdp = kgdb_buf;
2482         }
2483         kgdb_chars--;
2484
2485         return(*kgdp++);
2486 }
2487
2488 void kgdb_interruptible(int yes)
2489 {
2490         volatile smc_t  *smcp;
2491
2492         smcp = &cpmp->cp_smc[KGDB_SER_IDX];
2493
2494         if (yes == 1)
2495                 smcp->smc_smcm |= SMCM_RX;
2496         else
2497                 smcp->smc_smcm &= ~SMCM_RX;
2498 }
2499
2500 void kgdb_map_scc(void)
2501 {
2502         struct          serial_state *ser;
2503         uint            mem_addr;
2504         volatile        cbd_t           *bdp;
2505         volatile        smc_uart_t      *up;
2506
2507         cpmp = (cpm8xx_t *)&(((immap_t *)IMAP_ADDR)->im_cpm);
2508
2509         /* To avoid data cache CPM DMA coherency problems, allocate a
2510          * buffer in the CPM DPRAM.  This will work until the CPM and
2511          * serial ports are initialized.  At that time a memory buffer
2512          * will be allocated.
2513          * The port is already initialized from the boot procedure, all
2514          * we do here is give it a different buffer and make it a FIFO.
2515          */
2516
2517         ser = rs_table;
2518
2519         /* Right now, assume we are using SMCs.
2520         */
2521         up = (smc_uart_t *)&cpmp->cp_dparam[ser->port];
2522
2523         /* Allocate space for an input FIFO, plus a few bytes for output.
2524          * Allocate bytes to maintain word alignment.
2525          */
2526         mem_addr = (uint)(&cpmp->cp_dpmem[0xa00]);
2527
2528         /* Set the physical address of the host memory buffers in
2529          * the buffer descriptors.
2530          */
2531         bdp = (cbd_t *)&cpmp->cp_dpmem[up->smc_rbase];
2532         bdp->cbd_bufaddr = mem_addr;
2533
2534         bdp = (cbd_t *)&cpmp->cp_dpmem[up->smc_tbase];
2535         bdp->cbd_bufaddr = mem_addr+RX_BUF_SIZE;
2536
2537         up->smc_mrblr = RX_BUF_SIZE;            /* receive buffer length */
2538         up->smc_maxidl = RX_BUF_SIZE;
2539 }
2540 #endif
2541
2542 static kdev_t serial_console_device(struct console *c)
2543 {
2544         return MKDEV(TTY_MAJOR, 64 + c->index);
2545 }
2546
2547 /*
2548  *      Register console.
2549  */
2550 long __init console_8xx_init(long kmem_start, long kmem_end)
2551 {
2552         register_console(&sercons);
2553         return kmem_start;
2554 }
2555
2556 #endif /* CONFIG_SERIAL_CONSOLE */
2557
2558 /* Index in baud rate table of the default console baud rate.
2559 */
2560 static  int     baud_idx;
2561
2562 /*
2563  * The serial driver boot-time initialization code!
2564  */
2565
2566 int __init rs_8xx_alloc_brg(int port)
2567 {
2568     static int brg = 0;
2569     volatile cpm8xx_t *cp = cpmp;
2570     int res = brg;
2571
2572     /* "Wire" the BRG to the specified port
2573     */
2574     switch (port) {
2575     case QUICC_CPM_SMC1:
2576         cp->cp_simode = (cp->cp_simode & ~(0x07<<12)) | (brg<<12);
2577         break;
2578     case QUICC_CPM_SMC2:
2579         cp->cp_simode = (cp->cp_simode & ~(0x07<<28)) | (brg<<28);
2580         break;
2581     case QUICC_CPM_SCC1:
2582         cp->cp_sicr = (cp->cp_sicr & ~(0xFF<<0)) | (((brg<<3)|(brg<<0))<<0);
2583         break;
2584     case QUICC_CPM_SCC2:
2585         cp->cp_sicr = (cp->cp_sicr & ~(0xFF<<8)) | (((brg<<3)|(brg<<0))<<8);
2586         break;
2587     case QUICC_CPM_SCC3:
2588         cp->cp_sicr = (cp->cp_sicr & ~(0xFF<<16)) | (((brg<<3)|(brg<<0))<<16);
2589         break;
2590     case QUICC_CPM_SCC4:
2591         cp->cp_sicr = (cp->cp_sicr & ~(0xFF<<24)) | (((brg<<3)|(brg<<0))<<24);
2592         break;
2593     }
2594     /* Consume this BRG - Note: the last BRG will be reused if this
2595     */
2596     /* function is called too many times!
2597     */
2598     if (brg < QUICC_MAX_BRG) brg++;
2599     return res;
2600 }
2601
2602 int __init rs_8xx_init(void)
2603 {
2604         struct serial_state * state;
2605         ser_info_t      *info;
2606         uint            mem_addr, dp_addr, iobits;
2607         int             i, j, idx;
2608         ushort          chan;
2609         volatile        cbd_t           *bdp;
2610         volatile        cpm8xx_t        *cp;
2611         volatile        smc_t           *sp;
2612         volatile        smc_uart_t      *up;
2613         volatile        scc_t           *scp;
2614         volatile        scc_uart_t      *sup;
2615         volatile        immap_t         *immap;
2616
2617         init_bh(SERIAL_BH, do_serial_bh);
2618
2619         show_serial_version();
2620
2621         /* Initialize the tty_driver structure */
2622
2623         __clear_user(&serial_driver,sizeof(struct tty_driver));
2624         serial_driver.magic = TTY_DRIVER_MAGIC;
2625         serial_driver.driver_name = "serial";
2626 #ifdef CONFIG_DEVFS_FS
2627         serial_driver.name = "tts/%d";
2628 #else
2629         serial_driver.name = "ttyS";
2630 #endif
2631         serial_driver.major = TTY_MAJOR;
2632         serial_driver.minor_start = 64;
2633         serial_driver.num = NR_PORTS;
2634         serial_driver.type = TTY_DRIVER_TYPE_SERIAL;
2635         serial_driver.subtype = SERIAL_TYPE_NORMAL;
2636         serial_driver.init_termios = tty_std_termios;
2637         serial_driver.init_termios.c_cflag =
2638                 baud_idx | CS8 | CREAD | HUPCL | CLOCAL;
2639         serial_driver.flags = TTY_DRIVER_REAL_RAW;
2640         serial_driver.refcount = &serial_refcount;
2641         serial_driver.table = serial_table;
2642         serial_driver.termios = serial_termios;
2643         serial_driver.termios_locked = serial_termios_locked;
2644
2645         serial_driver.open = rs_8xx_open;
2646         serial_driver.close = rs_8xx_close;
2647         serial_driver.write = rs_8xx_write;
2648         serial_driver.put_char = rs_8xx_put_char;
2649         serial_driver.write_room = rs_8xx_write_room;
2650         serial_driver.chars_in_buffer = rs_8xx_chars_in_buffer;
2651         serial_driver.flush_buffer = rs_8xx_flush_buffer;
2652         serial_driver.ioctl = rs_8xx_ioctl;
2653         serial_driver.throttle = rs_8xx_throttle;
2654         serial_driver.unthrottle = rs_8xx_unthrottle;
2655         serial_driver.send_xchar = rs_8xx_send_xchar;
2656         serial_driver.set_termios = rs_8xx_set_termios;
2657         serial_driver.stop = rs_8xx_stop;
2658         serial_driver.start = rs_8xx_start;
2659         serial_driver.hangup = rs_8xx_hangup;
2660         serial_driver.wait_until_sent = rs_8xx_wait_until_sent;
2661         serial_driver.read_proc = rs_8xx_read_proc;
2662
2663         /*
2664          * The callout device is just like normal device except for
2665          * major number and the subtype code.
2666          */
2667         callout_driver = serial_driver;
2668 #ifdef CONFIG_DEVFS_FS
2669         callout_driver.name = "cua/%d";
2670 #else
2671         callout_driver.name = "cua";
2672 #endif
2673         callout_driver.major = TTYAUX_MAJOR;
2674         callout_driver.subtype = SERIAL_TYPE_CALLOUT;
2675         callout_driver.read_proc = 0;
2676         callout_driver.proc_entry = 0;
2677
2678         if (tty_register_driver(&serial_driver))
2679                 panic("Couldn't register serial driver\n");
2680         if (tty_register_driver(&callout_driver))
2681                 panic("Couldn't register callout driver\n");
2682
2683         cp = cpmp;      /* Get pointer to Communication Processor */
2684         immap = (immap_t *)IMAP_ADDR;   /* and to internal registers */
2685
2686
2687         /* Configure SCC2, SCC3, and SCC4 instead of port A parallel I/O.
2688          */
2689 #ifdef CONFIG_USE_SCC_IO
2690 #ifndef CONFIG_MBX
2691         /* The "standard" configuration through the 860.
2692         */
2693         immap->im_ioport.iop_papar |= 0x00fc;
2694         immap->im_ioport.iop_padir &= ~0x00fc;
2695         immap->im_ioport.iop_paodr &= ~0x00fc;
2696 #else
2697         /* On the MBX, SCC3 is through Port D.
2698         */
2699         immap->im_ioport.iop_papar |= 0x000c;   /* SCC2 on port A */
2700         immap->im_ioport.iop_padir &= ~0x000c;
2701         immap->im_ioport.iop_paodr &= ~0x000c;
2702
2703         immap->im_ioport.iop_pdpar |= 0x0030;   /* SCC3 on port D */
2704 #endif
2705
2706         /* Since we don't yet do modem control, connect the port C pins
2707          * as general purpose I/O.  This will assert CTS and CD for the
2708          * SCC ports.
2709          */
2710         immap->im_ioport.iop_pcdir |= 0x03c6;
2711         immap->im_ioport.iop_pcpar &= ~0x03c6;
2712
2713         /* Connect SCC2 and SCC3 to NMSI.  Connect BRG3 to SCC2 and
2714          * BRG4 to SCC3.
2715          */
2716         cp->cp_sicr &= ~0x00ffff00;
2717         cp->cp_sicr |= 0x001b1200;
2718
2719 #ifdef CONFIG_PP04
2720         /* Frequentis PP04 forced to RS-232 until we know better.
2721          * Port C 12 and 13 low enables RS-232 on SCC3 and SCC4.
2722          */
2723         immap->im_ioport.iop_pcdir |= 0x000c;
2724         immap->im_ioport.iop_pcpar &= ~0x000c;
2725         immap->im_ioport.iop_pcdat &= ~0x000c;
2726
2727         /* This enables the TX driver.
2728         */
2729         cp->cp_pbpar &= ~0x6000;
2730         cp->cp_pbdat &= ~0x6000;
2731 #endif
2732 #endif
2733
2734         for (i = 0, state = rs_table; i < NR_PORTS; i++,state++) {
2735                 state->magic = SSTATE_MAGIC;
2736                 state->line = i;
2737                 state->type = PORT_UNKNOWN;
2738                 state->custom_divisor = 0;
2739                 state->close_delay = 5*HZ/10;
2740                 state->closing_wait = 30*HZ;
2741                 state->callout_termios = callout_driver.init_termios;
2742                 state->normal_termios = serial_driver.init_termios;
2743                 state->icount.cts = state->icount.dsr =
2744                         state->icount.rng = state->icount.dcd = 0;
2745                 state->icount.rx = state->icount.tx = 0;
2746                 state->icount.frame = state->icount.parity = 0;
2747                 state->icount.overrun = state->icount.brk = 0;
2748                 PORT_BRG_SET(state->smc_scc_num, rs_8xx_alloc_brg(state->smc_scc_num));
2749                 printk(KERN_INFO "ttyS%d at 0x%04x is on %s%d using BRG%d\n",
2750                        i, (unsigned int)(state->port),
2751                        PORT_IS_SCC(state->smc_scc_num) ? "SCC" : "SMC",
2752                        PORT_NUM(state->smc_scc_num)+1,
2753                        PORT_BRG(state->smc_scc_num)+1);
2754 #ifdef CONFIG_SERIAL_CONSOLE
2755                 /* If we just printed the message on the console port, and
2756                  * we are about to initialize it for general use, we have
2757                  * to wait a couple of character times for the CR/NL to
2758                  * make it out of the transmit buffer.
2759                  */
2760                 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2761                         mdelay(2);
2762 #endif
2763                 info = kmalloc(sizeof(ser_info_t), GFP_KERNEL);
2764                 if (info) {
2765                         __clear_user(info,sizeof(ser_info_t));
2766                         init_waitqueue_head(&info->open_wait);
2767                         init_waitqueue_head(&info->close_wait);
2768                         info->magic = SERIAL_MAGIC;
2769                         info->flags = state->flags;
2770                         info->tqueue.routine = do_softint;
2771                         info->tqueue.data = info;
2772                         info->tqueue_hangup.routine = do_serial_hangup;
2773                         info->tqueue_hangup.data = info;
2774                         info->line = i;
2775                         info->state = state;
2776                         state->info = (struct async_struct *)info;
2777
2778                         /* We need to allocate a transmit and receive buffer
2779                          * descriptors from dual port ram, and a character
2780                          * buffer area from host mem.
2781                          */
2782                         dp_addr = m8xx_cpm_dpalloc(sizeof(cbd_t) * RX_NUM_FIFO);
2783
2784                         /* Allocate space for FIFOs in the host memory.
2785                         */
2786                         mem_addr = m8xx_cpm_hostalloc(RX_NUM_FIFO * RX_BUF_SIZE);
2787                         info->rx_va_base = (unsigned char *)mem_addr;
2788
2789                         /* Set the physical address of the host memory
2790                          * buffers in the buffer descriptors, and the
2791                          * virtual address for us to work with.
2792                          */
2793                         bdp = (cbd_t *)&cp->cp_dpmem[dp_addr];
2794                         info->rx_cur = info->rx_bd_base = (cbd_t *)bdp;
2795
2796                         for (j=0; j<(RX_NUM_FIFO-1); j++) {
2797                                 bdp->cbd_bufaddr = iopa(mem_addr);
2798                                 bdp->cbd_sc = BD_SC_EMPTY | BD_SC_INTRPT;
2799                                 mem_addr += RX_BUF_SIZE;
2800                                 bdp++;
2801                         }
2802                         bdp->cbd_bufaddr = iopa(mem_addr);
2803                         bdp->cbd_sc = BD_SC_WRAP | BD_SC_EMPTY | BD_SC_INTRPT;
2804
2805                         idx = PORT_NUM(info->state->smc_scc_num);
2806                         if (PORT_IS_SCC(info->state->smc_scc_num)) {
2807                                 scp = &cp->cp_scc[idx];
2808                                 sup = (scc_uart_t *)&cp->cp_dparam[state->port];
2809                                 sup->scc_genscc.scc_rbase = dp_addr;
2810                         }
2811                         else {
2812                                 sp = &cp->cp_smc[idx];
2813                                 up = (smc_uart_t *)&cp->cp_dparam[state->port];
2814                                 up->smc_rbase = dp_addr;
2815                         }
2816
2817                         dp_addr = m8xx_cpm_dpalloc(sizeof(cbd_t) * TX_NUM_FIFO);
2818
2819                         /* Allocate space for FIFOs in the host memory.
2820                         */
2821                         mem_addr = m8xx_cpm_hostalloc(TX_NUM_FIFO * TX_BUF_SIZE);
2822                         info->tx_va_base = (unsigned char *)mem_addr;
2823
2824                         /* Set the physical address of the host memory
2825                          * buffers in the buffer descriptors, and the
2826                          * virtual address for us to work with.
2827                          */
2828                         bdp = (cbd_t *)&cp->cp_dpmem[dp_addr];
2829                         info->tx_cur = info->tx_bd_base = (cbd_t *)bdp;
2830
2831                         for (j=0; j<(TX_NUM_FIFO-1); j++) {
2832                                 bdp->cbd_bufaddr = iopa(mem_addr);
2833                                 bdp->cbd_sc = BD_SC_INTRPT;
2834                                 mem_addr += TX_BUF_SIZE;
2835                                 bdp++;
2836                         }
2837                         bdp->cbd_bufaddr = iopa(mem_addr);
2838                         bdp->cbd_sc = (BD_SC_WRAP | BD_SC_INTRPT);
2839
2840                         if (PORT_IS_SCC(info->state->smc_scc_num)) {
2841                                 sup->scc_genscc.scc_tbase = dp_addr;
2842
2843                                 /* Set up the uart parameters in the
2844                                  * parameter ram.
2845                                  */
2846                                 sup->scc_genscc.scc_rfcr = SMC_EB;
2847                                 sup->scc_genscc.scc_tfcr = SMC_EB;
2848
2849                                 /* Set this to 1 for now, so we get single
2850                                  * character interrupts.  Using idle charater
2851                                  * time requires some additional tuning.
2852                                  */
2853                                 sup->scc_genscc.scc_mrblr = 1;
2854                                 sup->scc_maxidl = 0;
2855                                 sup->scc_brkcr = 1;
2856                                 sup->scc_parec = 0;
2857                                 sup->scc_frmec = 0;
2858                                 sup->scc_nosec = 0;
2859                                 sup->scc_brkec = 0;
2860                                 sup->scc_uaddr1 = 0;
2861                                 sup->scc_uaddr2 = 0;
2862                                 sup->scc_toseq = 0;
2863                                 sup->scc_char1 = 0x8000;
2864                                 sup->scc_char2 = 0x8000;
2865                                 sup->scc_char3 = 0x8000;
2866                                 sup->scc_char4 = 0x8000;
2867                                 sup->scc_char5 = 0x8000;
2868                                 sup->scc_char6 = 0x8000;
2869                                 sup->scc_char7 = 0x8000;
2870                                 sup->scc_char8 = 0x8000;
2871                                 sup->scc_rccm = 0xc0ff;
2872
2873                                 /* Send the CPM an initialize command.
2874                                 */
2875                                 chan = scc_chan_map[idx];
2876
2877                                 cp->cp_cpcr = mk_cr_cmd(chan,
2878                                                 CPM_CR_INIT_TRX) | CPM_CR_FLG;
2879                                 while (cp->cp_cpcr & CPM_CR_FLG);
2880
2881                                 /* Set UART mode, 8 bit, no parity, one stop.
2882                                  * Enable receive and transmit.
2883                                  */
2884                                 scp->scc_gsmrh = 0;
2885                                 scp->scc_gsmrl =
2886                                         (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
2887
2888                                 /* Disable all interrupts and clear all pending
2889                                  * events.
2890                                  */
2891                                 scp->scc_sccm = 0;
2892                                 scp->scc_scce = 0xffff;
2893                                 scp->scc_dsr = 0x7e7e;
2894                                 scp->scc_pmsr = 0x3000;
2895
2896                                 /* If the port is the console, enable Rx and Tx.
2897                                 */
2898 #ifdef CONFIG_SERIAL_CONSOLE
2899                                 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2900                                         scp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
2901 #endif
2902                         }
2903                         else {
2904                                 /* Configure SMCs Tx/Rx instead of port B
2905                                  * parallel I/O.  On 823/850 these are on
2906                                  * port A for SMC2.
2907                                  */
2908 #ifndef CONFIG_ALTSMC2
2909                                 iobits = 0xc0 << (idx * 4);
2910                                 cp->cp_pbpar |= iobits;
2911                                 cp->cp_pbdir &= ~iobits;
2912                                 cp->cp_pbodr &= ~iobits;
2913 #else
2914                                 iobits = 0xc0;
2915                                 if (idx == 0) {
2916                                         /* SMC1 on Port B, like all 8xx.
2917                                         */
2918                                         cp->cp_pbpar |= iobits;
2919                                         cp->cp_pbdir &= ~iobits;
2920                                         cp->cp_pbodr &= ~iobits;
2921                                 }
2922                                 else {
2923                                         /* SMC2 is on Port A.
2924                                         */
2925                                         immap->im_ioport.iop_papar |= iobits;
2926                                         immap->im_ioport.iop_padir &= ~iobits;
2927                                         immap->im_ioport.iop_paodr &= ~iobits;
2928                                 }
2929 #endif /* CONFIG_ALTSMC2 */
2930
2931 #if 0
2932                                 /* Connect the baud rate generator to the
2933                                  * SMC based upon index in rs_table.  Also
2934                                  * make sure it is connected to NMSI.
2935                                  */
2936                                 cp->cp_simode &= ~(0xffff << (idx * 16));
2937                                 cp->cp_simode |= (i << ((idx * 16) + 12));
2938 #endif
2939
2940                                 up->smc_tbase = dp_addr;
2941
2942                                 /* Set up the uart parameters in the
2943                                  * parameter ram.
2944                                  */
2945                                 up->smc_rfcr = SMC_EB;
2946                                 up->smc_tfcr = SMC_EB;
2947
2948                                 /* Set this to 1 for now, so we get single
2949                                  * character interrupts.  Using idle charater
2950                                  * time requires some additional tuning.
2951                                  */
2952                                 up->smc_mrblr = 1;
2953                                 up->smc_maxidl = 0;
2954                                 up->smc_brkcr = 1;
2955
2956                                 /* Send the CPM an initialize command.
2957                                 */
2958                                 chan = smc_chan_map[idx];
2959
2960                                 cp->cp_cpcr = mk_cr_cmd(chan,
2961                                                 CPM_CR_INIT_TRX) | CPM_CR_FLG;
2962                                 while (cp->cp_cpcr & CPM_CR_FLG);
2963
2964                                 /* Set UART mode, 8 bit, no parity, one stop.
2965                                  * Enable receive and transmit.
2966                                  */
2967                                 sp->smc_smcmr = smcr_mk_clen(9) | SMCMR_SM_UART;
2968
2969                                 /* Disable all interrupts and clear all pending
2970                                  * events.
2971                                  */
2972                                 sp->smc_smcm = 0;
2973                                 sp->smc_smce = 0xff;
2974
2975                                 /* If the port is the console, enable Rx and Tx.
2976                                 */
2977 #ifdef CONFIG_SERIAL_CONSOLE
2978                                 if (i == CONFIG_SERIAL_CONSOLE_PORT)
2979                                         sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
2980 #endif
2981                         }
2982
2983                         /* Install interrupt handler.
2984                         */
2985                         if ((request_irq(CPM_IRQ_OFFSET + state->irq, rs_8xx_interrupt, 0, cpm_int_name[state->irq], info)) != 0)
2986                                 panic("Could not allocate UART IRQ!");
2987
2988                         /* Set up the baud rate generator.
2989                         */
2990                         m8xx_cpm_setbrg(PORT_BRG(state->smc_scc_num), baud_table[baud_idx]);
2991
2992                 }
2993         }
2994         console_tx_buf_len = TX_BUF_SIZE;
2995         return 0;
2996 }
2997
2998 /* This must always be called before the rs_8xx_init() function, otherwise
2999  * it blows away the port control information.
3000 */
3001 static int __init serial_console_setup(struct console *co, char *options)
3002 {
3003         struct          serial_state *ser;
3004         uint            mem_addr, dp_addr, bidx, idx;
3005         ushort          chan;
3006         volatile        cbd_t           *bdp;
3007         volatile        cpm8xx_t        *cp;
3008         volatile        smc_t           *sp;
3009         volatile        scc_t           *scp;
3010         volatile        smc_uart_t      *up;
3011         volatile        scc_uart_t      *sup;
3012         bd_t                            *bd;
3013
3014         bd = (bd_t *)__res;
3015
3016         for (bidx = 0; bidx < (sizeof(baud_table) / sizeof(int)); bidx++)
3017                 if (bd->bi_baudrate == baud_table[bidx])
3018                         break;
3019         /* make sure we have a useful value */
3020         if (bidx == (sizeof(baud_table) / sizeof(int)))
3021                 bidx = 13;      /* B9600 */
3022
3023         co->cflag = CREAD|CLOCAL|bidx|CS8;
3024         baud_idx = bidx;
3025
3026         ser = rs_table + co->index;
3027
3028         cp = cpmp;      /* Get pointer to Communication Processor */
3029
3030         idx = PORT_NUM(ser->smc_scc_num);
3031         if (PORT_IS_SCC(ser->smc_scc_num)) {
3032                 scp = &cp->cp_scc[idx];
3033                 sup = (scc_uart_t *)&cp->cp_dparam[ser->port];
3034         }
3035         else {
3036                 sp = &cp->cp_smc[idx];
3037                 up = (smc_uart_t *)&cpmp->cp_dparam[ser->port];
3038         }
3039
3040         /* When we get here, the CPM has been reset, so we need
3041          * to configure the port.
3042          * We need to allocate a transmit and receive buffer descriptor
3043          * from dual port ram, and a character buffer area from host mem.
3044          */
3045
3046         /* Allocate space for two FIFOs.  We can't allocate from host
3047          * memory yet because vm allocator isn't initialized
3048          * during this early console init.
3049          */
3050         dp_addr = m8xx_cpm_dpalloc(2*EARLY_BUF_SIZE);
3051         mem_addr = (uint)(&cpmp->cp_dpmem[dp_addr]);
3052
3053         /* Allocate space for two buffer descriptors in the DP ram.
3054         */
3055         dp_addr = m8xx_cpm_dpalloc(sizeof(cbd_t) * 2);
3056
3057         /* Set the physical address of the host memory buffers in
3058          * the buffer descriptors.
3059          */
3060         bdp = (cbd_t *)&cp->cp_dpmem[dp_addr];
3061         bdp->cbd_bufaddr = iopa(mem_addr);
3062         (bdp+1)->cbd_bufaddr = iopa(mem_addr+4);
3063
3064         consinfo.rx_va_base = (unsigned char *) mem_addr;
3065         consinfo.rx_bd_base = (cbd_t *) bdp;
3066         consinfo.tx_va_base = (unsigned char *) (mem_addr + EARLY_BUF_SIZE);
3067         consinfo.tx_bd_base = (cbd_t *) (bdp+1);
3068
3069         /* For the receive, set empty and wrap.
3070          * For transmit, set wrap.
3071          */
3072         bdp->cbd_sc = BD_SC_EMPTY | BD_SC_WRAP;
3073         (bdp+1)->cbd_sc = BD_SC_WRAP;
3074
3075         /* Set up the uart parameters in the parameter ram.
3076         */
3077         if (PORT_IS_SCC(ser->smc_scc_num)) {
3078
3079                 sup->scc_genscc.scc_rbase = dp_addr;
3080                 sup->scc_genscc.scc_tbase = dp_addr + sizeof(cbd_t);
3081
3082                 /* Set up the uart parameters in the
3083                  * parameter ram.
3084                  */
3085                 sup->scc_genscc.scc_rfcr = SMC_EB;
3086                 sup->scc_genscc.scc_tfcr = SMC_EB;
3087
3088                 /* Set this to 1 for now, so we get single
3089                  * character interrupts.  Using idle charater
3090                  * time requires some additional tuning.
3091                  */
3092                 sup->scc_genscc.scc_mrblr = 1;
3093                 sup->scc_maxidl = 0;
3094                 sup->scc_brkcr = 1;
3095                 sup->scc_parec = 0;
3096                 sup->scc_frmec = 0;
3097                 sup->scc_nosec = 0;
3098                 sup->scc_brkec = 0;
3099                 sup->scc_uaddr1 = 0;
3100                 sup->scc_uaddr2 = 0;
3101                 sup->scc_toseq = 0;
3102                 sup->scc_char1 = 0x8000;
3103                 sup->scc_char2 = 0x8000;
3104                 sup->scc_char3 = 0x8000;
3105                 sup->scc_char4 = 0x8000;
3106                 sup->scc_char5 = 0x8000;
3107                 sup->scc_char6 = 0x8000;
3108                 sup->scc_char7 = 0x8000;
3109                 sup->scc_char8 = 0x8000;
3110                 sup->scc_rccm = 0xc0ff;
3111
3112                 /* Send the CPM an initialize command.
3113                 */
3114                 chan = scc_chan_map[idx];
3115
3116                 cp->cp_cpcr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
3117                 while (cp->cp_cpcr & CPM_CR_FLG);
3118
3119                 /* Set UART mode, 8 bit, no parity, one stop.
3120                  * Enable receive and transmit.
3121                  */
3122                 scp->scc_gsmrh = 0;
3123                 scp->scc_gsmrl =
3124                         (SCC_GSMRL_MODE_UART | SCC_GSMRL_TDCR_16 | SCC_GSMRL_RDCR_16);
3125
3126                 /* Disable all interrupts and clear all pending
3127                  * events.
3128                  */
3129                 scp->scc_sccm = 0;
3130                 scp->scc_scce = 0xffff;
3131                 scp->scc_dsr = 0x7e7e;
3132                 scp->scc_pmsr = 0x3000;
3133
3134                 scp->scc_gsmrl |= (SCC_GSMRL_ENR | SCC_GSMRL_ENT);
3135
3136         }
3137         else {
3138                 up->smc_rbase = dp_addr;        /* Base of receive buffer desc. */
3139                 up->smc_tbase = dp_addr+sizeof(cbd_t);  /* Base of xmt buffer desc. */
3140                 up->smc_rfcr = SMC_EB;
3141                 up->smc_tfcr = SMC_EB;
3142
3143                 /* Set this to 1 for now, so we get single character interrupts.
3144                 */
3145                 up->smc_mrblr = 1;              /* receive buffer length */
3146                 up->smc_maxidl = 0;             /* wait forever for next char */
3147
3148                 /* Send the CPM an initialize command.
3149                 */
3150                 chan = smc_chan_map[idx];
3151                 cp->cp_cpcr = mk_cr_cmd(chan, CPM_CR_INIT_TRX) | CPM_CR_FLG;
3152                 printk("%s", "");
3153                 while (cp->cp_cpcr & CPM_CR_FLG);
3154
3155                 /* Set UART mode, 8 bit, no parity, one stop.
3156                  * Enable receive and transmit.
3157                  */
3158                 sp->smc_smcmr = smcr_mk_clen(9) |  SMCMR_SM_UART;
3159
3160                 /* And finally, enable Rx and Tx.
3161                 */
3162                 sp->smc_smcmr |= SMCMR_REN | SMCMR_TEN;
3163         }
3164
3165         /* Set up the baud rate generator.
3166         */
3167         m8xx_cpm_setbrg((ser - rs_table), bd->bi_baudrate);
3168         console_tx_buf_len = EARLY_BUF_SIZE;
3169         return 0;
3170 }
3171