vserver 1.9.3
[linux-2.6.git] / drivers / char / serial167.c
1 /*
2  * linux/drivers/char/serial167.c
3  *
4  * Driver for MVME166/7 board serial ports, which are via a CD2401.
5  * Based very much on cyclades.c.
6  *
7  * MVME166/7 work by Richard Hirst [richard@sleepie.demon.co.uk]
8  *
9  * ==============================================================
10  *
11  * static char rcsid[] =
12  * "$Revision: 1.36.1.4 $$Date: 1995/03/29 06:14:14 $";
13  *
14  *  linux/kernel/cyclades.c
15  *
16  * Maintained by Marcio Saito (cyclades@netcom.com) and
17  * Randolph Bentson (bentson@grieg.seaslug.org)
18  *
19  * Much of the design and some of the code came from serial.c
20  * which was copyright (C) 1991, 1992  Linus Torvalds.  It was
21  * extensively rewritten by Theodore Ts'o, 8/16/92 -- 9/14/92,
22  * and then fixed as suggested by Michael K. Johnson 12/12/92.
23  *
24  * This version does not support shared irq's.
25  *
26  * $Log: cyclades.c,v $
27  * Revision 1.36.1.4  1995/03/29  06:14:14  bentson
28  * disambiguate between Cyclom-16Y and Cyclom-32Ye;
29  *
30  * Changes:
31  *
32  * 200 lines of changes record removed - RGH 11-10-95, starting work on
33  * converting this to drive serial ports on mvme166 (cd2401).
34  *
35  * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 2000/08/25
36  * - get rid of verify_area
37  * - use get_user to access memory from userspace in set_threshold,
38  *   set_default_threshold and set_timeout
39  * - don't use the panic function in serial167_init
40  * - do resource release on failure on serial167_init
41  * - include missing restore_flags in mvme167_serial_console_setup
42  */
43
44 #include <linux/config.h>
45 #include <linux/errno.h>
46 #include <linux/signal.h>
47 #include <linux/sched.h>
48 #include <linux/timer.h>
49 #include <linux/tty.h>
50 #include <linux/interrupt.h>
51 #include <linux/serial.h>
52 #include <linux/serialP.h>
53 #include <linux/string.h>
54 #include <linux/fcntl.h>
55 #include <linux/ptrace.h>
56 #include <linux/serial167.h>
57 #include <linux/delay.h>
58 #include <linux/major.h>
59 #include <linux/mm.h>
60 #include <linux/console.h>
61 #include <linux/module.h>
62
63 #include <asm/system.h>
64 #include <asm/io.h>
65 #include <asm/bitops.h>
66 #include <asm/mvme16xhw.h>
67 #include <asm/bootinfo.h>
68 #include <asm/setup.h>
69
70 #include <linux/types.h>
71 #include <linux/kernel.h>
72
73 #include <asm/uaccess.h>
74 #include <linux/init.h>
75
76 #define SERIAL_PARANOIA_CHECK
77 #undef  SERIAL_DEBUG_OPEN
78 #undef  SERIAL_DEBUG_THROTTLE
79 #undef  SERIAL_DEBUG_OTHER
80 #undef  SERIAL_DEBUG_IO
81 #undef  SERIAL_DEBUG_COUNT
82 #undef  SERIAL_DEBUG_DTR
83 #undef  CYCLOM_16Y_HACK
84 #define  CYCLOM_ENABLE_MONITORING
85
86 #define WAKEUP_CHARS 256
87
88 #define STD_COM_FLAGS (0)
89
90 #define SERIAL_TYPE_NORMAL  1
91
92 DECLARE_TASK_QUEUE(tq_cyclades);
93
94 static struct tty_driver *cy_serial_driver;
95 extern int serial_console;
96 static struct cyclades_port *serial_console_info = NULL;
97 static unsigned int serial_console_cflag = 0;
98 u_char initial_console_speed;
99
100 /* Base address of cd2401 chip on mvme166/7 */
101
102 #define BASE_ADDR (0xfff45000)
103 #define pcc2chip        ((volatile u_char *)0xfff42000)
104 #define PccSCCMICR      0x1d
105 #define PccSCCTICR      0x1e
106 #define PccSCCRICR      0x1f
107 #define PccTPIACKR      0x25
108 #define PccRPIACKR      0x27
109 #define PccIMLR         0x3f
110
111 /* This is the per-port data structure */
112 struct cyclades_port cy_port[] = {
113       /* CARD#  */
114         {-1 },      /* ttyS0 */
115         {-1 },      /* ttyS1 */
116         {-1 },      /* ttyS2 */
117         {-1 },      /* ttyS3 */
118 };
119 #define NR_PORTS        (sizeof(cy_port)/sizeof(struct cyclades_port))
120
121 /*
122  * tmp_buf is used as a temporary buffer by serial_write.  We need to
123  * lock it in case the copy_from_user blocks while swapping in a page,
124  * and some other program tries to do a serial write at the same time.
125  * Since the lock will only come under contention when the system is
126  * swapping and available memory is low, it makes sense to share one
127  * buffer across all the serial ports, since it significantly saves
128  * memory if large numbers of serial ports are open.
129  */
130 static unsigned char *tmp_buf = 0;
131 DECLARE_MUTEX(tmp_buf_sem);
132
133 /*
134  * This is used to look up the divisor speeds and the timeouts
135  * We're normally limited to 15 distinct baud rates.  The extra
136  * are accessed via settings in info->flags.
137  *         0,     1,     2,     3,     4,     5,     6,     7,     8,     9,
138  *        10,    11,    12,    13,    14,    15,    16,    17,    18,    19,
139  *                                                  HI            VHI
140  */
141 static int baud_table[] = {
142            0,    50,    75,   110,   134,   150,   200,   300,   600,  1200,
143         1800,  2400,  4800,  9600, 19200, 38400, 57600, 76800,115200,150000,
144         0};
145
146 #if 0
147 static char baud_co[] = {  /* 25 MHz clock option table */
148         /* value =>    00    01   02    03    04 */
149         /* divide by    8    32   128   512  2048 */
150         0x00,  0x04,  0x04,  0x04,  0x04,  0x04,  0x03,  0x03,  0x03,  0x02,
151         0x02,  0x02,  0x01,  0x01,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00};
152
153 static char baud_bpr[] = {  /* 25 MHz baud rate period table */
154         0x00,  0xf5,  0xa3,  0x6f,  0x5c,  0x51,  0xf5,  0xa3,  0x51,  0xa3,
155         0x6d,  0x51,  0xa3,  0x51,  0xa3,  0x51,  0x36,  0x29,  0x1b,  0x15};
156 #endif
157
158 /* I think 166 brd clocks 2401 at 20MHz.... */
159
160 /* These values are written directly to tcor, and >> 5 for writing to rcor */
161 static u_char baud_co[] = {  /* 20 MHz clock option table */
162         0x00,  0x80,  0x80,  0x80,  0x80,  0x80,  0x80,  0x60,  0x60,  0x40,
163         0x40,  0x40,  0x20,  0x20,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00};
164
165 /* These values written directly to tbpr/rbpr */
166 static u_char baud_bpr[] = {  /* 20 MHz baud rate period table */
167         0x00,  0xc0,  0x80,  0x58,  0x6c,  0x40,  0xc0,  0x81,  0x40,  0x81,
168         0x57,  0x40,  0x81,  0x40,  0x81,  0x40,  0x2b,  0x20,  0x15,  0x10};
169
170 static u_char baud_cor4[] = {  /* receive threshold */
171         0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,  0x0a,
172         0x0a,  0x0a,  0x0a,  0x09,  0x09,  0x08,  0x08,  0x08,  0x08,  0x07};
173
174
175
176 static void shutdown(struct cyclades_port *);
177 static int startup (struct cyclades_port *);
178 static void cy_throttle(struct tty_struct *);
179 static void cy_unthrottle(struct tty_struct *);
180 static void config_setup(struct cyclades_port *);
181 extern void console_print(const char *);
182 #ifdef CYCLOM_SHOW_STATUS
183 static void show_status(int);
184 #endif
185
186 #ifdef CONFIG_REMOTE_DEBUG
187 static void debug_setup(void);
188 void queueDebugChar (int c);
189 int getDebugChar(void);
190
191 #define DEBUG_PORT      1
192 #define DEBUG_LEN       256
193
194 typedef struct {
195         int     in;
196         int     out;
197         unsigned char   buf[DEBUG_LEN];
198 } debugq;
199
200 debugq debugiq;
201 #endif
202
203 /*
204  * I have my own version of udelay(), as it is needed when initialising
205  * the chip, before the delay loop has been calibrated.  Should probably
206  * reference one of the vmechip2 or pccchip2 counter for an accurate
207  * delay, but this wild guess will do for now.
208  */
209
210 void my_udelay (long us)
211 {
212         u_char x;
213         volatile u_char *p = &x;
214         int i;
215
216         while (us--)
217                 for (i = 100; i; i--)
218                         x |= *p;
219 }
220
221 static inline int
222 serial_paranoia_check(struct cyclades_port *info, char *name,
223                       const char *routine)
224 {
225 #ifdef SERIAL_PARANOIA_CHECK
226     static const char *badmagic =
227         "Warning: bad magic number for serial struct (%s) in %s\n";
228     static const char *badinfo =
229         "Warning: null cyclades_port for (%s) in %s\n";
230     static const char *badrange =
231         "Warning: cyclades_port out of range for (%s) in %s\n";
232
233     if (!info) {
234         printk(badinfo, name, routine);
235         return 1;
236     }
237
238     if( (long)info < (long)(&cy_port[0])
239     || (long)(&cy_port[NR_PORTS]) < (long)info ){
240         printk(badrange, name, routine);
241         return 1;
242     }
243
244     if (info->magic != CYCLADES_MAGIC) {
245         printk(badmagic, name, routine);
246         return 1;
247     }
248 #endif
249         return 0;
250 } /* serial_paranoia_check */
251
252 #if 0
253 /* The following diagnostic routines allow the driver to spew
254    information on the screen, even (especially!) during interrupts.
255  */
256 void
257 SP(char *data){
258   unsigned long flags;
259     local_irq_save(flags);
260         console_print(data);
261     local_irq_restore(flags);
262 }
263 char scrn[2];
264 void
265 CP(char data){
266   unsigned long flags;
267     local_irq_save(flags);
268         scrn[0] = data;
269         console_print(scrn);
270     local_irq_restore(flags);
271 }/* CP */
272
273 void CP1(int data) { (data<10)?  CP(data+'0'): CP(data+'A'-10); }/* CP1 */
274 void CP2(int data) { CP1((data>>4) & 0x0f); CP1( data & 0x0f); }/* CP2 */
275 void CP4(int data) { CP2((data>>8) & 0xff); CP2(data & 0xff); }/* CP4 */
276 void CP8(long data) { CP4((data>>16) & 0xffff); CP4(data & 0xffff); }/* CP8 */
277 #endif
278
279 /* This routine waits up to 1000 micro-seconds for the previous
280    command to the Cirrus chip to complete and then issues the
281    new command.  An error is returned if the previous command
282    didn't finish within the time limit.
283  */
284 u_short
285 write_cy_cmd(volatile u_char *base_addr, u_char cmd)
286 {
287   unsigned long flags;
288   volatile int  i;
289
290     local_irq_save(flags);
291         /* Check to see that the previous command has completed */
292         for(i = 0 ; i < 100 ; i++){
293             if (base_addr[CyCCR] == 0){
294                 break;
295             }
296             my_udelay(10L);
297         }
298         /* if the CCR never cleared, the previous command
299             didn't finish within the "reasonable time" */
300         if ( i == 10 ) {
301             local_irq_restore(flags);
302             return (-1);
303         }
304
305         /* Issue the new command */
306         base_addr[CyCCR] = cmd;
307     local_irq_restore(flags);
308     return(0);
309 } /* write_cy_cmd */
310
311
312 /* cy_start and cy_stop provide software output flow control as a
313    function of XON/XOFF, software CTS, and other such stuff. */
314
315 static void
316 cy_stop(struct tty_struct *tty)
317 {
318   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
319   volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
320   int channel;
321   unsigned long flags;
322
323 #ifdef SERIAL_DEBUG_OTHER
324     printk("cy_stop %s\n", tty->name); /* */
325 #endif
326
327     if (serial_paranoia_check(info, tty->name, "cy_stop"))
328         return;
329         
330     channel = info->line;
331
332     local_irq_save(flags);
333         base_addr[CyCAR] = (u_char)(channel); /* index channel */
334         base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
335     local_irq_restore(flags);
336
337     return;
338 } /* cy_stop */
339
340 static void
341 cy_start(struct tty_struct *tty)
342 {
343   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
344   volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
345   int channel;
346   unsigned long flags;
347
348 #ifdef SERIAL_DEBUG_OTHER
349     printk("cy_start %s\n", tty->name); /* */
350 #endif
351
352     if (serial_paranoia_check(info, tty->name, "cy_start"))
353         return;
354         
355     channel = info->line;
356
357     local_irq_save(flags);
358         base_addr[CyCAR] = (u_char)(channel);
359         base_addr[CyIER] |= CyTxMpty;
360     local_irq_restore(flags);
361
362     return;
363 } /* cy_start */
364
365
366 /*
367  * This routine is used by the interrupt handler to schedule
368  * processing in the software interrupt portion of the driver
369  * (also known as the "bottom half").  This can be called any
370  * number of times for any channel without harm.
371  */
372 static inline void
373 cy_sched_event(struct cyclades_port *info, int event)
374 {
375     info->event |= 1 << event; /* remember what kind of event and who */
376     queue_task(&info->tqueue, &tq_cyclades); /* it belongs to */
377     mark_bh(CYCLADES_BH);                       /* then trigger event */
378 } /* cy_sched_event */
379
380
381 /* The real interrupt service routines are called
382    whenever the card wants its hand held--chars
383    received, out buffer empty, modem change, etc.
384  */
385 static irqreturn_t
386 cd2401_rxerr_interrupt(int irq, void *dev_id, struct pt_regs *fp)
387 {
388     struct tty_struct *tty;
389     struct cyclades_port *info;
390     volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
391     unsigned char err, rfoc;
392     int channel;
393     char data;
394
395     /* determine the channel and change to that context */
396     channel = (u_short ) (base_addr[CyLICR] >> 2);
397     info = &cy_port[channel];
398     info->last_active = jiffies;
399
400     if ((err = base_addr[CyRISR]) & CyTIMEOUT) {
401         /* This is a receive timeout interrupt, ignore it */
402         base_addr[CyREOIR] = CyNOTRANS;
403         return IRQ_HANDLED;
404     }
405
406     /* Read a byte of data if there is any - assume the error
407      * is associated with this character */
408
409     if ((rfoc = base_addr[CyRFOC]) != 0)
410         data = base_addr[CyRDR];
411     else
412         data = 0;
413
414     /* if there is nowhere to put the data, discard it */
415     if(info->tty == 0) {
416         base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
417         return IRQ_HANDLED;
418     }
419     else { /* there is an open port for this data */
420         tty = info->tty;
421         if(err & info->ignore_status_mask){
422             base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
423             return IRQ_HANDLED;
424         }
425         if (tty->flip.count < TTY_FLIPBUF_SIZE){
426             tty->flip.count++;
427             if (err & info->read_status_mask){
428                 if(err & CyBREAK){
429                     *tty->flip.flag_buf_ptr++ = TTY_BREAK;
430                     *tty->flip.char_buf_ptr++ = data;
431                     if (info->flags & ASYNC_SAK){
432                         do_SAK(tty);
433                     }
434                 }else if(err & CyFRAME){
435                     *tty->flip.flag_buf_ptr++ = TTY_FRAME;
436                     *tty->flip.char_buf_ptr++ = data;
437                 }else if(err & CyPARITY){
438                     *tty->flip.flag_buf_ptr++ = TTY_PARITY;
439                     *tty->flip.char_buf_ptr++ = data;
440                 }else if(err & CyOVERRUN){
441                     *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
442                     *tty->flip.char_buf_ptr++ = 0;
443                     /*
444                        If the flip buffer itself is
445                        overflowing, we still loose
446                        the next incoming character.
447                      */
448                     if(tty->flip.count < TTY_FLIPBUF_SIZE){
449                         tty->flip.count++;
450                         *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
451                         *tty->flip.char_buf_ptr++ = data;
452                     }
453                 /* These two conditions may imply */
454                 /* a normal read should be done. */
455                 /* else if(data & CyTIMEOUT) */
456                 /* else if(data & CySPECHAR) */
457                 }else{
458                     *tty->flip.flag_buf_ptr++ = 0;
459                     *tty->flip.char_buf_ptr++ = 0;
460                 }
461             }else{
462                 *tty->flip.flag_buf_ptr++ = 0;
463                 *tty->flip.char_buf_ptr++ = 0;
464             }
465         }else{
466             /* there was a software buffer overrun
467                and nothing could be done about it!!! */
468         }
469     }
470     queue_task(&tty->flip.tqueue, &tq_timer);
471     /* end of service */
472     base_addr[CyREOIR] = rfoc ? 0 : CyNOTRANS;
473     return IRQ_HANDLED;
474 } /* cy_rxerr_interrupt */
475
476 static irqreturn_t
477 cd2401_modem_interrupt(int irq, void *dev_id, struct pt_regs *fp)
478 {
479     struct cyclades_port *info;
480     volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
481     int channel;
482     int mdm_change;
483     int mdm_status;
484
485
486     /* determine the channel and change to that context */
487     channel = (u_short ) (base_addr[CyLICR] >> 2);
488     info = &cy_port[channel];
489     info->last_active = jiffies;
490
491     mdm_change = base_addr[CyMISR];
492     mdm_status = base_addr[CyMSVR1];
493
494     if(info->tty == 0){ /* nowhere to put the data, ignore it */
495         ;
496     }else{
497         if((mdm_change & CyDCD)
498         && (info->flags & ASYNC_CHECK_CD)){
499             if(mdm_status & CyDCD){
500 /* CP('!'); */
501                 cy_sched_event(info, Cy_EVENT_OPEN_WAKEUP);
502             } else {
503 /* CP('@'); */
504                 cy_sched_event(info, Cy_EVENT_HANGUP);
505             }
506         }
507         if((mdm_change & CyCTS)
508         && (info->flags & ASYNC_CTS_FLOW)){
509             if(info->tty->stopped){
510                 if(mdm_status & CyCTS){
511                     /* !!! cy_start isn't used because... */
512                     info->tty->stopped = 0;
513                     base_addr[CyIER] |= CyTxMpty;
514                     cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
515                 }
516             }else{
517                 if(!(mdm_status & CyCTS)){
518                     /* !!! cy_stop isn't used because... */
519                     info->tty->stopped = 1;
520                     base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
521                 }
522             }
523         }
524         if(mdm_status & CyDSR){
525         }
526     }
527     base_addr[CyMEOIR] = 0;
528     return IRQ_HANDLED;
529 } /* cy_modem_interrupt */
530
531 static irqreturn_t
532 cd2401_tx_interrupt(int irq, void *dev_id, struct pt_regs *fp)
533 {
534     struct cyclades_port *info;
535     volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
536     int channel;
537     int char_count, saved_cnt;
538     int outch;
539
540     /* determine the channel and change to that context */
541     channel = (u_short ) (base_addr[CyLICR] >> 2);
542
543 #ifdef CONFIG_REMOTE_DEBUG
544     if (channel == DEBUG_PORT) {
545         panic ("TxInt on debug port!!!");
546     }
547 #endif
548
549     info = &cy_port[channel];
550
551     /* validate the port number (as configured and open) */
552     if( (channel < 0) || (NR_PORTS <= channel) ){
553         base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
554         base_addr[CyTEOIR] = CyNOTRANS;
555         return IRQ_HANDLED;
556     }
557     info->last_active = jiffies;
558     if(info->tty == 0){
559         base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
560         if (info->xmit_cnt < WAKEUP_CHARS) {
561             cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
562         }
563         base_addr[CyTEOIR] = CyNOTRANS;
564         return IRQ_HANDLED;
565     }
566
567     /* load the on-chip space available for outbound data */
568     saved_cnt = char_count = base_addr[CyTFTC];
569
570     if(info->x_char) { /* send special char */
571         outch = info->x_char;
572         base_addr[CyTDR] = outch;
573         char_count--;
574         info->x_char = 0;
575     }
576
577     if (info->x_break){
578         /*  The Cirrus chip requires the "Embedded Transmit
579             Commands" of start break, delay, and end break
580             sequences to be sent.  The duration of the
581             break is given in TICs, which runs at HZ
582             (typically 100) and the PPR runs at 200 Hz,
583             so the delay is duration * 200/HZ, and thus a
584             break can run from 1/100 sec to about 5/4 sec.
585             Need to check these values - RGH 141095.
586          */
587         base_addr[CyTDR] = 0; /* start break */
588         base_addr[CyTDR] = 0x81;
589         base_addr[CyTDR] = 0; /* delay a bit */
590         base_addr[CyTDR] = 0x82;
591         base_addr[CyTDR] = info->x_break*200/HZ;
592         base_addr[CyTDR] = 0; /* terminate break */
593         base_addr[CyTDR] = 0x83;
594         char_count -= 7;
595         info->x_break = 0;
596     }
597
598     while (char_count > 0){
599         if (!info->xmit_cnt){
600             base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
601             break;
602         }
603         if (info->xmit_buf == 0){
604             base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
605             break;
606         }
607         if (info->tty->stopped || info->tty->hw_stopped){
608             base_addr[CyIER] &= ~(CyTxMpty|CyTxRdy);
609             break;
610         }
611         /* Because the Embedded Transmit Commands have been
612            enabled, we must check to see if the escape
613            character, NULL, is being sent.  If it is, we
614            must ensure that there is room for it to be
615            doubled in the output stream.  Therefore we
616            no longer advance the pointer when the character
617            is fetched, but rather wait until after the check
618            for a NULL output character. (This is necessary
619            because there may not be room for the two chars
620            needed to send a NULL.
621          */
622         outch = info->xmit_buf[info->xmit_tail];
623         if( outch ){
624             info->xmit_cnt--;
625             info->xmit_tail = (info->xmit_tail + 1)
626                                       & (PAGE_SIZE - 1);
627             base_addr[CyTDR] = outch;
628             char_count--;
629         }else{
630             if(char_count > 1){
631                 info->xmit_cnt--;
632                 info->xmit_tail = (info->xmit_tail + 1)
633                                           & (PAGE_SIZE - 1);
634                 base_addr[CyTDR] = outch;
635                 base_addr[CyTDR] = 0;
636                 char_count--;
637                 char_count--;
638             }else{
639                 break;
640             }
641         }
642     }
643
644     if (info->xmit_cnt < WAKEUP_CHARS) {
645         cy_sched_event(info, Cy_EVENT_WRITE_WAKEUP);
646     }
647     base_addr[CyTEOIR] = (char_count != saved_cnt) ? 0 : CyNOTRANS;
648     return IRQ_HANDLED;
649 } /* cy_tx_interrupt */
650
651 static irqreturn_t
652 cd2401_rx_interrupt(int irq, void *dev_id, struct pt_regs *fp)
653 {
654     struct tty_struct *tty;
655     struct cyclades_port *info;
656     volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
657     int channel;
658     char data;
659     int char_count;
660     int save_cnt;
661
662     /* determine the channel and change to that context */
663     channel = (u_short ) (base_addr[CyLICR] >> 2);
664     info = &cy_port[channel];
665     info->last_active = jiffies;
666     save_cnt = char_count = base_addr[CyRFOC];
667
668 #ifdef CONFIG_REMOTE_DEBUG
669     if (channel == DEBUG_PORT) {
670         while (char_count--) {
671             data = base_addr[CyRDR];
672             queueDebugChar(data);
673         }
674     }
675     else
676 #endif
677     /* if there is nowhere to put the data, discard it */
678     if(info->tty == 0){
679         while(char_count--){
680             data = base_addr[CyRDR];
681         }
682     }else{ /* there is an open port for this data */
683         tty = info->tty;
684         /* load # characters available from the chip */
685
686 #ifdef CYCLOM_ENABLE_MONITORING
687         ++info->mon.int_count;
688         info->mon.char_count += char_count;
689         if (char_count > info->mon.char_max)
690             info->mon.char_max = char_count;
691         info->mon.char_last = char_count;
692 #endif
693         while(char_count--){
694             data = base_addr[CyRDR];
695             if (tty->flip.count >= TTY_FLIPBUF_SIZE){
696                 continue;
697             }
698             tty->flip.count++;
699             *tty->flip.flag_buf_ptr++ = TTY_NORMAL;
700             *tty->flip.char_buf_ptr++ = data;
701 #ifdef CYCLOM_16Y_HACK
702             udelay(10L);
703 #endif
704         }
705         queue_task(&tty->flip.tqueue, &tq_timer);
706     }
707     /* end of service */
708     base_addr[CyREOIR] = save_cnt ? 0 : CyNOTRANS;
709     return IRQ_HANDLED;
710 } /* cy_rx_interrupt */
711
712 /*
713  * This routine is used to handle the "bottom half" processing for the
714  * serial driver, known also the "software interrupt" processing.
715  * This processing is done at the kernel interrupt level, after the
716  * cy_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON.  This
717  * is where time-consuming activities which can not be done in the
718  * interrupt driver proper are done; the interrupt driver schedules
719  * them using cy_sched_event(), and they get done here.
720  *
721  * This is done through one level of indirection--the task queue.
722  * When a hardware interrupt service routine wants service by the
723  * driver's bottom half, it enqueues the appropriate tq_struct (one
724  * per port) to the tq_cyclades work queue and sets a request flag
725  * via mark_bh for processing that queue.  When the time is right,
726  * do_cyclades_bh is called (because of the mark_bh) and it requests
727  * that the work queue be processed.
728  *
729  * Although this may seem unwieldy, it gives the system a way to
730  * pass an argument (in this case the pointer to the cyclades_port
731  * structure) to the bottom half of the driver.  Previous kernels
732  * had to poll every port to see if that port needed servicing.
733  */
734 static void
735 do_cyclades_bh(void)
736 {
737     run_task_queue(&tq_cyclades);
738 } /* do_cyclades_bh */
739
740 static void
741 do_softint(void *private_)
742 {
743   struct cyclades_port *info = (struct cyclades_port *) private_;
744   struct tty_struct    *tty;
745
746     tty = info->tty;
747     if (!tty)
748         return;
749
750     if (test_and_clear_bit(Cy_EVENT_HANGUP, &info->event)) {
751         tty_hangup(info->tty);
752         wake_up_interruptible(&info->open_wait);
753         info->flags &= ~ASYNC_NORMAL_ACTIVE;
754     }
755     if (test_and_clear_bit(Cy_EVENT_OPEN_WAKEUP, &info->event)) {
756         wake_up_interruptible(&info->open_wait);
757     }
758     if (test_and_clear_bit(Cy_EVENT_WRITE_WAKEUP, &info->event)) {
759         tty_wakeup(tty);
760     }
761 } /* do_softint */
762
763
764 /* This is called whenever a port becomes active;
765    interrupts are enabled and DTR & RTS are turned on.
766  */
767 static int
768 startup(struct cyclades_port * info)
769 {
770   unsigned long flags;
771   volatile unsigned char *base_addr = (unsigned char *)BASE_ADDR;
772   int channel;
773
774     if (info->flags & ASYNC_INITIALIZED){
775         return 0;
776     }
777
778     if (!info->type){
779         if (info->tty){
780             set_bit(TTY_IO_ERROR, &info->tty->flags);
781         }
782         return 0;
783     }
784     if (!info->xmit_buf){
785         info->xmit_buf = (unsigned char *) get_zeroed_page (GFP_KERNEL);
786         if (!info->xmit_buf){
787             return -ENOMEM;
788         }
789     }
790
791     config_setup(info);
792
793     channel = info->line;
794
795 #ifdef SERIAL_DEBUG_OPEN
796     printk("startup channel %d\n", channel);
797 #endif
798
799     local_irq_save(flags);
800         base_addr[CyCAR] = (u_char)channel;
801         write_cy_cmd(base_addr,CyENB_RCVR|CyENB_XMTR);
802
803         base_addr[CyCAR] = (u_char)channel; /* !!! Is this needed? */
804         base_addr[CyMSVR1] = CyRTS;
805 /* CP('S');CP('1'); */
806         base_addr[CyMSVR2] = CyDTR;
807
808 #ifdef SERIAL_DEBUG_DTR
809         printk("cyc: %d: raising DTR\n", __LINE__);
810         printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1], base_addr[CyMSVR2]);
811 #endif
812
813         base_addr[CyIER] |= CyRxData;
814         info->flags |= ASYNC_INITIALIZED;
815
816         if (info->tty){
817             clear_bit(TTY_IO_ERROR, &info->tty->flags);
818         }
819         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
820
821     local_irq_restore(flags);
822
823 #ifdef SERIAL_DEBUG_OPEN
824     printk(" done\n");
825 #endif
826     return 0;
827 } /* startup */
828
829 void
830 start_xmit( struct cyclades_port *info )
831 {
832   unsigned long flags;
833   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
834   int channel;
835
836     channel = info->line;
837     local_irq_save(flags);
838         base_addr[CyCAR] = channel;
839         base_addr[CyIER] |= CyTxMpty;
840     local_irq_restore(flags);
841 } /* start_xmit */
842
843 /*
844  * This routine shuts down a serial port; interrupts are disabled,
845  * and DTR is dropped if the hangup on close termio flag is on.
846  */
847 static void
848 shutdown(struct cyclades_port * info)
849 {
850   unsigned long flags;
851   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
852   int channel;
853
854     if (!(info->flags & ASYNC_INITIALIZED)){
855 /* CP('$'); */
856         return;
857     }
858
859     channel = info->line;
860
861 #ifdef SERIAL_DEBUG_OPEN
862     printk("shutdown channel %d\n", channel);
863 #endif
864
865     /* !!! REALLY MUST WAIT FOR LAST CHARACTER TO BE
866        SENT BEFORE DROPPING THE LINE !!!  (Perhaps
867        set some flag that is read when XMTY happens.)
868        Other choices are to delay some fixed interval
869        or schedule some later processing.
870      */
871     local_irq_save(flags);
872         if (info->xmit_buf){
873             free_page((unsigned long) info->xmit_buf);
874             info->xmit_buf = 0;
875         }
876
877         base_addr[CyCAR] = (u_char)channel;
878         if (!info->tty || (info->tty->termios->c_cflag & HUPCL)) {
879             base_addr[CyMSVR1] = 0;
880 /* CP('C');CP('1'); */
881             base_addr[CyMSVR2] = 0;
882 #ifdef SERIAL_DEBUG_DTR
883             printk("cyc: %d: dropping DTR\n", __LINE__);
884             printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1], base_addr[CyMSVR2]);
885 #endif
886         }
887         write_cy_cmd(base_addr,CyDIS_RCVR);
888          /* it may be appropriate to clear _XMIT at
889            some later date (after testing)!!! */
890
891         if (info->tty){
892             set_bit(TTY_IO_ERROR, &info->tty->flags);
893         }
894         info->flags &= ~ASYNC_INITIALIZED;
895     local_irq_restore(flags);
896
897 #ifdef SERIAL_DEBUG_OPEN
898     printk(" done\n");
899 #endif
900     return;
901 } /* shutdown */
902
903 /*
904  * This routine finds or computes the various line characteristics.
905  */
906 static void
907 config_setup(struct cyclades_port * info)
908 {
909   unsigned long flags;
910   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
911   int channel;
912   unsigned cflag;
913   int   i;
914   unsigned char ti, need_init_chan = 0;
915
916     if (!info->tty || !info->tty->termios){
917         return;
918     }
919     if (info->line == -1){
920         return;
921     }
922     cflag = info->tty->termios->c_cflag;
923
924     /* baud rate */
925     i = cflag & CBAUD;
926 #ifdef CBAUDEX
927 /* Starting with kernel 1.1.65, there is direct support for
928    higher baud rates.  The following code supports those
929    changes.  The conditional aspect allows this driver to be
930    used for earlier as well as later kernel versions.  (The
931    mapping is slightly different from serial.c because there
932    is still the possibility of supporting 75 kbit/sec with
933    the Cyclades board.)
934  */
935     if (i & CBAUDEX) {
936         if (i == B57600)
937             i = 16;
938         else if(i == B115200) 
939             i = 18;
940 #ifdef B78600
941         else if(i == B78600) 
942             i = 17;
943 #endif
944         else
945             info->tty->termios->c_cflag &= ~CBAUDEX;
946     }
947 #endif
948     if (i == 15) {
949             if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_HI)
950                     i += 1;
951             if ((info->flags & ASYNC_SPD_MASK) == ASYNC_SPD_VHI)
952                     i += 3;
953     }
954     /* Don't ever change the speed of the console port.  It will
955      * run at the speed specified in bootinfo, or at 19.2K */
956     /* Actually, it should run at whatever speed 166Bug was using */
957     /* Note info->timeout isn't used at present */
958     if (info != serial_console_info) {
959         info->tbpr = baud_bpr[i]; /* Tx BPR */
960         info->tco = baud_co[i]; /* Tx CO */
961         info->rbpr = baud_bpr[i]; /* Rx BPR */
962         info->rco = baud_co[i] >> 5; /* Rx CO */
963         if (baud_table[i] == 134) {
964             info->timeout = (info->xmit_fifo_size*HZ*30/269) + 2;
965             /* get it right for 134.5 baud */
966         } else if (baud_table[i]) {
967             info->timeout = (info->xmit_fifo_size*HZ*15/baud_table[i]) + 2;
968         /* this needs to be propagated into the card info */
969         } else {
970             info->timeout = 0;
971         }
972     }
973     /* By tradition (is it a standard?) a baud rate of zero
974        implies the line should be/has been closed.  A bit
975        later in this routine such a test is performed. */
976
977     /* byte size and parity */
978     info->cor7 = 0;
979     info->cor6 = 0;
980     info->cor5 = 0;
981     info->cor4 = (info->default_threshold
982                   ? info->default_threshold
983                   : baud_cor4[i]); /* receive threshold */
984     /* Following two lines added 101295, RGH. */
985     /* It is obviously wrong to access CyCORx, and not info->corx here,
986      * try and remember to fix it later! */
987     channel = info->line;
988     base_addr[CyCAR] = (u_char)channel;
989     if (C_CLOCAL(info->tty)) {
990         if (base_addr[CyIER] & CyMdmCh)
991             base_addr[CyIER] &= ~CyMdmCh; /* without modem intr */
992                                /* ignore 1->0 modem transitions */
993         if (base_addr[CyCOR4] & (CyDSR|CyCTS|CyDCD))
994             base_addr[CyCOR4] &= ~(CyDSR|CyCTS|CyDCD);
995                                /* ignore 0->1 modem transitions */
996         if (base_addr[CyCOR5] & (CyDSR|CyCTS|CyDCD))
997             base_addr[CyCOR5] &= ~(CyDSR|CyCTS|CyDCD);
998     } else {
999         if ((base_addr[CyIER] & CyMdmCh) != CyMdmCh)
1000             base_addr[CyIER] |= CyMdmCh; /* with modem intr */
1001                                /* act on 1->0 modem transitions */
1002         if ((base_addr[CyCOR4] & (CyDSR|CyCTS|CyDCD)) != (CyDSR|CyCTS|CyDCD))
1003             base_addr[CyCOR4] |= CyDSR|CyCTS|CyDCD;
1004                                /* act on 0->1 modem transitions */
1005         if ((base_addr[CyCOR5] & (CyDSR|CyCTS|CyDCD)) != (CyDSR|CyCTS|CyDCD))
1006             base_addr[CyCOR5] |= CyDSR|CyCTS|CyDCD;
1007     }
1008     info->cor3 = (cflag & CSTOPB) ? Cy_2_STOP : Cy_1_STOP;
1009     info->cor2 = CyETC;
1010     switch(cflag & CSIZE){
1011     case CS5:
1012         info->cor1 = Cy_5_BITS;
1013         break;
1014     case CS6:
1015         info->cor1 = Cy_6_BITS;
1016         break;
1017     case CS7:
1018         info->cor1 = Cy_7_BITS;
1019         break;
1020     case CS8:
1021         info->cor1 = Cy_8_BITS;
1022         break;
1023     }
1024     if (cflag & PARENB){
1025         if (cflag & PARODD){
1026             info->cor1 |= CyPARITY_O;
1027         }else{
1028             info->cor1 |= CyPARITY_E;
1029         }
1030     }else{
1031         info->cor1 |= CyPARITY_NONE;
1032     }
1033         
1034     /* CTS flow control flag */
1035 #if 0
1036     /* Don't complcate matters for now! RGH 141095 */
1037     if (cflag & CRTSCTS){
1038         info->flags |= ASYNC_CTS_FLOW;
1039         info->cor2 |= CyCtsAE;
1040     }else{
1041         info->flags &= ~ASYNC_CTS_FLOW;
1042         info->cor2 &= ~CyCtsAE;
1043     }
1044 #endif
1045     if (cflag & CLOCAL)
1046         info->flags &= ~ASYNC_CHECK_CD;
1047     else
1048         info->flags |= ASYNC_CHECK_CD;
1049
1050      /***********************************************
1051         The hardware option, CyRtsAO, presents RTS when
1052         the chip has characters to send.  Since most modems
1053         use RTS as reverse (inbound) flow control, this
1054         option is not used.  If inbound flow control is
1055         necessary, DTR can be programmed to provide the
1056         appropriate signals for use with a non-standard
1057         cable.  Contact Marcio Saito for details.
1058      ***********************************************/
1059
1060     channel = info->line;
1061
1062     local_irq_save(flags);
1063         base_addr[CyCAR] = (u_char)channel;
1064
1065         /* CyCMR set once only in mvme167_init_serial() */
1066         if (base_addr[CyLICR] != channel << 2)
1067             base_addr[CyLICR] = channel << 2;
1068         if (base_addr[CyLIVR] != 0x5c)
1069             base_addr[CyLIVR] = 0x5c;
1070
1071        /* tx and rx baud rate */
1072
1073         if (base_addr[CyCOR1] != info->cor1)
1074             need_init_chan = 1;
1075         if (base_addr[CyTCOR] != info->tco)
1076             base_addr[CyTCOR] = info->tco;
1077         if (base_addr[CyTBPR] != info->tbpr)
1078             base_addr[CyTBPR] = info->tbpr;
1079         if (base_addr[CyRCOR] != info->rco)
1080             base_addr[CyRCOR] = info->rco;
1081         if (base_addr[CyRBPR] != info->rbpr)
1082             base_addr[CyRBPR] = info->rbpr;
1083
1084         /* set line characteristics  according configuration */
1085
1086         if (base_addr[CySCHR1] != START_CHAR(info->tty))
1087             base_addr[CySCHR1] = START_CHAR(info->tty);
1088         if (base_addr[CySCHR2] != STOP_CHAR(info->tty))
1089             base_addr[CySCHR2] = STOP_CHAR(info->tty);
1090         if (base_addr[CySCRL] != START_CHAR(info->tty))
1091             base_addr[CySCRL] = START_CHAR(info->tty);
1092         if (base_addr[CySCRH] != START_CHAR(info->tty))
1093             base_addr[CySCRH] = START_CHAR(info->tty);
1094         if (base_addr[CyCOR1] != info->cor1)
1095             base_addr[CyCOR1] = info->cor1;
1096         if (base_addr[CyCOR2] != info->cor2)
1097             base_addr[CyCOR2] = info->cor2;
1098         if (base_addr[CyCOR3] != info->cor3)
1099             base_addr[CyCOR3] = info->cor3;
1100         if (base_addr[CyCOR4] != info->cor4)
1101             base_addr[CyCOR4] = info->cor4;
1102         if (base_addr[CyCOR5] != info->cor5)
1103             base_addr[CyCOR5] = info->cor5;
1104         if (base_addr[CyCOR6] != info->cor6)
1105             base_addr[CyCOR6] = info->cor6;
1106         if (base_addr[CyCOR7] != info->cor7)
1107             base_addr[CyCOR7] = info->cor7;
1108
1109         if (need_init_chan)
1110             write_cy_cmd(base_addr,CyINIT_CHAN);
1111
1112         base_addr[CyCAR] = (u_char)channel; /* !!! Is this needed? */
1113
1114         /* 2ms default rx timeout */
1115         ti = info->default_timeout ? info->default_timeout : 0x02;
1116         if (base_addr[CyRTPRL] != ti)
1117             base_addr[CyRTPRL] = ti;
1118         if (base_addr[CyRTPRH] != 0)
1119             base_addr[CyRTPRH] = 0;
1120
1121         /* Set up RTS here also ????? RGH 141095 */
1122         if(i == 0){ /* baud rate is zero, turn off line */
1123             if ((base_addr[CyMSVR2] & CyDTR) == CyDTR)
1124                 base_addr[CyMSVR2] = 0;
1125 #ifdef SERIAL_DEBUG_DTR
1126             printk("cyc: %d: dropping DTR\n", __LINE__);
1127             printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1], base_addr[CyMSVR2]);
1128 #endif
1129         }else{
1130             if ((base_addr[CyMSVR2] & CyDTR) != CyDTR)
1131                 base_addr[CyMSVR2] = CyDTR;
1132 #ifdef SERIAL_DEBUG_DTR
1133             printk("cyc: %d: raising DTR\n", __LINE__);
1134             printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1], base_addr[CyMSVR2]);
1135 #endif
1136         }
1137
1138         if (info->tty){
1139             clear_bit(TTY_IO_ERROR, &info->tty->flags);
1140         }
1141
1142     local_irq_restore(flags);
1143
1144 } /* config_setup */
1145
1146
1147 static void
1148 cy_put_char(struct tty_struct *tty, unsigned char ch)
1149 {
1150   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1151   unsigned long flags;
1152
1153 #ifdef SERIAL_DEBUG_IO
1154     printk("cy_put_char %s(0x%02x)\n", tty->name, ch);
1155 #endif
1156
1157     if (serial_paranoia_check(info, tty->name, "cy_put_char"))
1158         return;
1159
1160     if (!tty || !info->xmit_buf)
1161         return;
1162
1163     local_irq_save(flags);
1164         if (info->xmit_cnt >= PAGE_SIZE - 1) {
1165             local_irq_restore(flags);
1166             return;
1167         }
1168
1169         info->xmit_buf[info->xmit_head++] = ch;
1170         info->xmit_head &= PAGE_SIZE - 1;
1171         info->xmit_cnt++;
1172     local_irq_restore(flags);
1173 } /* cy_put_char */
1174
1175
1176 static void
1177 cy_flush_chars(struct tty_struct *tty)
1178 {
1179   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1180   unsigned long flags;
1181   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1182   int channel;
1183                                 
1184 #ifdef SERIAL_DEBUG_IO
1185     printk("cy_flush_chars %s\n", tty->name); /* */
1186 #endif
1187
1188     if (serial_paranoia_check(info, tty->name, "cy_flush_chars"))
1189         return;
1190
1191     if (info->xmit_cnt <= 0 || tty->stopped
1192     || tty->hw_stopped || !info->xmit_buf)
1193         return;
1194
1195     channel = info->line;
1196
1197     local_irq_save(flags);
1198         base_addr[CyCAR] = channel;
1199         base_addr[CyIER] |= CyTxMpty;
1200     local_irq_restore(flags);
1201 } /* cy_flush_chars */
1202
1203
1204 /* This routine gets called when tty_write has put something into
1205     the write_queue.  If the port is not already transmitting stuff,
1206     start it off by enabling interrupts.  The interrupt service
1207     routine will then ensure that the characters are sent.  If the
1208     port is already active, there is no need to kick it.
1209  */
1210 static int
1211 cy_write(struct tty_struct * tty, int from_user,
1212            const unsigned char *buf, int count)
1213 {
1214   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1215   unsigned long flags;
1216   int c, total = 0;
1217
1218 #ifdef SERIAL_DEBUG_IO
1219     printk("cy_write %s\n", tty->name); /* */
1220 #endif
1221
1222     if (serial_paranoia_check(info, tty->name, "cy_write")){
1223         return 0;
1224     }
1225         
1226     if (!tty || !info->xmit_buf || !tmp_buf){
1227         return 0;
1228     }
1229
1230     if (from_user) {
1231             down(&tmp_buf_sem);
1232             while (1) {
1233                     c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1234                                               SERIAL_XMIT_SIZE - info->xmit_head));
1235                     if (c <= 0)
1236                             break;
1237
1238                     c -= copy_from_user(tmp_buf, buf, c);
1239                     if (!c) {
1240                             if (!total)
1241                                     total = -EFAULT;
1242                             break;
1243                     }
1244
1245                     local_irq_save(flags);
1246                     c = min_t(int, c, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1247                                           SERIAL_XMIT_SIZE - info->xmit_head));
1248                     memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
1249                     info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1250                     info->xmit_cnt += c;
1251                     local_irq_restore(flags);
1252
1253                     buf += c;
1254                     count -= c;
1255                     total += c;
1256             }
1257             up(&tmp_buf_sem);
1258     } else {
1259             while (1) {
1260                     local_irq_save(flags);
1261                     c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
1262                                               SERIAL_XMIT_SIZE - info->xmit_head));
1263                     if (c <= 0) {
1264                             local_irq_restore(flags);
1265                             break;
1266                     }
1267
1268                     memcpy(info->xmit_buf + info->xmit_head, buf, c);
1269                     info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1270                     info->xmit_cnt += c;
1271                     local_irq_restore(flags);
1272
1273                     buf += c;
1274                     count -= c;
1275                     total += c;
1276             }
1277     }
1278
1279     if (info->xmit_cnt
1280     && !tty->stopped
1281     && !tty->hw_stopped ) {
1282         start_xmit(info);
1283     }
1284     return total;
1285 } /* cy_write */
1286
1287
1288 static int
1289 cy_write_room(struct tty_struct *tty)
1290 {
1291   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1292   int   ret;
1293                                 
1294 #ifdef SERIAL_DEBUG_IO
1295     printk("cy_write_room %s\n", tty->name); /* */
1296 #endif
1297
1298     if (serial_paranoia_check(info, tty->name, "cy_write_room"))
1299         return 0;
1300     ret = PAGE_SIZE - info->xmit_cnt - 1;
1301     if (ret < 0)
1302         ret = 0;
1303     return ret;
1304 } /* cy_write_room */
1305
1306
1307 static int
1308 cy_chars_in_buffer(struct tty_struct *tty)
1309 {
1310   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1311                                 
1312 #ifdef SERIAL_DEBUG_IO
1313     printk("cy_chars_in_buffer %s %d\n", tty->name, info->xmit_cnt); /* */
1314 #endif
1315
1316     if (serial_paranoia_check(info, tty->name, "cy_chars_in_buffer"))
1317         return 0;
1318
1319     return info->xmit_cnt;
1320 } /* cy_chars_in_buffer */
1321
1322
1323 static void
1324 cy_flush_buffer(struct tty_struct *tty)
1325 {
1326   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1327   unsigned long flags;
1328                                 
1329 #ifdef SERIAL_DEBUG_IO
1330     printk("cy_flush_buffer %s\n", tty->name); /* */
1331 #endif
1332
1333     if (serial_paranoia_check(info, tty->name, "cy_flush_buffer"))
1334         return;
1335     local_irq_save(flags);
1336         info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1337     local_irq_restore(flags);
1338     tty_wakeup(tty);
1339 } /* cy_flush_buffer */
1340
1341
1342 /* This routine is called by the upper-layer tty layer to signal
1343    that incoming characters should be throttled or that the
1344    throttle should be released.
1345  */
1346 static void
1347 cy_throttle(struct tty_struct * tty)
1348 {
1349   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1350   unsigned long flags;
1351   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1352   int channel;
1353
1354 #ifdef SERIAL_DEBUG_THROTTLE
1355   char buf[64];
1356         
1357     printk("throttle %s: %d....\n", tty_name(tty, buf),
1358            tty->ldisc.chars_in_buffer(tty));
1359     printk("cy_throttle %s\n", tty->name);
1360 #endif
1361
1362     if (serial_paranoia_check(info, tty->name, "cy_nthrottle")){
1363             return;
1364     }
1365
1366     if (I_IXOFF(tty)) {
1367         info->x_char = STOP_CHAR(tty);
1368             /* Should use the "Send Special Character" feature!!! */
1369     }
1370
1371     channel = info->line;
1372
1373     local_irq_save(flags);
1374         base_addr[CyCAR] = (u_char)channel;
1375         base_addr[CyMSVR1] = 0;
1376     local_irq_restore(flags);
1377
1378     return;
1379 } /* cy_throttle */
1380
1381
1382 static void
1383 cy_unthrottle(struct tty_struct * tty)
1384 {
1385   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1386   unsigned long flags;
1387   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1388   int channel;
1389
1390 #ifdef SERIAL_DEBUG_THROTTLE
1391   char buf[64];
1392         
1393     printk("throttle %s: %d....\n", tty_name(tty, buf),
1394            tty->ldisc.chars_in_buffer(tty));
1395     printk("cy_unthrottle %s\n", tty->name);
1396 #endif
1397
1398     if (serial_paranoia_check(info, tty->name, "cy_nthrottle")){
1399             return;
1400     }
1401
1402     if (I_IXOFF(tty)) {
1403         info->x_char = START_CHAR(tty);
1404         /* Should use the "Send Special Character" feature!!! */
1405     }
1406
1407     channel = info->line;
1408
1409     local_irq_save(flags);
1410         base_addr[CyCAR] = (u_char)channel;
1411         base_addr[CyMSVR1] = CyRTS;
1412     local_irq_restore(flags);
1413
1414     return;
1415 } /* cy_unthrottle */
1416
1417 static int
1418 get_serial_info(struct cyclades_port * info,
1419                            struct serial_struct * retinfo)
1420 {
1421   struct serial_struct tmp;
1422
1423 /* CP('g'); */
1424     if (!retinfo)
1425             return -EFAULT;
1426     memset(&tmp, 0, sizeof(tmp));
1427     tmp.type = info->type;
1428     tmp.line = info->line;
1429     tmp.port = info->line;
1430     tmp.irq = 0;
1431     tmp.flags = info->flags;
1432     tmp.baud_base = 0;          /*!!!*/
1433     tmp.close_delay = info->close_delay;
1434     tmp.custom_divisor = 0;     /*!!!*/
1435     tmp.hub6 = 0;               /*!!!*/
1436     return copy_to_user(retinfo,&tmp,sizeof(*retinfo)) ? -EFAULT : 0;
1437 } /* get_serial_info */
1438
1439 static int
1440 set_serial_info(struct cyclades_port * info,
1441                            struct serial_struct * new_info)
1442 {
1443   struct serial_struct new_serial;
1444   struct cyclades_port old_info;
1445
1446 /* CP('s'); */
1447     if (!new_info)
1448             return -EFAULT;
1449     if (copy_from_user(&new_serial,new_info,sizeof(new_serial)))
1450             return -EFAULT;
1451     old_info = *info;
1452
1453     if (!capable(CAP_SYS_ADMIN)) {
1454             if ((new_serial.close_delay != info->close_delay) ||
1455                 ((new_serial.flags & ASYNC_FLAGS & ~ASYNC_USR_MASK) !=
1456                  (info->flags & ASYNC_FLAGS & ~ASYNC_USR_MASK)))
1457                     return -EPERM;
1458             info->flags = ((info->flags & ~ASYNC_USR_MASK) |
1459                            (new_serial.flags & ASYNC_USR_MASK));
1460             goto check_and_exit;
1461     }
1462
1463
1464     /*
1465      * OK, past this point, all the error checking has been done.
1466      * At this point, we start making changes.....
1467      */
1468
1469     info->flags = ((info->flags & ~ASYNC_FLAGS) |
1470                     (new_serial.flags & ASYNC_FLAGS));
1471     info->close_delay = new_serial.close_delay;
1472
1473
1474 check_and_exit:
1475     if (info->flags & ASYNC_INITIALIZED){
1476         config_setup(info);
1477         return 0;
1478     }else{
1479         return startup(info);
1480     }
1481 } /* set_serial_info */
1482
1483 static int
1484 cy_tiocmget(struct tty_struct *tty, struct file *file)
1485 {
1486   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
1487   int channel;
1488   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1489   unsigned long flags;
1490   unsigned char status;
1491   unsigned int result;
1492
1493     channel = info->line;
1494
1495     local_irq_save(flags);
1496         base_addr[CyCAR] = (u_char)channel;
1497         status = base_addr[CyMSVR1] | base_addr[CyMSVR2];
1498     local_irq_restore(flags);
1499
1500     return    ((status  & CyRTS) ? TIOCM_RTS : 0)
1501             | ((status  & CyDTR) ? TIOCM_DTR : 0)
1502             | ((status  & CyDCD) ? TIOCM_CAR : 0)
1503             | ((status  & CyDSR) ? TIOCM_DSR : 0)
1504             | ((status  & CyCTS) ? TIOCM_CTS : 0);
1505 } /* cy_tiocmget */
1506
1507 static int
1508 cy_tiocmset(struct tty_struct *tty, struct file *file,
1509             unsigned int set, unsigned int clear)
1510 {
1511   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
1512   int channel;
1513   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1514   unsigned long flags;
1515   unsigned int arg;
1516           
1517     channel = info->line;
1518
1519         if (set & TIOCM_RTS){
1520             local_irq_save(flags);
1521                 base_addr[CyCAR] = (u_char)channel;
1522                 base_addr[CyMSVR1] = CyRTS;
1523             local_irq_restore(flags);
1524         }
1525         if (set & TIOCM_DTR){
1526             local_irq_save(flags);
1527             base_addr[CyCAR] = (u_char)channel;
1528 /* CP('S');CP('2'); */
1529             base_addr[CyMSVR2] = CyDTR;
1530 #ifdef SERIAL_DEBUG_DTR
1531             printk("cyc: %d: raising DTR\n", __LINE__);
1532             printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1], base_addr[CyMSVR2]);
1533 #endif
1534             local_irq_restore(flags);
1535         }
1536
1537         if (clear & TIOCM_RTS){
1538             local_irq_save(flags);
1539                 base_addr[CyCAR] = (u_char)channel;
1540                 base_addr[CyMSVR1] = 0;
1541             local_irq_restore(flags);
1542         }
1543         if (clear & TIOCM_DTR){
1544             local_irq_save(flags);
1545             base_addr[CyCAR] = (u_char)channel;
1546 /* CP('C');CP('2'); */
1547             base_addr[CyMSVR2] = 0;
1548 #ifdef SERIAL_DEBUG_DTR
1549             printk("cyc: %d: dropping DTR\n", __LINE__);
1550             printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1], base_addr[CyMSVR2]);
1551 #endif
1552             local_irq_restore(flags);
1553         }
1554
1555     return 0;
1556 } /* set_modem_info */
1557
1558 static void
1559 send_break( struct cyclades_port * info, int duration)
1560 { /* Let the transmit ISR take care of this (since it
1561      requires stuffing characters into the output stream).
1562    */
1563     info->x_break = duration;
1564     if (!info->xmit_cnt ) {
1565         start_xmit(info);
1566     }
1567 } /* send_break */
1568
1569 static int
1570 get_mon_info(struct cyclades_port * info, struct cyclades_monitor * mon)
1571 {
1572
1573    if (copy_to_user(mon, &info->mon, sizeof(struct cyclades_monitor)))
1574            return -EFAULT;
1575    info->mon.int_count  = 0;
1576    info->mon.char_count = 0;
1577    info->mon.char_max   = 0;
1578    info->mon.char_last  = 0;
1579    return 0;
1580 }
1581
1582 static int
1583 set_threshold(struct cyclades_port * info, unsigned long *arg)
1584 {
1585    volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1586    unsigned long value;
1587    int channel;
1588    
1589    if (get_user(value, arg))
1590            return -EFAULT;
1591
1592    channel = info->line;
1593    info->cor4 &= ~CyREC_FIFO;
1594    info->cor4 |= value & CyREC_FIFO;
1595    base_addr[CyCOR4] = info->cor4;
1596    return 0;
1597 }
1598
1599 static int
1600 get_threshold(struct cyclades_port * info, unsigned long *value)
1601 {
1602    volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1603    int channel;
1604    unsigned long tmp;
1605    
1606    channel = info->line;
1607
1608    tmp = base_addr[CyCOR4] & CyREC_FIFO;
1609    return put_user(tmp,value);
1610 }
1611
1612 static int
1613 set_default_threshold(struct cyclades_port * info, unsigned long *arg)
1614 {
1615    unsigned long value;
1616
1617    if (get_user(value, arg))
1618         return -EFAULT;
1619
1620    info->default_threshold = value & 0x0f;
1621    return 0;
1622 }
1623
1624 static int
1625 get_default_threshold(struct cyclades_port * info, unsigned long *value)
1626 {
1627    return put_user(info->default_threshold,value);
1628 }
1629
1630 static int
1631 set_timeout(struct cyclades_port * info, unsigned long *arg)
1632 {
1633    volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1634    int channel;
1635    unsigned long value;
1636
1637    if (get_user(value, arg))
1638            return -EFAULT;
1639    
1640    channel = info->line;
1641
1642    base_addr[CyRTPRL] = value & 0xff;
1643    base_addr[CyRTPRH] = (value >> 8) & 0xff;
1644    return 0;
1645 }
1646
1647 static int
1648 get_timeout(struct cyclades_port * info, unsigned long *value)
1649 {
1650    volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
1651    int channel;
1652    unsigned long tmp;
1653    
1654    channel = info->line;
1655
1656    tmp = base_addr[CyRTPRL];
1657    return put_user(tmp,value);
1658 }
1659
1660 static int
1661 set_default_timeout(struct cyclades_port * info, unsigned long value)
1662 {
1663    info->default_timeout = value & 0xff;
1664    return 0;
1665 }
1666
1667 static int
1668 get_default_timeout(struct cyclades_port * info, unsigned long *value)
1669 {
1670    return put_user(info->default_timeout,value);
1671 }
1672
1673 static int
1674 cy_ioctl(struct tty_struct *tty, struct file * file,
1675             unsigned int cmd, unsigned long arg)
1676 {
1677   unsigned long val;
1678   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
1679   int ret_val = 0;
1680
1681 #ifdef SERIAL_DEBUG_OTHER
1682     printk("cy_ioctl %s, cmd = %x arg = %lx\n", tty->name, cmd, arg); /* */
1683 #endif
1684
1685     switch (cmd) {
1686         case CYGETMON:
1687             ret_val = get_mon_info(info, (struct cyclades_monitor *)arg);
1688             break;
1689         case CYGETTHRESH:
1690             ret_val = get_threshold(info, (unsigned long *)arg);
1691             break;
1692         case CYSETTHRESH:
1693             ret_val = set_threshold(info, (unsigned long *)arg);
1694             break;
1695         case CYGETDEFTHRESH:
1696             ret_val = get_default_threshold(info, (unsigned long *)arg);
1697             break;
1698         case CYSETDEFTHRESH:
1699             ret_val = set_default_threshold(info, (unsigned long *)arg);
1700             break;
1701         case CYGETTIMEOUT:
1702             ret_val = get_timeout(info, (unsigned long *)arg);
1703             break;
1704         case CYSETTIMEOUT:
1705             ret_val = set_timeout(info, (unsigned long *)arg);
1706             break;
1707         case CYGETDEFTIMEOUT:
1708             ret_val = get_default_timeout(info, (unsigned long *)arg);
1709             break;
1710         case CYSETDEFTIMEOUT:
1711             ret_val = set_default_timeout(info, (unsigned long)arg);
1712             break;
1713         case TCSBRK:    /* SVID version: non-zero arg --> no break */
1714             ret_val = tty_check_change(tty);
1715             if (ret_val)
1716                     break;
1717             tty_wait_until_sent(tty,0);
1718             if (!arg)
1719                 send_break(info, HZ/4); /* 1/4 second */
1720             break;
1721         case TCSBRKP:   /* support for POSIX tcsendbreak() */
1722             ret_val = tty_check_change(tty);
1723             if (ret_val)
1724                 break;
1725             tty_wait_until_sent(tty,0);
1726             send_break(info, arg ? arg*(HZ/10) : HZ/4);
1727             break;
1728
1729 /* The following commands are incompletely implemented!!! */
1730         case TIOCGSOFTCAR:
1731             ret_val = put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
1732             break;
1733         case TIOCSSOFTCAR:
1734             ret_val = get_user(val, (unsigned long *) arg);
1735             if (ret_val)
1736                     break;
1737             tty->termios->c_cflag =
1738                     ((tty->termios->c_cflag & ~CLOCAL) | (val ? CLOCAL : 0));
1739             break;
1740         case TIOCGSERIAL:
1741             ret_val = get_serial_info(info, (struct serial_struct *) arg);
1742             break;
1743         case TIOCSSERIAL:
1744             ret_val = set_serial_info(info,
1745                                    (struct serial_struct *) arg);
1746             break;
1747         default:
1748             ret_val = -ENOIOCTLCMD;
1749     }
1750
1751 #ifdef SERIAL_DEBUG_OTHER
1752     printk("cy_ioctl done\n");
1753 #endif
1754
1755     return ret_val;
1756 } /* cy_ioctl */
1757
1758
1759
1760
1761 static void
1762 cy_set_termios(struct tty_struct *tty, struct termios * old_termios)
1763 {
1764   struct cyclades_port *info = (struct cyclades_port *)tty->driver_data;
1765
1766 #ifdef SERIAL_DEBUG_OTHER
1767     printk("cy_set_termios %s\n", tty->name);
1768 #endif
1769
1770     if (tty->termios->c_cflag == old_termios->c_cflag)
1771         return;
1772     config_setup(info);
1773
1774     if ((old_termios->c_cflag & CRTSCTS) &&
1775         !(tty->termios->c_cflag & CRTSCTS)) {
1776             tty->stopped = 0;
1777             cy_start(tty);
1778     }
1779 #ifdef tytso_patch_94Nov25_1726
1780     if (!(old_termios->c_cflag & CLOCAL) &&
1781         (tty->termios->c_cflag & CLOCAL))
1782             wake_up_interruptible(&info->open_wait);
1783 #endif
1784
1785     return;
1786 } /* cy_set_termios */
1787
1788
1789 static void
1790 cy_close(struct tty_struct * tty, struct file * filp)
1791 {
1792   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
1793
1794 /* CP('C'); */
1795 #ifdef SERIAL_DEBUG_OTHER
1796     printk("cy_close %s\n", tty->name);
1797 #endif
1798
1799     if (!info
1800     || serial_paranoia_check(info, tty->name, "cy_close")){
1801         return;
1802     }
1803 #ifdef SERIAL_DEBUG_OPEN
1804     printk("cy_close %s, count = %d\n", tty->name, info->count);
1805 #endif
1806
1807     if ((tty->count == 1) && (info->count != 1)) {
1808         /*
1809          * Uh, oh.  tty->count is 1, which means that the tty
1810          * structure will be freed.  Info->count should always
1811          * be one in these conditions.  If it's greater than
1812          * one, we've got real problems, since it means the
1813          * serial port won't be shutdown.
1814          */
1815         printk("cy_close: bad serial port count; tty->count is 1, "
1816            "info->count is %d\n", info->count);
1817         info->count = 1;
1818     }
1819 #ifdef SERIAL_DEBUG_COUNT
1820     printk("cyc: %d: decrementing count to %d\n", __LINE__, info->count - 1);
1821 #endif
1822     if (--info->count < 0) {
1823         printk("cy_close: bad serial port count for ttys%d: %d\n",
1824                info->line, info->count);
1825 #ifdef SERIAL_DEBUG_COUNT
1826     printk("cyc: %d: setting count to 0\n", __LINE__);
1827 #endif
1828         info->count = 0;
1829     }
1830     if (info->count)
1831         return;
1832     info->flags |= ASYNC_CLOSING;
1833     if (info->flags & ASYNC_INITIALIZED)
1834         tty_wait_until_sent(tty, 3000); /* 30 seconds timeout */
1835     shutdown(info);
1836     if (tty->driver->flush_buffer)
1837         tty->driver->flush_buffer(tty);
1838     tty_ldisc_flush(tty);
1839     info->event = 0;
1840     info->tty = 0;
1841     if (info->blocked_open) {
1842         if (info->close_delay) {
1843             current->state = TASK_INTERRUPTIBLE;
1844             schedule_timeout(info->close_delay);
1845         }
1846         wake_up_interruptible(&info->open_wait);
1847     }
1848     info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1849     wake_up_interruptible(&info->close_wait);
1850
1851 #ifdef SERIAL_DEBUG_OTHER
1852     printk("cy_close done\n");
1853 #endif
1854
1855     return;
1856 } /* cy_close */
1857
1858 /*
1859  * cy_hangup() --- called by tty_hangup() when a hangup is signaled.
1860  */
1861 void
1862 cy_hangup(struct tty_struct *tty)
1863 {
1864   struct cyclades_port * info = (struct cyclades_port *)tty->driver_data;
1865         
1866 #ifdef SERIAL_DEBUG_OTHER
1867     printk("cy_hangup %s\n", tty->name); /* */
1868 #endif
1869
1870     if (serial_paranoia_check(info, tty->name, "cy_hangup"))
1871         return;
1872     
1873     shutdown(info);
1874 #if 0
1875     info->event = 0;
1876     info->count = 0;
1877 #ifdef SERIAL_DEBUG_COUNT
1878     printk("cyc: %d: setting count to 0\n", __LINE__);
1879 #endif
1880     info->tty = 0;
1881 #endif
1882     info->flags &= ~ASYNC_NORMAL_ACTIVE;
1883     wake_up_interruptible(&info->open_wait);
1884 } /* cy_hangup */
1885
1886
1887
1888 /*
1889  * ------------------------------------------------------------
1890  * cy_open() and friends
1891  * ------------------------------------------------------------
1892  */
1893
1894 static int
1895 block_til_ready(struct tty_struct *tty, struct file * filp,
1896                            struct cyclades_port *info)
1897 {
1898   DECLARE_WAITQUEUE(wait, current);
1899   unsigned long flags;
1900   int channel;
1901   int retval;
1902   volatile u_char *base_addr = (u_char *)BASE_ADDR;
1903
1904     /*
1905      * If the device is in the middle of being closed, then block
1906      * until it's done, and then try again.
1907      */
1908     if (info->flags & ASYNC_CLOSING) {
1909         interruptible_sleep_on(&info->close_wait);
1910         if (info->flags & ASYNC_HUP_NOTIFY){
1911             return -EAGAIN;
1912         }else{
1913             return -ERESTARTSYS;
1914         }
1915     }
1916
1917     /*
1918      * If non-blocking mode is set, then make the check up front
1919      * and then exit.
1920      */
1921     if (filp->f_flags & O_NONBLOCK) {
1922         info->flags |= ASYNC_NORMAL_ACTIVE;
1923         return 0;
1924     }
1925
1926     /*
1927      * Block waiting for the carrier detect and the line to become
1928      * free (i.e., not in use by the callout).  While we are in
1929      * this loop, info->count is dropped by one, so that
1930      * cy_close() knows when to free things.  We restore it upon
1931      * exit, either normal or abnormal.
1932      */
1933     retval = 0;
1934     add_wait_queue(&info->open_wait, &wait);
1935 #ifdef SERIAL_DEBUG_OPEN
1936     printk("block_til_ready before block: %s, count = %d\n",
1937            tty->name, info->count);/**/
1938 #endif
1939     info->count--;
1940 #ifdef SERIAL_DEBUG_COUNT
1941     printk("cyc: %d: decrementing count to %d\n", __LINE__, info->count);
1942 #endif
1943     info->blocked_open++;
1944
1945     channel = info->line;
1946
1947     while (1) {
1948         local_irq_save(flags);
1949         base_addr[CyCAR] = (u_char)channel;
1950         base_addr[CyMSVR1] = CyRTS;
1951 /* CP('S');CP('4'); */
1952         base_addr[CyMSVR2] = CyDTR;
1953 #ifdef SERIAL_DEBUG_DTR
1954         printk("cyc: %d: raising DTR\n", __LINE__);
1955         printk("     status: 0x%x, 0x%x\n", base_addr[CyMSVR1], base_addr[CyMSVR2]);
1956 #endif
1957         local_irq_restore(flags);
1958         set_current_state(TASK_INTERRUPTIBLE);
1959         if (tty_hung_up_p(filp)
1960         || !(info->flags & ASYNC_INITIALIZED) ){
1961             if (info->flags & ASYNC_HUP_NOTIFY) {
1962                 retval = -EAGAIN;
1963             }else{
1964                 retval = -ERESTARTSYS;
1965             }
1966             break;
1967         }
1968         local_irq_save(flags);
1969             base_addr[CyCAR] = (u_char)channel;
1970 /* CP('L');CP1(1 && C_CLOCAL(tty)); CP1(1 && (base_addr[CyMSVR1] & CyDCD) ); */
1971             if (!(info->flags & ASYNC_CLOSING)
1972             && (C_CLOCAL(tty)
1973                 || (base_addr[CyMSVR1] & CyDCD))) {
1974                     local_irq_restore(flags);
1975                     break;
1976             }
1977         local_irq_restore(flags);
1978         if (signal_pending(current)) {
1979             retval = -ERESTARTSYS;
1980             break;
1981         }
1982 #ifdef SERIAL_DEBUG_OPEN
1983         printk("block_til_ready blocking: %s, count = %d\n",
1984                tty->name, info->count);/**/
1985 #endif
1986         schedule();
1987     }
1988     current->state = TASK_RUNNING;
1989     remove_wait_queue(&info->open_wait, &wait);
1990     if (!tty_hung_up_p(filp)){
1991         info->count++;
1992 #ifdef SERIAL_DEBUG_COUNT
1993     printk("cyc: %d: incrementing count to %d\n", __LINE__, info->count);
1994 #endif
1995     }
1996     info->blocked_open--;
1997 #ifdef SERIAL_DEBUG_OPEN
1998     printk("block_til_ready after blocking: %s, count = %d\n",
1999            tty->name, info->count);/**/
2000 #endif
2001     if (retval)
2002             return retval;
2003     info->flags |= ASYNC_NORMAL_ACTIVE;
2004     return 0;
2005 } /* block_til_ready */
2006
2007 /*
2008  * This routine is called whenever a serial port is opened.  It
2009  * performs the serial-specific initialization for the tty structure.
2010  */
2011 int
2012 cy_open(struct tty_struct *tty, struct file * filp)
2013 {
2014   struct cyclades_port  *info;
2015   int retval, line;
2016
2017 /* CP('O'); */
2018     line = tty->index;
2019     if ((line < 0) || (NR_PORTS <= line)){
2020         return -ENODEV;
2021     }
2022     info = &cy_port[line];
2023     if (info->line < 0){
2024         return -ENODEV;
2025     }
2026 #ifdef SERIAL_DEBUG_OTHER
2027     printk("cy_open %s\n", tty->name); /* */
2028 #endif
2029     if (serial_paranoia_check(info, tty->name, "cy_open")){
2030         return -ENODEV;
2031     }
2032 #ifdef SERIAL_DEBUG_OPEN
2033     printk("cy_open %s, count = %d\n", tty->name, info->count);/**/
2034 #endif
2035     info->count++;
2036 #ifdef SERIAL_DEBUG_COUNT
2037     printk("cyc: %d: incrementing count to %d\n", __LINE__, info->count);
2038 #endif
2039     tty->driver_data = info;
2040     info->tty = tty;
2041
2042     if (!tmp_buf) {
2043         tmp_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
2044         if (!tmp_buf){
2045             return -ENOMEM;
2046         }
2047     }
2048
2049     /*
2050      * Start up serial port
2051      */
2052     retval = startup(info);
2053     if (retval){
2054         return retval;
2055     }
2056
2057     retval = block_til_ready(tty, filp, info);
2058     if (retval) {
2059 #ifdef SERIAL_DEBUG_OPEN
2060         printk("cy_open returning after block_til_ready with %d\n",
2061                retval);
2062 #endif
2063         return retval;
2064     }
2065
2066 #ifdef SERIAL_DEBUG_OPEN
2067     printk("cy_open done\n");/**/
2068 #endif
2069     return 0;
2070 } /* cy_open */
2071
2072
2073
2074 /*
2075  * ---------------------------------------------------------------------
2076  * serial167_init() and friends
2077  *
2078  * serial167_init() is called at boot-time to initialize the serial driver.
2079  * ---------------------------------------------------------------------
2080  */
2081
2082 /*
2083  * This routine prints out the appropriate serial driver version
2084  * number, and identifies which options were configured into this
2085  * driver.
2086  */
2087 static void
2088 show_version(void)
2089 {
2090     printk("MVME166/167 cd2401 driver\n");
2091 } /* show_version */
2092
2093 /* initialize chips on card -- return number of valid
2094    chips (which is number of ports/4) */
2095
2096 /*
2097  * This initialises the hardware to a reasonable state.  It should
2098  * probe the chip first so as to copy 166-Bug setup as a default for
2099  * port 0.  It initialises CMR to CyASYNC; that is never done again, so
2100  * as to limit the number of CyINIT_CHAN commands in normal running.
2101  *
2102  * ... I wonder what I should do if this fails ...
2103  */
2104
2105 void
2106 mvme167_serial_console_setup(int cflag)
2107 {
2108         volatile unsigned char* base_addr = (u_char *)BASE_ADDR;
2109         int ch;
2110         u_char spd;
2111         u_char rcor, rbpr, badspeed = 0;
2112         unsigned long flags;
2113
2114         local_irq_save(flags);
2115
2116         /*
2117          * First probe channel zero of the chip, to see what speed has
2118          * been selected.
2119          */
2120
2121         base_addr[CyCAR] = 0;
2122
2123         rcor = base_addr[CyRCOR] << 5;
2124         rbpr = base_addr[CyRBPR];
2125
2126         for (spd = 0; spd < sizeof(baud_bpr); spd++)
2127                 if (rbpr == baud_bpr[spd] && rcor == baud_co[spd])
2128                         break;
2129         if (spd >= sizeof(baud_bpr)) {
2130                 spd = 14;       /* 19200 */
2131                 badspeed = 1;   /* Failed to identify speed */
2132         }
2133         initial_console_speed = spd;
2134
2135         /* OK, we have chosen a speed, now reset and reinitialise */
2136
2137         my_udelay(20000L);      /* Allow time for any active o/p to complete */
2138         if(base_addr[CyCCR] != 0x00){
2139             local_irq_restore(flags);
2140             /* printk(" chip is never idle (CCR != 0)\n"); */
2141             return;
2142         }
2143
2144         base_addr[CyCCR] = CyCHIP_RESET;        /* Reset the chip */
2145         my_udelay(1000L);
2146
2147         if(base_addr[CyGFRCR] == 0x00){
2148             local_irq_restore(flags);
2149             /* printk(" chip is not responding (GFRCR stayed 0)\n"); */
2150             return;
2151         }
2152
2153         /*
2154          * System clock is 20Mhz, divided by 2048, so divide by 10 for a 1.0ms
2155          * tick
2156          */
2157
2158         base_addr[CyTPR] = 10;
2159
2160         base_addr[CyPILR1] = 0x01;    /* Interrupt level for modem change */
2161         base_addr[CyPILR2] = 0x02;    /* Interrupt level for tx ints */
2162         base_addr[CyPILR3] = 0x03;    /* Interrupt level for rx ints */
2163
2164         /*
2165          * Attempt to set up all channels to something reasonable, and
2166          * bang out a INIT_CHAN command.  We should then be able to limit
2167          * the ammount of fiddling we have to do in normal running.
2168          */
2169
2170         for (ch = 3; ch >= 0 ; ch--) {
2171                 base_addr[CyCAR] = (u_char)ch;
2172                 base_addr[CyIER] = 0;
2173                 base_addr[CyCMR] = CyASYNC;
2174                 base_addr[CyLICR] = (u_char)ch << 2;
2175                 base_addr[CyLIVR] = 0x5c;
2176                 base_addr[CyTCOR] = baud_co[spd];
2177                 base_addr[CyTBPR] = baud_bpr[spd];
2178                 base_addr[CyRCOR] = baud_co[spd] >> 5;
2179                 base_addr[CyRBPR] = baud_bpr[spd];
2180                 base_addr[CySCHR1] = 'Q' & 0x1f;
2181                 base_addr[CySCHR2] = 'X' & 0x1f;
2182                 base_addr[CySCRL] = 0;
2183                 base_addr[CySCRH] = 0;
2184                 base_addr[CyCOR1] = Cy_8_BITS | CyPARITY_NONE;
2185                 base_addr[CyCOR2] = 0;
2186                 base_addr[CyCOR3] = Cy_1_STOP;
2187                 base_addr[CyCOR4] = baud_cor4[spd];
2188                 base_addr[CyCOR5] = 0;
2189                 base_addr[CyCOR6] = 0;
2190                 base_addr[CyCOR7] = 0;
2191                 base_addr[CyRTPRL] = 2;
2192                 base_addr[CyRTPRH] = 0;
2193                 base_addr[CyMSVR1] = 0;
2194                 base_addr[CyMSVR2] = 0;
2195                 write_cy_cmd(base_addr,CyINIT_CHAN|CyDIS_RCVR|CyDIS_XMTR);
2196         }
2197
2198         /*
2199          * Now do specials for channel zero....
2200          */
2201
2202         base_addr[CyMSVR1] = CyRTS;
2203         base_addr[CyMSVR2] = CyDTR;
2204         base_addr[CyIER] = CyRxData;
2205         write_cy_cmd(base_addr,CyENB_RCVR|CyENB_XMTR);
2206
2207         local_irq_restore(flags);
2208
2209         my_udelay(20000L);      /* Let it all settle down */
2210
2211         printk("CD2401 initialised,  chip is rev 0x%02x\n", base_addr[CyGFRCR]);
2212         if (badspeed)
2213                 printk("  WARNING:  Failed to identify line speed, rcor=%02x,rbpr=%02x\n",
2214                                         rcor >> 5, rbpr);
2215 } /* serial_console_init */
2216
2217 static struct tty_operations cy_ops = {
2218         .open = cy_open,
2219         .close = cy_close,
2220         .write = cy_write,
2221         .put_char = cy_put_char,
2222         .flush_chars = cy_flush_chars,
2223         .write_room = cy_write_room,
2224         .chars_in_buffer = cy_chars_in_buffer,
2225         .flush_buffer = cy_flush_buffer,
2226         .ioctl = cy_ioctl,
2227         .throttle = cy_throttle,
2228         .unthrottle = cy_unthrottle,
2229         .set_termios = cy_set_termios,
2230         .stop = cy_stop,
2231         .start = cy_start,
2232         .hangup = cy_hangup,
2233         .tiocmget = cy_tiocmget,
2234         .tiocmset = cy_tiocmset,
2235 };
2236 /* The serial driver boot-time initialization code!
2237     Hardware I/O ports are mapped to character special devices on a
2238     first found, first allocated manner.  That is, this code searches
2239     for Cyclom cards in the system.  As each is found, it is probed
2240     to discover how many chips (and thus how many ports) are present.
2241     These ports are mapped to the tty ports 64 and upward in monotonic
2242     fashion.  If an 8-port card is replaced with a 16-port card, the
2243     port mapping on a following card will shift.
2244
2245     This approach is different from what is used in the other serial
2246     device driver because the Cyclom is more properly a multiplexer,
2247     not just an aggregation of serial ports on one card.
2248
2249     If there are more cards with more ports than have been statically
2250     allocated above, a warning is printed and the extra ports are ignored.
2251  */
2252 static int __init
2253 serial167_init(void)
2254 {
2255   struct cyclades_port *info;
2256   int ret = 0;
2257   int good_ports = 0;
2258   int port_num = 0;
2259   int index;
2260   int DefSpeed;
2261 #ifdef notyet
2262   struct sigaction sa;
2263 #endif
2264
2265     if (!(mvme16x_config &MVME16x_CONFIG_GOT_CD2401))
2266         return 0;
2267
2268     cy_serial_driver = alloc_tty_driver(NR_PORTS);
2269     if (!cy_serial_driver)
2270         return -ENOMEM;
2271
2272 #if 0
2273 scrn[1] = '\0';
2274 #endif
2275
2276     show_version();
2277
2278     /* Has "console=0,9600n8" been used in bootinfo to change speed? */
2279     if (serial_console_cflag)
2280         DefSpeed = serial_console_cflag & 0017;
2281     else {
2282         DefSpeed = initial_console_speed;
2283         serial_console_info = &cy_port[0];
2284         serial_console_cflag = DefSpeed | CS8;
2285 #if 0
2286         serial_console = 64; /*callout_driver.minor_start*/
2287 #endif
2288     }
2289
2290     /* Initialize the tty_driver structure */
2291     
2292     cy_serial_driver->owner = THIS_MODULE;
2293     cy_serial_driver->devfs_name = "tts/";
2294     cy_serial_driver->name = "ttyS";
2295     cy_serial_driver->major = TTY_MAJOR;
2296     cy_serial_driver->minor_start = 64;
2297     cy_serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
2298     cy_serial_driver->subtype = SERIAL_TYPE_NORMAL;
2299     cy_serial_driver->init_termios = tty_std_termios;
2300     cy_serial_driver->init_termios.c_cflag =
2301             B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2302     cy_serial_driver->flags = TTY_DRIVER_REAL_RAW;
2303     tty_set_operations(cy_serial_driver, &cy_ops);
2304
2305     ret = tty_register_driver(cy_serial_driver);
2306     if (ret) {
2307             printk(KERN_ERR "Couldn't register MVME166/7 serial driver\n");
2308             put_tty_driver(cy_serial_driver);
2309             return ret;
2310     }
2311
2312     init_bh(CYCLADES_BH, do_cyclades_bh);
2313
2314     port_num = 0;
2315     info = cy_port;
2316     for (index = 0; index < 1; index++) {
2317
2318         good_ports = 4;
2319
2320         if(port_num < NR_PORTS){
2321             while( good_ports-- && port_num < NR_PORTS){
2322                 /*** initialize port ***/
2323                 info->magic = CYCLADES_MAGIC;
2324                 info->type = PORT_CIRRUS;
2325                 info->card = index;
2326                 info->line = port_num;
2327                 info->flags = STD_COM_FLAGS;
2328                 info->tty = 0;
2329                 info->xmit_fifo_size = 12;
2330                 info->cor1 = CyPARITY_NONE|Cy_8_BITS;
2331                 info->cor2 = CyETC;
2332                 info->cor3 = Cy_1_STOP;
2333                 info->cor4 = 0x08; /* _very_ small receive threshold */
2334                 info->cor5 = 0;
2335                 info->cor6 = 0;
2336                 info->cor7 = 0;
2337                 info->tbpr = baud_bpr[DefSpeed]; /* Tx BPR */
2338                 info->tco = baud_co[DefSpeed]; /* Tx CO */
2339                 info->rbpr = baud_bpr[DefSpeed]; /* Rx BPR */
2340                 info->rco = baud_co[DefSpeed] >> 5; /* Rx CO */
2341                 info->close_delay = 0;
2342                 info->x_char = 0;
2343                 info->event = 0;
2344                 info->count = 0;
2345 #ifdef SERIAL_DEBUG_COUNT
2346     printk("cyc: %d: setting count to 0\n", __LINE__);
2347 #endif
2348                 info->blocked_open = 0;
2349                 info->default_threshold = 0;
2350                 info->default_timeout = 0;
2351                 info->tqueue.routine = do_softint;
2352                 info->tqueue.data = info;
2353                 init_waitqueue_head(&info->open_wait);
2354                 init_waitqueue_head(&info->close_wait);
2355                 /* info->session */
2356                 /* info->pgrp */
2357 /*** !!!!!!!! this may expose new bugs !!!!!!!!! *********/
2358                 info->read_status_mask = CyTIMEOUT| CySPECHAR| CyBREAK
2359                                        | CyPARITY| CyFRAME| CyOVERRUN;
2360                 /* info->timeout */
2361
2362                 printk("ttyS%d ", info->line);
2363                 port_num++;info++;
2364                 if(!(port_num & 7)){
2365                     printk("\n               ");
2366                 }
2367             }
2368         }
2369         printk("\n");
2370     }
2371     while( port_num < NR_PORTS){
2372         info->line = -1;
2373         port_num++;info++;
2374     }
2375 #ifdef CONFIG_REMOTE_DEBUG
2376     debug_setup();
2377 #endif
2378     ret = request_irq(MVME167_IRQ_SER_ERR, cd2401_rxerr_interrupt, 0,
2379                                 "cd2401_errors", cd2401_rxerr_interrupt);
2380     if (ret) {
2381             printk(KERN_ERR "Could't get cd2401_errors IRQ");
2382             goto cleanup_serial_driver;
2383     }
2384
2385     ret = request_irq(MVME167_IRQ_SER_MODEM, cd2401_modem_interrupt, 0,
2386                                 "cd2401_modem", cd2401_modem_interrupt);
2387     if (ret) {
2388             printk(KERN_ERR "Could't get cd2401_modem IRQ");
2389             goto cleanup_irq_cd2401_errors;
2390     }
2391
2392     ret = request_irq(MVME167_IRQ_SER_TX, cd2401_tx_interrupt, 0,
2393                                 "cd2401_txints", cd2401_tx_interrupt);
2394     if (ret) {
2395             printk(KERN_ERR "Could't get cd2401_txints IRQ");
2396             goto cleanup_irq_cd2401_modem;
2397     }
2398
2399     ret = request_irq(MVME167_IRQ_SER_RX, cd2401_rx_interrupt, 0,
2400                                 "cd2401_rxints", cd2401_rx_interrupt);
2401     if (ret) {
2402             printk(KERN_ERR "Could't get cd2401_rxints IRQ");
2403             goto cleanup_irq_cd2401_txints;
2404     }
2405
2406     /* Now we have registered the interrupt handlers, allow the interrupts */
2407
2408     pcc2chip[PccSCCMICR] = 0x15;                /* Serial ints are level 5 */
2409     pcc2chip[PccSCCTICR] = 0x15;
2410     pcc2chip[PccSCCRICR] = 0x15;
2411
2412     pcc2chip[PccIMLR] = 3;                      /* Allow PCC2 ints above 3!? */
2413
2414     return 0;
2415 cleanup_irq_cd2401_txints:
2416     free_irq(MVME167_IRQ_SER_TX, cd2401_tx_interrupt);
2417 cleanup_irq_cd2401_modem:
2418     free_irq(MVME167_IRQ_SER_MODEM, cd2401_modem_interrupt);
2419 cleanup_irq_cd2401_errors:
2420     free_irq(MVME167_IRQ_SER_ERR, cd2401_rxerr_interrupt);
2421 cleanup_serial_driver:
2422     if (tty_unregister_driver(cy_serial_driver))
2423             printk(KERN_ERR "Couldn't unregister MVME166/7 serial driver\n");
2424     put_tty_driver(cy_serial_driver);
2425     return ret;
2426 } /* serial167_init */
2427
2428 module_init(serial167_init);
2429
2430
2431 #ifdef CYCLOM_SHOW_STATUS
2432 static void
2433 show_status(int line_num)
2434 {
2435   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
2436   int channel;
2437   struct cyclades_port * info;
2438   unsigned long flags;
2439
2440     info = &cy_port[line_num];
2441     channel = info->line;
2442     printk("  channel %d\n", channel);/**/
2443
2444     printk(" cy_port\n");
2445     printk("  card line flags = %d %d %x\n",
2446                  info->card, info->line, info->flags);
2447     printk("  *tty read_status_mask timeout xmit_fifo_size = %lx %x %x %x\n",
2448                  (long)info->tty, info->read_status_mask,
2449                  info->timeout, info->xmit_fifo_size);
2450     printk("  cor1,cor2,cor3,cor4,cor5,cor6,cor7 = %x %x %x %x %x %x %x\n",
2451              info->cor1, info->cor2, info->cor3, info->cor4, info->cor5,
2452                         info->cor6, info->cor7);
2453     printk("  tbpr,tco,rbpr,rco = %d %d %d %d\n",
2454              info->tbpr, info->tco, info->rbpr, info->rco);
2455     printk("  close_delay event count = %d %d %d\n",
2456              info->close_delay, info->event, info->count);
2457     printk("  x_char blocked_open = %x %x\n",
2458              info->x_char, info->blocked_open);
2459     printk("  open_wait = %lx %lx %lx\n",
2460              (long)info->open_wait);
2461
2462
2463     local_irq_save(flags);
2464
2465 /* Global Registers */
2466
2467         printk(" CyGFRCR %x\n", base_addr[CyGFRCR]);
2468         printk(" CyCAR %x\n", base_addr[CyCAR]);
2469         printk(" CyRISR %x\n", base_addr[CyRISR]);
2470         printk(" CyTISR %x\n", base_addr[CyTISR]);
2471         printk(" CyMISR %x\n", base_addr[CyMISR]);
2472         printk(" CyRIR %x\n", base_addr[CyRIR]);
2473         printk(" CyTIR %x\n", base_addr[CyTIR]);
2474         printk(" CyMIR %x\n", base_addr[CyMIR]);
2475         printk(" CyTPR %x\n", base_addr[CyTPR]);
2476
2477         base_addr[CyCAR] = (u_char)channel;
2478
2479 /* Virtual Registers */
2480
2481 #if 0
2482         printk(" CyRIVR %x\n", base_addr[CyRIVR]);
2483         printk(" CyTIVR %x\n", base_addr[CyTIVR]);
2484         printk(" CyMIVR %x\n", base_addr[CyMIVR]);
2485         printk(" CyMISR %x\n", base_addr[CyMISR]);
2486 #endif
2487
2488 /* Channel Registers */
2489
2490         printk(" CyCCR %x\n", base_addr[CyCCR]);
2491         printk(" CyIER %x\n", base_addr[CyIER]);
2492         printk(" CyCOR1 %x\n", base_addr[CyCOR1]);
2493         printk(" CyCOR2 %x\n", base_addr[CyCOR2]);
2494         printk(" CyCOR3 %x\n", base_addr[CyCOR3]);
2495         printk(" CyCOR4 %x\n", base_addr[CyCOR4]);
2496         printk(" CyCOR5 %x\n", base_addr[CyCOR5]);
2497 #if 0
2498         printk(" CyCCSR %x\n", base_addr[CyCCSR]);
2499         printk(" CyRDCR %x\n", base_addr[CyRDCR]);
2500 #endif
2501         printk(" CySCHR1 %x\n", base_addr[CySCHR1]);
2502         printk(" CySCHR2 %x\n", base_addr[CySCHR2]);
2503 #if 0
2504         printk(" CySCHR3 %x\n", base_addr[CySCHR3]);
2505         printk(" CySCHR4 %x\n", base_addr[CySCHR4]);
2506         printk(" CySCRL %x\n", base_addr[CySCRL]);
2507         printk(" CySCRH %x\n", base_addr[CySCRH]);
2508         printk(" CyLNC %x\n", base_addr[CyLNC]);
2509         printk(" CyMCOR1 %x\n", base_addr[CyMCOR1]);
2510         printk(" CyMCOR2 %x\n", base_addr[CyMCOR2]);
2511 #endif
2512         printk(" CyRTPRL %x\n", base_addr[CyRTPRL]);
2513         printk(" CyRTPRH %x\n", base_addr[CyRTPRH]);
2514         printk(" CyMSVR1 %x\n", base_addr[CyMSVR1]);
2515         printk(" CyMSVR2 %x\n", base_addr[CyMSVR2]);
2516         printk(" CyRBPR %x\n", base_addr[CyRBPR]);
2517         printk(" CyRCOR %x\n", base_addr[CyRCOR]);
2518         printk(" CyTBPR %x\n", base_addr[CyTBPR]);
2519         printk(" CyTCOR %x\n", base_addr[CyTCOR]);
2520
2521     local_irq_restore(flags);
2522 } /* show_status */
2523 #endif
2524
2525
2526 #if 0
2527 /* Dummy routine in mvme16x/config.c for now */
2528
2529 /* Serial console setup. Called from linux/init/main.c */
2530
2531 void console_setup(char *str, int *ints)
2532 {
2533         char *s;
2534         int baud, bits, parity;
2535         int cflag = 0;
2536
2537         /* Sanity check. */
2538         if (ints[0] > 3 || ints[1] > 3) return;
2539
2540         /* Get baud, bits and parity */
2541         baud = 2400;
2542         bits = 8;
2543         parity = 'n';
2544         if (ints[2]) baud = ints[2];
2545         if ((s = strchr(str, ','))) {
2546                 do {
2547                         s++;
2548                 } while(*s >= '0' && *s <= '9');
2549                 if (*s) parity = *s++;
2550                 if (*s) bits   = *s - '0';
2551         }
2552
2553         /* Now construct a cflag setting. */
2554         switch(baud) {
2555                 case 1200:
2556                         cflag |= B1200;
2557                         break;
2558                 case 9600:
2559                         cflag |= B9600;
2560                         break;
2561                 case 19200:
2562                         cflag |= B19200;
2563                         break;
2564                 case 38400:
2565                         cflag |= B38400;
2566                         break;
2567                 case 2400:
2568                 default:
2569                         cflag |= B2400;
2570                         break;
2571         }
2572         switch(bits) {
2573                 case 7:
2574                         cflag |= CS7;
2575                         break;
2576                 default:
2577                 case 8:
2578                         cflag |= CS8;
2579                         break;
2580         }
2581         switch(parity) {
2582                 case 'o': case 'O':
2583                         cflag |= PARODD;
2584                         break;
2585                 case 'e': case 'E':
2586                         cflag |= PARENB;
2587                         break;
2588         }
2589
2590         serial_console_info = &cy_port[ints[1]];
2591         serial_console_cflag = cflag;
2592         serial_console = ints[1] + 64; /*callout_driver.minor_start*/
2593 }
2594 #endif
2595
2596 /*
2597  * The following is probably out of date for 2.1.x serial console stuff.
2598  *
2599  * The console is registered early on from arch/m68k/kernel/setup.c, and
2600  * it therefore relies on the chip being setup correctly by 166-Bug.  This
2601  * seems reasonable, as the serial port has been used to invoke the system
2602  * boot.  It also means that this function must not rely on any data
2603  * initialisation performed by serial167_init() etc.
2604  *
2605  * Of course, once the console has been registered, we had better ensure
2606  * that serial167_init() doesn't leave the chip non-functional.
2607  *
2608  * The console must be locked when we get here.
2609  */
2610
2611 void serial167_console_write(struct console *co, const char *str, unsigned count)
2612 {
2613         volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
2614         unsigned long flags;
2615         volatile u_char sink;
2616         u_char ier;
2617         int port;
2618         u_char do_lf = 0;
2619         int i = 0;
2620
2621         local_irq_save(flags);
2622
2623         /* Ensure transmitter is enabled! */
2624
2625         port = 0;
2626         base_addr[CyCAR] = (u_char)port;
2627         while (base_addr[CyCCR])
2628                 ;
2629         base_addr[CyCCR] = CyENB_XMTR;
2630
2631         ier = base_addr[CyIER];
2632         base_addr[CyIER] = CyTxMpty;
2633
2634         while (1) {
2635                 if (pcc2chip[PccSCCTICR] & 0x20)
2636                 {
2637                         /* We have a Tx int. Acknowledge it */
2638                         sink = pcc2chip[PccTPIACKR];
2639                         if ((base_addr[CyLICR] >> 2) == port) {
2640                                 if (i == count) {
2641                                         /* Last char of string is now output */
2642                                         base_addr[CyTEOIR] = CyNOTRANS;
2643                                         break;
2644                                 }
2645                                 if (do_lf) {
2646                                         base_addr[CyTDR] = '\n';
2647                                         str++;
2648                                         i++;
2649                                         do_lf = 0;
2650                                 }
2651                                 else if (*str == '\n') {
2652                                         base_addr[CyTDR] = '\r';
2653                                         do_lf = 1;
2654                                 }
2655                                 else {
2656                                         base_addr[CyTDR] = *str++;
2657                                         i++;
2658                                 }
2659                                 base_addr[CyTEOIR] = 0;
2660                         }
2661                         else
2662                                 base_addr[CyTEOIR] = CyNOTRANS;
2663                 }
2664         }
2665
2666         base_addr[CyIER] = ier;
2667
2668         local_irq_restore(flags);
2669 }
2670
2671 static struct tty_driver *serial167_console_device(struct console *c, int *index)
2672 {
2673         *index = c->index;
2674         return cy_serial_driver;
2675 }
2676
2677
2678 static int __init serial167_console_setup(struct console *co, char *options)
2679 {
2680         return 0;
2681 }
2682
2683
2684 static struct console sercons = {
2685         .name           = "ttyS",
2686         .write          = serial167_console_write,
2687         .device         = serial167_console_device,
2688         .setup          = serial167_console_setup,
2689         .flags          = CON_PRINTBUFFER,
2690         .index          = -1,
2691 };
2692
2693
2694 static int __init serial167_console_init(void)
2695 {
2696         if (vme_brdtype == VME_TYPE_MVME166 ||
2697                         vme_brdtype == VME_TYPE_MVME167 ||
2698                         vme_brdtype == VME_TYPE_MVME177) {
2699                 mvme167_serial_console_setup(0);
2700                 register_console(&sercons);
2701         }
2702         return 0;
2703 }
2704 console_initcall(serial167_console_init);
2705
2706 #ifdef CONFIG_REMOTE_DEBUG
2707 void putDebugChar (int c)
2708 {
2709         volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
2710         unsigned long flags;
2711         volatile u_char sink;
2712         u_char ier;
2713         int port;
2714
2715         local_irq_save(flags);
2716
2717         /* Ensure transmitter is enabled! */
2718
2719         port = DEBUG_PORT;
2720         base_addr[CyCAR] = (u_char)port;
2721         while (base_addr[CyCCR])
2722                 ;
2723         base_addr[CyCCR] = CyENB_XMTR;
2724
2725         ier = base_addr[CyIER];
2726         base_addr[CyIER] = CyTxMpty;
2727
2728         while (1) {
2729                 if (pcc2chip[PccSCCTICR] & 0x20)
2730                 {
2731                         /* We have a Tx int. Acknowledge it */
2732                         sink = pcc2chip[PccTPIACKR];
2733                         if ((base_addr[CyLICR] >> 2) == port) {
2734                                 base_addr[CyTDR] = c;
2735                                 base_addr[CyTEOIR] = 0;
2736                                 break;
2737                         }
2738                         else
2739                                 base_addr[CyTEOIR] = CyNOTRANS;
2740                 }
2741         }
2742
2743         base_addr[CyIER] = ier;
2744
2745         local_irq_restore(flags);
2746 }
2747
2748 int getDebugChar()
2749 {
2750         volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
2751         unsigned long flags;
2752         volatile u_char sink;
2753         u_char ier;
2754         int port;
2755         int i, c;
2756
2757         i = debugiq.out;
2758         if (i != debugiq.in) {
2759                 c = debugiq.buf[i];
2760                 if (++i == DEBUG_LEN)
2761                         i = 0;
2762                 debugiq.out = i;
2763                 return c;
2764         }
2765         /* OK, nothing in queue, wait in poll loop */
2766
2767         local_irq_save(flags);
2768
2769         /* Ensure receiver is enabled! */
2770
2771         port = DEBUG_PORT;
2772         base_addr[CyCAR] = (u_char)port;
2773 #if 0
2774         while (base_addr[CyCCR])
2775                 ;
2776         base_addr[CyCCR] = CyENB_RCVR;
2777 #endif
2778         ier = base_addr[CyIER];
2779         base_addr[CyIER] = CyRxData;
2780
2781         while (1) {
2782                 if (pcc2chip[PccSCCRICR] & 0x20)
2783                 {
2784                         /* We have a Rx int. Acknowledge it */
2785                         sink = pcc2chip[PccRPIACKR];
2786                         if ((base_addr[CyLICR] >> 2) == port) {
2787                                 int cnt = base_addr[CyRFOC];
2788                                 while (cnt-- > 0)
2789                                 {
2790                                         c = base_addr[CyRDR];
2791                                         if (c == 0)
2792                                                 printk ("!! debug char is null (cnt=%d) !!", cnt);
2793                                         else
2794                                                 queueDebugChar (c);
2795                                 }
2796                                 base_addr[CyREOIR] = 0;
2797                                 i = debugiq.out;
2798                                 if (i == debugiq.in)
2799                                         panic ("Debug input queue empty!");
2800                                 c = debugiq.buf[i];
2801                                 if (++i == DEBUG_LEN)
2802                                         i = 0;
2803                                 debugiq.out = i;
2804                                 break;
2805                         }
2806                         else
2807                                 base_addr[CyREOIR] = CyNOTRANS;
2808                 }
2809         }
2810
2811         base_addr[CyIER] = ier;
2812
2813         local_irq_restore(flags);
2814
2815         return (c);
2816 }
2817
2818 void queueDebugChar (int c)
2819 {
2820         int i;
2821
2822         i = debugiq.in;
2823         debugiq.buf[i] = c;
2824         if (++i == DEBUG_LEN)
2825                 i = 0;
2826         if (i != debugiq.out)
2827                 debugiq.in = i;
2828 }
2829
2830 static void
2831 debug_setup()
2832 {
2833   unsigned long flags;
2834   volatile unsigned char *base_addr = (u_char *)BASE_ADDR;
2835   int   i, cflag;
2836
2837     cflag = B19200;
2838
2839     local_irq_save(flags);
2840
2841     for (i = 0; i < 4; i++)
2842     {
2843         base_addr[CyCAR] = i;
2844         base_addr[CyLICR] = i << 2;
2845     }
2846
2847     debugiq.in = debugiq.out = 0;
2848
2849     base_addr[CyCAR] = DEBUG_PORT;
2850
2851     /* baud rate */
2852     i = cflag & CBAUD;
2853
2854     base_addr[CyIER] = 0;
2855
2856     base_addr[CyCMR] = CyASYNC;
2857     base_addr[CyLICR] = DEBUG_PORT << 2;
2858     base_addr[CyLIVR] = 0x5c;
2859
2860     /* tx and rx baud rate */
2861
2862     base_addr[CyTCOR] = baud_co[i];
2863     base_addr[CyTBPR] = baud_bpr[i];
2864     base_addr[CyRCOR] = baud_co[i] >> 5;
2865     base_addr[CyRBPR] = baud_bpr[i];
2866
2867     /* set line characteristics  according configuration */
2868
2869     base_addr[CySCHR1] = 0;
2870     base_addr[CySCHR2] = 0;
2871     base_addr[CySCRL] = 0;
2872     base_addr[CySCRH] = 0;
2873     base_addr[CyCOR1] = Cy_8_BITS | CyPARITY_NONE;
2874     base_addr[CyCOR2] = 0;
2875     base_addr[CyCOR3] = Cy_1_STOP;
2876     base_addr[CyCOR4] = baud_cor4[i];
2877     base_addr[CyCOR5] = 0;
2878     base_addr[CyCOR6] = 0;
2879     base_addr[CyCOR7] = 0;
2880
2881     write_cy_cmd(base_addr,CyINIT_CHAN);
2882     write_cy_cmd(base_addr,CyENB_RCVR);
2883
2884     base_addr[CyCAR] = DEBUG_PORT; /* !!! Is this needed? */
2885
2886     base_addr[CyRTPRL] = 2;
2887     base_addr[CyRTPRH] = 0;
2888
2889     base_addr[CyMSVR1] = CyRTS;
2890     base_addr[CyMSVR2] = CyDTR;
2891
2892     base_addr[CyIER] = CyRxData;
2893
2894     local_irq_restore(flags);
2895
2896 } /* debug_setup */
2897
2898 #endif
2899
2900 MODULE_LICENSE("GPL");