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