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