sio.c revision 82685
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 82685 2001-08-31 20:37:02Z 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 */ 66554944Speter {0x36339008, NULL}, /* BDP3336 - Best Data Prods. 336F */ 66654944Speter {0x0014490a, NULL}, /* BRI1400 - Boca 33.6 PnP */ 66754944Speter {0x0015490a, NULL}, /* BRI1500 - Internal Fax Data */ 66854944Speter {0x0034490a, NULL}, /* BRI3400 - Internal ACF Modem */ 66965438Ssheldonh {0x0094490a, NULL}, /* BRI9400 - Boca K56Flex PnP */ 67054944Speter {0x00b4490a, NULL}, /* BRIB400 - Boca 56k PnP */ 67154944Speter {0x0030320d, NULL}, /* CIR3000 - Cirrus Logic V43 */ 67256229Speter {0x0100440e, NULL}, /* CRD0001 - Cardinal MVP288IV ? */ 67360674Stanimura {0x36033610, NULL}, /* DAV0336 - DAVICOM 336PNP MODEM */ 67459888Stanimura {0x0000aa1a, NULL}, /* FUJ0000 - FUJITSU Modem 33600 PNP/I2 */ 67554944Speter {0x1200c31e, NULL}, /* GVC0012 - VF1128HV-R9 (win modem?) */ 67654944Speter {0x0303c31e, NULL}, /* GVC0303 - MaxTech 33.6 PnP D/F/V */ 67755124Speter {0x0505c31e, NULL}, /* GVC0505 - GVC 56k Faxmodem */ 67873961Ssanpei {0x0116c31e, NULL}, /* GVC1601 - Rockwell V.34 Plug & Play Modem */ 67954944Speter {0x0050c31e, NULL}, /* GVC5000 - some GVC modem */ 68054944Speter {0x3800f91e, NULL}, /* GWY0038 - Telepath with v.90 */ 68154944Speter {0x9062f91e, NULL}, /* GWY6290 - Telepath with x2 Technology */ 68273960Ssanpei {0x8100e425, NULL}, /* IOD0081 - I-O DATA DEVICE,INC. IFML-560 */ 68366920Stanimura {0x21002534, NULL}, /* MAE0021 - Jetstream Int V.90 56k Voice Series 2*/ 68454944Speter {0x0000f435, NULL}, /* MOT0000 - Motorola ModemSURFR 33.6 Intern */ 68554944Speter {0x5015f435, NULL}, /* MOT1550 - Motorola ModemSURFR 56K Modem */ 68654944Speter {0xf015f435, NULL}, /* MOT15F0 - Motorola VoiceSURFR 56K Modem */ 68754944Speter {0x6045f435, NULL}, /* MOT4560 - Motorola ? */ 68854944Speter {0x61e7a338, NULL}, /* NECE761 - 33.6Modem */ 68970356Stanimura {0x08804f3f, NULL}, /* OZO8008 - Zoom (33.6k Modem) */ 69060674Stanimura {0x0f804f3f, NULL}, /* OZO800f - Zoom 2812 (56k Modem) */ 69154944Speter {0x39804f3f, NULL}, /* OZO8039 - Zoom 56k flex */ 69277222Ssanpei {0x00914f3f, NULL}, /* OZO9100 - Zoom 2919 (K56 Faxmodem) */ 69357769Speter {0x3024a341, NULL}, /* PMC2430 - Pace 56 Voice Internal Modem */ 69454944Speter {0x1000eb49, NULL}, /* ROK0010 - Rockwell ? */ 69582685Smurray {0x1200b23d, NULL}, /* RSS0012 - OMRON ME5614ISA */ 69654944Speter {0x5002734a, NULL}, /* RSS0250 - 5614Jx3(G) Internal Modem */ 69760708Stanimura {0x6202734a, NULL}, /* RSS0262 - 5614Jx3[G] V90+K56Flex Modem */ 69854944Speter {0xc100ad4d, NULL}, /* SMM00C1 - Leopard 56k PnP */ 69956229Speter {0x9012b04e, NULL}, /* SUP1290 - Supra ? */ 70054944Speter {0x1013b04e, NULL}, /* SUP1310 - SupraExpress 336i PnP */ 70154944Speter {0x8013b04e, NULL}, /* SUP1380 - SupraExpress 288i PnP Voice */ 70254944Speter {0x8113b04e, NULL}, /* SUP1381 - SupraExpress 336i PnP Voice */ 70354944Speter {0x5016b04e, NULL}, /* SUP1650 - Supra 336i Sp Intl */ 70460947Stanimura {0x7016b04e, NULL}, /* SUP1670 - Supra 336i V+ Intl */ 70554944Speter {0x7420b04e, NULL}, /* SUP2070 - Supra ? */ 70654944Speter {0x8020b04e, NULL}, /* SUP2080 - Supra ? */ 70754944Speter {0x8420b04e, NULL}, /* SUP2084 - SupraExpress 56i PnP */ 70856229Speter {0x7121b04e, NULL}, /* SUP2171 - SupraExpress 56i Sp? */ 70954944Speter {0x8024b04e, NULL}, /* SUP2480 - Supra ? */ 71054944Speter {0x01007256, NULL}, /* USR0001 - U.S. Robotics Inc., Sportster W */ 71154944Speter {0x02007256, NULL}, /* USR0002 - U.S. Robotics Inc. Sportster 33. */ 71254944Speter {0x04007256, NULL}, /* USR0004 - USR Sportster 14.4k */ 71354944Speter {0x06007256, NULL}, /* USR0006 - USR Sportster 33.6k */ 71456229Speter {0x11007256, NULL}, /* USR0011 - USR ? */ 71554944Speter {0x01017256, NULL}, /* USR0101 - USR ? */ 71654944Speter {0x30207256, NULL}, /* USR2030 - U.S.Robotics Inc. Sportster 560 */ 71754944Speter {0x50207256, NULL}, /* USR2050 - U.S.Robotics Inc. Sportster 33. */ 71854944Speter {0x70207256, NULL}, /* USR2070 - U.S.Robotics Inc. Sportster 560 */ 71954944Speter {0x30307256, NULL}, /* USR3030 - U.S. Robotics 56K FAX INT */ 72054944Speter {0x31307256, NULL}, /* USR3031 - U.S. Robotics 56K FAX INT */ 72158848Speter {0x50307256, NULL}, /* USR3050 - U.S. Robotics 56K FAX INT */ 72254944Speter {0x70307256, NULL}, /* USR3070 - U.S. Robotics 56K Voice INT */ 72356229Speter {0x90307256, NULL}, /* USR3090 - USR ? */ 72464688Speter {0x70917256, NULL}, /* USR9170 - U.S. Robotics 56K FAX INT */ 72554944Speter {0x90917256, NULL}, /* USR9190 - USR 56k Voice INT */ 72654944Speter {0x0300695c, NULL}, /* WCI0003 - Fax/Voice/Modem/Speakphone/Asvd */ 72765469Ssheldonh {0x01a0896a, NULL}, /* ZTIA001 - Zoom Internal V90 Faxmodem */ 72854944Speter {0x61f7896a, NULL}, /* ZTIF761 - Zoom ComStar 33.6 */ 72951078Speter {0} 73051078Speter}; 73151078Speter 73256229Speter 73356229Speter 73451078Speterstatic int 73552471Simpsio_isa_probe(dev) 73652471Simp device_t dev; 73752471Simp{ 73852471Simp /* Check isapnp ids */ 73952471Simp if (ISA_PNP_PROBE(device_get_parent(dev), dev, sio_ids) == ENXIO) 74052471Simp return (ENXIO); 74158885Simp return (sioprobe(dev, 0)); 74252471Simp} 74352471Simp 74452471Simpstatic int 74558885Simpsioprobe(dev, xrid) 74651078Speter device_t dev; 74758885Simp int xrid; 74851078Speter{ 74953344Speter#if 0 75051078Speter static bool_t already_init; 75153344Speter device_t xdev; 75253344Speter#endif 75360471Snyan struct com_s *com; 75451078Speter bool_t failures[10]; 75551078Speter int fn; 75651078Speter device_t idev; 75751078Speter Port_t iobase; 75851078Speter intrmask_t irqmap[4]; 75951078Speter intrmask_t irqs; 76051078Speter u_char mcr_image; 76151078Speter int result; 76254206Speter u_long xirq; 76351088Speter u_int flags = device_get_flags(dev); 76451078Speter int rid; 76551078Speter struct resource *port; 76651078Speter 76758885Simp rid = xrid; 76851078Speter port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 76951078Speter 0, ~0, IO_COMSIZE, RF_ACTIVE); 77051078Speter if (!port) 77157915Simp return (ENXIO); 77251078Speter 77360471Snyan com = device_get_softc(dev); 77460471Snyan com->bst = rman_get_bustag(port); 77560471Snyan com->bsh = rman_get_bushandle(port); 77660471Snyan 77770174Sjhb if (atomic_cmpset_int(&sio_inited, 0, 1)) 77872238Sjhb mtx_init(&sio_lock, driver_name, MTX_SPIN); 77970174Sjhb 78053344Speter#if 0 78153344Speter /* 78253344Speter * XXX this is broken - when we are first called, there are no 78353344Speter * previously configured IO ports. We could hard code 78453344Speter * 0x3f8, 0x2f8, 0x3e8, 0x2e8 etc but that's probably worse. 78553344Speter * This code has been doing nothing since the conversion since 78653344Speter * "count" is zero the first time around. 78753344Speter */ 78851078Speter if (!already_init) { 78951078Speter /* 79051078Speter * Turn off MCR_IENABLE for all likely serial ports. An unused 79151078Speter * port with its MCR_IENABLE gate open will inhibit interrupts 79251078Speter * from any used port that shares the interrupt vector. 79351078Speter * XXX the gate enable is elsewhere for some multiports. 79451078Speter */ 79551078Speter device_t *devs; 79653344Speter int count, i, xioport; 79751078Speter 79851078Speter devclass_get_devices(sio_devclass, &devs, &count); 79951078Speter for (i = 0; i < count; i++) { 80051078Speter xdev = devs[i]; 80154194Speter if (device_is_enabled(xdev) && 80254194Speter bus_get_resource(xdev, SYS_RES_IOPORT, 0, &xioport, 80354194Speter NULL) == 0) 80453344Speter outb(xioport + com_mcr, 0); 80551078Speter } 80651078Speter free(devs, M_TEMP); 80751078Speter already_init = TRUE; 80851078Speter } 80953344Speter#endif 81051078Speter 81151078Speter if (COM_LLCONSOLE(flags)) { 81251078Speter printf("sio%d: reserved for low-level i/o\n", 81351078Speter device_get_unit(dev)); 81456788Sbde bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 81551078Speter return (ENXIO); 81651078Speter } 81751078Speter 81851078Speter /* 81951078Speter * If the device is on a multiport card and has an AST/4 82051078Speter * compatible interrupt control register, initialize this 82151078Speter * register and prepare to leave MCR_IENABLE clear in the mcr. 82251078Speter * Otherwise, prepare to set MCR_IENABLE in the mcr. 82351078Speter * Point idev to the device struct giving the correct id_irq. 82451078Speter * This is the struct for the master device if there is one. 82551078Speter */ 82651078Speter idev = dev; 82751078Speter mcr_image = MCR_IENABLE; 82851078Speter#ifdef COM_MULTIPORT 82957234Sbde if (COM_ISMULTIPORT(flags)) { 83054206Speter Port_t xiobase; 83154206Speter u_long io; 83254206Speter 83351078Speter idev = devclass_get_device(sio_devclass, COM_MPMASTER(flags)); 83451078Speter if (idev == NULL) { 83551078Speter printf("sio%d: master device %d not configured\n", 83651078Speter device_get_unit(dev), COM_MPMASTER(flags)); 83751078Speter idev = dev; 83851078Speter } 83957234Sbde if (!COM_NOTAST4(flags)) { 84057234Sbde if (bus_get_resource(idev, SYS_RES_IOPORT, 0, &io, 84157234Sbde NULL) == 0) { 84257234Sbde xiobase = io; 84357234Sbde if (bus_get_resource(idev, SYS_RES_IRQ, 0, 84457234Sbde NULL, NULL) == 0) 84557234Sbde outb(xiobase + com_scr, 0x80); 84657234Sbde else 84757234Sbde outb(xiobase + com_scr, 0); 84857234Sbde } 84957234Sbde mcr_image = 0; 85051078Speter } 85151078Speter } 85251078Speter#endif /* COM_MULTIPORT */ 85354194Speter if (bus_get_resource(idev, SYS_RES_IRQ, 0, NULL, NULL) != 0) 85451078Speter mcr_image = 0; 85551078Speter 85651078Speter bzero(failures, sizeof failures); 85751078Speter iobase = rman_get_start(port); 85851078Speter 85951078Speter /* 86051078Speter * We don't want to get actual interrupts, just masked ones. 86151078Speter * Interrupts from this line should already be masked in the ICU, 86251078Speter * but mask them in the processor as well in case there are some 86351078Speter * (misconfigured) shared interrupts. 86451078Speter */ 86572200Sbmilekic mtx_lock_spin(&sio_lock); 86651078Speter/* EXTRA DELAY? */ 86751078Speter 86851078Speter /* 86951078Speter * Initialize the speed and the word size and wait long enough to 87051078Speter * drain the maximum of 16 bytes of junk in device output queues. 87151078Speter * The speed is undefined after a master reset and must be set 87251078Speter * before relying on anything related to output. There may be 87351078Speter * junk after a (very fast) soft reboot and (apparently) after 87451078Speter * master reset. 87551078Speter * XXX what about the UART bug avoided by waiting in comparam()? 87651078Speter * We don't want to to wait long enough to drain at 2 bps. 87751078Speter */ 87851078Speter if (iobase == siocniobase) 87951078Speter DELAY((16 + 1) * 1000000 / (comdefaultrate / 10)); 88051078Speter else { 88160471Snyan sio_setreg(com, com_cfcr, CFCR_DLAB | CFCR_8BITS); 88260471Snyan sio_setreg(com, com_dlbl, COMBRD(SIO_TEST_SPEED) & 0xff); 88360471Snyan sio_setreg(com, com_dlbh, (u_int) COMBRD(SIO_TEST_SPEED) >> 8); 88460471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); 88551078Speter DELAY((16 + 1) * 1000000 / (SIO_TEST_SPEED / 10)); 88651078Speter } 88751078Speter 88851078Speter /* 88951078Speter * Enable the interrupt gate and disable device interupts. This 89051078Speter * should leave the device driving the interrupt line low and 89151078Speter * guarantee an edge trigger if an interrupt can be generated. 89251078Speter */ 89351078Speter/* EXTRA DELAY? */ 89460471Snyan sio_setreg(com, com_mcr, mcr_image); 89560471Snyan sio_setreg(com, com_ier, 0); 89651078Speter DELAY(1000); /* XXX */ 89751078Speter irqmap[0] = isa_irq_pending(); 89851078Speter 89951078Speter /* 90051078Speter * Attempt to set loopback mode so that we can send a null byte 90151078Speter * without annoying any external device. 90251078Speter */ 90351078Speter/* EXTRA DELAY? */ 90460471Snyan sio_setreg(com, com_mcr, mcr_image | MCR_LOOPBACK); 90551078Speter 90651078Speter /* 90751078Speter * Attempt to generate an output interrupt. On 8250's, setting 90851078Speter * IER_ETXRDY generates an interrupt independent of the current 90951078Speter * setting and independent of whether the THR is empty. On 16450's, 91051078Speter * setting IER_ETXRDY generates an interrupt independent of the 91151078Speter * current setting. On 16550A's, setting IER_ETXRDY only 91251078Speter * generates an interrupt when IER_ETXRDY is not already set. 91351078Speter */ 91460471Snyan sio_setreg(com, com_ier, IER_ETXRDY); 91551078Speter 91651078Speter /* 91751078Speter * On some 16x50 incompatibles, setting IER_ETXRDY doesn't generate 91851078Speter * an interrupt. They'd better generate one for actually doing 91951078Speter * output. Loopback may be broken on the same incompatibles but 92051078Speter * it's unlikely to do more than allow the null byte out. 92151078Speter */ 92260471Snyan sio_setreg(com, com_data, 0); 92351078Speter DELAY((1 + 2) * 1000000 / (SIO_TEST_SPEED / 10)); 92451078Speter 92551078Speter /* 92651078Speter * Turn off loopback mode so that the interrupt gate works again 92751078Speter * (MCR_IENABLE was hidden). This should leave the device driving 92851078Speter * an interrupt line high. It doesn't matter if the interrupt 92951078Speter * line oscillates while we are not looking at it, since interrupts 93051078Speter * are disabled. 93151078Speter */ 93251078Speter/* EXTRA DELAY? */ 93360471Snyan sio_setreg(com, com_mcr, mcr_image); 93451078Speter 93551078Speter /* 93652471Simp * Some pcmcia cards have the "TXRDY bug", so we check everyone 93751078Speter * for IIR_TXRDY implementation ( Palido 321s, DC-1S... ) 93851078Speter */ 93953370Speter if (COM_NOPROBE(flags)) { 94053370Speter /* Reading IIR register twice */ 94153370Speter for (fn = 0; fn < 2; fn ++) { 94253370Speter DELAY(10000); 94360471Snyan failures[6] = sio_getreg(com, com_iir); 94453370Speter } 94553370Speter /* Check IIR_TXRDY clear ? */ 94653370Speter result = 0; 94753370Speter if (failures[6] & IIR_TXRDY) { 94853370Speter /* Nop, Double check with clearing IER */ 94960471Snyan sio_setreg(com, com_ier, 0); 95060471Snyan if (sio_getreg(com, com_iir) & IIR_NOPEND) { 95153370Speter /* Ok. we're familia this gang */ 95253370Speter SET_FLAG(dev, COM_C_IIR_TXRDYBUG); 95353370Speter } else { 95453370Speter /* Unknown, Just omit this chip.. XXX */ 95553370Speter result = ENXIO; 95681793Simp sio_setreg(com, com_mcr, 0); 95753370Speter } 95851078Speter } else { 95953370Speter /* OK. this is well-known guys */ 96053370Speter CLR_FLAG(dev, COM_C_IIR_TXRDYBUG); 96151078Speter } 96281793Simp sio_setreg(com, com_ier, 0); 96360471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); 96472200Sbmilekic mtx_unlock_spin(&sio_lock); 96553344Speter bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 96653344Speter return (iobase == siocniobase ? 0 : result); 96753344Speter } 96853344Speter 96951078Speter /* 97051078Speter * Check that 97151078Speter * o the CFCR, IER and MCR in UART hold the values written to them 97251078Speter * (the values happen to be all distinct - this is good for 97351078Speter * avoiding false positive tests from bus echoes). 97451078Speter * o an output interrupt is generated and its vector is correct. 97551078Speter * o the interrupt goes away when the IIR in the UART is read. 97651078Speter */ 97751078Speter/* EXTRA DELAY? */ 97860471Snyan failures[0] = sio_getreg(com, com_cfcr) - CFCR_8BITS; 97960471Snyan failures[1] = sio_getreg(com, com_ier) - IER_ETXRDY; 98060471Snyan failures[2] = sio_getreg(com, com_mcr) - mcr_image; 98151078Speter DELAY(10000); /* Some internal modems need this time */ 98251078Speter irqmap[1] = isa_irq_pending(); 98360471Snyan failures[4] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_TXRDY; 98451078Speter DELAY(1000); /* XXX */ 98551078Speter irqmap[2] = isa_irq_pending(); 98660471Snyan failures[6] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 98751078Speter 98851078Speter /* 98951078Speter * Turn off all device interrupts and check that they go off properly. 99051078Speter * Leave MCR_IENABLE alone. For ports without a master port, it gates 99151078Speter * the OUT2 output of the UART to 99251078Speter * the ICU input. Closing the gate would give a floating ICU input 99351078Speter * (unless there is another device driving it) and spurious interrupts. 99451078Speter * (On the system that this was first tested on, the input floats high 99551078Speter * and gives a (masked) interrupt as soon as the gate is closed.) 99651078Speter */ 99760471Snyan sio_setreg(com, com_ier, 0); 99860471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); /* dummy to avoid bus echo */ 99960471Snyan failures[7] = sio_getreg(com, com_ier); 100051078Speter DELAY(1000); /* XXX */ 100151078Speter irqmap[3] = isa_irq_pending(); 100260471Snyan failures[9] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 100351078Speter 100472200Sbmilekic mtx_unlock_spin(&sio_lock); 100551078Speter 100651078Speter irqs = irqmap[1] & ~irqmap[0]; 100754194Speter if (bus_get_resource(idev, SYS_RES_IRQ, 0, &xirq, NULL) == 0 && 100854194Speter ((1 << xirq) & irqs) == 0) 100951078Speter printf( 101054206Speter "sio%d: configured irq %ld not in bitmap of probed irqs %#x\n", 101153344Speter device_get_unit(dev), xirq, irqs); 101251078Speter if (bootverbose) 101351078Speter printf("sio%d: irq maps: %#x %#x %#x %#x\n", 101451078Speter device_get_unit(dev), 101551078Speter irqmap[0], irqmap[1], irqmap[2], irqmap[3]); 101651078Speter 101751078Speter result = 0; 101851078Speter for (fn = 0; fn < sizeof failures; ++fn) 101951078Speter if (failures[fn]) { 102060471Snyan sio_setreg(com, com_mcr, 0); 102151078Speter result = ENXIO; 102251078Speter if (bootverbose) { 102351078Speter printf("sio%d: probe failed test(s):", 102451078Speter device_get_unit(dev)); 102551078Speter for (fn = 0; fn < sizeof failures; ++fn) 102651078Speter if (failures[fn]) 102751078Speter printf(" %d", fn); 102851078Speter printf("\n"); 102951078Speter } 103051078Speter break; 103151078Speter } 103251078Speter bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 103351078Speter return (iobase == siocniobase ? 0 : result); 103451078Speter} 103551078Speter 103651078Speter#ifdef COM_ESP 103751078Speterstatic int 103851078Speterespattach(com, esp_port) 103951078Speter struct com_s *com; 104051078Speter Port_t esp_port; 104151078Speter{ 104251078Speter u_char dips; 104351078Speter u_char val; 104451078Speter 104551078Speter /* 104651078Speter * Check the ESP-specific I/O port to see if we're an ESP 104751078Speter * card. If not, return failure immediately. 104851078Speter */ 104951078Speter if ((inb(esp_port) & 0xf3) == 0) { 105051078Speter printf(" port 0x%x is not an ESP board?\n", esp_port); 105151078Speter return (0); 105251078Speter } 105351078Speter 105451078Speter /* 105551078Speter * We've got something that claims to be a Hayes ESP card. 105651078Speter * Let's hope so. 105751078Speter */ 105851078Speter 105951078Speter /* Get the dip-switch configuration */ 106051078Speter outb(esp_port + ESP_CMD1, ESP_GETDIPS); 106151078Speter dips = inb(esp_port + ESP_STATUS1); 106251078Speter 106351078Speter /* 106451078Speter * Bits 0,1 of dips say which COM port we are. 106551078Speter */ 106660471Snyan if (rman_get_start(com->ioportres) == likely_com_ports[dips & 0x03]) 106751078Speter printf(" : ESP"); 106851078Speter else { 106951078Speter printf(" esp_port has com %d\n", dips & 0x03); 107051078Speter return (0); 107151078Speter } 107251078Speter 107351078Speter /* 107451078Speter * Check for ESP version 2.0 or later: bits 4,5,6 = 010. 107551078Speter */ 107651078Speter outb(esp_port + ESP_CMD1, ESP_GETTEST); 107751078Speter val = inb(esp_port + ESP_STATUS1); /* clear reg 1 */ 107851078Speter val = inb(esp_port + ESP_STATUS2); 107951078Speter if ((val & 0x70) < 0x20) { 108051078Speter printf("-old (%o)", val & 0x70); 108151078Speter return (0); 108251078Speter } 108351078Speter 108451078Speter /* 108551078Speter * Check for ability to emulate 16550: bit 7 == 1 108651078Speter */ 108751078Speter if ((dips & 0x80) == 0) { 108851078Speter printf(" slave"); 108951078Speter return (0); 109051078Speter } 109151078Speter 109251078Speter /* 109351078Speter * Okay, we seem to be a Hayes ESP card. Whee. 109451078Speter */ 109551078Speter com->esp = TRUE; 109651078Speter com->esp_port = esp_port; 109751078Speter return (1); 109851078Speter} 109951078Speter#endif /* COM_ESP */ 110051078Speter 110151078Speterstatic int 110252471Simpsio_isa_attach(dev) 110352471Simp device_t dev; 110452471Simp{ 110558885Simp return (sioattach(dev, 0)); 110652471Simp} 110752471Simp 110852471Simpstatic int 110958885Simpsioattach(dev, xrid) 111051078Speter device_t dev; 111158885Simp int xrid; 111251078Speter{ 111351078Speter struct com_s *com; 111451078Speter#ifdef COM_ESP 111551078Speter Port_t *espp; 111651078Speter#endif 111751078Speter Port_t iobase; 111851078Speter int unit; 111953344Speter u_int flags; 112051078Speter int rid; 112151078Speter struct resource *port; 112253344Speter int ret; 112351078Speter 112458885Simp rid = xrid; 112551078Speter port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 112651078Speter 0, ~0, IO_COMSIZE, RF_ACTIVE); 112751078Speter if (!port) 112857915Simp return (ENXIO); 112951078Speter 113051078Speter iobase = rman_get_start(port); 113151078Speter unit = device_get_unit(dev); 113251078Speter com = device_get_softc(dev); 113353344Speter flags = device_get_flags(dev); 113451078Speter 113553344Speter if (unit >= sio_numunits) 113653344Speter sio_numunits = unit + 1; 113751078Speter /* 113851078Speter * sioprobe() has initialized the device registers as follows: 113951078Speter * o cfcr = CFCR_8BITS. 114051078Speter * It is most important that CFCR_DLAB is off, so that the 114151078Speter * data port is not hidden when we enable interrupts. 114251078Speter * o ier = 0. 114351078Speter * Interrupts are only enabled when the line is open. 114451078Speter * o mcr = MCR_IENABLE, or 0 if the port has AST/4 compatible 114551078Speter * interrupt control register or the config specifies no irq. 114651078Speter * Keeping MCR_DTR and MCR_RTS off might stop the external 114751078Speter * device from sending before we are ready. 114851078Speter */ 114951078Speter bzero(com, sizeof *com); 115051078Speter com->unit = unit; 115151078Speter com->ioportres = port; 115260471Snyan com->bst = rman_get_bustag(port); 115360471Snyan com->bsh = rman_get_bushandle(port); 115451078Speter com->cfcr_image = CFCR_8BITS; 115551078Speter com->dtr_wait = 3 * hz; 115651078Speter com->loses_outints = COM_LOSESOUTINTS(flags) != 0; 115757234Sbde com->no_irq = bus_get_resource(dev, SYS_RES_IRQ, 0, NULL, NULL) != 0; 115851078Speter com->tx_fifo_size = 1; 115951078Speter com->obufs[0].l_head = com->obuf1; 116051078Speter com->obufs[1].l_head = com->obuf2; 116151078Speter 116251078Speter com->data_port = iobase + com_data; 116351078Speter com->int_id_port = iobase + com_iir; 116451078Speter com->modem_ctl_port = iobase + com_mcr; 116551078Speter com->mcr_image = inb(com->modem_ctl_port); 116651078Speter com->line_status_port = iobase + com_lsr; 116751078Speter com->modem_status_port = iobase + com_msr; 116851078Speter com->intr_ctl_port = iobase + com_ier; 116951078Speter 117051078Speter /* 117151078Speter * We don't use all the flags from <sys/ttydefaults.h> since they 117251078Speter * are only relevant for logins. It's important to have echo off 117351078Speter * initially so that the line doesn't start blathering before the 117451078Speter * echo flag can be turned off. 117551078Speter */ 117651078Speter com->it_in.c_iflag = 0; 117751078Speter com->it_in.c_oflag = 0; 117851078Speter com->it_in.c_cflag = TTYDEF_CFLAG; 117951078Speter com->it_in.c_lflag = 0; 118051078Speter if (unit == comconsole) { 118151078Speter com->it_in.c_iflag = TTYDEF_IFLAG; 118251078Speter com->it_in.c_oflag = TTYDEF_OFLAG; 118351078Speter com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL; 118451078Speter com->it_in.c_lflag = TTYDEF_LFLAG; 118551078Speter com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL; 118651078Speter com->lt_out.c_ispeed = com->lt_out.c_ospeed = 118751078Speter com->lt_in.c_ispeed = com->lt_in.c_ospeed = 118851078Speter com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate; 118951078Speter } else 119051078Speter com->it_in.c_ispeed = com->it_in.c_ospeed = TTYDEF_SPEED; 119165605Sjhb if (siosetwater(com, com->it_in.c_ispeed) != 0) { 119272200Sbmilekic mtx_unlock_spin(&sio_lock); 119356788Sbde /* 119456788Sbde * Leave i/o resources allocated if this is a `cn'-level 119556788Sbde * console, so that other devices can't snarf them. 119656788Sbde */ 119756788Sbde if (iobase != siocniobase) 119856788Sbde bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 119956788Sbde return (ENOMEM); 120051078Speter } 120172200Sbmilekic mtx_unlock_spin(&sio_lock); 120251078Speter termioschars(&com->it_in); 120351078Speter com->it_out = com->it_in; 120451078Speter 120551078Speter /* attempt to determine UART type */ 120651078Speter printf("sio%d: type", unit); 120751078Speter 120851078Speter 120951078Speter#ifdef COM_MULTIPORT 121051078Speter if (!COM_ISMULTIPORT(flags) && !COM_IIR_TXRDYBUG(flags)) 121151078Speter#else 121251078Speter if (!COM_IIR_TXRDYBUG(flags)) 121351078Speter#endif 121451078Speter { 121551078Speter u_char scr; 121651078Speter u_char scr1; 121751078Speter u_char scr2; 121851078Speter 121960471Snyan scr = sio_getreg(com, com_scr); 122060471Snyan sio_setreg(com, com_scr, 0xa5); 122160471Snyan scr1 = sio_getreg(com, com_scr); 122260471Snyan sio_setreg(com, com_scr, 0x5a); 122360471Snyan scr2 = sio_getreg(com, com_scr); 122460471Snyan sio_setreg(com, com_scr, scr); 122551078Speter if (scr1 != 0xa5 || scr2 != 0x5a) { 122651078Speter printf(" 8250"); 122751078Speter goto determined_type; 122851078Speter } 122951078Speter } 123060471Snyan sio_setreg(com, com_fifo, FIFO_ENABLE | FIFO_RX_HIGH); 123151078Speter DELAY(100); 123251078Speter com->st16650a = 0; 123351078Speter switch (inb(com->int_id_port) & IIR_FIFO_MASK) { 123451078Speter case FIFO_RX_LOW: 123551078Speter printf(" 16450"); 123651078Speter break; 123751078Speter case FIFO_RX_MEDL: 123851078Speter printf(" 16450?"); 123951078Speter break; 124051078Speter case FIFO_RX_MEDH: 124151078Speter printf(" 16550?"); 124251078Speter break; 124351078Speter case FIFO_RX_HIGH: 124451078Speter if (COM_NOFIFO(flags)) { 124551078Speter printf(" 16550A fifo disabled"); 124651078Speter } else { 124751078Speter com->hasfifo = TRUE; 124851078Speter if (COM_ST16650A(flags)) { 124951078Speter com->st16650a = 1; 125051078Speter com->tx_fifo_size = 32; 125151078Speter printf(" ST16650A"); 125251078Speter } else { 125351078Speter com->tx_fifo_size = COM_FIFOSIZE(flags); 125451078Speter printf(" 16550A"); 125551078Speter } 125651078Speter } 125751078Speter#ifdef COM_ESP 125851078Speter for (espp = likely_esp_ports; *espp != 0; espp++) 125951078Speter if (espattach(com, *espp)) { 126051078Speter com->tx_fifo_size = 1024; 126151078Speter break; 126251078Speter } 126351078Speter#endif 126451078Speter if (!com->st16650a) { 126551078Speter if (!com->tx_fifo_size) 126651078Speter com->tx_fifo_size = 16; 126751078Speter else 126851078Speter printf(" lookalike with %d bytes FIFO", 126951078Speter com->tx_fifo_size); 127051078Speter } 127151078Speter 127251078Speter break; 127351078Speter } 127451078Speter 127551078Speter#ifdef COM_ESP 127651078Speter if (com->esp) { 127751078Speter /* 127851078Speter * Set 16550 compatibility mode. 127951078Speter * We don't use the ESP_MODE_SCALE bit to increase the 128051078Speter * fifo trigger levels because we can't handle large 128151078Speter * bursts of input. 128251078Speter * XXX flow control should be set in comparam(), not here. 128351078Speter */ 128451078Speter outb(com->esp_port + ESP_CMD1, ESP_SETMODE); 128551078Speter outb(com->esp_port + ESP_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO); 128651078Speter 128751078Speter /* Set RTS/CTS flow control. */ 128851078Speter outb(com->esp_port + ESP_CMD1, ESP_SETFLOWTYPE); 128951078Speter outb(com->esp_port + ESP_CMD2, ESP_FLOW_RTS); 129051078Speter outb(com->esp_port + ESP_CMD2, ESP_FLOW_CTS); 129151078Speter 129251078Speter /* Set flow-control levels. */ 129351078Speter outb(com->esp_port + ESP_CMD1, ESP_SETRXFLOW); 129451078Speter outb(com->esp_port + ESP_CMD2, HIBYTE(768)); 129551078Speter outb(com->esp_port + ESP_CMD2, LOBYTE(768)); 129651078Speter outb(com->esp_port + ESP_CMD2, HIBYTE(512)); 129751078Speter outb(com->esp_port + ESP_CMD2, LOBYTE(512)); 129851078Speter } 129951078Speter#endif /* COM_ESP */ 130060471Snyan sio_setreg(com, com_fifo, 0); 130151078Speterdetermined_type: ; 130251078Speter 130351078Speter#ifdef COM_MULTIPORT 130451078Speter if (COM_ISMULTIPORT(flags)) { 130553344Speter device_t masterdev; 130653344Speter 130751078Speter com->multiport = TRUE; 130851078Speter printf(" (multiport"); 130951078Speter if (unit == COM_MPMASTER(flags)) 131051078Speter printf(" master"); 131151078Speter printf(")"); 131253344Speter masterdev = devclass_get_device(sio_devclass, 131353344Speter COM_MPMASTER(flags)); 131457234Sbde com->no_irq = (masterdev == NULL || bus_get_resource(masterdev, 131557234Sbde SYS_RES_IRQ, 0, NULL, NULL) != 0); 131651078Speter } 131751078Speter#endif /* COM_MULTIPORT */ 131851078Speter if (unit == comconsole) 131951078Speter printf(", console"); 132053344Speter if (COM_IIR_TXRDYBUG(flags)) 132151078Speter printf(" with a bogus IIR_TXRDY register"); 132251078Speter printf("\n"); 132351078Speter 132467551Sjhb if (sio_fast_ih == NULL) { 132572238Sjhb swi_add(&tty_ithd, "tty:sio", siopoll, NULL, SWI_TTY, 0, 132672238Sjhb &sio_fast_ih); 132772238Sjhb swi_add(&clk_ithd, "tty:sio", siopoll, NULL, SWI_TTY, 0, 132872238Sjhb &sio_slow_ih); 132951078Speter } 133065131Sphk com->devs[0] = make_dev(&sio_cdevsw, unit, 133151078Speter UID_ROOT, GID_WHEEL, 0600, "ttyd%r", unit); 133265131Sphk com->devs[1] = make_dev(&sio_cdevsw, unit | CONTROL_INIT_STATE, 133351078Speter UID_ROOT, GID_WHEEL, 0600, "ttyid%r", unit); 133465131Sphk com->devs[2] = make_dev(&sio_cdevsw, unit | CONTROL_LOCK_STATE, 133551078Speter UID_ROOT, GID_WHEEL, 0600, "ttyld%r", unit); 133665131Sphk com->devs[3] = make_dev(&sio_cdevsw, unit | CALLOUT_MASK, 133751078Speter UID_UUCP, GID_DIALER, 0660, "cuaa%r", unit); 133865131Sphk com->devs[4] = make_dev(&sio_cdevsw, 133965131Sphk unit | CALLOUT_MASK | CONTROL_INIT_STATE, 134051078Speter UID_UUCP, GID_DIALER, 0660, "cuaia%r", unit); 134165131Sphk com->devs[5] = make_dev(&sio_cdevsw, 134265131Sphk unit | CALLOUT_MASK | CONTROL_LOCK_STATE, 134351078Speter UID_UUCP, GID_DIALER, 0660, "cuala%r", unit); 134451078Speter com->flags = flags; 134551078Speter com->pps.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR; 134651078Speter pps_init(&com->pps); 134751078Speter 134851078Speter rid = 0; 134951078Speter com->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0ul, ~0ul, 1, 135053344Speter RF_ACTIVE); 135153344Speter if (com->irqres) { 135253344Speter ret = BUS_SETUP_INTR(device_get_parent(dev), dev, com->irqres, 135365557Sjasone INTR_TYPE_TTY | INTR_FAST, 135454386Simp siointr, com, &com->cookie); 135554194Speter if (ret) { 135654194Speter ret = BUS_SETUP_INTR(device_get_parent(dev), dev, 135754194Speter com->irqres, INTR_TYPE_TTY, 135854386Simp siointr, com, &com->cookie); 135954194Speter if (ret == 0) 136054194Speter device_printf(dev, "unable to activate interrupt in fast mode - using normal mode"); 136154194Speter } 136253344Speter if (ret) 136353344Speter device_printf(dev, "could not activate interrupt\n"); 136478504Siedowse#if defined(DDB) && (defined(BREAK_TO_DEBUGGER) || \ 136578504Siedowse defined(ALT_BREAK_TO_DEBUGGER)) 136678504Siedowse /* 136778504Siedowse * Enable interrupts for early break-to-debugger support 136878504Siedowse * on the console. 136978504Siedowse */ 137078504Siedowse if (ret == 0 && unit == comconsole) 137178504Siedowse outb(siocniobase + com_ier, IER_ERXRDY | IER_ERLS | 137278504Siedowse IER_EMSC); 137378504Siedowse#endif 137453344Speter } 137551078Speter 137651078Speter return (0); 137751078Speter} 137851078Speter 137951078Speterstatic int 138051078Spetersioopen(dev, flag, mode, p) 138151078Speter dev_t dev; 138251078Speter int flag; 138351078Speter int mode; 138451078Speter struct proc *p; 138551078Speter{ 138651078Speter struct com_s *com; 138751078Speter int error; 138851078Speter int mynor; 138951078Speter int s; 139051078Speter struct tty *tp; 139151078Speter int unit; 139251078Speter 139351078Speter mynor = minor(dev); 139451078Speter unit = MINOR_TO_UNIT(mynor); 139553344Speter com = com_addr(unit); 139653344Speter if (com == NULL) 139751078Speter return (ENXIO); 139851078Speter if (com->gone) 139951078Speter return (ENXIO); 140051078Speter if (mynor & CONTROL_MASK) 140151078Speter return (0); 140251078Speter tp = dev->si_tty = com->tp = ttymalloc(com->tp); 140351078Speter s = spltty(); 140451078Speter /* 140551078Speter * We jump to this label after all non-interrupted sleeps to pick 140651078Speter * up any changes of the device state. 140751078Speter */ 140851078Speteropen_top: 140951078Speter while (com->state & CS_DTR_OFF) { 141051078Speter error = tsleep(&com->dtr_wait, TTIPRI | PCATCH, "siodtr", 0); 141151078Speter if (com_addr(unit) == NULL) 141251078Speter return (ENXIO); 141351078Speter if (error != 0 || com->gone) 141451078Speter goto out; 141551078Speter } 141651078Speter if (tp->t_state & TS_ISOPEN) { 141751078Speter /* 141851078Speter * The device is open, so everything has been initialized. 141951078Speter * Handle conflicts. 142051078Speter */ 142151078Speter if (mynor & CALLOUT_MASK) { 142251078Speter if (!com->active_out) { 142351078Speter error = EBUSY; 142451078Speter goto out; 142551078Speter } 142651078Speter } else { 142751078Speter if (com->active_out) { 142851078Speter if (flag & O_NONBLOCK) { 142951078Speter error = EBUSY; 143051078Speter goto out; 143151078Speter } 143251078Speter error = tsleep(&com->active_out, 143351078Speter TTIPRI | PCATCH, "siobi", 0); 143451078Speter if (com_addr(unit) == NULL) 143551078Speter return (ENXIO); 143651078Speter if (error != 0 || com->gone) 143751078Speter goto out; 143851078Speter goto open_top; 143951078Speter } 144051078Speter } 144151078Speter if (tp->t_state & TS_XCLUDE && 144251078Speter suser(p)) { 144351078Speter error = EBUSY; 144451078Speter goto out; 144551078Speter } 144651078Speter } else { 144751078Speter /* 144851078Speter * The device isn't open, so there are no conflicts. 144951078Speter * Initialize it. Initialization is done twice in many 145051078Speter * cases: to preempt sleeping callin opens if we are 145151078Speter * callout, and to complete a callin open after DCD rises. 145251078Speter */ 145351078Speter tp->t_oproc = comstart; 145451078Speter tp->t_param = comparam; 145551654Sphk tp->t_stop = comstop; 145651078Speter tp->t_dev = dev; 145751078Speter tp->t_termios = mynor & CALLOUT_MASK 145851078Speter ? com->it_out : com->it_in; 145951078Speter (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET); 146051078Speter com->poll = com->no_irq; 146151078Speter com->poll_output = com->loses_outints; 146251078Speter ++com->wopeners; 146351078Speter error = comparam(tp, &tp->t_termios); 146451078Speter --com->wopeners; 146551078Speter if (error != 0) 146651078Speter goto out; 146751078Speter /* 146851078Speter * XXX we should goto open_top if comparam() slept. 146951078Speter */ 147051078Speter if (com->hasfifo) { 147151078Speter /* 147251078Speter * (Re)enable and drain fifos. 147351078Speter * 147451078Speter * Certain SMC chips cause problems if the fifos 147551078Speter * are enabled while input is ready. Turn off the 147651078Speter * fifo if necessary to clear the input. We test 147751078Speter * the input ready bit after enabling the fifos 147851078Speter * since we've already enabled them in comparam() 147951078Speter * and to handle races between enabling and fresh 148051078Speter * input. 148151078Speter */ 148251078Speter while (TRUE) { 148360471Snyan sio_setreg(com, com_fifo, 148460471Snyan FIFO_RCV_RST | FIFO_XMT_RST 148560471Snyan | com->fifo_image); 148651078Speter /* 148751078Speter * XXX the delays are for superstitious 148851078Speter * historical reasons. It must be less than 148951078Speter * the character time at the maximum 149051078Speter * supported speed (87 usec at 115200 bps 149151078Speter * 8N1). Otherwise we might loop endlessly 149251078Speter * if data is streaming in. We used to use 149351078Speter * delays of 100. That usually worked 149451078Speter * because DELAY(100) used to usually delay 149551078Speter * for about 85 usec instead of 100. 149651078Speter */ 149751078Speter DELAY(50); 149851078Speter if (!(inb(com->line_status_port) & LSR_RXRDY)) 149951078Speter break; 150060471Snyan sio_setreg(com, com_fifo, 0); 150151078Speter DELAY(50); 150251078Speter (void) inb(com->data_port); 150351078Speter } 150451078Speter } 150551078Speter 150672200Sbmilekic mtx_lock_spin(&sio_lock); 150751078Speter (void) inb(com->line_status_port); 150851078Speter (void) inb(com->data_port); 150951078Speter com->prev_modem_status = com->last_modem_status 151051078Speter = inb(com->modem_status_port); 151151078Speter if (COM_IIR_TXRDYBUG(com->flags)) { 151251078Speter outb(com->intr_ctl_port, IER_ERXRDY | IER_ERLS 151351078Speter | IER_EMSC); 151451078Speter } else { 151551078Speter outb(com->intr_ctl_port, IER_ERXRDY | IER_ETXRDY 151651078Speter | IER_ERLS | IER_EMSC); 151751078Speter } 151872200Sbmilekic mtx_unlock_spin(&sio_lock); 151951078Speter /* 152051078Speter * Handle initial DCD. Callout devices get a fake initial 152151078Speter * DCD (trapdoor DCD). If we are callout, then any sleeping 152251078Speter * callin opens get woken up and resume sleeping on "siobi" 152351078Speter * instead of "siodcd". 152451078Speter */ 152551078Speter /* 152651078Speter * XXX `mynor & CALLOUT_MASK' should be 152751078Speter * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where 152851078Speter * TRAPDOOR_CARRIER is the default initial state for callout 152951078Speter * devices and SOFT_CARRIER is like CLOCAL except it hides 153051078Speter * the true carrier. 153151078Speter */ 153251078Speter if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK) 153351078Speter (*linesw[tp->t_line].l_modem)(tp, 1); 153451078Speter } 153551078Speter /* 153651078Speter * Wait for DCD if necessary. 153751078Speter */ 153851078Speter if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK) 153951078Speter && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) { 154051078Speter ++com->wopeners; 154151078Speter error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "siodcd", 0); 154251078Speter if (com_addr(unit) == NULL) 154351078Speter return (ENXIO); 154451078Speter --com->wopeners; 154551078Speter if (error != 0 || com->gone) 154651078Speter goto out; 154751078Speter goto open_top; 154851078Speter } 154951078Speter error = (*linesw[tp->t_line].l_open)(dev, tp); 155051078Speter disc_optim(tp, &tp->t_termios, com); 155151078Speter if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK) 155251078Speter com->active_out = TRUE; 155351078Speter siosettimeout(); 155451078Speterout: 155551078Speter splx(s); 155651078Speter if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0) 155751078Speter comhardclose(com); 155851078Speter return (error); 155951078Speter} 156051078Speter 156151078Speterstatic int 156251078Spetersioclose(dev, flag, mode, p) 156351078Speter dev_t dev; 156451078Speter int flag; 156551078Speter int mode; 156651078Speter struct proc *p; 156751078Speter{ 156851078Speter struct com_s *com; 156951078Speter int mynor; 157051078Speter int s; 157151078Speter struct tty *tp; 157251078Speter 157351078Speter mynor = minor(dev); 157451078Speter if (mynor & CONTROL_MASK) 157551078Speter return (0); 157651078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 157757915Simp if (com == NULL) 157857915Simp return (ENODEV); 157951078Speter tp = com->tp; 158051078Speter s = spltty(); 158151078Speter (*linesw[tp->t_line].l_close)(tp, flag); 158251078Speter disc_optim(tp, &tp->t_termios, com); 158351654Sphk comstop(tp, FREAD | FWRITE); 158451078Speter comhardclose(com); 158551078Speter ttyclose(tp); 158651078Speter siosettimeout(); 158751078Speter splx(s); 158851078Speter if (com->gone) { 158951078Speter printf("sio%d: gone\n", com->unit); 159051078Speter s = spltty(); 159151078Speter if (com->ibuf != NULL) 159251078Speter free(com->ibuf, M_DEVBUF); 159351078Speter bzero(tp, sizeof *tp); 159451078Speter splx(s); 159551078Speter } 159651078Speter return (0); 159751078Speter} 159851078Speter 159951078Speterstatic void 160051078Spetercomhardclose(com) 160151078Speter struct com_s *com; 160251078Speter{ 160351078Speter int s; 160451078Speter struct tty *tp; 160551078Speter int unit; 160651078Speter 160751078Speter unit = com->unit; 160851078Speter s = spltty(); 160951078Speter com->poll = FALSE; 161051078Speter com->poll_output = FALSE; 161151078Speter com->do_timestamp = FALSE; 161251078Speter com->do_dcd_timestamp = FALSE; 161351078Speter com->pps.ppsparam.mode = 0; 161460471Snyan sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 161578504Siedowse tp = com->tp; 161678504Siedowse 161778504Siedowse#if defined(DDB) && (defined(BREAK_TO_DEBUGGER) || \ 161878504Siedowse defined(ALT_BREAK_TO_DEBUGGER)) 161978504Siedowse /* 162078504Siedowse * Leave interrupts enabled and don't clear DTR if this is the 162178504Siedowse * console. This allows us to detect break-to-debugger events 162278504Siedowse * while the console device is closed. 162378504Siedowse */ 162478504Siedowse if (com->unit != comconsole) 162578504Siedowse#endif 162651078Speter { 162760471Snyan sio_setreg(com, com_ier, 0); 162851078Speter if (tp->t_cflag & HUPCL 162951078Speter /* 163051078Speter * XXX we will miss any carrier drop between here and the 163151078Speter * next open. Perhaps we should watch DCD even when the 163251078Speter * port is closed; it is not sufficient to check it at 163351078Speter * the next open because it might go up and down while 163451078Speter * we're not watching. 163551078Speter */ 163651078Speter || (!com->active_out 163751078Speter && !(com->prev_modem_status & MSR_DCD) 163851078Speter && !(com->it_in.c_cflag & CLOCAL)) 163951078Speter || !(tp->t_state & TS_ISOPEN)) { 164051078Speter (void)commctl(com, TIOCM_DTR, DMBIC); 164151078Speter if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) { 164251078Speter timeout(siodtrwakeup, com, com->dtr_wait); 164351078Speter com->state |= CS_DTR_OFF; 164451078Speter } 164551078Speter } 164651078Speter } 164751078Speter if (com->hasfifo) { 164851078Speter /* 164951078Speter * Disable fifos so that they are off after controlled 165051078Speter * reboots. Some BIOSes fail to detect 16550s when the 165151078Speter * fifos are enabled. 165251078Speter */ 165360471Snyan sio_setreg(com, com_fifo, 0); 165451078Speter } 165551078Speter com->active_out = FALSE; 165651078Speter wakeup(&com->active_out); 165751078Speter wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */ 165851078Speter splx(s); 165951078Speter} 166051078Speter 166151078Speterstatic int 166251078Spetersioread(dev, uio, flag) 166351078Speter dev_t dev; 166451078Speter struct uio *uio; 166551078Speter int flag; 166651078Speter{ 166751078Speter int mynor; 166851078Speter struct com_s *com; 166951078Speter 167051078Speter mynor = minor(dev); 167151078Speter if (mynor & CONTROL_MASK) 167251078Speter return (ENODEV); 167351078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 167457915Simp if (com == NULL || com->gone) 167551078Speter return (ENODEV); 167651078Speter return ((*linesw[com->tp->t_line].l_read)(com->tp, uio, flag)); 167751078Speter} 167851078Speter 167951078Speterstatic int 168051078Spetersiowrite(dev, uio, flag) 168151078Speter dev_t dev; 168251078Speter struct uio *uio; 168351078Speter int flag; 168451078Speter{ 168551078Speter int mynor; 168651078Speter struct com_s *com; 168751078Speter int unit; 168851078Speter 168951078Speter mynor = minor(dev); 169051078Speter if (mynor & CONTROL_MASK) 169151078Speter return (ENODEV); 169251078Speter 169351078Speter unit = MINOR_TO_UNIT(mynor); 169451078Speter com = com_addr(unit); 169557915Simp if (com == NULL || com->gone) 169651078Speter return (ENODEV); 169751078Speter /* 169851078Speter * (XXX) We disallow virtual consoles if the physical console is 169951078Speter * a serial port. This is in case there is a display attached that 170051078Speter * is not the console. In that situation we don't need/want the X 170151078Speter * server taking over the console. 170251078Speter */ 170351078Speter if (constty != NULL && unit == comconsole) 170451078Speter constty = NULL; 170551078Speter return ((*linesw[com->tp->t_line].l_write)(com->tp, uio, flag)); 170651078Speter} 170751078Speter 170851078Speterstatic void 170951078Spetersiobusycheck(chan) 171051078Speter void *chan; 171151078Speter{ 171251078Speter struct com_s *com; 171351078Speter int s; 171451078Speter 171551078Speter com = (struct com_s *)chan; 171651078Speter 171751078Speter /* 171851078Speter * Clear TS_BUSY if low-level output is complete. 171951078Speter * spl locking is sufficient because siointr1() does not set CS_BUSY. 172051078Speter * If siointr1() clears CS_BUSY after we look at it, then we'll get 172151078Speter * called again. Reading the line status port outside of siointr1() 172251078Speter * is safe because CS_BUSY is clear so there are no output interrupts 172351078Speter * to lose. 172451078Speter */ 172551078Speter s = spltty(); 172651078Speter if (com->state & CS_BUSY) 172751078Speter com->extra_state &= ~CSE_BUSYCHECK; /* False alarm. */ 172851078Speter else if ((inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY)) 172951078Speter == (LSR_TSRE | LSR_TXRDY)) { 173051078Speter com->tp->t_state &= ~TS_BUSY; 173151078Speter ttwwakeup(com->tp); 173251078Speter com->extra_state &= ~CSE_BUSYCHECK; 173351078Speter } else 173451078Speter timeout(siobusycheck, com, hz / 100); 173551078Speter splx(s); 173651078Speter} 173751078Speter 173851078Speterstatic void 173951078Spetersiodtrwakeup(chan) 174051078Speter void *chan; 174151078Speter{ 174251078Speter struct com_s *com; 174351078Speter 174451078Speter com = (struct com_s *)chan; 174551078Speter com->state &= ~CS_DTR_OFF; 174651078Speter wakeup(&com->dtr_wait); 174751078Speter} 174851078Speter 174965557Sjasone/* 175070174Sjhb * Call this function with the sio_lock mutex held. It will return with the 175170174Sjhb * lock still held. 175265557Sjasone */ 175351078Speterstatic void 175451078Spetersioinput(com) 175551078Speter struct com_s *com; 175651078Speter{ 175751078Speter u_char *buf; 175851078Speter int incc; 175951078Speter u_char line_status; 176051078Speter int recv_data; 176151078Speter struct tty *tp; 176251078Speter 176351078Speter buf = com->ibuf; 176451078Speter tp = com->tp; 176551078Speter if (!(tp->t_state & TS_ISOPEN) || !(tp->t_cflag & CREAD)) { 176651078Speter com_events -= (com->iptr - com->ibuf); 176751078Speter com->iptr = com->ibuf; 176851078Speter return; 176951078Speter } 177051078Speter if (tp->t_state & TS_CAN_BYPASS_L_RINT) { 177151078Speter /* 177251078Speter * Avoid the grotesquely inefficient lineswitch routine 177351078Speter * (ttyinput) in "raw" mode. It usually takes about 450 177451078Speter * instructions (that's without canonical processing or echo!). 177551078Speter * slinput is reasonably fast (usually 40 instructions plus 177651078Speter * call overhead). 177751078Speter */ 177851078Speter do { 177965557Sjasone /* 178065557Sjasone * This may look odd, but it is using save-and-enable 178165557Sjasone * semantics instead of the save-and-disable semantics 178265557Sjasone * that are used everywhere else. 178365557Sjasone */ 178472200Sbmilekic mtx_unlock_spin(&sio_lock); 178551078Speter incc = com->iptr - buf; 178651078Speter if (tp->t_rawq.c_cc + incc > tp->t_ihiwat 178751078Speter && (com->state & CS_RTS_IFLOW 178851078Speter || tp->t_iflag & IXOFF) 178951078Speter && !(tp->t_state & TS_TBLOCK)) 179051078Speter ttyblock(tp); 179151078Speter com->delta_error_counts[CE_TTY_BUF_OVERFLOW] 179251078Speter += b_to_q((char *)buf, incc, &tp->t_rawq); 179351078Speter buf += incc; 179451078Speter tk_nin += incc; 179551078Speter tk_rawcc += incc; 179651078Speter tp->t_rawcc += incc; 179751078Speter ttwakeup(tp); 179851078Speter if (tp->t_state & TS_TTSTOP 179951078Speter && (tp->t_iflag & IXANY 180051078Speter || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) { 180151078Speter tp->t_state &= ~TS_TTSTOP; 180251078Speter tp->t_lflag &= ~FLUSHO; 180351078Speter comstart(tp); 180451078Speter } 180572200Sbmilekic mtx_lock_spin(&sio_lock); 180651078Speter } while (buf < com->iptr); 180751078Speter } else { 180851078Speter do { 180965557Sjasone /* 181065557Sjasone * This may look odd, but it is using save-and-enable 181165557Sjasone * semantics instead of the save-and-disable semantics 181265557Sjasone * that are used everywhere else. 181365557Sjasone */ 181472200Sbmilekic mtx_unlock_spin(&sio_lock); 181551078Speter line_status = buf[com->ierroff]; 181651078Speter recv_data = *buf++; 181751078Speter if (line_status 181851078Speter & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) { 181951078Speter if (line_status & LSR_BI) 182051078Speter recv_data |= TTY_BI; 182151078Speter if (line_status & LSR_FE) 182251078Speter recv_data |= TTY_FE; 182351078Speter if (line_status & LSR_OE) 182451078Speter recv_data |= TTY_OE; 182551078Speter if (line_status & LSR_PE) 182651078Speter recv_data |= TTY_PE; 182751078Speter } 182851078Speter (*linesw[tp->t_line].l_rint)(recv_data, tp); 182972200Sbmilekic mtx_lock_spin(&sio_lock); 183051078Speter } while (buf < com->iptr); 183151078Speter } 183251078Speter com_events -= (com->iptr - com->ibuf); 183351078Speter com->iptr = com->ibuf; 183451078Speter 183551078Speter /* 183651078Speter * There is now room for another low-level buffer full of input, 183751078Speter * so enable RTS if it is now disabled and there is room in the 183851078Speter * high-level buffer. 183951078Speter */ 184051078Speter if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & MCR_RTS) && 184151078Speter !(tp->t_state & TS_TBLOCK)) 184251078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 184351078Speter} 184451078Speter 184551078Spetervoid 184651078Spetersiointr(arg) 184751078Speter void *arg; 184851078Speter{ 184970174Sjhb struct com_s *com; 185070174Sjhb 185151078Speter#ifndef COM_MULTIPORT 185270174Sjhb com = (struct com_s *)arg; 185370174Sjhb 185472200Sbmilekic mtx_lock_spin(&sio_lock); 185570174Sjhb siointr1(com); 185672200Sbmilekic mtx_unlock_spin(&sio_lock); 185751078Speter#else /* COM_MULTIPORT */ 185851078Speter bool_t possibly_more_intrs; 185951078Speter int unit; 186051078Speter 186151078Speter /* 186251078Speter * Loop until there is no activity on any port. This is necessary 186351078Speter * to get an interrupt edge more than to avoid another interrupt. 186451078Speter * If the IRQ signal is just an OR of the IRQ signals from several 186551078Speter * devices, then the edge from one may be lost because another is 186651078Speter * on. 186751078Speter */ 186872200Sbmilekic mtx_lock_spin(&sio_lock); 186951078Speter do { 187051078Speter possibly_more_intrs = FALSE; 187153344Speter for (unit = 0; unit < sio_numunits; ++unit) { 187251078Speter com = com_addr(unit); 187351078Speter /* 187451078Speter * XXX COM_LOCK(); 187551078Speter * would it work here, or be counter-productive? 187651078Speter */ 187751078Speter if (com != NULL 187851078Speter && !com->gone 187951078Speter && (inb(com->int_id_port) & IIR_IMASK) 188051078Speter != IIR_NOPEND) { 188151078Speter siointr1(com); 188251078Speter possibly_more_intrs = TRUE; 188351078Speter } 188451078Speter /* XXX COM_UNLOCK(); */ 188551078Speter } 188651078Speter } while (possibly_more_intrs); 188772200Sbmilekic mtx_unlock_spin(&sio_lock); 188851078Speter#endif /* COM_MULTIPORT */ 188951078Speter} 189051078Speter 189151078Speterstatic void 189251078Spetersiointr1(com) 189351078Speter struct com_s *com; 189451078Speter{ 189551078Speter u_char line_status; 189651078Speter u_char modem_status; 189751078Speter u_char *ioptr; 189851078Speter u_char recv_data; 189951078Speter u_char int_ctl; 190051078Speter u_char int_ctl_new; 190151078Speter struct timecounter *tc; 190251078Speter u_int count; 190351078Speter 190451078Speter int_ctl = inb(com->intr_ctl_port); 190551078Speter int_ctl_new = int_ctl; 190651078Speter 190751078Speter while (!com->gone) { 190851078Speter if (com->pps.ppsparam.mode & PPS_CAPTUREBOTH) { 190951078Speter modem_status = inb(com->modem_status_port); 191051078Speter if ((modem_status ^ com->last_modem_status) & MSR_DCD) { 191151078Speter tc = timecounter; 191251078Speter count = tc->tc_get_timecount(tc); 191351078Speter pps_event(&com->pps, tc, count, 191451078Speter (modem_status & MSR_DCD) ? 191551078Speter PPS_CAPTUREASSERT : PPS_CAPTURECLEAR); 191651078Speter } 191751078Speter } 191851078Speter line_status = inb(com->line_status_port); 191951078Speter 192051078Speter /* input event? (check first to help avoid overruns) */ 192151078Speter while (line_status & LSR_RCV_MASK) { 192251078Speter /* break/unnattached error bits or real input? */ 192351078Speter if (!(line_status & LSR_RXRDY)) 192451078Speter recv_data = 0; 192551078Speter else 192651078Speter recv_data = inb(com->data_port); 192761649Sps#if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER) 192861649Sps /* 192961649Sps * Solaris implements a new BREAK which is initiated 193061649Sps * by a character sequence CR ~ ^b which is similar 193161649Sps * to a familiar pattern used on Sun servers by the 193261649Sps * Remote Console. 193361649Sps */ 193461649Sps#define KEY_CRTLB 2 /* ^B */ 193561649Sps#define KEY_CR 13 /* CR '\r' */ 193661649Sps#define KEY_TILDE 126 /* ~ */ 193761649Sps 193861649Sps if (com->unit == comconsole) { 193961649Sps static int brk_state1 = 0, brk_state2 = 0; 194061649Sps if (recv_data == KEY_CR) { 194161649Sps brk_state1 = recv_data; 194261649Sps brk_state2 = 0; 194365557Sjasone } else if (brk_state1 == KEY_CR 194465557Sjasone && (recv_data == KEY_TILDE 194565557Sjasone || recv_data == KEY_CRTLB)) { 194661649Sps if (recv_data == KEY_TILDE) 194761649Sps brk_state2 = recv_data; 194865557Sjasone else if (brk_state2 == KEY_TILDE 194965557Sjasone && recv_data == KEY_CRTLB) { 195061649Sps breakpoint(); 195165557Sjasone brk_state1 = 0; 195265557Sjasone brk_state2 = 0; 195361649Sps goto cont; 195461649Sps } else 195561649Sps brk_state2 = 0; 195661649Sps } else 195761649Sps brk_state1 = 0; 195861649Sps } 195961649Sps#endif 196051078Speter if (line_status & (LSR_BI | LSR_FE | LSR_PE)) { 196151078Speter /* 196251078Speter * Don't store BI if IGNBRK or FE/PE if IGNPAR. 196351078Speter * Otherwise, push the work to a higher level 196451078Speter * (to handle PARMRK) if we're bypassing. 196551078Speter * Otherwise, convert BI/FE and PE+INPCK to 0. 196651078Speter * 196751078Speter * This makes bypassing work right in the 196851078Speter * usual "raw" case (IGNBRK set, and IGNPAR 196951078Speter * and INPCK clear). 197051078Speter * 197151078Speter * Note: BI together with FE/PE means just BI. 197251078Speter */ 197351078Speter if (line_status & LSR_BI) { 197451078Speter#if defined(DDB) && defined(BREAK_TO_DEBUGGER) 197551078Speter if (com->unit == comconsole) { 197651078Speter breakpoint(); 197751078Speter goto cont; 197851078Speter } 197951078Speter#endif 198051078Speter if (com->tp == NULL 198151078Speter || com->tp->t_iflag & IGNBRK) 198251078Speter goto cont; 198351078Speter } else { 198451078Speter if (com->tp == NULL 198551078Speter || com->tp->t_iflag & IGNPAR) 198651078Speter goto cont; 198751078Speter } 198851078Speter if (com->tp->t_state & TS_CAN_BYPASS_L_RINT 198951078Speter && (line_status & (LSR_BI | LSR_FE) 199051078Speter || com->tp->t_iflag & INPCK)) 199151078Speter recv_data = 0; 199251078Speter } 199351078Speter ++com->bytes_in; 199451078Speter if (com->hotchar != 0 && recv_data == com->hotchar) 199572238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 199651078Speter ioptr = com->iptr; 199751078Speter if (ioptr >= com->ibufend) 199851078Speter CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW); 199951078Speter else { 200051078Speter if (com->do_timestamp) 200151078Speter microtime(&com->timestamp); 200251078Speter ++com_events; 200372238Sjhb swi_sched(sio_slow_ih, SWI_DELAY); 200451078Speter#if 0 /* for testing input latency vs efficiency */ 200551078Speterif (com->iptr - com->ibuf == 8) 200672238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 200751078Speter#endif 200851078Speter ioptr[0] = recv_data; 200951078Speter ioptr[com->ierroff] = line_status; 201051078Speter com->iptr = ++ioptr; 201151078Speter if (ioptr == com->ihighwater 201251078Speter && com->state & CS_RTS_IFLOW) 201351078Speter outb(com->modem_ctl_port, 201451078Speter com->mcr_image &= ~MCR_RTS); 201551078Speter if (line_status & LSR_OE) 201651078Speter CE_RECORD(com, CE_OVERRUN); 201751078Speter } 201851078Spetercont: 201951078Speter /* 202051078Speter * "& 0x7F" is to avoid the gcc-1.40 generating a slow 202151078Speter * jump from the top of the loop to here 202251078Speter */ 202351078Speter line_status = inb(com->line_status_port) & 0x7F; 202451078Speter } 202551078Speter 202651078Speter /* modem status change? (always check before doing output) */ 202751078Speter modem_status = inb(com->modem_status_port); 202851078Speter if (modem_status != com->last_modem_status) { 202951078Speter if (com->do_dcd_timestamp 203051078Speter && !(com->last_modem_status & MSR_DCD) 203151078Speter && modem_status & MSR_DCD) 203251078Speter microtime(&com->dcd_timestamp); 203351078Speter 203451078Speter /* 203551078Speter * Schedule high level to handle DCD changes. Note 203651078Speter * that we don't use the delta bits anywhere. Some 203751078Speter * UARTs mess them up, and it's easy to remember the 203851078Speter * previous bits and calculate the delta. 203951078Speter */ 204051078Speter com->last_modem_status = modem_status; 204151078Speter if (!(com->state & CS_CHECKMSR)) { 204251078Speter com_events += LOTS_OF_EVENTS; 204351078Speter com->state |= CS_CHECKMSR; 204472238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 204551078Speter } 204651078Speter 204751078Speter /* handle CTS change immediately for crisp flow ctl */ 204851078Speter if (com->state & CS_CTS_OFLOW) { 204951078Speter if (modem_status & MSR_CTS) 205051078Speter com->state |= CS_ODEVREADY; 205151078Speter else 205251078Speter com->state &= ~CS_ODEVREADY; 205351078Speter } 205451078Speter } 205551078Speter 205651078Speter /* output queued and everything ready? */ 205751078Speter if (line_status & LSR_TXRDY 205851078Speter && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) { 205951078Speter ioptr = com->obufq.l_head; 206051078Speter if (com->tx_fifo_size > 1) { 206151078Speter u_int ocount; 206251078Speter 206351078Speter ocount = com->obufq.l_tail - ioptr; 206451078Speter if (ocount > com->tx_fifo_size) 206551078Speter ocount = com->tx_fifo_size; 206651078Speter com->bytes_out += ocount; 206751078Speter do 206851078Speter outb(com->data_port, *ioptr++); 206951078Speter while (--ocount != 0); 207051078Speter } else { 207151078Speter outb(com->data_port, *ioptr++); 207251078Speter ++com->bytes_out; 207351078Speter } 207451078Speter com->obufq.l_head = ioptr; 207551078Speter if (COM_IIR_TXRDYBUG(com->flags)) { 207651078Speter int_ctl_new = int_ctl | IER_ETXRDY; 207751078Speter } 207851078Speter if (ioptr >= com->obufq.l_tail) { 207951078Speter struct lbq *qp; 208051078Speter 208151078Speter qp = com->obufq.l_next; 208251078Speter qp->l_queued = FALSE; 208351078Speter qp = qp->l_next; 208451078Speter if (qp != NULL) { 208551078Speter com->obufq.l_head = qp->l_head; 208651078Speter com->obufq.l_tail = qp->l_tail; 208751078Speter com->obufq.l_next = qp; 208851078Speter } else { 208951078Speter /* output just completed */ 209053344Speter if (COM_IIR_TXRDYBUG(com->flags)) { 209151078Speter int_ctl_new = int_ctl & ~IER_ETXRDY; 209251078Speter } 209351078Speter com->state &= ~CS_BUSY; 209451078Speter } 209551078Speter if (!(com->state & CS_ODONE)) { 209651078Speter com_events += LOTS_OF_EVENTS; 209751078Speter com->state |= CS_ODONE; 209867551Sjhb /* handle at high level ASAP */ 209972238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 210051078Speter } 210151078Speter } 210253344Speter if (COM_IIR_TXRDYBUG(com->flags) && (int_ctl != int_ctl_new)) { 210351078Speter outb(com->intr_ctl_port, int_ctl_new); 210451078Speter } 210551078Speter } 210651078Speter 210751078Speter /* finished? */ 210851078Speter#ifndef COM_MULTIPORT 210951078Speter if ((inb(com->int_id_port) & IIR_IMASK) == IIR_NOPEND) 211051078Speter#endif /* COM_MULTIPORT */ 211151078Speter return; 211251078Speter } 211351078Speter} 211451078Speter 211551078Speterstatic int 211651078Spetersioioctl(dev, cmd, data, flag, p) 211751078Speter dev_t dev; 211851078Speter u_long cmd; 211951078Speter caddr_t data; 212051078Speter int flag; 212151078Speter struct proc *p; 212251078Speter{ 212351078Speter struct com_s *com; 212451078Speter int error; 212551078Speter int mynor; 212651078Speter int s; 212751078Speter struct tty *tp; 212851078Speter#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 212951078Speter u_long oldcmd; 213051078Speter struct termios term; 213151078Speter#endif 213251078Speter 213351078Speter mynor = minor(dev); 213451078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 213557915Simp if (com == NULL || com->gone) 213651078Speter return (ENODEV); 213751078Speter if (mynor & CONTROL_MASK) { 213851078Speter struct termios *ct; 213951078Speter 214051078Speter switch (mynor & CONTROL_MASK) { 214151078Speter case CONTROL_INIT_STATE: 214251078Speter ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in; 214351078Speter break; 214451078Speter case CONTROL_LOCK_STATE: 214551078Speter ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in; 214651078Speter break; 214751078Speter default: 214851078Speter return (ENODEV); /* /dev/nodev */ 214951078Speter } 215051078Speter switch (cmd) { 215151078Speter case TIOCSETA: 215251078Speter error = suser(p); 215351078Speter if (error != 0) 215451078Speter return (error); 215551078Speter *ct = *(struct termios *)data; 215651078Speter return (0); 215751078Speter case TIOCGETA: 215851078Speter *(struct termios *)data = *ct; 215951078Speter return (0); 216051078Speter case TIOCGETD: 216151078Speter *(int *)data = TTYDISC; 216251078Speter return (0); 216351078Speter case TIOCGWINSZ: 216451078Speter bzero(data, sizeof(struct winsize)); 216551078Speter return (0); 216651078Speter default: 216751078Speter return (ENOTTY); 216851078Speter } 216951078Speter } 217051078Speter tp = com->tp; 217151078Speter#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 217251078Speter term = tp->t_termios; 217351078Speter oldcmd = cmd; 217451078Speter error = ttsetcompat(tp, &cmd, data, &term); 217551078Speter if (error != 0) 217651078Speter return (error); 217751078Speter if (cmd != oldcmd) 217851078Speter data = (caddr_t)&term; 217951078Speter#endif 218051078Speter if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) { 218151078Speter int cc; 218251078Speter struct termios *dt = (struct termios *)data; 218351078Speter struct termios *lt = mynor & CALLOUT_MASK 218451078Speter ? &com->lt_out : &com->lt_in; 218551078Speter 218651078Speter dt->c_iflag = (tp->t_iflag & lt->c_iflag) 218751078Speter | (dt->c_iflag & ~lt->c_iflag); 218851078Speter dt->c_oflag = (tp->t_oflag & lt->c_oflag) 218951078Speter | (dt->c_oflag & ~lt->c_oflag); 219051078Speter dt->c_cflag = (tp->t_cflag & lt->c_cflag) 219151078Speter | (dt->c_cflag & ~lt->c_cflag); 219251078Speter dt->c_lflag = (tp->t_lflag & lt->c_lflag) 219351078Speter | (dt->c_lflag & ~lt->c_lflag); 219451078Speter for (cc = 0; cc < NCCS; ++cc) 219551078Speter if (lt->c_cc[cc] != 0) 219651078Speter dt->c_cc[cc] = tp->t_cc[cc]; 219751078Speter if (lt->c_ispeed != 0) 219851078Speter dt->c_ispeed = tp->t_ispeed; 219951078Speter if (lt->c_ospeed != 0) 220051078Speter dt->c_ospeed = tp->t_ospeed; 220151078Speter } 220251078Speter error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p); 220351078Speter if (error != ENOIOCTL) 220451078Speter return (error); 220551078Speter s = spltty(); 220651078Speter error = ttioctl(tp, cmd, data, flag); 220751078Speter disc_optim(tp, &tp->t_termios, com); 220851078Speter if (error != ENOIOCTL) { 220951078Speter splx(s); 221051078Speter return (error); 221151078Speter } 221251078Speter switch (cmd) { 221351078Speter case TIOCSBRK: 221460471Snyan sio_setreg(com, com_cfcr, com->cfcr_image |= CFCR_SBREAK); 221551078Speter break; 221651078Speter case TIOCCBRK: 221760471Snyan sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 221851078Speter break; 221951078Speter case TIOCSDTR: 222051078Speter (void)commctl(com, TIOCM_DTR, DMBIS); 222151078Speter break; 222251078Speter case TIOCCDTR: 222351078Speter (void)commctl(com, TIOCM_DTR, DMBIC); 222451078Speter break; 222551078Speter /* 222651078Speter * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The 222751078Speter * changes get undone on the next call to comparam(). 222851078Speter */ 222951078Speter case TIOCMSET: 223051078Speter (void)commctl(com, *(int *)data, DMSET); 223151078Speter break; 223251078Speter case TIOCMBIS: 223351078Speter (void)commctl(com, *(int *)data, DMBIS); 223451078Speter break; 223551078Speter case TIOCMBIC: 223651078Speter (void)commctl(com, *(int *)data, DMBIC); 223751078Speter break; 223851078Speter case TIOCMGET: 223951078Speter *(int *)data = commctl(com, 0, DMGET); 224051078Speter break; 224151078Speter case TIOCMSDTRWAIT: 224251078Speter /* must be root since the wait applies to following logins */ 224351078Speter error = suser(p); 224451078Speter if (error != 0) { 224551078Speter splx(s); 224651078Speter return (error); 224751078Speter } 224851078Speter com->dtr_wait = *(int *)data * hz / 100; 224951078Speter break; 225051078Speter case TIOCMGDTRWAIT: 225151078Speter *(int *)data = com->dtr_wait * 100 / hz; 225251078Speter break; 225351078Speter case TIOCTIMESTAMP: 225451078Speter com->do_timestamp = TRUE; 225551078Speter *(struct timeval *)data = com->timestamp; 225651078Speter break; 225751078Speter case TIOCDCDTIMESTAMP: 225851078Speter com->do_dcd_timestamp = TRUE; 225951078Speter *(struct timeval *)data = com->dcd_timestamp; 226051078Speter break; 226151078Speter default: 226251078Speter splx(s); 226351078Speter error = pps_ioctl(cmd, data, &com->pps); 226451078Speter if (error == ENODEV) 226551078Speter error = ENOTTY; 226651078Speter return (error); 226751078Speter } 226851078Speter splx(s); 226951078Speter return (0); 227051078Speter} 227151078Speter 227265557Sjasone/* software interrupt handler for SWI_TTY */ 227351078Speterstatic void 227467551Sjhbsiopoll(void *dummy) 227551078Speter{ 227651078Speter int unit; 227751078Speter 227851078Speter if (com_events == 0) 227951078Speter return; 228051078Speterrepeat: 228153344Speter for (unit = 0; unit < sio_numunits; ++unit) { 228251078Speter struct com_s *com; 228351078Speter int incc; 228451078Speter struct tty *tp; 228551078Speter 228651078Speter com = com_addr(unit); 228751078Speter if (com == NULL) 228851078Speter continue; 228951078Speter tp = com->tp; 229051078Speter if (tp == NULL || com->gone) { 229151078Speter /* 229251078Speter * Discard any events related to never-opened or 229351078Speter * going-away devices. 229451078Speter */ 229572200Sbmilekic mtx_lock_spin(&sio_lock); 229651078Speter incc = com->iptr - com->ibuf; 229751078Speter com->iptr = com->ibuf; 229851078Speter if (com->state & CS_CHECKMSR) { 229951078Speter incc += LOTS_OF_EVENTS; 230051078Speter com->state &= ~CS_CHECKMSR; 230151078Speter } 230251078Speter com_events -= incc; 230372200Sbmilekic mtx_unlock_spin(&sio_lock); 230451078Speter continue; 230551078Speter } 230651078Speter if (com->iptr != com->ibuf) { 230772200Sbmilekic mtx_lock_spin(&sio_lock); 230851078Speter sioinput(com); 230972200Sbmilekic mtx_unlock_spin(&sio_lock); 231051078Speter } 231151078Speter if (com->state & CS_CHECKMSR) { 231251078Speter u_char delta_modem_status; 231351078Speter 231472200Sbmilekic mtx_lock_spin(&sio_lock); 231551078Speter delta_modem_status = com->last_modem_status 231651078Speter ^ com->prev_modem_status; 231751078Speter com->prev_modem_status = com->last_modem_status; 231851078Speter com_events -= LOTS_OF_EVENTS; 231951078Speter com->state &= ~CS_CHECKMSR; 232072200Sbmilekic mtx_unlock_spin(&sio_lock); 232151078Speter if (delta_modem_status & MSR_DCD) 232251078Speter (*linesw[tp->t_line].l_modem) 232351078Speter (tp, com->prev_modem_status & MSR_DCD); 232451078Speter } 232551078Speter if (com->state & CS_ODONE) { 232672200Sbmilekic mtx_lock_spin(&sio_lock); 232751078Speter com_events -= LOTS_OF_EVENTS; 232851078Speter com->state &= ~CS_ODONE; 232972200Sbmilekic mtx_unlock_spin(&sio_lock); 233051078Speter if (!(com->state & CS_BUSY) 233151078Speter && !(com->extra_state & CSE_BUSYCHECK)) { 233251078Speter timeout(siobusycheck, com, hz / 100); 233351078Speter com->extra_state |= CSE_BUSYCHECK; 233451078Speter } 233551078Speter (*linesw[tp->t_line].l_start)(tp); 233651078Speter } 233751078Speter if (com_events == 0) 233851078Speter break; 233951078Speter } 234051078Speter if (com_events >= LOTS_OF_EVENTS) 234151078Speter goto repeat; 234251078Speter} 234351078Speter 234451078Speterstatic int 234551078Spetercomparam(tp, t) 234651078Speter struct tty *tp; 234751078Speter struct termios *t; 234851078Speter{ 234951078Speter u_int cfcr; 235051078Speter int cflag; 235151078Speter struct com_s *com; 235251078Speter int divisor; 235351078Speter u_char dlbh; 235451078Speter u_char dlbl; 235551078Speter int s; 235651078Speter int unit; 235751078Speter 235851078Speter /* do historical conversions */ 235951078Speter if (t->c_ispeed == 0) 236051078Speter t->c_ispeed = t->c_ospeed; 236151078Speter 236251078Speter /* check requested parameters */ 236351078Speter divisor = ttspeedtab(t->c_ospeed, comspeedtab); 236451078Speter if (divisor < 0 || (divisor > 0 && t->c_ispeed != t->c_ospeed)) 236551078Speter return (EINVAL); 236651078Speter 236751078Speter /* parameters are OK, convert them to the com struct and the device */ 236851078Speter unit = DEV_TO_UNIT(tp->t_dev); 236951078Speter com = com_addr(unit); 237057915Simp if (com == NULL) 237157915Simp return (ENODEV); 237251078Speter s = spltty(); 237351078Speter if (divisor == 0) 237451078Speter (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */ 237551078Speter else 237651078Speter (void)commctl(com, TIOCM_DTR, DMBIS); 237751078Speter cflag = t->c_cflag; 237851078Speter switch (cflag & CSIZE) { 237951078Speter case CS5: 238051078Speter cfcr = CFCR_5BITS; 238151078Speter break; 238251078Speter case CS6: 238351078Speter cfcr = CFCR_6BITS; 238451078Speter break; 238551078Speter case CS7: 238651078Speter cfcr = CFCR_7BITS; 238751078Speter break; 238851078Speter default: 238951078Speter cfcr = CFCR_8BITS; 239051078Speter break; 239151078Speter } 239251078Speter if (cflag & PARENB) { 239351078Speter cfcr |= CFCR_PENAB; 239451078Speter if (!(cflag & PARODD)) 239551078Speter cfcr |= CFCR_PEVEN; 239651078Speter } 239751078Speter if (cflag & CSTOPB) 239851078Speter cfcr |= CFCR_STOPB; 239951078Speter 240051078Speter if (com->hasfifo && divisor != 0) { 240151078Speter /* 240251078Speter * Use a fifo trigger level low enough so that the input 240351078Speter * latency from the fifo is less than about 16 msec and 240451078Speter * the total latency is less than about 30 msec. These 240551078Speter * latencies are reasonable for humans. Serial comms 240651078Speter * protocols shouldn't expect anything better since modem 240751078Speter * latencies are larger. 240851078Speter */ 240951078Speter com->fifo_image = t->c_ospeed <= 4800 241051078Speter ? FIFO_ENABLE : FIFO_ENABLE | FIFO_RX_HIGH; 241151078Speter#ifdef COM_ESP 241251078Speter /* 241351078Speter * The Hayes ESP card needs the fifo DMA mode bit set 241451078Speter * in compatibility mode. If not, it will interrupt 241551078Speter * for each character received. 241651078Speter */ 241751078Speter if (com->esp) 241851078Speter com->fifo_image |= FIFO_DMA_MODE; 241951078Speter#endif 242060471Snyan sio_setreg(com, com_fifo, com->fifo_image); 242151078Speter } 242251078Speter 242365605Sjhb /* 242465605Sjhb * This returns with interrupts disabled so that we can complete 242565605Sjhb * the speed change atomically. Keeping interrupts disabled is 242665605Sjhb * especially important while com_data is hidden. 242765605Sjhb */ 242865605Sjhb (void) siosetwater(com, t->c_ispeed); 242965557Sjasone 243051078Speter if (divisor != 0) { 243160471Snyan sio_setreg(com, com_cfcr, cfcr | CFCR_DLAB); 243251078Speter /* 243351078Speter * Only set the divisor registers if they would change, 243451078Speter * since on some 16550 incompatibles (UMC8669F), setting 243551078Speter * them while input is arriving them loses sync until 243651078Speter * data stops arriving. 243751078Speter */ 243851078Speter dlbl = divisor & 0xFF; 243960471Snyan if (sio_getreg(com, com_dlbl) != dlbl) 244060471Snyan sio_setreg(com, com_dlbl, dlbl); 244151078Speter dlbh = (u_int) divisor >> 8; 244260471Snyan if (sio_getreg(com, com_dlbh) != dlbh) 244360471Snyan sio_setreg(com, com_dlbh, dlbh); 244451078Speter } 244551078Speter 244660471Snyan sio_setreg(com, com_cfcr, com->cfcr_image = cfcr); 244751078Speter 244851078Speter if (!(tp->t_state & TS_TTSTOP)) 244951078Speter com->state |= CS_TTGO; 245051078Speter 245151078Speter if (cflag & CRTS_IFLOW) { 245251078Speter if (com->st16650a) { 245360471Snyan sio_setreg(com, com_cfcr, 0xbf); 245460471Snyan sio_setreg(com, com_fifo, 245560471Snyan sio_getreg(com, com_fifo) | 0x40); 245651078Speter } 245751078Speter com->state |= CS_RTS_IFLOW; 245851078Speter /* 245951078Speter * If CS_RTS_IFLOW just changed from off to on, the change 246051078Speter * needs to be propagated to MCR_RTS. This isn't urgent, 246151078Speter * so do it later by calling comstart() instead of repeating 246251078Speter * a lot of code from comstart() here. 246351078Speter */ 246451078Speter } else if (com->state & CS_RTS_IFLOW) { 246551078Speter com->state &= ~CS_RTS_IFLOW; 246651078Speter /* 246751078Speter * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS 246851078Speter * on here, since comstart() won't do it later. 246951078Speter */ 247051078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 247151078Speter if (com->st16650a) { 247260471Snyan sio_setreg(com, com_cfcr, 0xbf); 247360471Snyan sio_setreg(com, com_fifo, 247460471Snyan sio_getreg(com, com_fifo) & ~0x40); 247551078Speter } 247651078Speter } 247751078Speter 247851078Speter 247951078Speter /* 248051078Speter * Set up state to handle output flow control. 248151078Speter * XXX - worth handling MDMBUF (DCD) flow control at the lowest level? 248251078Speter * Now has 10+ msec latency, while CTS flow has 50- usec latency. 248351078Speter */ 248451078Speter com->state |= CS_ODEVREADY; 248551078Speter com->state &= ~CS_CTS_OFLOW; 248651078Speter if (cflag & CCTS_OFLOW) { 248751078Speter com->state |= CS_CTS_OFLOW; 248851078Speter if (!(com->last_modem_status & MSR_CTS)) 248951078Speter com->state &= ~CS_ODEVREADY; 249051078Speter if (com->st16650a) { 249160471Snyan sio_setreg(com, com_cfcr, 0xbf); 249260471Snyan sio_setreg(com, com_fifo, 249360471Snyan sio_getreg(com, com_fifo) | 0x80); 249451078Speter } 249551078Speter } else { 249651078Speter if (com->st16650a) { 249760471Snyan sio_setreg(com, com_cfcr, 0xbf); 249860471Snyan sio_setreg(com, com_fifo, 249960471Snyan sio_getreg(com, com_fifo) & ~0x80); 250051078Speter } 250151078Speter } 250251078Speter 250360471Snyan sio_setreg(com, com_cfcr, com->cfcr_image); 250451078Speter 250551078Speter /* XXX shouldn't call functions while intrs are disabled. */ 250651078Speter disc_optim(tp, t, com); 250751078Speter /* 250851078Speter * Recover from fiddling with CS_TTGO. We used to call siointr1() 250951078Speter * unconditionally, but that defeated the careful discarding of 251051078Speter * stale input in sioopen(). 251151078Speter */ 251251078Speter if (com->state >= (CS_BUSY | CS_TTGO)) 251351078Speter siointr1(com); 251451078Speter 251572200Sbmilekic mtx_unlock_spin(&sio_lock); 251651078Speter splx(s); 251751078Speter comstart(tp); 251851078Speter if (com->ibufold != NULL) { 251951078Speter free(com->ibufold, M_DEVBUF); 252051078Speter com->ibufold = NULL; 252151078Speter } 252251078Speter return (0); 252351078Speter} 252451078Speter 252565605Sjhb/* 252670174Sjhb * This function must be called with the sio_lock mutex released and will 252770174Sjhb * return with it obtained. 252865605Sjhb */ 252951078Speterstatic int 253065605Sjhbsiosetwater(com, speed) 253151078Speter struct com_s *com; 253251078Speter speed_t speed; 253351078Speter{ 253451078Speter int cp4ticks; 253551078Speter u_char *ibuf; 253651078Speter int ibufsize; 253751078Speter struct tty *tp; 253851078Speter 253951078Speter /* 254051078Speter * Make the buffer size large enough to handle a softtty interrupt 254151078Speter * latency of about 2 ticks without loss of throughput or data 254251078Speter * (about 3 ticks if input flow control is not used or not honoured, 254351078Speter * but a bit less for CS5-CS7 modes). 254451078Speter */ 254551078Speter cp4ticks = speed / 10 / hz * 4; 254651078Speter for (ibufsize = 128; ibufsize < cp4ticks;) 254751078Speter ibufsize <<= 1; 254865605Sjhb if (ibufsize == com->ibufsize) { 254972200Sbmilekic mtx_lock_spin(&sio_lock); 255051078Speter return (0); 255165605Sjhb } 255251078Speter 255351078Speter /* 255451078Speter * Allocate input buffer. The extra factor of 2 in the size is 255551078Speter * to allow for an error byte for each input byte. 255651078Speter */ 255751078Speter ibuf = malloc(2 * ibufsize, M_DEVBUF, M_NOWAIT); 255865605Sjhb if (ibuf == NULL) { 255972200Sbmilekic mtx_lock_spin(&sio_lock); 256051078Speter return (ENOMEM); 256165605Sjhb } 256251078Speter 256351078Speter /* Initialize non-critical variables. */ 256451078Speter com->ibufold = com->ibuf; 256551078Speter com->ibufsize = ibufsize; 256651078Speter tp = com->tp; 256751078Speter if (tp != NULL) { 256851078Speter tp->t_ififosize = 2 * ibufsize; 256951078Speter tp->t_ispeedwat = (speed_t)-1; 257051078Speter tp->t_ospeedwat = (speed_t)-1; 257151078Speter } 257251078Speter 257351078Speter /* 257451078Speter * Read current input buffer, if any. Continue with interrupts 257551078Speter * disabled. 257651078Speter */ 257772200Sbmilekic mtx_lock_spin(&sio_lock); 257851078Speter if (com->iptr != com->ibuf) 257951078Speter sioinput(com); 258051078Speter 258151078Speter /*- 258251078Speter * Initialize critical variables, including input buffer watermarks. 258351078Speter * The external device is asked to stop sending when the buffer 258451078Speter * exactly reaches high water, or when the high level requests it. 258551078Speter * The high level is notified immediately (rather than at a later 258651078Speter * clock tick) when this watermark is reached. 258751078Speter * The buffer size is chosen so the watermark should almost never 258851078Speter * be reached. 258951078Speter * The low watermark is invisibly 0 since the buffer is always 259051078Speter * emptied all at once. 259151078Speter */ 259251078Speter com->iptr = com->ibuf = ibuf; 259351078Speter com->ibufend = ibuf + ibufsize; 259451078Speter com->ierroff = ibufsize; 259551078Speter com->ihighwater = ibuf + 3 * ibufsize / 4; 259651078Speter return (0); 259751078Speter} 259851078Speter 259951078Speterstatic void 260051078Spetercomstart(tp) 260151078Speter struct tty *tp; 260251078Speter{ 260351078Speter struct com_s *com; 260451078Speter int s; 260551078Speter int unit; 260651078Speter 260751078Speter unit = DEV_TO_UNIT(tp->t_dev); 260851078Speter com = com_addr(unit); 260957915Simp if (com == NULL) 261057915Simp return; 261151078Speter s = spltty(); 261272200Sbmilekic mtx_lock_spin(&sio_lock); 261351078Speter if (tp->t_state & TS_TTSTOP) 261451078Speter com->state &= ~CS_TTGO; 261551078Speter else 261651078Speter com->state |= CS_TTGO; 261751078Speter if (tp->t_state & TS_TBLOCK) { 261851078Speter if (com->mcr_image & MCR_RTS && com->state & CS_RTS_IFLOW) 261951078Speter outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS); 262051078Speter } else { 262151078Speter if (!(com->mcr_image & MCR_RTS) && com->iptr < com->ihighwater 262251078Speter && com->state & CS_RTS_IFLOW) 262351078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 262451078Speter } 262572200Sbmilekic mtx_unlock_spin(&sio_lock); 262651078Speter if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) { 262751078Speter ttwwakeup(tp); 262851078Speter splx(s); 262951078Speter return; 263051078Speter } 263151078Speter if (tp->t_outq.c_cc != 0) { 263251078Speter struct lbq *qp; 263351078Speter struct lbq *next; 263451078Speter 263551078Speter if (!com->obufs[0].l_queued) { 263651078Speter com->obufs[0].l_tail 263751078Speter = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1, 263851078Speter sizeof com->obuf1); 263951078Speter com->obufs[0].l_next = NULL; 264051078Speter com->obufs[0].l_queued = TRUE; 264172200Sbmilekic mtx_lock_spin(&sio_lock); 264251078Speter if (com->state & CS_BUSY) { 264351078Speter qp = com->obufq.l_next; 264451078Speter while ((next = qp->l_next) != NULL) 264551078Speter qp = next; 264651078Speter qp->l_next = &com->obufs[0]; 264751078Speter } else { 264851078Speter com->obufq.l_head = com->obufs[0].l_head; 264951078Speter com->obufq.l_tail = com->obufs[0].l_tail; 265051078Speter com->obufq.l_next = &com->obufs[0]; 265151078Speter com->state |= CS_BUSY; 265251078Speter } 265372200Sbmilekic mtx_unlock_spin(&sio_lock); 265451078Speter } 265551078Speter if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) { 265651078Speter com->obufs[1].l_tail 265751078Speter = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2, 265851078Speter sizeof com->obuf2); 265951078Speter com->obufs[1].l_next = NULL; 266051078Speter com->obufs[1].l_queued = TRUE; 266172200Sbmilekic mtx_lock_spin(&sio_lock); 266251078Speter if (com->state & CS_BUSY) { 266351078Speter qp = com->obufq.l_next; 266451078Speter while ((next = qp->l_next) != NULL) 266551078Speter qp = next; 266651078Speter qp->l_next = &com->obufs[1]; 266751078Speter } else { 266851078Speter com->obufq.l_head = com->obufs[1].l_head; 266951078Speter com->obufq.l_tail = com->obufs[1].l_tail; 267051078Speter com->obufq.l_next = &com->obufs[1]; 267151078Speter com->state |= CS_BUSY; 267251078Speter } 267372200Sbmilekic mtx_unlock_spin(&sio_lock); 267451078Speter } 267551078Speter tp->t_state |= TS_BUSY; 267651078Speter } 267772200Sbmilekic mtx_lock_spin(&sio_lock); 267851078Speter if (com->state >= (CS_BUSY | CS_TTGO)) 267951078Speter siointr1(com); /* fake interrupt to start output */ 268072200Sbmilekic mtx_unlock_spin(&sio_lock); 268151078Speter ttwwakeup(tp); 268251078Speter splx(s); 268351078Speter} 268451078Speter 268551078Speterstatic void 268651654Sphkcomstop(tp, rw) 268751078Speter struct tty *tp; 268851078Speter int rw; 268951078Speter{ 269051078Speter struct com_s *com; 269151078Speter 269251078Speter com = com_addr(DEV_TO_UNIT(tp->t_dev)); 269357915Simp if (com == NULL || com->gone) 269451078Speter return; 269572200Sbmilekic mtx_lock_spin(&sio_lock); 269651078Speter if (rw & FWRITE) { 269751078Speter if (com->hasfifo) 269851078Speter#ifdef COM_ESP 269951078Speter /* XXX avoid h/w bug. */ 270051078Speter if (!com->esp) 270151078Speter#endif 270260471Snyan sio_setreg(com, com_fifo, 270360471Snyan FIFO_XMT_RST | com->fifo_image); 270451078Speter com->obufs[0].l_queued = FALSE; 270551078Speter com->obufs[1].l_queued = FALSE; 270651078Speter if (com->state & CS_ODONE) 270751078Speter com_events -= LOTS_OF_EVENTS; 270851078Speter com->state &= ~(CS_ODONE | CS_BUSY); 270951078Speter com->tp->t_state &= ~TS_BUSY; 271051078Speter } 271151078Speter if (rw & FREAD) { 271251078Speter if (com->hasfifo) 271351078Speter#ifdef COM_ESP 271451078Speter /* XXX avoid h/w bug. */ 271551078Speter if (!com->esp) 271651078Speter#endif 271760471Snyan sio_setreg(com, com_fifo, 271860471Snyan FIFO_RCV_RST | com->fifo_image); 271951078Speter com_events -= (com->iptr - com->ibuf); 272051078Speter com->iptr = com->ibuf; 272151078Speter } 272272200Sbmilekic mtx_unlock_spin(&sio_lock); 272351078Speter comstart(tp); 272451078Speter} 272551078Speter 272651078Speterstatic int 272751078Spetercommctl(com, bits, how) 272851078Speter struct com_s *com; 272951078Speter int bits; 273051078Speter int how; 273151078Speter{ 273251078Speter int mcr; 273351078Speter int msr; 273451078Speter 273551078Speter if (how == DMGET) { 273651078Speter bits = TIOCM_LE; /* XXX - always enabled while open */ 273751078Speter mcr = com->mcr_image; 273851078Speter if (mcr & MCR_DTR) 273951078Speter bits |= TIOCM_DTR; 274051078Speter if (mcr & MCR_RTS) 274151078Speter bits |= TIOCM_RTS; 274251078Speter msr = com->prev_modem_status; 274351078Speter if (msr & MSR_CTS) 274451078Speter bits |= TIOCM_CTS; 274551078Speter if (msr & MSR_DCD) 274651078Speter bits |= TIOCM_CD; 274751078Speter if (msr & MSR_DSR) 274851078Speter bits |= TIOCM_DSR; 274951078Speter /* 275051078Speter * XXX - MSR_RI is naturally volatile, and we make MSR_TERI 275151078Speter * more volatile by reading the modem status a lot. Perhaps 275251078Speter * we should latch both bits until the status is read here. 275351078Speter */ 275451078Speter if (msr & (MSR_RI | MSR_TERI)) 275551078Speter bits |= TIOCM_RI; 275651078Speter return (bits); 275751078Speter } 275851078Speter mcr = 0; 275951078Speter if (bits & TIOCM_DTR) 276051078Speter mcr |= MCR_DTR; 276151078Speter if (bits & TIOCM_RTS) 276251078Speter mcr |= MCR_RTS; 276351078Speter if (com->gone) 276451078Speter return(0); 276572200Sbmilekic mtx_lock_spin(&sio_lock); 276651078Speter switch (how) { 276751078Speter case DMSET: 276851078Speter outb(com->modem_ctl_port, 276951078Speter com->mcr_image = mcr | (com->mcr_image & MCR_IENABLE)); 277051078Speter break; 277151078Speter case DMBIS: 277251078Speter outb(com->modem_ctl_port, com->mcr_image |= mcr); 277351078Speter break; 277451078Speter case DMBIC: 277551078Speter outb(com->modem_ctl_port, com->mcr_image &= ~mcr); 277651078Speter break; 277751078Speter } 277872200Sbmilekic mtx_unlock_spin(&sio_lock); 277951078Speter return (0); 278051078Speter} 278151078Speter 278251078Speterstatic void 278351078Spetersiosettimeout() 278451078Speter{ 278551078Speter struct com_s *com; 278651078Speter bool_t someopen; 278751078Speter int unit; 278851078Speter 278951078Speter /* 279051078Speter * Set our timeout period to 1 second if no polled devices are open. 279151078Speter * Otherwise set it to max(1/200, 1/hz). 279251078Speter * Enable timeouts iff some device is open. 279351078Speter */ 279451078Speter untimeout(comwakeup, (void *)NULL, sio_timeout_handle); 279551078Speter sio_timeout = hz; 279651078Speter someopen = FALSE; 279753344Speter for (unit = 0; unit < sio_numunits; ++unit) { 279851078Speter com = com_addr(unit); 279951078Speter if (com != NULL && com->tp != NULL 280051078Speter && com->tp->t_state & TS_ISOPEN && !com->gone) { 280151078Speter someopen = TRUE; 280251078Speter if (com->poll || com->poll_output) { 280351078Speter sio_timeout = hz > 200 ? hz / 200 : 1; 280451078Speter break; 280551078Speter } 280651078Speter } 280751078Speter } 280851078Speter if (someopen) { 280951078Speter sio_timeouts_until_log = hz / sio_timeout; 281051078Speter sio_timeout_handle = timeout(comwakeup, (void *)NULL, 281151078Speter sio_timeout); 281251078Speter } else { 281351078Speter /* Flush error messages, if any. */ 281451078Speter sio_timeouts_until_log = 1; 281551078Speter comwakeup((void *)NULL); 281651078Speter untimeout(comwakeup, (void *)NULL, sio_timeout_handle); 281751078Speter } 281851078Speter} 281951078Speter 282051078Speterstatic void 282151078Spetercomwakeup(chan) 282251078Speter void *chan; 282351078Speter{ 282451078Speter struct com_s *com; 282551078Speter int unit; 282651078Speter 282751078Speter sio_timeout_handle = timeout(comwakeup, (void *)NULL, sio_timeout); 282851078Speter 282951078Speter /* 283051078Speter * Recover from lost output interrupts. 283151078Speter * Poll any lines that don't use interrupts. 283251078Speter */ 283353344Speter for (unit = 0; unit < sio_numunits; ++unit) { 283451078Speter com = com_addr(unit); 283551078Speter if (com != NULL && !com->gone 283651078Speter && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) { 283772200Sbmilekic mtx_lock_spin(&sio_lock); 283851078Speter siointr1(com); 283972200Sbmilekic mtx_unlock_spin(&sio_lock); 284051078Speter } 284151078Speter } 284251078Speter 284351078Speter /* 284451078Speter * Check for and log errors, but not too often. 284551078Speter */ 284651078Speter if (--sio_timeouts_until_log > 0) 284751078Speter return; 284851078Speter sio_timeouts_until_log = hz / sio_timeout; 284953344Speter for (unit = 0; unit < sio_numunits; ++unit) { 285051078Speter int errnum; 285151078Speter 285251078Speter com = com_addr(unit); 285351078Speter if (com == NULL) 285451078Speter continue; 285551078Speter if (com->gone) 285651078Speter continue; 285751078Speter for (errnum = 0; errnum < CE_NTYPES; ++errnum) { 285851078Speter u_int delta; 285951078Speter u_long total; 286051078Speter 286172200Sbmilekic mtx_lock_spin(&sio_lock); 286251078Speter delta = com->delta_error_counts[errnum]; 286351078Speter com->delta_error_counts[errnum] = 0; 286472200Sbmilekic mtx_unlock_spin(&sio_lock); 286551078Speter if (delta == 0) 286651078Speter continue; 286751078Speter total = com->error_counts[errnum] += delta; 286851078Speter log(LOG_ERR, "sio%d: %u more %s%s (total %lu)\n", 286951078Speter unit, delta, error_desc[errnum], 287051078Speter delta == 1 ? "" : "s", total); 287151078Speter } 287251078Speter } 287351078Speter} 287451078Speter 287551078Speterstatic void 287651078Speterdisc_optim(tp, t, com) 287751078Speter struct tty *tp; 287851078Speter struct termios *t; 287951078Speter struct com_s *com; 288051078Speter{ 288151078Speter if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) 288251078Speter && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) 288351078Speter && (!(t->c_iflag & PARMRK) 288451078Speter || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) 288551078Speter && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) 288651078Speter && linesw[tp->t_line].l_rint == ttyinput) 288751078Speter tp->t_state |= TS_CAN_BYPASS_L_RINT; 288851078Speter else 288951078Speter tp->t_state &= ~TS_CAN_BYPASS_L_RINT; 289051078Speter com->hotchar = linesw[tp->t_line].l_hotchar; 289151078Speter} 289251078Speter 289351078Speter/* 289451078Speter * Following are all routines needed for SIO to act as console 289551078Speter */ 289651078Speter#include <sys/cons.h> 289751078Speter 289851078Speterstruct siocnstate { 289951078Speter u_char dlbl; 290051078Speter u_char dlbh; 290151078Speter u_char ier; 290251078Speter u_char cfcr; 290351078Speter u_char mcr; 290451078Speter}; 290551078Speter 290666230Sjhb#ifndef __alpha__ 290751078Speterstatic speed_t siocngetspeed __P((Port_t, struct speedtab *)); 290866230Sjhb#endif 290951078Speterstatic void siocnclose __P((struct siocnstate *sp, Port_t iobase)); 291051078Speterstatic void siocnopen __P((struct siocnstate *sp, Port_t iobase, int speed)); 291151078Speterstatic void siocntxwait __P((Port_t iobase)); 291251078Speter 291366230Sjhb#ifdef __alpha__ 291466230Sjhbint siocnattach __P((int port, int speed)); 291566230Sjhbint siogdbattach __P((int port, int speed)); 291666230Sjhbint siogdbgetc __P((void)); 291766230Sjhbvoid siogdbputc __P((int c)); 291866230Sjhb#else 291951078Speterstatic cn_probe_t siocnprobe; 292051078Speterstatic cn_init_t siocninit; 292166230Sjhb#endif 292251078Speterstatic cn_checkc_t siocncheckc; 292351078Speterstatic cn_getc_t siocngetc; 292451078Speterstatic cn_putc_t siocnputc; 292551078Speter 292651078Speter#ifdef __i386__ 292755823SyokotaCONS_DRIVER(sio, siocnprobe, siocninit, NULL, siocngetc, siocncheckc, 292855823Syokota siocnputc, NULL); 292951078Speter#endif 293051078Speter 293151078Speter/* To get the GDB related variables */ 293251078Speter#if DDB > 0 293351078Speter#include <ddb/ddb.h> 293451078Speter#endif 293551078Speter 293651078Speterstatic void 293751078Spetersiocntxwait(iobase) 293851078Speter Port_t iobase; 293951078Speter{ 294051078Speter int timo; 294151078Speter 294251078Speter /* 294351078Speter * Wait for any pending transmission to finish. Required to avoid 294451078Speter * the UART lockup bug when the speed is changed, and for normal 294551078Speter * transmits. 294651078Speter */ 294751078Speter timo = 100000; 294851078Speter while ((inb(iobase + com_lsr) & (LSR_TSRE | LSR_TXRDY)) 294951078Speter != (LSR_TSRE | LSR_TXRDY) && --timo != 0) 295051078Speter ; 295151078Speter} 295251078Speter 295366230Sjhb#ifndef __alpha__ 295466230Sjhb 295551078Speter/* 295651078Speter * Read the serial port specified and try to figure out what speed 295751078Speter * it's currently running at. We're assuming the serial port has 295851078Speter * been initialized and is basicly idle. This routine is only intended 295951078Speter * to be run at system startup. 296051078Speter * 296151078Speter * If the value read from the serial port doesn't make sense, return 0. 296251078Speter */ 296351078Speter 296451078Speterstatic speed_t 296551078Spetersiocngetspeed(iobase, table) 296651078Speter Port_t iobase; 296751078Speter struct speedtab *table; 296851078Speter{ 296951078Speter int code; 297051078Speter u_char dlbh; 297151078Speter u_char dlbl; 297251078Speter u_char cfcr; 297351078Speter 297451078Speter cfcr = inb(iobase + com_cfcr); 297551078Speter outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 297651078Speter 297751078Speter dlbl = inb(iobase + com_dlbl); 297851078Speter dlbh = inb(iobase + com_dlbh); 297951078Speter 298051078Speter outb(iobase + com_cfcr, cfcr); 298151078Speter 298251078Speter code = dlbh << 8 | dlbl; 298351078Speter 298453344Speter for (; table->sp_speed != -1; table++) 298551078Speter if (table->sp_code == code) 298651078Speter return (table->sp_speed); 298751078Speter 298858885Simp return (0); /* didn't match anything sane */ 298951078Speter} 299051078Speter 299166230Sjhb#endif 299266230Sjhb 299351078Speterstatic void 299451078Spetersiocnopen(sp, iobase, speed) 299551078Speter struct siocnstate *sp; 299651078Speter Port_t iobase; 299751078Speter int speed; 299851078Speter{ 299951078Speter int divisor; 300051078Speter u_char dlbh; 300151078Speter u_char dlbl; 300251078Speter 300351078Speter /* 300451078Speter * Save all the device control registers except the fifo register 300551078Speter * and set our default ones (cs8 -parenb speed=comdefaultrate). 300651078Speter * We can't save the fifo register since it is read-only. 300751078Speter */ 300851078Speter sp->ier = inb(iobase + com_ier); 300951078Speter outb(iobase + com_ier, 0); /* spltty() doesn't stop siointr() */ 301051078Speter siocntxwait(iobase); 301151078Speter sp->cfcr = inb(iobase + com_cfcr); 301251078Speter outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 301351078Speter sp->dlbl = inb(iobase + com_dlbl); 301451078Speter sp->dlbh = inb(iobase + com_dlbh); 301551078Speter /* 301651078Speter * Only set the divisor registers if they would change, since on 301751078Speter * some 16550 incompatibles (Startech), setting them clears the 301851078Speter * data input register. This also reduces the effects of the 301951078Speter * UMC8669F bug. 302051078Speter */ 302151078Speter divisor = ttspeedtab(speed, comspeedtab); 302251078Speter dlbl = divisor & 0xFF; 302351078Speter if (sp->dlbl != dlbl) 302451078Speter outb(iobase + com_dlbl, dlbl); 302551078Speter dlbh = (u_int) divisor >> 8; 302651078Speter if (sp->dlbh != dlbh) 302751078Speter outb(iobase + com_dlbh, dlbh); 302851078Speter outb(iobase + com_cfcr, CFCR_8BITS); 302951078Speter sp->mcr = inb(iobase + com_mcr); 303051078Speter /* 303151078Speter * We don't want interrupts, but must be careful not to "disable" 303251078Speter * them by clearing the MCR_IENABLE bit, since that might cause 303351078Speter * an interrupt by floating the IRQ line. 303451078Speter */ 303551078Speter outb(iobase + com_mcr, (sp->mcr & MCR_IENABLE) | MCR_DTR | MCR_RTS); 303651078Speter} 303751078Speter 303851078Speterstatic void 303951078Spetersiocnclose(sp, iobase) 304051078Speter struct siocnstate *sp; 304151078Speter Port_t iobase; 304251078Speter{ 304351078Speter /* 304451078Speter * Restore the device control registers. 304551078Speter */ 304651078Speter siocntxwait(iobase); 304751078Speter outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 304851078Speter if (sp->dlbl != inb(iobase + com_dlbl)) 304951078Speter outb(iobase + com_dlbl, sp->dlbl); 305051078Speter if (sp->dlbh != inb(iobase + com_dlbh)) 305151078Speter outb(iobase + com_dlbh, sp->dlbh); 305251078Speter outb(iobase + com_cfcr, sp->cfcr); 305351078Speter /* 305451078Speter * XXX damp oscillations of MCR_DTR and MCR_RTS by not restoring them. 305551078Speter */ 305651078Speter outb(iobase + com_mcr, sp->mcr | MCR_DTR | MCR_RTS); 305751078Speter outb(iobase + com_ier, sp->ier); 305851078Speter} 305951078Speter 306066230Sjhb#ifndef __alpha__ 306166230Sjhb 306251078Speterstatic void 306351078Spetersiocnprobe(cp) 306451078Speter struct consdev *cp; 306551078Speter{ 306651078Speter speed_t boot_speed; 306751078Speter u_char cfcr; 306851078Speter int s, unit; 306951078Speter struct siocnstate sp; 307051078Speter 307151078Speter /* 307251078Speter * Find our first enabled console, if any. If it is a high-level 307351078Speter * console device, then initialize it and return successfully. 307451078Speter * If it is a low-level console device, then initialize it and 307551078Speter * return unsuccessfully. It must be initialized in both cases 307651078Speter * for early use by console drivers and debuggers. Initializing 307751078Speter * the hardware is not necessary in all cases, since the i/o 307851078Speter * routines initialize it on the fly, but it is necessary if 307951078Speter * input might arrive while the hardware is switched back to an 308051078Speter * uninitialized state. We can't handle multiple console devices 308151078Speter * yet because our low-level routines don't take a device arg. 308251078Speter * We trust the user to set the console flags properly so that we 308351078Speter * don't need to probe. 308451078Speter */ 308551078Speter cp->cn_pri = CN_DEAD; 308651078Speter 308751078Speter for (unit = 0; unit < 16; unit++) { /* XXX need to know how many */ 308851078Speter int flags; 308951078Speter int disabled; 309051078Speter if (resource_int_value("sio", unit, "disabled", &disabled) == 0) { 309151078Speter if (disabled) 309251078Speter continue; 309351078Speter } 309451078Speter if (resource_int_value("sio", unit, "flags", &flags)) 309551078Speter continue; 309651078Speter if (COM_CONSOLE(flags) || COM_DEBUGGER(flags)) { 309751078Speter int port; 309851078Speter Port_t iobase; 309951078Speter 310051078Speter if (resource_int_value("sio", unit, "port", &port)) 310151078Speter continue; 310251078Speter iobase = port; 310351078Speter s = spltty(); 310451078Speter if (boothowto & RB_SERIAL) { 310551078Speter boot_speed = siocngetspeed(iobase, comspeedtab); 310651078Speter if (boot_speed) 310751078Speter comdefaultrate = boot_speed; 310851078Speter } 310951078Speter 311051078Speter /* 311151078Speter * Initialize the divisor latch. We can't rely on 311251078Speter * siocnopen() to do this the first time, since it 311351078Speter * avoids writing to the latch if the latch appears 311451078Speter * to have the correct value. Also, if we didn't 311551078Speter * just read the speed from the hardware, then we 311651078Speter * need to set the speed in hardware so that 311751078Speter * switching it later is null. 311851078Speter */ 311951078Speter cfcr = inb(iobase + com_cfcr); 312051078Speter outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 312151078Speter outb(iobase + com_dlbl, 312251078Speter COMBRD(comdefaultrate) & 0xff); 312351078Speter outb(iobase + com_dlbh, 312451078Speter (u_int) COMBRD(comdefaultrate) >> 8); 312551078Speter outb(iobase + com_cfcr, cfcr); 312651078Speter 312751078Speter siocnopen(&sp, iobase, comdefaultrate); 312851078Speter 312951078Speter splx(s); 313051078Speter if (COM_CONSOLE(flags) && !COM_LLCONSOLE(flags)) { 313151078Speter cp->cn_dev = makedev(CDEV_MAJOR, unit); 313251078Speter cp->cn_pri = COM_FORCECONSOLE(flags) 313351078Speter || boothowto & RB_SERIAL 313451078Speter ? CN_REMOTE : CN_NORMAL; 313551078Speter siocniobase = iobase; 313651078Speter siocnunit = unit; 313751078Speter } 313851078Speter if (COM_DEBUGGER(flags)) { 313951078Speter printf("sio%d: gdb debugging port\n", unit); 314051078Speter siogdbiobase = iobase; 314151078Speter siogdbunit = unit; 314251078Speter#if DDB > 0 314351078Speter gdbdev = makedev(CDEV_MAJOR, unit); 314451078Speter gdb_getc = siocngetc; 314551078Speter gdb_putc = siocnputc; 314651078Speter#endif 314751078Speter } 314851078Speter } 314951078Speter } 315051078Speter#ifdef __i386__ 315151078Speter#if DDB > 0 315251078Speter /* 315351078Speter * XXX Ugly Compatability. 315451078Speter * If no gdb port has been specified, set it to be the console 315551078Speter * as some configuration files don't specify the gdb port. 315651078Speter */ 315751078Speter if (gdbdev == NODEV && (boothowto & RB_GDB)) { 315851078Speter printf("Warning: no GDB port specified. Defaulting to sio%d.\n", 315951078Speter siocnunit); 316051078Speter printf("Set flag 0x80 on desired GDB port in your\n"); 316151078Speter printf("configuration file (currently sio only).\n"); 316251078Speter siogdbiobase = siocniobase; 316351078Speter siogdbunit = siocnunit; 316451078Speter gdbdev = makedev(CDEV_MAJOR, siocnunit); 316551078Speter gdb_getc = siocngetc; 316651078Speter gdb_putc = siocnputc; 316751078Speter } 316851078Speter#endif 316951078Speter#endif 317051078Speter} 317151078Speter 317266230Sjhbstatic void 317366230Sjhbsiocninit(cp) 317466230Sjhb struct consdev *cp; 317566230Sjhb{ 317666230Sjhb comconsole = DEV_TO_UNIT(cp->cn_dev); 317766230Sjhb} 317866230Sjhb 317966230Sjhb#endif 318066230Sjhb 318151078Speter#ifdef __alpha__ 318251078Speter 318355868SgallatinCONS_DRIVER(sio, NULL, NULL, NULL, siocngetc, siocncheckc, siocnputc, NULL); 318451078Speter 318551078Speterint 318651078Spetersiocnattach(port, speed) 318751078Speter int port; 318851078Speter int speed; 318951078Speter{ 319051078Speter int s; 319151078Speter u_char cfcr; 319251078Speter struct siocnstate sp; 319351078Speter 319451078Speter siocniobase = port; 319551078Speter comdefaultrate = speed; 319651078Speter sio_consdev.cn_pri = CN_NORMAL; 319751078Speter sio_consdev.cn_dev = makedev(CDEV_MAJOR, 0); 319851078Speter 319951078Speter s = spltty(); 320051078Speter 320151078Speter /* 320251078Speter * Initialize the divisor latch. We can't rely on 320351078Speter * siocnopen() to do this the first time, since it 320451078Speter * avoids writing to the latch if the latch appears 320551078Speter * to have the correct value. Also, if we didn't 320651078Speter * just read the speed from the hardware, then we 320751078Speter * need to set the speed in hardware so that 320851078Speter * switching it later is null. 320951078Speter */ 321051078Speter cfcr = inb(siocniobase + com_cfcr); 321151078Speter outb(siocniobase + com_cfcr, CFCR_DLAB | cfcr); 321251078Speter outb(siocniobase + com_dlbl, 321351078Speter COMBRD(comdefaultrate) & 0xff); 321451078Speter outb(siocniobase + com_dlbh, 321551078Speter (u_int) COMBRD(comdefaultrate) >> 8); 321651078Speter outb(siocniobase + com_cfcr, cfcr); 321751078Speter 321851078Speter siocnopen(&sp, siocniobase, comdefaultrate); 321951078Speter splx(s); 322051078Speter 322151078Speter cn_tab = &sio_consdev; 322258885Simp return (0); 322351078Speter} 322451078Speter 322551078Speterint 322651078Spetersiogdbattach(port, speed) 322751078Speter int port; 322851078Speter int speed; 322951078Speter{ 323051078Speter int s; 323151078Speter u_char cfcr; 323251078Speter struct siocnstate sp; 323365714Sjhb int unit = 1; /* XXX !!! */ 323451078Speter 323551078Speter siogdbiobase = port; 323651078Speter gdbdefaultrate = speed; 323751078Speter 323865714Sjhb printf("sio%d: gdb debugging port\n", unit); 323965714Sjhb siogdbunit = unit; 324065714Sjhb#if DDB > 0 324165714Sjhb gdbdev = makedev(CDEV_MAJOR, unit); 324265714Sjhb gdb_getc = siocngetc; 324365714Sjhb gdb_putc = siocnputc; 324465714Sjhb#endif 324565714Sjhb 324651078Speter s = spltty(); 324751078Speter 324851078Speter /* 324951078Speter * Initialize the divisor latch. We can't rely on 325051078Speter * siocnopen() to do this the first time, since it 325151078Speter * avoids writing to the latch if the latch appears 325251078Speter * to have the correct value. Also, if we didn't 325351078Speter * just read the speed from the hardware, then we 325451078Speter * need to set the speed in hardware so that 325551078Speter * switching it later is null. 325651078Speter */ 325751078Speter cfcr = inb(siogdbiobase + com_cfcr); 325851078Speter outb(siogdbiobase + com_cfcr, CFCR_DLAB | cfcr); 325951078Speter outb(siogdbiobase + com_dlbl, 326051078Speter COMBRD(gdbdefaultrate) & 0xff); 326151078Speter outb(siogdbiobase + com_dlbh, 326251078Speter (u_int) COMBRD(gdbdefaultrate) >> 8); 326351078Speter outb(siogdbiobase + com_cfcr, cfcr); 326451078Speter 326551078Speter siocnopen(&sp, siogdbiobase, gdbdefaultrate); 326651078Speter splx(s); 326751078Speter 326858885Simp return (0); 326951078Speter} 327051078Speter 327151078Speter#endif 327251078Speter 327351078Speterstatic int 327451078Spetersiocncheckc(dev) 327551078Speter dev_t dev; 327651078Speter{ 327751078Speter int c; 327851078Speter Port_t iobase; 327951078Speter int s; 328051078Speter struct siocnstate sp; 328151078Speter 328251078Speter if (minor(dev) == siogdbunit) 328351078Speter iobase = siogdbiobase; 328451078Speter else 328551078Speter iobase = siocniobase; 328651078Speter s = spltty(); 328751078Speter siocnopen(&sp, iobase, comdefaultrate); 328851078Speter if (inb(iobase + com_lsr) & LSR_RXRDY) 328951078Speter c = inb(iobase + com_data); 329051078Speter else 329151078Speter c = -1; 329251078Speter siocnclose(&sp, iobase); 329351078Speter splx(s); 329451078Speter return (c); 329551078Speter} 329651078Speter 329751078Speter 329851078Speterint 329951078Spetersiocngetc(dev) 330051078Speter dev_t dev; 330151078Speter{ 330251078Speter int c; 330351078Speter Port_t iobase; 330451078Speter int s; 330551078Speter struct siocnstate sp; 330651078Speter 330751078Speter if (minor(dev) == siogdbunit) 330851078Speter iobase = siogdbiobase; 330951078Speter else 331051078Speter iobase = siocniobase; 331151078Speter s = spltty(); 331251078Speter siocnopen(&sp, iobase, comdefaultrate); 331351078Speter while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 331451078Speter ; 331551078Speter c = inb(iobase + com_data); 331651078Speter siocnclose(&sp, iobase); 331751078Speter splx(s); 331851078Speter return (c); 331951078Speter} 332051078Speter 332151078Spetervoid 332251078Spetersiocnputc(dev, c) 332351078Speter dev_t dev; 332451078Speter int c; 332551078Speter{ 332651078Speter int s; 332751078Speter struct siocnstate sp; 332851078Speter Port_t iobase; 332951078Speter 333051078Speter if (minor(dev) == siogdbunit) 333151078Speter iobase = siogdbiobase; 333251078Speter else 333351078Speter iobase = siocniobase; 333451078Speter s = spltty(); 333551078Speter siocnopen(&sp, iobase, comdefaultrate); 333651078Speter siocntxwait(iobase); 333751078Speter outb(iobase + com_data, c); 333851078Speter siocnclose(&sp, iobase); 333951078Speter splx(s); 334051078Speter} 334151078Speter 334251078Speter#ifdef __alpha__ 334351078Speterint 334451078Spetersiogdbgetc() 334551078Speter{ 334651078Speter int c; 334751078Speter Port_t iobase; 334851078Speter int s; 334951078Speter struct siocnstate sp; 335051078Speter 335151078Speter iobase = siogdbiobase; 335251078Speter s = spltty(); 335351078Speter siocnopen(&sp, iobase, gdbdefaultrate); 335451078Speter while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 335551078Speter ; 335651078Speter c = inb(iobase + com_data); 335751078Speter siocnclose(&sp, iobase); 335851078Speter splx(s); 335951078Speter return (c); 336051078Speter} 336151078Speter 336251078Spetervoid 336351078Spetersiogdbputc(c) 336451078Speter int c; 336551078Speter{ 336651078Speter int s; 336751078Speter struct siocnstate sp; 336851078Speter 336951078Speter s = spltty(); 337051078Speter siocnopen(&sp, siogdbiobase, gdbdefaultrate); 337151078Speter siocntxwait(siogdbiobase); 337251078Speter outb(siogdbiobase + com_data, c); 337351078Speter siocnclose(&sp, siogdbiobase); 337451078Speter splx(s); 337551078Speter} 337651078Speter#endif 337751078Speter 337852471SimpDRIVER_MODULE(sio, isa, sio_isa_driver, sio_devclass, 0, 0); 337982555SmsmithDRIVER_MODULE(sio, acpi, sio_isa_driver, sio_devclass, 0, 0); 338052471Simp#if NCARD > 0 338152471SimpDRIVER_MODULE(sio, pccard, sio_pccard_driver, sio_devclass, 0, 0); 338252471Simp#endif 338358885Simp#if NPCI > 0 338458885SimpDRIVER_MODULE(sio, pci, sio_pci_driver, sio_devclass, 0, 0); 338577750SimpDRIVER_MODULE(sio, cardbus, sio_pci_driver, sio_devclass, 0, 0); 338658885Simp#endif 3387