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