sio.c revision 83546
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 83546 2001-09-16 11:14:56Z 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 }, 58183546Smurray { 0x2000131f, "CyberSerial (1-port) 16550", 0x10 }, 58283546Smurray { 0x01101407, "Koutech IOFLEX-2S PCI Dual Port Serial", 0x10 }, 58383546Smurray { 0x01111407, "Koutech IOFLEX-2S PCI Dual Port Serial", 0x10 }, 58477750Simp { 0x048011c1, "Lucent kermit based PCI Modem", 0x14 }, 58583546Smurray { 0x95211415, "Oxford Semiconductor PCI Dual Port Serial", 0x10 }, 58676869Ssanpei { 0x0000151f, "SmartLink 5634PCV SurfRider", 0x10 }, 58777750Simp /* { 0xXXXXXXXX, "Xircom Cardbus modem", 0x10 }, */ 58858885Simp { 0x00000000, NULL, 0 } 58958885Simp}; 59058885Simp 59158885Simpstatic int 59258885Simpsio_pci_attach(dev) 59358885Simp device_t dev; 59458885Simp{ 59558885Simp u_int32_t type; 59658885Simp struct pci_ids *id; 59758885Simp 59858885Simp type = pci_get_devid(dev); 59958885Simp id = pci_ids; 60058885Simp while (id->type && id->type != type) 60158885Simp id++; 60258885Simp if (id->desc == NULL) 60358885Simp return (ENXIO); 60458885Simp sio_pci_kludge_unit(dev); 60558885Simp return (sioattach(dev, id->rid)); 60658885Simp} 60758885Simp 60858885Simp/* 60958885Simp * Don't cut and paste this to other drivers. It is a horrible kludge 61058885Simp * which will fail to work and also be unnecessary in future versions. 61158885Simp */ 61258885Simpstatic void 61358885Simpsio_pci_kludge_unit(dev) 61458885Simp device_t dev; 61558885Simp{ 61658885Simp devclass_t dc; 61758885Simp int err; 61858885Simp int start; 61958885Simp int unit; 62058885Simp 62158885Simp unit = 0; 62258885Simp start = 0; 62358885Simp while (resource_int_value("sio", unit, "port", &start) == 0 && 62458885Simp start > 0) 62558885Simp unit++; 62658885Simp if (device_get_unit(dev) < unit) { 62758885Simp dc = device_get_devclass(dev); 62858885Simp while (devclass_get_device(dc, unit)) 62958885Simp unit++; 63058885Simp device_printf(dev, "moving to sio%d\n", unit); 63158885Simp err = device_set_unit(dev, unit); /* EVIL DO NOT COPY */ 63258885Simp if (err) 63358885Simp device_printf(dev, "error moving device %d\n", err); 63458885Simp } 63558885Simp} 63658885Simp 63758885Simpstatic int 63858885Simpsio_pci_probe(dev) 63958885Simp device_t dev; 64058885Simp{ 64158885Simp u_int32_t type; 64258885Simp struct pci_ids *id; 64358885Simp 64458885Simp type = pci_get_devid(dev); 64558885Simp id = pci_ids; 64658885Simp while (id->type && id->type != type) 64758885Simp id++; 64858885Simp if (id->desc == NULL) 64958885Simp return (ENXIO); 65058885Simp device_set_desc(dev, id->desc); 65158885Simp return (sioprobe(dev, id->rid)); 65258885Simp} 65358885Simp#endif /* NPCI > 0 */ 65458885Simp 65551078Speterstatic struct isa_pnp_id sio_ids[] = { 65651078Speter {0x0005d041, "Standard PC COM port"}, /* PNP0500 */ 65751078Speter {0x0105d041, "16550A-compatible COM port"}, /* PNP0501 */ 65851078Speter {0x0205d041, "Multiport serial device (non-intelligent 16550)"}, /* PNP0502 */ 65951078Speter {0x1005d041, "Generic IRDA-compatible device"}, /* PNP0510 */ 66051078Speter {0x1105d041, "Generic IRDA-compatible device"}, /* PNP0511 */ 66154944Speter /* Devices that do not have a compatid */ 66260696Stanimura {0x12206804, NULL}, /* ACH2012 - 5634BTS 56K Video Ready Modem */ 66354944Speter {0x7602a904, NULL}, /* AEI0276 - 56K v.90 Fax Modem (LKT) */ 66454944Speter {0x00007905, NULL}, /* AKY0000 - 56K Plug&Play Modem */ 66554944Speter {0x01405407, NULL}, /* AZT4001 - AZT3000 PnP SOUND DEVICE, MODEM */ 66654944Speter {0x56039008, NULL}, /* BDP0356 - Best Data 56x2 */ 66782696Smurray {0x56159008, NULL}, /* BDP1556 - B.D. Smart One 56SPS,Voice Modem*/ 66854944Speter {0x36339008, NULL}, /* BDP3336 - Best Data Prods. 336F */ 66954944Speter {0x0014490a, NULL}, /* BRI1400 - Boca 33.6 PnP */ 67054944Speter {0x0015490a, NULL}, /* BRI1500 - Internal Fax Data */ 67154944Speter {0x0034490a, NULL}, /* BRI3400 - Internal ACF Modem */ 67265438Ssheldonh {0x0094490a, NULL}, /* BRI9400 - Boca K56Flex PnP */ 67354944Speter {0x00b4490a, NULL}, /* BRIB400 - Boca 56k PnP */ 67454944Speter {0x0030320d, NULL}, /* CIR3000 - Cirrus Logic V43 */ 67556229Speter {0x0100440e, NULL}, /* CRD0001 - Cardinal MVP288IV ? */ 67660674Stanimura {0x36033610, NULL}, /* DAV0336 - DAVICOM 336PNP MODEM */ 67759888Stanimura {0x0000aa1a, NULL}, /* FUJ0000 - FUJITSU Modem 33600 PNP/I2 */ 67854944Speter {0x1200c31e, NULL}, /* GVC0012 - VF1128HV-R9 (win modem?) */ 67954944Speter {0x0303c31e, NULL}, /* GVC0303 - MaxTech 33.6 PnP D/F/V */ 68055124Speter {0x0505c31e, NULL}, /* GVC0505 - GVC 56k Faxmodem */ 68173961Ssanpei {0x0116c31e, NULL}, /* GVC1601 - Rockwell V.34 Plug & Play Modem */ 68254944Speter {0x0050c31e, NULL}, /* GVC5000 - some GVC modem */ 68354944Speter {0x3800f91e, NULL}, /* GWY0038 - Telepath with v.90 */ 68454944Speter {0x9062f91e, NULL}, /* GWY6290 - Telepath with x2 Technology */ 68573960Ssanpei {0x8100e425, NULL}, /* IOD0081 - I-O DATA DEVICE,INC. IFML-560 */ 68666920Stanimura {0x21002534, NULL}, /* MAE0021 - Jetstream Int V.90 56k Voice Series 2*/ 68754944Speter {0x0000f435, NULL}, /* MOT0000 - Motorola ModemSURFR 33.6 Intern */ 68854944Speter {0x5015f435, NULL}, /* MOT1550 - Motorola ModemSURFR 56K Modem */ 68954944Speter {0xf015f435, NULL}, /* MOT15F0 - Motorola VoiceSURFR 56K Modem */ 69054944Speter {0x6045f435, NULL}, /* MOT4560 - Motorola ? */ 69154944Speter {0x61e7a338, NULL}, /* NECE761 - 33.6Modem */ 69270356Stanimura {0x08804f3f, NULL}, /* OZO8008 - Zoom (33.6k Modem) */ 69360674Stanimura {0x0f804f3f, NULL}, /* OZO800f - Zoom 2812 (56k Modem) */ 69454944Speter {0x39804f3f, NULL}, /* OZO8039 - Zoom 56k flex */ 69577222Ssanpei {0x00914f3f, NULL}, /* OZO9100 - Zoom 2919 (K56 Faxmodem) */ 69657769Speter {0x3024a341, NULL}, /* PMC2430 - Pace 56 Voice Internal Modem */ 69754944Speter {0x1000eb49, NULL}, /* ROK0010 - Rockwell ? */ 69882685Smurray {0x1200b23d, NULL}, /* RSS0012 - OMRON ME5614ISA */ 69954944Speter {0x5002734a, NULL}, /* RSS0250 - 5614Jx3(G) Internal Modem */ 70060708Stanimura {0x6202734a, NULL}, /* RSS0262 - 5614Jx3[G] V90+K56Flex Modem */ 70154944Speter {0xc100ad4d, NULL}, /* SMM00C1 - Leopard 56k PnP */ 70256229Speter {0x9012b04e, NULL}, /* SUP1290 - Supra ? */ 70354944Speter {0x1013b04e, NULL}, /* SUP1310 - SupraExpress 336i PnP */ 70454944Speter {0x8013b04e, NULL}, /* SUP1380 - SupraExpress 288i PnP Voice */ 70554944Speter {0x8113b04e, NULL}, /* SUP1381 - SupraExpress 336i PnP Voice */ 70654944Speter {0x5016b04e, NULL}, /* SUP1650 - Supra 336i Sp Intl */ 70760947Stanimura {0x7016b04e, NULL}, /* SUP1670 - Supra 336i V+ Intl */ 70854944Speter {0x7420b04e, NULL}, /* SUP2070 - Supra ? */ 70954944Speter {0x8020b04e, NULL}, /* SUP2080 - Supra ? */ 71054944Speter {0x8420b04e, NULL}, /* SUP2084 - SupraExpress 56i PnP */ 71156229Speter {0x7121b04e, NULL}, /* SUP2171 - SupraExpress 56i Sp? */ 71254944Speter {0x8024b04e, NULL}, /* SUP2480 - Supra ? */ 71354944Speter {0x01007256, NULL}, /* USR0001 - U.S. Robotics Inc., Sportster W */ 71454944Speter {0x02007256, NULL}, /* USR0002 - U.S. Robotics Inc. Sportster 33. */ 71554944Speter {0x04007256, NULL}, /* USR0004 - USR Sportster 14.4k */ 71654944Speter {0x06007256, NULL}, /* USR0006 - USR Sportster 33.6k */ 71756229Speter {0x11007256, NULL}, /* USR0011 - USR ? */ 71854944Speter {0x01017256, NULL}, /* USR0101 - USR ? */ 71954944Speter {0x30207256, NULL}, /* USR2030 - U.S.Robotics Inc. Sportster 560 */ 72054944Speter {0x50207256, NULL}, /* USR2050 - U.S.Robotics Inc. Sportster 33. */ 72154944Speter {0x70207256, NULL}, /* USR2070 - U.S.Robotics Inc. Sportster 560 */ 72254944Speter {0x30307256, NULL}, /* USR3030 - U.S. Robotics 56K FAX INT */ 72354944Speter {0x31307256, NULL}, /* USR3031 - U.S. Robotics 56K FAX INT */ 72458848Speter {0x50307256, NULL}, /* USR3050 - U.S. Robotics 56K FAX INT */ 72554944Speter {0x70307256, NULL}, /* USR3070 - U.S. Robotics 56K Voice INT */ 72656229Speter {0x90307256, NULL}, /* USR3090 - USR ? */ 72764688Speter {0x70917256, NULL}, /* USR9170 - U.S. Robotics 56K FAX INT */ 72854944Speter {0x90917256, NULL}, /* USR9190 - USR 56k Voice INT */ 72954944Speter {0x0300695c, NULL}, /* WCI0003 - Fax/Voice/Modem/Speakphone/Asvd */ 73065469Ssheldonh {0x01a0896a, NULL}, /* ZTIA001 - Zoom Internal V90 Faxmodem */ 73154944Speter {0x61f7896a, NULL}, /* ZTIF761 - Zoom ComStar 33.6 */ 73251078Speter {0} 73351078Speter}; 73451078Speter 73556229Speter 73656229Speter 73751078Speterstatic int 73852471Simpsio_isa_probe(dev) 73952471Simp device_t dev; 74052471Simp{ 74152471Simp /* Check isapnp ids */ 74252471Simp if (ISA_PNP_PROBE(device_get_parent(dev), dev, sio_ids) == ENXIO) 74352471Simp return (ENXIO); 74458885Simp return (sioprobe(dev, 0)); 74552471Simp} 74652471Simp 74752471Simpstatic int 74858885Simpsioprobe(dev, xrid) 74951078Speter device_t dev; 75058885Simp int xrid; 75151078Speter{ 75253344Speter#if 0 75351078Speter static bool_t already_init; 75453344Speter device_t xdev; 75553344Speter#endif 75660471Snyan struct com_s *com; 75751078Speter bool_t failures[10]; 75851078Speter int fn; 75951078Speter device_t idev; 76051078Speter Port_t iobase; 76151078Speter intrmask_t irqmap[4]; 76251078Speter intrmask_t irqs; 76351078Speter u_char mcr_image; 76451078Speter int result; 76554206Speter u_long xirq; 76651088Speter u_int flags = device_get_flags(dev); 76751078Speter int rid; 76851078Speter struct resource *port; 76951078Speter 77058885Simp rid = xrid; 77151078Speter port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 77251078Speter 0, ~0, IO_COMSIZE, RF_ACTIVE); 77351078Speter if (!port) 77457915Simp return (ENXIO); 77551078Speter 77660471Snyan com = device_get_softc(dev); 77760471Snyan com->bst = rman_get_bustag(port); 77860471Snyan com->bsh = rman_get_bushandle(port); 77960471Snyan 78070174Sjhb if (atomic_cmpset_int(&sio_inited, 0, 1)) 78172238Sjhb mtx_init(&sio_lock, driver_name, MTX_SPIN); 78270174Sjhb 78353344Speter#if 0 78453344Speter /* 78553344Speter * XXX this is broken - when we are first called, there are no 78653344Speter * previously configured IO ports. We could hard code 78753344Speter * 0x3f8, 0x2f8, 0x3e8, 0x2e8 etc but that's probably worse. 78853344Speter * This code has been doing nothing since the conversion since 78953344Speter * "count" is zero the first time around. 79053344Speter */ 79151078Speter if (!already_init) { 79251078Speter /* 79351078Speter * Turn off MCR_IENABLE for all likely serial ports. An unused 79451078Speter * port with its MCR_IENABLE gate open will inhibit interrupts 79551078Speter * from any used port that shares the interrupt vector. 79651078Speter * XXX the gate enable is elsewhere for some multiports. 79751078Speter */ 79851078Speter device_t *devs; 79953344Speter int count, i, xioport; 80051078Speter 80151078Speter devclass_get_devices(sio_devclass, &devs, &count); 80251078Speter for (i = 0; i < count; i++) { 80351078Speter xdev = devs[i]; 80454194Speter if (device_is_enabled(xdev) && 80554194Speter bus_get_resource(xdev, SYS_RES_IOPORT, 0, &xioport, 80654194Speter NULL) == 0) 80753344Speter outb(xioport + com_mcr, 0); 80851078Speter } 80951078Speter free(devs, M_TEMP); 81051078Speter already_init = TRUE; 81151078Speter } 81253344Speter#endif 81351078Speter 81451078Speter if (COM_LLCONSOLE(flags)) { 81551078Speter printf("sio%d: reserved for low-level i/o\n", 81651078Speter device_get_unit(dev)); 81756788Sbde bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 81851078Speter return (ENXIO); 81951078Speter } 82051078Speter 82151078Speter /* 82251078Speter * If the device is on a multiport card and has an AST/4 82351078Speter * compatible interrupt control register, initialize this 82451078Speter * register and prepare to leave MCR_IENABLE clear in the mcr. 82551078Speter * Otherwise, prepare to set MCR_IENABLE in the mcr. 82651078Speter * Point idev to the device struct giving the correct id_irq. 82751078Speter * This is the struct for the master device if there is one. 82851078Speter */ 82951078Speter idev = dev; 83051078Speter mcr_image = MCR_IENABLE; 83151078Speter#ifdef COM_MULTIPORT 83257234Sbde if (COM_ISMULTIPORT(flags)) { 83354206Speter Port_t xiobase; 83454206Speter u_long io; 83554206Speter 83651078Speter idev = devclass_get_device(sio_devclass, COM_MPMASTER(flags)); 83751078Speter if (idev == NULL) { 83851078Speter printf("sio%d: master device %d not configured\n", 83951078Speter device_get_unit(dev), COM_MPMASTER(flags)); 84051078Speter idev = dev; 84151078Speter } 84257234Sbde if (!COM_NOTAST4(flags)) { 84357234Sbde if (bus_get_resource(idev, SYS_RES_IOPORT, 0, &io, 84457234Sbde NULL) == 0) { 84557234Sbde xiobase = io; 84657234Sbde if (bus_get_resource(idev, SYS_RES_IRQ, 0, 84757234Sbde NULL, NULL) == 0) 84857234Sbde outb(xiobase + com_scr, 0x80); 84957234Sbde else 85057234Sbde outb(xiobase + com_scr, 0); 85157234Sbde } 85257234Sbde mcr_image = 0; 85351078Speter } 85451078Speter } 85551078Speter#endif /* COM_MULTIPORT */ 85654194Speter if (bus_get_resource(idev, SYS_RES_IRQ, 0, NULL, NULL) != 0) 85751078Speter mcr_image = 0; 85851078Speter 85951078Speter bzero(failures, sizeof failures); 86051078Speter iobase = rman_get_start(port); 86151078Speter 86251078Speter /* 86351078Speter * We don't want to get actual interrupts, just masked ones. 86451078Speter * Interrupts from this line should already be masked in the ICU, 86551078Speter * but mask them in the processor as well in case there are some 86651078Speter * (misconfigured) shared interrupts. 86751078Speter */ 86872200Sbmilekic mtx_lock_spin(&sio_lock); 86951078Speter/* EXTRA DELAY? */ 87051078Speter 87151078Speter /* 87251078Speter * Initialize the speed and the word size and wait long enough to 87351078Speter * drain the maximum of 16 bytes of junk in device output queues. 87451078Speter * The speed is undefined after a master reset and must be set 87551078Speter * before relying on anything related to output. There may be 87651078Speter * junk after a (very fast) soft reboot and (apparently) after 87751078Speter * master reset. 87851078Speter * XXX what about the UART bug avoided by waiting in comparam()? 87951078Speter * We don't want to to wait long enough to drain at 2 bps. 88051078Speter */ 88151078Speter if (iobase == siocniobase) 88251078Speter DELAY((16 + 1) * 1000000 / (comdefaultrate / 10)); 88351078Speter else { 88460471Snyan sio_setreg(com, com_cfcr, CFCR_DLAB | CFCR_8BITS); 88560471Snyan sio_setreg(com, com_dlbl, COMBRD(SIO_TEST_SPEED) & 0xff); 88660471Snyan sio_setreg(com, com_dlbh, (u_int) COMBRD(SIO_TEST_SPEED) >> 8); 88760471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); 88851078Speter DELAY((16 + 1) * 1000000 / (SIO_TEST_SPEED / 10)); 88951078Speter } 89051078Speter 89151078Speter /* 89251078Speter * Enable the interrupt gate and disable device interupts. This 89351078Speter * should leave the device driving the interrupt line low and 89451078Speter * guarantee an edge trigger if an interrupt can be generated. 89551078Speter */ 89651078Speter/* EXTRA DELAY? */ 89760471Snyan sio_setreg(com, com_mcr, mcr_image); 89860471Snyan sio_setreg(com, com_ier, 0); 89951078Speter DELAY(1000); /* XXX */ 90051078Speter irqmap[0] = isa_irq_pending(); 90151078Speter 90251078Speter /* 90351078Speter * Attempt to set loopback mode so that we can send a null byte 90451078Speter * without annoying any external device. 90551078Speter */ 90651078Speter/* EXTRA DELAY? */ 90760471Snyan sio_setreg(com, com_mcr, mcr_image | MCR_LOOPBACK); 90851078Speter 90951078Speter /* 91051078Speter * Attempt to generate an output interrupt. On 8250's, setting 91151078Speter * IER_ETXRDY generates an interrupt independent of the current 91251078Speter * setting and independent of whether the THR is empty. On 16450's, 91351078Speter * setting IER_ETXRDY generates an interrupt independent of the 91451078Speter * current setting. On 16550A's, setting IER_ETXRDY only 91551078Speter * generates an interrupt when IER_ETXRDY is not already set. 91651078Speter */ 91760471Snyan sio_setreg(com, com_ier, IER_ETXRDY); 91851078Speter 91951078Speter /* 92051078Speter * On some 16x50 incompatibles, setting IER_ETXRDY doesn't generate 92151078Speter * an interrupt. They'd better generate one for actually doing 92251078Speter * output. Loopback may be broken on the same incompatibles but 92351078Speter * it's unlikely to do more than allow the null byte out. 92451078Speter */ 92560471Snyan sio_setreg(com, com_data, 0); 92651078Speter DELAY((1 + 2) * 1000000 / (SIO_TEST_SPEED / 10)); 92751078Speter 92851078Speter /* 92951078Speter * Turn off loopback mode so that the interrupt gate works again 93051078Speter * (MCR_IENABLE was hidden). This should leave the device driving 93151078Speter * an interrupt line high. It doesn't matter if the interrupt 93251078Speter * line oscillates while we are not looking at it, since interrupts 93351078Speter * are disabled. 93451078Speter */ 93551078Speter/* EXTRA DELAY? */ 93660471Snyan sio_setreg(com, com_mcr, mcr_image); 93751078Speter 93851078Speter /* 93952471Simp * Some pcmcia cards have the "TXRDY bug", so we check everyone 94051078Speter * for IIR_TXRDY implementation ( Palido 321s, DC-1S... ) 94151078Speter */ 94253370Speter if (COM_NOPROBE(flags)) { 94353370Speter /* Reading IIR register twice */ 94453370Speter for (fn = 0; fn < 2; fn ++) { 94553370Speter DELAY(10000); 94660471Snyan failures[6] = sio_getreg(com, com_iir); 94753370Speter } 94853370Speter /* Check IIR_TXRDY clear ? */ 94953370Speter result = 0; 95053370Speter if (failures[6] & IIR_TXRDY) { 95153370Speter /* Nop, Double check with clearing IER */ 95260471Snyan sio_setreg(com, com_ier, 0); 95360471Snyan if (sio_getreg(com, com_iir) & IIR_NOPEND) { 95453370Speter /* Ok. we're familia this gang */ 95553370Speter SET_FLAG(dev, COM_C_IIR_TXRDYBUG); 95653370Speter } else { 95753370Speter /* Unknown, Just omit this chip.. XXX */ 95853370Speter result = ENXIO; 95981793Simp sio_setreg(com, com_mcr, 0); 96053370Speter } 96151078Speter } else { 96253370Speter /* OK. this is well-known guys */ 96353370Speter CLR_FLAG(dev, COM_C_IIR_TXRDYBUG); 96451078Speter } 96581793Simp sio_setreg(com, com_ier, 0); 96660471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); 96772200Sbmilekic mtx_unlock_spin(&sio_lock); 96853344Speter bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 96953344Speter return (iobase == siocniobase ? 0 : result); 97053344Speter } 97153344Speter 97251078Speter /* 97351078Speter * Check that 97451078Speter * o the CFCR, IER and MCR in UART hold the values written to them 97551078Speter * (the values happen to be all distinct - this is good for 97651078Speter * avoiding false positive tests from bus echoes). 97751078Speter * o an output interrupt is generated and its vector is correct. 97851078Speter * o the interrupt goes away when the IIR in the UART is read. 97951078Speter */ 98051078Speter/* EXTRA DELAY? */ 98160471Snyan failures[0] = sio_getreg(com, com_cfcr) - CFCR_8BITS; 98260471Snyan failures[1] = sio_getreg(com, com_ier) - IER_ETXRDY; 98360471Snyan failures[2] = sio_getreg(com, com_mcr) - mcr_image; 98451078Speter DELAY(10000); /* Some internal modems need this time */ 98551078Speter irqmap[1] = isa_irq_pending(); 98660471Snyan failures[4] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_TXRDY; 98751078Speter DELAY(1000); /* XXX */ 98851078Speter irqmap[2] = isa_irq_pending(); 98960471Snyan failures[6] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 99051078Speter 99151078Speter /* 99251078Speter * Turn off all device interrupts and check that they go off properly. 99351078Speter * Leave MCR_IENABLE alone. For ports without a master port, it gates 99451078Speter * the OUT2 output of the UART to 99551078Speter * the ICU input. Closing the gate would give a floating ICU input 99651078Speter * (unless there is another device driving it) and spurious interrupts. 99751078Speter * (On the system that this was first tested on, the input floats high 99851078Speter * and gives a (masked) interrupt as soon as the gate is closed.) 99951078Speter */ 100060471Snyan sio_setreg(com, com_ier, 0); 100160471Snyan sio_setreg(com, com_cfcr, CFCR_8BITS); /* dummy to avoid bus echo */ 100260471Snyan failures[7] = sio_getreg(com, com_ier); 100351078Speter DELAY(1000); /* XXX */ 100451078Speter irqmap[3] = isa_irq_pending(); 100560471Snyan failures[9] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 100651078Speter 100772200Sbmilekic mtx_unlock_spin(&sio_lock); 100851078Speter 100951078Speter irqs = irqmap[1] & ~irqmap[0]; 101054194Speter if (bus_get_resource(idev, SYS_RES_IRQ, 0, &xirq, NULL) == 0 && 101154194Speter ((1 << xirq) & irqs) == 0) 101251078Speter printf( 101354206Speter "sio%d: configured irq %ld not in bitmap of probed irqs %#x\n", 101453344Speter device_get_unit(dev), xirq, irqs); 101551078Speter if (bootverbose) 101651078Speter printf("sio%d: irq maps: %#x %#x %#x %#x\n", 101751078Speter device_get_unit(dev), 101851078Speter irqmap[0], irqmap[1], irqmap[2], irqmap[3]); 101951078Speter 102051078Speter result = 0; 102151078Speter for (fn = 0; fn < sizeof failures; ++fn) 102251078Speter if (failures[fn]) { 102360471Snyan sio_setreg(com, com_mcr, 0); 102451078Speter result = ENXIO; 102551078Speter if (bootverbose) { 102651078Speter printf("sio%d: probe failed test(s):", 102751078Speter device_get_unit(dev)); 102851078Speter for (fn = 0; fn < sizeof failures; ++fn) 102951078Speter if (failures[fn]) 103051078Speter printf(" %d", fn); 103151078Speter printf("\n"); 103251078Speter } 103351078Speter break; 103451078Speter } 103551078Speter bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 103651078Speter return (iobase == siocniobase ? 0 : result); 103751078Speter} 103851078Speter 103951078Speter#ifdef COM_ESP 104051078Speterstatic int 104151078Speterespattach(com, esp_port) 104251078Speter struct com_s *com; 104351078Speter Port_t esp_port; 104451078Speter{ 104551078Speter u_char dips; 104651078Speter u_char val; 104751078Speter 104851078Speter /* 104951078Speter * Check the ESP-specific I/O port to see if we're an ESP 105051078Speter * card. If not, return failure immediately. 105151078Speter */ 105251078Speter if ((inb(esp_port) & 0xf3) == 0) { 105351078Speter printf(" port 0x%x is not an ESP board?\n", esp_port); 105451078Speter return (0); 105551078Speter } 105651078Speter 105751078Speter /* 105851078Speter * We've got something that claims to be a Hayes ESP card. 105951078Speter * Let's hope so. 106051078Speter */ 106151078Speter 106251078Speter /* Get the dip-switch configuration */ 106351078Speter outb(esp_port + ESP_CMD1, ESP_GETDIPS); 106451078Speter dips = inb(esp_port + ESP_STATUS1); 106551078Speter 106651078Speter /* 106751078Speter * Bits 0,1 of dips say which COM port we are. 106851078Speter */ 106960471Snyan if (rman_get_start(com->ioportres) == likely_com_ports[dips & 0x03]) 107051078Speter printf(" : ESP"); 107151078Speter else { 107251078Speter printf(" esp_port has com %d\n", dips & 0x03); 107351078Speter return (0); 107451078Speter } 107551078Speter 107651078Speter /* 107751078Speter * Check for ESP version 2.0 or later: bits 4,5,6 = 010. 107851078Speter */ 107951078Speter outb(esp_port + ESP_CMD1, ESP_GETTEST); 108051078Speter val = inb(esp_port + ESP_STATUS1); /* clear reg 1 */ 108151078Speter val = inb(esp_port + ESP_STATUS2); 108251078Speter if ((val & 0x70) < 0x20) { 108351078Speter printf("-old (%o)", val & 0x70); 108451078Speter return (0); 108551078Speter } 108651078Speter 108751078Speter /* 108851078Speter * Check for ability to emulate 16550: bit 7 == 1 108951078Speter */ 109051078Speter if ((dips & 0x80) == 0) { 109151078Speter printf(" slave"); 109251078Speter return (0); 109351078Speter } 109451078Speter 109551078Speter /* 109651078Speter * Okay, we seem to be a Hayes ESP card. Whee. 109751078Speter */ 109851078Speter com->esp = TRUE; 109951078Speter com->esp_port = esp_port; 110051078Speter return (1); 110151078Speter} 110251078Speter#endif /* COM_ESP */ 110351078Speter 110451078Speterstatic int 110552471Simpsio_isa_attach(dev) 110652471Simp device_t dev; 110752471Simp{ 110858885Simp return (sioattach(dev, 0)); 110952471Simp} 111052471Simp 111152471Simpstatic int 111258885Simpsioattach(dev, xrid) 111351078Speter device_t dev; 111458885Simp int xrid; 111551078Speter{ 111651078Speter struct com_s *com; 111751078Speter#ifdef COM_ESP 111851078Speter Port_t *espp; 111951078Speter#endif 112051078Speter Port_t iobase; 112151078Speter int unit; 112253344Speter u_int flags; 112351078Speter int rid; 112451078Speter struct resource *port; 112553344Speter int ret; 112651078Speter 112758885Simp rid = xrid; 112851078Speter port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 112951078Speter 0, ~0, IO_COMSIZE, RF_ACTIVE); 113051078Speter if (!port) 113157915Simp return (ENXIO); 113251078Speter 113351078Speter iobase = rman_get_start(port); 113451078Speter unit = device_get_unit(dev); 113551078Speter com = device_get_softc(dev); 113653344Speter flags = device_get_flags(dev); 113751078Speter 113853344Speter if (unit >= sio_numunits) 113953344Speter sio_numunits = unit + 1; 114051078Speter /* 114151078Speter * sioprobe() has initialized the device registers as follows: 114251078Speter * o cfcr = CFCR_8BITS. 114351078Speter * It is most important that CFCR_DLAB is off, so that the 114451078Speter * data port is not hidden when we enable interrupts. 114551078Speter * o ier = 0. 114651078Speter * Interrupts are only enabled when the line is open. 114751078Speter * o mcr = MCR_IENABLE, or 0 if the port has AST/4 compatible 114851078Speter * interrupt control register or the config specifies no irq. 114951078Speter * Keeping MCR_DTR and MCR_RTS off might stop the external 115051078Speter * device from sending before we are ready. 115151078Speter */ 115251078Speter bzero(com, sizeof *com); 115351078Speter com->unit = unit; 115451078Speter com->ioportres = port; 115560471Snyan com->bst = rman_get_bustag(port); 115660471Snyan com->bsh = rman_get_bushandle(port); 115751078Speter com->cfcr_image = CFCR_8BITS; 115851078Speter com->dtr_wait = 3 * hz; 115951078Speter com->loses_outints = COM_LOSESOUTINTS(flags) != 0; 116057234Sbde com->no_irq = bus_get_resource(dev, SYS_RES_IRQ, 0, NULL, NULL) != 0; 116151078Speter com->tx_fifo_size = 1; 116251078Speter com->obufs[0].l_head = com->obuf1; 116351078Speter com->obufs[1].l_head = com->obuf2; 116451078Speter 116551078Speter com->data_port = iobase + com_data; 116651078Speter com->int_id_port = iobase + com_iir; 116751078Speter com->modem_ctl_port = iobase + com_mcr; 116851078Speter com->mcr_image = inb(com->modem_ctl_port); 116951078Speter com->line_status_port = iobase + com_lsr; 117051078Speter com->modem_status_port = iobase + com_msr; 117151078Speter com->intr_ctl_port = iobase + com_ier; 117251078Speter 117351078Speter /* 117451078Speter * We don't use all the flags from <sys/ttydefaults.h> since they 117551078Speter * are only relevant for logins. It's important to have echo off 117651078Speter * initially so that the line doesn't start blathering before the 117751078Speter * echo flag can be turned off. 117851078Speter */ 117951078Speter com->it_in.c_iflag = 0; 118051078Speter com->it_in.c_oflag = 0; 118151078Speter com->it_in.c_cflag = TTYDEF_CFLAG; 118251078Speter com->it_in.c_lflag = 0; 118351078Speter if (unit == comconsole) { 118451078Speter com->it_in.c_iflag = TTYDEF_IFLAG; 118551078Speter com->it_in.c_oflag = TTYDEF_OFLAG; 118651078Speter com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL; 118751078Speter com->it_in.c_lflag = TTYDEF_LFLAG; 118851078Speter com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL; 118951078Speter com->lt_out.c_ispeed = com->lt_out.c_ospeed = 119051078Speter com->lt_in.c_ispeed = com->lt_in.c_ospeed = 119151078Speter com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate; 119251078Speter } else 119351078Speter com->it_in.c_ispeed = com->it_in.c_ospeed = TTYDEF_SPEED; 119465605Sjhb if (siosetwater(com, com->it_in.c_ispeed) != 0) { 119572200Sbmilekic mtx_unlock_spin(&sio_lock); 119656788Sbde /* 119756788Sbde * Leave i/o resources allocated if this is a `cn'-level 119856788Sbde * console, so that other devices can't snarf them. 119956788Sbde */ 120056788Sbde if (iobase != siocniobase) 120156788Sbde bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 120256788Sbde return (ENOMEM); 120351078Speter } 120472200Sbmilekic mtx_unlock_spin(&sio_lock); 120551078Speter termioschars(&com->it_in); 120651078Speter com->it_out = com->it_in; 120751078Speter 120851078Speter /* attempt to determine UART type */ 120951078Speter printf("sio%d: type", unit); 121051078Speter 121151078Speter 121251078Speter#ifdef COM_MULTIPORT 121351078Speter if (!COM_ISMULTIPORT(flags) && !COM_IIR_TXRDYBUG(flags)) 121451078Speter#else 121551078Speter if (!COM_IIR_TXRDYBUG(flags)) 121651078Speter#endif 121751078Speter { 121851078Speter u_char scr; 121951078Speter u_char scr1; 122051078Speter u_char scr2; 122151078Speter 122260471Snyan scr = sio_getreg(com, com_scr); 122360471Snyan sio_setreg(com, com_scr, 0xa5); 122460471Snyan scr1 = sio_getreg(com, com_scr); 122560471Snyan sio_setreg(com, com_scr, 0x5a); 122660471Snyan scr2 = sio_getreg(com, com_scr); 122760471Snyan sio_setreg(com, com_scr, scr); 122851078Speter if (scr1 != 0xa5 || scr2 != 0x5a) { 122951078Speter printf(" 8250"); 123051078Speter goto determined_type; 123151078Speter } 123251078Speter } 123360471Snyan sio_setreg(com, com_fifo, FIFO_ENABLE | FIFO_RX_HIGH); 123451078Speter DELAY(100); 123551078Speter com->st16650a = 0; 123651078Speter switch (inb(com->int_id_port) & IIR_FIFO_MASK) { 123751078Speter case FIFO_RX_LOW: 123851078Speter printf(" 16450"); 123951078Speter break; 124051078Speter case FIFO_RX_MEDL: 124151078Speter printf(" 16450?"); 124251078Speter break; 124351078Speter case FIFO_RX_MEDH: 124451078Speter printf(" 16550?"); 124551078Speter break; 124651078Speter case FIFO_RX_HIGH: 124751078Speter if (COM_NOFIFO(flags)) { 124851078Speter printf(" 16550A fifo disabled"); 124951078Speter } else { 125051078Speter com->hasfifo = TRUE; 125151078Speter if (COM_ST16650A(flags)) { 125251078Speter com->st16650a = 1; 125351078Speter com->tx_fifo_size = 32; 125451078Speter printf(" ST16650A"); 125551078Speter } else { 125651078Speter com->tx_fifo_size = COM_FIFOSIZE(flags); 125751078Speter printf(" 16550A"); 125851078Speter } 125951078Speter } 126051078Speter#ifdef COM_ESP 126151078Speter for (espp = likely_esp_ports; *espp != 0; espp++) 126251078Speter if (espattach(com, *espp)) { 126351078Speter com->tx_fifo_size = 1024; 126451078Speter break; 126551078Speter } 126651078Speter#endif 126751078Speter if (!com->st16650a) { 126851078Speter if (!com->tx_fifo_size) 126951078Speter com->tx_fifo_size = 16; 127051078Speter else 127151078Speter printf(" lookalike with %d bytes FIFO", 127251078Speter com->tx_fifo_size); 127351078Speter } 127451078Speter 127551078Speter break; 127651078Speter } 127751078Speter 127851078Speter#ifdef COM_ESP 127951078Speter if (com->esp) { 128051078Speter /* 128151078Speter * Set 16550 compatibility mode. 128251078Speter * We don't use the ESP_MODE_SCALE bit to increase the 128351078Speter * fifo trigger levels because we can't handle large 128451078Speter * bursts of input. 128551078Speter * XXX flow control should be set in comparam(), not here. 128651078Speter */ 128751078Speter outb(com->esp_port + ESP_CMD1, ESP_SETMODE); 128851078Speter outb(com->esp_port + ESP_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO); 128951078Speter 129051078Speter /* Set RTS/CTS flow control. */ 129151078Speter outb(com->esp_port + ESP_CMD1, ESP_SETFLOWTYPE); 129251078Speter outb(com->esp_port + ESP_CMD2, ESP_FLOW_RTS); 129351078Speter outb(com->esp_port + ESP_CMD2, ESP_FLOW_CTS); 129451078Speter 129551078Speter /* Set flow-control levels. */ 129651078Speter outb(com->esp_port + ESP_CMD1, ESP_SETRXFLOW); 129751078Speter outb(com->esp_port + ESP_CMD2, HIBYTE(768)); 129851078Speter outb(com->esp_port + ESP_CMD2, LOBYTE(768)); 129951078Speter outb(com->esp_port + ESP_CMD2, HIBYTE(512)); 130051078Speter outb(com->esp_port + ESP_CMD2, LOBYTE(512)); 130151078Speter } 130251078Speter#endif /* COM_ESP */ 130360471Snyan sio_setreg(com, com_fifo, 0); 130451078Speterdetermined_type: ; 130551078Speter 130651078Speter#ifdef COM_MULTIPORT 130751078Speter if (COM_ISMULTIPORT(flags)) { 130853344Speter device_t masterdev; 130953344Speter 131051078Speter com->multiport = TRUE; 131151078Speter printf(" (multiport"); 131251078Speter if (unit == COM_MPMASTER(flags)) 131351078Speter printf(" master"); 131451078Speter printf(")"); 131553344Speter masterdev = devclass_get_device(sio_devclass, 131653344Speter COM_MPMASTER(flags)); 131757234Sbde com->no_irq = (masterdev == NULL || bus_get_resource(masterdev, 131857234Sbde SYS_RES_IRQ, 0, NULL, NULL) != 0); 131951078Speter } 132051078Speter#endif /* COM_MULTIPORT */ 132151078Speter if (unit == comconsole) 132251078Speter printf(", console"); 132353344Speter if (COM_IIR_TXRDYBUG(flags)) 132451078Speter printf(" with a bogus IIR_TXRDY register"); 132551078Speter printf("\n"); 132651078Speter 132767551Sjhb if (sio_fast_ih == NULL) { 132872238Sjhb swi_add(&tty_ithd, "tty:sio", siopoll, NULL, SWI_TTY, 0, 132972238Sjhb &sio_fast_ih); 133072238Sjhb swi_add(&clk_ithd, "tty:sio", siopoll, NULL, SWI_TTY, 0, 133172238Sjhb &sio_slow_ih); 133251078Speter } 133365131Sphk com->devs[0] = make_dev(&sio_cdevsw, unit, 133451078Speter UID_ROOT, GID_WHEEL, 0600, "ttyd%r", unit); 133565131Sphk com->devs[1] = make_dev(&sio_cdevsw, unit | CONTROL_INIT_STATE, 133651078Speter UID_ROOT, GID_WHEEL, 0600, "ttyid%r", unit); 133765131Sphk com->devs[2] = make_dev(&sio_cdevsw, unit | CONTROL_LOCK_STATE, 133851078Speter UID_ROOT, GID_WHEEL, 0600, "ttyld%r", unit); 133965131Sphk com->devs[3] = make_dev(&sio_cdevsw, unit | CALLOUT_MASK, 134051078Speter UID_UUCP, GID_DIALER, 0660, "cuaa%r", unit); 134165131Sphk com->devs[4] = make_dev(&sio_cdevsw, 134265131Sphk unit | CALLOUT_MASK | CONTROL_INIT_STATE, 134351078Speter UID_UUCP, GID_DIALER, 0660, "cuaia%r", unit); 134465131Sphk com->devs[5] = make_dev(&sio_cdevsw, 134565131Sphk unit | CALLOUT_MASK | CONTROL_LOCK_STATE, 134651078Speter UID_UUCP, GID_DIALER, 0660, "cuala%r", unit); 134751078Speter com->flags = flags; 134851078Speter com->pps.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR; 134951078Speter pps_init(&com->pps); 135051078Speter 135151078Speter rid = 0; 135251078Speter com->irqres = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0ul, ~0ul, 1, 135353344Speter RF_ACTIVE); 135453344Speter if (com->irqres) { 135553344Speter ret = BUS_SETUP_INTR(device_get_parent(dev), dev, com->irqres, 135665557Sjasone INTR_TYPE_TTY | INTR_FAST, 135754386Simp siointr, com, &com->cookie); 135854194Speter if (ret) { 135954194Speter ret = BUS_SETUP_INTR(device_get_parent(dev), dev, 136054194Speter com->irqres, INTR_TYPE_TTY, 136154386Simp siointr, com, &com->cookie); 136254194Speter if (ret == 0) 136383246Sdd device_printf(dev, "unable to activate interrupt in fast mode - using normal mode\n"); 136454194Speter } 136553344Speter if (ret) 136653344Speter device_printf(dev, "could not activate interrupt\n"); 136778504Siedowse#if defined(DDB) && (defined(BREAK_TO_DEBUGGER) || \ 136878504Siedowse defined(ALT_BREAK_TO_DEBUGGER)) 136978504Siedowse /* 137078504Siedowse * Enable interrupts for early break-to-debugger support 137178504Siedowse * on the console. 137278504Siedowse */ 137378504Siedowse if (ret == 0 && unit == comconsole) 137478504Siedowse outb(siocniobase + com_ier, IER_ERXRDY | IER_ERLS | 137578504Siedowse IER_EMSC); 137678504Siedowse#endif 137753344Speter } 137851078Speter 137951078Speter return (0); 138051078Speter} 138151078Speter 138251078Speterstatic int 138383366Sjuliansioopen(dev, flag, mode, td) 138451078Speter dev_t dev; 138551078Speter int flag; 138651078Speter int mode; 138783366Sjulian struct thread *td; 138851078Speter{ 138951078Speter struct com_s *com; 139051078Speter int error; 139151078Speter int mynor; 139251078Speter int s; 139351078Speter struct tty *tp; 139451078Speter int unit; 139551078Speter 139651078Speter mynor = minor(dev); 139751078Speter unit = MINOR_TO_UNIT(mynor); 139853344Speter com = com_addr(unit); 139953344Speter if (com == NULL) 140051078Speter return (ENXIO); 140151078Speter if (com->gone) 140251078Speter return (ENXIO); 140351078Speter if (mynor & CONTROL_MASK) 140451078Speter return (0); 140551078Speter tp = dev->si_tty = com->tp = ttymalloc(com->tp); 140651078Speter s = spltty(); 140751078Speter /* 140851078Speter * We jump to this label after all non-interrupted sleeps to pick 140951078Speter * up any changes of the device state. 141051078Speter */ 141151078Speteropen_top: 141251078Speter while (com->state & CS_DTR_OFF) { 141351078Speter error = tsleep(&com->dtr_wait, TTIPRI | PCATCH, "siodtr", 0); 141451078Speter if (com_addr(unit) == NULL) 141551078Speter return (ENXIO); 141651078Speter if (error != 0 || com->gone) 141751078Speter goto out; 141851078Speter } 141951078Speter if (tp->t_state & TS_ISOPEN) { 142051078Speter /* 142151078Speter * The device is open, so everything has been initialized. 142251078Speter * Handle conflicts. 142351078Speter */ 142451078Speter if (mynor & CALLOUT_MASK) { 142551078Speter if (!com->active_out) { 142651078Speter error = EBUSY; 142751078Speter goto out; 142851078Speter } 142951078Speter } else { 143051078Speter if (com->active_out) { 143151078Speter if (flag & O_NONBLOCK) { 143251078Speter error = EBUSY; 143351078Speter goto out; 143451078Speter } 143551078Speter error = tsleep(&com->active_out, 143651078Speter TTIPRI | PCATCH, "siobi", 0); 143751078Speter if (com_addr(unit) == NULL) 143851078Speter return (ENXIO); 143951078Speter if (error != 0 || com->gone) 144051078Speter goto out; 144151078Speter goto open_top; 144251078Speter } 144351078Speter } 144451078Speter if (tp->t_state & TS_XCLUDE && 144583366Sjulian suser_td(td)) { 144651078Speter error = EBUSY; 144751078Speter goto out; 144851078Speter } 144951078Speter } else { 145051078Speter /* 145151078Speter * The device isn't open, so there are no conflicts. 145251078Speter * Initialize it. Initialization is done twice in many 145351078Speter * cases: to preempt sleeping callin opens if we are 145451078Speter * callout, and to complete a callin open after DCD rises. 145551078Speter */ 145651078Speter tp->t_oproc = comstart; 145751078Speter tp->t_param = comparam; 145851654Sphk tp->t_stop = comstop; 145951078Speter tp->t_dev = dev; 146051078Speter tp->t_termios = mynor & CALLOUT_MASK 146151078Speter ? com->it_out : com->it_in; 146251078Speter (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET); 146351078Speter com->poll = com->no_irq; 146451078Speter com->poll_output = com->loses_outints; 146551078Speter ++com->wopeners; 146651078Speter error = comparam(tp, &tp->t_termios); 146751078Speter --com->wopeners; 146851078Speter if (error != 0) 146951078Speter goto out; 147051078Speter /* 147151078Speter * XXX we should goto open_top if comparam() slept. 147251078Speter */ 147351078Speter if (com->hasfifo) { 147451078Speter /* 147551078Speter * (Re)enable and drain fifos. 147651078Speter * 147751078Speter * Certain SMC chips cause problems if the fifos 147851078Speter * are enabled while input is ready. Turn off the 147951078Speter * fifo if necessary to clear the input. We test 148051078Speter * the input ready bit after enabling the fifos 148151078Speter * since we've already enabled them in comparam() 148251078Speter * and to handle races between enabling and fresh 148351078Speter * input. 148451078Speter */ 148551078Speter while (TRUE) { 148660471Snyan sio_setreg(com, com_fifo, 148760471Snyan FIFO_RCV_RST | FIFO_XMT_RST 148860471Snyan | com->fifo_image); 148951078Speter /* 149051078Speter * XXX the delays are for superstitious 149151078Speter * historical reasons. It must be less than 149251078Speter * the character time at the maximum 149351078Speter * supported speed (87 usec at 115200 bps 149451078Speter * 8N1). Otherwise we might loop endlessly 149551078Speter * if data is streaming in. We used to use 149651078Speter * delays of 100. That usually worked 149751078Speter * because DELAY(100) used to usually delay 149851078Speter * for about 85 usec instead of 100. 149951078Speter */ 150051078Speter DELAY(50); 150151078Speter if (!(inb(com->line_status_port) & LSR_RXRDY)) 150251078Speter break; 150360471Snyan sio_setreg(com, com_fifo, 0); 150451078Speter DELAY(50); 150551078Speter (void) inb(com->data_port); 150651078Speter } 150751078Speter } 150851078Speter 150972200Sbmilekic mtx_lock_spin(&sio_lock); 151051078Speter (void) inb(com->line_status_port); 151151078Speter (void) inb(com->data_port); 151251078Speter com->prev_modem_status = com->last_modem_status 151351078Speter = inb(com->modem_status_port); 151451078Speter if (COM_IIR_TXRDYBUG(com->flags)) { 151551078Speter outb(com->intr_ctl_port, IER_ERXRDY | IER_ERLS 151651078Speter | IER_EMSC); 151751078Speter } else { 151851078Speter outb(com->intr_ctl_port, IER_ERXRDY | IER_ETXRDY 151951078Speter | IER_ERLS | IER_EMSC); 152051078Speter } 152172200Sbmilekic mtx_unlock_spin(&sio_lock); 152251078Speter /* 152351078Speter * Handle initial DCD. Callout devices get a fake initial 152451078Speter * DCD (trapdoor DCD). If we are callout, then any sleeping 152551078Speter * callin opens get woken up and resume sleeping on "siobi" 152651078Speter * instead of "siodcd". 152751078Speter */ 152851078Speter /* 152951078Speter * XXX `mynor & CALLOUT_MASK' should be 153051078Speter * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where 153151078Speter * TRAPDOOR_CARRIER is the default initial state for callout 153251078Speter * devices and SOFT_CARRIER is like CLOCAL except it hides 153351078Speter * the true carrier. 153451078Speter */ 153551078Speter if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK) 153651078Speter (*linesw[tp->t_line].l_modem)(tp, 1); 153751078Speter } 153851078Speter /* 153951078Speter * Wait for DCD if necessary. 154051078Speter */ 154151078Speter if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK) 154251078Speter && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) { 154351078Speter ++com->wopeners; 154451078Speter error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "siodcd", 0); 154551078Speter if (com_addr(unit) == NULL) 154651078Speter return (ENXIO); 154751078Speter --com->wopeners; 154851078Speter if (error != 0 || com->gone) 154951078Speter goto out; 155051078Speter goto open_top; 155151078Speter } 155251078Speter error = (*linesw[tp->t_line].l_open)(dev, tp); 155351078Speter disc_optim(tp, &tp->t_termios, com); 155451078Speter if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK) 155551078Speter com->active_out = TRUE; 155651078Speter siosettimeout(); 155751078Speterout: 155851078Speter splx(s); 155951078Speter if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0) 156051078Speter comhardclose(com); 156151078Speter return (error); 156251078Speter} 156351078Speter 156451078Speterstatic int 156583366Sjuliansioclose(dev, flag, mode, td) 156651078Speter dev_t dev; 156751078Speter int flag; 156851078Speter int mode; 156983366Sjulian struct thread *td; 157051078Speter{ 157151078Speter struct com_s *com; 157251078Speter int mynor; 157351078Speter int s; 157451078Speter struct tty *tp; 157551078Speter 157651078Speter mynor = minor(dev); 157751078Speter if (mynor & CONTROL_MASK) 157851078Speter return (0); 157951078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 158057915Simp if (com == NULL) 158157915Simp return (ENODEV); 158251078Speter tp = com->tp; 158351078Speter s = spltty(); 158451078Speter (*linesw[tp->t_line].l_close)(tp, flag); 158551078Speter disc_optim(tp, &tp->t_termios, com); 158651654Sphk comstop(tp, FREAD | FWRITE); 158751078Speter comhardclose(com); 158851078Speter ttyclose(tp); 158951078Speter siosettimeout(); 159051078Speter splx(s); 159151078Speter if (com->gone) { 159251078Speter printf("sio%d: gone\n", com->unit); 159351078Speter s = spltty(); 159451078Speter if (com->ibuf != NULL) 159551078Speter free(com->ibuf, M_DEVBUF); 159651078Speter bzero(tp, sizeof *tp); 159751078Speter splx(s); 159851078Speter } 159951078Speter return (0); 160051078Speter} 160151078Speter 160251078Speterstatic void 160351078Spetercomhardclose(com) 160451078Speter struct com_s *com; 160551078Speter{ 160651078Speter int s; 160751078Speter struct tty *tp; 160851078Speter int unit; 160951078Speter 161051078Speter unit = com->unit; 161151078Speter s = spltty(); 161251078Speter com->poll = FALSE; 161351078Speter com->poll_output = FALSE; 161451078Speter com->do_timestamp = FALSE; 161551078Speter com->do_dcd_timestamp = FALSE; 161651078Speter com->pps.ppsparam.mode = 0; 161760471Snyan sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 161878504Siedowse tp = com->tp; 161978504Siedowse 162078504Siedowse#if defined(DDB) && (defined(BREAK_TO_DEBUGGER) || \ 162178504Siedowse defined(ALT_BREAK_TO_DEBUGGER)) 162278504Siedowse /* 162378504Siedowse * Leave interrupts enabled and don't clear DTR if this is the 162478504Siedowse * console. This allows us to detect break-to-debugger events 162578504Siedowse * while the console device is closed. 162678504Siedowse */ 162778504Siedowse if (com->unit != comconsole) 162878504Siedowse#endif 162951078Speter { 163060471Snyan sio_setreg(com, com_ier, 0); 163151078Speter if (tp->t_cflag & HUPCL 163251078Speter /* 163351078Speter * XXX we will miss any carrier drop between here and the 163451078Speter * next open. Perhaps we should watch DCD even when the 163551078Speter * port is closed; it is not sufficient to check it at 163651078Speter * the next open because it might go up and down while 163751078Speter * we're not watching. 163851078Speter */ 163951078Speter || (!com->active_out 164051078Speter && !(com->prev_modem_status & MSR_DCD) 164151078Speter && !(com->it_in.c_cflag & CLOCAL)) 164251078Speter || !(tp->t_state & TS_ISOPEN)) { 164351078Speter (void)commctl(com, TIOCM_DTR, DMBIC); 164451078Speter if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) { 164551078Speter timeout(siodtrwakeup, com, com->dtr_wait); 164651078Speter com->state |= CS_DTR_OFF; 164751078Speter } 164851078Speter } 164951078Speter } 165051078Speter if (com->hasfifo) { 165151078Speter /* 165251078Speter * Disable fifos so that they are off after controlled 165351078Speter * reboots. Some BIOSes fail to detect 16550s when the 165451078Speter * fifos are enabled. 165551078Speter */ 165660471Snyan sio_setreg(com, com_fifo, 0); 165751078Speter } 165851078Speter com->active_out = FALSE; 165951078Speter wakeup(&com->active_out); 166051078Speter wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */ 166151078Speter splx(s); 166251078Speter} 166351078Speter 166451078Speterstatic int 166551078Spetersioread(dev, uio, flag) 166651078Speter dev_t dev; 166751078Speter struct uio *uio; 166851078Speter int flag; 166951078Speter{ 167051078Speter int mynor; 167151078Speter struct com_s *com; 167251078Speter 167351078Speter mynor = minor(dev); 167451078Speter if (mynor & CONTROL_MASK) 167551078Speter return (ENODEV); 167651078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 167757915Simp if (com == NULL || com->gone) 167851078Speter return (ENODEV); 167951078Speter return ((*linesw[com->tp->t_line].l_read)(com->tp, uio, flag)); 168051078Speter} 168151078Speter 168251078Speterstatic int 168351078Spetersiowrite(dev, uio, flag) 168451078Speter dev_t dev; 168551078Speter struct uio *uio; 168651078Speter int flag; 168751078Speter{ 168851078Speter int mynor; 168951078Speter struct com_s *com; 169051078Speter int unit; 169151078Speter 169251078Speter mynor = minor(dev); 169351078Speter if (mynor & CONTROL_MASK) 169451078Speter return (ENODEV); 169551078Speter 169651078Speter unit = MINOR_TO_UNIT(mynor); 169751078Speter com = com_addr(unit); 169857915Simp if (com == NULL || com->gone) 169951078Speter return (ENODEV); 170051078Speter /* 170151078Speter * (XXX) We disallow virtual consoles if the physical console is 170251078Speter * a serial port. This is in case there is a display attached that 170351078Speter * is not the console. In that situation we don't need/want the X 170451078Speter * server taking over the console. 170551078Speter */ 170651078Speter if (constty != NULL && unit == comconsole) 170751078Speter constty = NULL; 170851078Speter return ((*linesw[com->tp->t_line].l_write)(com->tp, uio, flag)); 170951078Speter} 171051078Speter 171151078Speterstatic void 171251078Spetersiobusycheck(chan) 171351078Speter void *chan; 171451078Speter{ 171551078Speter struct com_s *com; 171651078Speter int s; 171751078Speter 171851078Speter com = (struct com_s *)chan; 171951078Speter 172051078Speter /* 172151078Speter * Clear TS_BUSY if low-level output is complete. 172251078Speter * spl locking is sufficient because siointr1() does not set CS_BUSY. 172351078Speter * If siointr1() clears CS_BUSY after we look at it, then we'll get 172451078Speter * called again. Reading the line status port outside of siointr1() 172551078Speter * is safe because CS_BUSY is clear so there are no output interrupts 172651078Speter * to lose. 172751078Speter */ 172851078Speter s = spltty(); 172951078Speter if (com->state & CS_BUSY) 173051078Speter com->extra_state &= ~CSE_BUSYCHECK; /* False alarm. */ 173151078Speter else if ((inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY)) 173251078Speter == (LSR_TSRE | LSR_TXRDY)) { 173351078Speter com->tp->t_state &= ~TS_BUSY; 173451078Speter ttwwakeup(com->tp); 173551078Speter com->extra_state &= ~CSE_BUSYCHECK; 173651078Speter } else 173751078Speter timeout(siobusycheck, com, hz / 100); 173851078Speter splx(s); 173951078Speter} 174051078Speter 174151078Speterstatic void 174251078Spetersiodtrwakeup(chan) 174351078Speter void *chan; 174451078Speter{ 174551078Speter struct com_s *com; 174651078Speter 174751078Speter com = (struct com_s *)chan; 174851078Speter com->state &= ~CS_DTR_OFF; 174951078Speter wakeup(&com->dtr_wait); 175051078Speter} 175151078Speter 175265557Sjasone/* 175370174Sjhb * Call this function with the sio_lock mutex held. It will return with the 175470174Sjhb * lock still held. 175565557Sjasone */ 175651078Speterstatic void 175751078Spetersioinput(com) 175851078Speter struct com_s *com; 175951078Speter{ 176051078Speter u_char *buf; 176151078Speter int incc; 176251078Speter u_char line_status; 176351078Speter int recv_data; 176451078Speter struct tty *tp; 176551078Speter 176651078Speter buf = com->ibuf; 176751078Speter tp = com->tp; 176851078Speter if (!(tp->t_state & TS_ISOPEN) || !(tp->t_cflag & CREAD)) { 176951078Speter com_events -= (com->iptr - com->ibuf); 177051078Speter com->iptr = com->ibuf; 177151078Speter return; 177251078Speter } 177351078Speter if (tp->t_state & TS_CAN_BYPASS_L_RINT) { 177451078Speter /* 177551078Speter * Avoid the grotesquely inefficient lineswitch routine 177651078Speter * (ttyinput) in "raw" mode. It usually takes about 450 177751078Speter * instructions (that's without canonical processing or echo!). 177851078Speter * slinput is reasonably fast (usually 40 instructions plus 177951078Speter * call overhead). 178051078Speter */ 178151078Speter do { 178265557Sjasone /* 178365557Sjasone * This may look odd, but it is using save-and-enable 178465557Sjasone * semantics instead of the save-and-disable semantics 178565557Sjasone * that are used everywhere else. 178665557Sjasone */ 178772200Sbmilekic mtx_unlock_spin(&sio_lock); 178851078Speter incc = com->iptr - buf; 178951078Speter if (tp->t_rawq.c_cc + incc > tp->t_ihiwat 179051078Speter && (com->state & CS_RTS_IFLOW 179151078Speter || tp->t_iflag & IXOFF) 179251078Speter && !(tp->t_state & TS_TBLOCK)) 179351078Speter ttyblock(tp); 179451078Speter com->delta_error_counts[CE_TTY_BUF_OVERFLOW] 179551078Speter += b_to_q((char *)buf, incc, &tp->t_rawq); 179651078Speter buf += incc; 179751078Speter tk_nin += incc; 179851078Speter tk_rawcc += incc; 179951078Speter tp->t_rawcc += incc; 180051078Speter ttwakeup(tp); 180151078Speter if (tp->t_state & TS_TTSTOP 180251078Speter && (tp->t_iflag & IXANY 180351078Speter || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) { 180451078Speter tp->t_state &= ~TS_TTSTOP; 180551078Speter tp->t_lflag &= ~FLUSHO; 180651078Speter comstart(tp); 180751078Speter } 180872200Sbmilekic mtx_lock_spin(&sio_lock); 180951078Speter } while (buf < com->iptr); 181051078Speter } else { 181151078Speter do { 181265557Sjasone /* 181365557Sjasone * This may look odd, but it is using save-and-enable 181465557Sjasone * semantics instead of the save-and-disable semantics 181565557Sjasone * that are used everywhere else. 181665557Sjasone */ 181772200Sbmilekic mtx_unlock_spin(&sio_lock); 181851078Speter line_status = buf[com->ierroff]; 181951078Speter recv_data = *buf++; 182051078Speter if (line_status 182151078Speter & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) { 182251078Speter if (line_status & LSR_BI) 182351078Speter recv_data |= TTY_BI; 182451078Speter if (line_status & LSR_FE) 182551078Speter recv_data |= TTY_FE; 182651078Speter if (line_status & LSR_OE) 182751078Speter recv_data |= TTY_OE; 182851078Speter if (line_status & LSR_PE) 182951078Speter recv_data |= TTY_PE; 183051078Speter } 183151078Speter (*linesw[tp->t_line].l_rint)(recv_data, tp); 183272200Sbmilekic mtx_lock_spin(&sio_lock); 183351078Speter } while (buf < com->iptr); 183451078Speter } 183551078Speter com_events -= (com->iptr - com->ibuf); 183651078Speter com->iptr = com->ibuf; 183751078Speter 183851078Speter /* 183951078Speter * There is now room for another low-level buffer full of input, 184051078Speter * so enable RTS if it is now disabled and there is room in the 184151078Speter * high-level buffer. 184251078Speter */ 184351078Speter if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & MCR_RTS) && 184451078Speter !(tp->t_state & TS_TBLOCK)) 184551078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 184651078Speter} 184751078Speter 184851078Spetervoid 184951078Spetersiointr(arg) 185051078Speter void *arg; 185151078Speter{ 185270174Sjhb struct com_s *com; 185370174Sjhb 185451078Speter#ifndef COM_MULTIPORT 185570174Sjhb com = (struct com_s *)arg; 185670174Sjhb 185772200Sbmilekic mtx_lock_spin(&sio_lock); 185870174Sjhb siointr1(com); 185972200Sbmilekic mtx_unlock_spin(&sio_lock); 186051078Speter#else /* COM_MULTIPORT */ 186151078Speter bool_t possibly_more_intrs; 186251078Speter int unit; 186351078Speter 186451078Speter /* 186551078Speter * Loop until there is no activity on any port. This is necessary 186651078Speter * to get an interrupt edge more than to avoid another interrupt. 186751078Speter * If the IRQ signal is just an OR of the IRQ signals from several 186851078Speter * devices, then the edge from one may be lost because another is 186951078Speter * on. 187051078Speter */ 187172200Sbmilekic mtx_lock_spin(&sio_lock); 187251078Speter do { 187351078Speter possibly_more_intrs = FALSE; 187453344Speter for (unit = 0; unit < sio_numunits; ++unit) { 187551078Speter com = com_addr(unit); 187651078Speter /* 187751078Speter * XXX COM_LOCK(); 187851078Speter * would it work here, or be counter-productive? 187951078Speter */ 188051078Speter if (com != NULL 188151078Speter && !com->gone 188251078Speter && (inb(com->int_id_port) & IIR_IMASK) 188351078Speter != IIR_NOPEND) { 188451078Speter siointr1(com); 188551078Speter possibly_more_intrs = TRUE; 188651078Speter } 188751078Speter /* XXX COM_UNLOCK(); */ 188851078Speter } 188951078Speter } while (possibly_more_intrs); 189072200Sbmilekic mtx_unlock_spin(&sio_lock); 189151078Speter#endif /* COM_MULTIPORT */ 189251078Speter} 189351078Speter 189451078Speterstatic void 189551078Spetersiointr1(com) 189651078Speter struct com_s *com; 189751078Speter{ 189851078Speter u_char line_status; 189951078Speter u_char modem_status; 190051078Speter u_char *ioptr; 190151078Speter u_char recv_data; 190251078Speter u_char int_ctl; 190351078Speter u_char int_ctl_new; 190451078Speter struct timecounter *tc; 190551078Speter u_int count; 190651078Speter 190751078Speter int_ctl = inb(com->intr_ctl_port); 190851078Speter int_ctl_new = int_ctl; 190951078Speter 191051078Speter while (!com->gone) { 191151078Speter if (com->pps.ppsparam.mode & PPS_CAPTUREBOTH) { 191251078Speter modem_status = inb(com->modem_status_port); 191351078Speter if ((modem_status ^ com->last_modem_status) & MSR_DCD) { 191451078Speter tc = timecounter; 191551078Speter count = tc->tc_get_timecount(tc); 191651078Speter pps_event(&com->pps, tc, count, 191751078Speter (modem_status & MSR_DCD) ? 191851078Speter PPS_CAPTUREASSERT : PPS_CAPTURECLEAR); 191951078Speter } 192051078Speter } 192151078Speter line_status = inb(com->line_status_port); 192251078Speter 192351078Speter /* input event? (check first to help avoid overruns) */ 192451078Speter while (line_status & LSR_RCV_MASK) { 192551078Speter /* break/unnattached error bits or real input? */ 192651078Speter if (!(line_status & LSR_RXRDY)) 192751078Speter recv_data = 0; 192851078Speter else 192951078Speter recv_data = inb(com->data_port); 193061649Sps#if defined(DDB) && defined(ALT_BREAK_TO_DEBUGGER) 193161649Sps /* 193261649Sps * Solaris implements a new BREAK which is initiated 193361649Sps * by a character sequence CR ~ ^b which is similar 193461649Sps * to a familiar pattern used on Sun servers by the 193561649Sps * Remote Console. 193661649Sps */ 193761649Sps#define KEY_CRTLB 2 /* ^B */ 193861649Sps#define KEY_CR 13 /* CR '\r' */ 193961649Sps#define KEY_TILDE 126 /* ~ */ 194061649Sps 194161649Sps if (com->unit == comconsole) { 194261649Sps static int brk_state1 = 0, brk_state2 = 0; 194361649Sps if (recv_data == KEY_CR) { 194461649Sps brk_state1 = recv_data; 194561649Sps brk_state2 = 0; 194665557Sjasone } else if (brk_state1 == KEY_CR 194765557Sjasone && (recv_data == KEY_TILDE 194865557Sjasone || recv_data == KEY_CRTLB)) { 194961649Sps if (recv_data == KEY_TILDE) 195061649Sps brk_state2 = recv_data; 195165557Sjasone else if (brk_state2 == KEY_TILDE 195265557Sjasone && recv_data == KEY_CRTLB) { 195361649Sps breakpoint(); 195465557Sjasone brk_state1 = 0; 195565557Sjasone brk_state2 = 0; 195661649Sps goto cont; 195761649Sps } else 195861649Sps brk_state2 = 0; 195961649Sps } else 196061649Sps brk_state1 = 0; 196161649Sps } 196261649Sps#endif 196351078Speter if (line_status & (LSR_BI | LSR_FE | LSR_PE)) { 196451078Speter /* 196551078Speter * Don't store BI if IGNBRK or FE/PE if IGNPAR. 196651078Speter * Otherwise, push the work to a higher level 196751078Speter * (to handle PARMRK) if we're bypassing. 196851078Speter * Otherwise, convert BI/FE and PE+INPCK to 0. 196951078Speter * 197051078Speter * This makes bypassing work right in the 197151078Speter * usual "raw" case (IGNBRK set, and IGNPAR 197251078Speter * and INPCK clear). 197351078Speter * 197451078Speter * Note: BI together with FE/PE means just BI. 197551078Speter */ 197651078Speter if (line_status & LSR_BI) { 197751078Speter#if defined(DDB) && defined(BREAK_TO_DEBUGGER) 197851078Speter if (com->unit == comconsole) { 197951078Speter breakpoint(); 198051078Speter goto cont; 198151078Speter } 198251078Speter#endif 198351078Speter if (com->tp == NULL 198451078Speter || com->tp->t_iflag & IGNBRK) 198551078Speter goto cont; 198651078Speter } else { 198751078Speter if (com->tp == NULL 198851078Speter || com->tp->t_iflag & IGNPAR) 198951078Speter goto cont; 199051078Speter } 199151078Speter if (com->tp->t_state & TS_CAN_BYPASS_L_RINT 199251078Speter && (line_status & (LSR_BI | LSR_FE) 199351078Speter || com->tp->t_iflag & INPCK)) 199451078Speter recv_data = 0; 199551078Speter } 199651078Speter ++com->bytes_in; 199751078Speter if (com->hotchar != 0 && recv_data == com->hotchar) 199872238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 199951078Speter ioptr = com->iptr; 200051078Speter if (ioptr >= com->ibufend) 200151078Speter CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW); 200251078Speter else { 200351078Speter if (com->do_timestamp) 200451078Speter microtime(&com->timestamp); 200551078Speter ++com_events; 200672238Sjhb swi_sched(sio_slow_ih, SWI_DELAY); 200751078Speter#if 0 /* for testing input latency vs efficiency */ 200851078Speterif (com->iptr - com->ibuf == 8) 200972238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 201051078Speter#endif 201151078Speter ioptr[0] = recv_data; 201251078Speter ioptr[com->ierroff] = line_status; 201351078Speter com->iptr = ++ioptr; 201451078Speter if (ioptr == com->ihighwater 201551078Speter && com->state & CS_RTS_IFLOW) 201651078Speter outb(com->modem_ctl_port, 201751078Speter com->mcr_image &= ~MCR_RTS); 201851078Speter if (line_status & LSR_OE) 201951078Speter CE_RECORD(com, CE_OVERRUN); 202051078Speter } 202151078Spetercont: 202251078Speter /* 202351078Speter * "& 0x7F" is to avoid the gcc-1.40 generating a slow 202451078Speter * jump from the top of the loop to here 202551078Speter */ 202651078Speter line_status = inb(com->line_status_port) & 0x7F; 202751078Speter } 202851078Speter 202951078Speter /* modem status change? (always check before doing output) */ 203051078Speter modem_status = inb(com->modem_status_port); 203151078Speter if (modem_status != com->last_modem_status) { 203251078Speter if (com->do_dcd_timestamp 203351078Speter && !(com->last_modem_status & MSR_DCD) 203451078Speter && modem_status & MSR_DCD) 203551078Speter microtime(&com->dcd_timestamp); 203651078Speter 203751078Speter /* 203851078Speter * Schedule high level to handle DCD changes. Note 203951078Speter * that we don't use the delta bits anywhere. Some 204051078Speter * UARTs mess them up, and it's easy to remember the 204151078Speter * previous bits and calculate the delta. 204251078Speter */ 204351078Speter com->last_modem_status = modem_status; 204451078Speter if (!(com->state & CS_CHECKMSR)) { 204551078Speter com_events += LOTS_OF_EVENTS; 204651078Speter com->state |= CS_CHECKMSR; 204772238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 204851078Speter } 204951078Speter 205051078Speter /* handle CTS change immediately for crisp flow ctl */ 205151078Speter if (com->state & CS_CTS_OFLOW) { 205251078Speter if (modem_status & MSR_CTS) 205351078Speter com->state |= CS_ODEVREADY; 205451078Speter else 205551078Speter com->state &= ~CS_ODEVREADY; 205651078Speter } 205751078Speter } 205851078Speter 205951078Speter /* output queued and everything ready? */ 206051078Speter if (line_status & LSR_TXRDY 206151078Speter && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) { 206251078Speter ioptr = com->obufq.l_head; 206351078Speter if (com->tx_fifo_size > 1) { 206451078Speter u_int ocount; 206551078Speter 206651078Speter ocount = com->obufq.l_tail - ioptr; 206751078Speter if (ocount > com->tx_fifo_size) 206851078Speter ocount = com->tx_fifo_size; 206951078Speter com->bytes_out += ocount; 207051078Speter do 207151078Speter outb(com->data_port, *ioptr++); 207251078Speter while (--ocount != 0); 207351078Speter } else { 207451078Speter outb(com->data_port, *ioptr++); 207551078Speter ++com->bytes_out; 207651078Speter } 207751078Speter com->obufq.l_head = ioptr; 207851078Speter if (COM_IIR_TXRDYBUG(com->flags)) { 207951078Speter int_ctl_new = int_ctl | IER_ETXRDY; 208051078Speter } 208151078Speter if (ioptr >= com->obufq.l_tail) { 208251078Speter struct lbq *qp; 208351078Speter 208451078Speter qp = com->obufq.l_next; 208551078Speter qp->l_queued = FALSE; 208651078Speter qp = qp->l_next; 208751078Speter if (qp != NULL) { 208851078Speter com->obufq.l_head = qp->l_head; 208951078Speter com->obufq.l_tail = qp->l_tail; 209051078Speter com->obufq.l_next = qp; 209151078Speter } else { 209251078Speter /* output just completed */ 209353344Speter if (COM_IIR_TXRDYBUG(com->flags)) { 209451078Speter int_ctl_new = int_ctl & ~IER_ETXRDY; 209551078Speter } 209651078Speter com->state &= ~CS_BUSY; 209751078Speter } 209851078Speter if (!(com->state & CS_ODONE)) { 209951078Speter com_events += LOTS_OF_EVENTS; 210051078Speter com->state |= CS_ODONE; 210167551Sjhb /* handle at high level ASAP */ 210272238Sjhb swi_sched(sio_fast_ih, SWI_NOSWITCH); 210351078Speter } 210451078Speter } 210553344Speter if (COM_IIR_TXRDYBUG(com->flags) && (int_ctl != int_ctl_new)) { 210651078Speter outb(com->intr_ctl_port, int_ctl_new); 210751078Speter } 210851078Speter } 210951078Speter 211051078Speter /* finished? */ 211151078Speter#ifndef COM_MULTIPORT 211251078Speter if ((inb(com->int_id_port) & IIR_IMASK) == IIR_NOPEND) 211351078Speter#endif /* COM_MULTIPORT */ 211451078Speter return; 211551078Speter } 211651078Speter} 211751078Speter 211851078Speterstatic int 211983366Sjuliansioioctl(dev, cmd, data, flag, td) 212051078Speter dev_t dev; 212151078Speter u_long cmd; 212251078Speter caddr_t data; 212351078Speter int flag; 212483366Sjulian struct thread *td; 212551078Speter{ 212651078Speter struct com_s *com; 212751078Speter int error; 212851078Speter int mynor; 212951078Speter int s; 213051078Speter struct tty *tp; 213151078Speter#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 213251078Speter u_long oldcmd; 213351078Speter struct termios term; 213451078Speter#endif 213551078Speter 213651078Speter mynor = minor(dev); 213751078Speter com = com_addr(MINOR_TO_UNIT(mynor)); 213857915Simp if (com == NULL || com->gone) 213951078Speter return (ENODEV); 214051078Speter if (mynor & CONTROL_MASK) { 214151078Speter struct termios *ct; 214251078Speter 214351078Speter switch (mynor & CONTROL_MASK) { 214451078Speter case CONTROL_INIT_STATE: 214551078Speter ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in; 214651078Speter break; 214751078Speter case CONTROL_LOCK_STATE: 214851078Speter ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in; 214951078Speter break; 215051078Speter default: 215151078Speter return (ENODEV); /* /dev/nodev */ 215251078Speter } 215351078Speter switch (cmd) { 215451078Speter case TIOCSETA: 215583366Sjulian error = suser_td(td); 215651078Speter if (error != 0) 215751078Speter return (error); 215851078Speter *ct = *(struct termios *)data; 215951078Speter return (0); 216051078Speter case TIOCGETA: 216151078Speter *(struct termios *)data = *ct; 216251078Speter return (0); 216351078Speter case TIOCGETD: 216451078Speter *(int *)data = TTYDISC; 216551078Speter return (0); 216651078Speter case TIOCGWINSZ: 216751078Speter bzero(data, sizeof(struct winsize)); 216851078Speter return (0); 216951078Speter default: 217051078Speter return (ENOTTY); 217151078Speter } 217251078Speter } 217351078Speter tp = com->tp; 217451078Speter#if defined(COMPAT_43) || defined(COMPAT_SUNOS) 217551078Speter term = tp->t_termios; 217651078Speter oldcmd = cmd; 217751078Speter error = ttsetcompat(tp, &cmd, data, &term); 217851078Speter if (error != 0) 217951078Speter return (error); 218051078Speter if (cmd != oldcmd) 218151078Speter data = (caddr_t)&term; 218251078Speter#endif 218351078Speter if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) { 218451078Speter int cc; 218551078Speter struct termios *dt = (struct termios *)data; 218651078Speter struct termios *lt = mynor & CALLOUT_MASK 218751078Speter ? &com->lt_out : &com->lt_in; 218851078Speter 218951078Speter dt->c_iflag = (tp->t_iflag & lt->c_iflag) 219051078Speter | (dt->c_iflag & ~lt->c_iflag); 219151078Speter dt->c_oflag = (tp->t_oflag & lt->c_oflag) 219251078Speter | (dt->c_oflag & ~lt->c_oflag); 219351078Speter dt->c_cflag = (tp->t_cflag & lt->c_cflag) 219451078Speter | (dt->c_cflag & ~lt->c_cflag); 219551078Speter dt->c_lflag = (tp->t_lflag & lt->c_lflag) 219651078Speter | (dt->c_lflag & ~lt->c_lflag); 219751078Speter for (cc = 0; cc < NCCS; ++cc) 219851078Speter if (lt->c_cc[cc] != 0) 219951078Speter dt->c_cc[cc] = tp->t_cc[cc]; 220051078Speter if (lt->c_ispeed != 0) 220151078Speter dt->c_ispeed = tp->t_ispeed; 220251078Speter if (lt->c_ospeed != 0) 220351078Speter dt->c_ospeed = tp->t_ospeed; 220451078Speter } 220583366Sjulian error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td); 220651078Speter if (error != ENOIOCTL) 220751078Speter return (error); 220851078Speter s = spltty(); 220951078Speter error = ttioctl(tp, cmd, data, flag); 221051078Speter disc_optim(tp, &tp->t_termios, com); 221151078Speter if (error != ENOIOCTL) { 221251078Speter splx(s); 221351078Speter return (error); 221451078Speter } 221551078Speter switch (cmd) { 221651078Speter case TIOCSBRK: 221760471Snyan sio_setreg(com, com_cfcr, com->cfcr_image |= CFCR_SBREAK); 221851078Speter break; 221951078Speter case TIOCCBRK: 222060471Snyan sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 222151078Speter break; 222251078Speter case TIOCSDTR: 222351078Speter (void)commctl(com, TIOCM_DTR, DMBIS); 222451078Speter break; 222551078Speter case TIOCCDTR: 222651078Speter (void)commctl(com, TIOCM_DTR, DMBIC); 222751078Speter break; 222851078Speter /* 222951078Speter * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The 223051078Speter * changes get undone on the next call to comparam(). 223151078Speter */ 223251078Speter case TIOCMSET: 223351078Speter (void)commctl(com, *(int *)data, DMSET); 223451078Speter break; 223551078Speter case TIOCMBIS: 223651078Speter (void)commctl(com, *(int *)data, DMBIS); 223751078Speter break; 223851078Speter case TIOCMBIC: 223951078Speter (void)commctl(com, *(int *)data, DMBIC); 224051078Speter break; 224151078Speter case TIOCMGET: 224251078Speter *(int *)data = commctl(com, 0, DMGET); 224351078Speter break; 224451078Speter case TIOCMSDTRWAIT: 224551078Speter /* must be root since the wait applies to following logins */ 224683366Sjulian error = suser_td(td); 224751078Speter if (error != 0) { 224851078Speter splx(s); 224951078Speter return (error); 225051078Speter } 225151078Speter com->dtr_wait = *(int *)data * hz / 100; 225251078Speter break; 225351078Speter case TIOCMGDTRWAIT: 225451078Speter *(int *)data = com->dtr_wait * 100 / hz; 225551078Speter break; 225651078Speter case TIOCTIMESTAMP: 225751078Speter com->do_timestamp = TRUE; 225851078Speter *(struct timeval *)data = com->timestamp; 225951078Speter break; 226051078Speter case TIOCDCDTIMESTAMP: 226151078Speter com->do_dcd_timestamp = TRUE; 226251078Speter *(struct timeval *)data = com->dcd_timestamp; 226351078Speter break; 226451078Speter default: 226551078Speter splx(s); 226651078Speter error = pps_ioctl(cmd, data, &com->pps); 226751078Speter if (error == ENODEV) 226851078Speter error = ENOTTY; 226951078Speter return (error); 227051078Speter } 227151078Speter splx(s); 227251078Speter return (0); 227351078Speter} 227451078Speter 227565557Sjasone/* software interrupt handler for SWI_TTY */ 227651078Speterstatic void 227767551Sjhbsiopoll(void *dummy) 227851078Speter{ 227951078Speter int unit; 228051078Speter 228151078Speter if (com_events == 0) 228251078Speter return; 228351078Speterrepeat: 228453344Speter for (unit = 0; unit < sio_numunits; ++unit) { 228551078Speter struct com_s *com; 228651078Speter int incc; 228751078Speter struct tty *tp; 228851078Speter 228951078Speter com = com_addr(unit); 229051078Speter if (com == NULL) 229151078Speter continue; 229251078Speter tp = com->tp; 229351078Speter if (tp == NULL || com->gone) { 229451078Speter /* 229551078Speter * Discard any events related to never-opened or 229651078Speter * going-away devices. 229751078Speter */ 229872200Sbmilekic mtx_lock_spin(&sio_lock); 229951078Speter incc = com->iptr - com->ibuf; 230051078Speter com->iptr = com->ibuf; 230151078Speter if (com->state & CS_CHECKMSR) { 230251078Speter incc += LOTS_OF_EVENTS; 230351078Speter com->state &= ~CS_CHECKMSR; 230451078Speter } 230551078Speter com_events -= incc; 230672200Sbmilekic mtx_unlock_spin(&sio_lock); 230751078Speter continue; 230851078Speter } 230951078Speter if (com->iptr != com->ibuf) { 231072200Sbmilekic mtx_lock_spin(&sio_lock); 231151078Speter sioinput(com); 231272200Sbmilekic mtx_unlock_spin(&sio_lock); 231351078Speter } 231451078Speter if (com->state & CS_CHECKMSR) { 231551078Speter u_char delta_modem_status; 231651078Speter 231772200Sbmilekic mtx_lock_spin(&sio_lock); 231851078Speter delta_modem_status = com->last_modem_status 231951078Speter ^ com->prev_modem_status; 232051078Speter com->prev_modem_status = com->last_modem_status; 232151078Speter com_events -= LOTS_OF_EVENTS; 232251078Speter com->state &= ~CS_CHECKMSR; 232372200Sbmilekic mtx_unlock_spin(&sio_lock); 232451078Speter if (delta_modem_status & MSR_DCD) 232551078Speter (*linesw[tp->t_line].l_modem) 232651078Speter (tp, com->prev_modem_status & MSR_DCD); 232751078Speter } 232851078Speter if (com->state & CS_ODONE) { 232972200Sbmilekic mtx_lock_spin(&sio_lock); 233051078Speter com_events -= LOTS_OF_EVENTS; 233151078Speter com->state &= ~CS_ODONE; 233272200Sbmilekic mtx_unlock_spin(&sio_lock); 233351078Speter if (!(com->state & CS_BUSY) 233451078Speter && !(com->extra_state & CSE_BUSYCHECK)) { 233551078Speter timeout(siobusycheck, com, hz / 100); 233651078Speter com->extra_state |= CSE_BUSYCHECK; 233751078Speter } 233851078Speter (*linesw[tp->t_line].l_start)(tp); 233951078Speter } 234051078Speter if (com_events == 0) 234151078Speter break; 234251078Speter } 234351078Speter if (com_events >= LOTS_OF_EVENTS) 234451078Speter goto repeat; 234551078Speter} 234651078Speter 234751078Speterstatic int 234851078Spetercomparam(tp, t) 234951078Speter struct tty *tp; 235051078Speter struct termios *t; 235151078Speter{ 235251078Speter u_int cfcr; 235351078Speter int cflag; 235451078Speter struct com_s *com; 235551078Speter int divisor; 235651078Speter u_char dlbh; 235751078Speter u_char dlbl; 235851078Speter int s; 235951078Speter int unit; 236051078Speter 236151078Speter /* do historical conversions */ 236251078Speter if (t->c_ispeed == 0) 236351078Speter t->c_ispeed = t->c_ospeed; 236451078Speter 236551078Speter /* check requested parameters */ 236651078Speter divisor = ttspeedtab(t->c_ospeed, comspeedtab); 236751078Speter if (divisor < 0 || (divisor > 0 && t->c_ispeed != t->c_ospeed)) 236851078Speter return (EINVAL); 236951078Speter 237051078Speter /* parameters are OK, convert them to the com struct and the device */ 237151078Speter unit = DEV_TO_UNIT(tp->t_dev); 237251078Speter com = com_addr(unit); 237357915Simp if (com == NULL) 237457915Simp return (ENODEV); 237551078Speter s = spltty(); 237651078Speter if (divisor == 0) 237751078Speter (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */ 237851078Speter else 237951078Speter (void)commctl(com, TIOCM_DTR, DMBIS); 238051078Speter cflag = t->c_cflag; 238151078Speter switch (cflag & CSIZE) { 238251078Speter case CS5: 238351078Speter cfcr = CFCR_5BITS; 238451078Speter break; 238551078Speter case CS6: 238651078Speter cfcr = CFCR_6BITS; 238751078Speter break; 238851078Speter case CS7: 238951078Speter cfcr = CFCR_7BITS; 239051078Speter break; 239151078Speter default: 239251078Speter cfcr = CFCR_8BITS; 239351078Speter break; 239451078Speter } 239551078Speter if (cflag & PARENB) { 239651078Speter cfcr |= CFCR_PENAB; 239751078Speter if (!(cflag & PARODD)) 239851078Speter cfcr |= CFCR_PEVEN; 239951078Speter } 240051078Speter if (cflag & CSTOPB) 240151078Speter cfcr |= CFCR_STOPB; 240251078Speter 240351078Speter if (com->hasfifo && divisor != 0) { 240451078Speter /* 240551078Speter * Use a fifo trigger level low enough so that the input 240651078Speter * latency from the fifo is less than about 16 msec and 240751078Speter * the total latency is less than about 30 msec. These 240851078Speter * latencies are reasonable for humans. Serial comms 240951078Speter * protocols shouldn't expect anything better since modem 241051078Speter * latencies are larger. 241151078Speter */ 241251078Speter com->fifo_image = t->c_ospeed <= 4800 241351078Speter ? FIFO_ENABLE : FIFO_ENABLE | FIFO_RX_HIGH; 241451078Speter#ifdef COM_ESP 241551078Speter /* 241651078Speter * The Hayes ESP card needs the fifo DMA mode bit set 241751078Speter * in compatibility mode. If not, it will interrupt 241851078Speter * for each character received. 241951078Speter */ 242051078Speter if (com->esp) 242151078Speter com->fifo_image |= FIFO_DMA_MODE; 242251078Speter#endif 242360471Snyan sio_setreg(com, com_fifo, com->fifo_image); 242451078Speter } 242551078Speter 242665605Sjhb /* 242765605Sjhb * This returns with interrupts disabled so that we can complete 242865605Sjhb * the speed change atomically. Keeping interrupts disabled is 242965605Sjhb * especially important while com_data is hidden. 243065605Sjhb */ 243165605Sjhb (void) siosetwater(com, t->c_ispeed); 243265557Sjasone 243351078Speter if (divisor != 0) { 243460471Snyan sio_setreg(com, com_cfcr, cfcr | CFCR_DLAB); 243551078Speter /* 243651078Speter * Only set the divisor registers if they would change, 243751078Speter * since on some 16550 incompatibles (UMC8669F), setting 243851078Speter * them while input is arriving them loses sync until 243951078Speter * data stops arriving. 244051078Speter */ 244151078Speter dlbl = divisor & 0xFF; 244260471Snyan if (sio_getreg(com, com_dlbl) != dlbl) 244360471Snyan sio_setreg(com, com_dlbl, dlbl); 244451078Speter dlbh = (u_int) divisor >> 8; 244560471Snyan if (sio_getreg(com, com_dlbh) != dlbh) 244660471Snyan sio_setreg(com, com_dlbh, dlbh); 244751078Speter } 244851078Speter 244960471Snyan sio_setreg(com, com_cfcr, com->cfcr_image = cfcr); 245051078Speter 245151078Speter if (!(tp->t_state & TS_TTSTOP)) 245251078Speter com->state |= CS_TTGO; 245351078Speter 245451078Speter if (cflag & CRTS_IFLOW) { 245551078Speter if (com->st16650a) { 245660471Snyan sio_setreg(com, com_cfcr, 0xbf); 245760471Snyan sio_setreg(com, com_fifo, 245860471Snyan sio_getreg(com, com_fifo) | 0x40); 245951078Speter } 246051078Speter com->state |= CS_RTS_IFLOW; 246151078Speter /* 246251078Speter * If CS_RTS_IFLOW just changed from off to on, the change 246351078Speter * needs to be propagated to MCR_RTS. This isn't urgent, 246451078Speter * so do it later by calling comstart() instead of repeating 246551078Speter * a lot of code from comstart() here. 246651078Speter */ 246751078Speter } else if (com->state & CS_RTS_IFLOW) { 246851078Speter com->state &= ~CS_RTS_IFLOW; 246951078Speter /* 247051078Speter * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS 247151078Speter * on here, since comstart() won't do it later. 247251078Speter */ 247351078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 247451078Speter if (com->st16650a) { 247560471Snyan sio_setreg(com, com_cfcr, 0xbf); 247660471Snyan sio_setreg(com, com_fifo, 247760471Snyan sio_getreg(com, com_fifo) & ~0x40); 247851078Speter } 247951078Speter } 248051078Speter 248151078Speter 248251078Speter /* 248351078Speter * Set up state to handle output flow control. 248451078Speter * XXX - worth handling MDMBUF (DCD) flow control at the lowest level? 248551078Speter * Now has 10+ msec latency, while CTS flow has 50- usec latency. 248651078Speter */ 248751078Speter com->state |= CS_ODEVREADY; 248851078Speter com->state &= ~CS_CTS_OFLOW; 248951078Speter if (cflag & CCTS_OFLOW) { 249051078Speter com->state |= CS_CTS_OFLOW; 249151078Speter if (!(com->last_modem_status & MSR_CTS)) 249251078Speter com->state &= ~CS_ODEVREADY; 249351078Speter if (com->st16650a) { 249460471Snyan sio_setreg(com, com_cfcr, 0xbf); 249560471Snyan sio_setreg(com, com_fifo, 249660471Snyan sio_getreg(com, com_fifo) | 0x80); 249751078Speter } 249851078Speter } else { 249951078Speter if (com->st16650a) { 250060471Snyan sio_setreg(com, com_cfcr, 0xbf); 250160471Snyan sio_setreg(com, com_fifo, 250260471Snyan sio_getreg(com, com_fifo) & ~0x80); 250351078Speter } 250451078Speter } 250551078Speter 250660471Snyan sio_setreg(com, com_cfcr, com->cfcr_image); 250751078Speter 250851078Speter /* XXX shouldn't call functions while intrs are disabled. */ 250951078Speter disc_optim(tp, t, com); 251051078Speter /* 251151078Speter * Recover from fiddling with CS_TTGO. We used to call siointr1() 251251078Speter * unconditionally, but that defeated the careful discarding of 251351078Speter * stale input in sioopen(). 251451078Speter */ 251551078Speter if (com->state >= (CS_BUSY | CS_TTGO)) 251651078Speter siointr1(com); 251751078Speter 251872200Sbmilekic mtx_unlock_spin(&sio_lock); 251951078Speter splx(s); 252051078Speter comstart(tp); 252151078Speter if (com->ibufold != NULL) { 252251078Speter free(com->ibufold, M_DEVBUF); 252351078Speter com->ibufold = NULL; 252451078Speter } 252551078Speter return (0); 252651078Speter} 252751078Speter 252865605Sjhb/* 252970174Sjhb * This function must be called with the sio_lock mutex released and will 253070174Sjhb * return with it obtained. 253165605Sjhb */ 253251078Speterstatic int 253365605Sjhbsiosetwater(com, speed) 253451078Speter struct com_s *com; 253551078Speter speed_t speed; 253651078Speter{ 253751078Speter int cp4ticks; 253851078Speter u_char *ibuf; 253951078Speter int ibufsize; 254051078Speter struct tty *tp; 254151078Speter 254251078Speter /* 254351078Speter * Make the buffer size large enough to handle a softtty interrupt 254451078Speter * latency of about 2 ticks without loss of throughput or data 254551078Speter * (about 3 ticks if input flow control is not used or not honoured, 254651078Speter * but a bit less for CS5-CS7 modes). 254751078Speter */ 254851078Speter cp4ticks = speed / 10 / hz * 4; 254951078Speter for (ibufsize = 128; ibufsize < cp4ticks;) 255051078Speter ibufsize <<= 1; 255165605Sjhb if (ibufsize == com->ibufsize) { 255272200Sbmilekic mtx_lock_spin(&sio_lock); 255351078Speter return (0); 255465605Sjhb } 255551078Speter 255651078Speter /* 255751078Speter * Allocate input buffer. The extra factor of 2 in the size is 255851078Speter * to allow for an error byte for each input byte. 255951078Speter */ 256051078Speter ibuf = malloc(2 * ibufsize, M_DEVBUF, M_NOWAIT); 256165605Sjhb if (ibuf == NULL) { 256272200Sbmilekic mtx_lock_spin(&sio_lock); 256351078Speter return (ENOMEM); 256465605Sjhb } 256551078Speter 256651078Speter /* Initialize non-critical variables. */ 256751078Speter com->ibufold = com->ibuf; 256851078Speter com->ibufsize = ibufsize; 256951078Speter tp = com->tp; 257051078Speter if (tp != NULL) { 257151078Speter tp->t_ififosize = 2 * ibufsize; 257251078Speter tp->t_ispeedwat = (speed_t)-1; 257351078Speter tp->t_ospeedwat = (speed_t)-1; 257451078Speter } 257551078Speter 257651078Speter /* 257751078Speter * Read current input buffer, if any. Continue with interrupts 257851078Speter * disabled. 257951078Speter */ 258072200Sbmilekic mtx_lock_spin(&sio_lock); 258151078Speter if (com->iptr != com->ibuf) 258251078Speter sioinput(com); 258351078Speter 258451078Speter /*- 258551078Speter * Initialize critical variables, including input buffer watermarks. 258651078Speter * The external device is asked to stop sending when the buffer 258751078Speter * exactly reaches high water, or when the high level requests it. 258851078Speter * The high level is notified immediately (rather than at a later 258951078Speter * clock tick) when this watermark is reached. 259051078Speter * The buffer size is chosen so the watermark should almost never 259151078Speter * be reached. 259251078Speter * The low watermark is invisibly 0 since the buffer is always 259351078Speter * emptied all at once. 259451078Speter */ 259551078Speter com->iptr = com->ibuf = ibuf; 259651078Speter com->ibufend = ibuf + ibufsize; 259751078Speter com->ierroff = ibufsize; 259851078Speter com->ihighwater = ibuf + 3 * ibufsize / 4; 259951078Speter return (0); 260051078Speter} 260151078Speter 260251078Speterstatic void 260351078Spetercomstart(tp) 260451078Speter struct tty *tp; 260551078Speter{ 260651078Speter struct com_s *com; 260751078Speter int s; 260851078Speter int unit; 260951078Speter 261051078Speter unit = DEV_TO_UNIT(tp->t_dev); 261151078Speter com = com_addr(unit); 261257915Simp if (com == NULL) 261357915Simp return; 261451078Speter s = spltty(); 261572200Sbmilekic mtx_lock_spin(&sio_lock); 261651078Speter if (tp->t_state & TS_TTSTOP) 261751078Speter com->state &= ~CS_TTGO; 261851078Speter else 261951078Speter com->state |= CS_TTGO; 262051078Speter if (tp->t_state & TS_TBLOCK) { 262151078Speter if (com->mcr_image & MCR_RTS && com->state & CS_RTS_IFLOW) 262251078Speter outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS); 262351078Speter } else { 262451078Speter if (!(com->mcr_image & MCR_RTS) && com->iptr < com->ihighwater 262551078Speter && com->state & CS_RTS_IFLOW) 262651078Speter outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 262751078Speter } 262872200Sbmilekic mtx_unlock_spin(&sio_lock); 262951078Speter if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) { 263051078Speter ttwwakeup(tp); 263151078Speter splx(s); 263251078Speter return; 263351078Speter } 263451078Speter if (tp->t_outq.c_cc != 0) { 263551078Speter struct lbq *qp; 263651078Speter struct lbq *next; 263751078Speter 263851078Speter if (!com->obufs[0].l_queued) { 263951078Speter com->obufs[0].l_tail 264051078Speter = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1, 264151078Speter sizeof com->obuf1); 264251078Speter com->obufs[0].l_next = NULL; 264351078Speter com->obufs[0].l_queued = TRUE; 264472200Sbmilekic mtx_lock_spin(&sio_lock); 264551078Speter if (com->state & CS_BUSY) { 264651078Speter qp = com->obufq.l_next; 264751078Speter while ((next = qp->l_next) != NULL) 264851078Speter qp = next; 264951078Speter qp->l_next = &com->obufs[0]; 265051078Speter } else { 265151078Speter com->obufq.l_head = com->obufs[0].l_head; 265251078Speter com->obufq.l_tail = com->obufs[0].l_tail; 265351078Speter com->obufq.l_next = &com->obufs[0]; 265451078Speter com->state |= CS_BUSY; 265551078Speter } 265672200Sbmilekic mtx_unlock_spin(&sio_lock); 265751078Speter } 265851078Speter if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) { 265951078Speter com->obufs[1].l_tail 266051078Speter = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2, 266151078Speter sizeof com->obuf2); 266251078Speter com->obufs[1].l_next = NULL; 266351078Speter com->obufs[1].l_queued = TRUE; 266472200Sbmilekic mtx_lock_spin(&sio_lock); 266551078Speter if (com->state & CS_BUSY) { 266651078Speter qp = com->obufq.l_next; 266751078Speter while ((next = qp->l_next) != NULL) 266851078Speter qp = next; 266951078Speter qp->l_next = &com->obufs[1]; 267051078Speter } else { 267151078Speter com->obufq.l_head = com->obufs[1].l_head; 267251078Speter com->obufq.l_tail = com->obufs[1].l_tail; 267351078Speter com->obufq.l_next = &com->obufs[1]; 267451078Speter com->state |= CS_BUSY; 267551078Speter } 267672200Sbmilekic mtx_unlock_spin(&sio_lock); 267751078Speter } 267851078Speter tp->t_state |= TS_BUSY; 267951078Speter } 268072200Sbmilekic mtx_lock_spin(&sio_lock); 268151078Speter if (com->state >= (CS_BUSY | CS_TTGO)) 268251078Speter siointr1(com); /* fake interrupt to start output */ 268372200Sbmilekic mtx_unlock_spin(&sio_lock); 268451078Speter ttwwakeup(tp); 268551078Speter splx(s); 268651078Speter} 268751078Speter 268851078Speterstatic void 268951654Sphkcomstop(tp, rw) 269051078Speter struct tty *tp; 269151078Speter int rw; 269251078Speter{ 269351078Speter struct com_s *com; 269451078Speter 269551078Speter com = com_addr(DEV_TO_UNIT(tp->t_dev)); 269657915Simp if (com == NULL || com->gone) 269751078Speter return; 269872200Sbmilekic mtx_lock_spin(&sio_lock); 269951078Speter if (rw & FWRITE) { 270051078Speter if (com->hasfifo) 270151078Speter#ifdef COM_ESP 270251078Speter /* XXX avoid h/w bug. */ 270351078Speter if (!com->esp) 270451078Speter#endif 270560471Snyan sio_setreg(com, com_fifo, 270660471Snyan FIFO_XMT_RST | com->fifo_image); 270751078Speter com->obufs[0].l_queued = FALSE; 270851078Speter com->obufs[1].l_queued = FALSE; 270951078Speter if (com->state & CS_ODONE) 271051078Speter com_events -= LOTS_OF_EVENTS; 271151078Speter com->state &= ~(CS_ODONE | CS_BUSY); 271251078Speter com->tp->t_state &= ~TS_BUSY; 271351078Speter } 271451078Speter if (rw & FREAD) { 271551078Speter if (com->hasfifo) 271651078Speter#ifdef COM_ESP 271751078Speter /* XXX avoid h/w bug. */ 271851078Speter if (!com->esp) 271951078Speter#endif 272060471Snyan sio_setreg(com, com_fifo, 272160471Snyan FIFO_RCV_RST | com->fifo_image); 272251078Speter com_events -= (com->iptr - com->ibuf); 272351078Speter com->iptr = com->ibuf; 272451078Speter } 272572200Sbmilekic mtx_unlock_spin(&sio_lock); 272651078Speter comstart(tp); 272751078Speter} 272851078Speter 272951078Speterstatic int 273051078Spetercommctl(com, bits, how) 273151078Speter struct com_s *com; 273251078Speter int bits; 273351078Speter int how; 273451078Speter{ 273551078Speter int mcr; 273651078Speter int msr; 273751078Speter 273851078Speter if (how == DMGET) { 273951078Speter bits = TIOCM_LE; /* XXX - always enabled while open */ 274051078Speter mcr = com->mcr_image; 274151078Speter if (mcr & MCR_DTR) 274251078Speter bits |= TIOCM_DTR; 274351078Speter if (mcr & MCR_RTS) 274451078Speter bits |= TIOCM_RTS; 274551078Speter msr = com->prev_modem_status; 274651078Speter if (msr & MSR_CTS) 274751078Speter bits |= TIOCM_CTS; 274851078Speter if (msr & MSR_DCD) 274951078Speter bits |= TIOCM_CD; 275051078Speter if (msr & MSR_DSR) 275151078Speter bits |= TIOCM_DSR; 275251078Speter /* 275351078Speter * XXX - MSR_RI is naturally volatile, and we make MSR_TERI 275451078Speter * more volatile by reading the modem status a lot. Perhaps 275551078Speter * we should latch both bits until the status is read here. 275651078Speter */ 275751078Speter if (msr & (MSR_RI | MSR_TERI)) 275851078Speter bits |= TIOCM_RI; 275951078Speter return (bits); 276051078Speter } 276151078Speter mcr = 0; 276251078Speter if (bits & TIOCM_DTR) 276351078Speter mcr |= MCR_DTR; 276451078Speter if (bits & TIOCM_RTS) 276551078Speter mcr |= MCR_RTS; 276651078Speter if (com->gone) 276751078Speter return(0); 276872200Sbmilekic mtx_lock_spin(&sio_lock); 276951078Speter switch (how) { 277051078Speter case DMSET: 277151078Speter outb(com->modem_ctl_port, 277251078Speter com->mcr_image = mcr | (com->mcr_image & MCR_IENABLE)); 277351078Speter break; 277451078Speter case DMBIS: 277551078Speter outb(com->modem_ctl_port, com->mcr_image |= mcr); 277651078Speter break; 277751078Speter case DMBIC: 277851078Speter outb(com->modem_ctl_port, com->mcr_image &= ~mcr); 277951078Speter break; 278051078Speter } 278172200Sbmilekic mtx_unlock_spin(&sio_lock); 278251078Speter return (0); 278351078Speter} 278451078Speter 278551078Speterstatic void 278651078Spetersiosettimeout() 278751078Speter{ 278851078Speter struct com_s *com; 278951078Speter bool_t someopen; 279051078Speter int unit; 279151078Speter 279251078Speter /* 279351078Speter * Set our timeout period to 1 second if no polled devices are open. 279451078Speter * Otherwise set it to max(1/200, 1/hz). 279551078Speter * Enable timeouts iff some device is open. 279651078Speter */ 279751078Speter untimeout(comwakeup, (void *)NULL, sio_timeout_handle); 279851078Speter sio_timeout = hz; 279951078Speter someopen = FALSE; 280053344Speter for (unit = 0; unit < sio_numunits; ++unit) { 280151078Speter com = com_addr(unit); 280251078Speter if (com != NULL && com->tp != NULL 280351078Speter && com->tp->t_state & TS_ISOPEN && !com->gone) { 280451078Speter someopen = TRUE; 280551078Speter if (com->poll || com->poll_output) { 280651078Speter sio_timeout = hz > 200 ? hz / 200 : 1; 280751078Speter break; 280851078Speter } 280951078Speter } 281051078Speter } 281151078Speter if (someopen) { 281251078Speter sio_timeouts_until_log = hz / sio_timeout; 281351078Speter sio_timeout_handle = timeout(comwakeup, (void *)NULL, 281451078Speter sio_timeout); 281551078Speter } else { 281651078Speter /* Flush error messages, if any. */ 281751078Speter sio_timeouts_until_log = 1; 281851078Speter comwakeup((void *)NULL); 281951078Speter untimeout(comwakeup, (void *)NULL, sio_timeout_handle); 282051078Speter } 282151078Speter} 282251078Speter 282351078Speterstatic void 282451078Spetercomwakeup(chan) 282551078Speter void *chan; 282651078Speter{ 282751078Speter struct com_s *com; 282851078Speter int unit; 282951078Speter 283051078Speter sio_timeout_handle = timeout(comwakeup, (void *)NULL, sio_timeout); 283151078Speter 283251078Speter /* 283351078Speter * Recover from lost output interrupts. 283451078Speter * Poll any lines that don't use interrupts. 283551078Speter */ 283653344Speter for (unit = 0; unit < sio_numunits; ++unit) { 283751078Speter com = com_addr(unit); 283851078Speter if (com != NULL && !com->gone 283951078Speter && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) { 284072200Sbmilekic mtx_lock_spin(&sio_lock); 284151078Speter siointr1(com); 284272200Sbmilekic mtx_unlock_spin(&sio_lock); 284351078Speter } 284451078Speter } 284551078Speter 284651078Speter /* 284751078Speter * Check for and log errors, but not too often. 284851078Speter */ 284951078Speter if (--sio_timeouts_until_log > 0) 285051078Speter return; 285151078Speter sio_timeouts_until_log = hz / sio_timeout; 285253344Speter for (unit = 0; unit < sio_numunits; ++unit) { 285351078Speter int errnum; 285451078Speter 285551078Speter com = com_addr(unit); 285651078Speter if (com == NULL) 285751078Speter continue; 285851078Speter if (com->gone) 285951078Speter continue; 286051078Speter for (errnum = 0; errnum < CE_NTYPES; ++errnum) { 286151078Speter u_int delta; 286251078Speter u_long total; 286351078Speter 286472200Sbmilekic mtx_lock_spin(&sio_lock); 286551078Speter delta = com->delta_error_counts[errnum]; 286651078Speter com->delta_error_counts[errnum] = 0; 286772200Sbmilekic mtx_unlock_spin(&sio_lock); 286851078Speter if (delta == 0) 286951078Speter continue; 287051078Speter total = com->error_counts[errnum] += delta; 287151078Speter log(LOG_ERR, "sio%d: %u more %s%s (total %lu)\n", 287251078Speter unit, delta, error_desc[errnum], 287351078Speter delta == 1 ? "" : "s", total); 287451078Speter } 287551078Speter } 287651078Speter} 287751078Speter 287851078Speterstatic void 287951078Speterdisc_optim(tp, t, com) 288051078Speter struct tty *tp; 288151078Speter struct termios *t; 288251078Speter struct com_s *com; 288351078Speter{ 288451078Speter if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON)) 288551078Speter && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK)) 288651078Speter && (!(t->c_iflag & PARMRK) 288751078Speter || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK)) 288851078Speter && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN)) 288951078Speter && linesw[tp->t_line].l_rint == ttyinput) 289051078Speter tp->t_state |= TS_CAN_BYPASS_L_RINT; 289151078Speter else 289251078Speter tp->t_state &= ~TS_CAN_BYPASS_L_RINT; 289351078Speter com->hotchar = linesw[tp->t_line].l_hotchar; 289451078Speter} 289551078Speter 289651078Speter/* 289751078Speter * Following are all routines needed for SIO to act as console 289851078Speter */ 289951078Speter#include <sys/cons.h> 290051078Speter 290151078Speterstruct siocnstate { 290251078Speter u_char dlbl; 290351078Speter u_char dlbh; 290451078Speter u_char ier; 290551078Speter u_char cfcr; 290651078Speter u_char mcr; 290751078Speter}; 290851078Speter 290966230Sjhb#ifndef __alpha__ 291051078Speterstatic speed_t siocngetspeed __P((Port_t, struct speedtab *)); 291166230Sjhb#endif 291251078Speterstatic void siocnclose __P((struct siocnstate *sp, Port_t iobase)); 291351078Speterstatic void siocnopen __P((struct siocnstate *sp, Port_t iobase, int speed)); 291451078Speterstatic void siocntxwait __P((Port_t iobase)); 291551078Speter 291666230Sjhb#ifdef __alpha__ 291766230Sjhbint siocnattach __P((int port, int speed)); 291866230Sjhbint siogdbattach __P((int port, int speed)); 291966230Sjhbint siogdbgetc __P((void)); 292066230Sjhbvoid siogdbputc __P((int c)); 292166230Sjhb#else 292251078Speterstatic cn_probe_t siocnprobe; 292351078Speterstatic cn_init_t siocninit; 292466230Sjhb#endif 292551078Speterstatic cn_checkc_t siocncheckc; 292651078Speterstatic cn_getc_t siocngetc; 292751078Speterstatic cn_putc_t siocnputc; 292851078Speter 292951078Speter#ifdef __i386__ 293055823SyokotaCONS_DRIVER(sio, siocnprobe, siocninit, NULL, siocngetc, siocncheckc, 293155823Syokota siocnputc, NULL); 293251078Speter#endif 293351078Speter 293451078Speter/* To get the GDB related variables */ 293551078Speter#if DDB > 0 293651078Speter#include <ddb/ddb.h> 293751078Speter#endif 293851078Speter 293951078Speterstatic void 294051078Spetersiocntxwait(iobase) 294151078Speter Port_t iobase; 294251078Speter{ 294351078Speter int timo; 294451078Speter 294551078Speter /* 294651078Speter * Wait for any pending transmission to finish. Required to avoid 294751078Speter * the UART lockup bug when the speed is changed, and for normal 294851078Speter * transmits. 294951078Speter */ 295051078Speter timo = 100000; 295151078Speter while ((inb(iobase + com_lsr) & (LSR_TSRE | LSR_TXRDY)) 295251078Speter != (LSR_TSRE | LSR_TXRDY) && --timo != 0) 295351078Speter ; 295451078Speter} 295551078Speter 295666230Sjhb#ifndef __alpha__ 295766230Sjhb 295851078Speter/* 295951078Speter * Read the serial port specified and try to figure out what speed 296051078Speter * it's currently running at. We're assuming the serial port has 296151078Speter * been initialized and is basicly idle. This routine is only intended 296251078Speter * to be run at system startup. 296351078Speter * 296451078Speter * If the value read from the serial port doesn't make sense, return 0. 296551078Speter */ 296651078Speter 296751078Speterstatic speed_t 296851078Spetersiocngetspeed(iobase, table) 296951078Speter Port_t iobase; 297051078Speter struct speedtab *table; 297151078Speter{ 297251078Speter int code; 297351078Speter u_char dlbh; 297451078Speter u_char dlbl; 297551078Speter u_char cfcr; 297651078Speter 297751078Speter cfcr = inb(iobase + com_cfcr); 297851078Speter outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 297951078Speter 298051078Speter dlbl = inb(iobase + com_dlbl); 298151078Speter dlbh = inb(iobase + com_dlbh); 298251078Speter 298351078Speter outb(iobase + com_cfcr, cfcr); 298451078Speter 298551078Speter code = dlbh << 8 | dlbl; 298651078Speter 298753344Speter for (; table->sp_speed != -1; table++) 298851078Speter if (table->sp_code == code) 298951078Speter return (table->sp_speed); 299051078Speter 299158885Simp return (0); /* didn't match anything sane */ 299251078Speter} 299351078Speter 299466230Sjhb#endif 299566230Sjhb 299651078Speterstatic void 299751078Spetersiocnopen(sp, iobase, speed) 299851078Speter struct siocnstate *sp; 299951078Speter Port_t iobase; 300051078Speter int speed; 300151078Speter{ 300251078Speter int divisor; 300351078Speter u_char dlbh; 300451078Speter u_char dlbl; 300551078Speter 300651078Speter /* 300751078Speter * Save all the device control registers except the fifo register 300851078Speter * and set our default ones (cs8 -parenb speed=comdefaultrate). 300951078Speter * We can't save the fifo register since it is read-only. 301051078Speter */ 301151078Speter sp->ier = inb(iobase + com_ier); 301251078Speter outb(iobase + com_ier, 0); /* spltty() doesn't stop siointr() */ 301351078Speter siocntxwait(iobase); 301451078Speter sp->cfcr = inb(iobase + com_cfcr); 301551078Speter outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 301651078Speter sp->dlbl = inb(iobase + com_dlbl); 301751078Speter sp->dlbh = inb(iobase + com_dlbh); 301851078Speter /* 301951078Speter * Only set the divisor registers if they would change, since on 302051078Speter * some 16550 incompatibles (Startech), setting them clears the 302151078Speter * data input register. This also reduces the effects of the 302251078Speter * UMC8669F bug. 302351078Speter */ 302451078Speter divisor = ttspeedtab(speed, comspeedtab); 302551078Speter dlbl = divisor & 0xFF; 302651078Speter if (sp->dlbl != dlbl) 302751078Speter outb(iobase + com_dlbl, dlbl); 302851078Speter dlbh = (u_int) divisor >> 8; 302951078Speter if (sp->dlbh != dlbh) 303051078Speter outb(iobase + com_dlbh, dlbh); 303151078Speter outb(iobase + com_cfcr, CFCR_8BITS); 303251078Speter sp->mcr = inb(iobase + com_mcr); 303351078Speter /* 303451078Speter * We don't want interrupts, but must be careful not to "disable" 303551078Speter * them by clearing the MCR_IENABLE bit, since that might cause 303651078Speter * an interrupt by floating the IRQ line. 303751078Speter */ 303851078Speter outb(iobase + com_mcr, (sp->mcr & MCR_IENABLE) | MCR_DTR | MCR_RTS); 303951078Speter} 304051078Speter 304151078Speterstatic void 304251078Spetersiocnclose(sp, iobase) 304351078Speter struct siocnstate *sp; 304451078Speter Port_t iobase; 304551078Speter{ 304651078Speter /* 304751078Speter * Restore the device control registers. 304851078Speter */ 304951078Speter siocntxwait(iobase); 305051078Speter outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 305151078Speter if (sp->dlbl != inb(iobase + com_dlbl)) 305251078Speter outb(iobase + com_dlbl, sp->dlbl); 305351078Speter if (sp->dlbh != inb(iobase + com_dlbh)) 305451078Speter outb(iobase + com_dlbh, sp->dlbh); 305551078Speter outb(iobase + com_cfcr, sp->cfcr); 305651078Speter /* 305751078Speter * XXX damp oscillations of MCR_DTR and MCR_RTS by not restoring them. 305851078Speter */ 305951078Speter outb(iobase + com_mcr, sp->mcr | MCR_DTR | MCR_RTS); 306051078Speter outb(iobase + com_ier, sp->ier); 306151078Speter} 306251078Speter 306366230Sjhb#ifndef __alpha__ 306466230Sjhb 306551078Speterstatic void 306651078Spetersiocnprobe(cp) 306751078Speter struct consdev *cp; 306851078Speter{ 306951078Speter speed_t boot_speed; 307051078Speter u_char cfcr; 307151078Speter int s, unit; 307251078Speter struct siocnstate sp; 307351078Speter 307451078Speter /* 307551078Speter * Find our first enabled console, if any. If it is a high-level 307651078Speter * console device, then initialize it and return successfully. 307751078Speter * If it is a low-level console device, then initialize it and 307851078Speter * return unsuccessfully. It must be initialized in both cases 307951078Speter * for early use by console drivers and debuggers. Initializing 308051078Speter * the hardware is not necessary in all cases, since the i/o 308151078Speter * routines initialize it on the fly, but it is necessary if 308251078Speter * input might arrive while the hardware is switched back to an 308351078Speter * uninitialized state. We can't handle multiple console devices 308451078Speter * yet because our low-level routines don't take a device arg. 308551078Speter * We trust the user to set the console flags properly so that we 308651078Speter * don't need to probe. 308751078Speter */ 308851078Speter cp->cn_pri = CN_DEAD; 308951078Speter 309051078Speter for (unit = 0; unit < 16; unit++) { /* XXX need to know how many */ 309151078Speter int flags; 309251078Speter int disabled; 309351078Speter if (resource_int_value("sio", unit, "disabled", &disabled) == 0) { 309451078Speter if (disabled) 309551078Speter continue; 309651078Speter } 309751078Speter if (resource_int_value("sio", unit, "flags", &flags)) 309851078Speter continue; 309951078Speter if (COM_CONSOLE(flags) || COM_DEBUGGER(flags)) { 310051078Speter int port; 310151078Speter Port_t iobase; 310251078Speter 310351078Speter if (resource_int_value("sio", unit, "port", &port)) 310451078Speter continue; 310551078Speter iobase = port; 310651078Speter s = spltty(); 310751078Speter if (boothowto & RB_SERIAL) { 310851078Speter boot_speed = siocngetspeed(iobase, comspeedtab); 310951078Speter if (boot_speed) 311051078Speter comdefaultrate = boot_speed; 311151078Speter } 311251078Speter 311351078Speter /* 311451078Speter * Initialize the divisor latch. We can't rely on 311551078Speter * siocnopen() to do this the first time, since it 311651078Speter * avoids writing to the latch if the latch appears 311751078Speter * to have the correct value. Also, if we didn't 311851078Speter * just read the speed from the hardware, then we 311951078Speter * need to set the speed in hardware so that 312051078Speter * switching it later is null. 312151078Speter */ 312251078Speter cfcr = inb(iobase + com_cfcr); 312351078Speter outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 312451078Speter outb(iobase + com_dlbl, 312551078Speter COMBRD(comdefaultrate) & 0xff); 312651078Speter outb(iobase + com_dlbh, 312751078Speter (u_int) COMBRD(comdefaultrate) >> 8); 312851078Speter outb(iobase + com_cfcr, cfcr); 312951078Speter 313051078Speter siocnopen(&sp, iobase, comdefaultrate); 313151078Speter 313251078Speter splx(s); 313351078Speter if (COM_CONSOLE(flags) && !COM_LLCONSOLE(flags)) { 313451078Speter cp->cn_dev = makedev(CDEV_MAJOR, unit); 313551078Speter cp->cn_pri = COM_FORCECONSOLE(flags) 313651078Speter || boothowto & RB_SERIAL 313751078Speter ? CN_REMOTE : CN_NORMAL; 313851078Speter siocniobase = iobase; 313951078Speter siocnunit = unit; 314051078Speter } 314151078Speter if (COM_DEBUGGER(flags)) { 314251078Speter printf("sio%d: gdb debugging port\n", unit); 314351078Speter siogdbiobase = iobase; 314451078Speter siogdbunit = unit; 314551078Speter#if DDB > 0 314651078Speter gdbdev = makedev(CDEV_MAJOR, unit); 314751078Speter gdb_getc = siocngetc; 314851078Speter gdb_putc = siocnputc; 314951078Speter#endif 315051078Speter } 315151078Speter } 315251078Speter } 315351078Speter#ifdef __i386__ 315451078Speter#if DDB > 0 315551078Speter /* 315651078Speter * XXX Ugly Compatability. 315751078Speter * If no gdb port has been specified, set it to be the console 315851078Speter * as some configuration files don't specify the gdb port. 315951078Speter */ 316051078Speter if (gdbdev == NODEV && (boothowto & RB_GDB)) { 316151078Speter printf("Warning: no GDB port specified. Defaulting to sio%d.\n", 316251078Speter siocnunit); 316351078Speter printf("Set flag 0x80 on desired GDB port in your\n"); 316451078Speter printf("configuration file (currently sio only).\n"); 316551078Speter siogdbiobase = siocniobase; 316651078Speter siogdbunit = siocnunit; 316751078Speter gdbdev = makedev(CDEV_MAJOR, siocnunit); 316851078Speter gdb_getc = siocngetc; 316951078Speter gdb_putc = siocnputc; 317051078Speter } 317151078Speter#endif 317251078Speter#endif 317351078Speter} 317451078Speter 317566230Sjhbstatic void 317666230Sjhbsiocninit(cp) 317766230Sjhb struct consdev *cp; 317866230Sjhb{ 317966230Sjhb comconsole = DEV_TO_UNIT(cp->cn_dev); 318066230Sjhb} 318166230Sjhb 318266230Sjhb#endif 318366230Sjhb 318451078Speter#ifdef __alpha__ 318551078Speter 318655868SgallatinCONS_DRIVER(sio, NULL, NULL, NULL, siocngetc, siocncheckc, siocnputc, NULL); 318751078Speter 318851078Speterint 318951078Spetersiocnattach(port, speed) 319051078Speter int port; 319151078Speter int speed; 319251078Speter{ 319351078Speter int s; 319451078Speter u_char cfcr; 319551078Speter struct siocnstate sp; 319651078Speter 319751078Speter siocniobase = port; 319851078Speter comdefaultrate = speed; 319951078Speter sio_consdev.cn_pri = CN_NORMAL; 320051078Speter sio_consdev.cn_dev = makedev(CDEV_MAJOR, 0); 320151078Speter 320251078Speter s = spltty(); 320351078Speter 320451078Speter /* 320551078Speter * Initialize the divisor latch. We can't rely on 320651078Speter * siocnopen() to do this the first time, since it 320751078Speter * avoids writing to the latch if the latch appears 320851078Speter * to have the correct value. Also, if we didn't 320951078Speter * just read the speed from the hardware, then we 321051078Speter * need to set the speed in hardware so that 321151078Speter * switching it later is null. 321251078Speter */ 321351078Speter cfcr = inb(siocniobase + com_cfcr); 321451078Speter outb(siocniobase + com_cfcr, CFCR_DLAB | cfcr); 321551078Speter outb(siocniobase + com_dlbl, 321651078Speter COMBRD(comdefaultrate) & 0xff); 321751078Speter outb(siocniobase + com_dlbh, 321851078Speter (u_int) COMBRD(comdefaultrate) >> 8); 321951078Speter outb(siocniobase + com_cfcr, cfcr); 322051078Speter 322151078Speter siocnopen(&sp, siocniobase, comdefaultrate); 322251078Speter splx(s); 322351078Speter 322451078Speter cn_tab = &sio_consdev; 322558885Simp return (0); 322651078Speter} 322751078Speter 322851078Speterint 322951078Spetersiogdbattach(port, speed) 323051078Speter int port; 323151078Speter int speed; 323251078Speter{ 323351078Speter int s; 323451078Speter u_char cfcr; 323551078Speter struct siocnstate sp; 323665714Sjhb int unit = 1; /* XXX !!! */ 323751078Speter 323851078Speter siogdbiobase = port; 323951078Speter gdbdefaultrate = speed; 324051078Speter 324165714Sjhb printf("sio%d: gdb debugging port\n", unit); 324265714Sjhb siogdbunit = unit; 324365714Sjhb#if DDB > 0 324465714Sjhb gdbdev = makedev(CDEV_MAJOR, unit); 324565714Sjhb gdb_getc = siocngetc; 324665714Sjhb gdb_putc = siocnputc; 324765714Sjhb#endif 324865714Sjhb 324951078Speter s = spltty(); 325051078Speter 325151078Speter /* 325251078Speter * Initialize the divisor latch. We can't rely on 325351078Speter * siocnopen() to do this the first time, since it 325451078Speter * avoids writing to the latch if the latch appears 325551078Speter * to have the correct value. Also, if we didn't 325651078Speter * just read the speed from the hardware, then we 325751078Speter * need to set the speed in hardware so that 325851078Speter * switching it later is null. 325951078Speter */ 326051078Speter cfcr = inb(siogdbiobase + com_cfcr); 326151078Speter outb(siogdbiobase + com_cfcr, CFCR_DLAB | cfcr); 326251078Speter outb(siogdbiobase + com_dlbl, 326351078Speter COMBRD(gdbdefaultrate) & 0xff); 326451078Speter outb(siogdbiobase + com_dlbh, 326551078Speter (u_int) COMBRD(gdbdefaultrate) >> 8); 326651078Speter outb(siogdbiobase + com_cfcr, cfcr); 326751078Speter 326851078Speter siocnopen(&sp, siogdbiobase, gdbdefaultrate); 326951078Speter splx(s); 327051078Speter 327158885Simp return (0); 327251078Speter} 327351078Speter 327451078Speter#endif 327551078Speter 327651078Speterstatic int 327751078Spetersiocncheckc(dev) 327851078Speter dev_t dev; 327951078Speter{ 328051078Speter int c; 328151078Speter Port_t iobase; 328251078Speter int s; 328351078Speter struct siocnstate sp; 328451078Speter 328551078Speter if (minor(dev) == siogdbunit) 328651078Speter iobase = siogdbiobase; 328751078Speter else 328851078Speter iobase = siocniobase; 328951078Speter s = spltty(); 329051078Speter siocnopen(&sp, iobase, comdefaultrate); 329151078Speter if (inb(iobase + com_lsr) & LSR_RXRDY) 329251078Speter c = inb(iobase + com_data); 329351078Speter else 329451078Speter c = -1; 329551078Speter siocnclose(&sp, iobase); 329651078Speter splx(s); 329751078Speter return (c); 329851078Speter} 329951078Speter 330051078Speter 330151078Speterint 330251078Spetersiocngetc(dev) 330351078Speter dev_t dev; 330451078Speter{ 330551078Speter int c; 330651078Speter Port_t iobase; 330751078Speter int s; 330851078Speter struct siocnstate sp; 330951078Speter 331051078Speter if (minor(dev) == siogdbunit) 331151078Speter iobase = siogdbiobase; 331251078Speter else 331351078Speter iobase = siocniobase; 331451078Speter s = spltty(); 331551078Speter siocnopen(&sp, iobase, comdefaultrate); 331651078Speter while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 331751078Speter ; 331851078Speter c = inb(iobase + com_data); 331951078Speter siocnclose(&sp, iobase); 332051078Speter splx(s); 332151078Speter return (c); 332251078Speter} 332351078Speter 332451078Spetervoid 332551078Spetersiocnputc(dev, c) 332651078Speter dev_t dev; 332751078Speter int c; 332851078Speter{ 332951078Speter int s; 333051078Speter struct siocnstate sp; 333151078Speter Port_t iobase; 333251078Speter 333351078Speter if (minor(dev) == siogdbunit) 333451078Speter iobase = siogdbiobase; 333551078Speter else 333651078Speter iobase = siocniobase; 333751078Speter s = spltty(); 333851078Speter siocnopen(&sp, iobase, comdefaultrate); 333951078Speter siocntxwait(iobase); 334051078Speter outb(iobase + com_data, c); 334151078Speter siocnclose(&sp, iobase); 334251078Speter splx(s); 334351078Speter} 334451078Speter 334551078Speter#ifdef __alpha__ 334651078Speterint 334751078Spetersiogdbgetc() 334851078Speter{ 334951078Speter int c; 335051078Speter Port_t iobase; 335151078Speter int s; 335251078Speter struct siocnstate sp; 335351078Speter 335451078Speter iobase = siogdbiobase; 335551078Speter s = spltty(); 335651078Speter siocnopen(&sp, iobase, gdbdefaultrate); 335751078Speter while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 335851078Speter ; 335951078Speter c = inb(iobase + com_data); 336051078Speter siocnclose(&sp, iobase); 336151078Speter splx(s); 336251078Speter return (c); 336351078Speter} 336451078Speter 336551078Spetervoid 336651078Spetersiogdbputc(c) 336751078Speter int c; 336851078Speter{ 336951078Speter int s; 337051078Speter struct siocnstate sp; 337151078Speter 337251078Speter s = spltty(); 337351078Speter siocnopen(&sp, siogdbiobase, gdbdefaultrate); 337451078Speter siocntxwait(siogdbiobase); 337551078Speter outb(siogdbiobase + com_data, c); 337651078Speter siocnclose(&sp, siogdbiobase); 337751078Speter splx(s); 337851078Speter} 337951078Speter#endif 338051078Speter 338152471SimpDRIVER_MODULE(sio, isa, sio_isa_driver, sio_devclass, 0, 0); 338282555SmsmithDRIVER_MODULE(sio, acpi, sio_isa_driver, sio_devclass, 0, 0); 338352471Simp#if NCARD > 0 338452471SimpDRIVER_MODULE(sio, pccard, sio_pccard_driver, sio_devclass, 0, 0); 338552471Simp#endif 338658885Simp#if NPCI > 0 338758885SimpDRIVER_MODULE(sio, pci, sio_pci_driver, sio_devclass, 0, 0); 338877750SimpDRIVER_MODULE(sio, cardbus, sio_pci_driver, sio_devclass, 0, 0); 338958885Simp#endif 3390