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