sio.c revision 82696
151078Speter/*- 251078Speter * Copyright (c) 1991 The Regents of the University of California. 351078Speter * All rights reserved. 451078Speter * 551078Speter * Redistribution and use in source and binary forms, with or without 651078Speter * modification, are permitted provided that the following conditions 751078Speter * are met: 851078Speter * 1. Redistributions of source code must retain the above copyright 951078Speter * notice, this list of conditions and the following disclaimer. 1051078Speter * 2. Redistributions in binary form must reproduce the above copyright 1151078Speter * notice, this list of conditions and the following disclaimer in the 1251078Speter * documentation and/or other materials provided with the distribution. 1351078Speter * 3. All advertising materials mentioning features or use of this software 1451078Speter * must display the following acknowledgement: 1551078Speter * This product includes software developed by the University of 1651078Speter * California, Berkeley and its contributors. 1751078Speter * 4. Neither the name of the University nor the names of its contributors 1851078Speter * may be used to endorse or promote products derived from this software 1951078Speter * without specific prior written permission. 2051078Speter * 2151078Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2251078Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2351078Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2451078Speter * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2551078Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2651078Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2751078Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2851078Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2951078Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3051078Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3151078Speter * SUCH DAMAGE. 3251078Speter * 3351078Speter * $FreeBSD: head/sys/dev/sio/sio.c 82696 2001-08-31 21:51:00Z murray $ 3451078Speter * from: @(#)com.c 7.5 (Berkeley) 5/16/91 3551078Speter * from: i386/isa sio.c,v 1.234 3651078Speter */ 3751078Speter 3851078Speter#include "opt_comconsole.h" 3951078Speter#include "opt_compat.h" 4051078Speter#include "opt_ddb.h" 4151078Speter#include "opt_sio.h" 4254386Simp#include "card.h" 4358885Simp#include "pci.h" 4451078Speter 4551078Speter/* 4651078Speter * Serial driver, based on 386BSD-0.1 com driver. 4751078Speter * Mostly rewritten to use pseudo-DMA. 4851078Speter * Works for National Semiconductor NS8250-NS16550AF UARTs. 4951078Speter * COM driver, based on HP dca driver. 5051078Speter * 5151078Speter * Changes for PC-Card integration: 5251078Speter * - Added PC-Card driver table and handlers 5351078Speter */ 5451078Speter#include <sys/param.h> 5576166Smarkm#include <sys/systm.h> 5665822Sjhb#include <sys/bus.h> 5776166Smarkm#include <sys/bus.h> 5851078Speter#include <sys/conf.h> 5951078Speter#include <sys/dkstat.h> 6051078Speter#include <sys/fcntl.h> 6151078Speter#include <sys/interrupt.h> 6251078Speter#include <sys/kernel.h> 6376166Smarkm#include <sys/lock.h> 6476166Smarkm#include <sys/malloc.h> 6576166Smarkm#include <sys/module.h> 6676166Smarkm#include <sys/mutex.h> 6776166Smarkm#include <sys/proc.h> 6876166Smarkm#include <sys/reboot.h> 6976166Smarkm#include <sys/sysctl.h> 7051078Speter#include <sys/syslog.h> 7176166Smarkm#include <sys/tty.h> 7260471Snyan#include <machine/bus_pio.h> 7351078Speter#include <machine/bus.h> 7451078Speter#include <sys/rman.h> 7558377Sphk#include <sys/timetc.h> 7651078Speter#include <sys/timepps.h> 7751078Speter 7851078Speter#include <isa/isareg.h> 7951078Speter#include <isa/isavar.h> 8058885Simp#if NPCI > 0 8158885Simp#include <pci/pcireg.h> 8258885Simp#include <pci/pcivar.h> 8358885Simp#endif 8451078Speter 8551078Speter#include <machine/clock.h> 8651078Speter#include <machine/resource.h> 8751078Speter 8851078Speter#include <isa/sioreg.h> 8951078Speter 9051078Speter#ifdef COM_ESP 9177726Sjoerg#include <dev/ic/esp.h> 9251078Speter#endif 9377726Sjoerg#include <dev/ic/ns16550.h> 9451078Speter 9551078Speter#define LOTS_OF_EVENTS 64 /* helps separate urgent events from input */ 9651078Speter 9751078Speter#define CALLOUT_MASK 0x80 9851078Speter#define CONTROL_MASK 0x60 9951078Speter#define CONTROL_INIT_STATE 0x20 10051078Speter#define CONTROL_LOCK_STATE 0x40 10151078Speter#define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev))) 10251078Speter#define MINOR_MAGIC_MASK (CALLOUT_MASK | CONTROL_MASK) 10351078Speter#define MINOR_TO_UNIT(mynor) ((mynor) & ~MINOR_MAGIC_MASK) 10451078Speter 10551078Speter#ifdef COM_MULTIPORT 10651078Speter/* checks in flags for multiport and which is multiport "master chip" 10751078Speter * for a given card 10851078Speter */ 10951078Speter#define COM_ISMULTIPORT(flags) ((flags) & 0x01) 11051078Speter#define COM_MPMASTER(flags) (((flags) >> 8) & 0x0ff) 11151078Speter#define COM_NOTAST4(flags) ((flags) & 0x04) 11251078Speter#endif /* COM_MULTIPORT */ 11351078Speter 11451078Speter#define COM_CONSOLE(flags) ((flags) & 0x10) 11551078Speter#define COM_FORCECONSOLE(flags) ((flags) & 0x20) 11651078Speter#define COM_LLCONSOLE(flags) ((flags) & 0x40) 11751078Speter#define COM_DEBUGGER(flags) ((flags) & 0x80) 11851078Speter#define COM_LOSESOUTINTS(flags) ((flags) & 0x08) 11951078Speter#define COM_NOFIFO(flags) ((flags) & 0x02) 12051078Speter#define COM_ST16650A(flags) ((flags) & 0x20000) 12151078Speter#define COM_C_NOPROBE (0x40000) 12251078Speter#define COM_NOPROBE(flags) ((flags) & COM_C_NOPROBE) 12351078Speter#define COM_C_IIR_TXRDYBUG (0x80000) 12451078Speter#define COM_IIR_TXRDYBUG(flags) ((flags) & COM_C_IIR_TXRDYBUG) 12551078Speter#define COM_FIFOSIZE(flags) (((flags) & 0xff000000) >> 24) 12651078Speter 12751078Speter#define com_scr 7 /* scratch register for 16450-16550 (R/W) */ 12851078Speter 12960471Snyan#define sio_getreg(com, off) \ 13060471Snyan (bus_space_read_1((com)->bst, (com)->bsh, (off))) 13160471Snyan#define sio_setreg(com, off, value) \ 13260471Snyan (bus_space_write_1((com)->bst, (com)->bsh, (off), (value))) 13360471Snyan 13451078Speter/* 13551078Speter * com state bits. 13651078Speter * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher 13751078Speter * than the other bits so that they can be tested as a group without masking 13851078Speter * off the low bits. 13951078Speter * 14051078Speter * The following com and tty flags correspond closely: 14151078Speter * CS_BUSY = TS_BUSY (maintained by comstart(), siopoll() and 14253344Speter * comstop()) 14351078Speter * CS_TTGO = ~TS_TTSTOP (maintained by comparam() and comstart()) 14451078Speter * CS_CTS_OFLOW = CCTS_OFLOW (maintained by comparam()) 14551078Speter * CS_RTS_IFLOW = CRTS_IFLOW (maintained by comparam()) 14651078Speter * TS_FLUSH is not used. 14751078Speter * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON. 14851078Speter * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state). 14951078Speter */ 15051078Speter#define CS_BUSY 0x80 /* output in progress */ 15151078Speter#define CS_TTGO 0x40 /* output not stopped by XOFF */ 15251078Speter#define CS_ODEVREADY 0x20 /* external device h/w ready (CTS) */ 15351078Speter#define CS_CHECKMSR 1 /* check of MSR scheduled */ 15451078Speter#define CS_CTS_OFLOW 2 /* use CTS output flow control */ 15551078Speter#define CS_DTR_OFF 0x10 /* DTR held off */ 15651078Speter#define CS_ODONE 4 /* output completed */ 15751078Speter#define CS_RTS_IFLOW 8 /* use RTS input flow control */ 15851078Speter#define CSE_BUSYCHECK 1 /* siobusycheck() scheduled */ 15951078Speter 16051078Speterstatic char const * const error_desc[] = { 16151078Speter#define CE_OVERRUN 0 16251078Speter "silo overflow", 16351078Speter#define CE_INTERRUPT_BUF_OVERFLOW 1 16451078Speter "interrupt-level buffer overflow", 16551078Speter#define CE_TTY_BUF_OVERFLOW 2 16651078Speter "tty-level buffer overflow", 16751078Speter}; 16851078Speter 16951078Speter#define CE_NTYPES 3 17051078Speter#define CE_RECORD(com, errnum) (++(com)->delta_error_counts[errnum]) 17151078Speter 17251078Speter/* types. XXX - should be elsewhere */ 17351078Spetertypedef u_int Port_t; /* hardware port */ 17451078Spetertypedef u_char bool_t; /* boolean */ 17551078Speter 17651078Speter/* queue of linear buffers */ 17751078Speterstruct lbq { 17851078Speter u_char *l_head; /* next char to process */ 17951078Speter u_char *l_tail; /* one past the last char to process */ 18051078Speter struct lbq *l_next; /* next in queue */ 18151078Speter bool_t l_queued; /* nonzero if queued */ 18251078Speter}; 18351078Speter 18451078Speter/* com device structure */ 18551078Speterstruct com_s { 18651078Speter u_int flags; /* Copy isa device flags */ 18751078Speter u_char state; /* miscellaneous flag bits */ 18851078Speter bool_t active_out; /* nonzero if the callout device is open */ 18951078Speter u_char cfcr_image; /* copy of value written to CFCR */ 19051078Speter#ifdef COM_ESP 19151078Speter bool_t esp; /* is this unit a hayes esp board? */ 19251078Speter#endif 19351078Speter u_char extra_state; /* more flag bits, separate for order trick */ 19451078Speter u_char fifo_image; /* copy of value written to FIFO */ 19551078Speter bool_t hasfifo; /* nonzero for 16550 UARTs */ 19651078Speter bool_t st16650a; /* Is a Startech 16650A or RTS/CTS compat */ 19751078Speter bool_t loses_outints; /* nonzero if device loses output interrupts */ 19851078Speter u_char mcr_image; /* copy of value written to MCR */ 19951078Speter#ifdef COM_MULTIPORT 20051078Speter bool_t multiport; /* is this unit part of a multiport device? */ 20151078Speter#endif /* COM_MULTIPORT */ 20251078Speter bool_t no_irq; /* nonzero if irq is not attached */ 20351078Speter bool_t gone; /* hardware disappeared */ 20451078Speter bool_t poll; /* nonzero if polling is required */ 20551078Speter bool_t poll_output; /* nonzero if polling for output is required */ 20651078Speter int unit; /* unit number */ 20751078Speter int dtr_wait; /* time to hold DTR down on close (* 1/hz) */ 20851078Speter u_int tx_fifo_size; 20951078Speter u_int wopeners; /* # processes waiting for DCD in open() */ 21051078Speter 21151078Speter /* 21251078Speter * The high level of the driver never reads status registers directly 21351078Speter * because there would be too many side effects to handle conveniently. 21451078Speter * Instead, it reads copies of the registers stored here by the 21551078Speter * interrupt handler. 21651078Speter */ 21751078Speter u_char last_modem_status; /* last MSR read by intr handler */ 21851078Speter u_char prev_modem_status; /* last MSR handled by high level */ 21951078Speter 22051078Speter u_char hotchar; /* ldisc-specific char to be handled ASAP */ 22151078Speter u_char *ibuf; /* start of input buffer */ 22251078Speter u_char *ibufend; /* end of input buffer */ 22351078Speter u_char *ibufold; /* old input buffer, to be freed */ 22451078Speter u_char *ihighwater; /* threshold in input buffer */ 22551078Speter u_char *iptr; /* next free spot in input buffer */ 22651078Speter int ibufsize; /* size of ibuf (not include error bytes) */ 22751078Speter int ierroff; /* offset of error bytes in ibuf */ 22851078Speter 22951078Speter struct lbq obufq; /* head of queue of output buffers */ 23051078Speter struct lbq obufs[2]; /* output buffers */ 23151078Speter 23260471Snyan bus_space_tag_t bst; 23360471Snyan bus_space_handle_t bsh; 23460471Snyan 23551078Speter Port_t data_port; /* i/o ports */ 23651078Speter#ifdef COM_ESP 23751078Speter Port_t esp_port; 23851078Speter#endif 23951078Speter Port_t int_id_port; 24051078Speter Port_t modem_ctl_port; 24151078Speter Port_t line_status_port; 24251078Speter Port_t modem_status_port; 24351078Speter Port_t intr_ctl_port; /* Ports of IIR register */ 24451078Speter 24551078Speter struct tty *tp; /* cross reference */ 24651078Speter 24751078Speter /* Initial state. */ 24851078Speter struct termios it_in; /* should be in struct tty */ 24951078Speter struct termios it_out; 25051078Speter 25151078Speter /* Lock state. */ 25251078Speter struct termios lt_in; /* should be in struct tty */ 25351078Speter struct termios lt_out; 25451078Speter 25551078Speter bool_t do_timestamp; 25651078Speter bool_t do_dcd_timestamp; 25751078Speter struct timeval timestamp; 25851078Speter struct timeval dcd_timestamp; 25951078Speter struct pps_state pps; 26051078Speter 26151078Speter u_long bytes_in; /* statistics */ 26251078Speter u_long bytes_out; 26351078Speter u_int delta_error_counts[CE_NTYPES]; 26451078Speter u_long error_counts[CE_NTYPES]; 26551078Speter 26651078Speter struct resource *irqres; 26751078Speter struct resource *ioportres; 26854386Simp void *cookie; 26965131Sphk dev_t devs[6]; 27051078Speter 27151078Speter /* 27251078Speter * Data area for output buffers. Someday we should build the output 27351078Speter * buffer queue without copying data. 27451078Speter */ 27551078Speter u_char obuf1[256]; 27651078Speter u_char obuf2[256]; 27751078Speter}; 27851078Speter 27951078Speter#ifdef COM_ESP 28051078Speterstatic int espattach __P((struct com_s *com, Port_t esp_port)); 28151078Speter#endif 28258885Simpstatic int sioattach __P((device_t dev, int rid)); 28352471Simpstatic int sio_isa_attach __P((device_t dev)); 28451078Speter 28551078Speterstatic timeout_t siobusycheck; 28651078Speterstatic timeout_t siodtrwakeup; 28751078Speterstatic void comhardclose __P((struct com_s *com)); 28851078Speterstatic void sioinput __P((struct com_s *com)); 28951078Speterstatic void siointr1 __P((struct com_s *com)); 29051078Speterstatic void siointr __P((void *arg)); 29151078Speterstatic int commctl __P((struct com_s *com, int bits, int how)); 29251078Speterstatic int comparam __P((struct tty *tp, struct termios *t)); 29367551Sjhbstatic void siopoll __P((void *)); 29458885Simpstatic int sioprobe __P((device_t dev, int xrid)); 29552471Simpstatic int sio_isa_probe __P((device_t dev)); 29651078Speterstatic void siosettimeout __P((void)); 29765605Sjhbstatic int siosetwater __P((struct com_s *com, speed_t speed)); 29851078Speterstatic void comstart __P((struct tty *tp)); 29951654Sphkstatic void comstop __P((struct tty *tp, int rw)); 30051078Speterstatic timeout_t comwakeup; 30151078Speterstatic void disc_optim __P((struct tty *tp, struct termios *t, 30251078Speter struct com_s *com)); 30351078Speter 30452471Simp#if NCARD > 0 30552471Simpstatic int sio_pccard_attach __P((device_t dev)); 30654386Simpstatic int sio_pccard_detach __P((device_t dev)); 30752471Simpstatic int sio_pccard_probe __P((device_t dev)); 30852471Simp#endif /* NCARD > 0 */ 30951078Speter 31058885Simp#if NPCI > 0 31158885Simpstatic int sio_pci_attach __P((device_t dev)); 31258885Simpstatic void sio_pci_kludge_unit __P((device_t dev)); 31358885Simpstatic int sio_pci_probe __P((device_t dev)); 31458885Simp#endif /* NPCI > 0 */ 31558885Simp 31670174Sjhbstatic char driver_name[] = "sio"; 31770174Sjhbstatic struct mtx sio_lock; 31870174Sjhbstatic int sio_inited; 31951078Speter 32051078Speter/* table and macro for fast conversion from a unit number to its com struct */ 32151078Speterstatic devclass_t sio_devclass; 32251078Speter#define com_addr(unit) ((struct com_s *) \ 32351078Speter devclass_get_softc(sio_devclass, unit)) 32451078Speter 32552471Simpstatic device_method_t sio_isa_methods[] = { 32651078Speter /* Device interface */ 32752471Simp DEVMETHOD(device_probe, sio_isa_probe), 32852471Simp DEVMETHOD(device_attach, sio_isa_attach), 32951078Speter 33051078Speter { 0, 0 } 33151078Speter}; 33251078Speter 33352471Simpstatic driver_t sio_isa_driver = { 33451078Speter driver_name, 33552471Simp sio_isa_methods, 33651078Speter sizeof(struct com_s), 33751078Speter}; 33851078Speter 33952471Simp#if NCARD > 0 34052471Simpstatic device_method_t sio_pccard_methods[] = { 34152471Simp /* Device interface */ 34252471Simp DEVMETHOD(device_probe, sio_pccard_probe), 34352471Simp DEVMETHOD(device_attach, sio_pccard_attach), 34452471Simp DEVMETHOD(device_detach, sio_pccard_detach), 34552471Simp 34652471Simp { 0, 0 } 34752471Simp}; 34852471Simp 34952471Simpstatic driver_t sio_pccard_driver = { 35052471Simp driver_name, 35152471Simp sio_pccard_methods, 35252471Simp sizeof(struct com_s), 35352471Simp}; 35458885Simp#endif /* NCARD > 0 */ 35552471Simp 35658885Simp#if NPCI > 0 35758885Simpstatic device_method_t sio_pci_methods[] = { 35858885Simp /* Device interface */ 35958885Simp DEVMETHOD(device_probe, sio_pci_probe), 36058885Simp DEVMETHOD(device_attach, sio_pci_attach), 36158885Simp 36258885Simp { 0, 0 } 36358885Simp}; 36458885Simp 36558885Simpstatic driver_t sio_pci_driver = { 36658885Simp driver_name, 36758885Simp sio_pci_methods, 36858885Simp sizeof(struct com_s), 36958885Simp}; 37058885Simp#endif /* NPCI > 0 */ 37158885Simp 37251078Speterstatic d_open_t sioopen; 37351078Speterstatic d_close_t sioclose; 37451078Speterstatic d_read_t sioread; 37551078Speterstatic d_write_t siowrite; 37651078Speterstatic d_ioctl_t sioioctl; 37751078Speter 37851078Speter#define CDEV_MAJOR 28 37951078Speterstatic struct cdevsw sio_cdevsw = { 38051078Speter /* open */ sioopen, 38151078Speter /* close */ sioclose, 38251078Speter /* read */ sioread, 38351078Speter /* write */ siowrite, 38451078Speter /* ioctl */ sioioctl, 38551654Sphk /* poll */ ttypoll, 38651078Speter /* mmap */ nommap, 38751078Speter /* strategy */ nostrategy, 38851078Speter /* name */ driver_name, 38951078Speter /* maj */ CDEV_MAJOR, 39051078Speter /* dump */ nodump, 39151078Speter /* psize */ nopsize, 39272521Sjlemon /* flags */ D_TTY | D_KQFILTER, 39372521Sjlemon /* kqfilter */ ttykqfilter, 39451078Speter}; 39551078Speter 39651078Speterint comconsole = -1; 39751078Speterstatic volatile speed_t comdefaultrate = CONSPEED; 39851078Speter#ifdef __alpha__ 39951078Speterstatic volatile speed_t gdbdefaultrate = CONSPEED; 40051078Speter#endif 40151078Speterstatic u_int com_events; /* input chars + weighted output completions */ 40251078Speterstatic Port_t siocniobase; 40366230Sjhb#ifndef __alpha__ 40451078Speterstatic int siocnunit; 40566230Sjhb#endif 40651078Speterstatic Port_t siogdbiobase; 40751078Speterstatic int siogdbunit = -1; 40872238Sjhbstatic void *sio_slow_ih; 40972238Sjhbstatic void *sio_fast_ih; 41051078Speterstatic int sio_timeout; 41151078Speterstatic int sio_timeouts_until_log; 41251078Speterstatic struct callout_handle sio_timeout_handle 41351078Speter = CALLOUT_HANDLE_INITIALIZER(&sio_timeout_handle); 41453344Speterstatic int sio_numunits; 41551078Speter 41651078Speterstatic struct speedtab comspeedtab[] = { 41751078Speter { 0, 0 }, 41851078Speter { 50, COMBRD(50) }, 41951078Speter { 75, COMBRD(75) }, 42051078Speter { 110, COMBRD(110) }, 42151078Speter { 134, COMBRD(134) }, 42251078Speter { 150, COMBRD(150) }, 42351078Speter { 200, COMBRD(200) }, 42451078Speter { 300, COMBRD(300) }, 42551078Speter { 600, COMBRD(600) }, 42651078Speter { 1200, COMBRD(1200) }, 42751078Speter { 1800, COMBRD(1800) }, 42851078Speter { 2400, COMBRD(2400) }, 42951078Speter { 4800, COMBRD(4800) }, 43051078Speter { 9600, COMBRD(9600) }, 43151078Speter { 19200, COMBRD(19200) }, 43251078Speter { 38400, COMBRD(38400) }, 43351078Speter { 57600, COMBRD(57600) }, 43451078Speter { 115200, COMBRD(115200) }, 43551078Speter { -1, -1 } 43651078Speter}; 43751078Speter 43851078Speter#ifdef COM_ESP 43951078Speter/* XXX configure this properly. */ 44051078Speterstatic Port_t likely_com_ports[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, }; 44151078Speterstatic Port_t likely_esp_ports[] = { 0x140, 0x180, 0x280, 0 }; 44251078Speter#endif 44351078Speter 44451078Speter/* 44551078Speter * handle sysctl read/write requests for console speed 44651078Speter * 44751078Speter * In addition to setting comdefaultrate for I/O through /dev/console, 44851078Speter * also set the initial and lock values for the /dev/ttyXX device 44951078Speter * if there is one associated with the console. Finally, if the /dev/tty 45051078Speter * device has already been open, change the speed on the open running port 45151078Speter * itself. 45251078Speter */ 45351078Speter 45451078Speterstatic int 45562573Sphksysctl_machdep_comdefaultrate(SYSCTL_HANDLER_ARGS) 45651078Speter{ 45751078Speter int error, s; 45851078Speter speed_t newspeed; 45951078Speter struct com_s *com; 46051078Speter struct tty *tp; 46151078Speter 46251078Speter newspeed = comdefaultrate; 46351078Speter 46451078Speter error = sysctl_handle_opaque(oidp, &newspeed, sizeof newspeed, req); 46551078Speter if (error || !req->newptr) 46651078Speter return (error); 46751078Speter 46851078Speter comdefaultrate = newspeed; 46951078Speter 47051078Speter if (comconsole < 0) /* serial console not selected? */ 47151078Speter return (0); 47251078Speter 47351078Speter com = com_addr(comconsole); 47457915Simp if (com == NULL) 47551078Speter return (ENXIO); 47651078Speter 47751078Speter /* 47851078Speter * set the initial and lock rates for /dev/ttydXX and /dev/cuaXX 47951078Speter * (note, the lock rates really are boolean -- if non-zero, disallow 48051078Speter * speed changes) 48151078Speter */ 48251078Speter com->it_in.c_ispeed = com->it_in.c_ospeed = 48351078Speter com->lt_in.c_ispeed = com->lt_in.c_ospeed = 48451078Speter com->it_out.c_ispeed = com->it_out.c_ospeed = 48551078Speter com->lt_out.c_ispeed = com->lt_out.c_ospeed = comdefaultrate; 48651078Speter 48751078Speter /* 48851078Speter * if we're open, change the running rate too 48951078Speter */ 49051078Speter tp = com->tp; 49151078Speter if (tp && (tp->t_state & TS_ISOPEN)) { 49251078Speter tp->t_termios.c_ispeed = 49351078Speter tp->t_termios.c_ospeed = comdefaultrate; 49451078Speter s = spltty(); 49551078Speter error = comparam(tp, &tp->t_termios); 49651078Speter splx(s); 49751078Speter } 49851078Speter return error; 49951078Speter} 50051078Speter 50151078SpeterSYSCTL_PROC(_machdep, OID_AUTO, conspeed, CTLTYPE_INT | CTLFLAG_RW, 50251078Speter 0, 0, sysctl_machdep_comdefaultrate, "I", ""); 50351078Speter 50453344Speter#define SET_FLAG(dev, bit) device_set_flags(dev, device_get_flags(dev) | (bit)) 50553344Speter#define CLR_FLAG(dev, bit) device_set_flags(dev, device_get_flags(dev) & ~(bit)) 50653344Speter 50751078Speter#if NCARD > 0 50852471Simpstatic int 50952471Simpsio_pccard_probe(dev) 51052471Simp device_t dev; 51151078Speter{ 51254386Simp /* Do not probe IRQ - pccard doesn't turn on the interrupt line */ 51354386Simp /* until bus_setup_intr */ 51453344Speter SET_FLAG(dev, COM_C_NOPROBE); 51553344Speter 51658885Simp return (sioprobe(dev, 0)); 51752471Simp} 51851078Speter 51952471Simpstatic int 52052471Simpsio_pccard_attach(dev) 52152471Simp device_t dev; 52252471Simp{ 52358885Simp return (sioattach(dev, 0)); 52451078Speter} 52551078Speter 52651078Speter/* 52752471Simp * sio_detach - unload the driver and clear the table. 52851078Speter * XXX TODO: 52951078Speter * This is usually called when the card is ejected, but 53051078Speter * can be caused by a modunload of a controller driver. 53151078Speter * The idea is to reset the driver's view of the device 53251078Speter * and ensure that any driver entry points such as 53351078Speter * read and write do not hang. 53451078Speter */ 53553978Simpstatic int 53652471Simpsio_pccard_detach(dev) 53752471Simp device_t dev; 53851078Speter{ 53951078Speter struct com_s *com; 54065131Sphk int i; 54151078Speter 54252471Simp com = (struct com_s *) device_get_softc(dev); 54357915Simp if (com == NULL) { 54452471Simp device_printf(dev, "NULL com in siounload\n"); 54554386Simp return (0); 54651078Speter } 54754386Simp com->gone = 1; 54865131Sphk for (i = 0 ; i < 6; i++) 54965131Sphk destroy_dev(com->devs[i]); 55054386Simp if (com->irqres) { 55154386Simp bus_teardown_intr(dev, com->irqres, com->cookie); 55254386Simp bus_release_resource(dev, SYS_RES_IRQ, 0, com->irqres); 55354386Simp } 55454386Simp if (com->ioportres) 55554386Simp bus_release_resource(dev, SYS_RES_IOPORT, 0, com->ioportres); 55651078Speter if (com->tp && (com->tp->t_state & TS_ISOPEN)) { 55757915Simp device_printf(dev, "still open, forcing close\n"); 55877750Simp (*linesw[com->tp->t_line].l_close)(com->tp, 0); 55951078Speter com->tp->t_gen++; 56051078Speter ttyclose(com->tp); 56151078Speter ttwakeup(com->tp); 56251078Speter ttwwakeup(com->tp); 56351078Speter } else { 56451078Speter if (com->ibuf != NULL) 56551078Speter free(com->ibuf, M_DEVBUF); 56651078Speter } 56757915Simp device_printf(dev, "unloaded\n"); 56853978Simp return (0); 56951078Speter} 57051078Speter#endif /* NCARD > 0 */ 57151078Speter 57258885Simp#if NPCI > 0 57358885Simpstruct pci_ids { 57458885Simp u_int32_t type; 57558885Simp const char *desc; 57658885Simp int rid; 57758885Simp}; 57851078Speter 57958885Simpstatic struct pci_ids pci_ids[] = { 58058885Simp { 0x100812b9, "3COM PCI FaxModem", 0x10 }, 58177750Simp { 0x048011c1, "Lucent kermit based PCI Modem", 0x14 }, 58276869Ssanpei { 0x0000151f, "SmartLink 5634PCV SurfRider", 0x10 }, 58382682Smurray { 0x01101407, "Koutech IOFLEX-2S PCI Dual Port Serial", 0x10 }, 58482682Smurray { 0x01111407, "Koutech IOFLEX-2S PCI Dual Port Serial", 0x10 }, 58577750Simp /* { 0xXXXXXXXX, "Xircom Cardbus modem", 0x10 }, */ 58658885Simp { 0x00000000, NULL, 0 } 58758885Simp}; 58858885Simp 58958885Simpstatic int 59058885Simpsio_pci_attach(dev) 59158885Simp device_t dev; 59258885Simp{ 59358885Simp u_int32_t type; 59458885Simp struct pci_ids *id; 59558885Simp 59658885Simp type = pci_get_devid(dev); 59758885Simp id = pci_ids; 59858885Simp while (id->type && id->type != type) 59958885Simp id++; 60058885Simp if (id->desc == NULL) 60158885Simp return (ENXIO); 60258885Simp sio_pci_kludge_unit(dev); 60358885Simp return (sioattach(dev, id->rid)); 60458885Simp} 60558885Simp 60658885Simp/* 60758885Simp * Don't cut and paste this to other drivers. It is a horrible kludge 60858885Simp * which will fail to work and also be unnecessary in future versions. 60958885Simp */ 61058885Simpstatic void 61158885Simpsio_pci_kludge_unit(dev) 61258885Simp device_t dev; 61358885Simp{ 61458885Simp devclass_t dc; 61558885Simp int err; 61658885Simp int start; 61758885Simp int unit; 61858885Simp 61958885Simp unit = 0; 62058885Simp start = 0; 62158885Simp while (resource_int_value("sio", unit, "port", &start) == 0 && 62258885Simp start > 0) 62358885Simp unit++; 62458885Simp if (device_get_unit(dev) < unit) { 62558885Simp dc = device_get_devclass(dev); 62658885Simp while (devclass_get_device(dc, unit)) 62758885Simp unit++; 62858885Simp device_printf(dev, "moving to sio%d\n", unit); 62958885Simp err = device_set_unit(dev, unit); /* EVIL DO NOT COPY */ 63058885Simp if (err) 63158885Simp device_printf(dev, "error moving device %d\n", err); 63258885Simp } 63358885Simp} 63458885Simp 63558885Simpstatic int 63658885Simpsio_pci_probe(dev) 63758885Simp device_t dev; 63858885Simp{ 63958885Simp u_int32_t type; 64058885Simp struct pci_ids *id; 64158885Simp 64258885Simp type = pci_get_devid(dev); 64358885Simp id = pci_ids; 64458885Simp while (id->type && id->type != type) 64558885Simp id++; 64658885Simp if (id->desc == NULL) 64758885Simp return (ENXIO); 64858885Simp device_set_desc(dev, id->desc); 64958885Simp return (sioprobe(dev, id->rid)); 65058885Simp} 65158885Simp#endif /* NPCI > 0 */ 65258885Simp 65351078Speterstatic struct isa_pnp_id sio_ids[] = { 65451078Speter {0x0005d041, "Standard PC COM port"}, /* PNP0500 */ 65551078Speter {0x0105d041, "16550A-compatible COM port"}, /* PNP0501 */ 65651078Speter {0x0205d041, "Multiport serial device (non-intelligent 16550)"}, /* PNP0502 */ 65751078Speter {0x1005d041, "Generic IRDA-compatible device"}, /* PNP0510 */ 65851078Speter {0x1105d041, "Generic IRDA-compatible device"}, /* PNP0511 */ 65954944Speter /* Devices that do not have a compatid */ 66060696Stanimura {0x12206804, NULL}, /* ACH2012 - 5634BTS 56K Video Ready Modem */ 66154944Speter {0x7602a904, NULL}, /* AEI0276 - 56K v.90 Fax Modem (LKT) */ 66254944Speter {0x00007905, NULL}, /* AKY0000 - 56K Plug&Play Modem */ 66354944Speter {0x01405407, NULL}, /* AZT4001 - AZT3000 PnP SOUND DEVICE, MODEM */ 66454944Speter {0x56039008, NULL}, /* BDP0356 - Best Data 56x2 */ 66582696Smurray {0x56159008, NULL}, /* BDP1556 - B.D. Smart One 56SPS,Voice Modem*/ 66654944Speter {0x36339008, NULL}, /* BDP3336 - Best Data Prods. 336F */ 66754944Speter {0x0014490a, NULL}, /* BRI1400 - Boca 33.6 PnP */ 66854944Speter {0x0015490a, NULL}, /* BRI1500 - Internal Fax Data */ 66954944Speter {0x0034490a, NULL}, /* BRI3400 - Internal ACF Modem */ 67065438Ssheldonh {0x0094490a, NULL}, /* BRI9400 - Boca K56Flex PnP */ 67154944Speter {0x00b4490a, NULL}, /* BRIB400 - Boca 56k PnP */ 67254944Speter {0x0030320d, NULL}, /* CIR3000 - Cirrus Logic V43 */ 67356229Speter {0x0100440e, NULL}, /* CRD0001 - Cardinal MVP288IV ? */ 67460674Stanimura {0x36033610, NULL}, /* DAV0336 - DAVICOM 336PNP MODEM */ 67559888Stanimura {0x0000aa1a, NULL}, /* FUJ0000 - FUJITSU Modem 33600 PNP/I2 */ 67654944Speter {0x1200c31e, NULL}, /* GVC0012 - VF1128HV-R9 (win modem?) */ 67754944Speter {0x0303c31e, NULL}, /* GVC0303 - MaxTech 33.6 PnP D/F/V */ 67855124Speter {0x0505c31e, NULL}, /* GVC0505 - GVC 56k Faxmodem */ 67973961Ssanpei {0x0116c31e, NULL}, /* GVC1601 - Rockwell V.34 Plug & Play Modem */ 68054944Speter {0x0050c31e, NULL}, /* GVC5000 - some GVC modem */ 68154944Speter {0x3800f91e, NULL}, /* GWY0038 - Telepath with v.90 */ 68254944Speter {0x9062f91e, NULL}, /* GWY6290 - Telepath with x2 Technology */ 68373960Ssanpei {0x8100e425, NULL}, /* IOD0081 - I-O DATA DEVICE,INC. IFML-560 */ 68466920Stanimura {0x21002534, NULL}, /* MAE0021 - Jetstream Int V.90 56k Voice Series 2*/ 68554944Speter {0x0000f435, NULL}, /* MOT0000 - Motorola ModemSURFR 33.6 Intern */ 68654944Speter {0x5015f435, NULL}, /* MOT1550 - Motorola ModemSURFR 56K Modem */ 68754944Speter {0xf015f435, NULL}, /* MOT15F0 - Motorola VoiceSURFR 56K Modem */ 68854944Speter {0x6045f435, NULL}, /* MOT4560 - Motorola ? */ 68954944Speter {0x61e7a338, NULL}, /* NECE761 - 33.6Modem */ 69070356Stanimura {0x08804f3f, NULL}, /* OZO8008 - Zoom (33.6k Modem) */ 69160674Stanimura {0x0f804f3f, NULL}, /* OZO800f - Zoom 2812 (56k Modem) */ 69254944Speter {0x39804f3f, NULL}, /* OZO8039 - Zoom 56k flex */ 69377222Ssanpei {0x00914f3f, NULL}, /* OZO9100 - Zoom 2919 (K56 Faxmodem) */ 69457769Speter {0x3024a341, NULL}, /* PMC2430 - Pace 56 Voice Internal Modem */ 69554944Speter {0x1000eb49, NULL}, /* ROK0010 - Rockwell ? */ 69682685Smurray {0x1200b23d, NULL}, /* RSS0012 - OMRON ME5614ISA */ 69754944Speter {0x5002734a, NULL}, /* RSS0250 - 5614Jx3(G) Internal Modem */ 69860708Stanimura {0x6202734a, NULL}, /* RSS0262 - 5614Jx3[G] V90+K56Flex Modem */ 69954944Speter {0xc100ad4d, NULL}, /* SMM00C1 - Leopard 56k PnP */ 70056229Speter {0x9012b04e, NULL}, /* SUP1290 - Supra ? */ 70154944Speter {0x1013b04e, NULL}, /* SUP1310 - SupraExpress 336i PnP */ 70254944Speter {0x8013b04e, NULL}, /* SUP1380 - SupraExpress 288i PnP Voice */ 70354944Speter {0x8113b04e, NULL}, /* SUP1381 - SupraExpress 336i PnP Voice */ 70454944Speter {0x5016b04e, NULL}, /* SUP1650 - Supra 336i Sp Intl */ 70560947Stanimura {0x7016b04e, NULL}, /* SUP1670 - Supra 336i V+ Intl */ 70654944Speter {0x7420b04e, NULL}, /* SUP2070 - Supra ? */ 70754944Speter {0x8020b04e, NULL}, /* SUP2080 - Supra ? */ 70854944Speter {0x8420b04e, NULL}, /* SUP2084 - SupraExpress 56i PnP */ 70956229Speter {0x7121b04e, NULL}, /* SUP2171 - SupraExpress 56i Sp? */ 71054944Speter {0x8024b04e, NULL}, /* SUP2480 - Supra ? */ 71154944Speter {0x01007256, NULL}, /* USR0001 - U.S. Robotics Inc., Sportster W */ 71254944Speter {0x02007256, NULL}, /* USR0002 - U.S. Robotics Inc. Sportster 33. */ 71354944Speter {0x04007256, NULL}, /* USR0004 - USR Sportster 14.4k */ 71454944Speter {0x06007256, NULL}, /* USR0006 - USR Sportster 33.6k */ 71556229Speter {0x11007256, NULL}, /* USR0011 - USR ? */ 71654944Speter {0x01017256, NULL}, /* USR0101 - USR ? */ 71754944Speter {0x30207256, NULL}, /* USR2030 - U.S.Robotics Inc. Sportster 560 */ 71854944Speter {0x50207256, NULL}, /* USR2050 - U.S.Robotics Inc. Sportster 33. */ 71954944Speter {0x70207256, NULL}, /* USR2070 - U.S.Robotics Inc. Sportster 560 */ 72054944Speter {0x30307256, NULL}, /* USR3030 - U.S. Robotics 56K FAX INT */ 72154944Speter {0x31307256, NULL}, /* USR3031 - U.S. Robotics 56K FAX INT */ 72258848Speter {0x50307256, NULL}, /* USR3050 - U.S. Robotics 56K FAX INT */ 72354944Speter {0x70307256, NULL}, /* USR3070 - U.S. Robotics 56K Voice INT */ 72456229Speter {0x90307256, NULL}, /* USR3090 - USR ? */ 72564688Speter {0x70917256, NULL}, /* USR9170 - U.S. Robotics 56K FAX INT */ 72654944Speter {0x90917256, NULL}, /* USR9190 - USR 56k Voice INT */ 72754944Speter {0x0300695c, NULL}, /* WCI0003 - Fax/Voice/Modem/Speakphone/Asvd */ 72865469Ssheldonh {0x01a0896a, NULL}, /* ZTIA001 - Zoom Internal V90 Faxmodem */ 72954944Speter {0x61f7896a, NULL}, /* ZTIF761 - Zoom ComStar 33.6 */ 73051078Speter {0} 73151078Speter}; 73251078Speter 73356229Speter 73456229Speter 73551078Speterstatic int 73652471Simpsio_isa_probe(dev) 73752471Simp device_t dev; 73852471Simp{ 73952471Simp /* Check isapnp ids */ 74052471Simp if (ISA_PNP_PROBE(device_get_parent(dev), dev, sio_ids) == ENXIO) 74152471Simp return (ENXIO); 74258885Simp return (sioprobe(dev, 0)); 74352471Simp} 74452471Simp 74552471Simpstatic int 74658885Simpsioprobe(dev, xrid) 74751078Speter device_t dev; 74858885Simp int xrid; 74951078Speter{ 75053344Speter#if 0 75151078Speter static bool_t already_init; 75253344Speter device_t xdev; 75353344Speter#endif 75460471Snyan struct com_s *com; 75551078Speter bool_t failures[10]; 75651078Speter int fn; 75751078Speter device_t idev; 75851078Speter Port_t iobase; 75951078Speter intrmask_t irqmap[4]; 76051078Speter intrmask_t irqs; 76151078Speter u_char mcr_image; 76251078Speter int result; 76354206Speter u_long xirq; 76451088Speter u_int flags = device_get_flags(dev); 76551078Speter int rid; 76651078Speter struct resource *port; 76751078Speter 76858885Simp rid = xrid; 76951078Speter port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 77051078Speter 0, ~0, IO_COMSIZE, RF_ACTIVE); 77151078Speter if (!port) 77257915Simp return (ENXIO); 77351078Speter 77460471Snyan com = device_get_softc(dev); 77560471Snyan com->bst = rman_get_bustag(port); 77660471Snyan com->bsh = rman_get_bushandle(port); 77760471Snyan 77870174Sjhb if (atomic_cmpset_int(&sio_inited, 0, 1)) 77972238Sjhb mtx_init(&sio_lock, driver_name, MTX_SPIN); 78070174Sjhb 78153344Speter#if 0 78253344Speter /* 78353344Speter * XXX this is broken - when we are first called, there are no 78453344Speter * previously configured IO ports. We could hard code 78553344Speter * 0x3f8, 0x2f8, 0x3e8, 0x2e8 etc but that's probably worse. 78653344Speter * This code has been doing nothing since the conversion since 78753344Speter * "count" is zero the first time around. 78853344Speter */ 78951078Speter if (!already_init) { 79051078Speter /* 79151078Speter * Turn off MCR_IENABLE for all likely serial ports. An unused 79251078Speter * port with its MCR_IENABLE gate open will inhibit interrupts 79351078Speter * from any used port that shares the interrupt vector. 79451078Speter * XXX the gate enable is elsewhere for some multiports. 79551078Speter */ 79651078Speter device_t *devs; 79753344Speter int count, i, xioport; 79851078Speter 79951078Speter devclass_get_devices(sio_devclass, &devs, &count); 80051078Speter for (i = 0; i < count; i++) { 80151078Speter xdev = devs[i]; 80254194Speter if (device_is_enabled(xdev) && 80354194Speter bus_get_resource(xdev, SYS_RES_IOPORT, 0, &xioport, 80454194Speter NULL) == 0) 80553344Speter outb(xioport + com_mcr, 0); 80651078Speter } 80751078Speter free(devs, M_TEMP); 80851078Speter already_init = TRUE; 80951078Speter } 81053344Speter#endif 81151078Speter 81251078Speter if (COM_LLCONSOLE(flags)) { 81351078Speter printf("sio%d: reserved for low-level i/o\n", 81451078Speter device_get_unit(dev)); 81556788Sbde bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 81651078Speter return (ENXIO); 81751078Speter } 81851078Speter 81951078Speter /* 82051078Speter * If the device is on a multiport card and has an AST/4 82151078Speter * compatible interrupt control register, initialize this 82251078Speter * register and prepare to leave MCR_IENABLE clear in the mcr. 82351078Speter * Otherwise, prepare to set MCR_IENABLE in the mcr. 82451078Speter * Point idev to the device struct giving the correct id_irq. 82551078Speter * This is the struct for the master device if there is one. 82651078Speter */ 82751078Speter idev = dev; 82851078Speter mcr_image = MCR_IENABLE; 82951078Speter#ifdef COM_MULTIPORT 83057234Sbde if (COM_ISMULTIPORT(flags)) { 83154206Speter Port_t xiobase; 83254206Speter u_long io; 83354206Speter 83451078Speter idev = devclass_get_device(sio_devclass, COM_MPMASTER(flags)); 83551078Speter if (idev == NULL) { 83651078Speter printf("sio%d: master device %d not configured\n", 83751078Speter device_get_unit(dev), COM_MPMASTER(flags)); 83851078Speter idev = dev; 83951078Speter } 84057234Sbde if (!COM_NOTAST4(flags)) { 84157234Sbde if (bus_get_resource(idev, SYS_RES_IOPORT, 0, &io, 84257234Sbde NULL) == 0) { 84357234Sbde xiobase = io; 84457234Sbde if (bus_get_resource(idev, SYS_RES_IRQ, 0, 84557234Sbde NULL, NULL) == 0) 84657234Sbde outb(xiobase + com_scr, 0x80); 84757234Sbde else 84857234Sbde outb(xiobase + com_scr, 0); 84957234Sbde } 85057234Sbde mcr_image = 0; 85151078Speter } 85251078Speter } 85351078Speter#endif /* COM_MULTIPORT */ 85454194Speter if (bus_get_resource(idev, SYS_RES_IRQ, 0, NULL, NULL) != 0) 85551078Speter mcr_image = 0; 85651078Speter 85751078Speter bzero(failures, sizeof failures); 85851078Speter iobase = rman_get_start(port); 85951078Speter 86051078Speter /* 86151078Speter * We don't want to get actual interrupts, just masked ones. 86251078Speter * Interrupts from this line should already be masked in the ICU, 86351078Speter * but mask them in the processor as well in case there are some 86451078Speter * (misconfigured) shared interrupts. 86551078Speter */ 86672200Sbmilekic mtx_lock_spin(&sio_lock); 86751078Speter/* EXTRA DELAY? */ 86851078Speter 86951078Speter /* 87051078Speter * Initialize the speed and the word size and wait long enough to 87151078Speter * drain the maximum of 16 bytes of junk in device output queues. 87251078Speter * The speed is undefined after a master reset and must be set 87351078Speter * before relying on anything related to output. There may be 87451078Speter * junk after a (very fast) soft reboot and (apparently) after 87551078Speter * master reset. 87651078Speter * XXX what about the UART bug avoided by waiting in comparam()? 87751078Speter * We don't want to to wait long enough to drain at 2 bps. 87851078Speter */ 87951078Speter if (iobase == siocniobase) 88051078Speter DELAY((16 + 1) * 1000000 / (comdefaultrate / 10)); 88151078Speter else { 88260471Snyan sio_setreg(com, com_cfcr, CFCR_DLAB | CFCR_8BITS); 88360471Snyan sio_setreg(com, com_dlbl, COMBRD(SIO_TEST_SPEED) & 0xff); 88460471Snyan sio_setreg(com, com_dlbh, (u_int) COMBRD(SIO_TEST_SPEED) >> 8); 88560471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); 88651078Speter DELAY((16 + 1) * 1000000 / (SIO_TEST_SPEED / 10)); 88751078Speter } 88851078Speter 88951078Speter /* 89051078Speter * Enable the interrupt gate and disable device interupts. This 89151078Speter * should leave the device driving the interrupt line low and 89251078Speter * guarantee an edge trigger if an interrupt can be generated. 89351078Speter */ 89451078Speter/* EXTRA DELAY? */ 89560471Snyan sio_setreg(com, com_mcr, mcr_image); 89660471Snyan sio_setreg(com, com_ier, 0); 89751078Speter DELAY(1000); /* XXX */ 89851078Speter irqmap[0] = isa_irq_pending(); 89951078Speter 90051078Speter /* 90151078Speter * Attempt to set loopback mode so that we can send a null byte 90251078Speter * without annoying any external device. 90351078Speter */ 90451078Speter/* EXTRA DELAY? */ 90560471Snyan sio_setreg(com, com_mcr, mcr_image | MCR_LOOPBACK); 90651078Speter 90751078Speter /* 90851078Speter * Attempt to generate an output interrupt. On 8250's, setting 90951078Speter * IER_ETXRDY generates an interrupt independent of the current 91051078Speter * setting and independent of whether the THR is empty. On 16450's, 91151078Speter * setting IER_ETXRDY generates an interrupt independent of the 91251078Speter * current setting. On 16550A's, setting IER_ETXRDY only 91351078Speter * generates an interrupt when IER_ETXRDY is not already set. 91451078Speter */ 91560471Snyan sio_setreg(com, com_ier, IER_ETXRDY); 91651078Speter 91751078Speter /* 91851078Speter * On some 16x50 incompatibles, setting IER_ETXRDY doesn't generate 91951078Speter * an interrupt. They'd better generate one for actually doing 92051078Speter * output. Loopback may be broken on the same incompatibles but 92151078Speter * it's unlikely to do more than allow the null byte out. 92251078Speter */ 92360471Snyan sio_setreg(com, com_data, 0); 92451078Speter DELAY((1 + 2) * 1000000 / (SIO_TEST_SPEED / 10)); 92551078Speter 92651078Speter /* 92751078Speter * Turn off loopback mode so that the interrupt gate works again 92851078Speter * (MCR_IENABLE was hidden). This should leave the device driving 92951078Speter * an interrupt line high. It doesn't matter if the interrupt 93051078Speter * line oscillates while we are not looking at it, since interrupts 93151078Speter * are disabled. 93251078Speter */ 93351078Speter/* EXTRA DELAY? */ 93460471Snyan sio_setreg(com, com_mcr, mcr_image); 93551078Speter 93651078Speter /* 93752471Simp * Some pcmcia cards have the "TXRDY bug", so we check everyone 93851078Speter * for IIR_TXRDY implementation ( Palido 321s, DC-1S... ) 93951078Speter */ 94053370Speter if (COM_NOPROBE(flags)) { 94153370Speter /* Reading IIR register twice */ 94253370Speter for (fn = 0; fn < 2; fn ++) { 94353370Speter DELAY(10000); 94460471Snyan failures[6] = sio_getreg(com, com_iir); 94553370Speter } 94653370Speter /* Check IIR_TXRDY clear ? */ 94753370Speter result = 0; 94853370Speter if (failures[6] & IIR_TXRDY) { 94953370Speter /* Nop, Double check with clearing IER */ 95060471Snyan sio_setreg(com, com_ier, 0); 95160471Snyan if (sio_getreg(com, com_iir) & IIR_NOPEND) { 95253370Speter /* Ok. we're familia this gang */ 95353370Speter SET_FLAG(dev, COM_C_IIR_TXRDYBUG); 95453370Speter } else { 95553370Speter /* Unknown, Just omit this chip.. XXX */ 95653370Speter result = ENXIO; 95781793Simp sio_setreg(com, com_mcr, 0); 95853370Speter } 95951078Speter } else { 96053370Speter /* OK. this is well-known guys */ 96153370Speter CLR_FLAG(dev, COM_C_IIR_TXRDYBUG); 96251078Speter } 96381793Simp sio_setreg(com, com_ier, 0); 96460471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); 96572200Sbmilekic mtx_unlock_spin(&sio_lock); 96653344Speter bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 96753344Speter return (iobase == siocniobase ? 0 : result); 96853344Speter } 96953344Speter 97051078Speter /* 97151078Speter * Check that 97251078Speter * o the CFCR, IER and MCR in UART hold the values written to them 97351078Speter * (the values happen to be all distinct - this is good for 97451078Speter * avoiding false positive tests from bus echoes). 97551078Speter * o an output interrupt is generated and its vector is correct. 97651078Speter * o the interrupt goes away when the IIR in the UART is read. 97751078Speter */ 97851078Speter/* EXTRA DELAY? */ 97960471Snyan failures[0] = sio_getreg(com, com_cfcr) - CFCR_8BITS; 98060471Snyan failures[1] = sio_getreg(com, com_ier) - IER_ETXRDY; 98160471Snyan failures[2] = sio_getreg(com, com_mcr) - mcr_image; 98251078Speter DELAY(10000); /* Some internal modems need this time */ 98351078Speter irqmap[1] = isa_irq_pending(); 98460471Snyan failures[4] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_TXRDY; 98551078Speter DELAY(1000); /* XXX */ 98651078Speter irqmap[2] = isa_irq_pending(); 98760471Snyan failures[6] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 98851078Speter 98951078Speter /* 99051078Speter * Turn off all device interrupts and check that they go off properly. 99151078Speter * Leave MCR_IENABLE alone. For ports without a master port, it gates 99251078Speter * the OUT2 output of the UART to 99351078Speter * the ICU input. Closing the gate would give a floating ICU input 99451078Speter * (unless there is another device driving it) and spurious interrupts. 99551078Speter * (On the system that this was first tested on, the input floats high 99651078Speter * and gives a (masked) interrupt as soon as the gate is closed.) 99751078Speter */ 99860471Snyan sio_setreg(com, com_ier, 0); 99960471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); /* dummy to avoid bus echo */ 100060471Snyan failures[7] = sio_getreg(com, com_ier); 100151078Speter DELAY(1000); /* XXX */ 100251078Speter irqmap[3] = isa_irq_pending(); 100360471Snyan failures[9] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 100451078Speter 100572200Sbmilekic mtx_unlock_spin(&sio_lock); 100651078Speter 100751078Speter irqs = irqmap[1] & ~irqmap[0]; 100854194Speter if (bus_get_resource(idev, SYS_RES_IRQ, 0, &xirq, NULL) == 0 && 100954194Speter ((1 << xirq) & irqs) == 0) 101051078Speter printf( 101154206Speter "sio%d: configured irq %ld not in bitmap of probed irqs %#x\n", 101253344Speter device_get_unit(dev), xirq, irqs); 101351078Speter if (bootverbose) 101451078Speter printf("sio%d: irq maps: %#x %#x %#x %#x\n", 101551078Speter device_get_unit(dev), 101651078Speter irqmap[0], irqmap[1], irqmap[2], irqmap[3]); 101751078Speter 101851078Speter result = 0; 101951078Speter for (fn = 0; fn < sizeof failures; ++fn) 102051078Speter if (failures[fn]) { 102160471Snyan sio_setreg(com, com_mcr, 0); 102251078Speter result = ENXIO; 102351078Speter if (bootverbose) { 102451078Speter printf("sio%d: probe failed test(s):", 102551078Speter device_get_unit(dev)); 102651078Speter for (fn = 0; fn < sizeof failures; ++fn) 102751078Speter if (failures[fn]) 102851078Speter printf(" %d", fn); 102951078Speter printf("\n"); 103051078Speter } 103151078Speter break; 103251078Speter } 103351078Speter bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 103451078Speter return (iobase == siocniobase ? 0 : result); 103551078Speter} 103651078Speter 103751078Speter#ifdef COM_ESP 103851078Speterstatic int 103951078Speterespattach(com, esp_port) 104051078Speter struct com_s *com; 104151078Speter Port_t esp_port; 104251078Speter{ 104351078Speter u_char dips; 104451078Speter u_char val; 104551078Speter 104651078Speter /* 104751078Speter * Check the ESP-specific I/O port to see if we're an ESP 104851078Speter * card. If not, return failure immediately. 104951078Speter */ 105051078Speter if ((inb(esp_port) & 0xf3) == 0) { 105151078Speter printf(" port 0x%x is not an ESP board?\n", esp_port); 105251078Speter return (0); 105351078Speter } 105451078Speter 105551078Speter /* 105651078Speter * We've got something that claims to be a Hayes ESP card. 105751078Speter * Let's hope so. 105851078Speter */ 105951078Speter 106051078Speter /* Get the dip-switch configuration */ 106151078Speter outb(esp_port + ESP_CMD1, ESP_GETDIPS); 106251078Speter dips = inb(esp_port + ESP_STATUS1); 106351078Speter 106451078Speter /* 106551078Speter * Bits 0,1 of dips say which COM port we are. 106651078Speter */ 106760471Snyan if (rman_get_start(com->ioportres) == likely_com_ports[dips & 0x03]) 106851078Speter printf(" : ESP"); 106951078Speter else { 107051078Speter printf(" esp_port has com %d\n", dips & 0x03); 107151078Speter return (0); 107251078Speter } 107351078Speter 107451078Speter /* 107551078Speter * Check for ESP version 2.0 or later: bits 4,5,6 = 010. 107651078Speter */ 107751078Speter outb(esp_port + ESP_CMD1, ESP_GETTEST); 107851078Speter val = inb(esp_port + ESP_STATUS1); /* clear reg 1 */ 107951078Speter val = inb(esp_port + ESP_STATUS2); 108051078Speter if ((val & 0x70) < 0x20) { 108151078Speter printf("-old (%o)", val & 0x70); 108251078Speter return (0); 108351078Speter } 108451078Speter 108551078Speter /* 108651078Speter * Check for ability to emulate 16550: bit 7 == 1 108751078Speter */ 108851078Speter if ((dips & 0x80) == 0) { 108951078Speter printf(" slave"); 109051078Speter return (0); 109151078Speter } 109251078Speter 109351078Speter /* 109451078Speter * Okay, we seem to be a Hayes ESP card. Whee. 109551078Speter */ 109651078Speter com->esp = TRUE; 109751078Speter com->esp_port = esp_port; 109851078Speter return (1); 109951078Speter} 110051078Speter#endif /* COM_ESP */ 110151078Speter 110251078Speterstatic int 110352471Simpsio_isa_attach(dev) 110452471Simp device_t dev; 110552471Simp{ 110658885Simp return (sioattach(dev, 0)); 110752471Simp} 110852471Simp 110952471Simpstatic int 111058885Simpsioattach(dev, xrid) 111151078Speter device_t dev; 111258885Simp int xrid; 111351078Speter{ 111451078Speter struct com_s *com; 111551078Speter#ifdef COM_ESP 111651078Speter Port_t *espp; 111751078Speter#endif 111851078Speter Port_t iobase; 111951078Speter int unit; 112053344Speter u_int flags; 112151078Speter int rid; 112251078Speter struct resource *port; 112353344Speter int ret; 112451078Speter 112558885Simp rid = xrid; 112651078Speter port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 112751078Speter 0, ~0, IO_COMSIZE, RF_ACTIVE); 112851078Speter if (!port) 112957915Simp return (ENXIO); 113051078Speter 113151078Speter iobase = rman_get_start(port); 113251078Speter unit = device_get_unit(dev); 113351078Speter com = device_get_softc(dev); 113453344Speter flags = device_get_flags(dev); 113551078Speter 113653344Speter if (unit >= sio_numunits) 113753344Speter sio_numunits = unit + 1; 113851078Speter /* 113951078Speter * sioprobe() has initialized the device registers as follows: 114051078Speter * o cfcr = CFCR_8BITS. 114151078Speter * It is most important that CFCR_DLAB is off, so that the 114251078Speter * data port is not hidden when we enable interrupts. 114351078Speter * o ier = 0. 114451078Speter * Interrupts are only enabled when the line is open. 114551078Speter * o mcr = MCR_IENABLE, or 0 if the port has AST/4 compatible 114651078Speter * interrupt control register or the config specifies no irq. 114751078Speter * Keeping MCR_DTR and MCR_RTS off might stop the external 114851078Speter * device from sending before we are ready. 114951078Speter */ 115051078Speter bzero(com, sizeof *com); 115151078Speter com->unit = unit; 115251078Speter com->ioportres = port; 115360471Snyan com->bst = rman_get_bustag(port); 115460471Snyan com->bsh = rman_get_bushandle(port); 115551078Speter com->cfcr_image = CFCR_8BITS; 115651078Speter com->dtr_wait = 3 * hz; 115751078Speter com->loses_outints = COM_LOSESOUTINTS(flags) != 0; 115857234Sbde com->no_irq = bus_get_resource(dev, SYS_RES_IRQ, 0, NULL, NULL) != 0; 115951078Speter com->tx_fifo_size = 1; 116051078Speter com->obufs[0].l_head = com->obuf1; 116151078Speter com->obufs[1].l_head = com->obuf2; 116251078Speter 116351078Speter com->data_port = iobase + com_data; 116451078Speter com->int_id_port = iobase + com_iir; 116551078Speter com->modem_ctl_port = iobase + com_mcr; 116651078Speter com->mcr_image = inb(com->modem_ctl_port); 116751078Speter com->line_status_port = iobase + com_lsr; 116851078Speter com->modem_status_port = iobase + com_msr; 116951078Speter com->intr_ctl_port = iobase + com_ier; 117051078Speter 117151078Speter /* 117251078Speter * We don't use all the flags from <sys/ttydefaults.h> since they 117351078Speter * are only relevant for logins. It's important to have echo off 117451078Speter * initially so that the line doesn't start blathering before the 117551078Speter * echo flag can be turned off. 117651078Speter */ 117751078Speter com->it_in.c_iflag = 0; 117851078Speter com->it_in.c_oflag = 0; 117951078Speter com->it_in.c_cflag = TTYDEF_CFLAG; 118051078Speter com->it_in.c_lflag = 0; 118151078Speter if (unit == comconsole) { 118251078Speter com->it_in.c_iflag = TTYDEF_IFLAG; 118351078Speter com->it_in.c_oflag = TTYDEF_OFLAG; 118451078Speter com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL; 118551078Speter com->it_in.c_lflag = TTYDEF_LFLAG; 118651078Speter com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL; 118751078Speter com->lt_out.c_ispeed = com->lt_out.c_ospeed = 118851078Speter com->lt_in.c_ispeed = com->lt_in.c_ospeed = 118951078Speter com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate; 119051078Speter } else 119151078Speter com->it_in.c_ispeed = com->it_in.c_ospeed = TTYDEF_SPEED; 119265605Sjhb if (siosetwater(com, com->it_in.c_ispeed) != 0) { 119372200Sbmilekic mtx_unlock_spin(&sio_lock); 119456788Sbde /* 119556788Sbde * Leave i/o resources allocated if this is a `cn'-level 119656788Sbde * console, so that other devices can't snarf them. 119756788Sbde */ 119856788Sbde if (iobase != siocniobase) 119956788Sbde bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 120056788Sbde return (ENOMEM); 120151078Speter } 120272200Sbmilekic mtx_unlock_spin(&sio_lock); 120351078Speter termioschars(&com->it_in); 120451078Speter com->it_out = com->it_in; 120551078Speter 120651078Speter /* attempt to determine UART type */ 120751078Speter printf("sio%d: type", unit); 120851078Speter 120951078Speter 121051078Speter#ifdef COM_MULTIPORT 121151078Speter if (!COM_ISMULTIPORT(flags) && !COM_IIR_TXRDYBUG(flags)) 121251078Speter#else 121351078Speter if (!COM_IIR_TXRDYBUG(flags)) 121451078Speter#endif 121551078Speter { 121651078Speter u_char scr; 121751078Speter u_char scr1; 121851078Speter u_char scr2; 121951078Speter 122060471Snyan scr = sio_getreg(com, com_scr); 122160471Snyan sio_setreg(com, com_scr, 0xa5); 122260471Snyan scr1 = sio_getreg(com, com_scr); 122360471Snyan sio_setreg(com, com_scr, 0x5a); 122460471Snyan scr2 = sio_getreg(com, com_scr); 122560471Snyan sio_setreg(com, com_scr, scr); 122651078Speter if (scr1 != 0xa5 || scr2 != 0x5a) { 122751078Speter printf(" 8250"); 122851078Speter goto determined_type; 122951078Speter } 123051078Speter } 123160471Snyan sio_setreg(com, com_fifo, FIFO_ENABLE | FIFO_RX_HIGH); 123251078Speter DELAY(100); 123351078Speter com->st16650a = 0; 123451078Speter switch (inb(com->int_id_port) & IIR_FIFO_MASK) { 123551078Speter case FIFO_RX_LOW: 123651078Speter printf(" 16450"); 123751078Speter break; 123851078Speter case FIFO_RX_MEDL: 123951078Speter printf(" 16450?"); 124051078Speter break; 124151078Speter case FIFO_RX_MEDH: 124251078Speter printf(" 16550?"); 124351078Speter break; 124451078Speter case FIFO_RX_HIGH: 124551078Speter if (COM_NOFIFO(flags)) { 124651078Speter printf(" 16550A fifo disabled"); 124751078Speter } else { 124851078Speter com->hasfifo = TRUE; 124951078Speter if (COM_ST16650A(flags)) { 125051078Speter com->st16650a = 1; 125151078Speter com->tx_fifo_size = 32; 125251078Speter printf(" ST16650A"); 125351078Speter } else { 125451078Speter com->tx_fifo_size = COM_FIFOSIZE(flags); 125551078Speter printf(" 16550A"); 125651078Speter } 125751078Speter } 125851078Speter#ifdef COM_ESP 125951078Speter for (espp = likely_esp_ports; *espp != 0; espp++) 126051078Speter if (espattach(com, *espp)) { 126151078Speter com->tx_fifo_size = 1024; 126251078Speter break; 126351078Speter } 126451078Speter#endif 126551078Speter if (!com->st16650a) { 126651078Speter if (!com->tx_fifo_size) 126751078Speter com->tx_fifo_size = 16; 126851078Speter else 126951078Speter printf(" lookalike with %d bytes FIFO", 127051078Speter com->tx_fifo_size); 127151078Speter } 127251078Speter 127351078Speter break; 127451078Speter } 127551078Speter 127651078Speter#ifdef COM_ESP 127751078Speter if (com->esp) { 127851078Speter /* 127951078Speter * Set 16550 compatibility mode. 128051078Speter * We don't use the ESP_MODE_SCALE bit to increase the 128151078Speter * fifo trigger levels because we can't handle large 128251078Speter * bursts of input. 128351078Speter * XXX flow control should be set in comparam(), not here. 128451078Speter */ 128551078Speter outb(com->esp_port + ESP_CMD1, ESP_SETMODE); 128651078Speter outb(com->esp_port + ESP_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO); 128751078Speter 128851078Speter /* Set RTS/CTS flow control. */ 128951078Speter outb(com->esp_port + ESP_CMD1, ESP_SETFLOWTYPE); 129051078Speter outb(com->esp_port + ESP_CMD2, ESP_FLOW_RTS); 129151078Speter outb(com->esp_port + ESP_CMD2, ESP_FLOW_CTS); 129251078Speter 129351078Speter /* Set flow-control levels. */ 129451078Speter outb(com->esp_port + ESP_CMD1, ESP_SETRXFLOW); 129551078Speter outb(com->esp_port + ESP_CMD2, HIBYTE(768)); 129651078Speter outb(com->esp_port + ESP_CMD2, LOBYTE(768)); 129751078Speter outb(com->esp_port + ESP_CMD2, HIBYTE(512)); 129851078Speter outb(com->esp_port + ESP_CMD2, LOBYTE(512)); 129951078Speter } 130051078Speter#endif /* COM_ESP */ 130160471Snyan sio_setreg(com, com_fifo, 0); 130251078Speterdetermined_type: ; 130351078Speter 130451078Speter#ifdef COM_MULTIPORT 130551078Speter if (COM_ISMULTIPORT(flags)) { 130653344Speter device_t masterdev; 130753344Speter 130851078Speter com->multiport = TRUE; 130951078Speter printf(" (multiport"); 131051078Speter if (unit == COM_MPMASTER(flags)) 131151078Speter printf(" master"); 131251078Speter printf(")"); 131353344Speter masterdev = devclass_get_device(sio_devclass, 131453344Speter COM_MPMASTER(flags)); 131557234Sbde com->no_irq = (masterdev == NULL || bus_get_resource(masterdev, 131657234Sbde SYS_RES_IRQ, 0, NULL, NULL) != 0); 131751078Speter } 131851078Speter#endif /* COM_MULTIPORT */ 131951078Speter if (unit == comconsole) 132051078Speter printf(", console"); 132153344Speter if (COM_IIR_TXRDYBUG(flags)) 132251078Speter printf(" with a bogus IIR_TXRDY register"); 132351078Speter printf("\n"); 132451078Speter 132567551Sjhb if (sio_fast_ih == NULL) { 132672238Sjhb swi_add(&tty_ithd, "tty:sio", siopoll, NULL, SWI_TTY, 0, 132772238Sjhb &sio_fast_ih); 132872238Sjhb swi_add(&clk_ithd, "tty:sio", siopoll, NULL, SWI_TTY, 0, 132972238Sjhb &sio_slow_ih); 133051078Speter } 133165131Sphk com->devs[0] = make_dev(&sio_cdevsw, unit, 133251078Speter UID_ROOT, GID_WHEEL, 0600, "ttyd%r", unit); 133365131Sphk com->devs[1] = make_dev(&sio_cdevsw, unit | CONTROL_INIT_STATE, 133451078Speter UID_ROOT, GID_WHEEL, 0600, "ttyid%r", unit); 133565131Sphk com->devs[2] = make_dev(&sio_cdevsw, unit | CONTROL_LOCK_STATE, 133651078Speter UID_ROOT, GID_WHEEL, 0600, "ttyld%r", unit); 133765131Sphk com->devs[3] = make_dev(&sio_cdevsw, unit | CALLOUT_MASK, 133851078Speter UID_UUCP, GID_DIALER, 0660, "cuaa%r", unit); 133965131Sphk com->devs[4] = make_dev(&sio_cdevsw, 134065131Sphk unit | CALLOUT_MASK | CONTROL_INIT_STATE, 134151078Speter UID_UUCP, GID_DIALER, 0660, "cuaia%r", unit); 134265131Sphk com->devs[5] = make_dev(&sio_cdevsw, 134365131Sphk unit | CALLOUT_MASK | CONTROL_LOCK_STATE, 134451078Speter UID_UUCP, GID_DIALER, 0660, "cuala%r", unit); 134551078Speter com->flags = flags; 134651078Speter com->pps.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR; 134751078Speter pps_init(&com->pps); 134851078Speter 134951078Speter rid = 0; 135051078Speter com->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0ul, ~0ul, 1, 135153344Speter RF_ACTIVE); 135253344Speter if (com->irqres) { 135353344Speter ret = BUS_SETUP_INTR(device_get_parent(dev), dev, com->irqres, 135465557Sjasone INTR_TYPE_TTY | INTR_FAST, 135554386Simp siointr, com, &com->cookie); 135654194Speter if (ret) { 135754194Speter ret = BUS_SETUP_INTR(device_get_parent(dev), dev, 135854194Speter com->irqres, INTR_TYPE_TTY, 135954386Simp siointr, com, &com->cookie); 136054194Speter if (ret == 0) 136154194Speter device_printf(dev, "unable to activate interrupt in fast mode - using normal mode"); 136254194Speter } 136353344Speter if (ret) 136453344Speter device_printf(dev, "could not activate interrupt\n"); 136578504Siedowse#if defined(DDB) && (defined(BREAK_TO_DEBUGGER) || \ 136678504Siedowse defined(ALT_BREAK_TO_DEBUGGER)) 136778504Siedowse /* 136878504Siedowse * Enable interrupts for early break-to-debugger support 136978504Siedowse * on the console. 137078504Siedowse */ 137178504Siedowse if (ret == 0 && unit == comconsole) 137278504Siedowse outb(siocniobase + com_ier, IER_ERXRDY | IER_ERLS | 137378504Siedowse IER_EMSC); 137478504Siedowse#endif 137553344Speter } 137651078Speter 137751078Speter return (0); 137851078Speter} 137951078Speter 138051078Speterstatic int 138151078Spetersioopen(dev, flag, mode, p) 138251078Speter dev_t dev; 138351078Speter int flag; 138451078Speter int mode; 138551078Speter struct proc *p; 138651078Speter{ 138751078Speter struct com_s *com; 138851078Speter int error; 138951078Speter int mynor; 139051078Speter int s; 139151078Speter struct tty *tp; 139251078Speter int unit; 139351078Speter 139451078Speter mynor = minor(dev); 139551078Speter unit = MINOR_TO_UNIT(mynor); 139653344Speter com = com_addr(unit); 139753344Speter if (com == NULL) 139851078Speter return (ENXIO); 139951078Speter if (com->gone) 140051078Speter return (ENXIO); 140151078Speter if (mynor & CONTROL_MASK) 140251078Speter return (0); 140351078Speter tp = dev->si_tty = com->tp = ttymalloc(com->tp); 140451078Speter s = spltty(); 140551078Speter /* 140651078Speter * We jump to this label after all non-interrupted sleeps to pick 140751078Speter * up any changes of the device state. 140851078Speter */ 140951078Speteropen_top: 141051078Speter while (com->state & CS_DTR_OFF) { 141151078Speter error = tsleep(&com->dtr_wait, TTIPRI | PCATCH, "siodtr", 0); 141251078Speter if (com_addr(unit) == NULL) 141351078Speter return (ENXIO); 141451078Speter if (error != 0 || com->gone) 141551078Speter goto out; 141651078Speter } 141751078Speter if (tp->t_state & TS_ISOPEN) { 141851078Speter /* 141951078Speter * The device is open, so everything has been initialized. 142051078Speter * Handle conflicts. 142151078Speter */ 142251078Speter if (mynor & CALLOUT_MASK) { 142351078Speter if (!com->active_out) { 142451078Speter error = EBUSY; 142551078Speter goto out; 142651078Speter } 142751078Speter } else { 142851078Speter if (com->active_out) { 142951078Speter if (flag & O_NONBLOCK) { 143051078Speter error = EBUSY; 143151078Speter goto out; 143251078Speter } 143351078Speter error = tsleep(&com->active_out, 143451078Speter TTIPRI | PCATCH, "siobi", 0); 143551078Speter if (com_addr(unit) == NULL) 143651078Speter return (ENXIO); 143751078Speter if (error != 0 || com->gone) 143851078Speter goto out; 143951078Speter goto open_top; 144051078Speter } 144151078Speter } 144251078Speter if (tp->t_state & TS_XCLUDE && 144351078Speter suser(p)) { 144451078Speter error = EBUSY; 144551078Speter goto out; 144651078Speter } 144751078Speter } else { 144851078Speter /* 144951078Speter * The device isn't open, so there are no conflicts. 145051078Speter * Initialize it. Initialization is done twice in many 145151078Speter * cases: to preempt sleeping callin opens if we are 145251078Speter * callout, and to complete a callin open after DCD rises. 145351078Speter */ 145451078Speter tp->t_oproc = comstart; 145551078Speter tp->t_param = comparam; 145651654Sphk tp->t_stop = comstop; 145751078Speter tp->t_dev = dev; 145851078Speter tp->t_termios = mynor & CALLOUT_MASK 145951078Speter ? com->it_out : com->it_in; 146051078Speter (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET); 146151078Speter com->poll = com->no_irq; 146251078Speter com->poll_output = com->loses_outints; 146351078Speter ++com->wopeners; 146451078Speter error = comparam(tp, &tp->t_termios); 146551078Speter --com->wopeners; 146651078Speter if (error != 0) 146751078Speter goto out; 146851078Speter /* 146951078Speter * XXX we should goto open_top if comparam() slept. 147051078Speter */ 147151078Speter if (com->hasfifo) { 147251078Speter /* 147351078Speter * (Re)enable and drain fifos. 147451078Speter * 147551078Speter * Certain SMC chips cause problems if the fifos 147651078Speter * are enabled while input is ready. Turn off the 147751078Speter * fifo if necessary to clear the input. We test 147851078Speter * the input ready bit after enabling the fifos 147951078Speter * since we've already enabled them in comparam() 148051078Speter * and to handle races between enabling and fresh 148151078Speter * input. 148251078Speter */ 148351078Speter while (TRUE) { 148460471Snyan sio_setreg(com, com_fifo, 148560471Snyan FIFO_RCV_RST | FIFO_XMT_RST 148660471Snyan | com->fifo_image); 148751078Speter /* 148851078Speter * XXX the delays are for superstitious 148951078Speter * historical reasons. It must be less than 149051078Speter * the character time at the maximum 149151078Speter * supported speed (87 usec at 115200 bps 149251078Speter * 8N1). Otherwise we might loop endlessly 149351078Speter * if data is streaming in. We used to use 149451078Speter * delays of 100. That usually worked 149551078Speter * because DELAY(100) used to usually delay 149651078Speter * for about 85 usec instead of 100. 149751078Speter */ 149851078Speter DELAY(50); 149951078Speter if (!(inb(com->line_status_port) & LSR_RXRDY)) 150051078Speter break; 150160471Snyan sio_setreg(com, com_fifo, 0); 150251078Speter DELAY(50); 150351078Speter (void) inb(com->data_port); 150451078Speter } 150551078Speter } 150651078Speter 150772200Sbmilekic mtx_lock_spin(&sio_lock); 150851078Speter (void) inb(com->line_status_port); 150951078Speter (void) inb(com->data_port); 151051078Speter com->prev_modem_status = com->last_modem_status 151151078Speter = inb(com->modem_status_port); 151251078Speter if (COM_IIR_TXRDYBUG(com->flags)) { 151351078Speter outb(com->intr_ctl_port, IER_ERXRDY | IER_ERLS 151451078Speter | IER_EMSC); 151551078Speter } else { 151651078Speter outb(com->intr_ctl_port, IER_ERXRDY | IER_ETXRDY 151751078Speter | IER_ERLS | IER_EMSC); 151851078Speter } 151972200Sbmilekic mtx_unlock_spin(&sio_lock); 152051078Speter /* 152151078Speter * Handle initial DCD. Callout devices get a fake initial 152251078Speter * DCD (trapdoor DCD). If we are callout, then any sleeping 152351078Speter * callin opens get woken up and resume sleeping on "siobi" 152451078Speter * instead of "siodcd". 152551078Speter */ 152651078Speter /* 152751078Speter * XXX `mynor & CALLOUT_MASK' should be 152851078Speter * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where 152951078Speter * TRAPDOOR_CARRIER is the default initial state for callout 153051078Speter * devices and SOFT_CARRIER is like CLOCAL except it hides 153151078Speter * the true carrier. 153251078Speter */ 153351078Speter if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK) 153451078Speter (*linesw[tp->t_line].l_modem)(tp, 1); 153551078Speter } 153651078Speter /* 153751078Speter * Wait for DCD if necessary. 153851078Speter */ 153951078Speter if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK) 154051078Speter && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) { 154151078Speter ++com->wopeners; 154251078Speter error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "siodcd", 0); 154351078Speter if (com_addr(unit) == NULL) 154451078Speter return (ENXIO); 154551078Speter --com->wopeners; 154651078Speter if (error != 0 || com->gone) 154751078Speter goto out; 154851078Speter goto open_top; 154951078Speter } 155051078Speter error = (*linesw[tp->t_line].l_open)(dev, tp); 155151078Speter disc_optim(tp, &tp->t_termios, com); 155251078Speter if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK) 155351078Speter com->active_out = TRUE; 155451078Speter siosettimeout(); 155551078Speterout: 155651078Speter splx(s); 155751078Speter if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0) 155851078Speter comhardclose(com); 155951078Speter return (error); 156051078Speter} 156151078Speter 156251078Speterstatic int 156351078Spetersioclose(dev, flag, mode, p) 156451078Speter dev_t dev; 156551078Speter int flag; 156651078Speter int mode; 156751078Speter struct proc *p; 156851078Speter{ 156951078Speter struct com_s *com; 157051078Speter int mynor; 157151078Speter int s; 157251078Speter struct tty *tp; 157351078Speter 157451078Speter mynor = minor(dev); 157551078Speter if (mynor & CONTROL_MASK) 157651078Speter return (0); 157751078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 157857915Simp if (com == NULL) 157957915Simp return (ENODEV); 158051078Speter tp = com->tp; 158151078Speter s = spltty(); 158251078Speter (*linesw[tp->t_line].l_close)(tp, flag); 158351078Speter disc_optim(tp, &tp->t_termios, com); 158451654Sphk comstop(tp, FREAD | FWRITE); 158551078Speter comhardclose(com); 158651078Speter ttyclose(tp); 158751078Speter siosettimeout(); 158851078Speter splx(s); 158951078Speter if (com->gone) { 159051078Speter printf("sio%d: gone\n", com->unit); 159151078Speter s = spltty(); 159251078Speter if (com->ibuf != NULL) 159351078Speter free(com->ibuf, M_DEVBUF); 159451078Speter bzero(tp, sizeof *tp); 159551078Speter splx(s); 159651078Speter } 159751078Speter return (0); 159851078Speter} 159951078Speter 160051078Speterstatic void 160151078Spetercomhardclose(com) 160251078Speter struct com_s *com; 160351078Speter{ 160451078Speter int s; 160551078Speter struct tty *tp; 160651078Speter int unit; 160751078Speter 160851078Speter unit = com->unit; 160951078Speter s = spltty(); 161051078Speter com->poll = FALSE; 161151078Speter com->poll_output = FALSE; 161251078Speter com->do_timestamp = FALSE; 161351078Speter com->do_dcd_timestamp = FALSE; 161451078Speter com->pps.ppsparam.mode = 0; 161560471Snyan sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 161678504Siedowse tp = com->tp; 161778504Siedowse 161878504Siedowse#if defined(DDB) && (defined(BREAK_TO_DEBUGGER) || \ 161978504Siedowse defined(ALT_BREAK_TO_DEBUGGER)) 162078504Siedowse /* 162178504Siedowse * Leave interrupts enabled and don't clear DTR if this is the 162278504Siedowse * console. This allows us to detect break-to-debugger events 162378504Siedowse * while the console device is closed. 162478504Siedowse */ 162578504Siedowse if (com->unit != comconsole) 162678504Siedowse#endif 162751078Speter { 162860471Snyan sio_setreg(com, com_ier, 0); 162951078Speter if (tp->t_cflag & HUPCL 163051078Speter /* 163151078Speter * XXX we will miss any carrier drop between here and the 163251078Speter * next open. Perhaps we should watch DCD even when the 163351078Speter * port is closed; it is not sufficient to check it at 163451078Speter * the next open because it might go up and down while 163551078Speter * we're not watching. 163651078Speter */ 163751078Speter || (!com->active_out 163851078Speter && !(com->prev_modem_status & MSR_DCD) 163951078Speter && !(com->it_in.c_cflag & CLOCAL)) 164051078Speter || !(tp->t_state & TS_ISOPEN)) { 164151078Speter (void)commctl(com, TIOCM_DTR, DMBIC); 164251078Speter if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) { 164351078Speter timeout(siodtrwakeup, com, com->dtr_wait); 164451078Speter com->state |= CS_DTR_OFF; 164551078Speter } 164651078Speter } 164751078Speter } 164851078Speter if (com->hasfifo) { 164951078Speter /* 165051078Speter * Disable fifos so that they are off after controlled 165151078Speter * reboots. Some BIOSes fail to detect 16550s when the 165251078Speter * fifos are enabled. 165351078Speter */ 165460471Snyan sio_setreg(com, com_fifo, 0); 165551078Speter } 165651078Speter com->active_out = FALSE; 165751078Speter wakeup(&com->active_out); 165851078Speter wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */ 165951078Speter splx(s); 166051078Speter} 166151078Speter 166251078Speterstatic int 166351078Spetersioread(dev, uio, flag) 166451078Speter dev_t dev; 166551078Speter struct uio *uio; 166651078Speter int flag; 166751078Speter{ 166851078Speter int mynor; 166951078Speter struct com_s *com; 167051078Speter 167151078Speter mynor = minor(dev); 167251078Speter if (mynor & CONTROL_MASK) 167351078Speter return (ENODEV); 167451078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 167557915Simp if (com == NULL || com->gone) 167651078Speter return (ENODEV); 167751078Speter return ((*linesw[com->tp->t_line].l_read)(com->tp, uio, flag)); 167851078Speter} 167951078Speter 168051078Speterstatic int 168151078Spetersiowrite(dev, uio, flag) 168251078Speter dev_t dev; 168351078Speter struct uio *uio; 168451078Speter int flag; 168551078Speter{ 168651078Speter int mynor; 168751078Speter struct com_s *com; 168851078Speter int unit; 168951078Speter 169051078Speter mynor = minor(dev); 169151078Speter if (mynor & CONTROL_MASK) 169251078Speter return (ENODEV); 169351078Speter 169451078Speter unit = MINOR_TO_UNIT(mynor); 169551078Speter com = com_addr(unit); 169657915Simp if (com == NULL || com->gone) 169751078Speter return (ENODEV); 169851078Speter /* 169951078Speter * (XXX) We disallow virtual consoles if the physical console is 170051078Speter * a serial port. This is in case there is a display attached that 170151078Speter * is not the console. In that situation we don't need/want the X 170251078Speter * server taking over the console. 170351078Speter */ 170451078Speter if (constty != NULL && unit == comconsole) 170551078Speter constty = NULL; 170651078Speter return ((*linesw[com->tp->t_line].l_write)(com->tp, uio, flag)); 170751078Speter} 170851078Speter 170951078Speterstatic void 171051078Spetersiobusycheck(chan) 171151078Speter void *chan; 171251078Speter{ 171351078Speter struct com_s *com; 171451078Speter int s; 171551078Speter 171651078Speter com = (struct com_s *)chan; 171751078Speter 171851078Speter /* 171951078Speter * Clear TS_BUSY if low-level output is complete. 172051078Speter * spl locking is sufficient because siointr1() does not set CS_BUSY. 172151078Speter * If siointr1() clears CS_BUSY after we look at it, then we'll get 172251078Speter * called again. Reading the line status port outside of siointr1() 172351078Speter * is safe because CS_BUSY is clear so there are no output interrupts 172451078Speter * to lose. 172551078Speter */ 172651078Speter s = spltty(); 172751078Speter if (com->state & CS_BUSY) 172851078Speter com->extra_state &= ~CSE_BUSYCHECK; /* False alarm. */ 172951078Speter else if ((inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY)) 173051078Speter == (LSR_TSRE | LSR_TXRDY)) { 173151078Speter com->tp->t_state &= ~TS_BUSY; 173251078Speter ttwwakeup(com->tp); 173351078Speter com->extra_state &= ~CSE_BUSYCHECK; 173451078Speter } else 173551078Speter timeout(siobusycheck, com, hz / 100); 173651078Speter splx(s); 173751078Speter} 173851078Speter 173951078Speterstatic void 174051078Spetersiodtrwakeup(chan) 174151078Speter void *chan; 174251078Speter{ 174351078Speter struct com_s *com; 174451078Speter 174551078Speter com = (struct com_s *)chan; 174651078Speter com->state &= ~CS_DTR_OFF; 174751078Speter wakeup(&com->dtr_wait); 174851078Speter} 174951078Speter 175065557Sjasone/* 175170174Sjhb * Call this function with the sio_lock mutex held. It will return with the 175270174Sjhb * lock still held. 175365557Sjasone */ 175451078Speterstatic void 175551078Spetersioinput(com) 175651078Speter struct com_s *com; 175751078Speter{ 175851078Speter u_char *buf; 175951078Speter int incc; 176051078Speter u_char line_status; 176151078Speter int recv_data; 176251078Speter struct tty *tp; 176351078Speter 176451078Speter buf = com->ibuf; 176551078Speter tp = com->tp; 176651078Speter if (!(tp->t_state & TS_ISOPEN) || !(tp->t_cflag & CREAD)) { 176751078Speter com_events -= (com->iptr - com->ibuf); 176851078Speter com->iptr = com->ibuf; 176951078Speter return; 177051078Speter } 177151078Speter if (tp->t_state & TS_CAN_BYPASS_L_RINT) { 177251078Speter /* 177351078Speter * Avoid the grotesquely inefficient lineswitch routine 177451078Speter * (ttyinput) in "raw" mode. It usually takes about 450 177551078Speter * instructions (that's without canonical processing or echo!). 177651078Speter * slinput is reasonably fast (usually 40 instructions plus 177751078Speter * call overhead). 177851078Speter */ 177951078Speter do { 178065557Sjasone /* 178165557Sjasone * This may look odd, but it is using save-and-enable 178265557Sjasone * semantics instead of the save-and-disable semantics 178365557Sjasone * that are used everywhere else. 178465557Sjasone */ 178572200Sbmilekic mtx_unlock_spin(&sio_lock); 178651078Speter incc = com->iptr - buf; 178751078Speter if (tp->t_rawq.c_cc + incc > tp->t_ihiwat 178851078Speter && (com->state & CS_RTS_IFLOW 178951078Speter || tp->t_iflag & IXOFF) 179051078Speter && !(tp->t_state & TS_TBLOCK)) 179151078Speter ttyblock(tp); 179251078Speter com->delta_error_counts[CE_TTY_BUF_OVERFLOW] 179351078Speter += b_to_q((char *)buf, incc, &tp->t_rawq); 179451078Speter buf += incc; 179551078Speter tk_nin += incc; 179651078Speter tk_rawcc += incc; 179751078Speter tp->t_rawcc += incc; 179851078Speter ttwakeup(tp); 179951078Speter if (tp->t_state & TS_TTSTOP 180051078Speter && (tp->t_iflag & IXANY 180151078Speter || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) { 180251078Speter tp->t_state &= ~TS_TTSTOP; 180351078Speter tp->t_lflag &= ~FLUSHO; 180451078Speter comstart(tp); 180551078Speter } 180672200Sbmilekic mtx_lock_spin(&sio_lock); 180751078Speter } while (buf < com->iptr); 180851078Speter } else { 180951078Speter do { 181065557Sjasone /* 181165557Sjasone * This may look odd, but it is using save-and-enable 181265557Sjasone * semantics instead of the save-and-disable semantics 181365557Sjasone * that are used everywhere else. 181465557Sjasone */ 181572200Sbmilekic mtx_unlock_spin(&sio_lock); 181651078Speter line_status = buf[com->ierroff]; 181751078Speter recv_data = *buf++; 181851078Speter if (line_status 181951078Speter & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) { 182051078Speter if (line_status & LSR_BI) 182151078Speter recv_data |= TTY_BI; 182251078Speter if (line_status & LSR_FE) 182351078Speter recv_data |= TTY_FE; 182451078Speter if (line_status & LSR_OE) 182551078Speter recv_data |= TTY_OE; 182651078Speter if (line_status & LSR_PE) 182751078Speter recv_data |= TTY_PE; 182851078Speter } 182951078Speter (*linesw[tp->t_line].l_rint)(recv_data, tp); 183072200Sbmilekic mtx_lock_spin(&sio_lock); 183151078Speter } while (buf < com->iptr); 183251078Speter } 183351078Speter com_events -= (com->iptr - com->ibuf); 183451078Speter com->iptr = com->ibuf; 183551078Speter 183651078Speter /* 183751078Speter * There is now room for another low-level buffer full of input, 183851078Speter * so enable RTS if it is now disabled and there is room in the 183951078Speter * high-level buffer. 184051078Speter */ 184151078Speter if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & MCR_RTS) && 184251078Speter !(tp->t_state & TS_TBLOCK)) 184351078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 184451078Speter} 184551078Speter 184651078Spetervoid 184751078Spetersiointr(arg) 184851078Speter void *arg; 184951078Speter{ 185070174Sjhb struct com_s *com; 185170174Sjhb 185251078Speter#ifndef COM_MULTIPORT 185370174Sjhb com = (struct com_s *)arg; 185470174Sjhb 185572200Sbmilekic mtx_lock_spin(&sio_lock); 185670174Sjhb siointr1(com); 185772200Sbmilekic mtx_unlock_spin(&sio_lock); 185851078Speter#else /* COM_MULTIPORT */ 185951078Speter bool_t possibly_more_intrs; 186051078Speter int unit; 186151078Speter 186251078Speter /* 186351078Speter * Loop until there is no activity on any port. This is necessary 186451078Speter * to get an interrupt edge more than to avoid another interrupt. 186551078Speter * If the IRQ signal is just an OR of the IRQ signals from several 186651078Speter * devices, then the edge from one may be lost because another is 186751078Speter * on. 186851078Speter */ 186972200Sbmilekic mtx_lock_spin(&sio_lock); 187051078Speter do { 187151078Speter possibly_more_intrs = FALSE; 187253344Speter for (unit = 0; unit < sio_numunits; ++unit) { 187351078Speter com = com_addr(unit); 187451078Speter /* 187551078Speter * XXX COM_LOCK(); 187651078Speter * would it work here, or be counter-productive? 187751078Speter */ 187851078Speter if (com != NULL 187951078Speter && !com->gone 188051078Speter && (inb(com->int_id_port) & IIR_IMASK) 188151078Speter != IIR_NOPEND) { 188251078Speter siointr1(com); 188351078Speter possibly_more_intrs = TRUE; 188451078Speter } 188551078Speter /* XXX COM_UNLOCK(); */ 188651078Speter } 188751078Speter } while (possibly_more_intrs); 188872200Sbmilekic mtx_unlock_spin(&sio_lock); 188951078Speter#endif /* COM_MULTIPORT */ 189051078Speter} 189151078Speter 189251078Speterstatic void 189351078Spetersiointr1(com) 189451078Speter struct com_s *com; 189551078Speter{ 189651078Speter u_char line_status; 189751078Speter u_char modem_status; 189851078Speter u_char *ioptr; 189951078Speter u_char recv_data; 190051078Speter u_char int_ctl; 190151078Speter u_char int_ctl_new; 190251078Speter struct timecounter *tc; 190351078Speter u_int count; 190451078Speter 190551078Speter int_ctl = inb(com->intr_ctl_port); 190651078Speter int_ctl_new = int_ctl; 190751078Speter 190851078Speter while (!com->gone) { 190951078Speter if (com->pps.ppsparam.mode & PPS_CAPTUREBOTH) { 191051078Speter modem_status = inb(com->modem_status_port); 191151078Speter if ((modem_status ^ com->last_modem_status) & MSR_DCD) { 191251078Speter tc = timecounter; 191351078Speter count = tc->tc_get_timecount(tc); 191451078Speter pps_event(&com->pps, tc, count, 191551078Speter (modem_status & MSR_DCD) ? 191651078Speter PPS_CAPTUREASSERT : PPS_CAPTURECLEAR); 191751078Speter } 191851078Speter } 191951078Speter line_status = inb(com->line_status_port); 192051078Speter 192151078Speter /* input event? (check first to help avoid overruns) */ 192251078Speter while (line_status & LSR_RCV_MASK) { 192351078Speter /* break/unnattached error bits or real input? */ 192451078Speter if (!(line_status & LSR_RXRDY)) 192551078Speter recv_data = 0; 192651078Speter else 192751078Speter recv_data = inb(com->data_port); 192861649Sps#if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER) 192961649Sps /* 193061649Sps * Solaris implements a new BREAK which is initiated 193161649Sps * by a character sequence CR ~ ^b which is similar 193261649Sps * to a familiar pattern used on Sun servers by the 193361649Sps * Remote Console. 193461649Sps */ 193561649Sps#define KEY_CRTLB 2 /* ^B */ 193661649Sps#define KEY_CR 13 /* CR '\r' */ 193761649Sps#define KEY_TILDE 126 /* ~ */ 193861649Sps 193961649Sps if (com->unit == comconsole) { 194061649Sps static int brk_state1 = 0, brk_state2 = 0; 194161649Sps if (recv_data == KEY_CR) { 194261649Sps brk_state1 = recv_data; 194361649Sps brk_state2 = 0; 194465557Sjasone } else if (brk_state1 == KEY_CR 194565557Sjasone && (recv_data == KEY_TILDE 194665557Sjasone || recv_data == KEY_CRTLB)) { 194761649Sps if (recv_data == KEY_TILDE) 194861649Sps brk_state2 = recv_data; 194965557Sjasone else if (brk_state2 == KEY_TILDE 195065557Sjasone && recv_data == KEY_CRTLB) { 195161649Sps breakpoint(); 195265557Sjasone brk_state1 = 0; 195365557Sjasone brk_state2 = 0; 195461649Sps goto cont; 195561649Sps } else 195661649Sps brk_state2 = 0; 195761649Sps } else 195861649Sps brk_state1 = 0; 195961649Sps } 196061649Sps#endif 196151078Speter if (line_status & (LSR_BI | LSR_FE | LSR_PE)) { 196251078Speter /* 196351078Speter * Don't store BI if IGNBRK or FE/PE if IGNPAR. 196451078Speter * Otherwise, push the work to a higher level 196551078Speter * (to handle PARMRK) if we're bypassing. 196651078Speter * Otherwise, convert BI/FE and PE+INPCK to 0. 196751078Speter * 196851078Speter * This makes bypassing work right in the 196951078Speter * usual "raw" case (IGNBRK set, and IGNPAR 197051078Speter * and INPCK clear). 197151078Speter * 197251078Speter * Note: BI together with FE/PE means just BI. 197351078Speter */ 197451078Speter if (line_status & LSR_BI) { 197551078Speter#if defined(DDB) && defined(BREAK_TO_DEBUGGER) 197651078Speter if (com->unit == comconsole) { 197751078Speter breakpoint(); 197851078Speter goto cont; 197951078Speter } 198051078Speter#endif 198151078Speter if (com->tp == NULL 198251078Speter || com->tp->t_iflag & IGNBRK) 198351078Speter goto cont; 198451078Speter } else { 198551078Speter if (com->tp == NULL 198651078Speter || com->tp->t_iflag & IGNPAR) 198751078Speter goto cont; 198851078Speter } 198951078Speter if (com->tp->t_state & TS_CAN_BYPASS_L_RINT 199051078Speter && (line_status & (LSR_BI | LSR_FE) 199151078Speter || com->tp->t_iflag & INPCK)) 199251078Speter recv_data = 0; 199351078Speter } 199451078Speter ++com->bytes_in; 199551078Speter if (com->hotchar != 0 && recv_data == com->hotchar) 199672238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 199751078Speter ioptr = com->iptr; 199851078Speter if (ioptr >= com->ibufend) 199951078Speter CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW); 200051078Speter else { 200151078Speter if (com->do_timestamp) 200251078Speter microtime(&com->timestamp); 200351078Speter ++com_events; 200472238Sjhb swi_sched(sio_slow_ih, SWI_DELAY); 200551078Speter#if 0 /* for testing input latency vs efficiency */ 200651078Speterif (com->iptr - com->ibuf == 8) 200772238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 200851078Speter#endif 200951078Speter ioptr[0] = recv_data; 201051078Speter ioptr[com->ierroff] = line_status; 201151078Speter com->iptr = ++ioptr; 201251078Speter if (ioptr == com->ihighwater 201351078Speter && com->state & CS_RTS_IFLOW) 201451078Speter outb(com->modem_ctl_port, 201551078Speter com->mcr_image &= ~MCR_RTS); 201651078Speter if (line_status & LSR_OE) 201751078Speter CE_RECORD(com, CE_OVERRUN); 201851078Speter } 201951078Spetercont: 202051078Speter /* 202151078Speter * "& 0x7F" is to avoid the gcc-1.40 generating a slow 202251078Speter * jump from the top of the loop to here 202351078Speter */ 202451078Speter line_status = inb(com->line_status_port) & 0x7F; 202551078Speter } 202651078Speter 202751078Speter /* modem status change? (always check before doing output) */ 202851078Speter modem_status = inb(com->modem_status_port); 202951078Speter if (modem_status != com->last_modem_status) { 203051078Speter if (com->do_dcd_timestamp 203151078Speter && !(com->last_modem_status & MSR_DCD) 203251078Speter && modem_status & MSR_DCD) 203351078Speter microtime(&com->dcd_timestamp); 203451078Speter 203551078Speter /* 203651078Speter * Schedule high level to handle DCD changes. Note 203751078Speter * that we don't use the delta bits anywhere. Some 203851078Speter * UARTs mess them up, and it's easy to remember the 203951078Speter * previous bits and calculate the delta. 204051078Speter */ 204151078Speter com->last_modem_status = modem_status; 204251078Speter if (!(com->state & CS_CHECKMSR)) { 204351078Speter com_events += LOTS_OF_EVENTS; 204451078Speter com->state |= CS_CHECKMSR; 204572238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 204651078Speter } 204751078Speter 204851078Speter /* handle CTS change immediately for crisp flow ctl */ 204951078Speter if (com->state & CS_CTS_OFLOW) { 205051078Speter if (modem_status & MSR_CTS) 205151078Speter com->state |= CS_ODEVREADY; 205251078Speter else 205351078Speter com->state &= ~CS_ODEVREADY; 205451078Speter } 205551078Speter } 205651078Speter 205751078Speter /* output queued and everything ready? */ 205851078Speter if (line_status & LSR_TXRDY 205951078Speter && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) { 206051078Speter ioptr = com->obufq.l_head; 206151078Speter if (com->tx_fifo_size > 1) { 206251078Speter u_int ocount; 206351078Speter 206451078Speter ocount = com->obufq.l_tail - ioptr; 206551078Speter if (ocount > com->tx_fifo_size) 206651078Speter ocount = com->tx_fifo_size; 206751078Speter com->bytes_out += ocount; 206851078Speter do 206951078Speter outb(com->data_port, *ioptr++); 207051078Speter while (--ocount != 0); 207151078Speter } else { 207251078Speter outb(com->data_port, *ioptr++); 207351078Speter ++com->bytes_out; 207451078Speter } 207551078Speter com->obufq.l_head = ioptr; 207651078Speter if (COM_IIR_TXRDYBUG(com->flags)) { 207751078Speter int_ctl_new = int_ctl | IER_ETXRDY; 207851078Speter } 207951078Speter if (ioptr >= com->obufq.l_tail) { 208051078Speter struct lbq *qp; 208151078Speter 208251078Speter qp = com->obufq.l_next; 208351078Speter qp->l_queued = FALSE; 208451078Speter qp = qp->l_next; 208551078Speter if (qp != NULL) { 208651078Speter com->obufq.l_head = qp->l_head; 208751078Speter com->obufq.l_tail = qp->l_tail; 208851078Speter com->obufq.l_next = qp; 208951078Speter } else { 209051078Speter /* output just completed */ 209153344Speter if (COM_IIR_TXRDYBUG(com->flags)) { 209251078Speter int_ctl_new = int_ctl & ~IER_ETXRDY; 209351078Speter } 209451078Speter com->state &= ~CS_BUSY; 209551078Speter } 209651078Speter if (!(com->state & CS_ODONE)) { 209751078Speter com_events += LOTS_OF_EVENTS; 209851078Speter com->state |= CS_ODONE; 209967551Sjhb /* handle at high level ASAP */ 210072238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 210151078Speter } 210251078Speter } 210353344Speter if (COM_IIR_TXRDYBUG(com->flags) && (int_ctl != int_ctl_new)) { 210451078Speter outb(com->intr_ctl_port, int_ctl_new); 210551078Speter } 210651078Speter } 210751078Speter 210851078Speter /* finished? */ 210951078Speter#ifndef COM_MULTIPORT 211051078Speter if ((inb(com->int_id_port) & IIR_IMASK) == IIR_NOPEND) 211151078Speter#endif /* COM_MULTIPORT */ 211251078Speter return; 211351078Speter } 211451078Speter} 211551078Speter 211651078Speterstatic int 211751078Spetersioioctl(dev, cmd, data, flag, p) 211851078Speter dev_t dev; 211951078Speter u_long cmd; 212051078Speter caddr_t data; 212151078Speter int flag; 212251078Speter struct proc *p; 212351078Speter{ 212451078Speter struct com_s *com; 212551078Speter int error; 212651078Speter int mynor; 212751078Speter int s; 212851078Speter struct tty *tp; 212951078Speter#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 213051078Speter u_long oldcmd; 213151078Speter struct termios term; 213251078Speter#endif 213351078Speter 213451078Speter mynor = minor(dev); 213551078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 213657915Simp if (com == NULL || com->gone) 213751078Speter return (ENODEV); 213851078Speter if (mynor & CONTROL_MASK) { 213951078Speter struct termios *ct; 214051078Speter 214151078Speter switch (mynor & CONTROL_MASK) { 214251078Speter case CONTROL_INIT_STATE: 214351078Speter ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in; 214451078Speter break; 214551078Speter case CONTROL_LOCK_STATE: 214651078Speter ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in; 214751078Speter break; 214851078Speter default: 214951078Speter return (ENODEV); /* /dev/nodev */ 215051078Speter } 215151078Speter switch (cmd) { 215251078Speter case TIOCSETA: 215351078Speter error = suser(p); 215451078Speter if (error != 0) 215551078Speter return (error); 215651078Speter *ct = *(struct termios *)data; 215751078Speter return (0); 215851078Speter case TIOCGETA: 215951078Speter *(struct termios *)data = *ct; 216051078Speter return (0); 216151078Speter case TIOCGETD: 216251078Speter *(int *)data = TTYDISC; 216351078Speter return (0); 216451078Speter case TIOCGWINSZ: 216551078Speter bzero(data, sizeof(struct winsize)); 216651078Speter return (0); 216751078Speter default: 216851078Speter return (ENOTTY); 216951078Speter } 217051078Speter } 217151078Speter tp = com->tp; 217251078Speter#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 217351078Speter term = tp->t_termios; 217451078Speter oldcmd = cmd; 217551078Speter error = ttsetcompat(tp, &cmd, data, &term); 217651078Speter if (error != 0) 217751078Speter return (error); 217851078Speter if (cmd != oldcmd) 217951078Speter data = (caddr_t)&term; 218051078Speter#endif 218151078Speter if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) { 218251078Speter int cc; 218351078Speter struct termios *dt = (struct termios *)data; 218451078Speter struct termios *lt = mynor & CALLOUT_MASK 218551078Speter ? &com->lt_out : &com->lt_in; 218651078Speter 218751078Speter dt->c_iflag = (tp->t_iflag & lt->c_iflag) 218851078Speter | (dt->c_iflag & ~lt->c_iflag); 218951078Speter dt->c_oflag = (tp->t_oflag & lt->c_oflag) 219051078Speter | (dt->c_oflag & ~lt->c_oflag); 219151078Speter dt->c_cflag = (tp->t_cflag & lt->c_cflag) 219251078Speter | (dt->c_cflag & ~lt->c_cflag); 219351078Speter dt->c_lflag = (tp->t_lflag & lt->c_lflag) 219451078Speter | (dt->c_lflag & ~lt->c_lflag); 219551078Speter for (cc = 0; cc < NCCS; ++cc) 219651078Speter if (lt->c_cc[cc] != 0) 219751078Speter dt->c_cc[cc] = tp->t_cc[cc]; 219851078Speter if (lt->c_ispeed != 0) 219951078Speter dt->c_ispeed = tp->t_ispeed; 220051078Speter if (lt->c_ospeed != 0) 220151078Speter dt->c_ospeed = tp->t_ospeed; 220251078Speter } 220351078Speter error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 220451078Speter if (error != ENOIOCTL) 220551078Speter return (error); 220651078Speter s = spltty(); 220751078Speter error = ttioctl(tp, cmd, data, flag); 220851078Speter disc_optim(tp, &tp->t_termios, com); 220951078Speter if (error != ENOIOCTL) { 221051078Speter splx(s); 221151078Speter return (error); 221251078Speter } 221351078Speter switch (cmd) { 221451078Speter case TIOCSBRK: 221560471Snyan sio_setreg(com, com_cfcr, com->cfcr_image |= CFCR_SBREAK); 221651078Speter break; 221751078Speter case TIOCCBRK: 221860471Snyan sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 221951078Speter break; 222051078Speter case TIOCSDTR: 222151078Speter (void)commctl(com, TIOCM_DTR, DMBIS); 222251078Speter break; 222351078Speter case TIOCCDTR: 222451078Speter (void)commctl(com, TIOCM_DTR, DMBIC); 222551078Speter break; 222651078Speter /* 222751078Speter * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The 222851078Speter * changes get undone on the next call to comparam(). 222951078Speter */ 223051078Speter case TIOCMSET: 223151078Speter (void)commctl(com, *(int *)data, DMSET); 223251078Speter break; 223351078Speter case TIOCMBIS: 223451078Speter (void)commctl(com, *(int *)data, DMBIS); 223551078Speter break; 223651078Speter case TIOCMBIC: 223751078Speter (void)commctl(com, *(int *)data, DMBIC); 223851078Speter break; 223951078Speter case TIOCMGET: 224051078Speter *(int *)data = commctl(com, 0, DMGET); 224151078Speter break; 224251078Speter case TIOCMSDTRWAIT: 224351078Speter /* must be root since the wait applies to following logins */ 224451078Speter error = suser(p); 224551078Speter if (error != 0) { 224651078Speter splx(s); 224751078Speter return (error); 224851078Speter } 224951078Speter com->dtr_wait = *(int *)data * hz / 100; 225051078Speter break; 225151078Speter case TIOCMGDTRWAIT: 225251078Speter *(int *)data = com->dtr_wait * 100 / hz; 225351078Speter break; 225451078Speter case TIOCTIMESTAMP: 225551078Speter com->do_timestamp = TRUE; 225651078Speter *(struct timeval *)data = com->timestamp; 225751078Speter break; 225851078Speter case TIOCDCDTIMESTAMP: 225951078Speter com->do_dcd_timestamp = TRUE; 226051078Speter *(struct timeval *)data = com->dcd_timestamp; 226151078Speter break; 226251078Speter default: 226351078Speter splx(s); 226451078Speter error = pps_ioctl(cmd, data, &com->pps); 226551078Speter if (error == ENODEV) 226651078Speter error = ENOTTY; 226751078Speter return (error); 226851078Speter } 226951078Speter splx(s); 227051078Speter return (0); 227151078Speter} 227251078Speter 227365557Sjasone/* software interrupt handler for SWI_TTY */ 227451078Speterstatic void 227567551Sjhbsiopoll(void *dummy) 227651078Speter{ 227751078Speter int unit; 227851078Speter 227951078Speter if (com_events == 0) 228051078Speter return; 228151078Speterrepeat: 228253344Speter for (unit = 0; unit < sio_numunits; ++unit) { 228351078Speter struct com_s *com; 228451078Speter int incc; 228551078Speter struct tty *tp; 228651078Speter 228751078Speter com = com_addr(unit); 228851078Speter if (com == NULL) 228951078Speter continue; 229051078Speter tp = com->tp; 229151078Speter if (tp == NULL || com->gone) { 229251078Speter /* 229351078Speter * Discard any events related to never-opened or 229451078Speter * going-away devices. 229551078Speter */ 229672200Sbmilekic mtx_lock_spin(&sio_lock); 229751078Speter incc = com->iptr - com->ibuf; 229851078Speter com->iptr = com->ibuf; 229951078Speter if (com->state & CS_CHECKMSR) { 230051078Speter incc += LOTS_OF_EVENTS; 230151078Speter com->state &= ~CS_CHECKMSR; 230251078Speter } 230351078Speter com_events -= incc; 230472200Sbmilekic mtx_unlock_spin(&sio_lock); 230551078Speter continue; 230651078Speter } 230751078Speter if (com->iptr != com->ibuf) { 230872200Sbmilekic mtx_lock_spin(&sio_lock); 230951078Speter sioinput(com); 231072200Sbmilekic mtx_unlock_spin(&sio_lock); 231151078Speter } 231251078Speter if (com->state & CS_CHECKMSR) { 231351078Speter u_char delta_modem_status; 231451078Speter 231572200Sbmilekic mtx_lock_spin(&sio_lock); 231651078Speter delta_modem_status = com->last_modem_status 231751078Speter ^ com->prev_modem_status; 231851078Speter com->prev_modem_status = com->last_modem_status; 231951078Speter com_events -= LOTS_OF_EVENTS; 232051078Speter com->state &= ~CS_CHECKMSR; 232172200Sbmilekic mtx_unlock_spin(&sio_lock); 232251078Speter if (delta_modem_status & MSR_DCD) 232351078Speter (*linesw[tp->t_line].l_modem) 232451078Speter (tp, com->prev_modem_status & MSR_DCD); 232551078Speter } 232651078Speter if (com->state & CS_ODONE) { 232772200Sbmilekic mtx_lock_spin(&sio_lock); 232851078Speter com_events -= LOTS_OF_EVENTS; 232951078Speter com->state &= ~CS_ODONE; 233072200Sbmilekic mtx_unlock_spin(&sio_lock); 233151078Speter if (!(com->state & CS_BUSY) 233251078Speter && !(com->extra_state & CSE_BUSYCHECK)) { 233351078Speter timeout(siobusycheck, com, hz / 100); 233451078Speter com->extra_state |= CSE_BUSYCHECK; 233551078Speter } 233651078Speter (*linesw[tp->t_line].l_start)(tp); 233751078Speter } 233851078Speter if (com_events == 0) 233951078Speter break; 234051078Speter } 234151078Speter if (com_events >= LOTS_OF_EVENTS) 234251078Speter goto repeat; 234351078Speter} 234451078Speter 234551078Speterstatic int 234651078Spetercomparam(tp, t) 234751078Speter struct tty *tp; 234851078Speter struct termios *t; 234951078Speter{ 235051078Speter u_int cfcr; 235151078Speter int cflag; 235251078Speter struct com_s *com; 235351078Speter int divisor; 235451078Speter u_char dlbh; 235551078Speter u_char dlbl; 235651078Speter int s; 235751078Speter int unit; 235851078Speter 235951078Speter /* do historical conversions */ 236051078Speter if (t->c_ispeed == 0) 236151078Speter t->c_ispeed = t->c_ospeed; 236251078Speter 236351078Speter /* check requested parameters */ 236451078Speter divisor = ttspeedtab(t->c_ospeed, comspeedtab); 236551078Speter if (divisor < 0 || (divisor > 0 && t->c_ispeed != t->c_ospeed)) 236651078Speter return (EINVAL); 236751078Speter 236851078Speter /* parameters are OK, convert them to the com struct and the device */ 236951078Speter unit = DEV_TO_UNIT(tp->t_dev); 237051078Speter com = com_addr(unit); 237157915Simp if (com == NULL) 237257915Simp return (ENODEV); 237351078Speter s = spltty(); 237451078Speter if (divisor == 0) 237551078Speter (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */ 237651078Speter else 237751078Speter (void)commctl(com, TIOCM_DTR, DMBIS); 237851078Speter cflag = t->c_cflag; 237951078Speter switch (cflag & CSIZE) { 238051078Speter case CS5: 238151078Speter cfcr = CFCR_5BITS; 238251078Speter break; 238351078Speter case CS6: 238451078Speter cfcr = CFCR_6BITS; 238551078Speter break; 238651078Speter case CS7: 238751078Speter cfcr = CFCR_7BITS; 238851078Speter break; 238951078Speter default: 239051078Speter cfcr = CFCR_8BITS; 239151078Speter break; 239251078Speter } 239351078Speter if (cflag & PARENB) { 239451078Speter cfcr |= CFCR_PENAB; 239551078Speter if (!(cflag & PARODD)) 239651078Speter cfcr |= CFCR_PEVEN; 239751078Speter } 239851078Speter if (cflag & CSTOPB) 239951078Speter cfcr |= CFCR_STOPB; 240051078Speter 240151078Speter if (com->hasfifo && divisor != 0) { 240251078Speter /* 240351078Speter * Use a fifo trigger level low enough so that the input 240451078Speter * latency from the fifo is less than about 16 msec and 240551078Speter * the total latency is less than about 30 msec. These 240651078Speter * latencies are reasonable for humans. Serial comms 240751078Speter * protocols shouldn't expect anything better since modem 240851078Speter * latencies are larger. 240951078Speter */ 241051078Speter com->fifo_image = t->c_ospeed <= 4800 241151078Speter ? FIFO_ENABLE : FIFO_ENABLE | FIFO_RX_HIGH; 241251078Speter#ifdef COM_ESP 241351078Speter /* 241451078Speter * The Hayes ESP card needs the fifo DMA mode bit set 241551078Speter * in compatibility mode. If not, it will interrupt 241651078Speter * for each character received. 241751078Speter */ 241851078Speter if (com->esp) 241951078Speter com->fifo_image |= FIFO_DMA_MODE; 242051078Speter#endif 242160471Snyan sio_setreg(com, com_fifo, com->fifo_image); 242251078Speter } 242351078Speter 242465605Sjhb /* 242565605Sjhb * This returns with interrupts disabled so that we can complete 242665605Sjhb * the speed change atomically. Keeping interrupts disabled is 242765605Sjhb * especially important while com_data is hidden. 242865605Sjhb */ 242965605Sjhb (void) siosetwater(com, t->c_ispeed); 243065557Sjasone 243151078Speter if (divisor != 0) { 243260471Snyan sio_setreg(com, com_cfcr, cfcr | CFCR_DLAB); 243351078Speter /* 243451078Speter * Only set the divisor registers if they would change, 243551078Speter * since on some 16550 incompatibles (UMC8669F), setting 243651078Speter * them while input is arriving them loses sync until 243751078Speter * data stops arriving. 243851078Speter */ 243951078Speter dlbl = divisor & 0xFF; 244060471Snyan if (sio_getreg(com, com_dlbl) != dlbl) 244160471Snyan sio_setreg(com, com_dlbl, dlbl); 244251078Speter dlbh = (u_int) divisor >> 8; 244360471Snyan if (sio_getreg(com, com_dlbh) != dlbh) 244460471Snyan sio_setreg(com, com_dlbh, dlbh); 244551078Speter } 244651078Speter 244760471Snyan sio_setreg(com, com_cfcr, com->cfcr_image = cfcr); 244851078Speter 244951078Speter if (!(tp->t_state & TS_TTSTOP)) 245051078Speter com->state |= CS_TTGO; 245151078Speter 245251078Speter if (cflag & CRTS_IFLOW) { 245351078Speter if (com->st16650a) { 245460471Snyan sio_setreg(com, com_cfcr, 0xbf); 245560471Snyan sio_setreg(com, com_fifo, 245660471Snyan sio_getreg(com, com_fifo) | 0x40); 245751078Speter } 245851078Speter com->state |= CS_RTS_IFLOW; 245951078Speter /* 246051078Speter * If CS_RTS_IFLOW just changed from off to on, the change 246151078Speter * needs to be propagated to MCR_RTS. This isn't urgent, 246251078Speter * so do it later by calling comstart() instead of repeating 246351078Speter * a lot of code from comstart() here. 246451078Speter */ 246551078Speter } else if (com->state & CS_RTS_IFLOW) { 246651078Speter com->state &= ~CS_RTS_IFLOW; 246751078Speter /* 246851078Speter * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS 246951078Speter * on here, since comstart() won't do it later. 247051078Speter */ 247151078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 247251078Speter if (com->st16650a) { 247360471Snyan sio_setreg(com, com_cfcr, 0xbf); 247460471Snyan sio_setreg(com, com_fifo, 247560471Snyan sio_getreg(com, com_fifo) & ~0x40); 247651078Speter } 247751078Speter } 247851078Speter 247951078Speter 248051078Speter /* 248151078Speter * Set up state to handle output flow control. 248251078Speter * XXX - worth handling MDMBUF (DCD) flow control at the lowest level? 248351078Speter * Now has 10+ msec latency, while CTS flow has 50- usec latency. 248451078Speter */ 248551078Speter com->state |= CS_ODEVREADY; 248651078Speter com->state &= ~CS_CTS_OFLOW; 248751078Speter if (cflag & CCTS_OFLOW) { 248851078Speter com->state |= CS_CTS_OFLOW; 248951078Speter if (!(com->last_modem_status & MSR_CTS)) 249051078Speter com->state &= ~CS_ODEVREADY; 249151078Speter if (com->st16650a) { 249260471Snyan sio_setreg(com, com_cfcr, 0xbf); 249360471Snyan sio_setreg(com, com_fifo, 249460471Snyan sio_getreg(com, com_fifo) | 0x80); 249551078Speter } 249651078Speter } else { 249751078Speter if (com->st16650a) { 249860471Snyan sio_setreg(com, com_cfcr, 0xbf); 249960471Snyan sio_setreg(com, com_fifo, 250060471Snyan sio_getreg(com, com_fifo) & ~0x80); 250151078Speter } 250251078Speter } 250351078Speter 250460471Snyan sio_setreg(com, com_cfcr, com->cfcr_image); 250551078Speter 250651078Speter /* XXX shouldn't call functions while intrs are disabled. */ 250751078Speter disc_optim(tp, t, com); 250851078Speter /* 250951078Speter * Recover from fiddling with CS_TTGO. We used to call siointr1() 251051078Speter * unconditionally, but that defeated the careful discarding of 251151078Speter * stale input in sioopen(). 251251078Speter */ 251351078Speter if (com->state >= (CS_BUSY | CS_TTGO)) 251451078Speter siointr1(com); 251551078Speter 251672200Sbmilekic mtx_unlock_spin(&sio_lock); 251751078Speter splx(s); 251851078Speter comstart(tp); 251951078Speter if (com->ibufold != NULL) { 252051078Speter free(com->ibufold, M_DEVBUF); 252151078Speter com->ibufold = NULL; 252251078Speter } 252351078Speter return (0); 252451078Speter} 252551078Speter 252665605Sjhb/* 252770174Sjhb * This function must be called with the sio_lock mutex released and will 252870174Sjhb * return with it obtained. 252965605Sjhb */ 253051078Speterstatic int 253165605Sjhbsiosetwater(com, speed) 253251078Speter struct com_s *com; 253351078Speter speed_t speed; 253451078Speter{ 253551078Speter int cp4ticks; 253651078Speter u_char *ibuf; 253751078Speter int ibufsize; 253851078Speter struct tty *tp; 253951078Speter 254051078Speter /* 254151078Speter * Make the buffer size large enough to handle a softtty interrupt 254251078Speter * latency of about 2 ticks without loss of throughput or data 254351078Speter * (about 3 ticks if input flow control is not used or not honoured, 254451078Speter * but a bit less for CS5-CS7 modes). 254551078Speter */ 254651078Speter cp4ticks = speed / 10 / hz * 4; 254751078Speter for (ibufsize = 128; ibufsize < cp4ticks;) 254851078Speter ibufsize <<= 1; 254965605Sjhb if (ibufsize == com->ibufsize) { 255072200Sbmilekic mtx_lock_spin(&sio_lock); 255151078Speter return (0); 255265605Sjhb } 255351078Speter 255451078Speter /* 255551078Speter * Allocate input buffer. The extra factor of 2 in the size is 255651078Speter * to allow for an error byte for each input byte. 255751078Speter */ 255851078Speter ibuf = malloc(2 * ibufsize, M_DEVBUF, M_NOWAIT); 255965605Sjhb if (ibuf == NULL) { 256072200Sbmilekic mtx_lock_spin(&sio_lock); 256151078Speter return (ENOMEM); 256265605Sjhb } 256351078Speter 256451078Speter /* Initialize non-critical variables. */ 256551078Speter com->ibufold = com->ibuf; 256651078Speter com->ibufsize = ibufsize; 256751078Speter tp = com->tp; 256851078Speter if (tp != NULL) { 256951078Speter tp->t_ififosize = 2 * ibufsize; 257051078Speter tp->t_ispeedwat = (speed_t)-1; 257151078Speter tp->t_ospeedwat = (speed_t)-1; 257251078Speter } 257351078Speter 257451078Speter /* 257551078Speter * Read current input buffer, if any. Continue with interrupts 257651078Speter * disabled. 257751078Speter */ 257872200Sbmilekic mtx_lock_spin(&sio_lock); 257951078Speter if (com->iptr != com->ibuf) 258051078Speter sioinput(com); 258151078Speter 258251078Speter /*- 258351078Speter * Initialize critical variables, including input buffer watermarks. 258451078Speter * The external device is asked to stop sending when the buffer 258551078Speter * exactly reaches high water, or when the high level requests it. 258651078Speter * The high level is notified immediately (rather than at a later 258751078Speter * clock tick) when this watermark is reached. 258851078Speter * The buffer size is chosen so the watermark should almost never 258951078Speter * be reached. 259051078Speter * The low watermark is invisibly 0 since the buffer is always 259151078Speter * emptied all at once. 259251078Speter */ 259351078Speter com->iptr = com->ibuf = ibuf; 259451078Speter com->ibufend = ibuf + ibufsize; 259551078Speter com->ierroff = ibufsize; 259651078Speter com->ihighwater = ibuf + 3 * ibufsize / 4; 259751078Speter return (0); 259851078Speter} 259951078Speter 260051078Speterstatic void 260151078Spetercomstart(tp) 260251078Speter struct tty *tp; 260351078Speter{ 260451078Speter struct com_s *com; 260551078Speter int s; 260651078Speter int unit; 260751078Speter 260851078Speter unit = DEV_TO_UNIT(tp->t_dev); 260951078Speter com = com_addr(unit); 261057915Simp if (com == NULL) 261157915Simp return; 261251078Speter s = spltty(); 261372200Sbmilekic mtx_lock_spin(&sio_lock); 261451078Speter if (tp->t_state & TS_TTSTOP) 261551078Speter com->state &= ~CS_TTGO; 261651078Speter else 261751078Speter com->state |= CS_TTGO; 261851078Speter if (tp->t_state & TS_TBLOCK) { 261951078Speter if (com->mcr_image & MCR_RTS && com->state & CS_RTS_IFLOW) 262051078Speter outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS); 262151078Speter } else { 262251078Speter if (!(com->mcr_image & MCR_RTS) && com->iptr < com->ihighwater 262351078Speter && com->state & CS_RTS_IFLOW) 262451078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 262551078Speter } 262672200Sbmilekic mtx_unlock_spin(&sio_lock); 262751078Speter if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) { 262851078Speter ttwwakeup(tp); 262951078Speter splx(s); 263051078Speter return; 263151078Speter } 263251078Speter if (tp->t_outq.c_cc != 0) { 263351078Speter struct lbq *qp; 263451078Speter struct lbq *next; 263551078Speter 263651078Speter if (!com->obufs[0].l_queued) { 263751078Speter com->obufs[0].l_tail 263851078Speter = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1, 263951078Speter sizeof com->obuf1); 264051078Speter com->obufs[0].l_next = NULL; 264151078Speter com->obufs[0].l_queued = TRUE; 264272200Sbmilekic mtx_lock_spin(&sio_lock); 264351078Speter if (com->state & CS_BUSY) { 264451078Speter qp = com->obufq.l_next; 264551078Speter while ((next = qp->l_next) != NULL) 264651078Speter qp = next; 264751078Speter qp->l_next = &com->obufs[0]; 264851078Speter } else { 264951078Speter com->obufq.l_head = com->obufs[0].l_head; 265051078Speter com->obufq.l_tail = com->obufs[0].l_tail; 265151078Speter com->obufq.l_next = &com->obufs[0]; 265251078Speter com->state |= CS_BUSY; 265351078Speter } 265472200Sbmilekic mtx_unlock_spin(&sio_lock); 265551078Speter } 265651078Speter if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) { 265751078Speter com->obufs[1].l_tail 265851078Speter = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2, 265951078Speter sizeof com->obuf2); 266051078Speter com->obufs[1].l_next = NULL; 266151078Speter com->obufs[1].l_queued = TRUE; 266272200Sbmilekic mtx_lock_spin(&sio_lock); 266351078Speter if (com->state & CS_BUSY) { 266451078Speter qp = com->obufq.l_next; 266551078Speter while ((next = qp->l_next) != NULL) 266651078Speter qp = next; 266751078Speter qp->l_next = &com->obufs[1]; 266851078Speter } else { 266951078Speter com->obufq.l_head = com->obufs[1].l_head; 267051078Speter com->obufq.l_tail = com->obufs[1].l_tail; 267151078Speter com->obufq.l_next = &com->obufs[1]; 267251078Speter com->state |= CS_BUSY; 267351078Speter } 267472200Sbmilekic mtx_unlock_spin(&sio_lock); 267551078Speter } 267651078Speter tp->t_state |= TS_BUSY; 267751078Speter } 267872200Sbmilekic mtx_lock_spin(&sio_lock); 267951078Speter if (com->state >= (CS_BUSY | CS_TTGO)) 268051078Speter siointr1(com); /* fake interrupt to start output */ 268172200Sbmilekic mtx_unlock_spin(&sio_lock); 268251078Speter ttwwakeup(tp); 268351078Speter splx(s); 268451078Speter} 268551078Speter 268651078Speterstatic void 268751654Sphkcomstop(tp, rw) 268851078Speter struct tty *tp; 268951078Speter int rw; 269051078Speter{ 269151078Speter struct com_s *com; 269251078Speter 269351078Speter com = com_addr(DEV_TO_UNIT(tp->t_dev)); 269457915Simp if (com == NULL || com->gone) 269551078Speter return; 269672200Sbmilekic mtx_lock_spin(&sio_lock); 269751078Speter if (rw & FWRITE) { 269851078Speter if (com->hasfifo) 269951078Speter#ifdef COM_ESP 270051078Speter /* XXX avoid h/w bug. */ 270151078Speter if (!com->esp) 270251078Speter#endif 270360471Snyan sio_setreg(com, com_fifo, 270460471Snyan FIFO_XMT_RST | com->fifo_image); 270551078Speter com->obufs[0].l_queued = FALSE; 270651078Speter com->obufs[1].l_queued = FALSE; 270751078Speter if (com->state & CS_ODONE) 270851078Speter com_events -= LOTS_OF_EVENTS; 270951078Speter com->state &= ~(CS_ODONE | CS_BUSY); 271051078Speter com->tp->t_state &= ~TS_BUSY; 271151078Speter } 271251078Speter if (rw & FREAD) { 271351078Speter if (com->hasfifo) 271451078Speter#ifdef COM_ESP 271551078Speter /* XXX avoid h/w bug. */ 271651078Speter if (!com->esp) 271751078Speter#endif 271860471Snyan sio_setreg(com, com_fifo, 271960471Snyan FIFO_RCV_RST | com->fifo_image); 272051078Speter com_events -= (com->iptr - com->ibuf); 272151078Speter com->iptr = com->ibuf; 272251078Speter } 272372200Sbmilekic mtx_unlock_spin(&sio_lock); 272451078Speter comstart(tp); 272551078Speter} 272651078Speter 272751078Speterstatic int 272851078Spetercommctl(com, bits, how) 272951078Speter struct com_s *com; 273051078Speter int bits; 273151078Speter int how; 273251078Speter{ 273351078Speter int mcr; 273451078Speter int msr; 273551078Speter 273651078Speter if (how == DMGET) { 273751078Speter bits = TIOCM_LE; /* XXX - always enabled while open */ 273851078Speter mcr = com->mcr_image; 273951078Speter if (mcr & MCR_DTR) 274051078Speter bits |= TIOCM_DTR; 274151078Speter if (mcr & MCR_RTS) 274251078Speter bits |= TIOCM_RTS; 274351078Speter msr = com->prev_modem_status; 274451078Speter if (msr & MSR_CTS) 274551078Speter bits |= TIOCM_CTS; 274651078Speter if (msr & MSR_DCD) 274751078Speter bits |= TIOCM_CD; 274851078Speter if (msr & MSR_DSR) 274951078Speter bits |= TIOCM_DSR; 275051078Speter /* 275151078Speter * XXX - MSR_RI is naturally volatile, and we make MSR_TERI 275251078Speter * more volatile by reading the modem status a lot. Perhaps 275351078Speter * we should latch both bits until the status is read here. 275451078Speter */ 275551078Speter if (msr & (MSR_RI | MSR_TERI)) 275651078Speter bits |= TIOCM_RI; 275751078Speter return (bits); 275851078Speter } 275951078Speter mcr = 0; 276051078Speter if (bits & TIOCM_DTR) 276151078Speter mcr |= MCR_DTR; 276251078Speter if (bits & TIOCM_RTS) 276351078Speter mcr |= MCR_RTS; 276451078Speter if (com->gone) 276551078Speter return(0); 276672200Sbmilekic mtx_lock_spin(&sio_lock); 276751078Speter switch (how) { 276851078Speter case DMSET: 276951078Speter outb(com->modem_ctl_port, 277051078Speter com->mcr_image = mcr | (com->mcr_image & MCR_IENABLE)); 277151078Speter break; 277251078Speter case DMBIS: 277351078Speter outb(com->modem_ctl_port, com->mcr_image |= mcr); 277451078Speter break; 277551078Speter case DMBIC: 277651078Speter outb(com->modem_ctl_port, com->mcr_image &= ~mcr); 277751078Speter break; 277851078Speter } 277972200Sbmilekic mtx_unlock_spin(&sio_lock); 278051078Speter return (0); 278151078Speter} 278251078Speter 278351078Speterstatic void 278451078Spetersiosettimeout() 278551078Speter{ 278651078Speter struct com_s *com; 278751078Speter bool_t someopen; 278851078Speter int unit; 278951078Speter 279051078Speter /* 279151078Speter * Set our timeout period to 1 second if no polled devices are open. 279251078Speter * Otherwise set it to max(1/200, 1/hz). 279351078Speter * Enable timeouts iff some device is open. 279451078Speter */ 279551078Speter untimeout(comwakeup, (void *)NULL, sio_timeout_handle); 279651078Speter sio_timeout = hz; 279751078Speter someopen = FALSE; 279853344Speter for (unit = 0; unit < sio_numunits; ++unit) { 279951078Speter com = com_addr(unit); 280051078Speter if (com != NULL && com->tp != NULL 280151078Speter && com->tp->t_state & TS_ISOPEN && !com->gone) { 280251078Speter someopen = TRUE; 280351078Speter if (com->poll || com->poll_output) { 280451078Speter sio_timeout = hz > 200 ? hz / 200 : 1; 280551078Speter break; 280651078Speter } 280751078Speter } 280851078Speter } 280951078Speter if (someopen) { 281051078Speter sio_timeouts_until_log = hz / sio_timeout; 281151078Speter sio_timeout_handle = timeout(comwakeup, (void *)NULL, 281251078Speter sio_timeout); 281351078Speter } else { 281451078Speter /* Flush error messages, if any. */ 281551078Speter sio_timeouts_until_log = 1; 281651078Speter comwakeup((void *)NULL); 281751078Speter untimeout(comwakeup, (void *)NULL, sio_timeout_handle); 281851078Speter } 281951078Speter} 282051078Speter 282151078Speterstatic void 282251078Spetercomwakeup(chan) 282351078Speter void *chan; 282451078Speter{ 282551078Speter struct com_s *com; 282651078Speter int unit; 282751078Speter 282851078Speter sio_timeout_handle = timeout(comwakeup, (void *)NULL, sio_timeout); 282951078Speter 283051078Speter /* 283151078Speter * Recover from lost output interrupts. 283251078Speter * Poll any lines that don't use interrupts. 283351078Speter */ 283453344Speter for (unit = 0; unit < sio_numunits; ++unit) { 283551078Speter com = com_addr(unit); 283651078Speter if (com != NULL && !com->gone 283751078Speter && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) { 283872200Sbmilekic mtx_lock_spin(&sio_lock); 283951078Speter siointr1(com); 284072200Sbmilekic mtx_unlock_spin(&sio_lock); 284151078Speter } 284251078Speter } 284351078Speter 284451078Speter /* 284551078Speter * Check for and log errors, but not too often. 284651078Speter */ 284751078Speter if (--sio_timeouts_until_log > 0) 284851078Speter return; 284951078Speter sio_timeouts_until_log = hz / sio_timeout; 285053344Speter for (unit = 0; unit < sio_numunits; ++unit) { 285151078Speter int errnum; 285251078Speter 285351078Speter com = com_addr(unit); 285451078Speter if (com == NULL) 285551078Speter continue; 285651078Speter if (com->gone) 285751078Speter continue; 285851078Speter for (errnum = 0; errnum < CE_NTYPES; ++errnum) { 285951078Speter u_int delta; 286051078Speter u_long total; 286151078Speter 286272200Sbmilekic mtx_lock_spin(&sio_lock); 286351078Speter delta = com->delta_error_counts[errnum]; 286451078Speter com->delta_error_counts[errnum] = 0; 286572200Sbmilekic mtx_unlock_spin(&sio_lock); 286651078Speter if (delta == 0) 286751078Speter continue; 286851078Speter total = com->error_counts[errnum] += delta; 286951078Speter log(LOG_ERR, "sio%d: %u more %s%s (total %lu)\n", 287051078Speter unit, delta, error_desc[errnum], 287151078Speter delta == 1 ? "" : "s", total); 287251078Speter } 287351078Speter } 287451078Speter} 287551078Speter 287651078Speterstatic void 287751078Speterdisc_optim(tp, t, com) 287851078Speter struct tty *tp; 287951078Speter struct termios *t; 288051078Speter struct com_s *com; 288151078Speter{ 288251078Speter if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) 288351078Speter && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) 288451078Speter && (!(t->c_iflag & PARMRK) 288551078Speter || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) 288651078Speter && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) 288751078Speter && linesw[tp->t_line].l_rint == ttyinput) 288851078Speter tp->t_state |= TS_CAN_BYPASS_L_RINT; 288951078Speter else 289051078Speter tp->t_state &= ~TS_CAN_BYPASS_L_RINT; 289151078Speter com->hotchar = linesw[tp->t_line].l_hotchar; 289251078Speter} 289351078Speter 289451078Speter/* 289551078Speter * Following are all routines needed for SIO to act as console 289651078Speter */ 289751078Speter#include <sys/cons.h> 289851078Speter 289951078Speterstruct siocnstate { 290051078Speter u_char dlbl; 290151078Speter u_char dlbh; 290251078Speter u_char ier; 290351078Speter u_char cfcr; 290451078Speter u_char mcr; 290551078Speter}; 290651078Speter 290766230Sjhb#ifndef __alpha__ 290851078Speterstatic speed_t siocngetspeed __P((Port_t, struct speedtab *)); 290966230Sjhb#endif 291051078Speterstatic void siocnclose __P((struct siocnstate *sp, Port_t iobase)); 291151078Speterstatic void siocnopen __P((struct siocnstate *sp, Port_t iobase, int speed)); 291251078Speterstatic void siocntxwait __P((Port_t iobase)); 291351078Speter 291466230Sjhb#ifdef __alpha__ 291566230Sjhbint siocnattach __P((int port, int speed)); 291666230Sjhbint siogdbattach __P((int port, int speed)); 291766230Sjhbint siogdbgetc __P((void)); 291866230Sjhbvoid siogdbputc __P((int c)); 291966230Sjhb#else 292051078Speterstatic cn_probe_t siocnprobe; 292151078Speterstatic cn_init_t siocninit; 292266230Sjhb#endif 292351078Speterstatic cn_checkc_t siocncheckc; 292451078Speterstatic cn_getc_t siocngetc; 292551078Speterstatic cn_putc_t siocnputc; 292651078Speter 292751078Speter#ifdef __i386__ 292855823SyokotaCONS_DRIVER(sio, siocnprobe, siocninit, NULL, siocngetc, siocncheckc, 292955823Syokota siocnputc, NULL); 293051078Speter#endif 293151078Speter 293251078Speter/* To get the GDB related variables */ 293351078Speter#if DDB > 0 293451078Speter#include <ddb/ddb.h> 293551078Speter#endif 293651078Speter 293751078Speterstatic void 293851078Spetersiocntxwait(iobase) 293951078Speter Port_t iobase; 294051078Speter{ 294151078Speter int timo; 294251078Speter 294351078Speter /* 294451078Speter * Wait for any pending transmission to finish. Required to avoid 294551078Speter * the UART lockup bug when the speed is changed, and for normal 294651078Speter * transmits. 294751078Speter */ 294851078Speter timo = 100000; 294951078Speter while ((inb(iobase + com_lsr) & (LSR_TSRE | LSR_TXRDY)) 295051078Speter != (LSR_TSRE | LSR_TXRDY) && --timo != 0) 295151078Speter ; 295251078Speter} 295351078Speter 295466230Sjhb#ifndef __alpha__ 295566230Sjhb 295651078Speter/* 295751078Speter * Read the serial port specified and try to figure out what speed 295851078Speter * it's currently running at. We're assuming the serial port has 295951078Speter * been initialized and is basicly idle. This routine is only intended 296051078Speter * to be run at system startup. 296151078Speter * 296251078Speter * If the value read from the serial port doesn't make sense, return 0. 296351078Speter */ 296451078Speter 296551078Speterstatic speed_t 296651078Spetersiocngetspeed(iobase, table) 296751078Speter Port_t iobase; 296851078Speter struct speedtab *table; 296951078Speter{ 297051078Speter int code; 297151078Speter u_char dlbh; 297251078Speter u_char dlbl; 297351078Speter u_char cfcr; 297451078Speter 297551078Speter cfcr = inb(iobase + com_cfcr); 297651078Speter outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 297751078Speter 297851078Speter dlbl = inb(iobase + com_dlbl); 297951078Speter dlbh = inb(iobase + com_dlbh); 298051078Speter 298151078Speter outb(iobase + com_cfcr, cfcr); 298251078Speter 298351078Speter code = dlbh << 8 | dlbl; 298451078Speter 298553344Speter for (; table->sp_speed != -1; table++) 298651078Speter if (table->sp_code == code) 298751078Speter return (table->sp_speed); 298851078Speter 298958885Simp return (0); /* didn't match anything sane */ 299051078Speter} 299151078Speter 299266230Sjhb#endif 299366230Sjhb 299451078Speterstatic void 299551078Spetersiocnopen(sp, iobase, speed) 299651078Speter struct siocnstate *sp; 299751078Speter Port_t iobase; 299851078Speter int speed; 299951078Speter{ 300051078Speter int divisor; 300151078Speter u_char dlbh; 300251078Speter u_char dlbl; 300351078Speter 300451078Speter /* 300551078Speter * Save all the device control registers except the fifo register 300651078Speter * and set our default ones (cs8 -parenb speed=comdefaultrate). 300751078Speter * We can't save the fifo register since it is read-only. 300851078Speter */ 300951078Speter sp->ier = inb(iobase + com_ier); 301051078Speter outb(iobase + com_ier, 0); /* spltty() doesn't stop siointr() */ 301151078Speter siocntxwait(iobase); 301251078Speter sp->cfcr = inb(iobase + com_cfcr); 301351078Speter outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 301451078Speter sp->dlbl = inb(iobase + com_dlbl); 301551078Speter sp->dlbh = inb(iobase + com_dlbh); 301651078Speter /* 301751078Speter * Only set the divisor registers if they would change, since on 301851078Speter * some 16550 incompatibles (Startech), setting them clears the 301951078Speter * data input register. This also reduces the effects of the 302051078Speter * UMC8669F bug. 302151078Speter */ 302251078Speter divisor = ttspeedtab(speed, comspeedtab); 302351078Speter dlbl = divisor & 0xFF; 302451078Speter if (sp->dlbl != dlbl) 302551078Speter outb(iobase + com_dlbl, dlbl); 302651078Speter dlbh = (u_int) divisor >> 8; 302751078Speter if (sp->dlbh != dlbh) 302851078Speter outb(iobase + com_dlbh, dlbh); 302951078Speter outb(iobase + com_cfcr, CFCR_8BITS); 303051078Speter sp->mcr = inb(iobase + com_mcr); 303151078Speter /* 303251078Speter * We don't want interrupts, but must be careful not to "disable" 303351078Speter * them by clearing the MCR_IENABLE bit, since that might cause 303451078Speter * an interrupt by floating the IRQ line. 303551078Speter */ 303651078Speter outb(iobase + com_mcr, (sp->mcr & MCR_IENABLE) | MCR_DTR | MCR_RTS); 303751078Speter} 303851078Speter 303951078Speterstatic void 304051078Spetersiocnclose(sp, iobase) 304151078Speter struct siocnstate *sp; 304251078Speter Port_t iobase; 304351078Speter{ 304451078Speter /* 304551078Speter * Restore the device control registers. 304651078Speter */ 304751078Speter siocntxwait(iobase); 304851078Speter outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 304951078Speter if (sp->dlbl != inb(iobase + com_dlbl)) 305051078Speter outb(iobase + com_dlbl, sp->dlbl); 305151078Speter if (sp->dlbh != inb(iobase + com_dlbh)) 305251078Speter outb(iobase + com_dlbh, sp->dlbh); 305351078Speter outb(iobase + com_cfcr, sp->cfcr); 305451078Speter /* 305551078Speter * XXX damp oscillations of MCR_DTR and MCR_RTS by not restoring them. 305651078Speter */ 305751078Speter outb(iobase + com_mcr, sp->mcr | MCR_DTR | MCR_RTS); 305851078Speter outb(iobase + com_ier, sp->ier); 305951078Speter} 306051078Speter 306166230Sjhb#ifndef __alpha__ 306266230Sjhb 306351078Speterstatic void 306451078Spetersiocnprobe(cp) 306551078Speter struct consdev *cp; 306651078Speter{ 306751078Speter speed_t boot_speed; 306851078Speter u_char cfcr; 306951078Speter int s, unit; 307051078Speter struct siocnstate sp; 307151078Speter 307251078Speter /* 307351078Speter * Find our first enabled console, if any. If it is a high-level 307451078Speter * console device, then initialize it and return successfully. 307551078Speter * If it is a low-level console device, then initialize it and 307651078Speter * return unsuccessfully. It must be initialized in both cases 307751078Speter * for early use by console drivers and debuggers. Initializing 307851078Speter * the hardware is not necessary in all cases, since the i/o 307951078Speter * routines initialize it on the fly, but it is necessary if 308051078Speter * input might arrive while the hardware is switched back to an 308151078Speter * uninitialized state. We can't handle multiple console devices 308251078Speter * yet because our low-level routines don't take a device arg. 308351078Speter * We trust the user to set the console flags properly so that we 308451078Speter * don't need to probe. 308551078Speter */ 308651078Speter cp->cn_pri = CN_DEAD; 308751078Speter 308851078Speter for (unit = 0; unit < 16; unit++) { /* XXX need to know how many */ 308951078Speter int flags; 309051078Speter int disabled; 309151078Speter if (resource_int_value("sio", unit, "disabled", &disabled) == 0) { 309251078Speter if (disabled) 309351078Speter continue; 309451078Speter } 309551078Speter if (resource_int_value("sio", unit, "flags", &flags)) 309651078Speter continue; 309751078Speter if (COM_CONSOLE(flags) || COM_DEBUGGER(flags)) { 309851078Speter int port; 309951078Speter Port_t iobase; 310051078Speter 310151078Speter if (resource_int_value("sio", unit, "port", &port)) 310251078Speter continue; 310351078Speter iobase = port; 310451078Speter s = spltty(); 310551078Speter if (boothowto & RB_SERIAL) { 310651078Speter boot_speed = siocngetspeed(iobase, comspeedtab); 310751078Speter if (boot_speed) 310851078Speter comdefaultrate = boot_speed; 310951078Speter } 311051078Speter 311151078Speter /* 311251078Speter * Initialize the divisor latch. We can't rely on 311351078Speter * siocnopen() to do this the first time, since it 311451078Speter * avoids writing to the latch if the latch appears 311551078Speter * to have the correct value. Also, if we didn't 311651078Speter * just read the speed from the hardware, then we 311751078Speter * need to set the speed in hardware so that 311851078Speter * switching it later is null. 311951078Speter */ 312051078Speter cfcr = inb(iobase + com_cfcr); 312151078Speter outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 312251078Speter outb(iobase + com_dlbl, 312351078Speter COMBRD(comdefaultrate) & 0xff); 312451078Speter outb(iobase + com_dlbh, 312551078Speter (u_int) COMBRD(comdefaultrate) >> 8); 312651078Speter outb(iobase + com_cfcr, cfcr); 312751078Speter 312851078Speter siocnopen(&sp, iobase, comdefaultrate); 312951078Speter 313051078Speter splx(s); 313151078Speter if (COM_CONSOLE(flags) && !COM_LLCONSOLE(flags)) { 313251078Speter cp->cn_dev = makedev(CDEV_MAJOR, unit); 313351078Speter cp->cn_pri = COM_FORCECONSOLE(flags) 313451078Speter || boothowto & RB_SERIAL 313551078Speter ? CN_REMOTE : CN_NORMAL; 313651078Speter siocniobase = iobase; 313751078Speter siocnunit = unit; 313851078Speter } 313951078Speter if (COM_DEBUGGER(flags)) { 314051078Speter printf("sio%d: gdb debugging port\n", unit); 314151078Speter siogdbiobase = iobase; 314251078Speter siogdbunit = unit; 314351078Speter#if DDB > 0 314451078Speter gdbdev = makedev(CDEV_MAJOR, unit); 314551078Speter gdb_getc = siocngetc; 314651078Speter gdb_putc = siocnputc; 314751078Speter#endif 314851078Speter } 314951078Speter } 315051078Speter } 315151078Speter#ifdef __i386__ 315251078Speter#if DDB > 0 315351078Speter /* 315451078Speter * XXX Ugly Compatability. 315551078Speter * If no gdb port has been specified, set it to be the console 315651078Speter * as some configuration files don't specify the gdb port. 315751078Speter */ 315851078Speter if (gdbdev == NODEV && (boothowto & RB_GDB)) { 315951078Speter printf("Warning: no GDB port specified. Defaulting to sio%d.\n", 316051078Speter siocnunit); 316151078Speter printf("Set flag 0x80 on desired GDB port in your\n"); 316251078Speter printf("configuration file (currently sio only).\n"); 316351078Speter siogdbiobase = siocniobase; 316451078Speter siogdbunit = siocnunit; 316551078Speter gdbdev = makedev(CDEV_MAJOR, siocnunit); 316651078Speter gdb_getc = siocngetc; 316751078Speter gdb_putc = siocnputc; 316851078Speter } 316951078Speter#endif 317051078Speter#endif 317151078Speter} 317251078Speter 317366230Sjhbstatic void 317466230Sjhbsiocninit(cp) 317566230Sjhb struct consdev *cp; 317666230Sjhb{ 317766230Sjhb comconsole = DEV_TO_UNIT(cp->cn_dev); 317866230Sjhb} 317966230Sjhb 318066230Sjhb#endif 318166230Sjhb 318251078Speter#ifdef __alpha__ 318351078Speter 318455868SgallatinCONS_DRIVER(sio, NULL, NULL, NULL, siocngetc, siocncheckc, siocnputc, NULL); 318551078Speter 318651078Speterint 318751078Spetersiocnattach(port, speed) 318851078Speter int port; 318951078Speter int speed; 319051078Speter{ 319151078Speter int s; 319251078Speter u_char cfcr; 319351078Speter struct siocnstate sp; 319451078Speter 319551078Speter siocniobase = port; 319651078Speter comdefaultrate = speed; 319751078Speter sio_consdev.cn_pri = CN_NORMAL; 319851078Speter sio_consdev.cn_dev = makedev(CDEV_MAJOR, 0); 319951078Speter 320051078Speter s = spltty(); 320151078Speter 320251078Speter /* 320351078Speter * Initialize the divisor latch. We can't rely on 320451078Speter * siocnopen() to do this the first time, since it 320551078Speter * avoids writing to the latch if the latch appears 320651078Speter * to have the correct value. Also, if we didn't 320751078Speter * just read the speed from the hardware, then we 320851078Speter * need to set the speed in hardware so that 320951078Speter * switching it later is null. 321051078Speter */ 321151078Speter cfcr = inb(siocniobase + com_cfcr); 321251078Speter outb(siocniobase + com_cfcr, CFCR_DLAB | cfcr); 321351078Speter outb(siocniobase + com_dlbl, 321451078Speter COMBRD(comdefaultrate) & 0xff); 321551078Speter outb(siocniobase + com_dlbh, 321651078Speter (u_int) COMBRD(comdefaultrate) >> 8); 321751078Speter outb(siocniobase + com_cfcr, cfcr); 321851078Speter 321951078Speter siocnopen(&sp, siocniobase, comdefaultrate); 322051078Speter splx(s); 322151078Speter 322251078Speter cn_tab = &sio_consdev; 322358885Simp return (0); 322451078Speter} 322551078Speter 322651078Speterint 322751078Spetersiogdbattach(port, speed) 322851078Speter int port; 322951078Speter int speed; 323051078Speter{ 323151078Speter int s; 323251078Speter u_char cfcr; 323351078Speter struct siocnstate sp; 323465714Sjhb int unit = 1; /* XXX !!! */ 323551078Speter 323651078Speter siogdbiobase = port; 323751078Speter gdbdefaultrate = speed; 323851078Speter 323965714Sjhb printf("sio%d: gdb debugging port\n", unit); 324065714Sjhb siogdbunit = unit; 324165714Sjhb#if DDB > 0 324265714Sjhb gdbdev = makedev(CDEV_MAJOR, unit); 324365714Sjhb gdb_getc = siocngetc; 324465714Sjhb gdb_putc = siocnputc; 324565714Sjhb#endif 324665714Sjhb 324751078Speter s = spltty(); 324851078Speter 324951078Speter /* 325051078Speter * Initialize the divisor latch. We can't rely on 325151078Speter * siocnopen() to do this the first time, since it 325251078Speter * avoids writing to the latch if the latch appears 325351078Speter * to have the correct value. Also, if we didn't 325451078Speter * just read the speed from the hardware, then we 325551078Speter * need to set the speed in hardware so that 325651078Speter * switching it later is null. 325751078Speter */ 325851078Speter cfcr = inb(siogdbiobase + com_cfcr); 325951078Speter outb(siogdbiobase + com_cfcr, CFCR_DLAB | cfcr); 326051078Speter outb(siogdbiobase + com_dlbl, 326151078Speter COMBRD(gdbdefaultrate) & 0xff); 326251078Speter outb(siogdbiobase + com_dlbh, 326351078Speter (u_int) COMBRD(gdbdefaultrate) >> 8); 326451078Speter outb(siogdbiobase + com_cfcr, cfcr); 326551078Speter 326651078Speter siocnopen(&sp, siogdbiobase, gdbdefaultrate); 326751078Speter splx(s); 326851078Speter 326958885Simp return (0); 327051078Speter} 327151078Speter 327251078Speter#endif 327351078Speter 327451078Speterstatic int 327551078Spetersiocncheckc(dev) 327651078Speter dev_t dev; 327751078Speter{ 327851078Speter int c; 327951078Speter Port_t iobase; 328051078Speter int s; 328151078Speter struct siocnstate sp; 328251078Speter 328351078Speter if (minor(dev) == siogdbunit) 328451078Speter iobase = siogdbiobase; 328551078Speter else 328651078Speter iobase = siocniobase; 328751078Speter s = spltty(); 328851078Speter siocnopen(&sp, iobase, comdefaultrate); 328951078Speter if (inb(iobase + com_lsr) & LSR_RXRDY) 329051078Speter c = inb(iobase + com_data); 329151078Speter else 329251078Speter c = -1; 329351078Speter siocnclose(&sp, iobase); 329451078Speter splx(s); 329551078Speter return (c); 329651078Speter} 329751078Speter 329851078Speter 329951078Speterint 330051078Spetersiocngetc(dev) 330151078Speter dev_t dev; 330251078Speter{ 330351078Speter int c; 330451078Speter Port_t iobase; 330551078Speter int s; 330651078Speter struct siocnstate sp; 330751078Speter 330851078Speter if (minor(dev) == siogdbunit) 330951078Speter iobase = siogdbiobase; 331051078Speter else 331151078Speter iobase = siocniobase; 331251078Speter s = spltty(); 331351078Speter siocnopen(&sp, iobase, comdefaultrate); 331451078Speter while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 331551078Speter ; 331651078Speter c = inb(iobase + com_data); 331751078Speter siocnclose(&sp, iobase); 331851078Speter splx(s); 331951078Speter return (c); 332051078Speter} 332151078Speter 332251078Spetervoid 332351078Spetersiocnputc(dev, c) 332451078Speter dev_t dev; 332551078Speter int c; 332651078Speter{ 332751078Speter int s; 332851078Speter struct siocnstate sp; 332951078Speter Port_t iobase; 333051078Speter 333151078Speter if (minor(dev) == siogdbunit) 333251078Speter iobase = siogdbiobase; 333351078Speter else 333451078Speter iobase = siocniobase; 333551078Speter s = spltty(); 333651078Speter siocnopen(&sp, iobase, comdefaultrate); 333751078Speter siocntxwait(iobase); 333851078Speter outb(iobase + com_data, c); 333951078Speter siocnclose(&sp, iobase); 334051078Speter splx(s); 334151078Speter} 334251078Speter 334351078Speter#ifdef __alpha__ 334451078Speterint 334551078Spetersiogdbgetc() 334651078Speter{ 334751078Speter int c; 334851078Speter Port_t iobase; 334951078Speter int s; 335051078Speter struct siocnstate sp; 335151078Speter 335251078Speter iobase = siogdbiobase; 335351078Speter s = spltty(); 335451078Speter siocnopen(&sp, iobase, gdbdefaultrate); 335551078Speter while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 335651078Speter ; 335751078Speter c = inb(iobase + com_data); 335851078Speter siocnclose(&sp, iobase); 335951078Speter splx(s); 336051078Speter return (c); 336151078Speter} 336251078Speter 336351078Spetervoid 336451078Spetersiogdbputc(c) 336551078Speter int c; 336651078Speter{ 336751078Speter int s; 336851078Speter struct siocnstate sp; 336951078Speter 337051078Speter s = spltty(); 337151078Speter siocnopen(&sp, siogdbiobase, gdbdefaultrate); 337251078Speter siocntxwait(siogdbiobase); 337351078Speter outb(siogdbiobase + com_data, c); 337451078Speter siocnclose(&sp, siogdbiobase); 337551078Speter splx(s); 337651078Speter} 337751078Speter#endif 337851078Speter 337952471SimpDRIVER_MODULE(sio, isa, sio_isa_driver, sio_devclass, 0, 0); 338082555SmsmithDRIVER_MODULE(sio, acpi, sio_isa_driver, sio_devclass, 0, 0); 338152471Simp#if NCARD > 0 338252471SimpDRIVER_MODULE(sio, pccard, sio_pccard_driver, sio_devclass, 0, 0); 338352471Simp#endif 338458885Simp#if NPCI > 0 338558885SimpDRIVER_MODULE(sio, pci, sio_pci_driver, sio_devclass, 0, 0); 338677750SimpDRIVER_MODULE(sio, cardbus, sio_pci_driver, sio_devclass, 0, 0); 338758885Simp#endif 3388