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