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