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