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