Merge branch 'vendor/BINUTILS221'
[dragonfly.git] / sys / dev / serial / stl / stallion.c
1 /*****************************************************************************/
2
3 /*
4  * stallion.c  -- stallion multiport serial driver.
5  *
6  * Copyright (c) 1995-1996 Greg Ungerer (gerg@stallion.oz.au).
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Greg Ungerer.
20  * 4. Neither the name of the author nor the names of any co-contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $FreeBSD: src/sys/i386/isa/stallion.c,v 1.39.2.2 2001/08/30 12:29:57 murray Exp $
37  */
38
39 /*****************************************************************************/
40
41 #define TTYDEFCHARS     1
42
43 #include "use_pci.h"
44 #include "opt_compat.h"
45
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/tty.h>
51 #include <sys/proc.h>
52 #include <sys/priv.h>
53 #include <sys/conf.h>
54 #include <sys/fcntl.h>
55 #include <sys/thread2.h>
56 #include <bus/isa/isa_device.h>
57 #include <machine_base/isa/ic/scd1400.h>
58 #include <machine_base/isa/ic/sc26198.h>
59 #include <machine/comstats.h>
60
61 #if NPCI > 0
62 #include <bus/pci/pcivar.h>
63 #include <bus/pci/pcireg.h>
64 #endif
65
66 #undef STLDEBUG
67
68 /*****************************************************************************/
69
70 /*
71  *      Define the version level of the kernel - so we can compile in the
72  *      appropriate bits of code. By default this will compile for a 2.1
73  *      level kernel.
74  */
75 #define VFREEBSD        220
76
77 #if VFREEBSD >= 220
78 #define STATIC          static
79 #else
80 #define STATIC
81 #endif
82
83 /*****************************************************************************/
84
85 /*
86  *      Define different board types. At the moment I have only declared
87  *      those boards that this driver supports. But I will use the standard
88  *      "assigned" board numbers. In the future this driver will support
89  *      some of the other Stallion boards. Currently supported boards are
90  *      abbreviated as EIO = EasyIO and ECH = EasyConnection 8/32.
91  */
92 #define BRD_EASYIO      20
93 #define BRD_ECH         21
94 #define BRD_ECHMC       22
95 #define BRD_ECHPCI      26
96 #define BRD_ECH64PCI    27
97 #define BRD_EASYIOPCI   28
98
99 /*
100  *      When using the BSD "config" stuff there is no easy way to specifiy
101  *      a secondary IO address region. So it is hard wired here. Also the
102  *      shared interrupt information is hard wired here...
103  */
104 static unsigned int     stl_ioshared = 0x280;
105 static unsigned int     stl_irqshared = 0;
106
107 /*****************************************************************************/
108
109 /*
110  *      Define important driver limitations.
111  */
112 #define STL_MAXBRDS             8
113 #define STL_MAXPANELS           4
114 #define STL_MAXBANKS            8
115 #define STL_PORTSPERPANEL       16
116 #define STL_PORTSPERBRD         64
117
118 /*
119  *      Define the important minor number break down bits. These have been
120  *      chosen to be "compatible" with the standard sio driver minor numbers.
121  *      Extra high bits are used to distinguish between boards.
122  */
123 #define STL_CALLOUTDEV          0x80
124 #define STL_CTRLLOCK            0x40
125 #define STL_CTRLINIT            0x20
126 #define STL_CTRLDEV             (STL_CTRLLOCK | STL_CTRLINIT)
127
128 #define STL_MEMDEV      0x07000000
129
130 #define STL_DEFSPEED    TTYDEF_SPEED
131 #define STL_DEFCFLAG    (CS8 | CREAD | HUPCL)
132
133 /*
134  *      I haven't really decided (or measured) what buffer sizes give
135  *      a good balance between performance and memory usage. These seem
136  *      to work pretty well...
137  */
138 #define STL_RXBUFSIZE           2048
139 #define STL_TXBUFSIZE           2048
140
141 #define STL_TXBUFLOW            (STL_TXBUFSIZE / 4)
142 #define STL_RXBUFHIGH           (3 * STL_RXBUFSIZE / 4)
143
144 /*****************************************************************************/
145
146 /*
147  *      Define our local driver identity first. Set up stuff to deal with
148  *      all the local structures required by a serial tty driver.
149  */
150 static const char       stl_drvname[] = "stl";
151 static const char       stl_longdrvname[] = "Stallion Multiport Serial Driver";
152 static const char       stl_drvversion[] = "2.0.0";
153 static int              stl_brdprobed[STL_MAXBRDS];
154
155 static int              stl_nrbrds = 0;
156 static int              stl_doingtimeout = 0;
157 static struct callout   stl_poll_ch;
158
159 static const char       __file__[] = /*__FILE__*/ "stallion.c";
160
161 /*
162  *      Define global stats structures. Not used often, and can be
163  *      re-used for each stats call.
164  */
165 static combrd_t         stl_brdstats;
166 static comstats_t       stl_comstats;
167
168 /*****************************************************************************/
169
170 /*
171  *      Define a set of structures to hold all the board/panel/port info
172  *      for our ports. These will be dynamically allocated as required.
173  */
174
175 /*
176  *      Define a ring queue structure for each port. This will hold the
177  *      TX data waiting to be output. Characters are fed into this buffer
178  *      from the line discipline (or even direct from user space!) and
179  *      then fed into the UARTs during interrupts. Will use a clasic ring
180  *      queue here for this. The good thing about this type of ring queue
181  *      is that the head and tail pointers can be updated without interrupt
182  *      protection - since "write" code only needs to change the head, and
183  *      interrupt code only needs to change the tail.
184  */
185 typedef struct {
186         char    *buf;
187         char    *endbuf;
188         char    *head;
189         char    *tail;
190 } stlrq_t;
191
192 /*
193  *      Port, panel and board structures to hold status info about each.
194  *      The board structure contains pointers to structures for each panel
195  *      connected to it, and in turn each panel structure contains pointers
196  *      for each port structure for each port on that panel. Note that
197  *      the port structure also contains the board and panel number that it
198  *      is associated with, this makes it (fairly) easy to get back to the
199  *      board/panel info for a port. Also note that the tty struct is at
200  *      the top of the structure, this is important, since the code uses
201  *      this fact to get the port struct pointer from the tty struct
202  *      pointer!
203  */
204 typedef struct stlport {
205         struct tty      tty;
206         int             portnr;
207         int             panelnr;
208         int             brdnr;
209         int             ioaddr;
210         int             uartaddr;
211         int             pagenr;
212         int             callout;
213         int             brklen;
214         int             dtrwait;
215         int             dotimestamp;
216         int             waitopens;
217         int             hotchar;
218         void            *uartp;
219         unsigned int    state;
220         unsigned int    hwid;
221         unsigned int    sigs;
222         unsigned int    rxignoremsk;
223         unsigned int    rxmarkmsk;
224         unsigned int    crenable;
225         unsigned int    imr;
226         unsigned long   clk;
227         struct termios  initintios;
228         struct termios  initouttios;
229         struct termios  lockintios;
230         struct termios  lockouttios;
231         struct timeval  timestamp;
232         comstats_t      stats;
233         stlrq_t         tx;
234         stlrq_t         rx;
235         stlrq_t         rxstatus;
236         struct callout  dtr_ch;
237 } stlport_t;
238
239 typedef struct stlpanel {
240         int             panelnr;
241         int             brdnr;
242         int             pagenr;
243         int             nrports;
244         int             iobase;
245         unsigned int    hwid;
246         unsigned int    ackmask;
247         void            (*isr)(struct stlpanel *panelp, unsigned int iobase);
248         void            *uartp;
249         stlport_t       *ports[STL_PORTSPERPANEL];
250 } stlpanel_t;
251
252 typedef struct stlbrd {
253         int             brdnr;
254         int             brdtype;
255         int             unitid;
256         int             state;
257         int             nrpanels;
258         int             nrports;
259         int             nrbnks;
260         int             irq;
261         int             irqtype;
262         unsigned int    ioaddr1;
263         unsigned int    ioaddr2;
264         unsigned int    iostatus;
265         unsigned int    ioctrl;
266         unsigned int    ioctrlval;
267         unsigned int    hwid;
268         unsigned long   clk;
269         void            (*isr)(struct stlbrd *brdp);
270         unsigned int    bnkpageaddr[STL_MAXBANKS];
271         unsigned int    bnkstataddr[STL_MAXBANKS];
272         stlpanel_t      *bnk2panel[STL_MAXBANKS];
273         stlpanel_t      *panels[STL_MAXPANELS];
274         stlport_t       *ports[STL_PORTSPERBRD];
275 } stlbrd_t;
276
277 static stlbrd_t         *stl_brds[STL_MAXBRDS];
278
279 /*
280  *      Per board state flags. Used with the state field of the board struct.
281  *      Not really much here yet!
282  */
283 #define BRD_FOUND       0x1
284
285 /*
286  *      Define the port structure state flags. These set of flags are
287  *      modified at interrupt time - so setting and reseting them needs
288  *      to be atomic.
289  */
290 #define ASY_TXLOW       0x1
291 #define ASY_RXDATA      0x2
292 #define ASY_DCDCHANGE   0x4
293 #define ASY_DTRWAIT     0x8
294 #define ASY_RTSFLOW     0x10
295 #define ASY_RTSFLOWMODE 0x20
296 #define ASY_CTSFLOWMODE 0x40
297 #define ASY_TXFLOWED    0x80
298 #define ASY_TXBUSY      0x100
299 #define ASY_TXEMPTY     0x200
300
301 #define ASY_ACTIVE      (ASY_TXLOW | ASY_RXDATA | ASY_DCDCHANGE)
302
303 /*
304  *      Define an array of board names as printable strings. Handy for
305  *      referencing boards when printing trace and stuff.
306  */
307 static char     *stl_brdnames[] = {
308         NULL,
309         NULL,
310         NULL,
311         NULL,
312         NULL,
313         NULL,
314         NULL,
315         NULL,
316         NULL,
317         NULL,
318         NULL,
319         NULL,
320         NULL,
321         NULL,
322         NULL,
323         NULL,
324         NULL,
325         NULL,
326         NULL,
327         NULL,
328         "EasyIO",
329         "EC8/32-AT",
330         "EC8/32-MC",
331         NULL,
332         NULL,
333         NULL,
334         "EC8/32-PCI",
335         "EC8/64-PCI",
336         "EasyIO-PCI",
337 };
338
339 /*****************************************************************************/
340
341 /*
342  *      Hardware ID bits for the EasyIO and ECH boards. These defines apply
343  *      to the directly accessable io ports of these boards (not the cd1400
344  *      uarts - they are in scd1400.h).
345  */
346 #define EIO_8PORTRS     0x04
347 #define EIO_4PORTRS     0x05
348 #define EIO_8PORTDI     0x00
349 #define EIO_8PORTM      0x06
350 #define EIO_MK3         0x03
351 #define EIO_IDBITMASK   0x07
352
353 #define EIO_BRDMASK     0xf0
354 #define ID_BRD4         0x10
355 #define ID_BRD8         0x20
356 #define ID_BRD16        0x30
357
358 #define EIO_INTRPEND    0x08
359 #define EIO_INTEDGE     0x00
360 #define EIO_INTLEVEL    0x08
361
362 #define ECH_ID          0xa0
363 #define ECH_IDBITMASK   0xe0
364 #define ECH_BRDENABLE   0x08
365 #define ECH_BRDDISABLE  0x00
366 #define ECH_INTENABLE   0x01
367 #define ECH_INTDISABLE  0x00
368 #define ECH_INTLEVEL    0x02
369 #define ECH_INTEDGE     0x00
370 #define ECH_INTRPEND    0x01
371 #define ECH_BRDRESET    0x01
372
373 #define ECHMC_INTENABLE 0x01
374 #define ECHMC_BRDRESET  0x02
375
376 #define ECH_PNLSTATUS   2
377 #define ECH_PNL16PORT   0x20
378 #define ECH_PNLIDMASK   0x07
379 #define ECH_PNLXPID     0x40
380 #define ECH_PNLINTRPEND 0x80
381 #define ECH_ADDR2MASK   0x1e0
382
383 #define EIO_CLK         25000000
384 #define EIO_CLK8M       20000000
385 #define ECH_CLK         EIO_CLK
386
387 /*
388  *      Define the PCI vendor and device ID for Stallion PCI boards.
389  */
390 #define STL_PCINSVENDID 0x100b
391 #define STL_PCINSDEVID  0xd001
392
393 #define STL_PCIVENDID   0x124d
394 #define STL_PCI32DEVID  0x0000
395 #define STL_PCI64DEVID  0x0002
396 #define STL_PCIEIODEVID 0x0003
397
398 #define STL_PCIBADCLASS 0x0101
399
400 typedef struct stlpcibrd {
401         unsigned short          vendid;
402         unsigned short          devid;
403         int                     brdtype;
404 } stlpcibrd_t;
405
406 static  stlpcibrd_t     stl_pcibrds[] = {
407         { STL_PCIVENDID, STL_PCI64DEVID, BRD_ECH64PCI },
408         { STL_PCIVENDID, STL_PCIEIODEVID, BRD_EASYIOPCI },
409         { STL_PCIVENDID, STL_PCI32DEVID, BRD_ECHPCI },
410         { STL_PCINSVENDID, STL_PCINSDEVID, BRD_ECHPCI },
411 };
412
413 static int      stl_nrpcibrds = sizeof(stl_pcibrds) / sizeof(stlpcibrd_t);
414
415 /*****************************************************************************/
416
417 /*
418  *      Define the vector mapping bits for the programmable interrupt board
419  *      hardware. These bits encode the interrupt for the board to use - it
420  *      is software selectable (except the EIO-8M).
421  */
422 static unsigned char    stl_vecmap[] = {
423         0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
424         0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
425 };
426
427 /*
428  *      Set up enable and disable macros for the ECH boards. They require
429  *      the secondary io address space to be activated and deactivated.
430  *      This way all ECH boards can share their secondary io region.
431  *      If this is an ECH-PCI board then also need to set the page pointer
432  *      to point to the correct page.
433  */
434 #define BRDENABLE(brdnr,pagenr)                                         \
435         if (stl_brds[(brdnr)]->brdtype == BRD_ECH)                      \
436                 outb(stl_brds[(brdnr)]->ioctrl,                         \
437                         (stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE));\
438         else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI)              \
439                 outb(stl_brds[(brdnr)]->ioctrl, (pagenr));
440
441 #define BRDDISABLE(brdnr)                                               \
442         if (stl_brds[(brdnr)]->brdtype == BRD_ECH)                      \
443                 outb(stl_brds[(brdnr)]->ioctrl,                         \
444                         (stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE));
445
446 /*
447  *      Define some spare buffer space for un-wanted received characters.
448  */
449 static char     stl_unwanted[SC26198_RXFIFOSIZE];
450
451 /*****************************************************************************/
452
453 /*
454  *      Define macros to extract a brd and port number from a minor number.
455  *      This uses the extended minor number range in the upper 2 bytes of
456  *      the device number. This gives us plenty of minor numbers to play
457  *      with...
458  */
459 #define MKDEV2BRD(m)    ((minor(m) & 0x00700000) >> 20)
460 #define MKDEV2PORT(m)   ((minor(m) & 0x1f) | ((minor(m) & 0x00010000) >> 11))
461
462 /*
463  *      Define some handy local macros...
464  */
465 #ifndef MIN
466 #define MIN(a,b)        (((a) <= (b)) ? (a) : (b))
467 #endif
468
469 /*****************************************************************************/
470
471 /*
472  *      Declare all those functions in this driver!  First up is the set of
473  *      externally visible functions.
474  */
475
476 static int      stlprobe(struct isa_device *idp);
477 static int      stlattach(struct isa_device *idp);
478
479 STATIC  d_open_t        stlopen;
480 STATIC  d_close_t       stlclose;
481 STATIC  d_ioctl_t       stlioctl;
482
483 /*
484  *      Internal function prototypes.
485  */
486 static stlport_t *stl_dev2port(cdev_t dev);
487 static int      stl_findfreeunit(void);
488 static int      stl_rawopen(stlport_t *portp);
489 static int      stl_rawclose(stlport_t *portp);
490 static void     stl_flush(stlport_t *portp, int flag);
491 static int      stl_param(struct tty *tp, struct termios *tiosp);
492 static void     stl_start(struct tty *tp);
493 static void     stl_stop(struct tty *tp, int);
494 static void     stl_ttyoptim(stlport_t *portp, struct termios *tiosp);
495 static void     stl_dotimeout(void);
496 static void     stl_poll(void *arg);
497 static void     stl_rxprocess(stlport_t *portp);
498 static void     stl_flowcontrol(stlport_t *portp, int hw, int sw);
499 static void     stl_dtrwakeup(void *arg);
500 static int      stl_brdinit(stlbrd_t *brdp);
501 static int      stl_initeio(stlbrd_t *brdp);
502 static int      stl_initech(stlbrd_t *brdp);
503 static int      stl_initports(stlbrd_t *brdp, stlpanel_t *panelp);
504 static void     stl_eiointr(stlbrd_t *brdp);
505 static void     stl_echatintr(stlbrd_t *brdp);
506 static void     stl_echmcaintr(stlbrd_t *brdp);
507 static void     stl_echpciintr(stlbrd_t *brdp);
508 static void     stl_echpci64intr(stlbrd_t *brdp);
509 static int      stl_memioctl(cdev_t dev, unsigned long cmd, caddr_t data,
510                         int flag);
511 static int      stl_getbrdstats(caddr_t data);
512 static int      stl_getportstats(stlport_t *portp, caddr_t data);
513 static int      stl_clrportstats(stlport_t *portp, caddr_t data);
514 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr);
515 static void     stlintr(void *);
516
517 #if NPCI > 0
518 static const char *stlpciprobe(pcici_t tag, pcidi_t type);
519 static void     stlpciattach(pcici_t tag, int unit);
520 static void     stlpciintr(void * arg);
521 #endif
522
523 /*
524  *      CD1400 uart specific handling functions.
525  */
526 static void     stl_cd1400setreg(stlport_t *portp, int regnr, int value);
527 static int      stl_cd1400getreg(stlport_t *portp, int regnr);
528 static int      stl_cd1400updatereg(stlport_t *portp, int regnr, int value);
529 static int      stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
530 static void     stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
531 static int      stl_cd1400setport(stlport_t *portp, struct termios *tiosp);
532 static int      stl_cd1400getsignals(stlport_t *portp);
533 static void     stl_cd1400setsignals(stlport_t *portp, int dtr, int rts);
534 static void     stl_cd1400ccrwait(stlport_t *portp);
535 static void     stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx);
536 static void     stl_cd1400startrxtx(stlport_t *portp, int rx, int tx);
537 static void     stl_cd1400disableintrs(stlport_t *portp);
538 static void     stl_cd1400sendbreak(stlport_t *portp, long len);
539 static void     stl_cd1400sendflow(stlport_t *portp, int hw, int sw);
540 static int      stl_cd1400datastate(stlport_t *portp);
541 static void     stl_cd1400flush(stlport_t *portp, int flag);
542 static __inline void    stl_cd1400txisr(stlpanel_t *panelp, int ioaddr);
543 static void     stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr);
544 static void     stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr);
545 static void     stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase);
546 static void     stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase);
547
548 /*
549  *      SC26198 uart specific handling functions.
550  */
551 static void     stl_sc26198setreg(stlport_t *portp, int regnr, int value);
552 static int      stl_sc26198getreg(stlport_t *portp, int regnr);
553 static int      stl_sc26198updatereg(stlport_t *portp, int regnr, int value);
554 static int      stl_sc26198getglobreg(stlport_t *portp, int regnr);
555 static int      stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp);
556 static void     stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
557 static int      stl_sc26198setport(stlport_t *portp, struct termios *tiosp);
558 static int      stl_sc26198getsignals(stlport_t *portp);
559 static void     stl_sc26198setsignals(stlport_t *portp, int dtr, int rts);
560 static void     stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx);
561 static void     stl_sc26198startrxtx(stlport_t *portp, int rx, int tx);
562 static void     stl_sc26198disableintrs(stlport_t *portp);
563 static void     stl_sc26198sendbreak(stlport_t *portp, long len);
564 static void     stl_sc26198sendflow(stlport_t *portp, int hw, int sw);
565 static int      stl_sc26198datastate(stlport_t *portp);
566 static void     stl_sc26198flush(stlport_t *portp, int flag);
567 static void     stl_sc26198txunflow(stlport_t *portp);
568 static void     stl_sc26198wait(stlport_t *portp);
569 static void     stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase);
570 static void     stl_sc26198txisr(stlport_t *port);
571 static void     stl_sc26198rxisr(stlport_t *port, unsigned int iack);
572 static void     stl_sc26198rxgoodchars(stlport_t *portp);
573 static void     stl_sc26198rxbadchars(stlport_t *portp);
574 static void     stl_sc26198otherisr(stlport_t *port, unsigned int iack);
575
576 /*****************************************************************************/
577
578 /*
579  *      Generic UART support structure.
580  */
581 typedef struct uart {
582         int     (*panelinit)(stlbrd_t *brdp, stlpanel_t *panelp);
583         void    (*portinit)(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp);
584         int     (*setport)(stlport_t *portp, struct termios *tiosp);
585         int     (*getsignals)(stlport_t *portp);
586         void    (*setsignals)(stlport_t *portp, int dtr, int rts);
587         void    (*enablerxtx)(stlport_t *portp, int rx, int tx);
588         void    (*startrxtx)(stlport_t *portp, int rx, int tx);
589         void    (*disableintrs)(stlport_t *portp);
590         void    (*sendbreak)(stlport_t *portp, long len);
591         void    (*sendflow)(stlport_t *portp, int hw, int sw);
592         void    (*flush)(stlport_t *portp, int flag);
593         int     (*datastate)(stlport_t *portp);
594         void    (*intr)(stlpanel_t *panelp, unsigned int iobase);
595 } uart_t;
596
597 /*
598  *      Define some macros to make calling these functions nice and clean.
599  */
600 #define stl_panelinit           (* ((uart_t *) panelp->uartp)->panelinit)
601 #define stl_portinit            (* ((uart_t *) portp->uartp)->portinit)
602 #define stl_setport             (* ((uart_t *) portp->uartp)->setport)
603 #define stl_getsignals          (* ((uart_t *) portp->uartp)->getsignals)
604 #define stl_setsignals          (* ((uart_t *) portp->uartp)->setsignals)
605 #define stl_enablerxtx          (* ((uart_t *) portp->uartp)->enablerxtx)
606 #define stl_startrxtx           (* ((uart_t *) portp->uartp)->startrxtx)
607 #define stl_disableintrs        (* ((uart_t *) portp->uartp)->disableintrs)
608 #define stl_sendbreak           (* ((uart_t *) portp->uartp)->sendbreak)
609 #define stl_sendflow            (* ((uart_t *) portp->uartp)->sendflow)
610 #define stl_uartflush           (* ((uart_t *) portp->uartp)->flush)
611 #define stl_datastate           (* ((uart_t *) portp->uartp)->datastate)
612
613 /*****************************************************************************/
614
615 /*
616  *      CD1400 UART specific data initialization.
617  */
618 static uart_t stl_cd1400uart = {
619         stl_cd1400panelinit,
620         stl_cd1400portinit,
621         stl_cd1400setport,
622         stl_cd1400getsignals,
623         stl_cd1400setsignals,
624         stl_cd1400enablerxtx,
625         stl_cd1400startrxtx,
626         stl_cd1400disableintrs,
627         stl_cd1400sendbreak,
628         stl_cd1400sendflow,
629         stl_cd1400flush,
630         stl_cd1400datastate,
631         stl_cd1400eiointr
632 };
633
634 /*
635  *      Define the offsets within the register bank of a cd1400 based panel.
636  *      These io address offsets are common to the EasyIO board as well.
637  */
638 #define EREG_ADDR       0
639 #define EREG_DATA       4
640 #define EREG_RXACK      5
641 #define EREG_TXACK      6
642 #define EREG_MDACK      7
643
644 #define EREG_BANKSIZE   8
645
646 #define CD1400_CLK      25000000
647 #define CD1400_CLK8M    20000000
648
649 /*
650  *      Define the cd1400 baud rate clocks. These are used when calculating
651  *      what clock and divisor to use for the required baud rate. Also
652  *      define the maximum baud rate allowed, and the default base baud.
653  */
654 static int      stl_cd1400clkdivs[] = {
655         CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
656 };
657
658 /*
659  *      Define the maximum baud rate of the cd1400 devices.
660  */
661 #define CD1400_MAXBAUD  230400
662
663 /*****************************************************************************/
664
665 /*
666  *      SC26198 UART specific data initization.
667  */
668 static uart_t stl_sc26198uart = {
669         stl_sc26198panelinit,
670         stl_sc26198portinit,
671         stl_sc26198setport,
672         stl_sc26198getsignals,
673         stl_sc26198setsignals,
674         stl_sc26198enablerxtx,
675         stl_sc26198startrxtx,
676         stl_sc26198disableintrs,
677         stl_sc26198sendbreak,
678         stl_sc26198sendflow,
679         stl_sc26198flush,
680         stl_sc26198datastate,
681         stl_sc26198intr
682 };
683
684 /*
685  *      Define the offsets within the register bank of a sc26198 based panel.
686  */
687 #define XP_DATA         0
688 #define XP_ADDR         1
689 #define XP_MODID        2
690 #define XP_STATUS       2
691 #define XP_IACK         3
692
693 #define XP_BANKSIZE     4
694
695 /*
696  *      Define the sc26198 baud rate table. Offsets within the table
697  *      represent the actual baud rate selector of sc26198 registers.
698  */
699 static unsigned int     sc26198_baudtable[] = {
700         50, 75, 150, 200, 300, 450, 600, 900, 1200, 1800, 2400, 3600,
701         4800, 7200, 9600, 14400, 19200, 28800, 38400, 57600, 115200,
702         230400, 460800
703 };
704
705 #define SC26198_NRBAUDS (sizeof(sc26198_baudtable) / sizeof(unsigned int))
706
707 /*
708  *      Define the maximum baud rate of the sc26198 devices.
709  */
710 #define SC26198_MAXBAUD 460800
711
712 /*****************************************************************************/
713
714 /*
715  *      Declare the driver isa structure.
716  */
717 struct isa_driver       stldriver = {
718         stlprobe, stlattach, "stl"
719 };
720
721 /*****************************************************************************/
722
723 #if NPCI > 0
724
725 /*
726  *      Declare the driver pci structure.
727  */
728 static unsigned long    stl_count;
729
730 static struct pci_device        stlpcidriver = {
731         "stl",
732         stlpciprobe,
733         stlpciattach,
734         &stl_count,
735         NULL,
736 };
737
738 COMPAT_PCI_DRIVER (stlpci, stlpcidriver);
739
740 #endif
741
742 /*****************************************************************************/
743
744 #if VFREEBSD >= 220
745
746 /*
747  *      FreeBSD-2.2+ kernel linkage.
748  */
749
750 #define CDEV_MAJOR      72
751 static struct dev_ops stl_ops = {
752         { "stl", 0, D_TTY },
753         .d_open =       stlopen,
754         .d_close =      stlclose,
755         .d_read =       ttyread,
756         .d_write =      ttywrite,
757         .d_ioctl =      stlioctl,
758         .d_kqfilter =   ttykqfilter,
759         .d_revoke =     ttyrevoke
760 };
761
762 static void stl_drvinit(void *unused)
763 {
764 }
765
766 SYSINIT(sidev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,stl_drvinit,NULL)
767
768 #endif
769
770 /*****************************************************************************/
771
772 /*
773  *      Probe for some type of EasyIO or EasyConnection 8/32 board at
774  *      the supplied address. All we do is check if we can find the
775  *      board ID for the board... (Note, PCI boards not checked here,
776  *      they are done in the stlpciprobe() routine).
777  */
778
779 static int stlprobe(struct isa_device *idp)
780 {
781         unsigned int    status;
782
783 #if STLDEBUG
784         kprintf("stlprobe(idp=%x): unit=%d iobase=%x\n", (int) idp,
785                 idp->id_unit, idp->id_iobase);
786 #endif
787
788         if (idp->id_unit > STL_MAXBRDS)
789                 return(0);
790
791         status = inb(idp->id_iobase + 1);
792         if ((status & ECH_IDBITMASK) == ECH_ID) {
793                 stl_brdprobed[idp->id_unit] = BRD_ECH;
794                 return(1);
795         }
796
797         status = inb(idp->id_iobase + 2);
798         switch (status & EIO_IDBITMASK) {
799         case EIO_8PORTRS:
800         case EIO_8PORTM:
801         case EIO_8PORTDI:
802         case EIO_4PORTRS:
803         case EIO_MK3:
804                 stl_brdprobed[idp->id_unit] = BRD_EASYIO;
805                 return(1);
806         default:
807                 break;
808         }
809         
810         return(0);
811 }
812
813 /*****************************************************************************/
814
815 /*
816  *      Find an available internal board number (unit number). The problem
817  *      is that the same unit numbers can be assigned to different boards
818  *      detected during the ISA and PCI initialization phases.
819  */
820
821 static int stl_findfreeunit(void)
822 {
823         int     i;
824
825         for (i = 0; (i < STL_MAXBRDS); i++)
826                 if (stl_brds[i] == NULL)
827                         break;
828         return((i >= STL_MAXBRDS) ? -1 : i);
829 }
830
831 /*****************************************************************************/
832
833 /*
834  *      Allocate resources for and initialize the specified board.
835  */
836
837 static int stlattach(struct isa_device *idp)
838 {
839         stlbrd_t        *brdp;
840         int             boardnr, portnr, minor_dev;
841
842 #if STLDEBUG
843         kprintf("stlattach(idp=%p): unit=%d iobase=%x\n", (void *) idp,
844                 idp->id_unit, idp->id_iobase);
845 #endif
846
847 /*      idp->id_intr = (inthand2_t *)stlintr; */
848
849         brdp = kmalloc(sizeof(stlbrd_t), M_TTYS, M_WAITOK | M_ZERO);
850
851         if ((brdp->brdnr = stl_findfreeunit()) < 0) {
852                 kprintf("STALLION: too many boards found, max=%d\n",
853                         STL_MAXBRDS);
854                 return(0);
855         }
856         if (brdp->brdnr >= stl_nrbrds)
857                 stl_nrbrds = brdp->brdnr + 1;
858
859         brdp->unitid = idp->id_unit;
860         brdp->brdtype = stl_brdprobed[idp->id_unit];
861         brdp->ioaddr1 = idp->id_iobase;
862         brdp->ioaddr2 = stl_ioshared;
863         brdp->irq = ffs(idp->id_irq) - 1;
864         brdp->irqtype = stl_irqshared;
865         stl_brdinit(brdp);
866
867         /* register devices for DEVFS */
868         boardnr = brdp->brdnr;
869         make_dev(&stl_ops, boardnr + 0x1000000, UID_ROOT, GID_WHEEL,
870                  0600, "staliomem%d", boardnr);
871
872         for (portnr = 0, minor_dev = boardnr * 0x100000;
873               portnr < 32; portnr++, minor_dev++) {
874                 /* hw ports */
875                 make_dev(&stl_ops, minor_dev,
876                          UID_ROOT, GID_WHEEL, 0600,
877                          "ttyE%d", portnr + (boardnr * 64));
878                 make_dev(&stl_ops, minor_dev + 32,
879                          UID_ROOT, GID_WHEEL, 0600,
880                          "ttyiE%d", portnr + (boardnr * 64));
881                 make_dev(&stl_ops, minor_dev + 64,
882                          UID_ROOT, GID_WHEEL, 0600,
883                          "ttylE%d", portnr + (boardnr * 64));
884                 make_dev(&stl_ops, minor_dev + 128,
885                          UID_ROOT, GID_WHEEL, 0600,
886                          "cue%d", portnr + (boardnr * 64));
887                 make_dev(&stl_ops, minor_dev + 160,
888                          UID_ROOT, GID_WHEEL, 0600,
889                          "cuie%d", portnr + (boardnr * 64));
890                 make_dev(&stl_ops, minor_dev + 192,
891                          UID_ROOT, GID_WHEEL, 0600,
892                          "cule%d", portnr + (boardnr * 64));
893
894                 /* sw ports */
895                 make_dev(&stl_ops, minor_dev + 0x10000,
896                          UID_ROOT, GID_WHEEL, 0600,
897                          "ttyE%d", portnr + (boardnr * 64) + 32);
898                 make_dev(&stl_ops, minor_dev + 32 + 0x10000,
899                          UID_ROOT, GID_WHEEL, 0600,
900                          "ttyiE%d", portnr + (boardnr * 64) + 32);
901                 make_dev(&stl_ops, minor_dev + 64 + 0x10000,
902                          UID_ROOT, GID_WHEEL, 0600,
903                          "ttylE%d", portnr + (boardnr * 64) + 32);
904                 make_dev(&stl_ops, minor_dev + 128 + 0x10000,
905                          UID_ROOT, GID_WHEEL, 0600,
906                          "cue%d", portnr + (boardnr * 64) + 32);
907                 make_dev(&stl_ops, minor_dev + 160 + 0x10000,
908                          UID_ROOT, GID_WHEEL, 0600,
909                          "cuie%d", portnr + (boardnr * 64) + 32);
910                 make_dev(&stl_ops, minor_dev + 192 + 0x10000,
911                          UID_ROOT, GID_WHEEL, 0600,
912                          "cule%d", portnr + (boardnr * 64) + 32);
913         }
914         boardnr = brdp->brdnr;
915         make_dev(&stl_ops, boardnr + 0x1000000, UID_ROOT, GID_WHEEL,
916                  0600, "staliomem%d", boardnr);
917
918         for (portnr = 0, minor_dev = boardnr * 0x100000;
919               portnr < 32; portnr++, minor_dev++) {
920                 /* hw ports */
921                 make_dev(&stl_ops, minor_dev,
922                          UID_ROOT, GID_WHEEL, 0600,
923                          "ttyE%d", portnr + (boardnr * 64));
924                 make_dev(&stl_ops, minor_dev + 32,
925                          UID_ROOT, GID_WHEEL, 0600,
926                          "ttyiE%d", portnr + (boardnr * 64));
927                 make_dev(&stl_ops, minor_dev + 64,
928                          UID_ROOT, GID_WHEEL, 0600,
929                          "ttylE%d", portnr + (boardnr * 64));
930                 make_dev(&stl_ops, minor_dev + 128,
931                          UID_ROOT, GID_WHEEL, 0600,
932                          "cue%d", portnr + (boardnr * 64));
933                 make_dev(&stl_ops, minor_dev + 160,
934                          UID_ROOT, GID_WHEEL, 0600,
935                          "cuie%d", portnr + (boardnr * 64));
936                 make_dev(&stl_ops, minor_dev + 192,
937                          UID_ROOT, GID_WHEEL, 0600,
938                          "cule%d", portnr + (boardnr * 64));
939
940                 /* sw ports */
941                 make_dev(&stl_ops, minor_dev + 0x10000,
942                          UID_ROOT, GID_WHEEL, 0600,
943                          "ttyE%d", portnr + (boardnr * 64) + 32);
944                 make_dev(&stl_ops, minor_dev + 32 + 0x10000,
945                          UID_ROOT, GID_WHEEL, 0600,
946                          "ttyiE%d", portnr + (boardnr * 64) + 32);
947                 make_dev(&stl_ops, minor_dev + 64 + 0x10000,
948                          UID_ROOT, GID_WHEEL, 0600,
949                          "ttylE%d", portnr + (boardnr * 64) + 32);
950                 make_dev(&stl_ops, minor_dev + 128 + 0x10000,
951                          UID_ROOT, GID_WHEEL, 0600,
952                          "cue%d", portnr + (boardnr * 64) + 32);
953                 make_dev(&stl_ops, minor_dev + 160 + 0x10000,
954                          UID_ROOT, GID_WHEEL, 0600,
955                          "cuie%d", portnr + (boardnr * 64) + 32);
956                 make_dev(&stl_ops, minor_dev + 192 + 0x10000,
957                          UID_ROOT, GID_WHEEL, 0600,
958                          "cule%d", portnr + (boardnr * 64) + 32);
959         }
960
961         return(1);
962 }
963
964 /*****************************************************************************/
965
966 #if NPCI > 0
967
968 /*
969  *      Probe specifically for the PCI boards. We need to be a little
970  *      carefull here, since it looks sort like a Nat Semi IDE chip...
971  */
972
973 static const char *stlpciprobe(pcici_t tag, pcidi_t type)
974 {
975         unsigned long   class;
976         int             i, brdtype;
977
978 #if STLDEBUG
979         kprintf("stlpciprobe(tag=%x,type=%x)\n", (int) &tag, (int) type);
980 #endif
981
982         brdtype = 0;
983         for (i = 0; (i < stl_nrpcibrds); i++) {
984                 if (((type & 0xffff) == stl_pcibrds[i].vendid) &&
985                     (((type >> 16) & 0xffff) == stl_pcibrds[i].devid)) {
986                         brdtype = stl_pcibrds[i].brdtype;
987                         break;
988                 }
989         }
990
991         if (brdtype == 0)
992                 return(NULL);
993
994         class = pci_conf_read(tag, PCI_CLASS_REG);
995         if ((class & PCI_CLASS_MASK) == PCI_CLASS_MASS_STORAGE)
996                 return(NULL);
997
998         return(stl_brdnames[brdtype]);
999 }
1000
1001 /*****************************************************************************/
1002
1003 /*
1004  *      Allocate resources for and initialize the specified PCI board.
1005  */
1006
1007 void stlpciattach(pcici_t tag, int unit)
1008 {
1009         stlbrd_t        *brdp;
1010         unsigned int    bar[4];
1011         unsigned int    id;
1012         int             i;
1013         int             boardnr, portnr, minor_dev;
1014
1015 #if STLDEBUG
1016         kprintf("stlpciattach(tag=%x,unit=%x)\n", (int) &tag, unit);
1017 #endif
1018
1019         brdp = kmalloc(sizeof(stlbrd_t), M_TTYS, M_WAITOK | M_ZERO);
1020
1021         if ((unit < 0) || (unit > STL_MAXBRDS)) {
1022                 kprintf("STALLION: bad PCI board unit number=%d\n", unit);
1023                 return;
1024         }
1025
1026 /*
1027  *      Allocate us a new driver unique unit number.
1028  */
1029         if ((brdp->brdnr = stl_findfreeunit()) < 0) {
1030                 kprintf("STALLION: too many boards found, max=%d\n",
1031                         STL_MAXBRDS);
1032                 return;
1033         }
1034         if (brdp->brdnr >= stl_nrbrds)
1035                 stl_nrbrds = brdp->brdnr + 1;
1036
1037 /*
1038  *      Determine what type of PCI board this is...
1039  */
1040         id = (unsigned int) pci_conf_read(tag, 0x0);
1041         for (i = 0; (i < stl_nrpcibrds); i++) {
1042                 if (((id & 0xffff) == stl_pcibrds[i].vendid) &&
1043                     (((id >> 16) & 0xffff) == stl_pcibrds[i].devid)) {
1044                         brdp->brdtype = stl_pcibrds[i].brdtype;
1045                         break;
1046                 }
1047         }
1048
1049         if (i >= stl_nrpcibrds) {
1050                 kprintf("STALLION: probed PCI board unknown type=%x\n", id);
1051                 return;
1052         }
1053
1054         for (i = 0; (i < 4); i++)
1055                 bar[i] = (unsigned int) pci_conf_read(tag, 0x10 + (i * 4)) &
1056                         0xfffc;
1057
1058         switch (brdp->brdtype) {
1059         case BRD_ECH64PCI:
1060                 brdp->ioaddr1 = bar[1];
1061                 brdp->ioaddr2 = bar[2];
1062                 break;
1063         case BRD_EASYIOPCI:
1064                 brdp->ioaddr1 = bar[2];
1065                 brdp->ioaddr2 = bar[1];
1066                 break;
1067         case BRD_ECHPCI:
1068                 brdp->ioaddr1 = bar[1];
1069                 brdp->ioaddr2 = bar[0];
1070                 break;
1071         default:
1072                 kprintf("STALLION: unknown PCI board type=%d\n", brdp->brdtype);
1073                 return;
1074                 break;
1075         }
1076
1077         brdp->unitid = brdp->brdnr; /* PCI units auto-assigned */
1078         brdp->irq = ((int) pci_conf_read(tag, 0x3c)) & 0xff;
1079         brdp->irqtype = 0;
1080         if (pci_map_int(tag, stlpciintr, NULL) == 0) {
1081                 kprintf("STALLION: failed to map interrupt irq=%d for unit=%d\n",
1082                         brdp->irq, brdp->brdnr);
1083                 return;
1084         }
1085
1086         stl_brdinit(brdp);
1087
1088         /* register devices for DEVFS */
1089         boardnr = brdp->brdnr;
1090         make_dev(&stl_ops, boardnr + 0x1000000, UID_ROOT, GID_WHEEL,
1091                  0600, "staliomem%d", boardnr);
1092
1093         for (portnr = 0, minor_dev = boardnr * 0x100000;
1094               portnr < 32; portnr++, minor_dev++) {
1095                 /* hw ports */
1096                 make_dev(&stl_ops, minor_dev,
1097                          UID_ROOT, GID_WHEEL, 0600,
1098                          "ttyE%d", portnr + (boardnr * 64));
1099                 make_dev(&stl_ops, minor_dev + 32,
1100                          UID_ROOT, GID_WHEEL, 0600,
1101                          "ttyiE%d", portnr + (boardnr * 64));
1102                 make_dev(&stl_ops, minor_dev + 64,
1103                          UID_ROOT, GID_WHEEL, 0600,
1104                          "ttylE%d", portnr + (boardnr * 64));
1105                 make_dev(&stl_ops, minor_dev + 128,
1106                          UID_ROOT, GID_WHEEL, 0600,
1107                          "cue%d", portnr + (boardnr * 64));
1108                 make_dev(&stl_ops, minor_dev + 160,
1109                          UID_ROOT, GID_WHEEL, 0600,
1110                          "cuie%d", portnr + (boardnr * 64));
1111                 make_dev(&stl_ops, minor_dev + 192,
1112                          UID_ROOT, GID_WHEEL, 0600,
1113                          "cule%d", portnr + (boardnr * 64));
1114
1115                 /* sw ports */
1116                 make_dev(&stl_ops, minor_dev + 0x10000,
1117                          UID_ROOT, GID_WHEEL, 0600,
1118                          "ttyE%d", portnr + (boardnr * 64) + 32);
1119                 make_dev(&stl_ops, minor_dev + 32 + 0x10000,
1120                          UID_ROOT, GID_WHEEL, 0600,
1121                          "ttyiE%d", portnr + (boardnr * 64) + 32);
1122                 make_dev(&stl_ops, minor_dev + 64 + 0x10000,
1123                          UID_ROOT, GID_WHEEL, 0600,
1124                          "ttylE%d", portnr + (boardnr * 64) + 32);
1125                 make_dev(&stl_ops, minor_dev + 128 + 0x10000,
1126                          UID_ROOT, GID_WHEEL, 0600,
1127                          "cue%d", portnr + (boardnr * 64) + 32);
1128                 make_dev(&stl_ops, minor_dev + 160 + 0x10000,
1129                          UID_ROOT, GID_WHEEL, 0600,
1130                          "cuie%d", portnr + (boardnr * 64) + 32);
1131                 make_dev(&stl_ops, minor_dev + 192 + 0x10000,
1132                          UID_ROOT, GID_WHEEL, 0600,
1133                          "cule%d", portnr + (boardnr * 64) + 32);
1134         }
1135 }
1136
1137 #endif
1138
1139 /*****************************************************************************/
1140
1141 STATIC int stlopen(struct dev_open_args *ap)
1142 {
1143         cdev_t dev = ap->a_head.a_dev;
1144         struct tty      *tp;
1145         stlport_t       *portp;
1146         int             error, callout;
1147
1148 #if STLDEBUG
1149         kprintf("stlopen(dev=%x,flag=%x,mode=%x,p=%x)\n", (int) dev, flag,
1150                 mode, (int) p);
1151 #endif
1152
1153 /*
1154  *      Firstly check if the supplied device number is a valid device.
1155  */
1156         if (minor(dev) & STL_MEMDEV)
1157                 return(0);
1158
1159         portp = stl_dev2port(dev);
1160         if (portp == NULL)
1161                 return(ENXIO);
1162         if (minor(dev) & STL_CTRLDEV)
1163                 return(0);
1164         tp = &portp->tty;
1165         dev->si_tty = tp;
1166         callout = minor(dev) & STL_CALLOUTDEV;
1167         error = 0;
1168
1169         crit_enter();
1170
1171 stlopen_restart:
1172 /*
1173  *      Wait here for the DTR drop timeout period to expire.
1174  */
1175         while (portp->state & ASY_DTRWAIT) {
1176                 error = tsleep(&portp->dtrwait, PCATCH, "stldtr", 0);
1177                 if (error)
1178                         goto stlopen_end;
1179         }
1180         
1181 /*
1182  *      We have a valid device, so now we check if it is already open.
1183  *      If not then initialize the port hardware and set up the tty
1184  *      struct as required.
1185  */
1186         if ((tp->t_state & TS_ISOPEN) == 0) {
1187                 tp->t_oproc = stl_start;
1188                 tp->t_stop = stl_stop;
1189                 tp->t_param = stl_param;
1190                 tp->t_dev = dev;
1191                 tp->t_termios = callout ? portp->initouttios :
1192                         portp->initintios;
1193                 stl_rawopen(portp);
1194                 ttsetwater(tp);
1195                 if ((portp->sigs & TIOCM_CD) || callout)
1196                         (*linesw[tp->t_line].l_modem)(tp, 1);
1197         } else {
1198                 if (callout) {
1199                         if (portp->callout == 0) {
1200                                 error = EBUSY;
1201                                 goto stlopen_end;
1202                         }
1203                 } else {
1204                         if (portp->callout != 0) {
1205                                 if (ap->a_oflags & O_NONBLOCK) {
1206                                         error = EBUSY;
1207                                         goto stlopen_end;
1208                                 }
1209                                 error = tsleep(&portp->callout,
1210                                             PCATCH, "stlcall", 0);
1211                                 if (error)
1212                                         goto stlopen_end;
1213                                 goto stlopen_restart;
1214                         }
1215                 }
1216                 if ((tp->t_state & TS_XCLUDE) && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
1217                         error = EBUSY;
1218                         goto stlopen_end;
1219                 }
1220         }
1221
1222 /*
1223  *      If this port is not the callout device and we do not have carrier
1224  *      then we need to sleep, waiting for it to be asserted.
1225  */
1226         if (((tp->t_state & TS_CARR_ON) == 0) && !callout &&
1227                         ((tp->t_cflag & CLOCAL) == 0) &&
1228                         ((ap->a_oflags & O_NONBLOCK) == 0)) {
1229                 portp->waitopens++;
1230                 error = tsleep(TSA_CARR_ON(tp), PCATCH, "stldcd", 0);
1231                 portp->waitopens--;
1232                 if (error)
1233                         goto stlopen_end;
1234                 goto stlopen_restart;
1235         }
1236
1237 /*
1238  *      Open the line discipline.
1239  */
1240         error = (*linesw[tp->t_line].l_open)(dev, tp);
1241         stl_ttyoptim(portp, &tp->t_termios);
1242         if ((tp->t_state & TS_ISOPEN) && callout)
1243                 portp->callout = 1;
1244
1245 /*
1246  *      If for any reason we get to here and the port is not actually
1247  *      open then close of the physical hardware - no point leaving it
1248  *      active when the open failed...
1249  */
1250 stlopen_end:
1251         crit_exit();
1252         if (((tp->t_state & TS_ISOPEN) == 0) && (portp->waitopens == 0))
1253                 stl_rawclose(portp);
1254
1255         return(error);
1256 }
1257
1258 /*****************************************************************************/
1259
1260 STATIC int stlclose(struct dev_close_args *ap)
1261 {
1262         cdev_t dev = ap->a_head.a_dev;
1263         struct tty      *tp;
1264         stlport_t       *portp;
1265
1266 #if STLDEBUG
1267         kprintf("stlclose(dev=%s,flag=%x,mode=%x,p=%p)\n", devtoname(dev),
1268                 flag, mode, (void *) p);
1269 #endif
1270
1271         if (minor(dev) & STL_MEMDEV)
1272                 return(0);
1273         if (minor(dev) & STL_CTRLDEV)
1274                 return(0);
1275
1276         portp = stl_dev2port(dev);
1277         if (portp == NULL)
1278                 return(ENXIO);
1279         tp = &portp->tty;
1280
1281         crit_enter();
1282         (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
1283         stl_ttyoptim(portp, &tp->t_termios);
1284         stl_rawclose(portp);
1285         ttyclose(tp);
1286         crit_exit();
1287         return(0);
1288 }
1289
1290 /*****************************************************************************/
1291
1292 #if VFREEBSD >= 220
1293
1294 STATIC void stl_stop(struct tty *tp, int rw)
1295 {
1296 #if STLDEBUG
1297         kprintf("stl_stop(tp=%x,rw=%x)\n", (int) tp, rw);
1298 #endif
1299
1300         stl_flush((stlport_t *) tp, rw);
1301 }
1302
1303 #else
1304
1305 STATIC int stlstop(struct tty *tp, int rw)
1306 {
1307 #if STLDEBUG
1308         kprintf("stlstop(tp=%x,rw=%x)\n", (int) tp, rw);
1309 #endif
1310
1311         stl_flush((stlport_t *) tp, rw);
1312         return(0);
1313 }
1314
1315 #endif
1316
1317 /*****************************************************************************/
1318
1319 STATIC int stlioctl(struct dev_ioctl_args *ap)
1320 {
1321         cdev_t dev = ap->a_head.a_dev;
1322         u_long cmd = ap->a_cmd;
1323         caddr_t data = ap->a_data;
1324         struct termios  *newtios, *localtios;
1325         struct tty      *tp;
1326         stlport_t       *portp;
1327         int             error, i;
1328
1329 #if STLDEBUG
1330         kprintf("stlioctl(dev=%s,cmd=%lx,data=%p,flag=%x)\n",
1331                 devtoname(dev), cmd, (void *) data, ap->a_fflag);
1332 #endif
1333
1334         if (minor(dev) & STL_MEMDEV)
1335                 return(stl_memioctl(dev, cmd, data, ap->a_fflag));
1336
1337         portp = stl_dev2port(dev);
1338         if (portp == NULL)
1339                 return(ENODEV);
1340         tp = &portp->tty;
1341         error = 0;
1342         
1343 /*
1344  *      First up handle ioctls on the control devices.
1345  */
1346         if (minor(dev) & STL_CTRLDEV) {
1347                 if ((minor(dev) & STL_CTRLDEV) == STL_CTRLINIT)
1348                         localtios = (minor(dev) & STL_CALLOUTDEV) ?
1349                                 &portp->initouttios : &portp->initintios;
1350                 else if ((minor(dev) & STL_CTRLDEV) == STL_CTRLLOCK)
1351                         localtios = (minor(dev) & STL_CALLOUTDEV) ?
1352                                 &portp->lockouttios : &portp->lockintios;
1353                 else
1354                         return(ENODEV);
1355
1356                 switch (cmd) {
1357                 case TIOCSETA:
1358                         if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) == 0)
1359                                 *localtios = *((struct termios *) data);
1360                         break;
1361                 case TIOCGETA:
1362                         *((struct termios *) data) = *localtios;
1363                         break;
1364                 case TIOCGETD:
1365                         *((int *) data) = TTYDISC;
1366                         break;
1367                 case TIOCGWINSZ:
1368                         bzero(data, sizeof(struct winsize));
1369                         break;
1370                 default:
1371                         error = ENOTTY;
1372                         break;
1373                 }
1374                 return(error);
1375         }
1376
1377 /*
1378  *      Deal with 4.3 compatibility issues if we have too...
1379  */
1380 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1381         if (1) {
1382                 struct termios  tios;
1383                 unsigned long   oldcmd;
1384
1385                 tios = tp->t_termios;
1386                 oldcmd = cmd;
1387                 if ((error = ttsetcompat(tp, &cmd, data, &tios)))
1388                         return(error);
1389                 if (cmd != oldcmd)
1390                         data = (caddr_t) &tios;
1391         }
1392 #endif
1393
1394 /*
1395  *      Carry out some pre-cmd processing work first...
1396  *      Hmmm, not so sure we want this, disable for now...
1397  */
1398         if ((cmd == TIOCSETA) || (cmd == TIOCSETAW) || (cmd == TIOCSETAF)) {
1399                 newtios = (struct termios *) data;
1400                 localtios = (minor(dev) & STL_CALLOUTDEV) ? 
1401                         &portp->lockouttios : &portp->lockintios;
1402
1403                 newtios->c_iflag = (tp->t_iflag & localtios->c_iflag) |
1404                         (newtios->c_iflag & ~localtios->c_iflag);
1405                 newtios->c_oflag = (tp->t_oflag & localtios->c_oflag) |
1406                         (newtios->c_oflag & ~localtios->c_oflag);
1407                 newtios->c_cflag = (tp->t_cflag & localtios->c_cflag) |
1408                         (newtios->c_cflag & ~localtios->c_cflag);
1409                 newtios->c_lflag = (tp->t_lflag & localtios->c_lflag) |
1410                         (newtios->c_lflag & ~localtios->c_lflag);
1411                 for (i = 0; (i < NCCS); i++) {
1412                         if (localtios->c_cc[i] != 0)
1413                                 newtios->c_cc[i] = tp->t_cc[i];
1414                 }
1415                 if (localtios->c_ispeed != 0)
1416                         newtios->c_ispeed = tp->t_ispeed;
1417                 if (localtios->c_ospeed != 0)
1418                         newtios->c_ospeed = tp->t_ospeed;
1419         }
1420
1421 /*
1422  *      Call the line discipline and the common command processing to
1423  *      process this command (if they can).
1424  */
1425         error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data,
1426                                               ap->a_fflag, ap->a_cred);
1427         if (error != ENOIOCTL)
1428                 return(error);
1429
1430         crit_enter();
1431         error = ttioctl(tp, cmd, data, ap->a_fflag);
1432         stl_ttyoptim(portp, &tp->t_termios);
1433         if (error != ENOIOCTL) {
1434                 crit_exit();
1435                 return(error);
1436         }
1437
1438         error = 0;
1439
1440 /*
1441  *      Process local commands here. These are all commands that only we
1442  *      can take care of (they all rely on actually doing something special
1443  *      to the actual hardware).
1444  */
1445         switch (cmd) {
1446         case TIOCSBRK:
1447                 stl_sendbreak(portp, -1);
1448                 break;
1449         case TIOCCBRK:
1450                 stl_sendbreak(portp, -2);
1451                 break;
1452         case TIOCSDTR:
1453                 stl_setsignals(portp, 1, -1);
1454                 break;
1455         case TIOCCDTR:
1456                 stl_setsignals(portp, 0, -1);
1457                 break;
1458         case TIOCMSET:
1459                 i = *((int *) data);
1460                 stl_setsignals(portp, ((i & TIOCM_DTR) ? 1 : 0),
1461                         ((i & TIOCM_RTS) ? 1 : 0));
1462                 break;
1463         case TIOCMBIS:
1464                 i = *((int *) data);
1465                 stl_setsignals(portp, ((i & TIOCM_DTR) ? 1 : -1),
1466                         ((i & TIOCM_RTS) ? 1 : -1));
1467                 break;
1468         case TIOCMBIC:
1469                 i = *((int *) data);
1470                 stl_setsignals(portp, ((i & TIOCM_DTR) ? 0 : -1),
1471                         ((i & TIOCM_RTS) ? 0 : -1));
1472                 break;
1473         case TIOCMGET:
1474                 *((int *) data) = (stl_getsignals(portp) | TIOCM_LE);
1475                 break;
1476         case TIOCMSDTRWAIT:
1477                 if ((error = priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) == 0)
1478                         portp->dtrwait = *((int *) data) * hz / 100;
1479                 break;
1480         case TIOCMGDTRWAIT:
1481                 *((int *) data) = portp->dtrwait * 100 / hz;
1482                 break;
1483         case TIOCTIMESTAMP:
1484                 portp->dotimestamp = 1;
1485                 *((struct timeval *) data) = portp->timestamp;
1486                 break;
1487         default:
1488                 error = ENOTTY;
1489                 break;
1490         }
1491         crit_exit();
1492
1493         return(error);
1494 }
1495 /*****************************************************************************/
1496
1497 /*
1498  *      Convert the specified minor device number into a port struct
1499  *      pointer. Return NULL if the device number is not a valid port.
1500  */
1501
1502 STATIC stlport_t *stl_dev2port(cdev_t dev)
1503 {
1504         stlbrd_t        *brdp;
1505
1506         brdp = stl_brds[MKDEV2BRD(dev)];
1507         if (brdp == NULL)
1508                 return(NULL);
1509         return(brdp->ports[MKDEV2PORT(dev)]);
1510 }
1511
1512 /*****************************************************************************/
1513
1514 /*
1515  *      Initialize the port hardware. This involves enabling the transmitter
1516  *      and receiver, setting the port configuration, and setting the initial
1517  *      signal state.
1518  */
1519
1520 static int stl_rawopen(stlport_t *portp)
1521 {
1522 #if STLDEBUG
1523         kprintf("stl_rawopen(portp=%p): brdnr=%d panelnr=%d portnr=%d\n",
1524                 (void *) portp, portp->brdnr, portp->panelnr, portp->portnr);
1525 #endif
1526
1527         stl_setport(portp, &portp->tty.t_termios);
1528         portp->sigs = stl_getsignals(portp);
1529         stl_setsignals(portp, 1, 1);
1530         stl_enablerxtx(portp, 1, 1);
1531         stl_startrxtx(portp, 1, 0);
1532         return(0);
1533 }
1534
1535 /*****************************************************************************/
1536
1537 /*
1538  *      Shutdown the hardware of a port. Disable its transmitter and
1539  *      receiver, and maybe drop signals if appropriate.
1540  */
1541
1542 static int stl_rawclose(stlport_t *portp)
1543 {
1544         struct tty      *tp;
1545
1546 #if STLDEBUG
1547         kprintf("stl_rawclose(portp=%p): brdnr=%d panelnr=%d portnr=%d\n",
1548                 (void *) portp, portp->brdnr, portp->panelnr, portp->portnr);
1549 #endif
1550
1551         tp = &portp->tty;
1552         stl_disableintrs(portp);
1553         stl_enablerxtx(portp, 0, 0);
1554         stl_flush(portp, (FWRITE | FREAD));
1555         if (tp->t_cflag & HUPCL) {
1556                 stl_setsignals(portp, 0, 0);
1557                 if (portp->dtrwait != 0) {
1558                         portp->state |= ASY_DTRWAIT;
1559                         callout_reset(&portp->dtr_ch, portp->dtrwait,
1560                                         stl_dtrwakeup, portp);
1561                 }
1562         }
1563         portp->callout = 0;
1564         portp->brklen = 0;
1565         portp->state &= ~(ASY_ACTIVE | ASY_RTSFLOW);
1566         wakeup(&portp->callout);
1567         wakeup(TSA_CARR_ON(tp));
1568         return(0);
1569 }
1570
1571 /*****************************************************************************/
1572
1573 /*
1574  *      Clear the DTR waiting flag, and wake up any sleepers waiting for
1575  *      DTR wait period to finish.
1576  */
1577
1578 static void stl_dtrwakeup(void *arg)
1579 {
1580         stlport_t       *portp;
1581
1582         portp = (stlport_t *) arg;
1583         portp->state &= ~ASY_DTRWAIT;
1584         wakeup(&portp->dtrwait);
1585 }
1586
1587 /*****************************************************************************/
1588
1589 /*
1590  *      Start (or continue) the transfer of TX data on this port. If the
1591  *      port is not currently busy then load up the interrupt ring queue
1592  *      buffer and kick of the transmitter. If the port is running low on
1593  *      TX data then refill the ring queue. This routine is also used to
1594  *      activate input flow control!
1595  */
1596
1597 static void stl_start(struct tty *tp)
1598 {
1599         stlport_t       *portp;
1600         unsigned int    len, stlen;
1601         char            *head, *tail;
1602         int             count;
1603
1604         portp = (stlport_t *) tp;
1605
1606 #if STLDEBUG
1607         kprintf("stl_start(tp=%x): brdnr=%d portnr=%d\n", (int) tp, 
1608                 portp->brdnr, portp->portnr);
1609 #endif
1610
1611         crit_enter();
1612
1613 /*
1614  *      Check if the ports input has been blocked, and take appropriate action.
1615  *      Not very often do we really need to do anything, so make it quick.
1616  */
1617         if (tp->t_state & TS_TBLOCK) {
1618                 if ((portp->state & ASY_RTSFLOWMODE) &&
1619                     ((portp->state & ASY_RTSFLOW) == 0))
1620                         stl_flowcontrol(portp, 0, -1);
1621         } else {
1622                 if (portp->state & ASY_RTSFLOW)
1623                         stl_flowcontrol(portp, 1, -1);
1624         }
1625
1626 #if VFREEBSD == 205
1627 /*
1628  *      Check if the output cooked clist buffers are near empty, wake up
1629  *      the line discipline to fill it up.
1630  */
1631         if (tp->t_outq.c_cc <= tp->t_lowat) {
1632                 if (tp->t_state & TS_ASLEEP) {
1633                         tp->t_state &= ~TS_ASLEEP;
1634                         wakeup(&tp->t_outq);
1635                 }
1636                 KNOTE(&tp->t_wsel.si_note, 0);
1637         }
1638 #endif
1639
1640         if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
1641                 crit_exit();
1642                 return;
1643         }
1644
1645 /*
1646  *      Copy data from the clists into the interrupt ring queue. This will
1647  *      require at most 2 copys... What we do is calculate how many chars
1648  *      can fit into the ring queue, and how many can fit in 1 copy. If after
1649  *      the first copy there is still more room then do the second copy. 
1650  *      The beauty of this type of ring queue is that we do not need to
1651  *      spl protect our-selves, since we only ever update the head pointer,
1652  *      and the interrupt routine only ever updates the tail pointer.
1653  */
1654         if (tp->t_outq.c_cc != 0) {
1655                 head = portp->tx.head;
1656                 tail = portp->tx.tail;
1657                 if (head >= tail) {
1658                         len = STL_TXBUFSIZE - (head - tail) - 1;
1659                         stlen = portp->tx.endbuf - head;
1660                 } else {
1661                         len = tail - head - 1;
1662                         stlen = len;
1663                 }
1664
1665                 if (len > 0) {
1666                         stlen = MIN(len, stlen);
1667                         count = q_to_b(&tp->t_outq, head, stlen);
1668                         len -= count;
1669                         head += count;
1670                         if (head >= portp->tx.endbuf) {
1671                                 head = portp->tx.buf;
1672                                 if (len > 0) {
1673                                         stlen = q_to_b(&tp->t_outq, head, len);
1674                                         head += stlen;
1675                                         count += stlen;
1676                                 }
1677                         }
1678                         portp->tx.head = head;
1679                         if (count > 0)
1680                                 stl_startrxtx(portp, -1, 1);
1681                 }
1682
1683 /*
1684  *              If we sent something, make sure we are called again.
1685  */
1686                 tp->t_state |= TS_BUSY;
1687         }
1688
1689 #if VFREEBSD != 205
1690 /*
1691  *      Do any writer wakeups.
1692  */
1693         ttwwakeup(tp);
1694 #endif
1695
1696         crit_exit();
1697 }
1698
1699 /*****************************************************************************/
1700
1701 static void stl_flush(stlport_t *portp, int flag)
1702 {
1703         char    *head, *tail;
1704         int     len;
1705
1706 #if STLDEBUG
1707         kprintf("stl_flush(portp=%x,flag=%x)\n", (int) portp, flag);
1708 #endif
1709
1710         if (portp == NULL)
1711                 return;
1712
1713         crit_enter();
1714
1715         if (flag & FWRITE) {
1716                 stl_uartflush(portp, FWRITE);
1717                 portp->tx.tail = portp->tx.head;
1718         }
1719
1720 /*
1721  *      The only thing to watch out for when flushing the read side is
1722  *      the RX status buffer. The interrupt code relys on the status
1723  *      bytes as being zeroed all the time (it does not bother setting
1724  *      a good char status to 0, it expects that it already will be).
1725  *      We also need to un-flow the RX channel if flow control was
1726  *      active.
1727  */
1728         if (flag & FREAD) {
1729                 head = portp->rx.head;
1730                 tail = portp->rx.tail;
1731                 if (head != tail) {
1732                         if (head >= tail) {
1733                                 len = head - tail;
1734                         } else {
1735                                 len = portp->rx.endbuf - tail;
1736                                 bzero(portp->rxstatus.buf,
1737                                         (head - portp->rx.buf));
1738                         }
1739                         bzero((tail + STL_RXBUFSIZE), len);
1740                         portp->rx.tail = head;
1741                 }
1742
1743                 if ((portp->state & ASY_RTSFLOW) &&
1744                                 ((portp->tty.t_state & TS_TBLOCK) == 0))
1745                         stl_flowcontrol(portp, 1, -1);
1746         }
1747
1748         crit_exit();
1749 }
1750
1751 /*****************************************************************************/
1752
1753 /*
1754  *      Interrupt handler for host based boards. Interrupts for all boards
1755  *      are vectored through here.
1756  */
1757
1758 void stlintr(void *arg)
1759 {
1760         stlbrd_t        *brdp;
1761         int             i;
1762
1763 #if STLDEBUG
1764         kprintf("stlintr(unit=%d)\n", (int)arg);
1765 #endif
1766
1767         for (i = 0; (i < stl_nrbrds); i++) {
1768                 if ((brdp = stl_brds[i]) == NULL)
1769                         continue;
1770                 if (brdp->state == 0)
1771                         continue;
1772                 (* brdp->isr)(brdp);
1773         }
1774 }
1775
1776 /*****************************************************************************/
1777
1778 #if NPCI > 0
1779
1780 static void stlpciintr(void *arg)
1781 {
1782         stlintr(NULL);
1783 }
1784
1785 #endif
1786
1787 /*****************************************************************************/
1788
1789 /*
1790  *      Interrupt service routine for EasyIO boards.
1791  */
1792
1793 static void stl_eiointr(stlbrd_t *brdp)
1794 {
1795         stlpanel_t      *panelp;
1796         int             iobase;
1797
1798 #if STLDEBUG
1799         kprintf("stl_eiointr(brdp=%p)\n", brdp);
1800 #endif
1801
1802         panelp = (stlpanel_t *) brdp->panels[0];
1803         iobase = panelp->iobase;
1804         while (inb(brdp->iostatus) & EIO_INTRPEND)
1805                 (* panelp->isr)(panelp, iobase);
1806 }
1807
1808 /*
1809  *      Interrupt service routine for ECH-AT board types.
1810  */
1811
1812 static void stl_echatintr(stlbrd_t *brdp)
1813 {
1814         stlpanel_t      *panelp;
1815         unsigned int    ioaddr;
1816         int             bnknr;
1817
1818         outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDENABLE));
1819
1820         while (inb(brdp->iostatus) & ECH_INTRPEND) {
1821                 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
1822                         ioaddr = brdp->bnkstataddr[bnknr];
1823                         if (inb(ioaddr) & ECH_PNLINTRPEND) {
1824                                 panelp = brdp->bnk2panel[bnknr];
1825                                 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1826                         }
1827                 }
1828         }
1829
1830         outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDDISABLE));
1831 }
1832
1833 /*****************************************************************************/
1834
1835 /*
1836  *      Interrupt service routine for ECH-MCA board types.
1837  */
1838
1839 static void stl_echmcaintr(stlbrd_t *brdp)
1840 {
1841         stlpanel_t      *panelp;
1842         unsigned int    ioaddr;
1843         int             bnknr;
1844
1845         while (inb(brdp->iostatus) & ECH_INTRPEND) {
1846                 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
1847                         ioaddr = brdp->bnkstataddr[bnknr];
1848                         if (inb(ioaddr) & ECH_PNLINTRPEND) {
1849                                 panelp = brdp->bnk2panel[bnknr];
1850                                 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1851                         }
1852                 }
1853         }
1854 }
1855
1856 /*****************************************************************************/
1857
1858 /*
1859  *      Interrupt service routine for ECH-PCI board types.
1860  */
1861
1862 static void stl_echpciintr(stlbrd_t *brdp)
1863 {
1864         stlpanel_t      *panelp;
1865         unsigned int    ioaddr;
1866         int             bnknr, recheck;
1867
1868 #if STLDEBUG
1869         kprintf("stl_echpciintr(brdp=%x)\n", (int) brdp);
1870 #endif
1871
1872         for (;;) {
1873                 recheck = 0;
1874                 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
1875                         outb(brdp->ioctrl, brdp->bnkpageaddr[bnknr]);
1876                         ioaddr = brdp->bnkstataddr[bnknr];
1877                         if (inb(ioaddr) & ECH_PNLINTRPEND) {
1878                                 panelp = brdp->bnk2panel[bnknr];
1879                                 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1880                                 recheck++;
1881                         }
1882                 }
1883                 if (! recheck)
1884                         break;
1885         }
1886 }
1887
1888 /*****************************************************************************/
1889
1890 /*
1891  *      Interrupt service routine for EC8/64-PCI board types.
1892  */
1893
1894 static void stl_echpci64intr(stlbrd_t *brdp)
1895 {
1896         stlpanel_t      *panelp;
1897         unsigned int    ioaddr;
1898         int             bnknr;
1899
1900 #if STLDEBUG
1901         kprintf("stl_echpci64intr(brdp=%p)\n", brdp);
1902 #endif
1903
1904         while (inb(brdp->ioctrl) & 0x1) {
1905                 for (bnknr = 0; (bnknr < brdp->nrbnks); bnknr++) {
1906                         ioaddr = brdp->bnkstataddr[bnknr];
1907 #if STLDEBUG
1908         kprintf("    --> ioaddr=%x status=%x(%x)\n", ioaddr, inb(ioaddr) & ECH_PNLINTRPEND, inb(ioaddr));
1909 #endif
1910                         if (inb(ioaddr) & ECH_PNLINTRPEND) {
1911                                 panelp = brdp->bnk2panel[bnknr];
1912                                 (* panelp->isr)(panelp, (ioaddr & 0xfffc));
1913                         }
1914                 }
1915         }
1916 }
1917
1918 /*****************************************************************************/
1919
1920 /*
1921  *      If we haven't scheduled a timeout then do it, some port needs high
1922  *      level processing.
1923  */
1924
1925 static void stl_dotimeout(void)
1926 {
1927 #if STLDEBUG
1928         kprintf("stl_dotimeout()\n");
1929 #endif
1930         if (stl_doingtimeout == 0) {
1931                 if ((stl_poll_ch.c_flags & CALLOUT_DID_INIT) == 0)
1932                         callout_init(&stl_poll_ch);
1933                 callout_reset(&stl_poll_ch, 1, stl_poll, NULL);
1934                 stl_doingtimeout++;
1935         }
1936 }
1937
1938 /*****************************************************************************/
1939
1940 /*
1941  *      Service "software" level processing. Too slow or painfull to be done
1942  *      at real hardware interrupt time. This way we might also be able to
1943  *      do some service on other waiting ports as well...
1944  */
1945
1946 static void stl_poll(void *arg)
1947 {
1948         stlbrd_t        *brdp;
1949         stlport_t       *portp;
1950         struct tty      *tp;
1951         int             brdnr, portnr, rearm;
1952
1953 #if STLDEBUG
1954         kprintf("stl_poll()\n");
1955 #endif
1956
1957         stl_doingtimeout = 0;
1958         rearm = 0;
1959
1960         crit_enter();
1961         for (brdnr = 0; (brdnr < stl_nrbrds); brdnr++) {
1962                 if ((brdp = stl_brds[brdnr]) == NULL)
1963                         continue;
1964                 for (portnr = 0; (portnr < brdp->nrports); portnr++) {
1965                         if ((portp = brdp->ports[portnr]) == NULL)
1966                                 continue;
1967                         if ((portp->state & ASY_ACTIVE) == 0)
1968                                 continue;
1969                         tp = &portp->tty;
1970
1971                         if (portp->state & ASY_RXDATA)
1972                                 stl_rxprocess(portp);
1973                         if (portp->state & ASY_DCDCHANGE) {
1974                                 portp->state &= ~ASY_DCDCHANGE;
1975                                 portp->sigs = stl_getsignals(portp);
1976                                 (*linesw[tp->t_line].l_modem)(tp,
1977                                         (portp->sigs & TIOCM_CD));
1978                         }
1979                         if (portp->state & ASY_TXEMPTY) {
1980                                 if (stl_datastate(portp) == 0) {
1981                                         portp->state &= ~ASY_TXEMPTY;
1982                                         tp->t_state &= ~TS_BUSY;
1983                                         (*linesw[tp->t_line].l_start)(tp);
1984                                 }
1985                         }
1986                         if (portp->state & ASY_TXLOW) {
1987                                 portp->state &= ~ASY_TXLOW;
1988                                 (*linesw[tp->t_line].l_start)(tp);
1989                         }
1990
1991                         if (portp->state & ASY_ACTIVE)
1992                                 rearm++;
1993                 }
1994         }
1995         crit_exit();
1996
1997         if (rearm)
1998                 stl_dotimeout();
1999 }
2000
2001 /*****************************************************************************/
2002
2003 /*
2004  *      Process the RX data that has been buffered up in the RX ring queue.
2005  */
2006
2007 static void stl_rxprocess(stlport_t *portp)
2008 {
2009         struct tty      *tp;
2010         unsigned int    len, stlen, lostlen;
2011         char            *head, *tail;
2012         char            status;
2013         int             ch;
2014
2015 #if STLDEBUG
2016         kprintf("stl_rxprocess(portp=%x): brdnr=%d portnr=%d\n", (int) portp, 
2017                 portp->brdnr, portp->portnr);
2018 #endif
2019
2020         tp = &portp->tty;
2021         portp->state &= ~ASY_RXDATA;
2022
2023         if ((tp->t_state & TS_ISOPEN) == 0) {
2024                 stl_flush(portp, FREAD);
2025                 return;
2026         }
2027
2028 /*
2029  *      Calculate the amount of data in the RX ring queue. Also calculate
2030  *      the largest single copy size...
2031  */
2032         head = portp->rx.head;
2033         tail = portp->rx.tail;
2034         if (head >= tail) {
2035                 len = head - tail;
2036                 stlen = len;
2037         } else {
2038                 len = STL_RXBUFSIZE - (tail - head);
2039                 stlen = portp->rx.endbuf - tail;
2040         }
2041
2042         if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
2043                 if (len > 0) {
2044                         if (((tp->t_rawq.c_cc + len) >= TTYHOG) &&
2045                                         ((portp->state & ASY_RTSFLOWMODE) ||
2046                                         (tp->t_iflag & IXOFF)) &&
2047                                         ((tp->t_state & TS_TBLOCK) == 0)) {
2048                                 ch = TTYHOG - tp->t_rawq.c_cc - 1;
2049                                 len = (ch > 0) ? ch : 0;
2050                                 stlen = MIN(stlen, len);
2051                                 ttyblock(tp);
2052                         }
2053                         lostlen = b_to_q(tail, stlen, &tp->t_rawq);
2054                         tail += stlen;
2055                         len -= stlen;
2056                         if (tail >= portp->rx.endbuf) {
2057                                 tail = portp->rx.buf;
2058                                 lostlen += b_to_q(tail, len, &tp->t_rawq);
2059                                 tail += len;
2060                         }
2061                         portp->stats.rxlost += lostlen;
2062                         ttwakeup(tp);
2063                         portp->rx.tail = tail;
2064                 }
2065         } else {
2066                 while (portp->rx.tail != head) {
2067                         ch = (unsigned char) *(portp->rx.tail);
2068                         status = *(portp->rx.tail + STL_RXBUFSIZE);
2069                         if (status) {
2070                                 *(portp->rx.tail + STL_RXBUFSIZE) = 0;
2071                                 if (status & ST_BREAK)
2072                                         ch |= TTY_BI;
2073                                 if (status & ST_FRAMING)
2074                                         ch |= TTY_FE;
2075                                 if (status & ST_PARITY)
2076                                         ch |= TTY_PE;
2077                                 if (status & ST_OVERRUN)
2078                                         ch |= TTY_OE;
2079                         }
2080                         (*linesw[tp->t_line].l_rint)(ch, tp);
2081                         if (portp->rx.tail == head)
2082                                 break;
2083
2084                         if (++(portp->rx.tail) >= portp->rx.endbuf)
2085                                 portp->rx.tail = portp->rx.buf;
2086                 }
2087         }
2088
2089         if (head != portp->rx.tail)
2090                 portp->state |= ASY_RXDATA;
2091
2092 /*
2093  *      If we were flow controled then maybe the buffer is low enough that
2094  *      we can re-activate it.
2095  */
2096         if ((portp->state & ASY_RTSFLOW) && ((tp->t_state & TS_TBLOCK) == 0))
2097                 stl_flowcontrol(portp, 1, -1);
2098 }
2099
2100 /*****************************************************************************/
2101
2102 static int stl_param(struct tty *tp, struct termios *tiosp)
2103 {
2104         stlport_t       *portp;
2105
2106         portp = (stlport_t *) tp;
2107         if (portp == NULL)
2108                 return(ENODEV);
2109
2110         return(stl_setport(portp, tiosp));
2111 }
2112
2113 /*****************************************************************************/
2114
2115 /*
2116  *      Action the flow control as required. The hw and sw args inform the
2117  *      routine what flow control methods it should try.
2118  */
2119
2120 static void stl_flowcontrol(stlport_t *portp, int hw, int sw)
2121 {
2122         unsigned char   *head, *tail;
2123         int             len, hwflow;
2124
2125 #if STLDEBUG
2126         kprintf("stl_flowcontrol(portp=%x,hw=%d,sw=%d)\n", (int) portp, hw, sw);
2127 #endif
2128
2129         hwflow = -1;
2130
2131         if (portp->state & ASY_RTSFLOWMODE) {
2132                 if (hw == 0) {
2133                         if ((portp->state & ASY_RTSFLOW) == 0)
2134                                 hwflow = 0;
2135                 } else if (hw > 0) {
2136                         if (portp->state & ASY_RTSFLOW) {
2137                                 head = portp->rx.head;
2138                                 tail = portp->rx.tail;
2139                                 len = (head >= tail) ? (head - tail) :
2140                                         (STL_RXBUFSIZE - (tail - head));
2141                                 if (len < STL_RXBUFHIGH)
2142                                         hwflow = 1;
2143                         }
2144                 }
2145         }
2146
2147 /*
2148  *      We have worked out what to do, if anything. So now apply it to the
2149  *      UART port.
2150  */
2151         stl_sendflow(portp, hwflow, sw);
2152 }
2153
2154 /*****************************************************************************/
2155
2156 /*
2157  *      Enable l_rint processing bypass mode if tty modes allow it.
2158  */
2159
2160 static void stl_ttyoptim(stlport_t *portp, struct termios *tiosp)
2161 {
2162         struct tty      *tp;
2163
2164         tp = &portp->tty;
2165         if (((tiosp->c_iflag &
2166               (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP)) == 0) &&
2167             (((tiosp->c_iflag & BRKINT) == 0) || (tiosp->c_iflag & IGNBRK)) &&
2168             (((tiosp->c_iflag & PARMRK) == 0) ||
2169                 ((tiosp->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))) &&
2170             ((tiosp->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) ==0) &&
2171             (linesw[tp->t_line].l_rint == ttyinput))
2172                 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2173         else
2174                 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2175         portp->hotchar = linesw[tp->t_line].l_hotchar;
2176 }
2177
2178 /*****************************************************************************/
2179
2180 /*
2181  *      Try and find and initialize all the ports on a panel. We don't care
2182  *      what sort of board these ports are on - since the port io registers
2183  *      are almost identical when dealing with ports.
2184  */
2185
2186 static int stl_initports(stlbrd_t *brdp, stlpanel_t *panelp)
2187 {
2188         stlport_t       *portp;
2189         unsigned int    chipmask;
2190         int             i, j;
2191
2192 #if STLDEBUG
2193         kprintf("stl_initports(panelp=%x)\n", (int) panelp);
2194 #endif
2195
2196         chipmask = stl_panelinit(brdp, panelp);
2197
2198 /*
2199  *      All UART's are initialized if found. Now go through and setup
2200  *      each ports data structures. Also initialize each individual
2201  *      UART port.
2202  */
2203         for (i = 0; (i < panelp->nrports); i++) {
2204                 portp = kmalloc(sizeof(stlport_t), M_TTYS, M_WAITOK | M_ZERO);
2205
2206                 portp->portnr = i;
2207                 portp->brdnr = panelp->brdnr;
2208                 portp->panelnr = panelp->panelnr;
2209                 portp->uartp = panelp->uartp;
2210                 portp->clk = brdp->clk;
2211                 panelp->ports[i] = portp;
2212
2213                 j = STL_TXBUFSIZE + (2 * STL_RXBUFSIZE);
2214                 portp->tx.buf = kmalloc(j, M_TTYS, M_WAITOK);
2215                 portp->tx.endbuf = portp->tx.buf + STL_TXBUFSIZE;
2216                 portp->tx.head = portp->tx.buf;
2217                 portp->tx.tail = portp->tx.buf;
2218                 portp->rx.buf = portp->tx.buf + STL_TXBUFSIZE;
2219                 portp->rx.endbuf = portp->rx.buf + STL_RXBUFSIZE;
2220                 portp->rx.head = portp->rx.buf;
2221                 portp->rx.tail = portp->rx.buf;
2222                 portp->rxstatus.buf = portp->rx.buf + STL_RXBUFSIZE;
2223                 portp->rxstatus.endbuf = portp->rxstatus.buf + STL_RXBUFSIZE;
2224                 portp->rxstatus.head = portp->rxstatus.buf;
2225                 portp->rxstatus.tail = portp->rxstatus.buf;
2226                 bzero(portp->rxstatus.head, STL_RXBUFSIZE);
2227
2228                 portp->initintios.c_ispeed = STL_DEFSPEED;
2229                 portp->initintios.c_ospeed = STL_DEFSPEED;
2230                 portp->initintios.c_cflag = STL_DEFCFLAG;
2231                 portp->initintios.c_iflag = 0;
2232                 portp->initintios.c_oflag = 0;
2233                 portp->initintios.c_lflag = 0;
2234                 bcopy(&ttydefchars[0], &portp->initintios.c_cc[0],
2235                         sizeof(portp->initintios.c_cc));
2236                 portp->initouttios = portp->initintios;
2237                 portp->dtrwait = 3 * hz;
2238                 callout_init(&portp->dtr_ch);
2239
2240                 stl_portinit(brdp, panelp, portp);
2241         }
2242
2243         return(0);
2244 }
2245
2246 /*****************************************************************************/
2247
2248 /*
2249  *      Try to find and initialize an EasyIO board.
2250  */
2251
2252 static int stl_initeio(stlbrd_t *brdp)
2253 {
2254         stlpanel_t      *panelp;
2255         unsigned int    status;
2256
2257 #if STLDEBUG
2258         kprintf("stl_initeio(brdp=%x)\n", (int) brdp);
2259 #endif
2260
2261         brdp->ioctrl = brdp->ioaddr1 + 1;
2262         brdp->iostatus = brdp->ioaddr1 + 2;
2263         brdp->clk = EIO_CLK;
2264         brdp->isr = stl_eiointr;
2265
2266         status = inb(brdp->iostatus);
2267         switch (status & EIO_IDBITMASK) {
2268         case EIO_8PORTM:
2269                 brdp->clk = EIO_CLK8M;
2270                 /* fall thru */
2271         case EIO_8PORTRS:
2272         case EIO_8PORTDI:
2273                 brdp->nrports = 8;
2274                 break;
2275         case EIO_4PORTRS:
2276                 brdp->nrports = 4;
2277                 break;
2278         case EIO_MK3:
2279                 switch (status & EIO_BRDMASK) {
2280                 case ID_BRD4:
2281                         brdp->nrports = 4;
2282                         break;
2283                 case ID_BRD8:
2284                         brdp->nrports = 8;
2285                         break;
2286                 case ID_BRD16:
2287                         brdp->nrports = 16;
2288                         break;
2289                 default:
2290                         return(ENODEV);
2291                 }
2292                 brdp->ioctrl++;
2293                 break;
2294         default:
2295                 return(ENODEV);
2296         }
2297
2298         if (brdp->brdtype == BRD_EASYIOPCI) {
2299                 outb((brdp->ioaddr2 + 0x4c), 0x41);
2300         } else {
2301 /*
2302  *      Check that the supplied IRQ is good and then use it to setup the
2303  *      programmable interrupt bits on EIO board. Also set the edge/level
2304  *      triggered interrupt bit.
2305  */
2306                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2307                         (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2308                         kprintf("STALLION: invalid irq=%d for brd=%d\n",
2309                                brdp->irq, brdp->brdnr);
2310                         return(EINVAL);
2311                 }
2312                 outb(brdp->ioctrl, (stl_vecmap[brdp->irq] |
2313                         ((brdp->irqtype) ? EIO_INTLEVEL : EIO_INTEDGE)));
2314         }
2315
2316         panelp = kmalloc(sizeof(stlpanel_t), M_TTYS, M_WAITOK | M_ZERO);
2317         panelp->brdnr = brdp->brdnr;
2318         panelp->panelnr = 0;
2319         panelp->nrports = brdp->nrports;
2320         panelp->iobase = brdp->ioaddr1;
2321         panelp->hwid = status;
2322         if ((status & EIO_IDBITMASK) == EIO_MK3) {
2323                 panelp->uartp = (void *) &stl_sc26198uart;
2324                 panelp->isr = stl_sc26198intr;
2325         } else {
2326                 panelp->uartp = (void *) &stl_cd1400uart;
2327                 panelp->isr = stl_cd1400eiointr;
2328         }
2329         brdp->panels[0] = panelp;
2330         brdp->nrpanels = 1;
2331         brdp->hwid = status;
2332         brdp->state |= BRD_FOUND;
2333         return(0);
2334 }
2335
2336 /*****************************************************************************/
2337
2338 /*
2339  *      Try to find an ECH board and initialize it. This code is capable of
2340  *      dealing with all types of ECH board.
2341  */
2342
2343 static int stl_initech(stlbrd_t *brdp)
2344 {
2345         stlpanel_t      *panelp;
2346         unsigned int    status, nxtid;
2347         int             panelnr, ioaddr, banknr, i;
2348
2349 #if STLDEBUG
2350         kprintf("stl_initech(brdp=%x)\n", (int) brdp);
2351 #endif
2352
2353 /*
2354  *      Set up the initial board register contents for boards. This varys a
2355  *      bit between the different board types. So we need to handle each
2356  *      separately. Also do a check that the supplied IRQ is good.
2357  */
2358         switch (brdp->brdtype) {
2359
2360         case BRD_ECH:
2361                 brdp->isr = stl_echatintr;
2362                 brdp->ioctrl = brdp->ioaddr1 + 1;
2363                 brdp->iostatus = brdp->ioaddr1 + 1;
2364                 status = inb(brdp->iostatus);
2365                 if ((status & ECH_IDBITMASK) != ECH_ID)
2366                         return(ENODEV);
2367                 brdp->hwid = status;
2368
2369                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2370                     (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2371                         kprintf("STALLION: invalid irq=%d for brd=%d\n",
2372                                 brdp->irq, brdp->brdnr);
2373                         return(EINVAL);
2374                 }
2375                 status = ((brdp->ioaddr2 & ECH_ADDR2MASK) >> 1);
2376                 status |= (stl_vecmap[brdp->irq] << 1);
2377                 outb(brdp->ioaddr1, (status | ECH_BRDRESET));
2378                 brdp->ioctrlval = ECH_INTENABLE |
2379                         ((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
2380                 outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDENABLE));
2381                 outb(brdp->ioaddr1, status);
2382                 break;
2383
2384         case BRD_ECHMC:
2385                 brdp->isr = stl_echmcaintr;
2386                 brdp->ioctrl = brdp->ioaddr1 + 0x20;
2387                 brdp->iostatus = brdp->ioctrl;
2388                 status = inb(brdp->iostatus);
2389                 if ((status & ECH_IDBITMASK) != ECH_ID)
2390                         return(ENODEV);
2391                 brdp->hwid = status;
2392
2393                 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2394                     (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2395                         kprintf("STALLION: invalid irq=%d for brd=%d\n",
2396                                 brdp->irq, brdp->brdnr);
2397                         return(EINVAL);
2398                 }
2399                 outb(brdp->ioctrl, ECHMC_BRDRESET);
2400                 outb(brdp->ioctrl, ECHMC_INTENABLE);
2401                 break;
2402
2403         case BRD_ECHPCI:
2404                 brdp->isr = stl_echpciintr;
2405                 brdp->ioctrl = brdp->ioaddr1 + 2;
2406                 break;
2407
2408         case BRD_ECH64PCI:
2409                 brdp->isr = stl_echpci64intr;
2410                 brdp->ioctrl = brdp->ioaddr2 + 0x40;
2411                 outb((brdp->ioaddr1 + 0x4c), 0x43);
2412                 break;
2413
2414         default:
2415                 kprintf("STALLION: unknown board type=%d\n", brdp->brdtype);
2416                 break;
2417         }
2418
2419         brdp->clk = ECH_CLK;
2420
2421 /*
2422  *      Scan through the secondary io address space looking for panels.
2423  *      As we find'em allocate and initialize panel structures for each.
2424  */
2425         ioaddr = brdp->ioaddr2;
2426         panelnr = 0;
2427         nxtid = 0;
2428         banknr = 0;
2429
2430         for (i = 0; (i < STL_MAXPANELS); i++) {
2431                 if (brdp->brdtype == BRD_ECHPCI) {
2432                         outb(brdp->ioctrl, nxtid);
2433                         ioaddr = brdp->ioaddr2;
2434                 }
2435                 status = inb(ioaddr + ECH_PNLSTATUS);
2436                 if ((status & ECH_PNLIDMASK) != nxtid)
2437                         break;
2438                 panelp = kmalloc(sizeof(stlpanel_t), M_TTYS, M_WAITOK | M_ZERO);
2439                 panelp->brdnr = brdp->brdnr;
2440                 panelp->panelnr = panelnr;
2441                 panelp->iobase = ioaddr;
2442                 panelp->pagenr = nxtid;
2443                 panelp->hwid = status;
2444                 brdp->bnk2panel[banknr] = panelp;
2445                 brdp->bnkpageaddr[banknr] = nxtid;
2446                 brdp->bnkstataddr[banknr++] = ioaddr + ECH_PNLSTATUS;
2447
2448                 if (status & ECH_PNLXPID) {
2449                         panelp->uartp = (void *) &stl_sc26198uart;
2450                         panelp->isr = stl_sc26198intr;
2451                         if (status & ECH_PNL16PORT) {
2452                                 panelp->nrports = 16;
2453                                 brdp->bnk2panel[banknr] = panelp;
2454                                 brdp->bnkpageaddr[banknr] = nxtid;
2455                                 brdp->bnkstataddr[banknr++] = ioaddr + 4 +
2456                                         ECH_PNLSTATUS;
2457                         } else {
2458                                 panelp->nrports = 8;
2459                         }
2460                 } else {
2461                         panelp->uartp = (void *) &stl_cd1400uart;
2462                         panelp->isr = stl_cd1400echintr;
2463                         if (status & ECH_PNL16PORT) {
2464                                 panelp->nrports = 16;
2465                                 panelp->ackmask = 0x80;
2466                                 if (brdp->brdtype != BRD_ECHPCI)
2467                                         ioaddr += EREG_BANKSIZE;
2468                                 brdp->bnk2panel[banknr] = panelp;
2469                                 brdp->bnkpageaddr[banknr] = ++nxtid;
2470                                 brdp->bnkstataddr[banknr++] = ioaddr +
2471                                         ECH_PNLSTATUS;
2472                         } else {
2473                                 panelp->nrports = 8;
2474                                 panelp->ackmask = 0xc0;
2475                         }
2476                 }
2477
2478                 nxtid++;
2479                 ioaddr += EREG_BANKSIZE;
2480                 brdp->nrports += panelp->nrports;
2481                 brdp->panels[panelnr++] = panelp;
2482                 if ((brdp->brdtype == BRD_ECH) || (brdp->brdtype == BRD_ECHMC)){
2483                         if (ioaddr >= (brdp->ioaddr2 + 0x20)) {
2484                                 kprintf("STALLION: too many ports attached "
2485                                         "to board %d, remove last module\n",
2486                                         brdp->brdnr);
2487                                 break;
2488                         }
2489                 }
2490         }
2491
2492         brdp->nrpanels = panelnr;
2493         brdp->nrbnks = banknr;
2494         if (brdp->brdtype == BRD_ECH)
2495                 outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDDISABLE));
2496
2497         brdp->state |= BRD_FOUND;
2498         return(0);
2499 }
2500
2501 /*****************************************************************************/
2502
2503 /*
2504  *      Initialize and configure the specified board. This firstly probes
2505  *      for the board, if it is found then the board is initialized and
2506  *      then all its ports are initialized as well.
2507  */
2508
2509 static int stl_brdinit(stlbrd_t *brdp)
2510 {
2511         stlpanel_t      *panelp;
2512         int             i, j, k;
2513
2514 #if STLDEBUG
2515         kprintf("stl_brdinit(brdp=%x): unit=%d type=%d io1=%x io2=%x irq=%d\n",
2516                 (int) brdp, brdp->brdnr, brdp->brdtype, brdp->ioaddr1,
2517                 brdp->ioaddr2, brdp->irq);
2518 #endif
2519
2520         switch (brdp->brdtype) {
2521         case BRD_EASYIO:
2522         case BRD_EASYIOPCI:
2523                 stl_initeio(brdp);
2524                 break;
2525         case BRD_ECH:
2526         case BRD_ECHMC:
2527         case BRD_ECHPCI:
2528         case BRD_ECH64PCI:
2529                 stl_initech(brdp);
2530                 break;
2531         default:
2532                 kprintf("STALLION: unit=%d is unknown board type=%d\n",
2533                         brdp->brdnr, brdp->brdtype);
2534                 return(ENODEV);
2535         }
2536
2537         stl_brds[brdp->brdnr] = brdp;
2538         if ((brdp->state & BRD_FOUND) == 0) {
2539 #if 0
2540                 kprintf("STALLION: %s board not found, unit=%d io=%x irq=%d\n",
2541                         stl_brdnames[brdp->brdtype], brdp->brdnr,
2542                         brdp->ioaddr1, brdp->irq);
2543 #endif
2544                 return(ENODEV);
2545         }
2546
2547         for (i = 0, k = 0; (i < STL_MAXPANELS); i++) {
2548                 panelp = brdp->panels[i];
2549                 if (panelp != NULL) {
2550                         stl_initports(brdp, panelp);
2551                         for (j = 0; (j < panelp->nrports); j++)
2552                                 brdp->ports[k++] = panelp->ports[j];
2553                 }
2554         }
2555
2556         kprintf("stl%d: %s (driver version %s) unit=%d nrpanels=%d nrports=%d\n",
2557                 brdp->unitid, stl_brdnames[brdp->brdtype], stl_drvversion,
2558                 brdp->brdnr, brdp->nrpanels, brdp->nrports);
2559         return(0);
2560 }
2561
2562 /*****************************************************************************/
2563
2564 /*
2565  *      Return the board stats structure to user app.
2566  */
2567
2568 static int stl_getbrdstats(caddr_t data)
2569 {
2570         stlbrd_t        *brdp;
2571         stlpanel_t      *panelp;
2572         int             i;
2573
2574         stl_brdstats = *((combrd_t *) data);
2575         if (stl_brdstats.brd >= STL_MAXBRDS)
2576                 return(-ENODEV);
2577         brdp = stl_brds[stl_brdstats.brd];
2578         if (brdp == NULL)
2579                 return(-ENODEV);
2580
2581         bzero(&stl_brdstats, sizeof(combrd_t));
2582         stl_brdstats.brd = brdp->brdnr;
2583         stl_brdstats.type = brdp->brdtype;
2584         stl_brdstats.hwid = brdp->hwid;
2585         stl_brdstats.state = brdp->state;
2586         stl_brdstats.ioaddr = brdp->ioaddr1;
2587         stl_brdstats.ioaddr2 = brdp->ioaddr2;
2588         stl_brdstats.irq = brdp->irq;
2589         stl_brdstats.nrpanels = brdp->nrpanels;
2590         stl_brdstats.nrports = brdp->nrports;
2591         for (i = 0; (i < brdp->nrpanels); i++) {
2592                 panelp = brdp->panels[i];
2593                 stl_brdstats.panels[i].panel = i;
2594                 stl_brdstats.panels[i].hwid = panelp->hwid;
2595                 stl_brdstats.panels[i].nrports = panelp->nrports;
2596         }
2597
2598         *((combrd_t *) data) = stl_brdstats;
2599         return(0);
2600 }
2601
2602 /*****************************************************************************/
2603
2604 /*
2605  *      Resolve the referenced port number into a port struct pointer.
2606  */
2607
2608 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr)
2609 {
2610         stlbrd_t        *brdp;
2611         stlpanel_t      *panelp;
2612
2613         if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
2614                 return(NULL);
2615         brdp = stl_brds[brdnr];
2616         if (brdp == NULL)
2617                 return(NULL);
2618         if ((panelnr < 0) || (panelnr >= brdp->nrpanels))
2619                 return(NULL);
2620         panelp = brdp->panels[panelnr];
2621         if (panelp == NULL)
2622                 return(NULL);
2623         if ((portnr < 0) || (portnr >= panelp->nrports))
2624                 return(NULL);
2625         return(panelp->ports[portnr]);
2626 }
2627
2628 /*****************************************************************************/
2629
2630 /*
2631  *      Return the port stats structure to user app. A NULL port struct
2632  *      pointer passed in means that we need to find out from the app
2633  *      what port to get stats for (used through board control device).
2634  */
2635
2636 static int stl_getportstats(stlport_t *portp, caddr_t data)
2637 {
2638         unsigned char   *head, *tail;
2639
2640         if (portp == NULL) {
2641                 stl_comstats = *((comstats_t *) data);
2642                 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2643                         stl_comstats.port);
2644                 if (portp == NULL)
2645                         return(-ENODEV);
2646         }
2647
2648         portp->stats.state = portp->state;
2649         /*portp->stats.flags = portp->flags;*/
2650         portp->stats.hwid = portp->hwid;
2651         portp->stats.ttystate = portp->tty.t_state;
2652         portp->stats.cflags = portp->tty.t_cflag;
2653         portp->stats.iflags = portp->tty.t_iflag;
2654         portp->stats.oflags = portp->tty.t_oflag;
2655         portp->stats.lflags = portp->tty.t_lflag;
2656
2657         head = portp->tx.head;
2658         tail = portp->tx.tail;
2659         portp->stats.txbuffered = ((head >= tail) ? (head - tail) :
2660                 (STL_TXBUFSIZE - (tail - head)));
2661
2662         head = portp->rx.head;
2663         tail = portp->rx.tail;
2664         portp->stats.rxbuffered = (head >= tail) ? (head - tail) :
2665                 (STL_RXBUFSIZE - (tail - head));
2666
2667         portp->stats.signals = (unsigned long) stl_getsignals(portp);
2668
2669         *((comstats_t *) data) = portp->stats;
2670         return(0);
2671 }
2672
2673 /*****************************************************************************/
2674
2675 /*
2676  *      Clear the port stats structure. We also return it zeroed out...
2677  */
2678
2679 static int stl_clrportstats(stlport_t *portp, caddr_t data)
2680 {
2681         if (portp == NULL) {
2682                 stl_comstats = *((comstats_t *) data);
2683                 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2684                         stl_comstats.port);
2685                 if (portp == NULL)
2686                         return(ENODEV);
2687         }
2688
2689         bzero(&portp->stats, sizeof(comstats_t));
2690         portp->stats.brd = portp->brdnr;
2691         portp->stats.panel = portp->panelnr;
2692         portp->stats.port = portp->portnr;
2693         *((comstats_t *) data) = stl_comstats;
2694         return(0);
2695 }
2696
2697 /*****************************************************************************/
2698
2699 /*
2700  *      The "staliomem" device is used for stats collection in this driver.
2701  */
2702
2703 static int stl_memioctl(cdev_t dev, unsigned long cmd, caddr_t data, int flag)
2704 {
2705         int             rc;
2706
2707 #if STLDEBUG
2708         kprintf("stl_memioctl(dev=%s,cmd=%lx,data=%p,flag=%x)\n",
2709                 devtoname(dev), cmd, (void *) data, flag);
2710 #endif
2711
2712         rc = 0;
2713
2714         switch (cmd) {
2715         case COM_GETPORTSTATS:
2716                 rc = stl_getportstats(NULL, data);
2717                 break;
2718         case COM_CLRPORTSTATS:
2719                 rc = stl_clrportstats(NULL, data);
2720                 break;
2721         case COM_GETBRDSTATS:
2722                 rc = stl_getbrdstats(data);
2723                 break;
2724         default:
2725                 rc = ENOTTY;
2726                 break;
2727         }
2728
2729         return(rc);
2730 }
2731
2732 /*****************************************************************************/
2733
2734 /*****************************************************************************/
2735 /*                         CD1400 UART CODE                                  */
2736 /*****************************************************************************/
2737
2738 /*
2739  *      These functions get/set/update the registers of the cd1400 UARTs.
2740  *      Access to the cd1400 registers is via an address/data io port pair.
2741  */
2742
2743 static int stl_cd1400getreg(stlport_t *portp, int regnr)
2744 {
2745         outb(portp->ioaddr, (regnr + portp->uartaddr));
2746         return(inb(portp->ioaddr + EREG_DATA));
2747 }
2748
2749 /*****************************************************************************/
2750
2751 static void stl_cd1400setreg(stlport_t *portp, int regnr, int value)
2752 {
2753         outb(portp->ioaddr, (regnr + portp->uartaddr));
2754         outb((portp->ioaddr + EREG_DATA), value);
2755 }
2756
2757 /*****************************************************************************/
2758
2759 static int stl_cd1400updatereg(stlport_t *portp, int regnr, int value)
2760 {
2761         outb(portp->ioaddr, (regnr + portp->uartaddr));
2762         if (inb(portp->ioaddr + EREG_DATA) != value) {
2763                 outb((portp->ioaddr + EREG_DATA), value);
2764                 return(1);
2765         }
2766         return(0);
2767 }
2768
2769 /*****************************************************************************/
2770
2771 static void stl_cd1400flush(stlport_t *portp, int flag)
2772 {
2773
2774 #if STLDEBUG
2775         kprintf("stl_cd1400flush(portp=%x,flag=%x)\n", (int) portp, flag);
2776 #endif
2777
2778         if (portp == NULL)
2779                 return;
2780
2781         crit_enter();
2782
2783         if (flag & FWRITE) {
2784                 BRDENABLE(portp->brdnr, portp->pagenr);
2785                 stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
2786                 stl_cd1400ccrwait(portp);
2787                 stl_cd1400setreg(portp, CCR, CCR_TXFLUSHFIFO);
2788                 stl_cd1400ccrwait(portp);
2789                 BRDDISABLE(portp->brdnr);
2790         }
2791
2792         if (flag & FREAD) {
2793                 /* Hmmm */
2794         }
2795
2796         crit_exit();
2797 }
2798
2799 /*****************************************************************************/
2800
2801 static void stl_cd1400ccrwait(stlport_t *portp)
2802 {
2803         int     i;
2804
2805         for (i = 0; (i < CCR_MAXWAIT); i++) {
2806                 if (stl_cd1400getreg(portp, CCR) == 0)
2807                         return;
2808         }
2809
2810         kprintf("stl%d: cd1400 device not responding, panel=%d port=%d\n",
2811             portp->brdnr, portp->panelnr, portp->portnr);
2812 }
2813
2814 /*****************************************************************************/
2815
2816 /*
2817  *      Transmit interrupt handler. This has gotta be fast!  Handling TX
2818  *      chars is pretty simple, stuff as many as possible from the TX buffer
2819  *      into the cd1400 FIFO. Must also handle TX breaks here, since they
2820  *      are embedded as commands in the data stream. Oh no, had to use a goto!
2821  */
2822
2823 static __inline void stl_cd1400txisr(stlpanel_t *panelp, int ioaddr)
2824 {
2825         struct tty      *tp;
2826         stlport_t       *portp;
2827         unsigned char   ioack, srer;
2828         char            *head, *tail;
2829         int             len, stlen;
2830
2831 #if STLDEBUG
2832         kprintf("stl_cd1400txisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
2833 #endif
2834
2835         ioack = inb(ioaddr + EREG_TXACK);
2836         if (((ioack & panelp->ackmask) != 0) ||
2837             ((ioack & ACK_TYPMASK) != ACK_TYPTX)) {
2838                 kprintf("STALLION: bad TX interrupt ack value=%x\n",
2839                         ioack);
2840                 return;
2841         }
2842         portp = panelp->ports[(ioack >> 3)];
2843         tp = &portp->tty;
2844
2845 /*
2846  *      Unfortunately we need to handle breaks in the data stream, since
2847  *      this is the only way to generate them on the cd1400. Do it now if
2848  *      a break is to be sent. Some special cases here: brklen is -1 then
2849  *      start sending an un-timed break, if brklen is -2 then stop sending
2850  *      an un-timed break, if brklen is -3 then we have just sent an
2851  *      un-timed break and do not want any data to go out, if brklen is -4
2852  *      then a break has just completed so clean up the port settings.
2853  */
2854         if (portp->brklen != 0) {
2855                 if (portp->brklen >= -1) {
2856                         outb(ioaddr, (TDR + portp->uartaddr));
2857                         outb((ioaddr + EREG_DATA), ETC_CMD);
2858                         outb((ioaddr + EREG_DATA), ETC_STARTBREAK);
2859                         if (portp->brklen > 0) {
2860                                 outb((ioaddr + EREG_DATA), ETC_CMD);
2861                                 outb((ioaddr + EREG_DATA), ETC_DELAY);
2862                                 outb((ioaddr + EREG_DATA), portp->brklen);
2863                                 outb((ioaddr + EREG_DATA), ETC_CMD);
2864                                 outb((ioaddr + EREG_DATA), ETC_STOPBREAK);
2865                                 portp->brklen = -4;
2866                         } else {
2867                                 portp->brklen = -3;
2868                         }
2869                 } else if (portp->brklen == -2) {
2870                         outb(ioaddr, (TDR + portp->uartaddr));
2871                         outb((ioaddr + EREG_DATA), ETC_CMD);
2872                         outb((ioaddr + EREG_DATA), ETC_STOPBREAK);
2873                         portp->brklen = -4;
2874                 } else if (portp->brklen == -3) {
2875                         outb(ioaddr, (SRER + portp->uartaddr));
2876                         srer = inb(ioaddr + EREG_DATA);
2877                         srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
2878                         outb((ioaddr + EREG_DATA), srer);
2879                 } else {
2880                         outb(ioaddr, (COR2 + portp->uartaddr));
2881                         outb((ioaddr + EREG_DATA),
2882                                 (inb(ioaddr + EREG_DATA) & ~COR2_ETC));
2883                         portp->brklen = 0;
2884                 }
2885                 goto stl_txalldone;
2886         }
2887
2888         head = portp->tx.head;
2889         tail = portp->tx.tail;
2890         len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
2891         if ((len == 0) || ((len < STL_TXBUFLOW) &&
2892             ((portp->state & ASY_TXLOW) == 0))) {
2893                 portp->state |= ASY_TXLOW;
2894                 stl_dotimeout();
2895         }
2896
2897         if (len == 0) {
2898                 outb(ioaddr, (SRER + portp->uartaddr));
2899                 srer = inb(ioaddr + EREG_DATA);
2900                 if (srer & SRER_TXDATA) {
2901                         srer = (srer & ~SRER_TXDATA) | SRER_TXEMPTY;
2902                 } else {
2903                         srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
2904                         portp->state |= ASY_TXEMPTY;
2905                         portp->state &= ~ASY_TXBUSY;
2906                 }
2907                 outb((ioaddr + EREG_DATA), srer);
2908         } else {
2909                 len = MIN(len, CD1400_TXFIFOSIZE);
2910                 portp->stats.txtotal += len;
2911                 stlen = MIN(len, (portp->tx.endbuf - tail));
2912                 outb(ioaddr, (TDR + portp->uartaddr));
2913                 outsb((ioaddr + EREG_DATA), tail, stlen);
2914                 len -= stlen;
2915                 tail += stlen;
2916                 if (tail >= portp->tx.endbuf)
2917                         tail = portp->tx.buf;
2918                 if (len > 0) {
2919                         outsb((ioaddr + EREG_DATA), tail, len);
2920                         tail += len;
2921                 }
2922                 portp->tx.tail = tail;
2923         }
2924
2925 stl_txalldone:
2926         outb(ioaddr, (EOSRR + portp->uartaddr));
2927         outb((ioaddr + EREG_DATA), 0);
2928 }
2929
2930 /*****************************************************************************/
2931
2932 /*
2933  *      Receive character interrupt handler. Determine if we have good chars
2934  *      or bad chars and then process appropriately.
2935  */
2936
2937 static __inline void stl_cd1400rxisr(stlpanel_t *panelp, int ioaddr)
2938 {
2939         stlport_t       *portp;
2940         struct tty      *tp;
2941         unsigned int    ioack, len, buflen, stlen;
2942         unsigned char   status;
2943         char            ch;
2944         char            *head, *tail;
2945
2946 #if STLDEBUG
2947         kprintf("stl_cd1400rxisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
2948 #endif
2949
2950         ioack = inb(ioaddr + EREG_RXACK);
2951         if ((ioack & panelp->ackmask) != 0) {
2952                 kprintf("STALLION: bad RX interrupt ack value=%x\n", ioack);
2953                 return;
2954         }
2955         portp = panelp->ports[(ioack >> 3)];
2956         tp = &portp->tty;
2957
2958 /*
2959  *      First up, calculate how much room there is in the RX ring queue.
2960  *      We also want to keep track of the longest possible copy length,
2961  *      this has to allow for the wrapping of the ring queue.
2962  */
2963         head = portp->rx.head;
2964         tail = portp->rx.tail;
2965         if (head >= tail) {
2966                 buflen = STL_RXBUFSIZE - (head - tail) - 1;
2967                 stlen = portp->rx.endbuf - head;
2968         } else {
2969                 buflen = tail - head - 1;
2970                 stlen = buflen;
2971         }
2972
2973 /*
2974  *      Check if the input buffer is near full. If so then we should take
2975  *      some flow control action... It is very easy to do hardware and
2976  *      software flow control from here since we have the port selected on
2977  *      the UART.
2978  */
2979         if (buflen <= (STL_RXBUFSIZE - STL_RXBUFHIGH)) {
2980                 if (((portp->state & ASY_RTSFLOW) == 0) &&
2981                     (portp->state & ASY_RTSFLOWMODE)) {
2982                         portp->state |= ASY_RTSFLOW;
2983                         stl_cd1400setreg(portp, MCOR1,
2984                                 (stl_cd1400getreg(portp, MCOR1) & 0xf0));
2985                         stl_cd1400setreg(portp, MSVR2, 0);
2986                         portp->stats.rxrtsoff++;
2987                 }
2988         }
2989
2990 /*
2991  *      OK we are set, process good data... If the RX ring queue is full
2992  *      just chuck the chars - don't leave them in the UART.
2993  */
2994         if ((ioack & ACK_TYPMASK) == ACK_TYPRXGOOD) {
2995                 outb(ioaddr, (RDCR + portp->uartaddr));
2996                 len = inb(ioaddr + EREG_DATA);
2997                 if (buflen == 0) {
2998                         outb(ioaddr, (RDSR + portp->uartaddr));
2999                         insb((ioaddr + EREG_DATA), &stl_unwanted[0], len);
3000                         portp->stats.rxlost += len;
3001                         portp->stats.rxtotal += len;
3002                 } else {
3003                         len = MIN(len, buflen);
3004                         portp->stats.rxtotal += len;
3005                         stlen = MIN(len, stlen);
3006                         if (len > 0) {
3007                                 outb(ioaddr, (RDSR + portp->uartaddr));
3008                                 insb((ioaddr + EREG_DATA), head, stlen);
3009                                 head += stlen;
3010                                 if (head >= portp->rx.endbuf) {
3011                                         head = portp->rx.buf;
3012                                         len -= stlen;
3013                                         insb((ioaddr + EREG_DATA), head, len);
3014                                         head += len;
3015                                 }
3016                         }
3017                 }
3018         } else if ((ioack & ACK_TYPMASK) == ACK_TYPRXBAD) {
3019                 outb(ioaddr, (RDSR + portp->uartaddr));
3020                 status = inb(ioaddr + EREG_DATA);
3021                 ch = inb(ioaddr + EREG_DATA);
3022                 if (status & ST_BREAK)
3023                         portp->stats.rxbreaks++;
3024                 if (status & ST_FRAMING)
3025                         portp->stats.rxframing++;
3026                 if (status & ST_PARITY)
3027                         portp->stats.rxparity++;
3028                 if (status & ST_OVERRUN)
3029                         portp->stats.rxoverrun++;
3030                 if (status & ST_SCHARMASK) {
3031                         if ((status & ST_SCHARMASK) == ST_SCHAR1)
3032                                 portp->stats.txxon++;
3033                         if ((status & ST_SCHARMASK) == ST_SCHAR2)
3034                                 portp->stats.txxoff++;
3035                         goto stl_rxalldone;
3036                 }
3037                 if ((portp->rxignoremsk & status) == 0) {
3038                         if ((tp->t_state & TS_CAN_BYPASS_L_RINT) &&
3039                             ((status & ST_FRAMING) ||
3040                             ((status & ST_PARITY) && (tp->t_iflag & INPCK))))
3041                                 ch = 0;
3042                         if ((portp->rxmarkmsk & status) == 0)
3043                                 status = 0;
3044                         *(head + STL_RXBUFSIZE) = status;
3045                         *head++ = ch;
3046                         if (head >= portp->rx.endbuf)
3047                                 head = portp->rx.buf;
3048                 }
3049         } else {
3050                 kprintf("STALLION: bad RX interrupt ack value=%x\n", ioack);
3051                 return;
3052         }
3053
3054         portp->rx.head = head;
3055         portp->state |= ASY_RXDATA;
3056         stl_dotimeout();
3057
3058 stl_rxalldone:
3059         outb(ioaddr, (EOSRR + portp->uartaddr));
3060         outb((ioaddr + EREG_DATA), 0);
3061 }
3062
3063 /*****************************************************************************/
3064
3065 /*
3066  *      Modem interrupt handler. The is called when the modem signal line
3067  *      (DCD) has changed state.
3068  */
3069
3070 static __inline void stl_cd1400mdmisr(stlpanel_t *panelp, int ioaddr)
3071 {
3072         stlport_t       *portp;
3073         unsigned int    ioack;
3074         unsigned char   misr;
3075
3076 #if STLDEBUG
3077         kprintf("stl_cd1400mdmisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
3078 #endif
3079
3080         ioack = inb(ioaddr + EREG_MDACK);
3081         if (((ioack & panelp->ackmask) != 0) ||
3082             ((ioack & ACK_TYPMASK) != ACK_TYPMDM)) {
3083                 kprintf("STALLION: bad MODEM interrupt ack value=%x\n", ioack);
3084                 return;
3085         }
3086         portp = panelp->ports[(ioack >> 3)];
3087
3088         outb(ioaddr, (MISR + portp->uartaddr));
3089         misr = inb(ioaddr + EREG_DATA);
3090         if (misr & MISR_DCD) {
3091                 portp->state |= ASY_DCDCHANGE;
3092                 portp->stats.modem++;
3093                 stl_dotimeout();
3094         }
3095
3096         outb(ioaddr, (EOSRR + portp->uartaddr));
3097         outb((ioaddr + EREG_DATA), 0);
3098 }
3099
3100 /*****************************************************************************/
3101
3102 /*
3103  *      Interrupt service routine for cd1400 EasyIO boards.
3104  */
3105
3106 static void stl_cd1400eiointr(stlpanel_t *panelp, unsigned int iobase)
3107 {
3108         unsigned char   svrtype;
3109
3110 #if STLDEBUG
3111         kprintf("stl_cd1400eiointr(panelp=%x,iobase=%x)\n", (int) panelp,
3112                 iobase);
3113 #endif
3114
3115         outb(iobase, SVRR);
3116         svrtype = inb(iobase + EREG_DATA);
3117         if (panelp->nrports > 4) {
3118                 outb(iobase, (SVRR + 0x80));
3119                 svrtype |= inb(iobase + EREG_DATA);
3120         }
3121 #if STLDEBUG
3122 kprintf("stl_cd1400eiointr(panelp=%x,iobase=%x): svrr=%x\n", (int) panelp, iobase, svrtype);
3123 #endif
3124
3125         if (svrtype & SVRR_RX)
3126                 stl_cd1400rxisr(panelp, iobase);
3127         else if (svrtype & SVRR_TX)
3128                 stl_cd1400txisr(panelp, iobase);
3129         else if (svrtype & SVRR_MDM)
3130                 stl_cd1400mdmisr(panelp, iobase);
3131 }
3132
3133 /*****************************************************************************/
3134
3135 /*
3136  *      Interrupt service routine for cd1400 panels.
3137  */
3138
3139 static void stl_cd1400echintr(stlpanel_t *panelp, unsigned int iobase)
3140 {
3141         unsigned char   svrtype;
3142
3143 #if STLDEBUG
3144         kprintf("stl_cd1400echintr(panelp=%x,iobase=%x)\n", (int) panelp,
3145                 iobase);
3146 #endif
3147
3148         outb(iobase, SVRR);
3149         svrtype = inb(iobase + EREG_DATA);
3150         outb(iobase, (SVRR + 0x80));
3151         svrtype |= inb(iobase + EREG_DATA);
3152         if (svrtype & SVRR_RX)
3153                 stl_cd1400rxisr(panelp, iobase);
3154         else if (svrtype & SVRR_TX)
3155                 stl_cd1400txisr(panelp, iobase);
3156         else if (svrtype & SVRR_MDM)
3157                 stl_cd1400mdmisr(panelp, iobase);
3158 }
3159
3160 /*****************************************************************************/
3161
3162 /*
3163  *      Set up the cd1400 registers for a port based on the termios port
3164  *      settings.
3165  */
3166
3167 static int stl_cd1400setport(stlport_t *portp, struct termios *tiosp)
3168 {
3169         unsigned int    clkdiv;
3170         unsigned char   cor1, cor2, cor3;
3171         unsigned char   cor4, cor5, ccr;
3172         unsigned char   srer, sreron, sreroff;
3173         unsigned char   mcor1, mcor2, rtpr;
3174         unsigned char   clk, div;
3175
3176 #if STLDEBUG
3177         kprintf("stl_cd1400setport(portp=%x,tiosp=%x): brdnr=%d portnr=%d\n",
3178                 (int) portp, (int) tiosp, portp->brdnr, portp->portnr);
3179 #endif
3180
3181         cor1 = 0;
3182         cor2 = 0;
3183         cor3 = 0;
3184         cor4 = 0;
3185         cor5 = 0;
3186         ccr = 0;
3187         rtpr = 0;
3188         clk = 0;
3189         div = 0;
3190         mcor1 = 0;
3191         mcor2 = 0;
3192         sreron = 0;
3193         sreroff = 0;
3194
3195 /*
3196  *      Set up the RX char ignore mask with those RX error types we
3197  *      can ignore. We could have used some special modes of the cd1400
3198  *      UART to help, but it is better this way because we can keep stats
3199  *      on the number of each type of RX exception event.
3200  */
3201         portp->rxignoremsk = 0;
3202         if (tiosp->c_iflag & IGNPAR)
3203                 portp->rxignoremsk |= (ST_PARITY | ST_FRAMING | ST_OVERRUN);
3204         if (tiosp->c_iflag & IGNBRK)
3205                 portp->rxignoremsk |= ST_BREAK;
3206
3207         portp->rxmarkmsk = ST_OVERRUN;
3208         if (tiosp->c_iflag & (INPCK | PARMRK))
3209                 portp->rxmarkmsk |= (ST_PARITY | ST_FRAMING);
3210         if (tiosp->c_iflag & BRKINT)
3211                 portp->rxmarkmsk |= ST_BREAK;
3212
3213 /*
3214  *      Go through the char size, parity and stop bits and set all the
3215  *      option registers appropriately.
3216  */
3217         switch (tiosp->c_cflag & CSIZE) {
3218         case CS5:
3219                 cor1 |= COR1_CHL5;
3220                 break;
3221         case CS6:
3222                 cor1 |= COR1_CHL6;
3223                 break;
3224         case CS7:
3225                 cor1 |= COR1_CHL7;
3226                 break;
3227         default:
3228                 cor1 |= COR1_CHL8;
3229                 break;
3230         }
3231
3232         if (tiosp->c_cflag & CSTOPB)
3233                 cor1 |= COR1_STOP2;
3234         else
3235                 cor1 |= COR1_STOP1;
3236
3237         if (tiosp->c_cflag & PARENB) {
3238                 if (tiosp->c_cflag & PARODD)
3239                         cor1 |= (COR1_PARENB | COR1_PARODD);
3240                 else
3241                         cor1 |= (COR1_PARENB | COR1_PAREVEN);
3242         } else {
3243                 cor1 |= COR1_PARNONE;
3244         }
3245
3246 /*
3247  *      Set the RX FIFO threshold at 6 chars. This gives a bit of breathing
3248  *      space for hardware flow control and the like. This should be set to
3249  *      VMIN. Also here we will set the RX data timeout to 10ms - this should
3250  *      really be based on VTIME...
3251  */
3252         cor3 |= FIFO_RXTHRESHOLD;
3253         rtpr = 2;
3254
3255 /*
3256  *      Calculate the baud rate timers. For now we will just assume that
3257  *      the input and output baud are the same. Could have used a baud
3258  *      table here, but this way we can generate virtually any baud rate
3259  *      we like!
3260  */
3261         if (tiosp->c_ispeed == 0)
3262                 tiosp->c_ispeed = tiosp->c_ospeed;
3263         if ((tiosp->c_ospeed < 0) || (tiosp->c_ospeed > CD1400_MAXBAUD))
3264                 return(EINVAL);
3265
3266         if (tiosp->c_ospeed > 0) {
3267                 for (clk = 0; (clk < CD1400_NUMCLKS); clk++) {
3268                         clkdiv = ((portp->clk / stl_cd1400clkdivs[clk]) /
3269                                 tiosp->c_ospeed);
3270                         if (clkdiv < 0x100)
3271                                 break;
3272                 }
3273                 div = (unsigned char) clkdiv;
3274         }
3275
3276 /*
3277  *      Check what form of modem signaling is required and set it up.
3278  */
3279         if ((tiosp->c_cflag & CLOCAL) == 0) {
3280                 mcor1 |= MCOR1_DCD;
3281                 mcor2 |= MCOR2_DCD;
3282                 sreron |= SRER_MODEM;
3283         }
3284
3285 /*
3286  *      Setup cd1400 enhanced modes if we can. In particular we want to
3287  *      handle as much of the flow control as possbile automatically. As
3288  *      well as saving a few CPU cycles it will also greatly improve flow
3289  *      control reliablilty.
3290  */
3291         if (tiosp->c_iflag & IXON) {
3292                 cor2 |= COR2_TXIBE;
3293                 cor3 |= COR3_SCD12;
3294                 if (tiosp->c_iflag & IXANY)
3295                         cor2 |= COR2_IXM;
3296         }
3297
3298         if (tiosp->c_cflag & CCTS_OFLOW)
3299                 cor2 |= COR2_CTSAE;
3300         if (tiosp->c_cflag & CRTS_IFLOW)
3301                 mcor1 |= FIFO_RTSTHRESHOLD;
3302
3303 /*
3304  *      All cd1400 register values calculated so go through and set them
3305  *      all up.
3306  */
3307 #if STLDEBUG
3308         kprintf("SETPORT: portnr=%d panelnr=%d brdnr=%d\n", portp->portnr,
3309                 portp->panelnr, portp->brdnr);
3310         kprintf("    cor1=%x cor2=%x cor3=%x cor4=%x cor5=%x\n", cor1, cor2,
3311                 cor3, cor4, cor5);
3312         kprintf("    mcor1=%x mcor2=%x rtpr=%x sreron=%x sreroff=%x\n",
3313                 mcor1, mcor2, rtpr, sreron, sreroff);
3314         kprintf("    tcor=%x tbpr=%x rcor=%x rbpr=%x\n", clk, div, clk, div);
3315         kprintf("    schr1=%x schr2=%x schr3=%x schr4=%x\n",
3316                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP], tiosp->c_cc[VSTART],
3317                 tiosp->c_cc[VSTOP]);
3318 #endif
3319
3320         crit_enter();
3321         BRDENABLE(portp->brdnr, portp->pagenr);
3322         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3323         srer = stl_cd1400getreg(portp, SRER);
3324         stl_cd1400setreg(portp, SRER, 0);
3325         ccr += stl_cd1400updatereg(portp, COR1, cor1);
3326         ccr += stl_cd1400updatereg(portp, COR2, cor2);
3327         ccr += stl_cd1400updatereg(portp, COR3, cor3);
3328         if (ccr) {
3329                 stl_cd1400ccrwait(portp);
3330                 stl_cd1400setreg(portp, CCR, CCR_CORCHANGE);
3331         }
3332         stl_cd1400setreg(portp, COR4, cor4);
3333         stl_cd1400setreg(portp, COR5, cor5);
3334         stl_cd1400setreg(portp, MCOR1, mcor1);
3335         stl_cd1400setreg(portp, MCOR2, mcor2);
3336         if (tiosp->c_ospeed == 0) {
3337                 stl_cd1400setreg(portp, MSVR1, 0);
3338         } else {
3339                 stl_cd1400setreg(portp, MSVR1, MSVR1_DTR);
3340                 stl_cd1400setreg(portp, TCOR, clk);
3341                 stl_cd1400setreg(portp, TBPR, div);
3342                 stl_cd1400setreg(portp, RCOR, clk);
3343                 stl_cd1400setreg(portp, RBPR, div);
3344         }
3345         stl_cd1400setreg(portp, SCHR1, tiosp->c_cc[VSTART]);
3346         stl_cd1400setreg(portp, SCHR2, tiosp->c_cc[VSTOP]);
3347         stl_cd1400setreg(portp, SCHR3, tiosp->c_cc[VSTART]);
3348         stl_cd1400setreg(portp, SCHR4, tiosp->c_cc[VSTOP]);
3349         stl_cd1400setreg(portp, RTPR, rtpr);
3350         mcor1 = stl_cd1400getreg(portp, MSVR1);
3351         if (mcor1 & MSVR1_DCD)
3352                 portp->sigs |= TIOCM_CD;
3353         else
3354                 portp->sigs &= ~TIOCM_CD;
3355         stl_cd1400setreg(portp, SRER, ((srer & ~sreroff) | sreron));
3356         BRDDISABLE(portp->brdnr);
3357         portp->state &= ~(ASY_RTSFLOWMODE | ASY_CTSFLOWMODE);
3358         portp->state |= ((tiosp->c_cflag & CRTS_IFLOW) ? ASY_RTSFLOWMODE : 0);
3359         portp->state |= ((tiosp->c_cflag & CCTS_OFLOW) ? ASY_CTSFLOWMODE : 0);
3360         stl_ttyoptim(portp, tiosp);
3361         crit_exit();
3362
3363         return(0);
3364 }
3365
3366 /*****************************************************************************/
3367
3368 /*
3369  *      Action the flow control as required. The hw and sw args inform the
3370  *      routine what flow control methods it should try.
3371  */
3372
3373 static void stl_cd1400sendflow(stlport_t *portp, int hw, int sw)
3374 {
3375
3376 #if STLDEBUG
3377         kprintf("stl_cd1400sendflow(portp=%x,hw=%d,sw=%d)\n",
3378                 (int) portp, hw, sw);
3379 #endif
3380
3381         crit_enter();
3382         BRDENABLE(portp->brdnr, portp->pagenr);
3383         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3384
3385         if (sw >= 0) {
3386                 stl_cd1400ccrwait(portp);
3387                 if (sw) {
3388                         stl_cd1400setreg(portp, CCR, CCR_SENDSCHR2);
3389                         portp->stats.rxxoff++;
3390                 } else {
3391                         stl_cd1400setreg(portp, CCR, CCR_SENDSCHR1);
3392                         portp->stats.rxxon++;
3393                 }
3394                 stl_cd1400ccrwait(portp);
3395         }
3396
3397         if (hw == 0) {
3398                 portp->state |= ASY_RTSFLOW;
3399                 stl_cd1400setreg(portp, MCOR1,
3400                         (stl_cd1400getreg(portp, MCOR1) & 0xf0));
3401                 stl_cd1400setreg(portp, MSVR2, 0);
3402                 portp->stats.rxrtsoff++;
3403         } else if (hw > 0) {
3404                 portp->state &= ~ASY_RTSFLOW;
3405                 stl_cd1400setreg(portp, MSVR2, MSVR2_RTS);
3406                 stl_cd1400setreg(portp, MCOR1,
3407                         (stl_cd1400getreg(portp, MCOR1) | FIFO_RTSTHRESHOLD));
3408                 portp->stats.rxrtson++;
3409         }
3410
3411         BRDDISABLE(portp->brdnr);
3412         crit_exit();
3413 }
3414
3415 /*****************************************************************************/
3416
3417 /*
3418  *      Return the current state of data flow on this port. This is only
3419  *      really interresting when determining if data has fully completed
3420  *      transmission or not... This is easy for the cd1400, it accurately
3421  *      maintains the busy port flag.
3422  */
3423
3424 static int stl_cd1400datastate(stlport_t *portp)
3425 {
3426 #if STLDEBUG
3427         kprintf("stl_cd1400datastate(portp=%x)\n", (int) portp);
3428 #endif
3429
3430         if (portp == NULL)
3431                 return(0);
3432
3433         return((portp->state & ASY_TXBUSY) ? 1 : 0);
3434 }
3435
3436 /*****************************************************************************/
3437
3438 /*
3439  *      Set the state of the DTR and RTS signals. Got to do some extra
3440  *      work here to deal hardware flow control.
3441  */
3442
3443 static void stl_cd1400setsignals(stlport_t *portp, int dtr, int rts)
3444 {
3445         unsigned char   msvr1, msvr2;
3446
3447 #if STLDEBUG
3448         kprintf("stl_cd1400setsignals(portp=%x,dtr=%d,rts=%d)\n", (int) portp,
3449                 dtr, rts);
3450 #endif
3451
3452         msvr1 = 0;
3453         msvr2 = 0;
3454         if (dtr > 0)
3455                 msvr1 = MSVR1_DTR;
3456         if (rts > 0)
3457                 msvr2 = MSVR2_RTS;
3458
3459         crit_enter();
3460         BRDENABLE(portp->brdnr, portp->pagenr);
3461         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3462         if (rts >= 0) {
3463                 if (portp->tty.t_cflag & CRTS_IFLOW) {
3464                         if (rts == 0) {
3465                                 stl_cd1400setreg(portp, MCOR1,
3466                                       (stl_cd1400getreg(portp, MCOR1) & 0xf0));
3467                                 portp->stats.rxrtsoff++;
3468                         } else {
3469                                 stl_cd1400setreg(portp, MCOR1,
3470                                         (stl_cd1400getreg(portp, MCOR1) |
3471                                         FIFO_RTSTHRESHOLD));
3472                                 portp->stats.rxrtson++;
3473                         }
3474                 }
3475                 stl_cd1400setreg(portp, MSVR2, msvr2);
3476         }
3477         if (dtr >= 0)
3478                 stl_cd1400setreg(portp, MSVR1, msvr1);
3479         BRDDISABLE(portp->brdnr);
3480         crit_exit();
3481 }
3482
3483 /*****************************************************************************/
3484
3485 /*
3486  *      Get the state of the signals.
3487  */
3488
3489 static int stl_cd1400getsignals(stlport_t *portp)
3490 {
3491         unsigned char   msvr1, msvr2;
3492         int             sigs;
3493
3494 #if STLDEBUG
3495         kprintf("stl_cd1400getsignals(portp=%x)\n", (int) portp);
3496 #endif
3497
3498         crit_enter();
3499         BRDENABLE(portp->brdnr, portp->pagenr);
3500         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3501         msvr1 = stl_cd1400getreg(portp, MSVR1);
3502         msvr2 = stl_cd1400getreg(portp, MSVR2);
3503         BRDDISABLE(portp->brdnr);
3504         crit_exit();
3505
3506         sigs = 0;
3507         sigs |= (msvr1 & MSVR1_DCD) ? TIOCM_CD : 0;
3508         sigs |= (msvr1 & MSVR1_CTS) ? TIOCM_CTS : 0;
3509         sigs |= (msvr1 & MSVR1_DTR) ? TIOCM_DTR : 0;
3510         sigs |= (msvr2 & MSVR2_RTS) ? TIOCM_RTS : 0;
3511 #if 0
3512         sigs |= (msvr1 & MSVR1_RI) ? TIOCM_RI : 0;
3513         sigs |= (msvr1 & MSVR1_DSR) ? TIOCM_DSR : 0;
3514 #else
3515         sigs |= TIOCM_DSR;
3516 #endif
3517         return(sigs);
3518 }
3519
3520 /*****************************************************************************/
3521
3522 /*
3523  *      Enable or disable the Transmitter and/or Receiver.
3524  */
3525
3526 static void stl_cd1400enablerxtx(stlport_t *portp, int rx, int tx)
3527 {
3528         unsigned char   ccr;
3529
3530 #if STLDEBUG
3531         kprintf("stl_cd1400enablerxtx(portp=%x,rx=%d,tx=%d)\n",
3532                 (int) portp, rx, tx);
3533 #endif
3534
3535         ccr = 0;
3536         if (tx == 0)
3537                 ccr |= CCR_TXDISABLE;
3538         else if (tx > 0)
3539                 ccr |= CCR_TXENABLE;
3540         if (rx == 0)
3541                 ccr |= CCR_RXDISABLE;
3542         else if (rx > 0)
3543                 ccr |= CCR_RXENABLE;
3544
3545         crit_enter();
3546         BRDENABLE(portp->brdnr, portp->pagenr);
3547         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x03));
3548         stl_cd1400ccrwait(portp);
3549         stl_cd1400setreg(portp, CCR, ccr);
3550         stl_cd1400ccrwait(portp);
3551         BRDDISABLE(portp->brdnr);
3552         crit_exit();
3553 }
3554
3555 /*****************************************************************************/
3556
3557 /*
3558  *      Start or stop the Transmitter and/or Receiver.
3559  */
3560
3561 static void stl_cd1400startrxtx(stlport_t *portp, int rx, int tx)
3562 {
3563         unsigned char   sreron, sreroff;
3564
3565 #if STLDEBUG
3566         kprintf("stl_cd1400startrxtx(portp=%x,rx=%d,tx=%d)\n",
3567                 (int) portp, rx, tx);
3568 #endif
3569
3570         sreron = 0;
3571         sreroff = 0;
3572         if (tx == 0)
3573                 sreroff |= (SRER_TXDATA | SRER_TXEMPTY);
3574         else if (tx == 1)
3575                 sreron |= SRER_TXDATA;
3576         else if (tx >= 2)
3577                 sreron |= SRER_TXEMPTY;
3578         if (rx == 0)
3579                 sreroff |= SRER_RXDATA;
3580         else if (rx > 0)
3581                 sreron |= SRER_RXDATA;
3582
3583         crit_enter();
3584         BRDENABLE(portp->brdnr, portp->pagenr);
3585         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3586         stl_cd1400setreg(portp, SRER,
3587                 ((stl_cd1400getreg(portp, SRER) & ~sreroff) | sreron));
3588         BRDDISABLE(portp->brdnr);
3589         if (tx > 0) {
3590                 portp->state |= ASY_TXBUSY;
3591                 portp->tty.t_state |= TS_BUSY;
3592         }
3593         crit_exit();
3594 }
3595
3596 /*****************************************************************************/
3597
3598 /*
3599  *      Disable all interrupts from this port.
3600  */
3601
3602 static void stl_cd1400disableintrs(stlport_t *portp)
3603 {
3604
3605 #if STLDEBUG
3606         kprintf("stl_cd1400disableintrs(portp=%x)\n", (int) portp);
3607 #endif
3608
3609         crit_enter();
3610         BRDENABLE(portp->brdnr, portp->pagenr);
3611         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3612         stl_cd1400setreg(portp, SRER, 0);
3613         BRDDISABLE(portp->brdnr);
3614         crit_exit();
3615 }
3616
3617 /*****************************************************************************/
3618
3619 static void stl_cd1400sendbreak(stlport_t *portp, long len)
3620 {
3621
3622 #if STLDEBUG
3623         kprintf("stl_cd1400sendbreak(portp=%x,len=%d)\n", (int) portp,
3624                 (int) len);
3625 #endif
3626
3627         crit_enter();
3628         BRDENABLE(portp->brdnr, portp->pagenr);
3629         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3630         stl_cd1400setreg(portp, COR2,
3631                 (stl_cd1400getreg(portp, COR2) | COR2_ETC));
3632         stl_cd1400setreg(portp, SRER,
3633                 ((stl_cd1400getreg(portp, SRER) & ~SRER_TXDATA) |
3634                 SRER_TXEMPTY));
3635         BRDDISABLE(portp->brdnr);
3636         if (len > 0) {
3637                 len = len / 5;
3638                 portp->brklen = (len > 255) ? 255 : len;
3639         } else {
3640                 portp->brklen = len;
3641         }
3642         crit_exit();
3643         portp->stats.txbreaks++;
3644 }
3645
3646 /*****************************************************************************/
3647
3648 /*
3649  *      Try and find and initialize all the ports on a panel. We don't care
3650  *      what sort of board these ports are on - since the port io registers
3651  *      are almost identical when dealing with ports.
3652  */
3653
3654 static void stl_cd1400portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp)
3655 {
3656 #if STLDEBUG
3657         kprintf("stl_cd1400portinit(brdp=%x,panelp=%x,portp=%x)\n",
3658                 (int) brdp, (int) panelp, (int) portp);
3659 #endif
3660
3661         if ((brdp == NULL) || (panelp == NULL) ||
3662             (portp == NULL))
3663                 return;
3664
3665         portp->ioaddr = panelp->iobase + (((brdp->brdtype == BRD_ECHPCI) ||
3666                 (portp->portnr < 8)) ? 0 : EREG_BANKSIZE);
3667         portp->uartaddr = (portp->portnr & 0x04) << 5;
3668         portp->pagenr = panelp->pagenr + (portp->portnr >> 3);
3669
3670         BRDENABLE(portp->brdnr, portp->pagenr);
3671         stl_cd1400setreg(portp, CAR, (portp->portnr & 0x3));
3672         stl_cd1400setreg(portp, LIVR, (portp->portnr << 3));
3673         portp->hwid = stl_cd1400getreg(portp, GFRCR);
3674         BRDDISABLE(portp->brdnr);
3675 }
3676
3677 /*****************************************************************************/
3678
3679 /*
3680  *      Inbitialize the UARTs in a panel. We don't care what sort of board
3681  *      these ports are on - since the port io registers are almost
3682  *      identical when dealing with ports.
3683  */
3684
3685 static int stl_cd1400panelinit(stlbrd_t *brdp, stlpanel_t *panelp)
3686 {
3687         unsigned int    gfrcr;
3688         int             chipmask, i, j;
3689         int             nrchips, uartaddr, ioaddr;
3690
3691 #if STLDEBUG
3692         kprintf("stl_cd1400panelinit(brdp=%x,panelp=%x)\n", (int) brdp,
3693                 (int) panelp);
3694 #endif
3695
3696         BRDENABLE(panelp->brdnr, panelp->pagenr);
3697
3698 /*
3699  *      Check that each chip is present and started up OK.
3700  */
3701         chipmask = 0;
3702         nrchips = panelp->nrports / CD1400_PORTS;
3703         for (i = 0; (i < nrchips); i++) {
3704                 if (brdp->brdtype == BRD_ECHPCI) {
3705                         outb((panelp->pagenr + (i >> 1)), brdp->ioctrl);
3706                         ioaddr = panelp->iobase;
3707                 } else {
3708                         ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
3709                 }
3710                 uartaddr = (i & 0x01) ? 0x080 : 0;
3711                 outb(ioaddr, (GFRCR + uartaddr));
3712                 outb((ioaddr + EREG_DATA), 0);
3713                 outb(ioaddr, (CCR + uartaddr));
3714                 outb((ioaddr + EREG_DATA), CCR_RESETFULL);
3715                 outb((ioaddr + EREG_DATA), CCR_RESETFULL);
3716                 outb(ioaddr, (GFRCR + uartaddr));
3717                 for (j = 0; (j < CCR_MAXWAIT); j++) {
3718                         if ((gfrcr = inb(ioaddr + EREG_DATA)) != 0)
3719                                 break;
3720                 }
3721                 if ((j >= CCR_MAXWAIT) || (gfrcr < 0x40) || (gfrcr > 0x60)) {
3722                         kprintf("STALLION: cd1400 not responding, "
3723                                 "board=%d panel=%d chip=%d\n", panelp->brdnr,
3724                                 panelp->panelnr, i);
3725                         continue;
3726                 }
3727                 chipmask |= (0x1 << i);
3728                 outb(ioaddr, (PPR + uartaddr));
3729                 outb((ioaddr + EREG_DATA), PPR_SCALAR);
3730         }
3731
3732
3733         BRDDISABLE(panelp->brdnr);
3734         return(chipmask);
3735 }
3736
3737 /*****************************************************************************/
3738 /*                      SC26198 HARDWARE FUNCTIONS                           */
3739 /*****************************************************************************/
3740
3741 /*
3742  *      These functions get/set/update the registers of the sc26198 UARTs.
3743  *      Access to the sc26198 registers is via an address/data io port pair.
3744  *      (Maybe should make this inline...)
3745  */
3746
3747 static int stl_sc26198getreg(stlport_t *portp, int regnr)
3748 {
3749         outb((portp->ioaddr + XP_ADDR), (regnr | portp->uartaddr));
3750         return(inb(portp->ioaddr + XP_DATA));
3751 }
3752
3753 static void stl_sc26198setreg(stlport_t *portp, int regnr, int value)
3754 {
3755         outb((portp->ioaddr + XP_ADDR), (regnr | portp->uartaddr));
3756         outb((portp->ioaddr + XP_DATA), value);
3757 }
3758
3759 static int stl_sc26198updatereg(stlport_t *portp, int regnr, int value)
3760 {
3761         outb((portp->ioaddr + XP_ADDR), (regnr | portp->uartaddr));
3762         if (inb(portp->ioaddr + XP_DATA) != value) {
3763                 outb((portp->ioaddr + XP_DATA), value);
3764                 return(1);
3765         }
3766         return(0);
3767 }
3768
3769 /*****************************************************************************/
3770
3771 /*
3772  *      Functions to get and set the sc26198 global registers.
3773  */
3774
3775 static int stl_sc26198getglobreg(stlport_t *portp, int regnr)
3776 {
3777         outb((portp->ioaddr + XP_ADDR), regnr);
3778         return(inb(portp->ioaddr + XP_DATA));
3779 }
3780
3781 #if 0
3782 static void stl_sc26198setglobreg(stlport_t *portp, int regnr, int value)
3783 {
3784         outb((portp->ioaddr + XP_ADDR), regnr);
3785         outb((portp->ioaddr + XP_DATA), value);
3786 }
3787 #endif
3788
3789 /*****************************************************************************/
3790
3791 /*
3792  *      Inbitialize the UARTs in a panel. We don't care what sort of board
3793  *      these ports are on - since the port io registers are almost
3794  *      identical when dealing with ports.
3795  */
3796
3797 static int stl_sc26198panelinit(stlbrd_t *brdp, stlpanel_t *panelp)
3798 {
3799         int     chipmask, i;
3800         int     nrchips, ioaddr;
3801
3802 #if STLDEBUG
3803         kprintf("stl_sc26198panelinit(brdp=%x,panelp=%x)\n", (int) brdp,
3804                 (int) panelp);
3805 #endif
3806
3807         BRDENABLE(panelp->brdnr, panelp->pagenr);
3808
3809 /*
3810  *      Check that each chip is present and started up OK.
3811  */
3812         chipmask = 0;
3813         nrchips = (panelp->nrports + 4) / SC26198_PORTS;
3814         if (brdp->brdtype == BRD_ECHPCI)
3815                 outb(brdp->ioctrl, panelp->pagenr);
3816
3817         for (i = 0; (i < nrchips); i++) {
3818                 ioaddr = panelp->iobase + (i * 4); 
3819                 outb((ioaddr + XP_ADDR), SCCR);
3820                 outb((ioaddr + XP_DATA), CR_RESETALL);
3821                 outb((ioaddr + XP_ADDR), TSTR);
3822                 if (inb(ioaddr + XP_DATA) != 0) {
3823                         kprintf("STALLION: sc26198 not responding, "
3824                                 "board=%d panel=%d chip=%d\n", panelp->brdnr,
3825                                 panelp->panelnr, i);
3826                         continue;
3827                 }
3828                 chipmask |= (0x1 << i);
3829                 outb((ioaddr + XP_ADDR), GCCR);
3830                 outb((ioaddr + XP_DATA), GCCR_IVRTYPCHANACK);
3831                 outb((ioaddr + XP_ADDR), WDTRCR);
3832                 outb((ioaddr + XP_DATA), 0xff);
3833         }
3834
3835         BRDDISABLE(panelp->brdnr);
3836         return(chipmask);
3837 }
3838
3839 /*****************************************************************************/
3840
3841 /*
3842  *      Initialize hardware specific port registers.
3843  */
3844
3845 static void stl_sc26198portinit(stlbrd_t *brdp, stlpanel_t *panelp, stlport_t *portp)
3846 {
3847 #if STLDEBUG
3848         kprintf("stl_sc26198portinit(brdp=%x,panelp=%x,portp=%x)\n",
3849                 (int) brdp, (int) panelp, (int) portp);
3850 #endif
3851
3852         if ((brdp == NULL) || (panelp == NULL) ||
3853             (portp == NULL))
3854                 return;
3855
3856         portp->ioaddr = panelp->iobase + ((portp->portnr < 8) ? 0 : 4);
3857         portp->uartaddr = (portp->portnr & 0x07) << 4;
3858         portp->pagenr = panelp->pagenr;
3859         portp->hwid = 0x1;
3860
3861         BRDENABLE(portp->brdnr, portp->pagenr);
3862         stl_sc26198setreg(portp, IOPCR, IOPCR_SETSIGS);
3863         BRDDISABLE(portp->brdnr);
3864 }
3865
3866 /*****************************************************************************/
3867
3868 /*
3869  *      Set up the sc26198 registers for a port based on the termios port
3870  *      settings.
3871  */
3872
3873 static int stl_sc26198setport(stlport_t *portp, struct termios *tiosp)
3874 {
3875         unsigned char   mr0, mr1, mr2, clk;
3876         unsigned char   imron, imroff, iopr, ipr;
3877
3878 #if STLDEBUG
3879         kprintf("stl_sc26198setport(portp=%x,tiosp=%x): brdnr=%d portnr=%d\n",
3880                 (int) portp, (int) tiosp, portp->brdnr, portp->portnr);
3881 #endif
3882
3883         mr0 = 0;
3884         mr1 = 0;
3885         mr2 = 0;
3886         clk = 0;
3887         iopr = 0;
3888         imron = 0;
3889         imroff = 0;
3890
3891 /*
3892  *      Set up the RX char ignore mask with those RX error types we
3893  *      can ignore.
3894  */
3895         portp->rxignoremsk = 0;
3896         if (tiosp->c_iflag & IGNPAR)
3897                 portp->rxignoremsk |= (SR_RXPARITY | SR_RXFRAMING |
3898                         SR_RXOVERRUN);
3899         if (tiosp->c_iflag & IGNBRK)
3900                 portp->rxignoremsk |= SR_RXBREAK;
3901
3902         portp->rxmarkmsk = SR_RXOVERRUN;
3903         if (tiosp->c_iflag & (INPCK | PARMRK))
3904                 portp->rxmarkmsk |= (SR_RXPARITY | SR_RXFRAMING);
3905         if (tiosp->c_iflag & BRKINT)
3906                 portp->rxmarkmsk |= SR_RXBREAK;
3907
3908 /*
3909  *      Go through the char size, parity and stop bits and set all the
3910  *      option registers appropriately.
3911  */
3912         switch (tiosp->c_cflag & CSIZE) {
3913         case CS5:
3914                 mr1 |= MR1_CS5;
3915                 break;
3916         case CS6:
3917                 mr1 |= MR1_CS6;
3918                 break;
3919         case CS7:
3920                 mr1 |= MR1_CS7;
3921                 break;
3922         default:
3923                 mr1 |= MR1_CS8;
3924                 break;
3925         }
3926
3927         if (tiosp->c_cflag & CSTOPB)
3928                 mr2 |= MR2_STOP2;
3929         else
3930                 mr2 |= MR2_STOP1;
3931
3932         if (tiosp->c_cflag & PARENB) {
3933                 if (tiosp->c_cflag & PARODD)
3934                         mr1 |= (MR1_PARENB | MR1_PARODD);
3935                 else
3936                         mr1 |= (MR1_PARENB | MR1_PAREVEN);
3937         } else {
3938                 mr1 |= MR1_PARNONE;
3939         }
3940
3941         mr1 |= MR1_ERRBLOCK;
3942
3943 /*
3944  *      Set the RX FIFO threshold at 8 chars. This gives a bit of breathing
3945  *      space for hardware flow control and the like. This should be set to
3946  *      VMIN.
3947  */
3948         mr2 |= MR2_RXFIFOHALF;
3949
3950 /*
3951  *      Calculate the baud rate timers. For now we will just assume that
3952  *      the input and output baud are the same. The sc26198 has a fixed
3953  *      baud rate table, so only discrete baud rates possible.
3954  */
3955         if (tiosp->c_ispeed == 0)
3956                 tiosp->c_ispeed = tiosp->c_ospeed;
3957         if ((tiosp->c_ospeed < 0) || (tiosp->c_ospeed > SC26198_MAXBAUD))
3958                 return(EINVAL);
3959
3960         if (tiosp->c_ospeed > 0) {
3961                 for (clk = 0; (clk < SC26198_NRBAUDS); clk++) {
3962                         if (tiosp->c_ospeed <= sc26198_baudtable[clk])
3963                                 break;
3964                 }
3965         }
3966
3967 /*
3968  *      Check what form of modem signaling is required and set it up.
3969  */
3970         if ((tiosp->c_cflag & CLOCAL) == 0) {
3971                 iopr |= IOPR_DCDCOS;
3972                 imron |= IR_IOPORT;
3973         }
3974
3975 /*
3976  *      Setup sc26198 enhanced modes if we can. In particular we want to
3977  *      handle as much of the flow control as possible automatically. As
3978  *      well as saving a few CPU cycles it will also greatly improve flow
3979  *      control reliability.
3980  */
3981         if (tiosp->c_iflag & IXON) {
3982                 mr0 |= MR0_SWFTX | MR0_SWFT;
3983                 imron |= IR_XONXOFF;
3984         } else {
3985                 imroff |= IR_XONXOFF;
3986         }
3987 #if 0
3988         if (tiosp->c_iflag & IXOFF)
3989                 mr0 |= MR0_SWFRX;
3990 #endif
3991
3992         if (tiosp->c_cflag & CCTS_OFLOW)
3993                 mr2 |= MR2_AUTOCTS;
3994         if (tiosp->c_cflag & CRTS_IFLOW)
3995                 mr1 |= MR1_AUTORTS;
3996
3997 /*
3998  *      All sc26198 register values calculated so go through and set
3999  *      them all up.
4000  */
4001
4002 #if STLDEBUG
4003         kprintf("SETPORT: portnr=%d panelnr=%d brdnr=%d\n", portp->portnr,
4004                 portp->panelnr, portp->brdnr);
4005         kprintf("    mr0=%x mr1=%x mr2=%x clk=%x\n", mr0, mr1, mr2, clk);
4006         kprintf("    iopr=%x imron=%x imroff=%x\n", iopr, imron, imroff);
4007         kprintf("    schr1=%x schr2=%x schr3=%x schr4=%x\n",
4008                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP],
4009                 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP]);
4010 #endif
4011
4012         crit_enter();
4013         BRDENABLE(portp->brdnr, portp->pagenr);
4014         stl_sc26198setreg(portp, IMR, 0);
4015         stl_sc26198updatereg(portp, MR0, mr0);
4016         stl_sc26198updatereg(portp, MR1, mr1);
4017         stl_sc26198setreg(portp, SCCR, CR_RXERRBLOCK);
4018         stl_sc26198updatereg(portp, MR2, mr2);
4019         iopr = (stl_sc26198getreg(portp, IOPIOR) & ~IPR_CHANGEMASK) | iopr;
4020         if (tiosp->c_ospeed == 0) {
4021                 iopr &= ~IPR_DTR;
4022         } else {
4023                 iopr |= IPR_DTR;
4024                 stl_sc26198setreg(portp, TXCSR, clk);
4025                 stl_sc26198setreg(portp, RXCSR, clk);
4026         }
4027         stl_sc26198updatereg(portp, IOPIOR, iopr);
4028         stl_sc26198setreg(portp, XONCR, tiosp->c_cc[VSTART]);
4029         stl_sc26198setreg(portp, XOFFCR, tiosp->c_cc[VSTOP]);
4030         ipr = stl_sc26198getreg(portp, IPR);
4031         if (ipr & IPR_DCD)
4032                 portp->sigs &= ~TIOCM_CD;
4033         else
4034                 portp->sigs |= TIOCM_CD;
4035         portp->imr = (portp->imr & ~imroff) | imron;
4036         stl_sc26198setreg(portp, IMR, portp->imr);
4037         BRDDISABLE(portp->brdnr);
4038         portp->state &= ~(ASY_RTSFLOWMODE | ASY_CTSFLOWMODE);
4039         portp->state |= ((tiosp->c_cflag & CRTS_IFLOW) ? ASY_RTSFLOWMODE : 0);
4040         portp->state |= ((tiosp->c_cflag & CCTS_OFLOW) ? ASY_CTSFLOWMODE : 0);
4041         stl_ttyoptim(portp, tiosp);
4042         crit_exit();
4043
4044         return(0);
4045 }
4046
4047 /*****************************************************************************/
4048
4049 /*
4050  *      Set the state of the DTR and RTS signals.
4051  */
4052
4053 static void stl_sc26198setsignals(stlport_t *portp, int dtr, int rts)
4054 {
4055         unsigned char   iopioron, iopioroff;
4056
4057 #if STLDEBUG
4058         kprintf("stl_sc26198setsignals(portp=%x,dtr=%d,rts=%d)\n",
4059                 (int) portp, dtr, rts);
4060 #endif
4061
4062         iopioron = 0;
4063         iopioroff = 0;
4064         if (dtr == 0)
4065                 iopioroff |= IPR_DTR;
4066         else if (dtr > 0)
4067                 iopioron |= IPR_DTR;
4068         if (rts == 0)
4069                 iopioroff |= IPR_RTS;
4070         else if (rts > 0)
4071                 iopioron |= IPR_RTS;
4072
4073         crit_enter();
4074         BRDENABLE(portp->brdnr, portp->pagenr);
4075         if ((rts >= 0) && (portp->tty.t_cflag & CRTS_IFLOW)) {
4076                 if (rts == 0) {
4077                         stl_sc26198setreg(portp, MR1,
4078                                 (stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
4079                         portp->stats.rxrtsoff++;
4080                 } else {
4081                         stl_sc26198setreg(portp, MR1,
4082                                 (stl_sc26198getreg(portp, MR1) | MR1_AUTORTS));
4083                         portp->stats.rxrtson++;
4084                 }
4085         }
4086         stl_sc26198setreg(portp, IOPIOR,
4087                 ((stl_sc26198getreg(portp, IOPIOR) & ~iopioroff) | iopioron));
4088         BRDDISABLE(portp->brdnr);
4089         crit_exit();
4090 }
4091
4092 /*****************************************************************************/
4093
4094 /*
4095  *      Return the state of the signals.
4096  */
4097
4098 static int stl_sc26198getsignals(stlport_t *portp)
4099 {
4100         unsigned char   ipr;
4101         int             sigs;
4102
4103 #if STLDEBUG
4104         kprintf("stl_sc26198getsignals(portp=%x)\n", (int) portp);
4105 #endif
4106
4107         crit_enter();
4108         BRDENABLE(portp->brdnr, portp->pagenr);
4109         ipr = stl_sc26198getreg(portp, IPR);
4110         BRDDISABLE(portp->brdnr);
4111         crit_exit();
4112
4113         sigs = TIOCM_DSR;
4114         sigs |= (ipr & IPR_DCD) ? 0 : TIOCM_CD;
4115         sigs |= (ipr & IPR_CTS) ? 0 : TIOCM_CTS;
4116         sigs |= (ipr & IPR_DTR) ? 0: TIOCM_DTR;
4117         sigs |= (ipr & IPR_RTS) ? 0: TIOCM_RTS;
4118         return(sigs);
4119 }
4120
4121 /*****************************************************************************/
4122
4123 /*
4124  *      Enable/Disable the Transmitter and/or Receiver.
4125  */
4126
4127 static void stl_sc26198enablerxtx(stlport_t *portp, int rx, int tx)
4128 {
4129         unsigned char   ccr;
4130
4131 #if STLDEBUG
4132         kprintf("stl_sc26198enablerxtx(portp=%x,rx=%d,tx=%d)\n",
4133                 (int) portp, rx, tx);
4134 #endif
4135
4136         ccr = portp->crenable;
4137         if (tx == 0)
4138                 ccr &= ~CR_TXENABLE;
4139         else if (tx > 0)
4140                 ccr |= CR_TXENABLE;
4141         if (rx == 0)
4142                 ccr &= ~CR_RXENABLE;
4143         else if (rx > 0)
4144                 ccr |= CR_RXENABLE;
4145
4146         crit_enter();
4147         BRDENABLE(portp->brdnr, portp->pagenr);
4148         stl_sc26198setreg(portp, SCCR, ccr);
4149         BRDDISABLE(portp->brdnr);
4150         portp->crenable = ccr;
4151         crit_exit();
4152 }
4153
4154 /*****************************************************************************/
4155
4156 /*
4157  *      Start/stop the Transmitter and/or Receiver.
4158  */
4159
4160 static void stl_sc26198startrxtx(stlport_t *portp, int rx, int tx)
4161 {
4162         unsigned char   imr;
4163
4164 #if STLDEBUG
4165         kprintf("stl_sc26198startrxtx(portp=%x,rx=%d,tx=%d)\n",
4166                 (int) portp, rx, tx);
4167 #endif
4168
4169         imr = portp->imr;
4170         if (tx == 0)
4171                 imr &= ~IR_TXRDY;
4172         else if (tx == 1)
4173                 imr |= IR_TXRDY;
4174         if (rx == 0)
4175                 imr &= ~(IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG);
4176         else if (rx > 0)
4177                 imr |= IR_RXRDY | IR_RXBREAK | IR_RXWATCHDOG;
4178
4179         crit_enter();
4180         BRDENABLE(portp->brdnr, portp->pagenr);
4181         stl_sc26198setreg(portp, IMR, imr);
4182         BRDDISABLE(portp->brdnr);
4183         portp->imr = imr;
4184         if (tx > 0) {
4185                 portp->state |= ASY_TXBUSY;
4186                 portp->tty.t_state |= TS_BUSY;
4187         }
4188         crit_exit();
4189 }
4190
4191 /*****************************************************************************/
4192
4193 /*
4194  *      Disable all interrupts from this port.
4195  */
4196
4197 static void stl_sc26198disableintrs(stlport_t *portp)
4198 {
4199
4200 #if STLDEBUG
4201         kprintf("stl_sc26198disableintrs(portp=%x)\n", (int) portp);
4202 #endif
4203
4204         crit_enter();
4205         BRDENABLE(portp->brdnr, portp->pagenr);
4206         portp->imr = 0;
4207         stl_sc26198setreg(portp, IMR, 0);
4208         BRDDISABLE(portp->brdnr);
4209         crit_exit();
4210 }
4211
4212 /*****************************************************************************/
4213
4214 static void stl_sc26198sendbreak(stlport_t *portp, long len)
4215 {
4216
4217 #if STLDEBUG
4218         kprintf("stl_sc26198sendbreak(portp=%x,len=%d)\n",
4219                 (int) portp, (int) len);
4220 #endif
4221
4222         crit_enter();
4223         BRDENABLE(portp->brdnr, portp->pagenr);
4224         if (len == -1) {
4225                 stl_sc26198setreg(portp, SCCR, CR_TXSTARTBREAK);
4226                 portp->stats.txbreaks++;
4227         } else {
4228                 stl_sc26198setreg(portp, SCCR, CR_TXSTOPBREAK);
4229         }
4230         BRDDISABLE(portp->brdnr);
4231         crit_exit();
4232 }
4233
4234 /*****************************************************************************/
4235
4236 /*
4237  *      Take flow control actions...
4238  */
4239
4240 static void stl_sc26198sendflow(stlport_t *portp, int hw, int sw)
4241 {
4242         unsigned char   mr0;
4243
4244 #if STLDEBUG
4245         kprintf("stl_sc26198sendflow(portp=%x,hw=%d,sw=%d)\n",
4246                 (int) portp, hw, sw);
4247 #endif
4248
4249         if (portp == NULL)
4250                 return;
4251
4252         crit_enter();
4253         BRDENABLE(portp->brdnr, portp->pagenr);
4254
4255         if (sw >= 0) {
4256                 mr0 = stl_sc26198getreg(portp, MR0);
4257                 stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4258                 if (sw > 0) {
4259                         stl_sc26198setreg(portp, SCCR, CR_TXSENDXOFF);
4260                         mr0 &= ~MR0_SWFRX;
4261                         portp->stats.rxxoff++;
4262                 } else {
4263                         stl_sc26198setreg(portp, SCCR, CR_TXSENDXON);
4264                         mr0 |= MR0_SWFRX;
4265                         portp->stats.rxxon++;
4266                 }
4267                 stl_sc26198wait(portp);
4268                 stl_sc26198setreg(portp, MR0, mr0);
4269         }
4270
4271         if (hw == 0) {
4272                 portp->state |= ASY_RTSFLOW;
4273                 stl_sc26198setreg(portp, MR1,
4274                         (stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
4275                 stl_sc26198setreg(portp, IOPIOR,
4276                         (stl_sc26198getreg(portp, IOPIOR) & ~IOPR_RTS));
4277                 portp->stats.rxrtsoff++;
4278         } else if (hw > 0) {
4279                 portp->state &= ~ASY_RTSFLOW;
4280                 stl_sc26198setreg(portp, MR1,
4281                         (stl_sc26198getreg(portp, MR1) | MR1_AUTORTS));
4282                 stl_sc26198setreg(portp, IOPIOR,
4283                         (stl_sc26198getreg(portp, IOPIOR) | IOPR_RTS));
4284                 portp->stats.rxrtson++;
4285         }
4286
4287         BRDDISABLE(portp->brdnr);
4288         crit_exit();
4289 }
4290
4291 /*****************************************************************************/
4292
4293 /*
4294  *      Return the current state of data flow on this port. This is only
4295  *      really interresting when determining if data has fully completed
4296  *      transmission or not... The sc26198 interrupt scheme cannot
4297  *      determine when all data has actually drained, so we need to
4298  *      check the port statusy register to be sure.
4299  */
4300
4301 static int stl_sc26198datastate(stlport_t *portp)
4302 {
4303         unsigned char   sr;
4304
4305 #if STLDEBUG
4306         kprintf("stl_sc26198datastate(portp=%x)\n", (int) portp);
4307 #endif
4308
4309         if (portp == NULL)
4310                 return(0);
4311         if (portp->state & ASY_TXBUSY) 
4312                 return(1);
4313
4314         crit_enter();
4315         BRDENABLE(portp->brdnr, portp->pagenr);
4316         sr = stl_sc26198getreg(portp, SR);
4317         BRDDISABLE(portp->brdnr);
4318         crit_exit();
4319
4320         return((sr & SR_TXEMPTY) ? 0 : 1);
4321 }
4322
4323 /*****************************************************************************/
4324
4325 static void stl_sc26198flush(stlport_t *portp, int flag)
4326 {
4327
4328 #if STLDEBUG
4329         kprintf("stl_sc26198flush(portp=%x,flag=%x)\n", (int) portp, flag);
4330 #endif
4331
4332         if (portp == NULL)
4333                 return;
4334
4335         crit_enter();
4336         BRDENABLE(portp->brdnr, portp->pagenr);
4337         if (flag & FWRITE) {
4338                 stl_sc26198setreg(portp, SCCR, CR_TXRESET);
4339                 stl_sc26198setreg(portp, SCCR, portp->crenable);
4340         }
4341         if (flag & FREAD) {
4342                 while (stl_sc26198getreg(portp, SR) & SR_RXRDY)
4343                         stl_sc26198getreg(portp, RXFIFO);
4344         }
4345         BRDDISABLE(portp->brdnr);
4346         crit_exit();
4347 }
4348
4349 /*****************************************************************************/
4350
4351 /*
4352  *      If we are TX flow controlled and in IXANY mode then we may
4353  *      need to unflow control here. We gotta do this because of the
4354  *      automatic flow control modes of the sc26198 - which downs't
4355  *      support any concept of an IXANY mode.
4356  */
4357
4358 static void stl_sc26198txunflow(stlport_t *portp)
4359 {
4360         unsigned char   mr0;
4361
4362         mr0 = stl_sc26198getreg(portp, MR0);
4363         stl_sc26198setreg(portp, MR0, (mr0 & ~MR0_SWFRXTX));
4364         stl_sc26198setreg(portp, SCCR, CR_HOSTXON);
4365         stl_sc26198setreg(portp, MR0, mr0);
4366         portp->state &= ~ASY_TXFLOWED;
4367 }
4368
4369 /*****************************************************************************/
4370
4371 /*
4372  *      Delay for a small amount of time, to give the sc26198 a chance
4373  *      to process a command...
4374  */
4375
4376 static void stl_sc26198wait(stlport_t *portp)
4377 {
4378         int     i;
4379
4380 #if STLDEBUG
4381         kprintf("stl_sc26198wait(portp=%x)\n", (int) portp);
4382 #endif
4383
4384         if (portp == NULL)
4385                 return;
4386
4387         for (i = 0; (i < 20); i++)
4388                 stl_sc26198getglobreg(portp, TSTR);
4389 }
4390
4391 /*****************************************************************************/
4392
4393 /*
4394  *      Transmit interrupt handler. This has gotta be fast!  Handling TX
4395  *      chars is pretty simple, stuff as many as possible from the TX buffer
4396  *      into the sc26198 FIFO.
4397  */
4398
4399 static __inline void stl_sc26198txisr(stlport_t *portp)
4400 {
4401         unsigned int    ioaddr;
4402         unsigned char   mr0;
4403         char            *head, *tail;
4404         int             len, stlen;
4405
4406 #if STLDEBUG
4407         kprintf("stl_sc26198txisr(portp=%x)\n", (int) portp);
4408 #endif
4409
4410         ioaddr = portp->ioaddr;
4411
4412         head = portp->tx.head;
4413         tail = portp->tx.tail;
4414         len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
4415         if ((len == 0) || ((len < STL_TXBUFLOW) &&
4416             ((portp->state & ASY_TXLOW) == 0))) {
4417                 portp->state |= ASY_TXLOW;
4418                 stl_dotimeout();
4419         }
4420
4421         if (len == 0) {
4422                 outb((ioaddr + XP_ADDR), (MR0 | portp->uartaddr));
4423                 mr0 = inb(ioaddr + XP_DATA);
4424                 if ((mr0 & MR0_TXMASK) == MR0_TXEMPTY) {
4425                         portp->imr &= ~IR_TXRDY;
4426                         outb((ioaddr + XP_ADDR), (IMR | portp->uartaddr));
4427                         outb((ioaddr + XP_DATA), portp->imr);
4428                         portp->state |= ASY_TXEMPTY;
4429                         portp->state &= ~ASY_TXBUSY;
4430                 } else {
4431                         mr0 |= ((mr0 & ~MR0_TXMASK) | MR0_TXEMPTY);
4432                         outb((ioaddr + XP_DATA), mr0);
4433                 }
4434         } else {
4435                 len = MIN(len, SC26198_TXFIFOSIZE);
4436                 portp->stats.txtotal += len;
4437                 stlen = MIN(len, (portp->tx.endbuf - tail));
4438                 outb((ioaddr + XP_ADDR), GTXFIFO);
4439                 outsb((ioaddr + XP_DATA), tail, stlen);
4440                 len -= stlen;
4441                 tail += stlen;
4442                 if (tail >= portp->tx.endbuf)
4443                         tail = portp->tx.buf;
4444                 if (len > 0) {
4445                         outsb((ioaddr + XP_DATA), tail, len);
4446                         tail += len;
4447                 }
4448                 portp->tx.tail = tail;
4449         }
4450 }
4451
4452 /*****************************************************************************/
4453
4454 /*
4455  *      Receive character interrupt handler. Determine if we have good chars
4456  *      or bad chars and then process appropriately. Good chars are easy
4457  *      just shove the lot into the RX buffer and set all status byte to 0.
4458  *      If a bad RX char then process as required. This routine needs to be
4459  *      fast!
4460  */
4461
4462 static __inline void stl_sc26198rxisr(stlport_t *portp, unsigned int iack)
4463 {
4464 #if STLDEBUG
4465         kprintf("stl_sc26198rxisr(portp=%x,iack=%x)\n", (int) portp, iack);
4466 #endif
4467
4468         if ((iack & IVR_TYPEMASK) == IVR_RXDATA)
4469                 stl_sc26198rxgoodchars(portp);
4470         else
4471                 stl_sc26198rxbadchars(portp);
4472
4473 /*
4474  *      If we are TX flow controlled and in IXANY mode then we may need
4475  *      to unflow control here. We gotta do this because of the automatic
4476  *      flow control modes of the sc26198.
4477  */
4478         if ((portp->state & ASY_TXFLOWED) && (portp->tty.t_iflag & IXANY))
4479                 stl_sc26198txunflow(portp);
4480 }
4481
4482 /*****************************************************************************/
4483
4484 /*
4485  *      Process the good received characters from RX FIFO.
4486  */
4487
4488 static void stl_sc26198rxgoodchars(stlport_t *portp)
4489 {
4490         unsigned int    ioaddr, len, buflen, stlen;
4491         char            *head, *tail;
4492
4493 #if STLDEBUG
4494         kprintf("stl_sc26198rxgoodchars(port=%x)\n", (int) portp);
4495 #endif
4496
4497         ioaddr = portp->ioaddr;
4498
4499 /*
4500  *      First up, calculate how much room there is in the RX ring queue.
4501  *      We also want to keep track of the longest possible copy length,
4502  *      this has to allow for the wrapping of the ring queue.
4503  */
4504         head = portp->rx.head;
4505         tail = portp->rx.tail;
4506         if (head >= tail) {
4507                 buflen = STL_RXBUFSIZE - (head - tail) - 1;
4508                 stlen = portp->rx.endbuf - head;
4509         } else {
4510                 buflen = tail - head - 1;
4511                 stlen = buflen;
4512         }
4513
4514 /*
4515  *      Check if the input buffer is near full. If so then we should take
4516  *      some flow control action... It is very easy to do hardware and
4517  *      software flow control from here since we have the port selected on
4518  *      the UART.
4519  */
4520         if (buflen <= (STL_RXBUFSIZE - STL_RXBUFHIGH)) {
4521                 if (((portp->state & ASY_RTSFLOW) == 0) &&
4522                     (portp->state & ASY_RTSFLOWMODE)) {
4523                         portp->state |= ASY_RTSFLOW;
4524                         stl_sc26198setreg(portp, MR1,
4525                                 (stl_sc26198getreg(portp, MR1) & ~MR1_AUTORTS));
4526                         stl_sc26198setreg(portp, IOPIOR,
4527                                 (stl_sc26198getreg(portp, IOPIOR) & ~IOPR_RTS));
4528                         portp->stats.rxrtsoff++;
4529                 }
4530         }
4531
4532 /*
4533  *      OK we are set, process good data... If the RX ring queue is full
4534  *      just chuck the chars - don't leave them in the UART.
4535  */
4536         outb((ioaddr + XP_ADDR), GIBCR);
4537         len = inb(ioaddr + XP_DATA) + 1;
4538         if (buflen == 0) {
4539                 outb((ioaddr + XP_ADDR), GRXFIFO);
4540                 insb((ioaddr + XP_DATA), &stl_unwanted[0], len);
4541                 portp->stats.rxlost += len;
4542                 portp->stats.rxtotal += len;
4543         } else {
4544                 len = MIN(len, buflen);
4545                 portp->stats.rxtotal += len;
4546                 stlen = MIN(len, stlen);
4547                 if (len > 0) {
4548                         outb((ioaddr + XP_ADDR), GRXFIFO);
4549                         insb((ioaddr + XP_DATA), head, stlen);
4550                         head += stlen;
4551                         if (head >= portp->rx.endbuf) {
4552                                 head = portp->rx.buf;
4553                                 len -= stlen;
4554                                 insb((ioaddr + XP_DATA), head, len);
4555                                 head += len;
4556                         }
4557                 }
4558         }
4559
4560         portp->rx.head = head;
4561         portp->state |= ASY_RXDATA;
4562         stl_dotimeout();
4563 }
4564
4565 /*****************************************************************************/
4566
4567 /*
4568  *      Process all characters in the RX FIFO of the UART. Check all char
4569  *      status bytes as well, and process as required. We need to check
4570  *      all bytes in the FIFO, in case some more enter the FIFO while we
4571  *      are here. To get the exact character error type we need to switch
4572  *      into CHAR error mode (that is why we need to make sure we empty
4573  *      the FIFO).
4574  */
4575
4576 static void stl_sc26198rxbadchars(stlport_t *portp)
4577 {
4578         unsigned char   mr1;
4579         unsigned int    status;
4580         char            *head, *tail;
4581         char            ch;
4582         int             len;
4583
4584 /*
4585  *      First up, calculate how much room there is in the RX ring queue.
4586  *      We also want to keep track of the longest possible copy length,
4587  *      this has to allow for the wrapping of the ring queue.
4588  */
4589         head = portp->rx.head;
4590         tail = portp->rx.tail;
4591         len = (head >= tail) ? (STL_RXBUFSIZE - (head - tail) - 1) :
4592                 (tail - head - 1);
4593
4594 /*
4595  *      To get the precise error type for each character we must switch
4596  *      back into CHAR error mode.
4597  */
4598         mr1 = stl_sc26198getreg(portp, MR1);
4599         stl_sc26198setreg(portp, MR1, (mr1 & ~MR1_ERRBLOCK));
4600
4601         while ((status = stl_sc26198getreg(portp, SR)) & SR_RXRDY) {
4602                 stl_sc26198setreg(portp, SCCR, CR_CLEARRXERR);
4603                 ch = stl_sc26198getreg(portp, RXFIFO);
4604
4605                 if (status & SR_RXBREAK)
4606                         portp->stats.rxbreaks++;
4607                 if (status & SR_RXFRAMING)
4608                         portp->stats.rxframing++;
4609                 if (status & SR_RXPARITY)
4610                         portp->stats.rxparity++;
4611                 if (status & SR_RXOVERRUN)
4612                         portp->stats.rxoverrun++;
4613                 if ((portp->rxignoremsk & status) == 0) {
4614                         if ((portp->tty.t_state & TS_CAN_BYPASS_L_RINT) &&
4615                             ((status & SR_RXFRAMING) ||
4616                             ((status & SR_RXPARITY) &&
4617                             (portp->tty.t_iflag & INPCK))))
4618                                 ch = 0;
4619                         if ((portp->rxmarkmsk & status) == 0)
4620                                 status = 0;
4621                         if (len > 0) {
4622                                 *(head + STL_RXBUFSIZE) = status;
4623                                 *head++ = ch;
4624                                 if (head >= portp->rx.endbuf)
4625                                         head = portp->rx.buf;
4626                                 len--;
4627                         }
4628                 }
4629         }
4630
4631 /*
4632  *      To get correct interrupt class we must switch back into BLOCK
4633  *      error mode.
4634  */
4635         stl_sc26198setreg(portp, MR1, mr1);
4636
4637         portp->rx.head = head;
4638         portp->state |= ASY_RXDATA;
4639         stl_dotimeout();
4640 }
4641
4642 /*****************************************************************************/
4643
4644 /*
4645  *      Other interrupt handler. This includes modem signals, flow
4646  *      control actions, etc.
4647  */
4648
4649 static void stl_sc26198otherisr(stlport_t *portp, unsigned int iack)
4650 {
4651         unsigned char   cir, ipr, xisr;
4652
4653 #if STLDEBUG
4654         kprintf("stl_sc26198otherisr(portp=%x,iack=%x)\n", (int) portp, iack);
4655 #endif
4656
4657         cir = stl_sc26198getglobreg(portp, CIR);
4658
4659         switch (cir & CIR_SUBTYPEMASK) {
4660         case CIR_SUBCOS:
4661                 ipr = stl_sc26198getreg(portp, IPR);
4662                 if (ipr & IPR_DCDCHANGE) {
4663                         portp->state |= ASY_DCDCHANGE;
4664                         portp->stats.modem++;
4665                         stl_dotimeout();
4666                 }
4667                 break;
4668         case CIR_SUBXONXOFF:
4669                 xisr = stl_sc26198getreg(portp, XISR);
4670                 if (xisr & XISR_RXXONGOT) {
4671                         portp->state |= ASY_TXFLOWED;
4672                         portp->stats.txxoff++;
4673                 }
4674                 if (xisr & XISR_RXXOFFGOT) {
4675                         portp->state &= ~ASY_TXFLOWED;
4676                         portp->stats.txxon++;
4677                 }
4678                 break;
4679         case CIR_SUBBREAK:
4680                 stl_sc26198setreg(portp, SCCR, CR_BREAKRESET);
4681                 stl_sc26198rxbadchars(portp);
4682                 break;
4683         default:
4684                 break;
4685         }
4686 }
4687
4688 /*****************************************************************************/
4689
4690 /*
4691  *      Interrupt service routine for sc26198 panels.
4692  */
4693
4694 static void stl_sc26198intr(stlpanel_t *panelp, unsigned int iobase)
4695 {
4696         stlport_t       *portp;
4697         unsigned int    iack;
4698
4699 /* 
4700  *      Work around bug in sc26198 chip... Cannot have A6 address
4701  *      line of UART high, else iack will be returned as 0.
4702  */
4703         outb((iobase + 1), 0);
4704
4705         iack = inb(iobase + XP_IACK);
4706 #if STLDEBUG
4707         kprintf("stl_sc26198intr(panelp=%p,iobase=%x): iack=%x\n", panelp, iobase, iack);
4708 #endif
4709         portp = panelp->ports[(iack & IVR_CHANMASK) + ((iobase & 0x4) << 1)];
4710
4711         if (iack & IVR_RXDATA)
4712                 stl_sc26198rxisr(portp, iack);
4713         else if (iack & IVR_TXDATA)
4714                 stl_sc26198txisr(portp);
4715         else
4716                 stl_sc26198otherisr(portp, iack);
4717 }
4718
4719 /*****************************************************************************/