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