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