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