1 /*****************************************************************************/
4 * stallion.c -- stallion multiport serial driver.
6 * Copyright (c) 1995-1996 Greg Ungerer (gerg@stallion.oz.au).
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
39 /*****************************************************************************/
43 #include "opt_compat.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/kernel.h>
48 #include <sys/malloc.h>
51 #include <sys/fcntl.h>
53 #include <i386/isa/isa_device.h>
54 #include <i386/isa/ic/scd1400.h>
55 #include <machine/comstats.h>
59 #warning "The stallion pci device is broken and not compiled with LINT"
65 #error "The stallion pci driver requires the old pci compatibility shims"
67 #include <pci/pcivar.h>
68 #include <pci/pcireg.h>
71 /*****************************************************************************/
74 * Define the version level of the kernel - so we can compile in the
75 * appropriate bits of code. By default this will compile for a 2.1
86 /*****************************************************************************/
89 * Define different board types. At the moment I have only declared
90 * those boards that this driver supports. But I will use the standard
91 * "assigned" board numbers. In the future this driver will support
92 * some of the other Stallion boards. Currently supported boards are
93 * abbreviated as EIO = EasyIO and ECH = EasyConnection 8/32.
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...
105 static unsigned int stl_ioshared = 0x280;
106 static unsigned int stl_irqshared = 0;
108 /*****************************************************************************/
111 * Define important driver limitations.
113 #define STL_MAXBRDS 8
114 #define STL_MAXPANELS 4
115 #define STL_PORTSPERPANEL 16
116 #define STL_PORTSPERBRD 64
119 * Define the important minor number break down bits. These have been
120 * chosen to be "compatible" with the standard sio driver minor numbers.
121 * Extra high bits are used to distinguish between boards.
123 #define STL_CALLOUTDEV 0x80
124 #define STL_CTRLLOCK 0x40
125 #define STL_CTRLINIT 0x20
126 #define STL_CTRLDEV (STL_CTRLLOCK | STL_CTRLINIT)
128 #define STL_MEMDEV 0x07000000
130 #define STL_DEFSPEED TTYDEF_SPEED
131 #define STL_DEFCFLAG (CS8 | CREAD | HUPCL)
134 * I haven't really decided (or measured) what buffer sizes give
135 * a good balance between performance and memory usage. These seem
136 * to work pretty well...
138 #define STL_RXBUFSIZE 2048
139 #define STL_TXBUFSIZE 2048
141 #define STL_TXBUFLOW (STL_TXBUFSIZE / 4)
142 #define STL_RXBUFHIGH (3 * STL_RXBUFSIZE / 4)
144 /*****************************************************************************/
147 * Define our local driver identity first. Set up stuff to deal with
148 * all the local structures required by a serial tty driver.
150 static const char stl_drvname[] = "stl";
151 static const char stl_longdrvname[] = "Stallion Multiport Serial Driver";
152 static const char stl_drvversion[] = "1.0.0";
153 static int stl_brdprobed[STL_MAXBRDS];
155 static int stl_nrbrds = 0;
156 static int stl_doingtimeout = 0;
158 static const char __file__[] = /*__FILE__*/ "stallion.c";
161 * Define global stats structures. Not used often, and can be
162 * re-used for each stats call.
164 static combrd_t stl_brdstats;
165 static comstats_t stl_comstats;
167 /*****************************************************************************/
170 * Define a set of structures to hold all the board/panel/port info
171 * for our ports. These will be dynamically allocated as required.
175 * Define a ring queue structure for each port. This will hold the
176 * TX data waiting to be output. Characters are fed into this buffer
177 * from the line discipline (or even direct from user space!) and
178 * then fed into the UARTs during interrupts. Will use a clasic ring
179 * queue here for this. The good thing about this type of ring queue
180 * is that the head and tail pointers can be updated without interrupt
181 * protection - since "write" code only needs to change the head, and
182 * interrupt code only needs to change the tail.
192 * Port, panel and board structures to hold status info about each.
193 * The board structure contains pointers to structures for each panel
194 * connected to it, and in turn each panel structure contains pointers
195 * for each port structure for each port on that panel. Note that
196 * the port structure also contains the board and panel number that it
197 * is associated with, this makes it (fairly) easy to get back to the
198 * board/panel info for a port. Also note that the tty struct is at
199 * the top of the structure, this is important, since the code uses
200 * this fact to get the port struct pointer from the tty struct
220 unsigned int rxignoremsk;
221 unsigned int rxmarkmsk;
223 struct termios initintios;
224 struct termios initouttios;
225 struct termios lockintios;
226 struct termios lockouttios;
227 struct timeval timestamp;
241 unsigned int ackmask;
242 stlport_t *ports[STL_PORTSPERPANEL];
254 unsigned int ioaddr1;
255 unsigned int ioaddr2;
256 unsigned int iostatus;
258 unsigned int ioctrlval;
261 stlpanel_t *panels[STL_MAXPANELS];
262 stlport_t *ports[STL_PORTSPERBRD];
265 static stlbrd_t *stl_brds[STL_MAXBRDS];
268 * Per board state flags. Used with the state field of the board struct.
269 * Not really much here yet!
271 #define BRD_FOUND 0x1
274 * Define the port structure state flags. These set of flags are
275 * modified at interrupt time - so setting and reseting them needs
278 #define ASY_TXLOW 0x1
279 #define ASY_RXDATA 0x2
280 #define ASY_DCDCHANGE 0x4
281 #define ASY_DTRWAIT 0x8
282 #define ASY_RTSFLOW 0x10
283 #define ASY_RTSFLOWMODE 0x20
284 #define ASY_CTSFLOWMODE 0x40
286 #define ASY_ACTIVE (ASY_TXLOW | ASY_RXDATA | ASY_DCDCHANGE)
289 * Define an array of board names as printable strings. Handy for
290 * referencing boards when printing trace and stuff.
292 static char *stl_brdnames[] = {
322 /*****************************************************************************/
325 * Hardware ID bits for the EasyIO and ECH boards. These defines apply
326 * to the directly accessible io ports of these boards (not the cd1400
327 * uarts - they are in scd1400.h).
329 #define EIO_8PORTRS 0x04
330 #define EIO_4PORTRS 0x05
331 #define EIO_8PORTDI 0x00
332 #define EIO_8PORTM 0x06
333 #define EIO_IDBITMASK 0x07
334 #define EIO_INTRPEND 0x08
335 #define EIO_INTEDGE 0x00
336 #define EIO_INTLEVEL 0x08
339 #define ECH_IDBITMASK 0xe0
340 #define ECH_BRDENABLE 0x08
341 #define ECH_BRDDISABLE 0x00
342 #define ECH_INTENABLE 0x01
343 #define ECH_INTDISABLE 0x00
344 #define ECH_INTLEVEL 0x02
345 #define ECH_INTEDGE 0x00
346 #define ECH_INTRPEND 0x01
347 #define ECH_BRDRESET 0x01
349 #define ECHMC_INTENABLE 0x01
350 #define ECHMC_BRDRESET 0x02
352 #define ECH_PNLSTATUS 2
353 #define ECH_PNL16PORT 0x20
354 #define ECH_PNLIDMASK 0x07
355 #define ECH_PNLINTRPEND 0x80
356 #define ECH_ADDR2MASK 0x1e0
358 #define EIO_CLK 25000000
359 #define EIO_CLK8M 20000000
360 #define ECH_CLK EIO_CLK
363 * Define the offsets within the register bank for all io registers.
364 * These io address offsets are common to both the EIO and ECH.
372 #define EREG_BANKSIZE 8
375 * Define the PCI vendor and device id for ECH8/32-PCI.
377 #define STL_PCIDEVID 0xd001100b
380 * Define the vector mapping bits for the programmable interrupt board
381 * hardware. These bits encode the interrupt for the board to use - it
382 * is software selectable (except the EIO-8M).
384 static unsigned char stl_vecmap[] = {
385 0xff, 0xff, 0xff, 0x04, 0x06, 0x05, 0xff, 0x07,
386 0xff, 0xff, 0x00, 0x02, 0x01, 0xff, 0xff, 0x03
390 * Set up enable and disable macros for the ECH boards. They require
391 * the secondary io address space to be activated and deactivated.
392 * This way all ECH boards can share their secondary io region.
393 * If this is an ECH-PCI board then also need to set the page pointer
394 * to point to the correct page.
396 #define BRDENABLE(brdnr,pagenr) \
397 if (stl_brds[(brdnr)]->brdtype == BRD_ECH) \
398 outb(stl_brds[(brdnr)]->ioctrl, \
399 (stl_brds[(brdnr)]->ioctrlval | ECH_BRDENABLE));\
400 else if (stl_brds[(brdnr)]->brdtype == BRD_ECHPCI) \
401 outb(stl_brds[(brdnr)]->ioctrl, (pagenr));
403 #define BRDDISABLE(brdnr) \
404 if (stl_brds[(brdnr)]->brdtype == BRD_ECH) \
405 outb(stl_brds[(brdnr)]->ioctrl, \
406 (stl_brds[(brdnr)]->ioctrlval | ECH_BRDDISABLE));
409 * Define the cd1400 baud rate clocks. These are used when calculating
410 * what clock and divisor to use for the required baud rate. Also
411 * define the maximum baud rate allowed, and the default base baud.
413 static int stl_cd1400clkdivs[] = {
414 CD1400_CLK0, CD1400_CLK1, CD1400_CLK2, CD1400_CLK3, CD1400_CLK4
417 #define STL_MAXBAUD 230400
419 /*****************************************************************************/
422 * Define macros to extract a brd and port number from a minor number.
423 * This uses the extended minor number range in the upper 2 bytes of
424 * the device number. This gives us plenty of minor numbers to play
427 #define MKDEV2BRD(m) ((minor(m) & 0x00700000) >> 20)
428 #define MKDEV2PORT(m) ((minor(m) & 0x1f) | ((minor(m) & 0x00010000) >> 11))
431 * Define some handy local macros...
434 #define MIN(a,b) (((a) <= (b)) ? (a) : (b))
437 /*****************************************************************************/
440 * Declare all those functions in this driver! First up is the set of
441 * externally visible functions.
444 static int stlprobe(struct isa_device *idp);
445 static int stlattach(struct isa_device *idp);
447 STATIC d_open_t stlopen;
448 STATIC d_close_t stlclose;
449 STATIC d_ioctl_t stlioctl;
452 * Internal function prototypes.
454 static stlport_t *stl_dev2port(dev_t dev);
455 static int stl_findfreeunit(void);
456 static int stl_rawopen(stlport_t *portp);
457 static int stl_rawclose(stlport_t *portp);
458 static int stl_param(struct tty *tp, struct termios *tiosp);
459 static void stl_start(struct tty *tp);
460 static void stl_stop(struct tty *tp, int);
461 static void stl_ttyoptim(stlport_t *portp, struct termios *tiosp);
462 static void stl_dotimeout(void);
463 static void stl_poll(void *arg);
464 static void stl_rxprocess(stlport_t *portp);
465 static void stl_dtrwakeup(void *arg);
466 static int stl_brdinit(stlbrd_t *brdp);
467 static int stl_initeio(stlbrd_t *brdp);
468 static int stl_initech(stlbrd_t *brdp);
469 static int stl_initports(stlbrd_t *brdp, stlpanel_t *panelp);
470 static ointhand2_t stlintr;
471 static __inline void stl_txisr(stlpanel_t *panelp, int ioaddr);
472 static __inline void stl_rxisr(stlpanel_t *panelp, int ioaddr);
473 static __inline void stl_mdmisr(stlpanel_t *panelp, int ioaddr);
474 static void stl_setreg(stlport_t *portp, int regnr, int value);
475 static int stl_getreg(stlport_t *portp, int regnr);
476 static int stl_updatereg(stlport_t *portp, int regnr, int value);
477 static int stl_getsignals(stlport_t *portp);
478 static void stl_setsignals(stlport_t *portp, int dtr, int rts);
479 static void stl_flowcontrol(stlport_t *portp, int hw, int sw);
480 static void stl_ccrwait(stlport_t *portp);
481 static void stl_enablerxtx(stlport_t *portp, int rx, int tx);
482 static void stl_startrxtx(stlport_t *portp, int rx, int tx);
483 static void stl_disableintrs(stlport_t *portp);
484 static void stl_sendbreak(stlport_t *portp, long len);
485 static void stl_flush(stlport_t *portp, int flag);
486 static int stl_memioctl(dev_t dev, unsigned long cmd, caddr_t data,
487 int flag, struct proc *p);
488 static int stl_getbrdstats(caddr_t data);
489 static int stl_getportstats(stlport_t *portp, caddr_t data);
490 static int stl_clrportstats(stlport_t *portp, caddr_t data);
491 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr);
494 static const char *stlpciprobe(pcici_t tag, pcidi_t type);
495 static void stlpciattach(pcici_t tag, int unit);
496 static void stlpciintr(void * arg);
499 /*****************************************************************************/
502 * Declare the driver isa structure.
504 struct isa_driver stldriver = {
510 COMPAT_ISA_DRIVER(stl, stldriver);
512 /*****************************************************************************/
517 * Declare the driver pci structure.
519 static unsigned long stl_count;
521 static struct pci_device stlpcidriver = {
529 COMPAT_PCI_DRIVER (stlpci, stlpcidriver);
533 /*****************************************************************************/
538 * FreeBSD-2.2+ kernel linkage.
541 #define CDEV_MAJOR 72
542 static struct cdevsw stl_cdevsw = {
544 /* close */ stlclose,
546 /* write */ ttywrite,
547 /* ioctl */ stlioctl,
550 /* strategy */ nostrategy,
552 /* maj */ CDEV_MAJOR,
555 /* flags */ D_TTY | D_KQFILTER,
557 /* kqfilter */ ttykqfilter,
560 static void stl_drvinit(void *unused)
563 cdevsw_add(&stl_cdevsw);
566 SYSINIT(sidev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,stl_drvinit,NULL)
570 /*****************************************************************************/
573 * Probe for some type of EasyIO or EasyConnection 8/32 board at
574 * the supplied address. All we do is check if we can find the
575 * board ID for the board... (Note, PCI boards not checked here,
576 * they are done in the stlpciprobe() routine).
579 static int stlprobe(struct isa_device *idp)
584 printf("stlprobe(idp=%x): unit=%d iobase=%x\n", (int) idp,
585 idp->id_unit, idp->id_iobase);
588 if (idp->id_unit > STL_MAXBRDS)
591 status = inb(idp->id_iobase + 1);
592 if ((status & ECH_IDBITMASK) == ECH_ID) {
593 stl_brdprobed[idp->id_unit] = BRD_ECH;
597 status = inb(idp->id_iobase + 2);
598 switch (status & EIO_IDBITMASK) {
603 stl_brdprobed[idp->id_unit] = BRD_EASYIO;
612 /*****************************************************************************/
615 * Find an available internal board number (unit number). The problem
616 * is that the same unit numbers can be assigned to different boards
617 * detected during the ISA and PCI initialization phases.
620 static int stl_findfreeunit()
624 for (i = 0; (i < STL_MAXBRDS); i++)
625 if (stl_brds[i] == (stlbrd_t *) NULL)
627 return((i >= STL_MAXBRDS) ? -1 : i);
630 /*****************************************************************************/
633 * Allocate resources for and initialize the specified board.
636 static int stlattach(struct isa_device *idp)
641 printf("stlattach(idp=%p): unit=%d iobase=%x\n", (void *) idp,
642 idp->id_unit, idp->id_iobase);
645 idp->id_ointr = stlintr;
647 brdp = (stlbrd_t *) malloc(sizeof(stlbrd_t), M_TTYS, M_NOWAIT | M_ZERO);
648 if (brdp == (stlbrd_t *) NULL) {
649 printf("STALLION: failed to allocate memory (size=%d)\n",
654 if ((brdp->brdnr = stl_findfreeunit()) < 0) {
655 printf("STALLION: too many boards found, max=%d\n",
659 if (brdp->brdnr >= stl_nrbrds)
660 stl_nrbrds = brdp->brdnr + 1;
662 brdp->unitid = idp->id_unit;
663 brdp->brdtype = stl_brdprobed[idp->id_unit];
664 brdp->ioaddr1 = idp->id_iobase;
665 brdp->ioaddr2 = stl_ioshared;
666 brdp->irq = ffs(idp->id_irq) - 1;
667 brdp->irqtype = stl_irqshared;
673 /*****************************************************************************/
678 * Probe specifically for the PCI boards. We need to be a little
679 * carefull here, since it looks sort like a Nat Semi IDE chip...
682 static const char *stlpciprobe(pcici_t tag, pcidi_t type)
687 printf("stlpciprobe(tag=%x,type=%x)\n", (int) &tag, (int) type);
694 return((char *) NULL);
697 class = pci_conf_read(tag, PCI_CLASS_REG);
698 if ((class & PCI_CLASS_MASK) == PCI_CLASS_MASS_STORAGE)
699 return((char *) NULL);
701 return("Stallion EasyConnection 8/32-PCI");
704 /*****************************************************************************/
707 * Allocate resources for and initialize the specified PCI board.
710 void stlpciattach(pcici_t tag, int unit)
715 printf("stlpciattach(tag=%x,unit=%x)\n", (int) &tag, unit);
718 brdp = (stlbrd_t *) malloc(sizeof(stlbrd_t), M_TTYS, M_NOWAIT | M_ZERO);
719 if (brdp == (stlbrd_t *) NULL) {
720 printf("STALLION: failed to allocate memory (size=%d)\n",
725 if ((unit < 0) || (unit > STL_MAXBRDS)) {
726 printf("STALLION: bad PCI board unit number=%d\n", unit);
731 * Allocate us a new driver unique unit number.
733 if ((brdp->brdnr = stl_findfreeunit()) < 0) {
734 printf("STALLION: too many boards found, max=%d\n",
738 if (brdp->brdnr >= stl_nrbrds)
739 stl_nrbrds = brdp->brdnr + 1;
742 brdp->brdtype = BRD_ECHPCI;
743 brdp->ioaddr1 = ((unsigned int) pci_conf_read(tag, 0x14)) & 0xfffc;
744 brdp->ioaddr2 = ((unsigned int) pci_conf_read(tag, 0x10)) & 0xfffc;
745 brdp->irq = ((int) pci_conf_read(tag, 0x3c)) & 0xff;
747 if (pci_map_int(tag, stlpciintr, (void *) NULL, &tty_imask) == 0) {
748 printf("STALLION: failed to map interrupt irq=%d for unit=%d\n",
749 brdp->irq, brdp->brdnr);
754 printf("%s(%d): ECH-PCI iobase=%x iopage=%x irq=%d\n", __file__, __LINE__, brdp->ioaddr2, brdp->ioaddr1, brdp->irq);
761 /*****************************************************************************/
763 STATIC int stlopen(dev_t dev, int flag, int mode, struct proc *p)
767 int error, callout, x;
770 printf("stlopen(dev=%x,flag=%x,mode=%x,p=%x)\n", (int) dev, flag,
775 * Firstly check if the supplied device number is a valid device.
777 if (minor(dev) & STL_MEMDEV)
780 portp = stl_dev2port(dev);
781 if (portp == (stlport_t *) NULL)
785 callout = minor(dev) & STL_CALLOUTDEV;
792 * Wait here for the DTR drop timeout period to expire.
794 while (portp->state & ASY_DTRWAIT) {
795 error = tsleep(&portp->dtrwait, (TTIPRI | PCATCH),
802 * We have a valid device, so now we check if it is already open.
803 * If not then initialize the port hardware and set up the tty
804 * struct as required.
806 if ((tp->t_state & TS_ISOPEN) == 0) {
807 tp->t_oproc = stl_start;
808 tp->t_stop = stl_stop;
809 tp->t_param = stl_param;
811 tp->t_termios = callout ? portp->initouttios :
814 if ((portp->sigs & TIOCM_CD) || callout)
815 (*linesw[tp->t_line].l_modem)(tp, 1);
818 if (portp->callout == 0) {
823 if (portp->callout != 0) {
824 if (flag & O_NONBLOCK) {
828 error = tsleep(&portp->callout,
829 (TTIPRI | PCATCH), "stlcall", 0);
832 goto stlopen_restart;
835 if ((tp->t_state & TS_XCLUDE) &&
843 * If this port is not the callout device and we do not have carrier
844 * then we need to sleep, waiting for it to be asserted.
846 if (((tp->t_state & TS_CARR_ON) == 0) && !callout &&
847 ((tp->t_cflag & CLOCAL) == 0) &&
848 ((flag & O_NONBLOCK) == 0)) {
850 error = tsleep(TSA_CARR_ON(tp), (TTIPRI | PCATCH), "stldcd", 0);
854 goto stlopen_restart;
858 * Open the line discipline.
860 error = (*linesw[tp->t_line].l_open)(dev, tp);
861 stl_ttyoptim(portp, &tp->t_termios);
862 if ((tp->t_state & TS_ISOPEN) && callout)
866 * If for any reason we get to here and the port is not actually
867 * open then close of the physical hardware - no point leaving it
868 * active when the open failed...
872 if (((tp->t_state & TS_ISOPEN) == 0) && (portp->waitopens == 0))
878 /*****************************************************************************/
880 STATIC int stlclose(dev_t dev, int flag, int mode, struct proc *p)
887 printf("stlclose(dev=%s,flag=%x,mode=%x,p=%p)\n", devtoname(dev),
888 flag, mode, (void *) p);
891 if (minor(dev) & STL_MEMDEV)
894 portp = stl_dev2port(dev);
895 if (portp == (stlport_t *) NULL)
900 (*linesw[tp->t_line].l_close)(tp, flag);
901 stl_ttyoptim(portp, &tp->t_termios);
908 /*****************************************************************************/
912 STATIC void stl_stop(struct tty *tp, int rw)
915 printf("stl_stop(tp=%x,rw=%x)\n", (int) tp, rw);
918 stl_flush((stlport_t *) tp, rw);
923 STATIC int stlstop(struct tty *tp, int rw)
926 printf("stlstop(tp=%x,rw=%x)\n", (int) tp, rw);
929 stl_flush((stlport_t *) tp, rw);
935 /*****************************************************************************/
937 STATIC int stlioctl(dev_t dev, unsigned long cmd, caddr_t data, int flag,
940 struct termios *newtios, *localtios;
946 printf("stlioctl(dev=%s,cmd=%lx,data=%p,flag=%x,p=%p)\n",
947 devtoname(dev), cmd, (void *) data, flag, (void *) p);
950 if (minor(dev) & STL_MEMDEV)
951 return(stl_memioctl(dev, cmd, data, flag, p));
953 portp = stl_dev2port(dev);
954 if (portp == (stlport_t *) NULL)
960 * First up handle ioctls on the control devices.
962 if (minor(dev) & STL_CTRLDEV) {
963 if ((minor(dev) & STL_CTRLDEV) == STL_CTRLINIT)
964 localtios = (minor(dev) & STL_CALLOUTDEV) ?
965 &portp->initouttios : &portp->initintios;
966 else if ((minor(dev) & STL_CTRLDEV) == STL_CTRLLOCK)
967 localtios = (minor(dev) & STL_CALLOUTDEV) ?
968 &portp->lockouttios : &portp->lockintios;
974 if ((error = suser(p)) == 0)
975 *localtios = *((struct termios *) data);
978 *((struct termios *) data) = *localtios;
981 *((int *) data) = TTYDISC;
984 bzero(data, sizeof(struct winsize));
994 * Deal with 4.3 compatibility issues if we have too...
996 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
999 unsigned long oldcmd;
1001 tios = tp->t_termios;
1003 if ((error = ttsetcompat(tp, &cmd, data, &tios)))
1006 data = (caddr_t) &tios;
1011 * Carry out some pre-cmd processing work first...
1012 * Hmmm, not so sure we want this, disable for now...
1014 if ((cmd == TIOCSETA) || (cmd == TIOCSETAW) || (cmd == TIOCSETAF)) {
1015 newtios = (struct termios *) data;
1016 localtios = (minor(dev) & STL_CALLOUTDEV) ?
1017 &portp->lockouttios : &portp->lockintios;
1019 newtios->c_iflag = (tp->t_iflag & localtios->c_iflag) |
1020 (newtios->c_iflag & ~localtios->c_iflag);
1021 newtios->c_oflag = (tp->t_oflag & localtios->c_oflag) |
1022 (newtios->c_oflag & ~localtios->c_oflag);
1023 newtios->c_cflag = (tp->t_cflag & localtios->c_cflag) |
1024 (newtios->c_cflag & ~localtios->c_cflag);
1025 newtios->c_lflag = (tp->t_lflag & localtios->c_lflag) |
1026 (newtios->c_lflag & ~localtios->c_lflag);
1027 for (i = 0; (i < NCCS); i++) {
1028 if (localtios->c_cc[i] != 0)
1029 newtios->c_cc[i] = tp->t_cc[i];
1031 if (localtios->c_ispeed != 0)
1032 newtios->c_ispeed = tp->t_ispeed;
1033 if (localtios->c_ospeed != 0)
1034 newtios->c_ospeed = tp->t_ospeed;
1038 * Call the line discipline and the common command processing to
1039 * process this command (if they can).
1041 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1042 if (error != ENOIOCTL)
1046 error = ttioctl(tp, cmd, data, flag);
1047 stl_ttyoptim(portp, &tp->t_termios);
1048 if (error != ENOIOCTL) {
1056 * Process local commands here. These are all commands that only we
1057 * can take care of (they all rely on actually doing something special
1058 * to the actual hardware).
1062 stl_sendbreak(portp, -1);
1065 stl_sendbreak(portp, -2);
1068 stl_setsignals(portp, 1, -1);
1071 stl_setsignals(portp, 0, -1);
1074 i = *((int *) data);
1075 stl_setsignals(portp, ((i & TIOCM_DTR) ? 1 : 0),
1076 ((i & TIOCM_RTS) ? 1 : 0));
1079 i = *((int *) data);
1080 stl_setsignals(portp, ((i & TIOCM_DTR) ? 1 : -1),
1081 ((i & TIOCM_RTS) ? 1 : -1));
1084 i = *((int *) data);
1085 stl_setsignals(portp, ((i & TIOCM_DTR) ? 0 : -1),
1086 ((i & TIOCM_RTS) ? 0 : -1));
1089 *((int *) data) = (stl_getsignals(portp) | TIOCM_LE);
1092 if ((error = suser(p)) == 0)
1093 portp->dtrwait = *((int *) data) * hz / 100;
1096 *((int *) data) = portp->dtrwait * 100 / hz;
1099 portp->dotimestamp = 1;
1100 *((struct timeval *) data) = portp->timestamp;
1111 /*****************************************************************************/
1114 * Convert the specified minor device number into a port struct
1115 * pointer. Return NULL if the device number is not a valid port.
1118 STATIC stlport_t *stl_dev2port(dev_t dev)
1122 brdp = stl_brds[MKDEV2BRD(dev)];
1123 if (brdp == (stlbrd_t *) NULL)
1124 return((stlport_t *) NULL);
1125 return(brdp->ports[MKDEV2PORT(dev)]);
1128 /*****************************************************************************/
1131 * Initialize the port hardware. This involves enabling the transmitter
1132 * and receiver, setting the port configuration, and setting the initial
1136 static int stl_rawopen(stlport_t *portp)
1139 printf("stl_rawopen(portp=%p): brdnr=%d panelnr=%d portnr=%d\n",
1140 (void *) portp, portp->brdnr, portp->panelnr, portp->portnr);
1142 stl_param(&portp->tty, &portp->tty.t_termios);
1143 portp->sigs = stl_getsignals(portp);
1144 stl_setsignals(portp, 1, 1);
1145 stl_enablerxtx(portp, 1, 1);
1146 stl_startrxtx(portp, 1, 0);
1150 /*****************************************************************************/
1153 * Shutdown the hardware of a port. Disable its transmitter and
1154 * receiver, and maybe drop signals if appropriate.
1157 static int stl_rawclose(stlport_t *portp)
1162 printf("stl_rawclose(portp=%p): brdnr=%d panelnr=%d portnr=%d\n",
1163 (void *) portp, portp->brdnr, portp->panelnr, portp->portnr);
1167 stl_disableintrs(portp);
1168 stl_enablerxtx(portp, 0, 0);
1169 stl_flush(portp, (FWRITE | FREAD));
1170 if (tp->t_cflag & HUPCL) {
1171 stl_setsignals(portp, 0, 0);
1172 if (portp->dtrwait != 0) {
1173 portp->state |= ASY_DTRWAIT;
1174 timeout(stl_dtrwakeup, portp, portp->dtrwait);
1179 portp->state &= ~(ASY_ACTIVE | ASY_RTSFLOW);
1180 wakeup(&portp->callout);
1181 wakeup(TSA_CARR_ON(tp));
1185 /*****************************************************************************/
1188 * Clear the DTR waiting flag, and wake up any sleepers waiting for
1189 * DTR wait period to finish.
1192 static void stl_dtrwakeup(void *arg)
1196 portp = (stlport_t *) arg;
1197 portp->state &= ~ASY_DTRWAIT;
1198 wakeup(&portp->dtrwait);
1201 /*****************************************************************************/
1204 * Start (or continue) the transfer of TX data on this port. If the
1205 * port is not currently busy then load up the interrupt ring queue
1206 * buffer and kick of the transmitter. If the port is running low on
1207 * TX data then refill the ring queue. This routine is also used to
1208 * activate input flow control!
1211 static void stl_start(struct tty *tp)
1214 unsigned int len, stlen;
1218 portp = (stlport_t *) tp;
1221 printf("stl_start(tp=%x): brdnr=%d portnr=%d\n", (int) tp,
1222 portp->brdnr, portp->portnr);
1228 * Check if the ports input has been blocked, and take appropriate action.
1229 * Not very often do we really need to do anything, so make it quick.
1231 if (tp->t_state & TS_TBLOCK) {
1232 if ((portp->state & ASY_RTSFLOW) == 0)
1233 stl_flowcontrol(portp, 0, -1);
1235 if (portp->state & ASY_RTSFLOW)
1236 stl_flowcontrol(portp, 1, -1);
1241 * Check if the output cooked clist buffers are near empty, wake up
1242 * the line discipline to fill it up.
1244 if (tp->t_outq.c_cc <= tp->t_lowat) {
1245 if (tp->t_state & TS_ASLEEP) {
1246 tp->t_state &= ~TS_ASLEEP;
1247 wakeup(&tp->t_outq);
1249 selwakeup(&tp->t_wsel);
1253 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
1259 * Copy data from the clists into the interrupt ring queue. This will
1260 * require at most 2 copys... What we do is calculate how many chars
1261 * can fit into the ring queue, and how many can fit in 1 copy. If after
1262 * the first copy there is still more room then do the second copy.
1263 * The beauty of this type of ring queue is that we do not need to
1264 * spl protect our-selves, since we only ever update the head pointer,
1265 * and the interrupt routine only ever updates the tail pointer.
1267 if (tp->t_outq.c_cc != 0) {
1268 head = portp->tx.head;
1269 tail = portp->tx.tail;
1271 len = STL_TXBUFSIZE - (head - tail) - 1;
1272 stlen = portp->tx.endbuf - head;
1274 len = tail - head - 1;
1279 stlen = MIN(len, stlen);
1280 count = q_to_b(&tp->t_outq, head, stlen);
1283 if (head >= portp->tx.endbuf) {
1284 head = portp->tx.buf;
1286 stlen = q_to_b(&tp->t_outq, head, len);
1291 portp->tx.head = head;
1293 stl_startrxtx(portp, -1, 1);
1297 * If we sent something, make sure we are called again.
1299 tp->t_state |= TS_BUSY;
1304 * Do any writer wakeups.
1312 /*****************************************************************************/
1314 static void stl_flush(stlport_t *portp, int flag)
1320 printf("stl_flush(portp=%x,flag=%x)\n", (int) portp, flag);
1323 if (portp == (stlport_t *) NULL)
1328 if (flag & FWRITE) {
1329 BRDENABLE(portp->brdnr, portp->pagenr);
1330 stl_setreg(portp, CAR, (portp->portnr & 0x03));
1332 stl_setreg(portp, CCR, CCR_TXFLUSHFIFO);
1334 portp->tx.tail = portp->tx.head;
1335 BRDDISABLE(portp->brdnr);
1339 * The only thing to watch out for when flushing the read side is
1340 * the RX status buffer. The interrupt code relys on the status
1341 * bytes as being zeroed all the time (it does not bother setting
1342 * a good char status to 0, it expects that it already will be).
1343 * We also need to un-flow the RX channel if flow control was
1347 head = portp->rx.head;
1348 tail = portp->rx.tail;
1353 len = portp->rx.endbuf - tail;
1354 bzero(portp->rxstatus.buf,
1355 (head - portp->rx.buf));
1357 bzero((tail + STL_RXBUFSIZE), len);
1358 portp->rx.tail = head;
1361 if ((portp->state & ASY_RTSFLOW) &&
1362 ((portp->tty.t_state & TS_TBLOCK) == 0))
1363 stl_flowcontrol(portp, 1, -1);
1369 /*****************************************************************************/
1372 * These functions get/set/update the registers of the cd1400 UARTs.
1373 * Access to the cd1400 registers is via an address/data io port pair.
1374 * (Maybe should make this inline...)
1377 static int stl_getreg(stlport_t *portp, int regnr)
1379 outb(portp->ioaddr, (regnr + portp->uartaddr));
1380 return(inb(portp->ioaddr + EREG_DATA));
1383 /*****************************************************************************/
1385 static void stl_setreg(stlport_t *portp, int regnr, int value)
1387 outb(portp->ioaddr, (regnr + portp->uartaddr));
1388 outb((portp->ioaddr + EREG_DATA), value);
1391 /*****************************************************************************/
1393 static int stl_updatereg(stlport_t *portp, int regnr, int value)
1395 outb(portp->ioaddr, (regnr + portp->uartaddr));
1396 if (inb(portp->ioaddr + EREG_DATA) != value) {
1397 outb((portp->ioaddr + EREG_DATA), value);
1403 /*****************************************************************************/
1406 * Wait for the command register to be ready. We will poll this, since
1407 * it won't usually take too long to be ready, and it is only really
1408 * used for non-critical actions.
1411 static void stl_ccrwait(stlport_t *portp)
1415 for (i = 0; (i < CCR_MAXWAIT); i++) {
1416 if (stl_getreg(portp, CCR) == 0) {
1421 printf("STALLION: cd1400 device not responding, brd=%d panel=%d"
1422 "port=%d\n", portp->brdnr, portp->panelnr, portp->portnr);
1425 /*****************************************************************************/
1428 * Transmit interrupt handler. This has gotta be fast! Handling TX
1429 * chars is pretty simple, stuff as many as possible from the TX buffer
1430 * into the cd1400 FIFO. Must also handle TX breaks here, since they
1431 * are embedded as commands in the data stream. Oh no, had to use a goto!
1432 * This could be optimized more, will do when I get time...
1433 * In practice it is possible that interrupts are enabled but that the
1434 * port has been hung up. Need to handle not having any TX buffer here,
1435 * this is done by using the side effect that head and tail will also
1436 * be NULL if the buffer has been freed.
1439 static __inline void stl_txisr(stlpanel_t *panelp, int ioaddr)
1444 unsigned char ioack, srer;
1447 printf("stl_txisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
1450 ioack = inb(ioaddr + EREG_TXACK);
1451 if (((ioack & panelp->ackmask) != 0) ||
1452 ((ioack & ACK_TYPMASK) != ACK_TYPTX)) {
1453 printf("STALLION: bad TX interrupt ack value=%x\n", ioack);
1456 portp = panelp->ports[(ioack >> 3)];
1459 * Unfortunately we need to handle breaks in the data stream, since
1460 * this is the only way to generate them on the cd1400. Do it now if
1461 * a break is to be sent. Some special cases here: brklen is -1 then
1462 * start sending an un-timed break, if brklen is -2 then stop sending
1463 * an un-timed break, if brklen is -3 then we have just sent an
1464 * un-timed break and do not want any data to go out, if brklen is -4
1465 * then a break has just completed so clean up the port settings.
1467 if (portp->brklen != 0) {
1468 if (portp->brklen >= -1) {
1469 outb(ioaddr, (TDR + portp->uartaddr));
1470 outb((ioaddr + EREG_DATA), ETC_CMD);
1471 outb((ioaddr + EREG_DATA), ETC_STARTBREAK);
1472 if (portp->brklen > 0) {
1473 outb((ioaddr + EREG_DATA), ETC_CMD);
1474 outb((ioaddr + EREG_DATA), ETC_DELAY);
1475 outb((ioaddr + EREG_DATA), portp->brklen);
1476 outb((ioaddr + EREG_DATA), ETC_CMD);
1477 outb((ioaddr + EREG_DATA), ETC_STOPBREAK);
1482 } else if (portp->brklen == -2) {
1483 outb(ioaddr, (TDR + portp->uartaddr));
1484 outb((ioaddr + EREG_DATA), ETC_CMD);
1485 outb((ioaddr + EREG_DATA), ETC_STOPBREAK);
1487 } else if (portp->brklen == -3) {
1488 outb(ioaddr, (SRER + portp->uartaddr));
1489 srer = inb(ioaddr + EREG_DATA);
1490 srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
1491 outb((ioaddr + EREG_DATA), srer);
1493 outb(ioaddr, (COR2 + portp->uartaddr));
1494 outb((ioaddr + EREG_DATA),
1495 (inb(ioaddr + EREG_DATA) & ~COR2_ETC));
1501 head = portp->tx.head;
1502 tail = portp->tx.tail;
1503 len = (head >= tail) ? (head - tail) : (STL_TXBUFSIZE - (tail - head));
1504 if ((len == 0) || ((len < STL_TXBUFLOW) &&
1505 ((portp->state & ASY_TXLOW) == 0))) {
1506 portp->state |= ASY_TXLOW;
1511 outb(ioaddr, (SRER + portp->uartaddr));
1512 srer = inb(ioaddr + EREG_DATA);
1513 if (srer & SRER_TXDATA) {
1514 srer = (srer & ~SRER_TXDATA) | SRER_TXEMPTY;
1516 srer &= ~(SRER_TXDATA | SRER_TXEMPTY);
1517 portp->tty.t_state &= ~TS_BUSY;
1519 outb((ioaddr + EREG_DATA), srer);
1521 len = MIN(len, CD1400_TXFIFOSIZE);
1522 portp->stats.txtotal += len;
1523 stlen = MIN(len, (portp->tx.endbuf - tail));
1524 outb(ioaddr, (TDR + portp->uartaddr));
1525 outsb((ioaddr + EREG_DATA), tail, stlen);
1528 if (tail >= portp->tx.endbuf)
1529 tail = portp->tx.buf;
1531 outsb((ioaddr + EREG_DATA), tail, len);
1534 portp->tx.tail = tail;
1538 outb(ioaddr, (EOSRR + portp->uartaddr));
1539 outb((ioaddr + EREG_DATA), 0);
1542 /*****************************************************************************/
1545 * Receive character interrupt handler. Determine if we have good chars
1546 * or bad chars and then process appropriately. Good chars are easy
1547 * just shove the lot into the RX buffer and set all status bytes to 0.
1548 * If a bad RX char then process as required. This routine needs to be
1552 static __inline void stl_rxisr(stlpanel_t *panelp, int ioaddr)
1556 unsigned int ioack, len, buflen, stlen;
1557 unsigned char status;
1560 static char unwanted[CD1400_RXFIFOSIZE];
1563 printf("stl_rxisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
1566 ioack = inb(ioaddr + EREG_RXACK);
1567 if ((ioack & panelp->ackmask) != 0) {
1568 printf("STALLION: bad RX interrupt ack value=%x\n", ioack);
1571 portp = panelp->ports[(ioack >> 3)];
1575 * First up, caluclate how much room there is in the RX ring queue.
1576 * We also want to keep track of the longest possible copy length,
1577 * this has to allow for the wrapping of the ring queue.
1579 head = portp->rx.head;
1580 tail = portp->rx.tail;
1582 buflen = STL_RXBUFSIZE - (head - tail) - 1;
1583 stlen = portp->rx.endbuf - head;
1585 buflen = tail - head - 1;
1590 * Check if the input buffer is near full. If so then we should take
1591 * some flow control action... It is very easy to do hardware and
1592 * software flow control from here since we have the port selected on
1595 if (buflen <= (STL_RXBUFSIZE - STL_RXBUFHIGH)) {
1596 if (((portp->state & ASY_RTSFLOW) == 0) &&
1597 (portp->state & ASY_RTSFLOWMODE)) {
1598 portp->state |= ASY_RTSFLOW;
1599 stl_setreg(portp, MCOR1,
1600 (stl_getreg(portp, MCOR1) & 0xf0));
1601 stl_setreg(portp, MSVR2, 0);
1602 portp->stats.rxrtsoff++;
1607 * OK we are set, process good data... If the RX ring queue is full
1608 * just chuck the chars - don't leave them in the UART.
1610 if ((ioack & ACK_TYPMASK) == ACK_TYPRXGOOD) {
1611 outb(ioaddr, (RDCR + portp->uartaddr));
1612 len = inb(ioaddr + EREG_DATA);
1614 outb(ioaddr, (RDSR + portp->uartaddr));
1615 insb((ioaddr + EREG_DATA), &unwanted[0], len);
1616 portp->stats.rxlost += len;
1617 portp->stats.rxtotal += len;
1619 len = MIN(len, buflen);
1620 portp->stats.rxtotal += len;
1621 stlen = MIN(len, stlen);
1623 outb(ioaddr, (RDSR + portp->uartaddr));
1624 insb((ioaddr + EREG_DATA), head, stlen);
1626 if (head >= portp->rx.endbuf) {
1627 head = portp->rx.buf;
1629 insb((ioaddr + EREG_DATA), head, len);
1634 } else if ((ioack & ACK_TYPMASK) == ACK_TYPRXBAD) {
1635 outb(ioaddr, (RDSR + portp->uartaddr));
1636 status = inb(ioaddr + EREG_DATA);
1637 ch = inb(ioaddr + EREG_DATA);
1638 if (status & ST_BREAK)
1639 portp->stats.rxbreaks++;
1640 if (status & ST_FRAMING)
1641 portp->stats.rxframing++;
1642 if (status & ST_PARITY)
1643 portp->stats.rxparity++;
1644 if (status & ST_OVERRUN)
1645 portp->stats.rxoverrun++;
1646 if (status & ST_SCHARMASK) {
1647 if ((status & ST_SCHARMASK) == ST_SCHAR1)
1648 portp->stats.txxon++;
1649 if ((status & ST_SCHARMASK) == ST_SCHAR2)
1650 portp->stats.txxoff++;
1653 if ((portp->rxignoremsk & status) == 0) {
1654 if ((tp->t_state & TS_CAN_BYPASS_L_RINT) &&
1655 ((status & ST_FRAMING) ||
1656 ((status & ST_PARITY) && (tp->t_iflag & INPCK))))
1658 if ((portp->rxmarkmsk & status) == 0)
1660 *(head + STL_RXBUFSIZE) = status;
1662 if (head >= portp->rx.endbuf)
1663 head = portp->rx.buf;
1666 printf("STALLION: bad RX interrupt ack value=%x\n", ioack);
1670 portp->rx.head = head;
1671 portp->state |= ASY_RXDATA;
1675 outb(ioaddr, (EOSRR + portp->uartaddr));
1676 outb((ioaddr + EREG_DATA), 0);
1679 /*****************************************************************************/
1682 * Modem interrupt handler. The is called when the modem signal line
1683 * (DCD) has changed state. Leave most of the work to the off-level
1684 * processing routine.
1687 static __inline void stl_mdmisr(stlpanel_t *panelp, int ioaddr)
1694 printf("stl_mdmisr(panelp=%x,ioaddr=%x)\n", (int) panelp, ioaddr);
1697 ioack = inb(ioaddr + EREG_MDACK);
1698 if (((ioack & panelp->ackmask) != 0) ||
1699 ((ioack & ACK_TYPMASK) != ACK_TYPMDM)) {
1700 printf("STALLION: bad MODEM interrupt ack value=%x\n", ioack);
1703 portp = panelp->ports[(ioack >> 3)];
1705 outb(ioaddr, (MISR + portp->uartaddr));
1706 misr = inb(ioaddr + EREG_DATA);
1707 if (misr & MISR_DCD) {
1708 portp->state |= ASY_DCDCHANGE;
1709 portp->stats.modem++;
1713 outb(ioaddr, (EOSRR + portp->uartaddr));
1714 outb((ioaddr + EREG_DATA), 0);
1717 /*****************************************************************************/
1720 * Interrupt handler for EIO and ECH boards. This code ain't all that
1721 * pretty, but the idea is to make it as fast as possible. This code is
1722 * well suited to be assemblerized :-) We don't use the general purpose
1723 * register access functions here, for speed we will go strait to the
1727 static void stlintr(int unit)
1731 unsigned char svrtype;
1732 int i, panelnr, iobase;
1736 printf("stlintr(unit=%d)\n", unit);
1740 panelp = (stlpanel_t *) NULL;
1741 for (i = 0; (i < stl_nrbrds); ) {
1742 if ((brdp = stl_brds[i]) == (stlbrd_t *) NULL) {
1746 if (brdp->state == 0) {
1751 * The following section of code handles the subtle differences
1752 * between board types. It is sort of similar, but different
1753 * enough to handle each separately.
1755 if (brdp->brdtype == BRD_EASYIO) {
1756 if ((inb(brdp->iostatus) & EIO_INTRPEND) == 0) {
1760 panelp = brdp->panels[0];
1761 iobase = panelp->iobase;
1763 svrtype = inb(iobase + EREG_DATA);
1764 if (brdp->nrports > 4) {
1765 outb(iobase, (SVRR + 0x80));
1766 svrtype |= inb(iobase + EREG_DATA);
1768 } else if (brdp->brdtype == BRD_ECH) {
1769 if ((inb(brdp->iostatus) & ECH_INTRPEND) == 0) {
1773 outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDENABLE));
1774 for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
1775 panelp = brdp->panels[panelnr];
1776 iobase = panelp->iobase;
1777 if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1779 if (panelp->nrports > 8) {
1781 if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1785 if (panelnr >= brdp->nrpanels) {
1790 svrtype = inb(iobase + EREG_DATA);
1791 outb(iobase, (SVRR + 0x80));
1792 svrtype |= inb(iobase + EREG_DATA);
1793 } else if (brdp->brdtype == BRD_ECHPCI) {
1794 iobase = brdp->ioaddr2;
1795 for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
1796 panelp = brdp->panels[panelnr];
1797 outb(brdp->ioctrl, panelp->pagenr);
1798 if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1800 if (panelp->nrports > 8) {
1801 outb(brdp->ioctrl, (panelp->pagenr + 1));
1802 if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1806 if (panelnr >= brdp->nrpanels) {
1811 svrtype = inb(iobase + EREG_DATA);
1812 outb(iobase, (SVRR + 0x80));
1813 svrtype |= inb(iobase + EREG_DATA);
1814 } else if (brdp->brdtype == BRD_ECHMC) {
1815 if ((inb(brdp->iostatus) & ECH_INTRPEND) == 0) {
1819 for (panelnr = 0; (panelnr < brdp->nrpanels); panelnr++) {
1820 panelp = brdp->panels[panelnr];
1821 iobase = panelp->iobase;
1822 if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1824 if (panelp->nrports > 8) {
1826 if (inb(iobase + ECH_PNLSTATUS) & ECH_PNLINTRPEND)
1830 if (panelnr >= brdp->nrpanels) {
1835 svrtype = inb(iobase + EREG_DATA);
1836 outb(iobase, (SVRR + 0x80));
1837 svrtype |= inb(iobase + EREG_DATA);
1839 printf("STALLION: unknown board type=%x\n", brdp->brdtype);
1845 * We have determined what type of service is required for a
1846 * port. From here on in the service of a port is the same no
1847 * matter what the board type...
1849 if (svrtype & SVRR_RX)
1850 stl_rxisr(panelp, iobase);
1851 if (svrtype & SVRR_TX)
1852 stl_txisr(panelp, iobase);
1853 if (svrtype & SVRR_MDM)
1854 stl_mdmisr(panelp, iobase);
1856 if (brdp->brdtype == BRD_ECH)
1857 outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDDISABLE));
1861 /*****************************************************************************/
1865 static void stlpciintr(void *arg)
1872 /*****************************************************************************/
1875 * If we haven't scheduled a timeout then do it, some port needs high
1879 static void stl_dotimeout()
1882 printf("stl_dotimeout()\n");
1885 if (stl_doingtimeout == 0) {
1886 timeout(stl_poll, 0, 1);
1891 /*****************************************************************************/
1894 * Service "software" level processing. Too slow or painfull to be done
1895 * at real hardware interrupt time. This way we might also be able to
1896 * do some service on other waiting ports as well...
1899 static void stl_poll(void *arg)
1904 int brdnr, portnr, rearm, x;
1907 printf("stl_poll()\n");
1910 stl_doingtimeout = 0;
1914 for (brdnr = 0; (brdnr < stl_nrbrds); brdnr++) {
1915 if ((brdp = stl_brds[brdnr]) == (stlbrd_t *) NULL)
1917 for (portnr = 0; (portnr < brdp->nrports); portnr++) {
1918 if ((portp = brdp->ports[portnr]) == (stlport_t *) NULL)
1920 if ((portp->state & ASY_ACTIVE) == 0)
1924 if (portp->state & ASY_RXDATA)
1925 stl_rxprocess(portp);
1926 if (portp->state & ASY_DCDCHANGE) {
1927 portp->state &= ~ASY_DCDCHANGE;
1928 portp->sigs = stl_getsignals(portp);
1929 (*linesw[tp->t_line].l_modem)(tp,
1930 (portp->sigs & TIOCM_CD));
1932 if (portp->state & ASY_TXLOW) {
1933 portp->state &= ~ASY_TXLOW;
1934 (*linesw[tp->t_line].l_start)(tp);
1937 if (portp->state & ASY_ACTIVE)
1947 /*****************************************************************************/
1950 * Process the RX data that has been buffered up in the RX ring queue.
1953 static void stl_rxprocess(stlport_t *portp)
1956 unsigned int len, stlen, lostlen;
1962 printf("stl_rxprocess(portp=%x): brdnr=%d portnr=%d\n", (int) portp,
1963 portp->brdnr, portp->portnr);
1967 portp->state &= ~ASY_RXDATA;
1969 if ((tp->t_state & TS_ISOPEN) == 0) {
1970 stl_flush(portp, FREAD);
1975 * Calculate the amount of data in the RX ring queue. Also calculate
1976 * the largest single copy size...
1978 head = portp->rx.head;
1979 tail = portp->rx.tail;
1984 len = STL_RXBUFSIZE - (tail - head);
1985 stlen = portp->rx.endbuf - tail;
1988 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
1990 if (((tp->t_rawq.c_cc + len) >= TTYHOG) &&
1991 ((portp->state & ASY_RTSFLOWMODE) ||
1992 (tp->t_iflag & IXOFF)) &&
1993 ((tp->t_state & TS_TBLOCK) == 0)) {
1994 ch = TTYHOG - tp->t_rawq.c_cc - 1;
1995 len = (ch > 0) ? ch : 0;
1996 stlen = MIN(stlen, len);
1999 lostlen = b_to_q(tail, stlen, &tp->t_rawq);
2002 if (tail >= portp->rx.endbuf) {
2003 tail = portp->rx.buf;
2004 lostlen += b_to_q(tail, len, &tp->t_rawq);
2007 portp->stats.rxlost += lostlen;
2009 portp->rx.tail = tail;
2012 while (portp->rx.tail != head) {
2013 ch = (unsigned char) *(portp->rx.tail);
2014 status = *(portp->rx.tail + STL_RXBUFSIZE);
2016 *(portp->rx.tail + STL_RXBUFSIZE) = 0;
2017 if (status & ST_BREAK)
2019 if (status & ST_FRAMING)
2021 if (status & ST_PARITY)
2023 if (status & ST_OVERRUN)
2026 (*linesw[tp->t_line].l_rint)(ch, tp);
2027 if (portp->rx.tail == head)
2030 if (++(portp->rx.tail) >= portp->rx.endbuf)
2031 portp->rx.tail = portp->rx.buf;
2035 if (head != portp->rx.tail)
2036 portp->state |= ASY_RXDATA;
2039 * If we where flow controled then maybe the buffer is low enough that
2040 * we can re-activate it.
2042 if ((portp->state & ASY_RTSFLOW) && ((tp->t_state & TS_TBLOCK) == 0))
2043 stl_flowcontrol(portp, 1, -1);
2046 /*****************************************************************************/
2049 * Set up the cd1400 registers for a port based on the termios port
2053 static int stl_param(struct tty *tp, struct termios *tiosp)
2056 unsigned int clkdiv;
2057 unsigned char cor1, cor2, cor3;
2058 unsigned char cor4, cor5, ccr;
2059 unsigned char srer, sreron, sreroff;
2060 unsigned char mcor1, mcor2, rtpr;
2061 unsigned char clk, div;
2064 portp = (stlport_t *) tp;
2067 printf("stl_param(tp=%x,tiosp=%x): brdnr=%d portnr=%d\n", (int) tp,
2068 (int) tiosp, portp->brdnr, portp->portnr);
2086 * Set up the RX char ignore mask with those RX error types we
2087 * can ignore. We could have used some special modes of the cd1400
2088 * UART to help, but it is better this way because we can keep stats
2089 * on the number of each type of RX exception event.
2091 portp->rxignoremsk = 0;
2092 if (tiosp->c_iflag & IGNPAR)
2093 portp->rxignoremsk |= (ST_PARITY | ST_FRAMING | ST_OVERRUN);
2094 if (tiosp->c_iflag & IGNBRK)
2095 portp->rxignoremsk |= ST_BREAK;
2097 portp->rxmarkmsk = ST_OVERRUN;
2098 if (tiosp->c_iflag & (INPCK | PARMRK))
2099 portp->rxmarkmsk |= (ST_PARITY | ST_FRAMING);
2100 if (tiosp->c_iflag & BRKINT)
2101 portp->rxmarkmsk |= ST_BREAK;
2104 * Go through the char size, parity and stop bits and set all the
2105 * option registers appropriately.
2107 switch (tiosp->c_cflag & CSIZE) {
2122 if (tiosp->c_cflag & CSTOPB)
2127 if (tiosp->c_cflag & PARENB) {
2128 if (tiosp->c_cflag & PARODD)
2129 cor1 |= (COR1_PARENB | COR1_PARODD);
2131 cor1 |= (COR1_PARENB | COR1_PAREVEN);
2133 cor1 |= COR1_PARNONE;
2136 if (tiosp->c_iflag & ISTRIP)
2137 cor5 |= COR5_ISTRIP;
2140 * Set the RX FIFO threshold at 6 chars. This gives a bit of breathing
2141 * space for hardware flow control and the like. This should be set to
2142 * VMIN. Also here we will set the RX data timeout to 10ms - this should
2143 * really be based on VTIME...
2145 cor3 |= FIFO_RXTHRESHOLD;
2149 * Calculate the baud rate timers. For now we will just assume that
2150 * the input and output baud are the same. Could have used a baud
2151 * table here, but this way we can generate virtually any baud rate
2154 if (tiosp->c_ispeed == 0)
2155 tiosp->c_ispeed = tiosp->c_ospeed;
2156 if ((tiosp->c_ospeed < 0) || (tiosp->c_ospeed > STL_MAXBAUD))
2159 if (tiosp->c_ospeed > 0) {
2160 for (clk = 0; (clk < CD1400_NUMCLKS); clk++) {
2161 clkdiv = ((portp->clk / stl_cd1400clkdivs[clk]) /
2166 div = (unsigned char) clkdiv;
2170 * Check what form of modem signaling is required and set it up.
2172 if ((tiosp->c_cflag & CLOCAL) == 0) {
2175 sreron |= SRER_MODEM;
2179 * Setup cd1400 enhanced modes if we can. In particular we want to
2180 * handle as much of the flow control as possbile automatically. As
2181 * well as saving a few CPU cycles it will also greatly improve flow
2182 * control reliablilty.
2184 if (tiosp->c_iflag & IXON) {
2187 if (tiosp->c_iflag & IXANY)
2191 if (tiosp->c_cflag & CCTS_OFLOW)
2193 if (tiosp->c_cflag & CRTS_IFLOW)
2194 mcor1 |= FIFO_RTSTHRESHOLD;
2197 * All cd1400 register values calculated so go through and set them
2201 printf("SETPORT: portnr=%d panelnr=%d brdnr=%d\n", portp->portnr,
2202 portp->panelnr, portp->brdnr);
2203 printf(" cor1=%x cor2=%x cor3=%x cor4=%x cor5=%x\n", cor1, cor2,
2205 printf(" mcor1=%x mcor2=%x rtpr=%x sreron=%x sreroff=%x\n",
2206 mcor1, mcor2, rtpr, sreron, sreroff);
2207 printf(" tcor=%x tbpr=%x rcor=%x rbpr=%x\n", clk, div, clk, div);
2208 printf(" schr1=%x schr2=%x schr3=%x schr4=%x\n",
2209 tiosp->c_cc[VSTART], tiosp->c_cc[VSTOP], tiosp->c_cc[VSTART],
2210 tiosp->c_cc[VSTOP]);
2214 BRDENABLE(portp->brdnr, portp->pagenr);
2215 stl_setreg(portp, CAR, (portp->portnr & 0x3));
2216 srer = stl_getreg(portp, SRER);
2217 stl_setreg(portp, SRER, 0);
2218 ccr += stl_updatereg(portp, COR1, cor1);
2219 ccr += stl_updatereg(portp, COR2, cor2);
2220 ccr += stl_updatereg(portp, COR3, cor3);
2223 stl_setreg(portp, CCR, CCR_CORCHANGE);
2225 stl_setreg(portp, COR4, cor4);
2226 stl_setreg(portp, COR5, cor5);
2227 stl_setreg(portp, MCOR1, mcor1);
2228 stl_setreg(portp, MCOR2, mcor2);
2229 if (tiosp->c_ospeed == 0) {
2230 stl_setreg(portp, MSVR1, 0);
2232 stl_setreg(portp, MSVR1, MSVR1_DTR);
2233 stl_setreg(portp, TCOR, clk);
2234 stl_setreg(portp, TBPR, div);
2235 stl_setreg(portp, RCOR, clk);
2236 stl_setreg(portp, RBPR, div);
2238 stl_setreg(portp, SCHR1, tiosp->c_cc[VSTART]);
2239 stl_setreg(portp, SCHR2, tiosp->c_cc[VSTOP]);
2240 stl_setreg(portp, SCHR3, tiosp->c_cc[VSTART]);
2241 stl_setreg(portp, SCHR4, tiosp->c_cc[VSTOP]);
2242 stl_setreg(portp, RTPR, rtpr);
2243 mcor1 = stl_getreg(portp, MSVR1);
2244 if (mcor1 & MSVR1_DCD)
2245 portp->sigs |= TIOCM_CD;
2247 portp->sigs &= ~TIOCM_CD;
2248 stl_setreg(portp, SRER, ((srer & ~sreroff) | sreron));
2249 BRDDISABLE(portp->brdnr);
2250 portp->state &= ~(ASY_RTSFLOWMODE | ASY_CTSFLOWMODE);
2251 portp->state |= ((tiosp->c_cflag & CRTS_IFLOW) ? ASY_RTSFLOWMODE : 0);
2252 portp->state |= ((tiosp->c_cflag & CCTS_OFLOW) ? ASY_CTSFLOWMODE : 0);
2253 stl_ttyoptim(portp, tiosp);
2259 /*****************************************************************************/
2262 * Action the flow control as required. The hw and sw args inform the
2263 * routine what flow control methods it should try.
2266 static void stl_flowcontrol(stlport_t *portp, int hw, int sw)
2268 unsigned char *head, *tail;
2272 printf("stl_flowcontrol(portp=%x,hw=%d,sw=%d)\n", (int) portp, hw, sw);
2277 if (portp->state & ASY_RTSFLOWMODE) {
2279 if ((portp->state & ASY_RTSFLOW) == 0)
2281 } else if (hw > 0) {
2282 if (portp->state & ASY_RTSFLOW) {
2283 head = portp->rx.head;
2284 tail = portp->rx.tail;
2285 len = (head >= tail) ? (head - tail) :
2286 (STL_RXBUFSIZE - (tail - head));
2287 if (len < STL_RXBUFHIGH)
2294 * We have worked out what to do, if anything. So now apply it to the
2299 BRDENABLE(portp->brdnr, portp->pagenr);
2300 stl_setreg(portp, CAR, (portp->portnr & 0x03));
2302 portp->state |= ASY_RTSFLOW;
2303 stl_setreg(portp, MCOR1,
2304 (stl_getreg(portp, MCOR1) & 0xf0));
2305 stl_setreg(portp, MSVR2, 0);
2306 portp->stats.rxrtsoff++;
2307 } else if (hwflow > 0) {
2308 portp->state &= ~ASY_RTSFLOW;
2309 stl_setreg(portp, MSVR2, MSVR2_RTS);
2310 stl_setreg(portp, MCOR1,
2311 (stl_getreg(portp, MCOR1) | FIFO_RTSTHRESHOLD));
2312 portp->stats.rxrtson++;
2314 BRDDISABLE(portp->brdnr);
2320 /*****************************************************************************/
2323 * Set the state of the DTR and RTS signals.
2326 static void stl_setsignals(stlport_t *portp, int dtr, int rts)
2328 unsigned char msvr1, msvr2;
2332 printf("stl_setsignals(portp=%x,dtr=%d,rts=%d)\n", (int) portp,
2344 BRDENABLE(portp->brdnr, portp->pagenr);
2345 stl_setreg(portp, CAR, (portp->portnr & 0x03));
2347 stl_setreg(portp, MSVR2, msvr2);
2349 stl_setreg(portp, MSVR1, msvr1);
2350 BRDDISABLE(portp->brdnr);
2354 /*****************************************************************************/
2357 * Get the state of the signals.
2360 static int stl_getsignals(stlport_t *portp)
2362 unsigned char msvr1, msvr2;
2366 printf("stl_getsignals(portp=%x)\n", (int) portp);
2370 BRDENABLE(portp->brdnr, portp->pagenr);
2371 stl_setreg(portp, CAR, (portp->portnr & 0x3));
2372 msvr1 = stl_getreg(portp, MSVR1);
2373 msvr2 = stl_getreg(portp, MSVR2);
2374 BRDDISABLE(portp->brdnr);
2378 sigs |= (msvr1 & MSVR1_DCD) ? TIOCM_CD : 0;
2379 sigs |= (msvr1 & MSVR1_CTS) ? TIOCM_CTS : 0;
2380 sigs |= (msvr1 & MSVR1_RI) ? TIOCM_RI : 0;
2381 sigs |= (msvr1 & MSVR1_DSR) ? TIOCM_DSR : 0;
2382 sigs |= (msvr1 & MSVR1_DTR) ? TIOCM_DTR : 0;
2383 sigs |= (msvr2 & MSVR2_RTS) ? TIOCM_RTS : 0;
2387 /*****************************************************************************/
2390 * Enable or disable the Transmitter and/or Receiver.
2393 static void stl_enablerxtx(stlport_t *portp, int rx, int tx)
2399 printf("stl_enablerxtx(portp=%x,rx=%d,tx=%d)\n", (int) portp, rx, tx);
2404 ccr |= CCR_TXDISABLE;
2406 ccr |= CCR_TXENABLE;
2408 ccr |= CCR_RXDISABLE;
2410 ccr |= CCR_RXENABLE;
2413 BRDENABLE(portp->brdnr, portp->pagenr);
2414 stl_setreg(portp, CAR, (portp->portnr & 0x03));
2416 stl_setreg(portp, CCR, ccr);
2418 BRDDISABLE(portp->brdnr);
2422 /*****************************************************************************/
2425 * Start or stop the Transmitter and/or Receiver.
2428 static void stl_startrxtx(stlport_t *portp, int rx, int tx)
2430 unsigned char sreron, sreroff;
2434 printf("stl_startrxtx(portp=%x,rx=%d,tx=%d)\n", (int) portp, rx, tx);
2440 sreroff |= (SRER_TXDATA | SRER_TXEMPTY);
2442 sreron |= SRER_TXDATA;
2444 sreron |= SRER_TXEMPTY;
2446 sreroff |= SRER_RXDATA;
2448 sreron |= SRER_RXDATA;
2451 BRDENABLE(portp->brdnr, portp->pagenr);
2452 stl_setreg(portp, CAR, (portp->portnr & 0x3));
2453 stl_setreg(portp, SRER,
2454 ((stl_getreg(portp, SRER) & ~sreroff) | sreron));
2455 BRDDISABLE(portp->brdnr);
2457 portp->tty.t_state |= TS_BUSY;
2461 /*****************************************************************************/
2464 * Disable all interrupts from this port.
2467 static void stl_disableintrs(stlport_t *portp)
2472 printf("stl_disableintrs(portp=%x)\n", (int) portp);
2476 BRDENABLE(portp->brdnr, portp->pagenr);
2477 stl_setreg(portp, CAR, (portp->portnr & 0x3));
2478 stl_setreg(portp, SRER, 0);
2479 BRDDISABLE(portp->brdnr);
2483 /*****************************************************************************/
2485 static void stl_sendbreak(stlport_t *portp, long len)
2490 printf("stl_sendbreak(portp=%x,len=%d)\n", (int) portp, (int) len);
2494 BRDENABLE(portp->brdnr, portp->pagenr);
2495 stl_setreg(portp, CAR, (portp->portnr & 0x3));
2496 stl_setreg(portp, COR2, (stl_getreg(portp, COR2) | COR2_ETC));
2497 stl_setreg(portp, SRER,
2498 ((stl_getreg(portp, SRER) & ~SRER_TXDATA) | SRER_TXEMPTY));
2499 BRDDISABLE(portp->brdnr);
2502 portp->brklen = (len > 255) ? 255 : len;
2504 portp->brklen = len;
2507 portp->stats.txbreaks++;
2510 /*****************************************************************************/
2513 * Enable l_rint processing bypass mode if tty modes allow it.
2516 static void stl_ttyoptim(stlport_t *portp, struct termios *tiosp)
2521 if (((tiosp->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR)) == 0) &&
2522 (((tiosp->c_iflag & BRKINT) == 0) || (tiosp->c_iflag & IGNBRK)) &&
2523 (((tiosp->c_iflag & PARMRK) == 0) ||
2524 ((tiosp->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))) &&
2525 ((tiosp->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) ==0) &&
2526 (linesw[tp->t_line].l_rint == ttyinput))
2527 tp->t_state |= TS_CAN_BYPASS_L_RINT;
2529 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
2530 portp->hotchar = linesw[tp->t_line].l_hotchar;
2533 /*****************************************************************************/
2536 * Try and find and initialize all the ports on a panel. We don't care
2537 * what sort of board these ports are on - since the port io registers
2538 * are almost identical when dealing with ports.
2541 static int stl_initports(stlbrd_t *brdp, stlpanel_t *panelp)
2544 unsigned int chipmask;
2546 int nrchips, uartaddr, ioaddr;
2550 printf("stl_initports(panelp=%x)\n", (int) panelp);
2553 BRDENABLE(panelp->brdnr, panelp->pagenr);
2556 * Check that each chip is present and started up OK.
2559 nrchips = panelp->nrports / CD1400_PORTS;
2560 for (i = 0; (i < nrchips); i++) {
2561 if (brdp->brdtype == BRD_ECHPCI) {
2562 outb(brdp->ioctrl, (panelp->pagenr + (i >> 1)));
2563 ioaddr = panelp->iobase;
2565 ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 1));
2567 uartaddr = (i & 0x01) ? 0x080 : 0;
2568 outb(ioaddr, (GFRCR + uartaddr));
2569 outb((ioaddr + EREG_DATA), 0);
2570 outb(ioaddr, (CCR + uartaddr));
2571 outb((ioaddr + EREG_DATA), CCR_RESETFULL);
2572 outb((ioaddr + EREG_DATA), CCR_RESETFULL);
2573 outb(ioaddr, (GFRCR + uartaddr));
2574 for (j = 0; (j < CCR_MAXWAIT); j++) {
2575 gfrcr = inb(ioaddr + EREG_DATA);
2576 if ((gfrcr > 0x40) && (gfrcr < 0x60))
2579 if (j >= CCR_MAXWAIT) {
2580 printf("STALLION: cd1400 not responding, brd=%d "
2581 "panel=%d chip=%d\n", panelp->brdnr,
2582 panelp->panelnr, i);
2585 chipmask |= (0x1 << i);
2586 outb(ioaddr, (PPR + uartaddr));
2587 outb((ioaddr + EREG_DATA), PPR_SCALAR);
2591 * All cd1400's are initialized (if found!). Now go through and setup
2592 * each ports data structures. Also init the LIVR register of cd1400
2595 ioaddr = panelp->iobase;
2596 for (i = 0; (i < panelp->nrports); i++) {
2597 if (brdp->brdtype == BRD_ECHPCI) {
2598 outb(brdp->ioctrl, (panelp->pagenr + (i >> 3)));
2599 ioaddr = panelp->iobase;
2601 ioaddr = panelp->iobase + (EREG_BANKSIZE * (i >> 3));
2603 if ((chipmask & (0x1 << (i / 4))) == 0)
2605 portp = (stlport_t *) malloc(sizeof(stlport_t), M_TTYS,
2607 if (portp == (stlport_t *) NULL) {
2608 printf("STALLION: failed to allocate port memory "
2609 "(size=%d)\n", sizeof(stlport_t));
2614 portp->brdnr = panelp->brdnr;
2615 portp->panelnr = panelp->panelnr;
2616 portp->clk = brdp->clk;
2617 portp->ioaddr = ioaddr;
2618 portp->uartaddr = (i & 0x4) << 5;
2619 portp->pagenr = panelp->pagenr + (i >> 3);
2620 portp->hwid = stl_getreg(portp, GFRCR);
2621 stl_setreg(portp, CAR, (i & 0x3));
2622 stl_setreg(portp, LIVR, (i << 3));
2623 panelp->ports[i] = portp;
2625 j = STL_TXBUFSIZE + (2 * STL_RXBUFSIZE);
2626 portp->tx.buf = (char *) malloc(j, M_TTYS, M_NOWAIT);
2627 if (portp->tx.buf == (char *) NULL) {
2628 printf("STALLION: failed to allocate buffer memory "
2632 portp->tx.endbuf = portp->tx.buf + STL_TXBUFSIZE;
2633 portp->tx.head = portp->tx.buf;
2634 portp->tx.tail = portp->tx.buf;
2635 portp->rx.buf = portp->tx.buf + STL_TXBUFSIZE;
2636 portp->rx.endbuf = portp->rx.buf + STL_RXBUFSIZE;
2637 portp->rx.head = portp->rx.buf;
2638 portp->rx.tail = portp->rx.buf;
2639 portp->rxstatus.buf = portp->rx.buf + STL_RXBUFSIZE;
2640 portp->rxstatus.endbuf = portp->rxstatus.buf + STL_RXBUFSIZE;
2641 portp->rxstatus.head = portp->rxstatus.buf;
2642 portp->rxstatus.tail = portp->rxstatus.buf;
2643 bzero(portp->rxstatus.head, STL_RXBUFSIZE);
2645 portp->initintios.c_ispeed = STL_DEFSPEED;
2646 portp->initintios.c_ospeed = STL_DEFSPEED;
2647 portp->initintios.c_cflag = STL_DEFCFLAG;
2648 portp->initintios.c_iflag = 0;
2649 portp->initintios.c_oflag = 0;
2650 portp->initintios.c_lflag = 0;
2651 bcopy(&ttydefchars[0], &portp->initintios.c_cc[0],
2652 sizeof(portp->initintios.c_cc));
2653 portp->initouttios = portp->initintios;
2654 portp->dtrwait = 3 * hz;
2657 BRDDISABLE(panelp->brdnr);
2661 /*****************************************************************************/
2664 * Try to find and initialize an EasyIO board.
2667 static int stl_initeio(stlbrd_t *brdp)
2670 unsigned int status;
2673 printf("stl_initeio(brdp=%x)\n", (int) brdp);
2676 brdp->ioctrl = brdp->ioaddr1 + 1;
2677 brdp->iostatus = brdp->ioaddr1 + 2;
2678 brdp->clk = EIO_CLK;
2680 status = inb(brdp->iostatus);
2681 switch (status & EIO_IDBITMASK) {
2683 brdp->clk = EIO_CLK8M;
2697 * Check that the supplied IRQ is good and then use it to setup the
2698 * programmable interrupt bits on EIO board. Also set the edge/level
2699 * triggered interrupt bit.
2701 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2702 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2703 printf("STALLION: invalid irq=%d for brd=%d\n", brdp->irq,
2707 outb(brdp->ioctrl, (stl_vecmap[brdp->irq] |
2708 ((brdp->irqtype) ? EIO_INTLEVEL : EIO_INTEDGE)));
2710 panelp = (stlpanel_t *) malloc(sizeof(stlpanel_t), M_TTYS,
2712 if (panelp == (stlpanel_t *) NULL) {
2713 printf("STALLION: failed to allocate memory (size=%d)\n",
2714 sizeof(stlpanel_t));
2718 panelp->brdnr = brdp->brdnr;
2719 panelp->panelnr = 0;
2720 panelp->nrports = brdp->nrports;
2721 panelp->iobase = brdp->ioaddr1;
2722 panelp->hwid = status;
2723 brdp->panels[0] = panelp;
2725 brdp->hwid = status;
2726 brdp->state |= BRD_FOUND;
2730 /*****************************************************************************/
2733 * Try to find an ECH board and initialize it. This code is capable of
2734 * dealing with all types of ECH board.
2737 static int stl_initech(stlbrd_t *brdp)
2740 unsigned int status, nxtid;
2741 int panelnr, ioaddr, i;
2744 printf("stl_initech(brdp=%x)\n", (int) brdp);
2748 * Set up the initial board register contents for boards. This varys a
2749 * bit between the different board types. So we need to handle each
2750 * separately. Also do a check that the supplied IRQ is good.
2752 if (brdp->brdtype == BRD_ECH) {
2753 brdp->ioctrl = brdp->ioaddr1 + 1;
2754 brdp->iostatus = brdp->ioaddr1 + 1;
2755 status = inb(brdp->iostatus);
2756 if ((status & ECH_IDBITMASK) != ECH_ID)
2758 brdp->hwid = status;
2760 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2761 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2762 printf("STALLION: invalid irq=%d for brd=%d\n",
2763 brdp->irq, brdp->brdnr);
2766 status = ((brdp->ioaddr2 & ECH_ADDR2MASK) >> 1);
2767 status |= (stl_vecmap[brdp->irq] << 1);
2768 outb(brdp->ioaddr1, (status | ECH_BRDRESET));
2769 brdp->ioctrlval = ECH_INTENABLE |
2770 ((brdp->irqtype) ? ECH_INTLEVEL : ECH_INTEDGE);
2771 outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDENABLE));
2772 outb(brdp->ioaddr1, status);
2773 } else if (brdp->brdtype == BRD_ECHMC) {
2774 brdp->ioctrl = brdp->ioaddr1 + 0x20;
2775 brdp->iostatus = brdp->ioctrl;
2776 status = inb(brdp->iostatus);
2777 if ((status & ECH_IDBITMASK) != ECH_ID)
2779 brdp->hwid = status;
2781 if ((brdp->irq < 0) || (brdp->irq > 15) ||
2782 (stl_vecmap[brdp->irq] == (unsigned char) 0xff)) {
2783 printf("STALLION: invalid irq=%d for brd=%d\n",
2784 brdp->irq, brdp->brdnr);
2787 outb(brdp->ioctrl, ECHMC_BRDRESET);
2788 outb(brdp->ioctrl, ECHMC_INTENABLE);
2789 } else if (brdp->brdtype == BRD_ECHPCI) {
2790 brdp->ioctrl = brdp->ioaddr1 + 2;
2793 brdp->clk = ECH_CLK;
2796 * Scan through the secondary io address space looking for panels.
2797 * As we find'em allocate and initialize panel structures for each.
2799 ioaddr = brdp->ioaddr2;
2803 for (i = 0; (i < STL_MAXPANELS); i++) {
2804 if (brdp->brdtype == BRD_ECHPCI) {
2805 outb(brdp->ioctrl, nxtid);
2806 ioaddr = brdp->ioaddr2;
2808 status = inb(ioaddr + ECH_PNLSTATUS);
2809 if ((status & ECH_PNLIDMASK) != nxtid)
2811 panelp = (stlpanel_t *) malloc(sizeof(stlpanel_t), M_TTYS,
2813 if (panelp == (stlpanel_t *) NULL) {
2814 printf("STALLION: failed to allocate memory"
2815 "(size=%d)\n", sizeof(stlpanel_t));
2818 panelp->brdnr = brdp->brdnr;
2819 panelp->panelnr = panelnr;
2820 panelp->iobase = ioaddr;
2821 panelp->pagenr = nxtid;
2822 panelp->hwid = status;
2823 if (status & ECH_PNL16PORT) {
2824 if ((brdp->nrports + 16) > 32)
2826 panelp->nrports = 16;
2827 panelp->ackmask = 0x80;
2828 brdp->nrports += 16;
2829 ioaddr += (EREG_BANKSIZE * 2);
2832 panelp->nrports = 8;
2833 panelp->ackmask = 0xc0;
2835 ioaddr += EREG_BANKSIZE;
2838 brdp->panels[panelnr++] = panelp;
2840 if (ioaddr >= (brdp->ioaddr2 + 0x20))
2844 if (brdp->brdtype == BRD_ECH)
2845 outb(brdp->ioctrl, (brdp->ioctrlval | ECH_BRDDISABLE));
2847 brdp->state |= BRD_FOUND;
2851 /*****************************************************************************/
2854 * Initialize and configure the specified board. This firstly probes
2855 * for the board, if it is found then the board is initialized and
2856 * then all its ports are initialized as well.
2859 static int stl_brdinit(stlbrd_t *brdp)
2865 printf("stl_brdinit(brdp=%x): unit=%d type=%d io1=%x io2=%x irq=%d\n",
2866 (int) brdp, brdp->brdnr, brdp->brdtype, brdp->ioaddr1,
2867 brdp->ioaddr2, brdp->irq);
2870 switch (brdp->brdtype) {
2880 printf("STALLION: unit=%d is unknown board type=%d\n",
2881 brdp->brdnr, brdp->brdtype);
2885 stl_brds[brdp->brdnr] = brdp;
2886 if ((brdp->state & BRD_FOUND) == 0) {
2888 printf("STALLION: %s board not found, unit=%d io=%x irq=%d\n",
2889 stl_brdnames[brdp->brdtype], brdp->brdnr,
2890 brdp->ioaddr1, brdp->irq);
2895 for (i = 0, k = 0; (i < STL_MAXPANELS); i++) {
2896 panelp = brdp->panels[i];
2897 if (panelp != (stlpanel_t *) NULL) {
2898 stl_initports(brdp, panelp);
2899 for (j = 0; (j < panelp->nrports); j++)
2900 brdp->ports[k++] = panelp->ports[j];
2904 printf("stl%d: %s (driver version %s) unit=%d nrpanels=%d nrports=%d\n",
2905 brdp->unitid, stl_brdnames[brdp->brdtype], stl_drvversion,
2906 brdp->brdnr, brdp->nrpanels, brdp->nrports);
2910 /*****************************************************************************/
2913 * Return the board stats structure to user app.
2916 static int stl_getbrdstats(caddr_t data)
2922 stl_brdstats = *((combrd_t *) data);
2923 if (stl_brdstats.brd >= STL_MAXBRDS)
2925 brdp = stl_brds[stl_brdstats.brd];
2926 if (brdp == (stlbrd_t *) NULL)
2929 bzero(&stl_brdstats, sizeof(combrd_t));
2930 stl_brdstats.brd = brdp->brdnr;
2931 stl_brdstats.type = brdp->brdtype;
2932 stl_brdstats.hwid = brdp->hwid;
2933 stl_brdstats.state = brdp->state;
2934 stl_brdstats.ioaddr = brdp->ioaddr1;
2935 stl_brdstats.ioaddr2 = brdp->ioaddr2;
2936 stl_brdstats.irq = brdp->irq;
2937 stl_brdstats.nrpanels = brdp->nrpanels;
2938 stl_brdstats.nrports = brdp->nrports;
2939 for (i = 0; (i < brdp->nrpanels); i++) {
2940 panelp = brdp->panels[i];
2941 stl_brdstats.panels[i].panel = i;
2942 stl_brdstats.panels[i].hwid = panelp->hwid;
2943 stl_brdstats.panels[i].nrports = panelp->nrports;
2946 *((combrd_t *) data) = stl_brdstats;;
2950 /*****************************************************************************/
2953 * Resolve the referenced port number into a port struct pointer.
2956 static stlport_t *stl_getport(int brdnr, int panelnr, int portnr)
2961 if ((brdnr < 0) || (brdnr >= STL_MAXBRDS))
2962 return((stlport_t *) NULL);
2963 brdp = stl_brds[brdnr];
2964 if (brdp == (stlbrd_t *) NULL)
2965 return((stlport_t *) NULL);
2966 if ((panelnr < 0) || (panelnr >= brdp->nrpanels))
2967 return((stlport_t *) NULL);
2968 panelp = brdp->panels[panelnr];
2969 if (panelp == (stlpanel_t *) NULL)
2970 return((stlport_t *) NULL);
2971 if ((portnr < 0) || (portnr >= panelp->nrports))
2972 return((stlport_t *) NULL);
2973 return(panelp->ports[portnr]);
2976 /*****************************************************************************/
2979 * Return the port stats structure to user app. A NULL port struct
2980 * pointer passed in means that we need to find out from the app
2981 * what port to get stats for (used through board control device).
2984 static int stl_getportstats(stlport_t *portp, caddr_t data)
2986 unsigned char *head, *tail;
2988 if (portp == (stlport_t *) NULL) {
2989 stl_comstats = *((comstats_t *) data);
2990 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
2992 if (portp == (stlport_t *) NULL)
2996 portp->stats.state = portp->state;
2997 /*portp->stats.flags = portp->flags;*/
2998 portp->stats.hwid = portp->hwid;
2999 portp->stats.ttystate = portp->tty.t_state;
3000 portp->stats.cflags = portp->tty.t_cflag;
3001 portp->stats.iflags = portp->tty.t_iflag;
3002 portp->stats.oflags = portp->tty.t_oflag;
3003 portp->stats.lflags = portp->tty.t_lflag;
3005 head = portp->tx.head;
3006 tail = portp->tx.tail;
3007 portp->stats.txbuffered = ((head >= tail) ? (head - tail) :
3008 (STL_TXBUFSIZE - (tail - head)));
3010 head = portp->rx.head;
3011 tail = portp->rx.tail;
3012 portp->stats.rxbuffered = (head >= tail) ? (head - tail) :
3013 (STL_RXBUFSIZE - (tail - head));
3015 portp->stats.signals = (unsigned long) stl_getsignals(portp);
3017 *((comstats_t *) data) = portp->stats;
3021 /*****************************************************************************/
3024 * Clear the port stats structure. We also return it zeroed out...
3027 static int stl_clrportstats(stlport_t *portp, caddr_t data)
3029 if (portp == (stlport_t *) NULL) {
3030 stl_comstats = *((comstats_t *) data);
3031 portp = stl_getport(stl_comstats.brd, stl_comstats.panel,
3033 if (portp == (stlport_t *) NULL)
3037 bzero(&portp->stats, sizeof(comstats_t));
3038 portp->stats.brd = portp->brdnr;
3039 portp->stats.panel = portp->panelnr;
3040 portp->stats.port = portp->portnr;
3041 *((comstats_t *) data) = stl_comstats;
3045 /*****************************************************************************/
3048 * The "staliomem" device is used for stats collection in this driver.
3051 static int stl_memioctl(dev_t dev, unsigned long cmd, caddr_t data, int flag,
3058 printf("stl_memioctl(dev=%s,cmd=%lx,data=%p,flag=%x)\n",
3059 devtoname(dev), cmd, (void *) data, flag);
3062 brdnr = minor(dev) & 0x7;
3063 brdp = stl_brds[brdnr];
3064 if (brdp == (stlbrd_t *) NULL)
3066 if (brdp->state == 0)
3072 case COM_GETPORTSTATS:
3073 rc = stl_getportstats((stlport_t *) NULL, data);
3075 case COM_CLRPORTSTATS:
3076 rc = stl_clrportstats((stlport_t *) NULL, data);
3078 case COM_GETBRDSTATS:
3079 rc = stl_getbrdstats(data);
3089 /*****************************************************************************/