ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / sbus / char / aurora.c
1 /*      $Id: aurora.c,v 1.19 2002/01/08 16:00:16 davem Exp $
2  *      linux/drivers/sbus/char/aurora.c -- Aurora multiport driver
3  *
4  *      Copyright (c) 1999 by Oliver Aldulea (oli at bv dot ro)
5  *
6  *      This code is based on the RISCom/8 multiport serial driver written
7  *      by Dmitry Gorodchanin (pgmdsg@ibi.com), based on the Linux serial
8  *      driver, written by Linus Torvalds, Theodore T'so and others.
9  *      The Aurora multiport programming info was obtained mainly from the
10  *      Cirrus Logic CD180 documentation (available on the web), and by
11  *      doing heavy tests on the board. Many thanks to Eddie C. Dost for the
12  *      help on the sbus interface.
13  *
14  *      This program is free software; you can redistribute it and/or modify
15  *      it under the terms of the GNU General Public License as published by
16  *      the Free Software Foundation; either version 2 of the License, or
17  *      (at your option) any later version.
18  *
19  *      This program is distributed in the hope that it will be useful,
20  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
21  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *      GNU General Public License for more details.
23  *
24  *      You should have received a copy of the GNU General Public License
25  *      along with this program; if not, write to the Free Software
26  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27  *
28  *      Revision 1.0
29  *
30  *      This is the first public release.
31  *
32  *      Most of the information you need is in the aurora.h file. Please
33  *      read that file before reading this one.
34  *
35  *      Several parts of the code do not have comments yet.
36  * 
37  * n.b.  The board can support 115.2 bit rates, but only on a few
38  * ports. The total badwidth of one chip (ports 0-7 or 8-15) is equal
39  * to OSC_FREQ div 16. In case of my board, each chip can take 6
40  * channels of 115.2 kbaud.  This information is not well-tested.
41  * 
42  * Fixed to use tty_get_baud_rate().
43  *   Theodore Ts'o <tytso@mit.edu>, 2001-Oct-12
44  */
45
46 #include <linux/module.h>
47
48 #include <linux/errno.h>
49 #include <linux/sched.h>
50 #ifdef AURORA_INT_DEBUG
51 #include <linux/timer.h>
52 #endif
53 #include <linux/interrupt.h>
54 #include <linux/tty.h>
55 #include <linux/tty_flip.h>
56 #include <linux/major.h>
57 #include <linux/string.h>
58 #include <linux/fcntl.h>
59 #include <linux/mm.h>
60 #include <linux/kernel.h>
61 #include <linux/init.h>
62 #include <linux/delay.h>
63
64 #include <asm/io.h>
65 #include <asm/irq.h>
66 #include <asm/oplib.h>
67 #include <asm/system.h>
68 #include <asm/bitops.h>
69 #include <asm/kdebug.h>
70 #include <asm/sbus.h>
71 #include <asm/uaccess.h>
72
73 #include "aurora.h"
74 #include "cd180.h"
75
76 unsigned char irqs[4] = {
77         0, 0, 0, 0
78 };
79
80 #ifdef AURORA_INT_DEBUG
81 int irqhit=0;
82 #endif
83
84 #ifndef MIN
85 #define MIN(a,b) ((a) < (b) ? (a) : (b))
86 #endif
87
88 static struct tty_driver *aurora_driver;
89 static struct Aurora_board aurora_board[AURORA_NBOARD] = {
90         {0,},
91 };
92
93 static struct Aurora_port aurora_port[AURORA_TNPORTS] =  {
94         { 0, },
95 };
96
97 /* no longer used. static struct Aurora_board * IRQ_to_board[16] = { NULL, } ;*/
98 static unsigned char * tmp_buf = NULL;
99 static DECLARE_MUTEX(tmp_buf_sem);
100
101 DECLARE_TASK_QUEUE(tq_aurora);
102
103 static inline int aurora_paranoia_check(struct Aurora_port const * port,
104                                     char *name, const char *routine)
105 {
106 #ifdef AURORA_PARANOIA_CHECK
107         static const char *badmagic =
108                 KERN_DEBUG "aurora: Warning: bad aurora port magic number for device %s in %s\n";
109         static const char *badinfo =
110                 KERN_DEBUG "aurora: Warning: null aurora port for device %s in %s\n";
111
112         if (!port) {
113                 printk(badinfo, name, routine);
114                 return 1;
115         }
116         if (port->magic != AURORA_MAGIC) {
117                 printk(badmagic, name, routine);
118                 return 1;
119         }
120 #endif
121         return 0;
122 }
123
124 /*
125  * 
126  *  Service functions for aurora driver.
127  * 
128  */
129
130 /* Get board number from pointer */
131 extern inline int board_No (struct Aurora_board const * bp)
132 {
133         return bp - aurora_board;
134 }
135
136 /* Get port number from pointer */
137 extern inline int port_No (struct Aurora_port const * port)
138 {
139         return AURORA_PORT(port - aurora_port); 
140 }
141
142 /* Get pointer to board from pointer to port */
143 extern inline struct Aurora_board * port_Board(struct Aurora_port const * port)
144 {
145         return &aurora_board[AURORA_BOARD(port - aurora_port)];
146 }
147
148 /* Wait for Channel Command Register ready */
149 extern inline void aurora_wait_CCR(struct aurora_reg128 * r)
150 {
151         unsigned long delay;
152
153 #ifdef AURORA_DEBUG
154 printk("aurora_wait_CCR\n");
155 #endif
156         /* FIXME: need something more descriptive than 100000 :) */
157         for (delay = 100000; delay; delay--) 
158                 if (!sbus_readb(&r->r[CD180_CCR]))
159                         return;
160         printk(KERN_DEBUG "aurora: Timeout waiting for CCR.\n");
161 }
162
163 /*
164  *  aurora probe functions.
165  */
166
167 /* Must be called with enabled interrupts */
168 extern inline void aurora_long_delay(unsigned long delay)
169 {
170         unsigned long i;
171
172 #ifdef AURORA_DEBUG
173         printk("aurora_long_delay: start\n");
174 #endif
175         for (i = jiffies + delay; time_before(jiffies, i); ) ;
176 #ifdef AURORA_DEBUG
177         printk("aurora_long_delay: end\n");
178 #endif
179 }
180
181 /* Reset and setup CD180 chip */
182 static int aurora_init_CD180(struct Aurora_board * bp, int chip)
183 {
184         unsigned long flags;
185         int id;
186         
187 #ifdef AURORA_DEBUG
188         printk("aurora_init_CD180: start %d:%d\n",
189                board_No(bp), chip);
190 #endif
191         save_flags(flags); cli();
192         sbus_writeb(0, &bp->r[chip]->r[CD180_CAR]);
193         sbus_writeb(0, &bp->r[chip]->r[CD180_GSVR]);
194
195         /* Wait for CCR ready        */
196         aurora_wait_CCR(bp->r[chip]);
197
198         /* Reset CD180 chip          */
199         sbus_writeb(CCR_HARDRESET, &bp->r[chip]->r[CD180_CCR]);
200         udelay(1);
201         sti();
202         id=1000;
203         while((--id) &&
204               (sbus_readb(&bp->r[chip]->r[CD180_GSVR])!=0xff))udelay(100);
205         if(!id) {
206                 printk(KERN_ERR "aurora%d: Chip %d failed init.\n",
207                        board_No(bp), chip);
208                 restore_flags(flags);
209                 return(-1);
210         }
211         cli();
212         sbus_writeb((board_No(bp)<<5)|((chip+1)<<3),
213                     &bp->r[chip]->r[CD180_GSVR]); /* Set ID for this chip      */
214         sbus_writeb(0x80|bp->ACK_MINT,
215                     &bp->r[chip]->r[CD180_MSMR]); /* Prio for modem intr       */
216         sbus_writeb(0x80|bp->ACK_TINT,
217                     &bp->r[chip]->r[CD180_TSMR]); /* Prio for transmitter intr */
218         sbus_writeb(0x80|bp->ACK_RINT,
219                     &bp->r[chip]->r[CD180_RSMR]); /* Prio for receiver intr    */
220         /* Setting up prescaler. We need 4 tick per 1 ms */
221         sbus_writeb((bp->oscfreq/(1000000/AURORA_TPS)) >> 8,
222                     &bp->r[chip]->r[CD180_PPRH]);
223         sbus_writeb((bp->oscfreq/(1000000/AURORA_TPS)) & 0xff,
224                     &bp->r[chip]->r[CD180_PPRL]);
225
226         sbus_writeb(SRCR_AUTOPRI|SRCR_GLOBPRI,
227                     &bp->r[chip]->r[CD180_SRCR]);
228
229         id = sbus_readb(&bp->r[chip]->r[CD180_GFRCR]);
230         printk(KERN_INFO "aurora%d: Chip %d id %02x: ",
231                board_No(bp), chip,id);
232         if(sbus_readb(&bp->r[chip]->r[CD180_SRCR]) & 128) {
233                 switch (id) {
234                         case 0x82:printk("CL-CD1864 rev A\n");break;
235                         case 0x83:printk("CL-CD1865 rev A\n");break;
236                         case 0x84:printk("CL-CD1865 rev B\n");break;
237                         case 0x85:printk("CL-CD1865 rev C\n");break;
238                         default:printk("Unknown.\n");
239                 };
240         } else {
241                 switch (id) {
242                         case 0x81:printk("CL-CD180 rev B\n");break;
243                         case 0x82:printk("CL-CD180 rev C\n");break;
244                         default:printk("Unknown.\n");
245                 };
246         }
247         restore_flags(flags);
248 #ifdef AURORA_DEBUG
249         printk("aurora_init_CD180: end\n");
250 #endif
251         return 0;
252 }
253
254 static int valid_irq(unsigned char irq)
255 {
256 int i;
257 for(i=0;i<TYPE_1_IRQS;i++)
258         if (type_1_irq[i]==irq) return 1;
259 return 0;
260 }
261
262 static irqreturn_t aurora_interrupt(int irq, void * dev_id, struct pt_regs * regs);
263
264 /* Main probing routine, also sets irq. */
265 static int aurora_probe(void)
266 {
267         struct sbus_bus *sbus;
268         struct sbus_dev *sdev;
269         int grrr;
270         char buf[30];
271         int bn = 0;
272         struct Aurora_board *bp;
273
274         for_each_sbus(sbus) {
275                 for_each_sbusdev(sdev, sbus) {
276 /*                      printk("Try: %x %s\n",sdev,sdev->prom_name);*/
277                         if (!strcmp(sdev->prom_name, "sio16")) {
278 #ifdef AURORA_DEBUG
279                                 printk(KERN_INFO "aurora: sio16 at %p\n",sdev);
280 #endif
281                                 if((sdev->reg_addrs[0].reg_size!=1) &&
282                                    (sdev->reg_addrs[1].reg_size!=128) &&
283                                    (sdev->reg_addrs[2].reg_size!=128) &&
284                                    (sdev->reg_addrs[3].reg_size!=4)) {
285                                         printk(KERN_ERR "aurora%d: registers' sizes "
286                                                "do not match.\n", bn);
287                                         break;
288                                 }
289                                 bp = &aurora_board[bn];
290                                 bp->r0 = (struct aurora_reg1 *)
291                                         sbus_ioremap(&sdev->resource[0], 0,
292                                                      sdev->reg_addrs[0].reg_size,
293                                                      "sio16");
294                                 if (bp->r0 == NULL) {
295                                         printk(KERN_ERR "aurora%d: can't map "
296                                                "reg_addrs[0]\n", bn);
297                                         break;
298                                 }
299 #ifdef AURORA_DEBUG
300                                 printk("Map reg 0: %p\n", bp->r0);
301 #endif
302                                 bp->r[0] = (struct aurora_reg128 *)
303                                         sbus_ioremap(&sdev->resource[1], 0,
304                                                      sdev->reg_addrs[1].reg_size,
305                                                      "sio16");
306                                 if (bp->r[0] == NULL) {
307                                         printk(KERN_ERR "aurora%d: can't map "
308                                                "reg_addrs[1]\n", bn);
309                                         break;
310                                 }
311 #ifdef AURORA_DEBUG
312                                 printk("Map reg 1: %p\n", bp->r[0]);
313 #endif
314                                 bp->r[1] = (struct aurora_reg128 *)
315                                         sbus_ioremap(&sdev->resource[2], 0,
316                                                      sdev->reg_addrs[2].reg_size,
317                                                      "sio16");
318                                 if (bp->r[1] == NULL) {
319                                         printk(KERN_ERR "aurora%d: can't map "
320                                                "reg_addrs[2]\n", bn);
321                                         break;
322                                 }
323 #ifdef AURORA_DEBUG
324                                 printk("Map reg 2: %p\n", bp->r[1]);
325 #endif
326                                 bp->r3 = (struct aurora_reg4 *)
327                                         sbus_ioremap(&sdev->resource[3], 0,
328                                                      sdev->reg_addrs[3].reg_size,
329                                                      "sio16");
330                                 if (bp->r3 == NULL) {
331                                         printk(KERN_ERR "aurora%d: can't map "
332                                                "reg_addrs[3]\n", bn);
333                                         break;
334                                 }
335 #ifdef AURORA_DEBUG
336                                 printk("Map reg 3: %p\n", bp->r3);
337 #endif
338                                 /* Variables setup */
339                                 bp->flags = 0;
340 #ifdef AURORA_DEBUG
341                                 grrr=prom_getint(sdev->prom_node,"intr");
342                                 printk("intr pri %d\n", grrr);
343 #endif
344                                 if ((bp->irq=irqs[bn]) && valid_irq(bp->irq) &&
345                                     !request_irq(bp->irq|0x30, aurora_interrupt, SA_SHIRQ, "sio16", bp)) {
346                                         free_irq(bp->irq|0x30, bp);
347                                 } else
348                                 if ((bp->irq=prom_getint(sdev->prom_node, "bintr")) && valid_irq(bp->irq) &&
349                                     !request_irq(bp->irq|0x30, aurora_interrupt, SA_SHIRQ, "sio16", bp)) {
350                                         free_irq(bp->irq|0x30, bp);
351                                 } else
352                                 if ((bp->irq=prom_getint(sdev->prom_node, "intr")) && valid_irq(bp->irq) &&
353                                     !request_irq(bp->irq|0x30, aurora_interrupt, SA_SHIRQ, "sio16", bp)) {
354                                         free_irq(bp->irq|0x30, bp);
355                                 } else
356                                 for(grrr=0;grrr<TYPE_1_IRQS;grrr++) {
357                                         if ((bp->irq=type_1_irq[grrr])&&!request_irq(bp->irq|0x30, aurora_interrupt, SA_SHIRQ, "sio16", bp)) {
358                                                 free_irq(bp->irq|0x30, bp);
359                                                 break;
360                                         } else {
361                                         printk(KERN_ERR "aurora%d: Could not get an irq for this board !!!\n",bn);
362                                         bp->flags=0xff;
363                                         }
364                                 }
365                                 if(bp->flags==0xff)break;
366                                 printk(KERN_INFO "aurora%d: irq %d\n",bn,bp->irq&0x0f);
367                                 buf[0]=0;
368                                 grrr=prom_getproperty(sdev->prom_node,"dtr_rts",buf,sizeof(buf));
369                                 if(!strcmp(buf,"swapped")){
370                                         printk(KERN_INFO "aurora%d: Swapped DTR and RTS\n",bn);
371                                         bp->DTR=MSVR_RTS;
372                                         bp->RTS=MSVR_DTR;
373                                         bp->MSVDTR=CD180_MSVRTS;
374                                         bp->MSVRTS=CD180_MSVDTR;
375                                         bp->flags|=AURORA_BOARD_DTR_FLOW_OK;
376                                         }else{
377                                         #ifdef AURORA_FORCE_DTR_FLOW
378                                         printk(KERN_INFO "aurora%d: Forcing swapped DTR-RTS\n",bn);
379                                         bp->DTR=MSVR_RTS;
380                                         bp->RTS=MSVR_DTR;
381                                         bp->MSVDTR=CD180_MSVRTS;
382                                         bp->MSVRTS=CD180_MSVDTR;
383                                         bp->flags|=AURORA_BOARD_DTR_FLOW_OK;
384                                         #else
385                                         printk(KERN_INFO "aurora%d: Normal DTR and RTS\n",bn);
386                                         bp->DTR=MSVR_DTR;
387                                         bp->RTS=MSVR_RTS;
388                                         bp->MSVDTR=CD180_MSVDTR;
389                                         bp->MSVRTS=CD180_MSVRTS;
390                                         #endif
391                                 }
392                                 bp->oscfreq=prom_getint(sdev->prom_node,"clk")*100;
393                                 printk(KERN_INFO "aurora%d: Oscillator: %d Hz\n",bn,bp->oscfreq);
394                                 grrr=prom_getproperty(sdev->prom_node,"chip",buf,sizeof(buf));
395                                 printk(KERN_INFO "aurora%d: Chips: %s\n",bn,buf);
396                                 grrr=prom_getproperty(sdev->prom_node,"manu",buf,sizeof(buf));
397                                 printk(KERN_INFO "aurora%d: Manufacturer: %s\n",bn,buf);
398                                 grrr=prom_getproperty(sdev->prom_node,"model",buf,sizeof(buf));
399                                 printk(KERN_INFO "aurora%d: Model: %s\n",bn,buf);
400                                 grrr=prom_getproperty(sdev->prom_node,"rev",buf,sizeof(buf));
401                                 printk(KERN_INFO "aurora%d: Revision: %s\n",bn,buf);
402                                 grrr=prom_getproperty(sdev->prom_node,"mode",buf,sizeof(buf));
403                                 printk(KERN_INFO "aurora%d: Mode: %s\n",bn,buf);
404                                 #ifdef MODULE
405                                 bp->count=0;
406                                 #endif
407                                 bp->flags = AURORA_BOARD_PRESENT;
408                                 /* hardware ack */
409                                 bp->ACK_MINT=1;
410                                 bp->ACK_TINT=2;
411                                 bp->ACK_RINT=3;
412                                 bn++;
413                         }
414                 }
415         }
416         return bn;
417 }
418
419 static void aurora_release_io_range(struct Aurora_board *bp)
420 {
421         sbus_iounmap((unsigned long)bp->r0, 1);
422         sbus_iounmap((unsigned long)bp->r[0], 128);
423         sbus_iounmap((unsigned long)bp->r[1], 128);
424         sbus_iounmap((unsigned long)bp->r3, 4);
425 }
426
427 extern inline void aurora_mark_event(struct Aurora_port * port, int event)
428 {
429 #ifdef AURORA_DEBUG
430         printk("aurora_mark_event: start\n");
431 #endif
432         set_bit(event, &port->event);
433         queue_task(&port->tqueue, &tq_aurora);
434         mark_bh(AURORA_BH);
435 #ifdef AURORA_DEBUG
436         printk("aurora_mark_event: end\n");
437 #endif
438 }
439
440 static __inline__ struct Aurora_port * aurora_get_port(struct Aurora_board const * bp,
441                                                        int chip,
442                                                        unsigned char const *what)
443 {
444         unsigned char channel;
445         struct Aurora_port * port;
446
447         channel = ((chip << 3) |
448                    ((sbus_readb(&bp->r[chip]->r[CD180_GSCR]) & GSCR_CHAN) >> GSCR_CHAN_OFF));
449         port = &aurora_port[board_No(bp) * AURORA_NPORT * AURORA_NCD180 + channel];
450         if (port->flags & ASYNC_INITIALIZED)
451                 return port;
452
453         printk(KERN_DEBUG "aurora%d: %s interrupt from invalid port %d\n",
454                board_No(bp), what, channel);
455         return NULL;
456 }
457
458 static void aurora_receive_exc(struct Aurora_board const * bp, int chip)
459 {
460         struct Aurora_port *port;
461         struct tty_struct *tty;
462         unsigned char status;
463         unsigned char ch;
464         
465         if (!(port = aurora_get_port(bp, chip, "Receive_x")))
466                 return;
467
468         tty = port->tty;
469         if (tty->flip.count >= TTY_FLIPBUF_SIZE)  {
470 #ifdef AURORA_INTNORM
471                 printk("aurora%d: port %d: Working around flip buffer overflow.\n",
472                        board_No(bp), port_No(port));
473 #endif
474                 return;
475         }
476         
477 #ifdef AURORA_REPORT_OVERRUN    
478         status = sbus_readb(&bp->r[chip]->r[CD180_RCSR]);
479         if (status & RCSR_OE)  {
480                 port->overrun++;
481 #if 1
482                 printk("aurora%d: port %d: Overrun. Total %ld overruns.\n",
483                        board_No(bp), port_No(port), port->overrun);
484 #endif          
485         }
486         status &= port->mark_mask;
487 #else   
488         status = sbus_readb(&bp->r[chip]->r[CD180_RCSR]) & port->mark_mask;
489 #endif  
490         ch = sbus_readb(&bp->r[chip]->r[CD180_RDR]);
491         if (!status)
492                 return;
493
494         if (status & RCSR_TOUT)  {
495 /*              printk("aurora%d: port %d: Receiver timeout. Hardware problems ?\n",
496                        board_No(bp), port_No(port));*/
497                 return;
498                 
499         } else if (status & RCSR_BREAK)  {
500                 printk(KERN_DEBUG "aurora%d: port %d: Handling break...\n",
501                        board_No(bp), port_No(port));
502                 *tty->flip.flag_buf_ptr++ = TTY_BREAK;
503                 if (port->flags & ASYNC_SAK)
504                         do_SAK(tty);
505                 
506         } else if (status & RCSR_PE) 
507                 *tty->flip.flag_buf_ptr++ = TTY_PARITY;
508         
509         else if (status & RCSR_FE) 
510                 *tty->flip.flag_buf_ptr++ = TTY_FRAME;
511         
512         else if (status & RCSR_OE)
513                 *tty->flip.flag_buf_ptr++ = TTY_OVERRUN;
514         
515         else
516                 *tty->flip.flag_buf_ptr++ = 0;
517         
518         *tty->flip.char_buf_ptr++ = ch;
519         tty->flip.count++;
520         queue_task(&tty->flip.tqueue, &tq_timer);
521 }
522
523 static void aurora_receive(struct Aurora_board const * bp, int chip)
524 {
525         struct Aurora_port *port;
526         struct tty_struct *tty;
527         unsigned char count,cnt;
528
529         if (!(port = aurora_get_port(bp, chip, "Receive")))
530                 return;
531         
532         tty = port->tty;
533         
534         count = sbus_readb(&bp->r[chip]->r[CD180_RDCR]);
535
536 #ifdef AURORA_REPORT_FIFO
537         port->hits[count > 8 ? 9 : count]++;
538 #endif
539
540         while (count--)  {
541                 if (tty->flip.count >= TTY_FLIPBUF_SIZE)  {
542 #ifdef AURORA_INTNORM
543                         printk("aurora%d: port %d: Working around flip buffer overflow.\n",
544                                board_No(bp), port_No(port));
545 #endif
546                         break;
547                 }
548                 cnt = sbus_readb(&bp->r[chip]->r[CD180_RDR]);
549                 *tty->flip.char_buf_ptr++ = cnt;
550                 *tty->flip.flag_buf_ptr++ = 0;
551                 tty->flip.count++;
552         }
553         queue_task(&tty->flip.tqueue, &tq_timer);
554 }
555
556 static void aurora_transmit(struct Aurora_board const * bp, int chip)
557 {
558         struct Aurora_port *port;
559         struct tty_struct *tty;
560         unsigned char count;
561         
562         if (!(port = aurora_get_port(bp, chip, "Transmit")))
563                 return;
564                 
565         tty = port->tty;
566         
567         if (port->SRER & SRER_TXEMPTY)  {
568                 /* FIFO drained */
569                 sbus_writeb(port_No(port) & 7,
570                             &bp->r[chip]->r[CD180_CAR]);
571                 udelay(1);
572                 port->SRER &= ~SRER_TXEMPTY;
573                 sbus_writeb(port->SRER, &bp->r[chip]->r[CD180_SRER]);
574                 return;
575         }
576         
577         if ((port->xmit_cnt <= 0 && !port->break_length)
578             || tty->stopped || tty->hw_stopped)  {
579                 sbus_writeb(port_No(port) & 7,
580                             &bp->r[chip]->r[CD180_CAR]);
581                 udelay(1);
582                 port->SRER &= ~SRER_TXRDY;
583                 sbus_writeb(port->SRER,
584                             &bp->r[chip]->r[CD180_SRER]);
585                 return;
586         }
587         
588         if (port->break_length)  {
589                 if (port->break_length > 0)  {
590                         if (port->COR2 & COR2_ETC)  {
591                                 sbus_writeb(CD180_C_ESC,
592                                             &bp->r[chip]->r[CD180_TDR]);
593                                 sbus_writeb(CD180_C_SBRK,
594                                             &bp->r[chip]->r[CD180_TDR]);
595                                 port->COR2 &= ~COR2_ETC;
596                         }
597                         count = MIN(port->break_length, 0xff);
598                         sbus_writeb(CD180_C_ESC,
599                                     &bp->r[chip]->r[CD180_TDR]);
600                         sbus_writeb(CD180_C_DELAY,
601                                     &bp->r[chip]->r[CD180_TDR]);
602                         sbus_writeb(count,
603                                     &bp->r[chip]->r[CD180_TDR]);
604                         if (!(port->break_length -= count))
605                                 port->break_length--;
606                 } else  {
607                         sbus_writeb(CD180_C_ESC,
608                                     &bp->r[chip]->r[CD180_TDR]);
609                         sbus_writeb(CD180_C_EBRK,
610                                     &bp->r[chip]->r[CD180_TDR]);
611                         sbus_writeb(port->COR2,
612                                     &bp->r[chip]->r[CD180_COR2]);
613                         aurora_wait_CCR(bp->r[chip]);
614                         sbus_writeb(CCR_CORCHG2,
615                                     &bp->r[chip]->r[CD180_CCR]);
616                         port->break_length = 0;
617                 }
618                 return;
619         }
620         
621         count = CD180_NFIFO;
622         do {
623                 u8 byte = port->xmit_buf[port->xmit_tail++];
624
625                 sbus_writeb(byte, &bp->r[chip]->r[CD180_TDR]);
626                 port->xmit_tail = port->xmit_tail & (SERIAL_XMIT_SIZE-1);
627                 if (--port->xmit_cnt <= 0)
628                         break;
629         } while (--count > 0);
630         
631         if (port->xmit_cnt <= 0)  {
632                 sbus_writeb(port_No(port) & 7,
633                             &bp->r[chip]->r[CD180_CAR]);
634                 udelay(1);
635                 port->SRER &= ~SRER_TXRDY;
636                 sbus_writeb(port->SRER,
637                             &bp->r[chip]->r[CD180_SRER]);
638         }
639         if (port->xmit_cnt <= port->wakeup_chars)
640                 aurora_mark_event(port, RS_EVENT_WRITE_WAKEUP);
641 }
642
643 static void aurora_check_modem(struct Aurora_board const * bp, int chip)
644 {
645         struct Aurora_port *port;
646         struct tty_struct *tty;
647         unsigned char mcr;
648         
649         if (!(port = aurora_get_port(bp, chip, "Modem")))
650                 return;
651                 
652         tty = port->tty;
653         
654         mcr = sbus_readb(&bp->r[chip]->r[CD180_MCR]);
655         if (mcr & MCR_CDCHG)  {
656                 if (sbus_readb(&bp->r[chip]->r[CD180_MSVR]) & MSVR_CD) 
657                         wake_up_interruptible(&port->open_wait);
658                 else
659                         schedule_task(&port->tqueue_hangup);
660         }
661         
662 /* We don't have such things yet. My aurora board has DTR and RTS swapped, but that doesn't count in this driver. Let's hope
663  * Aurora didn't made any boards with CTS or DSR broken...
664  */
665 /* #ifdef AURORA_BRAIN_DAMAGED_CTS
666         if (mcr & MCR_CTSCHG)  {
667                 if (aurora_in(bp, CD180_MSVR) & MSVR_CTS)  {
668                         tty->hw_stopped = 0;
669                         port->SRER |= SRER_TXRDY;
670                         if (port->xmit_cnt <= port->wakeup_chars)
671                                 aurora_mark_event(port, RS_EVENT_WRITE_WAKEUP);
672                 } else  {
673                         tty->hw_stopped = 1;
674                         port->SRER &= ~SRER_TXRDY;
675                 }
676                 sbus_writeb(port->SRER, &bp->r[chip]->r[CD180_SRER]);
677         }
678         if (mcr & MCR_DSRCHG)  {
679                 if (aurora_in(bp, CD180_MSVR) & MSVR_DSR)  {
680                         tty->hw_stopped = 0;
681                         port->SRER |= SRER_TXRDY;
682                         if (port->xmit_cnt <= port->wakeup_chars)
683                                 aurora_mark_event(port, RS_EVENT_WRITE_WAKEUP);
684                 } else  {
685                         tty->hw_stopped = 1;
686                         port->SRER &= ~SRER_TXRDY;
687                 }
688                 sbus_writeb(port->SRER, &bp->r[chip]->r[CD180_SRER]);
689         }
690 #endif AURORA_BRAIN_DAMAGED_CTS */
691         
692         /* Clear change bits */
693         sbus_writeb(0, &bp->r[chip]->r[CD180_MCR]);
694 }
695
696 /* The main interrupt processing routine */
697 static irqreturn_t aurora_interrupt(int irq, void * dev_id, struct pt_regs * regs)
698 {
699         unsigned char status;
700         unsigned char ack,chip/*,chip_id*/;
701         struct Aurora_board * bp = (struct Aurora_board *) dev_id;
702         unsigned long loop = 0;
703
704 #ifdef AURORA_INT_DEBUG
705         printk("IRQ%d %d\n",irq,++irqhit);
706 #ifdef AURORA_FLOODPRO
707         if (irqhit>=AURORA_FLOODPRO)
708                 sbus_writeb(8, &bp->r0->r);
709 #endif
710 #endif
711         
712 /* old  bp = IRQ_to_board[irq&0x0f];*/
713         
714         if (!bp || !(bp->flags & AURORA_BOARD_ACTIVE))
715                 return IRQ_NONE;
716
717 /*      The while() below takes care of this.
718         status = sbus_readb(&bp->r[0]->r[CD180_SRSR]);
719 #ifdef AURORA_INT_DEBUG
720         printk("mumu: %02x\n", status);
721 #endif
722         if (!(status&SRSR_ANYINT))
723                 return IRQ_NONE; * Nobody has anything to say, so exit *
724 */
725         while ((loop++ < 48) &&
726                (status = sbus_readb(&bp->r[0]->r[CD180_SRSR]) & SRSR_ANYINT)){
727 #ifdef AURORA_INT_DEBUG
728                 printk("SRSR: %02x\n", status);
729 #endif
730                 if (status & SRSR_REXT) {
731                         ack = sbus_readb(&bp->r3->r[bp->ACK_RINT]);
732 #ifdef AURORA_INT_DEBUG
733                         printk("R-ACK %02x\n", ack);
734 #endif
735                         if ((ack >> 5) == board_No(bp)) {
736                                 if ((chip=((ack>>3)&3)-1) < AURORA_NCD180) {
737                                         if ((ack&GSVR_ITMASK)==GSVR_IT_RGD) {
738                                                 aurora_receive(bp,chip);
739                                                 sbus_writeb(0,
740                                                          &bp->r[chip]->r[CD180_EOSRR]);
741                                         } else if ((ack & GSVR_ITMASK) == GSVR_IT_REXC) {
742                                                 aurora_receive_exc(bp,chip);
743                                                 sbus_writeb(0,
744                                                          &bp->r[chip]->r[CD180_EOSRR]);
745                                         }
746                                 }
747                         }
748                 } else if (status & SRSR_TEXT) {
749                         ack = sbus_readb(&bp->r3->r[bp->ACK_TINT]);
750 #ifdef AURORA_INT_DEBUG
751                         printk("T-ACK %02x\n", ack);
752 #endif
753                         if ((ack >> 5) == board_No(bp)) {
754                                 if ((chip=((ack>>3)&3)-1) < AURORA_NCD180) {
755                                         if ((ack&GSVR_ITMASK)==GSVR_IT_TX) {
756                                                 aurora_transmit(bp,chip);
757                                                 sbus_writeb(0,
758                                                          &bp->r[chip]->r[CD180_EOSRR]);
759                                         }
760                                 }
761                         }
762                 } else if (status & SRSR_MEXT) {
763                         ack = sbus_readb(&bp->r3->r[bp->ACK_MINT]);
764 #ifdef AURORA_INT_DEBUG
765                         printk("M-ACK %02x\n", ack);
766 #endif
767                         if ((ack >> 5) == board_No(bp)) {
768                                 if ((chip = ((ack>>3)&3)-1) < AURORA_NCD180) {
769                                         if ((ack&GSVR_ITMASK)==GSVR_IT_MDM) {
770                                                 aurora_check_modem(bp,chip);
771                                                 sbus_writeb(0,
772                                                          &bp->r[chip]->r[CD180_EOSRR]);
773                                         }
774                                 }
775                         }
776                 }
777         }
778 /* I guess this faster code can be used with CD1865, using AUROPRI and GLOBPRI. */
779 #if 0
780         while ((loop++ < 48)&&(status=bp->r[0]->r[CD180_SRSR]&SRSR_ANYINT)){
781 #ifdef AURORA_INT_DEBUG
782                 printk("SRSR: %02x\n",status);
783 #endif
784                 ack = sbus_readb(&bp->r3->r[0]);
785 #ifdef AURORA_INT_DEBUG
786                 printk("ACK: %02x\n",ack);
787 #endif
788                 if ((ack>>5)==board_No(bp)) {
789                         if ((chip=((ack>>3)&3)-1) < AURORA_NCD180) {
790                                 ack&=GSVR_ITMASK;
791                                 if (ack==GSVR_IT_RGD) {
792                                         aurora_receive(bp,chip);
793                                         sbus_writeb(0,
794                                                     &bp->r[chip]->r[CD180_EOSRR]);
795                                 } else if (ack==GSVR_IT_REXC) {
796                                         aurora_receive_exc(bp,chip);
797                                         sbus_writeb(0,
798                                                     &bp->r[chip]->r[CD180_EOSRR]);
799                                 } else if (ack==GSVR_IT_TX) {
800                                         aurora_transmit(bp,chip);
801                                         sbus_writeb(0,
802                                                     &bp->r[chip]->r[CD180_EOSRR]);
803                                 } else if (ack==GSVR_IT_MDM) {
804                                         aurora_check_modem(bp,chip);
805                                         sbus_writeb(0,
806                                                     &bp->r[chip]->r[CD180_EOSRR]);
807                                 }
808                         }
809                 }
810         }
811 #endif
812
813 /* This is the old handling routine, used in riscom8 for only one CD180. I keep it here for reference. */
814 #if 0
815         for(chip=0;chip<AURORA_NCD180;chip++){
816                 chip_id=(board_No(bp)<<5)|((chip+1)<<3);
817                 loop=0;
818                 while ((loop++ < 1) &&
819                        ((status = sbus_readb(&bp->r[chip]->r[CD180_SRSR])) &
820                         (SRSR_TEXT | SRSR_MEXT | SRSR_REXT))) {
821
822                         if (status & SRSR_REXT) {
823                                 ack = sbus_readb(&bp->r3->r[bp->ACK_RINT]);
824                                 if (ack == (chip_id | GSVR_IT_RGD)) {
825 #ifdef AURORA_INTMSG
826                                         printk("RX ACK\n");
827 #endif
828                                         aurora_receive(bp,chip);
829                                 } else if (ack == (chip_id | GSVR_IT_REXC)) {
830 #ifdef AURORA_INTMSG
831                                         printk("RXC ACK\n");
832 #endif
833                                         aurora_receive_exc(bp,chip);
834                                 } else {
835 #ifdef AURORA_INTNORM
836                                         printk("aurora%d-%d: Bad receive ack 0x%02x.\n",
837                                                board_No(bp), chip, ack);
838 #endif
839                                 }
840                         } else if (status & SRSR_TEXT) {
841                                 ack = sbus_readb(&bp->r3->r[bp->ACK_TINT]);
842                                 if (ack == (chip_id | GSVR_IT_TX)){
843 #ifdef AURORA_INTMSG
844                                         printk("TX ACK\n");
845 #endif
846                                         aurora_transmit(bp,chip);
847                                 } else {
848 #ifdef AURORA_INTNORM
849                                         printk("aurora%d-%d: Bad transmit ack 0x%02x.\n",
850                                                board_No(bp), chip, ack);
851 #endif
852                                 }
853                         } else  if (status & SRSR_MEXT)  {
854                                 ack = sbus_readb(&bp->r3->r[bp->ACK_MINT]);
855                                 if (ack == (chip_id | GSVR_IT_MDM)){
856 #ifdef AURORA_INTMSG
857                                         printk("MDM ACK\n");
858 #endif
859                                         aurora_check_modem(bp,chip);
860                                 } else {
861 #ifdef AURORA_INTNORM
862                                         printk("aurora%d-%d: Bad modem ack 0x%02x.\n",
863                                                board_No(bp), chip, ack);
864 #endif
865                                 }
866                         }
867                         sbus_writeb(0, &bp->r[chip]->r[CD180_EOSRR]);
868                 }
869         }
870 #endif
871
872         return IRQ_HANDLED;
873 }
874
875 #ifdef AURORA_INT_DEBUG
876 static void aurora_timer (unsigned long ignored);
877
878 static struct timer_list aurora_poll_timer =
879                         TIMER_INITIALIZER(aurora_timer, 0, 0);
880
881 static void
882 aurora_timer (unsigned long ignored)
883 {
884         unsigned long flags;
885         int i;
886
887         save_flags(flags); cli();
888
889         printk("SRSR: %02x,%02x - ",
890                sbus_readb(&aurora_board[0].r[0]->r[CD180_SRSR]),
891                sbus_readb(&aurora_board[0].r[1]->r[CD180_SRSR]));
892         for (i = 0; i < 4; i++) {
893                 udelay(1);
894                 printk("%02x ",
895                        sbus_readb(&aurora_board[0].r3->r[i]));
896         }
897         printk("\n");
898
899         aurora_poll_timer.expires = jiffies + 300;
900         add_timer (&aurora_poll_timer);
901
902         restore_flags(flags);
903 }
904 #endif
905
906 /*
907  *  Routines for open & close processing.
908  */
909
910 /* Called with disabled interrupts */
911 static int aurora_setup_board(struct Aurora_board * bp)
912 {
913         int error;
914         
915 #ifdef AURORA_ALLIRQ
916         int i;
917         for (i = 0; i < AURORA_ALLIRQ; i++) {
918                 error = request_irq(allirq[i]|0x30, aurora_interrupt, SA_SHIRQ,
919                                     "sio16", bp);
920                 if (error)
921                         printk(KERN_ERR "IRQ%d request error %d\n",
922                                allirq[i], error);
923         }
924 #else
925         error = request_irq(bp->irq|0x30, aurora_interrupt, SA_SHIRQ,
926                             "sio16", bp);
927         if (error) {
928                 printk(KERN_ERR "IRQ request error %d\n", error);
929                 return error;
930         }
931 #endif
932         /* Board reset */
933         sbus_writeb(0, &bp->r0->r);
934         udelay(1);
935         if (bp->flags & AURORA_BOARD_TYPE_2) {
936                 /* unknown yet */
937         } else {
938                 sbus_writeb((AURORA_CFG_ENABLE_IO | AURORA_CFG_ENABLE_IRQ |
939                              (((bp->irq)&0x0f)>>2)),
940                             &bp->r0->r);
941         }
942         udelay(10000);
943
944         if (aurora_init_CD180(bp,0))error=1;error=0;
945         if (aurora_init_CD180(bp,1))error++;
946         if (error == AURORA_NCD180) {
947                 printk(KERN_ERR "Both chips failed initialisation.\n");
948                 return -EIO;
949         }
950
951 #ifdef AURORA_INT_DEBUG
952         aurora_poll_timer.expires= jiffies + 1;
953         add_timer(&aurora_poll_timer);
954 #endif
955 #ifdef AURORA_DEBUG
956         printk("aurora_setup_board: end\n");
957 #endif
958         return 0;
959 }
960
961 /* Called with disabled interrupts */
962 static void aurora_shutdown_board(struct Aurora_board *bp)
963 {
964         int i;
965
966 #ifdef AURORA_DEBUG
967         printk("aurora_shutdown_board: start\n");
968 #endif
969
970 #ifdef AURORA_INT_DEBUG
971         del_timer(&aurora_poll_timer);
972 #endif
973
974 #ifdef AURORA_ALLIRQ
975         for(i=0;i<AURORA_ALLIRQ;i++){
976                 free_irq(allirq[i]|0x30, bp);
977 /*              IRQ_to_board[allirq[i]&0xf] = NULL;*/
978         }
979 #else
980         free_irq(bp->irq|0x30, bp);
981 /*      IRQ_to_board[bp->irq&0xf] = NULL;*/
982 #endif  
983         /* Drop all DTR's */
984         for(i=0;i<16;i++){
985                 sbus_writeb(i & 7, &bp->r[i>>3]->r[CD180_CAR]);
986                 udelay(1);
987                 sbus_writeb(0, &bp->r[i>>3]->r[CD180_MSVR]);
988                 udelay(1);
989         }
990         /* Board shutdown */
991         sbus_writeb(0, &bp->r0->r);
992
993 #ifdef AURORA_DEBUG
994         printk("aurora_shutdown_board: end\n");
995 #endif
996 }
997
998 /* Setting up port characteristics. 
999  * Must be called with disabled interrupts
1000  */
1001 static void aurora_change_speed(struct Aurora_board *bp, struct Aurora_port *port)
1002 {
1003         struct tty_struct *tty;
1004         unsigned long baud;
1005         long tmp;
1006         unsigned char cor1 = 0, cor3 = 0;
1007         unsigned char mcor1 = 0, mcor2 = 0,chip;
1008         
1009 #ifdef AURORA_DEBUG
1010         printk("aurora_change_speed: start\n");
1011 #endif
1012         if (!(tty = port->tty) || !tty->termios)
1013                 return;
1014                 
1015         chip = AURORA_CD180(port_No(port));
1016
1017         port->SRER  = 0;
1018         port->COR2 = 0;
1019         port->MSVR = MSVR_RTS|MSVR_DTR;
1020         
1021         baud = tty_get_baud_rate(tty);
1022         
1023         /* Select port on the board */
1024         sbus_writeb(port_No(port) & 7,
1025                     &bp->r[chip]->r[CD180_CAR]);
1026         udelay(1);
1027         
1028         if (!baud)  {
1029                 /* Drop DTR & exit */
1030                 port->MSVR &= ~(bp->DTR|bp->RTS);
1031                 sbus_writeb(port->MSVR,
1032                             &bp->r[chip]->r[CD180_MSVR]);
1033                 return;
1034         } else  {
1035                 /* Set DTR on */
1036                 port->MSVR |= bp->DTR;
1037                 sbus_writeb(port->MSVR,
1038                             &bp->r[chip]->r[CD180_MSVR]);
1039         }
1040         
1041         /* Now we must calculate some speed dependent things. */
1042         
1043         /* Set baud rate for port. */
1044         tmp = (((bp->oscfreq + baud/2) / baud +
1045                 CD180_TPC/2) / CD180_TPC);
1046
1047 /*      tmp = (bp->oscfreq/7)/baud;
1048         if((tmp%10)>4)tmp=tmp/10+1;else tmp=tmp/10;*/
1049 /*      printk("Prescaler period: %d\n",tmp);*/
1050
1051         sbus_writeb((tmp >> 8) & 0xff,
1052                     &bp->r[chip]->r[CD180_RBPRH]);
1053         sbus_writeb((tmp >> 8) & 0xff,
1054                     &bp->r[chip]->r[CD180_TBPRH]);
1055         sbus_writeb(tmp & 0xff, &bp->r[chip]->r[CD180_RBPRL]);
1056         sbus_writeb(tmp & 0xff, &bp->r[chip]->r[CD180_TBPRL]);
1057         
1058         baud = (baud + 5) / 10;   /* Estimated CPS */
1059         
1060         /* Two timer ticks seems enough to wakeup something like SLIP driver */
1061         tmp = ((baud + HZ/2) / HZ) * 2 - CD180_NFIFO;           
1062         port->wakeup_chars = (tmp < 0) ? 0 : ((tmp >= SERIAL_XMIT_SIZE) ?
1063                                               SERIAL_XMIT_SIZE - 1 : tmp);
1064         
1065         /* Receiver timeout will be transmission time for 1.5 chars */
1066         tmp = (AURORA_TPS + AURORA_TPS/2 + baud/2) / baud;
1067         tmp = (tmp > 0xff) ? 0xff : tmp;
1068         sbus_writeb(tmp, &bp->r[chip]->r[CD180_RTPR]);
1069         
1070         switch (C_CSIZE(tty))  {
1071          case CS5:
1072                 cor1 |= COR1_5BITS;
1073                 break;
1074          case CS6:
1075                 cor1 |= COR1_6BITS;
1076                 break;
1077          case CS7:
1078                 cor1 |= COR1_7BITS;
1079                 break;
1080          case CS8:
1081                 cor1 |= COR1_8BITS;
1082                 break;
1083         }
1084         
1085         if (C_CSTOPB(tty)) 
1086                 cor1 |= COR1_2SB;
1087         
1088         cor1 |= COR1_IGNORE;
1089         if (C_PARENB(tty))  {
1090                 cor1 |= COR1_NORMPAR;
1091                 if (C_PARODD(tty)) 
1092                         cor1 |= COR1_ODDP;
1093                 if (I_INPCK(tty)) 
1094                         cor1 &= ~COR1_IGNORE;
1095         }
1096         /* Set marking of some errors */
1097         port->mark_mask = RCSR_OE | RCSR_TOUT;
1098         if (I_INPCK(tty)) 
1099                 port->mark_mask |= RCSR_FE | RCSR_PE;
1100         if (I_BRKINT(tty) || I_PARMRK(tty)) 
1101                 port->mark_mask |= RCSR_BREAK;
1102         if (I_IGNPAR(tty)) 
1103                 port->mark_mask &= ~(RCSR_FE | RCSR_PE);
1104         if (I_IGNBRK(tty))  {
1105                 port->mark_mask &= ~RCSR_BREAK;
1106                 if (I_IGNPAR(tty)) 
1107                         /* Real raw mode. Ignore all */
1108                         port->mark_mask &= ~RCSR_OE;
1109         }
1110         /* Enable Hardware Flow Control */
1111         if (C_CRTSCTS(tty))  {
1112 /*#ifdef AURORA_BRAIN_DAMAGED_CTS
1113                 port->SRER |= SRER_DSR | SRER_CTS;
1114                 mcor1 |= MCOR1_DSRZD | MCOR1_CTSZD;
1115                 mcor2 |= MCOR2_DSROD | MCOR2_CTSOD;
1116                 tty->hw_stopped = !(aurora_in(bp, CD180_MSVR) & (MSVR_CTS|MSVR_DSR));
1117 #else*/
1118                 port->COR2 |= COR2_CTSAE;
1119 /*#endif*/
1120                 if (bp->flags&AURORA_BOARD_DTR_FLOW_OK) {
1121                         mcor1 |= AURORA_RXTH;
1122                 }
1123         }
1124         /* Enable Software Flow Control. FIXME: I'm not sure about this */
1125         /* Some people reported that it works, but I still doubt */
1126         if (I_IXON(tty))  {
1127                 port->COR2 |= COR2_TXIBE;
1128                 cor3 |= (COR3_FCT | COR3_SCDE);
1129                 if (I_IXANY(tty))
1130                         port->COR2 |= COR2_IXM;
1131                 sbus_writeb(START_CHAR(tty),
1132                             &bp->r[chip]->r[CD180_SCHR1]);
1133                 sbus_writeb(STOP_CHAR(tty),
1134                             &bp->r[chip]->r[CD180_SCHR2]);
1135                 sbus_writeb(START_CHAR(tty),
1136                             &bp->r[chip]->r[CD180_SCHR3]);
1137                 sbus_writeb(STOP_CHAR(tty),
1138                             &bp->r[chip]->r[CD180_SCHR4]);
1139         }
1140         if (!C_CLOCAL(tty))  {
1141                 /* Enable CD check */
1142                 port->SRER |= SRER_CD;
1143                 mcor1 |= MCOR1_CDZD;
1144                 mcor2 |= MCOR2_CDOD;
1145         }
1146         
1147         if (C_CREAD(tty)) 
1148                 /* Enable receiver */
1149                 port->SRER |= SRER_RXD;
1150         
1151         /* Set input FIFO size (1-8 bytes) */
1152         cor3 |= AURORA_RXFIFO; 
1153         /* Setting up CD180 channel registers */
1154         sbus_writeb(cor1, &bp->r[chip]->r[CD180_COR1]);
1155         sbus_writeb(port->COR2, &bp->r[chip]->r[CD180_COR2]);
1156         sbus_writeb(cor3, &bp->r[chip]->r[CD180_COR3]);
1157         /* Make CD180 know about registers change */
1158         aurora_wait_CCR(bp->r[chip]);
1159         sbus_writeb(CCR_CORCHG1 | CCR_CORCHG2 | CCR_CORCHG3,
1160                     &bp->r[chip]->r[CD180_CCR]);
1161         /* Setting up modem option registers */
1162         sbus_writeb(mcor1, &bp->r[chip]->r[CD180_MCOR1]);
1163         sbus_writeb(mcor2, &bp->r[chip]->r[CD180_MCOR2]);
1164         /* Enable CD180 transmitter & receiver */
1165         aurora_wait_CCR(bp->r[chip]);
1166         sbus_writeb(CCR_TXEN | CCR_RXEN, &bp->r[chip]->r[CD180_CCR]);
1167         /* Enable interrupts */
1168         sbus_writeb(port->SRER, &bp->r[chip]->r[CD180_SRER]);
1169         /* And finally set RTS on */
1170         sbus_writeb(port->MSVR, &bp->r[chip]->r[CD180_MSVR]);
1171 #ifdef AURORA_DEBUG
1172         printk("aurora_change_speed: end\n");
1173 #endif
1174 }
1175
1176 /* Must be called with interrupts enabled */
1177 static int aurora_setup_port(struct Aurora_board *bp, struct Aurora_port *port)
1178 {
1179         unsigned long flags;
1180         
1181 #ifdef AURORA_DEBUG
1182         printk("aurora_setup_port: start %d\n",port_No(port));
1183 #endif
1184         if (port->flags & ASYNC_INITIALIZED)
1185                 return 0;
1186                 
1187         if (!port->xmit_buf) {
1188                 /* We may sleep in get_zeroed_page() */
1189                 unsigned long tmp;
1190                 
1191                 if (!(tmp = get_zeroed_page(GFP_KERNEL)))
1192                         return -ENOMEM;
1193                     
1194                 if (port->xmit_buf) {
1195                         free_page(tmp);
1196                         return -ERESTARTSYS;
1197                 }
1198                 port->xmit_buf = (unsigned char *) tmp;
1199         }
1200                 
1201         save_flags(flags); cli();
1202                 
1203         if (port->tty) 
1204                 clear_bit(TTY_IO_ERROR, &port->tty->flags);
1205                 
1206 #ifdef MODULE
1207         if ((port->count == 1) && ((++bp->count) == 1))
1208                         bp->flags |= AURORA_BOARD_ACTIVE;
1209 #endif
1210
1211         port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1212         aurora_change_speed(bp, port);
1213         port->flags |= ASYNC_INITIALIZED;
1214                 
1215         restore_flags(flags);
1216 #ifdef AURORA_DEBUG
1217         printk("aurora_setup_port: end\n");
1218 #endif
1219         return 0;
1220 }
1221
1222 /* Must be called with interrupts disabled */
1223 static void aurora_shutdown_port(struct Aurora_board *bp, struct Aurora_port *port)
1224 {
1225         struct tty_struct *tty;
1226         unsigned char chip;
1227
1228 #ifdef AURORA_DEBUG
1229         printk("aurora_shutdown_port: start\n");
1230 #endif
1231         if (!(port->flags & ASYNC_INITIALIZED)) 
1232                 return;
1233         
1234         chip = AURORA_CD180(port_No(port));
1235         
1236 #ifdef AURORA_REPORT_OVERRUN
1237         printk("aurora%d: port %d: Total %ld overruns were detected.\n",
1238                board_No(bp), port_No(port), port->overrun);
1239 #endif  
1240 #ifdef AURORA_REPORT_FIFO
1241         {
1242                 int i;
1243                 
1244                 printk("aurora%d: port %d: FIFO hits [ ",
1245                        board_No(bp), port_No(port));
1246                 for (i = 0; i < 10; i++)  {
1247                         printk("%ld ", port->hits[i]);
1248                 }
1249                 printk("].\n");
1250         }
1251 #endif  
1252         if (port->xmit_buf)  {
1253                 free_page((unsigned long) port->xmit_buf);
1254                 port->xmit_buf = NULL;
1255         }
1256
1257         if (!(tty = port->tty) || C_HUPCL(tty))  {
1258                 /* Drop DTR */
1259                 port->MSVR &= ~(bp->DTR|bp->RTS);
1260                 sbus_writeb(port->MSVR,
1261                             &bp->r[chip]->r[CD180_MSVR]);
1262         }
1263         
1264         /* Select port */
1265         sbus_writeb(port_No(port) & 7,
1266                     &bp->r[chip]->r[CD180_CAR]);
1267         udelay(1);
1268
1269         /* Reset port */
1270         aurora_wait_CCR(bp->r[chip]);
1271         sbus_writeb(CCR_SOFTRESET, &bp->r[chip]->r[CD180_CCR]);
1272
1273         /* Disable all interrupts from this port */
1274         port->SRER = 0;
1275         sbus_writeb(port->SRER, &bp->r[chip]->r[CD180_SRER]);
1276         
1277         if (tty)  
1278                 set_bit(TTY_IO_ERROR, &tty->flags);
1279         port->flags &= ~ASYNC_INITIALIZED;
1280
1281 #ifdef MODULE
1282         if (--bp->count < 0)  {
1283                 printk(KERN_DEBUG "aurora%d: aurora_shutdown_port: "
1284                        "bad board count: %d\n",
1285                        board_No(bp), bp->count);
1286                 bp->count = 0;
1287         }
1288         
1289         if (!bp->count)
1290                 bp->flags &= ~AURORA_BOARD_ACTIVE;
1291 #endif
1292
1293 #ifdef AURORA_DEBUG
1294         printk("aurora_shutdown_port: end\n");
1295 #endif
1296 }
1297
1298         
1299 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1300                            struct Aurora_port *port)
1301 {
1302         DECLARE_WAITQUEUE(wait, current);
1303         struct Aurora_board *bp = port_Board(port);
1304         int    retval;
1305         int    do_clocal = 0;
1306         int    CD;
1307         unsigned char chip;
1308         
1309 #ifdef AURORA_DEBUG
1310         printk("block_til_ready: start\n");
1311 #endif
1312         chip = AURORA_CD180(port_No(port));
1313
1314         /* If the device is in the middle of being closed, then block
1315          * until it's done, and then try again.
1316          */
1317         if (tty_hung_up_p(filp) || port->flags & ASYNC_CLOSING) {
1318                 interruptible_sleep_on(&port->close_wait);
1319                 if (port->flags & ASYNC_HUP_NOTIFY)
1320                         return -EAGAIN;
1321                 else
1322                         return -ERESTARTSYS;
1323         }
1324
1325         /* If non-blocking mode is set, or the port is not enabled,
1326          * then make the check up front and then exit.
1327          */
1328         if ((filp->f_flags & O_NONBLOCK) ||
1329             (tty->flags & (1 << TTY_IO_ERROR))) {
1330                 port->flags |= ASYNC_NORMAL_ACTIVE;
1331                 return 0;
1332         }
1333
1334         if (C_CLOCAL(tty))  
1335                 do_clocal = 1;
1336
1337         /* Block waiting for the carrier detect and the line to become
1338          * free (i.e., not in use by the callout).  While we are in
1339          * this loop, info->count is dropped by one, so that
1340          * rs_close() knows when to free things.  We restore it upon
1341          * exit, either normal or abnormal.
1342          */
1343         retval = 0;
1344         add_wait_queue(&port->open_wait, &wait);
1345         cli();
1346         if (!tty_hung_up_p(filp))
1347                 port->count--;
1348         sti();
1349         port->blocked_open++;
1350         while (1) {
1351                 cli();
1352                 sbus_writeb(port_No(port) & 7,
1353                             &bp->r[chip]->r[CD180_CAR]);
1354                 udelay(1);
1355                 CD = sbus_readb(&bp->r[chip]->r[CD180_MSVR]) & MSVR_CD;
1356                 port->MSVR=bp->RTS;
1357
1358                 /* auto drops DTR */
1359                 sbus_writeb(port->MSVR, &bp->r[chip]->r[CD180_MSVR]);
1360                 sti();
1361                 set_current_state(TASK_INTERRUPTIBLE);
1362                 if (tty_hung_up_p(filp) ||
1363                     !(port->flags & ASYNC_INITIALIZED)) {
1364                         if (port->flags & ASYNC_HUP_NOTIFY)
1365                                 retval = -EAGAIN;
1366                         else
1367                                 retval = -ERESTARTSYS;  
1368                         break;
1369                 }
1370                 if (!(port->flags & ASYNC_CLOSING) &&
1371                     (do_clocal || CD))
1372                         break;
1373                 if (signal_pending(current)) {
1374                         retval = -ERESTARTSYS;
1375                         break;
1376                 }
1377                 schedule();
1378         }
1379         current->state = TASK_RUNNING;
1380         remove_wait_queue(&port->open_wait, &wait);
1381         if (!tty_hung_up_p(filp))
1382                 port->count++;
1383         port->blocked_open--;
1384         if (retval)
1385                 return retval;
1386         
1387         port->flags |= ASYNC_NORMAL_ACTIVE;
1388 #ifdef AURORA_DEBUG
1389         printk("block_til_ready: end\n");
1390 #endif
1391         return 0;
1392 }       
1393
1394 static int aurora_open(struct tty_struct * tty, struct file * filp)
1395 {
1396         int board;
1397         int error;
1398         struct Aurora_port * port;
1399         struct Aurora_board * bp;
1400         unsigned long flags;
1401         
1402 #ifdef AURORA_DEBUG
1403         printk("aurora_open: start\n");
1404 #endif
1405         
1406         board = AURORA_BOARD(tty->index);
1407         if (board > AURORA_NBOARD ||
1408             !(aurora_board[board].flags & AURORA_BOARD_PRESENT)) {
1409 #ifdef AURORA_DEBUG
1410                 printk("aurora_open: error board %d present %d\n",
1411                        board, aurora_board[board].flags & AURORA_BOARD_PRESENT);
1412 #endif
1413                 return -ENODEV;
1414         }
1415         
1416         bp = &aurora_board[board];
1417         port = aurora_port + board * AURORA_NPORT * AURORA_NCD180 + AURORA_PORT(tty->index);
1418         if ((aurora_paranoia_check(port, tty->name, "aurora_open")) {
1419 #ifdef AURORA_DEBUG
1420                 printk("aurora_open: error paranoia check\n");
1421 #endif
1422                 return -ENODEV;
1423         }
1424         
1425         port->count++;
1426         tty->driver_data = port;
1427         port->tty = tty;
1428         
1429         if ((error = aurora_setup_port(bp, port))) {
1430 #ifdef AURORA_DEBUG
1431                 printk("aurora_open: error aurora_setup_port ret %d\n",error);
1432 #endif
1433                 return error;
1434         }
1435
1436         if ((error = block_til_ready(tty, filp, port))) {
1437 #ifdef AURORA_DEBUG
1438                 printk("aurora_open: error block_til_ready ret %d\n",error);
1439 #endif
1440                 return error;
1441         }
1442         
1443 #ifdef AURORA_DEBUG
1444         printk("aurora_open: end\n");
1445 #endif
1446         return 0;
1447 }
1448
1449 static void aurora_close(struct tty_struct * tty, struct file * filp)
1450 {
1451         struct Aurora_port *port = (struct Aurora_port *) tty->driver_data;
1452         struct Aurora_board *bp;
1453         unsigned long flags;
1454         unsigned long timeout;
1455         unsigned char chip;
1456         
1457 #ifdef AURORA_DEBUG
1458         printk("aurora_close: start\n");
1459 #endif
1460         
1461         if (!port || (aurora_paranoia_check(port, tty->name, "close"))
1462                 return;
1463         
1464         chip = AURORA_CD180(port_No(port));
1465
1466         save_flags(flags); cli();
1467         if (tty_hung_up_p(filp))  {
1468                 restore_flags(flags);
1469                 return;
1470         }
1471         
1472         bp = port_Board(port);
1473         if ((tty->count == 1) && (port->count != 1))  {
1474                 printk(KERN_DEBUG "aurora%d: aurora_close: bad port count; "
1475                        "tty->count is 1, port count is %d\n",
1476                        board_No(bp), port->count);
1477                 port->count = 1;
1478         }
1479         if (--port->count < 0)  {
1480                 printk(KERN_DEBUG "aurora%d: aurora_close: bad port "
1481                        "count for tty%d: %d\n",
1482                        board_No(bp), port_No(port), port->count);
1483                 port->count = 0;
1484         }
1485         if (port->count)  {
1486                 restore_flags(flags);
1487                 return;
1488         }
1489         port->flags |= ASYNC_CLOSING;
1490
1491         /* Now we wait for the transmit buffer to clear; and we notify 
1492          * the line discipline to only process XON/XOFF characters.
1493          */
1494         tty->closing = 1;
1495         if (port->closing_wait != ASYNC_CLOSING_WAIT_NONE){
1496 #ifdef AURORA_DEBUG
1497                 printk("aurora_close: waiting to flush...\n");
1498 #endif
1499                 tty_wait_until_sent(tty, port->closing_wait);
1500         }
1501
1502         /* At this point we stop accepting input.  To do this, we
1503          * disable the receive line status interrupts, and tell the
1504          * interrupt driver to stop checking the data ready bit in the
1505          * line status register.
1506          */
1507         port->SRER &= ~SRER_RXD;
1508         if (port->flags & ASYNC_INITIALIZED) {
1509                 port->SRER &= ~SRER_TXRDY;
1510                 port->SRER |= SRER_TXEMPTY;
1511                 sbus_writeb(port_No(port) & 7,
1512                             &bp->r[chip]->r[CD180_CAR]);
1513                 udelay(1);
1514                 sbus_writeb(port->SRER, &bp->r[chip]->r[CD180_SRER]);
1515                 /*
1516                  * Before we drop DTR, make sure the UART transmitter
1517                  * has completely drained; this is especially
1518                  * important if there is a transmit FIFO!
1519                  */
1520                 timeout = jiffies+HZ;
1521                 while(port->SRER & SRER_TXEMPTY)  {
1522                         current->state = TASK_INTERRUPTIBLE;
1523                         schedule_timeout(port->timeout);
1524                         if (time_after(jiffies, timeout))
1525                                 break;
1526                 }
1527         }
1528 #ifdef AURORA_DEBUG
1529         printk("aurora_close: shutdown_port\n");
1530 #endif
1531         aurora_shutdown_port(bp, port);
1532         if (tty->driver->flush_buffer)
1533                 tty->driver->flush_buffer(tty);
1534         if (tty->ldisc.flush_buffer)
1535                 tty->ldisc.flush_buffer(tty);
1536         tty->closing = 0;
1537         port->event = 0;
1538         port->tty = 0;
1539         if (port->blocked_open) {
1540                 if (port->close_delay) {
1541                         current->state = TASK_INTERRUPTIBLE;
1542                         schedule_timeout(port->close_delay);
1543                 }
1544                 wake_up_interruptible(&port->open_wait);
1545         }
1546         port->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1547         wake_up_interruptible(&port->close_wait);
1548         restore_flags(flags);
1549 #ifdef AURORA_DEBUG
1550         printk("aurora_close: end\n");
1551 #endif
1552 }
1553
1554 static int aurora_write(struct tty_struct * tty, int from_user, 
1555                         const unsigned char *buf, int count)
1556 {
1557         struct Aurora_port *port = (struct Aurora_port *) tty->driver_data;
1558         struct Aurora_board *bp;
1559         int c, total = 0;
1560         unsigned long flags;
1561         unsigned char chip;
1562
1563 #ifdef AURORA_DEBUG
1564         printk("aurora_write: start %d\n",count);
1565 #endif
1566         if ((aurora_paranoia_check(port, tty->name, "aurora_write"))
1567                 return 0;
1568                 
1569         chip = AURORA_CD180(port_No(port));
1570         
1571         bp = port_Board(port);
1572
1573         if (!tty || !port->xmit_buf || !tmp_buf)
1574                 return 0;
1575
1576         save_flags(flags);
1577         if (from_user) {
1578                 down(&tmp_buf_sem);
1579                 while (1) {
1580                         c = MIN(count, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1581                                            SERIAL_XMIT_SIZE - port->xmit_head));
1582                         if (c <= 0)
1583                                 break;
1584
1585                         c -= copy_from_user(tmp_buf, buf, c);
1586                         if (!c) {
1587                                 if (!total)
1588                                         total = -EFAULT;
1589                                 break;
1590                         }
1591                         cli();
1592                         c = MIN(c, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1593                                        SERIAL_XMIT_SIZE - port->xmit_head));
1594                         memcpy(port->xmit_buf + port->xmit_head, tmp_buf, c);
1595                         port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1596                         port->xmit_cnt += c;
1597                         restore_flags(flags);
1598
1599                         buf += c;
1600                         count -= c;
1601                         total += c;
1602                 }
1603                 up(&tmp_buf_sem);
1604         } else {
1605                 while (1) {
1606                         cli();
1607                         c = MIN(count, MIN(SERIAL_XMIT_SIZE - port->xmit_cnt - 1,
1608                                            SERIAL_XMIT_SIZE - port->xmit_head));
1609                         if (c <= 0) {
1610                                 restore_flags(flags);
1611                                 break;
1612                         }
1613                         memcpy(port->xmit_buf + port->xmit_head, buf, c);
1614                         port->xmit_head = (port->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
1615                         port->xmit_cnt += c;
1616                         restore_flags(flags);
1617
1618                         buf += c;
1619                         count -= c;
1620                         total += c;
1621                 }
1622         }
1623
1624         cli();
1625         if (port->xmit_cnt && !tty->stopped && !tty->hw_stopped &&
1626             !(port->SRER & SRER_TXRDY)) {
1627                 port->SRER |= SRER_TXRDY;
1628                 sbus_writeb(port_No(port) & 7,
1629                             &bp->r[chip]->r[CD180_CAR]);
1630                 udelay(1);
1631                 sbus_writeb(port->SRER, &bp->r[chip]->r[CD180_SRER]);
1632         }
1633         restore_flags(flags);
1634 #ifdef AURORA_DEBUG
1635         printk("aurora_write: end %d\n",total);
1636 #endif
1637         return total;
1638 }
1639
1640 static void aurora_put_char(struct tty_struct * tty, unsigned char ch)
1641 {
1642         struct Aurora_port *port = (struct Aurora_port *) tty->driver_data;
1643         unsigned long flags;
1644
1645 #ifdef AURORA_DEBUG
1646         printk("aurora_put_char: start %c\n",ch);
1647 #endif
1648         if ((aurora_paranoia_check(port, tty->name, "aurora_put_char"))
1649                 return;
1650
1651         if (!tty || !port->xmit_buf)
1652                 return;
1653
1654         save_flags(flags); cli();
1655         
1656         if (port->xmit_cnt >= SERIAL_XMIT_SIZE - 1) {
1657                 restore_flags(flags);
1658                 return;
1659         }
1660
1661         port->xmit_buf[port->xmit_head++] = ch;
1662         port->xmit_head &= SERIAL_XMIT_SIZE - 1;
1663         port->xmit_cnt++;
1664         restore_flags(flags);
1665 #ifdef AURORA_DEBUG
1666         printk("aurora_put_char: end\n");
1667 #endif
1668 }
1669
1670 static void aurora_flush_chars(struct tty_struct * tty)
1671 {
1672         struct Aurora_port *port = (struct Aurora_port *) tty->driver_data;
1673         unsigned long flags;
1674         unsigned char chip;
1675
1676 /*#ifdef AURORA_DEBUG
1677         printk("aurora_flush_chars: start\n");
1678 #endif*/
1679         if ((aurora_paranoia_check(port, tty->name, "aurora_flush_chars"))
1680                 return;
1681                 
1682         chip = AURORA_CD180(port_No(port));
1683         
1684         if (port->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
1685             !port->xmit_buf)
1686                 return;
1687
1688         save_flags(flags); cli();
1689         port->SRER |= SRER_TXRDY;
1690         sbus_writeb(port_No(port) & 7,
1691                     &port_Board(port)->r[chip]->r[CD180_CAR]);
1692         udelay(1);
1693         sbus_writeb(port->SRER,
1694                     &port_Board(port)->r[chip]->r[CD180_SRER]);
1695         restore_flags(flags);
1696 /*#ifdef AURORA_DEBUG
1697         printk("aurora_flush_chars: end\n");
1698 #endif*/
1699 }
1700
1701 static int aurora_write_room(struct tty_struct * tty)
1702 {
1703         struct Aurora_port *port = (struct Aurora_port *) tty->driver_data;
1704         int     ret;
1705
1706 #ifdef AURORA_DEBUG
1707         printk("aurora_write_room: start\n");
1708 #endif
1709         if ((aurora_paranoia_check(port, tty->name, "aurora_write_room"))
1710                 return 0;
1711
1712         ret = SERIAL_XMIT_SIZE - port->xmit_cnt - 1;
1713         if (ret < 0)
1714                 ret = 0;
1715 #ifdef AURORA_DEBUG
1716         printk("aurora_write_room: end\n");
1717 #endif
1718         return ret;
1719 }
1720
1721 static int aurora_chars_in_buffer(struct tty_struct *tty)
1722 {
1723         struct Aurora_port *port = (struct Aurora_port *) tty->driver_data;
1724                                 
1725         if ((aurora_paranoia_check(port, tty->name, "aurora_chars_in_buffer"))
1726                 return 0;
1727         
1728         return port->xmit_cnt;
1729 }
1730
1731 static void aurora_flush_buffer(struct tty_struct *tty)
1732 {
1733         struct Aurora_port *port = (struct Aurora_port *) tty->driver_data;
1734         unsigned long flags;
1735
1736 #ifdef AURORA_DEBUG
1737         printk("aurora_flush_buffer: start\n");
1738 #endif
1739         if ((aurora_paranoia_check(port, tty->name, "aurora_flush_buffer"))
1740                 return;
1741
1742         save_flags(flags); cli();
1743         port->xmit_cnt = port->xmit_head = port->xmit_tail = 0;
1744         restore_flags(flags);
1745         
1746         wake_up_interruptible(&tty->write_wait);
1747         if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1748             tty->ldisc.write_wakeup)
1749                 (tty->ldisc.write_wakeup)(tty);
1750 #ifdef AURORA_DEBUG
1751         printk("aurora_flush_buffer: end\n");
1752 #endif
1753 }
1754
1755 static int aurora_tiocmget(struct tty_struct *tty, struct file *file)
1756 {
1757         struct Aurora_port *port = (struct Aurora_port *) tty->driver_data;
1758         struct Aurora_board * bp;
1759         unsigned char status,chip;
1760         unsigned int result;
1761         unsigned long flags;
1762
1763 #ifdef AURORA_DEBUG
1764         printk("aurora_get_modem_info: start\n");
1765 #endif
1766         if ((aurora_paranoia_check(port, tty->name, __FUNCTION__))
1767                 return -ENODEV;
1768
1769         chip = AURORA_CD180(port_No(port));
1770
1771         bp = port_Board(port);
1772
1773         save_flags(flags); cli();
1774
1775         sbus_writeb(port_No(port) & 7, &bp->r[chip]->r[CD180_CAR]);
1776         udelay(1);
1777
1778         status = sbus_readb(&bp->r[chip]->r[CD180_MSVR]);
1779         result = 0/*bp->r[chip]->r[AURORA_RI] & (1u << port_No(port)) ? 0 : TIOCM_RNG*/;
1780
1781         restore_flags(flags);
1782
1783         result |= ((status & bp->RTS) ? TIOCM_RTS : 0)
1784                 | ((status & bp->DTR) ? TIOCM_DTR : 0)
1785                 | ((status & MSVR_CD)  ? TIOCM_CAR : 0)
1786                 | ((status & MSVR_DSR) ? TIOCM_DSR : 0)
1787                 | ((status & MSVR_CTS) ? TIOCM_CTS : 0);
1788
1789 #ifdef AURORA_DEBUG
1790         printk("aurora_get_modem_info: end\n");
1791 #endif
1792         return result;
1793 }
1794
1795 static int aurora_tiocmset(struct tty_struct *tty, struct file *file,
1796                            unsigned int set, unsigned int clear)
1797 {
1798         struct Aurora_port *port = (struct Aurora_port *) tty->driver_data;
1799         unsigned int arg;
1800         unsigned long flags;
1801         struct Aurora_board *bp = port_Board(port);
1802         unsigned char chip;
1803
1804 #ifdef AURORA_DEBUG
1805         printk("aurora_set_modem_info: start\n");
1806 #endif
1807         if ((aurora_paranoia_check(port, tty->name, __FUNCTION__))
1808                 return -ENODEV;
1809
1810         chip = AURORA_CD180(port_No(port));
1811
1812         save_flags(flags); cli();
1813         if (set & TIOCM_RTS)
1814                 port->MSVR |= bp->RTS;
1815         if (set & TIOCM_DTR)
1816                 port->MSVR |= bp->DTR;
1817         if (clear & TIOCM_RTS)
1818                 port->MSVR &= ~bp->RTS;
1819         if (clear & TIOCM_DTR)
1820                 port->MSVR &= ~bp->DTR;
1821
1822         sbus_writeb(port_No(port) & 7, &bp->r[chip]->r[CD180_CAR]);
1823         udelay(1);
1824
1825         sbus_writeb(port->MSVR, &bp->r[chip]->r[CD180_MSVR]);
1826
1827         restore_flags(flags);
1828 #ifdef AURORA_DEBUG
1829         printk("aurora_set_modem_info: end\n");
1830 #endif
1831         return 0;
1832 }
1833
1834 static void aurora_send_break(struct Aurora_port * port, unsigned long length)
1835 {
1836         struct Aurora_board *bp = port_Board(port);
1837         unsigned long flags;
1838         unsigned char chip;
1839         
1840 #ifdef AURORA_DEBUG
1841         printk("aurora_send_break: start\n");
1842 #endif
1843         chip = AURORA_CD180(port_No(port));
1844         
1845         save_flags(flags); cli();
1846
1847         port->break_length = AURORA_TPS / HZ * length;
1848         port->COR2 |= COR2_ETC;
1849         port->SRER  |= SRER_TXRDY;
1850         sbus_writeb(port_No(port) & 7, &bp->r[chip]->r[CD180_CAR]);
1851         udelay(1);
1852
1853         sbus_writeb(port->COR2, &bp->r[chip]->r[CD180_COR2]);
1854         sbus_writeb(port->SRER, &bp->r[chip]->r[CD180_SRER]);
1855         aurora_wait_CCR(bp->r[chip]);
1856
1857         sbus_writeb(CCR_CORCHG2, &bp->r[chip]->r[CD180_CCR]);
1858         aurora_wait_CCR(bp->r[chip]);
1859
1860         restore_flags(flags);
1861 #ifdef AURORA_DEBUG
1862         printk("aurora_send_break: end\n");
1863 #endif
1864 }
1865
1866 static int aurora_set_serial_info(struct Aurora_port * port,
1867                                   struct serial_struct * newinfo)
1868 {
1869         struct serial_struct tmp;
1870         struct Aurora_board *bp = port_Board(port);
1871         int change_speed;
1872         unsigned long flags;
1873
1874 #ifdef AURORA_DEBUG
1875         printk("aurora_set_serial_info: start\n");
1876 #endif
1877         if (copy_from_user(&tmp, newinfo, sizeof(tmp)))
1878                 return -EFAULT;
1879 #if 0   
1880         if ((tmp.irq != bp->irq) ||
1881             (tmp.port != bp->base) ||
1882             (tmp.type != PORT_CIRRUS) ||
1883             (tmp.baud_base != (bp->oscfreq + CD180_TPC/2) / CD180_TPC) ||
1884             (tmp.custom_divisor != 0) ||
1885             (tmp.xmit_fifo_size != CD180_NFIFO) ||
1886             (tmp.flags & ~AURORA_LEGAL_FLAGS))
1887                 return -EINVAL;
1888 #endif  
1889         
1890         change_speed = ((port->flags & ASYNC_SPD_MASK) !=
1891                         (tmp.flags & ASYNC_SPD_MASK));
1892         
1893         if (!capable(CAP_SYS_ADMIN)) {
1894                 if ((tmp.close_delay != port->close_delay) ||
1895                     (tmp.closing_wait != port->closing_wait) ||
1896                     ((tmp.flags & ~ASYNC_USR_MASK) !=
1897                      (port->flags & ~ASYNC_USR_MASK)))  
1898                         return -EPERM;
1899                 port->flags = ((port->flags & ~ASYNC_USR_MASK) |
1900                                (tmp.flags & ASYNC_USR_MASK));
1901         } else  {
1902                 port->flags = ((port->flags & ~ASYNC_FLAGS) |
1903                                (tmp.flags & ASYNC_FLAGS));
1904                 port->close_delay = tmp.close_delay;
1905                 port->closing_wait = tmp.closing_wait;
1906         }
1907         if (change_speed)  {
1908                 save_flags(flags); cli();
1909                 aurora_change_speed(bp, port);
1910                 restore_flags(flags);
1911         }
1912 #ifdef AURORA_DEBUG
1913         printk("aurora_set_serial_info: end\n");
1914 #endif
1915         return 0;
1916 }
1917
1918 extern int aurora_get_serial_info(struct Aurora_port * port,
1919                                   struct serial_struct * retinfo)
1920 {
1921         struct serial_struct tmp;
1922         struct Aurora_board *bp = port_Board(port);
1923         int error;
1924         
1925 #ifdef AURORA_DEBUG
1926         printk("aurora_get_serial_info: start\n");
1927 #endif
1928         error = verify_area(VERIFY_WRITE, (void *) retinfo, sizeof(tmp));
1929         if (error)
1930                 return error;
1931         
1932         memset(&tmp, 0, sizeof(tmp));
1933         tmp.type = PORT_CIRRUS;
1934         tmp.line = port - aurora_port;
1935         tmp.port = 0;
1936         tmp.irq  = bp->irq;
1937         tmp.flags = port->flags;
1938         tmp.baud_base = (bp->oscfreq + CD180_TPC/2) / CD180_TPC;
1939         tmp.close_delay = port->close_delay * HZ/100;
1940         tmp.closing_wait = port->closing_wait * HZ/100;
1941         tmp.xmit_fifo_size = CD180_NFIFO;
1942         copy_to_user(retinfo, &tmp, sizeof(tmp));
1943 #ifdef AURORA_DEBUG
1944 printk("aurora_get_serial_info: end\n");
1945 #endif
1946         return 0;
1947 }
1948
1949 static int aurora_ioctl(struct tty_struct * tty, struct file * filp, 
1950                     unsigned int cmd, unsigned long arg)
1951                     
1952 {
1953         struct Aurora_port *port = (struct Aurora_port *) tty->driver_data;
1954         int retval;
1955
1956 #ifdef AURORA_DEBUG
1957         printk("aurora_ioctl: start\n");
1958 #endif
1959         if ((aurora_paranoia_check(port, tty->name, "aurora_ioctl"))
1960                 return -ENODEV;
1961         
1962         switch (cmd) {
1963         case TCSBRK:    /* SVID version: non-zero arg --> no break */
1964                 retval = tty_check_change(tty);
1965                 if (retval)
1966                         return retval;
1967                 tty_wait_until_sent(tty, 0);
1968                 if (!arg)
1969                         aurora_send_break(port, HZ/4);  /* 1/4 second */
1970                 return 0;
1971         case TCSBRKP:   /* support for POSIX tcsendbreak() */
1972                 retval = tty_check_change(tty);
1973                 if (retval)
1974                         return retval;
1975                 tty_wait_until_sent(tty, 0);
1976                 aurora_send_break(port, arg ? arg*(HZ/10) : HZ/4);
1977                 return 0;
1978         case TIOCGSOFTCAR:
1979                 return put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *)arg);
1980         case TIOCSSOFTCAR:
1981                 if (get_user(arg,(unsigned long *)arg))
1982                         return -EFAULT;
1983                 tty->termios->c_cflag =
1984                         ((tty->termios->c_cflag & ~CLOCAL) |
1985                          (arg ? CLOCAL : 0));
1986                 return 0;
1987         case TIOCGSERIAL:       
1988                 return aurora_get_serial_info(port, (struct serial_struct *) arg);
1989         case TIOCSSERIAL:       
1990                 return aurora_set_serial_info(port, (struct serial_struct *) arg);
1991         default:
1992                 return -ENOIOCTLCMD;
1993         };
1994 #ifdef AURORA_DEBUG
1995         printk("aurora_ioctl: end\n");
1996 #endif
1997         return 0;
1998 }
1999
2000 static void aurora_throttle(struct tty_struct * tty)
2001 {
2002         struct Aurora_port *port = (struct Aurora_port *) tty->driver_data;
2003         struct Aurora_board *bp;
2004         unsigned long flags;
2005         unsigned char chip;
2006
2007 #ifdef AURORA_DEBUG
2008         printk("aurora_throttle: start\n");
2009 #endif
2010         if ((aurora_paranoia_check(port, tty->name, "aurora_throttle"))
2011                 return;
2012         
2013         bp = port_Board(port);
2014         chip = AURORA_CD180(port_No(port));
2015         
2016         save_flags(flags); cli();
2017         port->MSVR &= ~bp->RTS;
2018         sbus_writeb(port_No(port) & 7, &bp->r[chip]->r[CD180_CAR]);
2019         udelay(1);
2020         if (I_IXOFF(tty))  {
2021                 aurora_wait_CCR(bp->r[chip]);
2022                 sbus_writeb(CCR_SSCH2, &bp->r[chip]->r[CD180_CCR]);
2023                 aurora_wait_CCR(bp->r[chip]);
2024         }
2025         sbus_writeb(port->MSVR, &bp->r[chip]->r[CD180_MSVR]);
2026         restore_flags(flags);
2027 #ifdef AURORA_DEBUG
2028         printk("aurora_throttle: end\n");
2029 #endif
2030 }
2031
2032 static void aurora_unthrottle(struct tty_struct * tty)
2033 {
2034         struct Aurora_port *port = (struct Aurora_port *) tty->driver_data;
2035         struct Aurora_board *bp;
2036         unsigned long flags;
2037         unsigned char chip;
2038
2039 #ifdef AURORA_DEBUG
2040         printk("aurora_unthrottle: start\n");
2041 #endif
2042         if ((aurora_paranoia_check(port, tty->name, "aurora_unthrottle"))
2043                 return;
2044         
2045         bp = port_Board(port);
2046         
2047         chip = AURORA_CD180(port_No(port));
2048         
2049         save_flags(flags); cli();
2050         port->MSVR |= bp->RTS;
2051         sbus_writeb(port_No(port) & 7,
2052                     &bp->r[chip]->r[CD180_CAR]);
2053         udelay(1);
2054         if (I_IXOFF(tty))  {
2055                 aurora_wait_CCR(bp->r[chip]);
2056                 sbus_writeb(CCR_SSCH1,
2057                             &bp->r[chip]->r[CD180_CCR]);
2058                 aurora_wait_CCR(bp->r[chip]);
2059         }
2060         sbus_writeb(port->MSVR, &bp->r[chip]->r[CD180_MSVR]);
2061         restore_flags(flags);
2062 #ifdef AURORA_DEBUG
2063         printk("aurora_unthrottle: end\n");
2064 #endif
2065 }
2066
2067 static void aurora_stop(struct tty_struct * tty)
2068 {
2069         struct Aurora_port *port = (struct Aurora_port *) tty->driver_data;
2070         struct Aurora_board *bp;
2071         unsigned long flags;
2072         unsigned char chip;
2073
2074 #ifdef AURORA_DEBUG
2075         printk("aurora_stop: start\n");
2076 #endif
2077         if ((aurora_paranoia_check(port, tty->name, "aurora_stop"))
2078                 return;
2079         
2080         bp = port_Board(port);
2081         
2082         chip = AURORA_CD180(port_No(port));
2083         
2084         save_flags(flags); cli();
2085         port->SRER &= ~SRER_TXRDY;
2086         sbus_writeb(port_No(port) & 7,
2087                     &bp->r[chip]->r[CD180_CAR]);
2088         udelay(1);
2089         sbus_writeb(port->SRER,
2090                     &bp->r[chip]->r[CD180_SRER]);
2091         restore_flags(flags);
2092 #ifdef AURORA_DEBUG
2093         printk("aurora_stop: end\n");
2094 #endif
2095 }
2096
2097 static void aurora_start(struct tty_struct * tty)
2098 {
2099         struct Aurora_port *port = (struct Aurora_port *) tty->driver_data;
2100         struct Aurora_board *bp;
2101         unsigned long flags;
2102         unsigned char chip;
2103
2104 #ifdef AURORA_DEBUG
2105         printk("aurora_start: start\n");
2106 #endif
2107         if ((aurora_paranoia_check(port, tty->name, "aurora_start"))
2108                 return;
2109         
2110         bp = port_Board(port);
2111         
2112         chip = AURORA_CD180(port_No(port));
2113         
2114         save_flags(flags); cli();
2115         if (port->xmit_cnt && port->xmit_buf && !(port->SRER & SRER_TXRDY))  {
2116                 port->SRER |= SRER_TXRDY;
2117                 sbus_writeb(port_No(port) & 7,
2118                             &bp->r[chip]->r[CD180_CAR]);
2119                 udelay(1);
2120                 sbus_writeb(port->SRER,
2121                             &bp->r[chip]->r[CD180_SRER]);
2122         }
2123         restore_flags(flags);
2124 #ifdef AURORA_DEBUG
2125         printk("aurora_start: end\n");
2126 #endif
2127 }
2128
2129 /*
2130  * This routine is called from the scheduler tqueue when the interrupt
2131  * routine has signalled that a hangup has occurred.  The path of
2132  * hangup processing is:
2133  *
2134  *      serial interrupt routine -> (scheduler tqueue) ->
2135  *      do_aurora_hangup() -> tty->hangup() -> aurora_hangup()
2136  * 
2137  */
2138 static void do_aurora_hangup(void *private_)
2139 {
2140         struct Aurora_port      *port = (struct Aurora_port *) private_;
2141         struct tty_struct       *tty;
2142
2143 #ifdef AURORA_DEBUG
2144         printk("do_aurora_hangup: start\n");
2145 #endif
2146         tty = port->tty;
2147         if (tty != NULL) {
2148                 tty_hangup(tty);        /* FIXME: module removal race - AKPM */
2149 #ifdef AURORA_DEBUG
2150                 printk("do_aurora_hangup: end\n");
2151 #endif
2152         }
2153 }
2154
2155 static void aurora_hangup(struct tty_struct * tty)
2156 {
2157         struct Aurora_port *port = (struct Aurora_port *) tty->driver_data;
2158         struct Aurora_board *bp;
2159                                 
2160 #ifdef AURORA_DEBUG
2161         printk("aurora_hangup: start\n");
2162 #endif
2163         if ((aurora_paranoia_check(port, tty->name, "aurora_hangup"))
2164                 return;
2165         
2166         bp = port_Board(port);
2167         
2168         aurora_shutdown_port(bp, port);
2169         port->event = 0;
2170         port->count = 0;
2171         port->flags &= ~ASYNC_NORMAL_ACTIVE;
2172         port->tty = 0;
2173         wake_up_interruptible(&port->open_wait);
2174 #ifdef AURORA_DEBUG
2175         printk("aurora_hangup: end\n");
2176 #endif
2177 }
2178
2179 static void aurora_set_termios(struct tty_struct * tty, struct termios * old_termios)
2180 {
2181         struct Aurora_port *port = (struct Aurora_port *) tty->driver_data;
2182         unsigned long flags;
2183
2184 #ifdef AURORA_DEBUG
2185         printk("aurora_set_termios: start\n");
2186 #endif
2187         if ((aurora_paranoia_check(port, tty->name, "aurora_set_termios"))
2188                 return;
2189         
2190         if (tty->termios->c_cflag == old_termios->c_cflag &&
2191             tty->termios->c_iflag == old_termios->c_iflag)
2192                 return;
2193
2194         save_flags(flags); cli();
2195         aurora_change_speed(port_Board(port), port);
2196         restore_flags(flags);
2197
2198         if ((old_termios->c_cflag & CRTSCTS) &&
2199             !(tty->termios->c_cflag & CRTSCTS)) {
2200                 tty->hw_stopped = 0;
2201                 aurora_start(tty);
2202         }
2203 #ifdef AURORA_DEBUG
2204         printk("aurora_set_termios: end\n");
2205 #endif
2206 }
2207
2208 static void do_aurora_bh(void)
2209 {
2210          run_task_queue(&tq_aurora);
2211 }
2212
2213 static void do_softint(void *private_)
2214 {
2215         struct Aurora_port      *port = (struct Aurora_port *) private_;
2216         struct tty_struct       *tty;
2217
2218 #ifdef AURORA_DEBUG
2219         printk("do_softint: start\n");
2220 #endif
2221         tty = port->tty;
2222         if (tty == NULL)
2223                 return;
2224
2225         if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &port->event)) {
2226                 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
2227                     tty->ldisc.write_wakeup)
2228                         (tty->ldisc.write_wakeup)(tty);
2229                 wake_up_interruptible(&tty->write_wait);
2230         }
2231 #ifdef AURORA_DEBUG
2232         printk("do_softint: end\n");
2233 #endif
2234 }
2235
2236 static struct tty_operations aurora_ops = {
2237         .open  = aurora_open,
2238         .close = aurora_close,
2239         .write = aurora_write,
2240         .put_char = aurora_put_char,
2241         .flush_chars = aurora_flush_chars,
2242         .write_room = aurora_write_room,
2243         .chars_in_buffer = aurora_chars_in_buffer,
2244         .flush_buffer = aurora_flush_buffer,
2245         .ioctl = aurora_ioctl,
2246         .throttle = aurora_throttle,
2247         .unthrottle = aurora_unthrottle,
2248         .set_termios = aurora_set_termios,
2249         .stop = aurora_stop,
2250         .start = aurora_start,
2251         .hangup = aurora_hangup,
2252         .tiocmget = aurora_tiocmget,
2253         .tiocmset = aurora_tiocmset,
2254 };
2255
2256 static int aurora_init_drivers(void)
2257 {
2258         int error;
2259         int i;
2260
2261 #ifdef AURORA_DEBUG
2262         printk("aurora_init_drivers: start\n");
2263 #endif
2264         tmp_buf = (unsigned char *) get_zeroed_page(GFP_KERNEL);
2265         if (tmp_buf == NULL) {
2266                 printk(KERN_ERR "aurora: Couldn't get free page.\n");
2267                 return 1;
2268         }
2269         init_bh(AURORA_BH, do_aurora_bh);
2270         aurora_driver = alloc_tty_driver(AURORA_INPORTS);
2271         if (!aurora_driver) {
2272                 printk(KERN_ERR "aurora: Couldn't allocate tty driver.\n");
2273                 free_page((unsigned long) tmp_buf);
2274                 return 1;
2275         }
2276         aurora_driver->owner = THIS_MODULE;
2277         aurora_driver->name = "ttyA";
2278         aurora_driver->major = AURORA_MAJOR;
2279         aurora_driver->type = TTY_DRIVER_TYPE_SERIAL;
2280         aurora_driver->subtype = SERIAL_TYPE_NORMAL;
2281         aurora_driver->init_termios = tty_std_termios;
2282         aurora_driver->init_termios.c_cflag =
2283                 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
2284         aurora_driver->flags = TTY_DRIVER_REAL_RAW;
2285         tty_set_operations(aurora_driver, &aurora_ops);
2286         error = tty_register_driver(aurora_driver);
2287         if (error) {
2288                 put_tty_driver(aurora_driver);
2289                 free_page((unsigned long) tmp_buf);
2290                 printk(KERN_ERR "aurora: Couldn't register aurora driver, error = %d\n",
2291                        error);
2292                 return 1;
2293         }
2294         
2295         memset(aurora_port, 0, sizeof(aurora_port));
2296         for (i = 0; i < AURORA_TNPORTS; i++)  {
2297                 aurora_port[i].magic = AURORA_MAGIC;
2298                 aurora_port[i].tqueue.routine = do_softint;
2299                 aurora_port[i].tqueue.data = &aurora_port[i];
2300                 aurora_port[i].tqueue_hangup.routine = do_aurora_hangup;
2301                 aurora_port[i].tqueue_hangup.data = &aurora_port[i];
2302                 aurora_port[i].close_delay = 50 * HZ/100;
2303                 aurora_port[i].closing_wait = 3000 * HZ/100;
2304                 init_waitqueue_head(&aurora_port[i].open_wait);
2305                 init_waitqueue_head(&aurora_port[i].close_wait);
2306         }
2307 #ifdef AURORA_DEBUG
2308         printk("aurora_init_drivers: end\n");
2309 #endif
2310         return 0;
2311 }
2312
2313 static void aurora_release_drivers(void)
2314 {
2315 #ifdef AURORA_DEBUG
2316         printk("aurora_release_drivers: start\n");
2317 #endif
2318         free_page((unsigned long)tmp_buf);
2319         tty_unregister_driver(aurora_driver);
2320         put_tty_driver(aurora_driver);
2321 #ifdef AURORA_DEBUG
2322         printk("aurora_release_drivers: end\n");
2323 #endif
2324 }
2325
2326 /*
2327  * Called at boot time.
2328  *
2329  * You can specify IO base for up to RC_NBOARD cards,
2330  * using line "riscom8=0xiobase1,0xiobase2,.." at LILO prompt.
2331  * Note that there will be no probing at default
2332  * addresses in this case.
2333  *
2334  */
2335 void __init aurora_setup(char *str, int *ints)
2336 {
2337         int i;
2338
2339         for(i=0;(i<ints[0])&&(i<4);i++) {
2340                 if (ints[i+1]) irqs[i]=ints[i+1];
2341                 }
2342 }
2343
2344 static int __init aurora_real_init(void)
2345 {
2346         int found;
2347         int i;
2348
2349         printk(KERN_INFO "aurora: Driver starting.\n");
2350         if(aurora_init_drivers())
2351                 return -EIO;
2352         found = aurora_probe();
2353         if(!found) {
2354                 aurora_release_drivers();
2355                 printk(KERN_INFO "aurora: No Aurora Multiport boards detected.\n");
2356                 return -EIO;
2357         } else {
2358                 printk(KERN_INFO "aurora: %d boards found.\n", found);
2359         }
2360         for (i = 0; i < found; i++) {
2361                 int ret = aurora_setup_board(&aurora_board[i]);
2362
2363                 if (ret) {
2364 #ifdef AURORA_DEBUG
2365                         printk(KERN_ERR "aurora_init: error aurora_setup_board ret %d\n",
2366                                ret);
2367 #endif
2368                         return ret;
2369                 }
2370         }
2371         return 0;
2372 }
2373
2374 int irq  = 0;
2375 int irq1 = 0;
2376 int irq2 = 0;
2377 int irq3 = 0;
2378 MODULE_PARM(irq , "i");
2379 MODULE_PARM(irq1, "i");
2380 MODULE_PARM(irq2, "i");
2381 MODULE_PARM(irq3, "i");
2382
2383 static int __init aurora_init(void) 
2384 {
2385         if (irq ) irqs[0]=irq ;
2386         if (irq1) irqs[1]=irq1;
2387         if (irq2) irqs[2]=irq2;
2388         if (irq3) irqs[3]=irq3;
2389         return aurora_real_init();
2390 }
2391         
2392 static void __exit aurora_cleanup(void)
2393 {
2394         int i;
2395         
2396 #ifdef AURORA_DEBUG
2397 printk("cleanup_module: aurora_release_drivers\n");
2398 #endif
2399
2400         aurora_release_drivers();
2401         for (i = 0; i < AURORA_NBOARD; i++)
2402                 if (aurora_board[i].flags & AURORA_BOARD_PRESENT) {
2403                         aurora_shutdown_board(&aurora_board[i]);
2404                         aurora_release_io_range(&aurora_board[i]);
2405                 }
2406 }
2407
2408 module_init(aurora_init);
2409 module_exit(aurora_cleanup);
2410 MODULE_LICENSE("GPL");