2 * Copyright (c) 1991 The Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * from: @(#)com.c 7.5 (Berkeley) 5/16/91
34 * $Id: sio.c,v 1.85 1999/04/03 15:51:14 kato Exp $
37 #include "opt_comconsole.h"
38 #include "opt_compat.h"
40 #include "opt_devfs.h"
41 /* #include "opt_sio.h" */
43 /* #include "pnp.h" */
47 * Serial driver, based on 386BSD-0.1 com driver.
48 * Mostly rewritten to use pseudo-DMA.
49 * Works for National Semiconductor NS8250-NS16550AF UARTs.
50 * COM driver, based on HP dca driver.
52 * Changes for PC-Card integration:
53 * - Added PC-Card driver table and handlers
55 /*===============================================================
56 * 386BSD(98),FreeBSD-1.1x(98) com driver.
58 * modified for PC9801 by M.Ishii
59 * Kyoto University Microcomputer Club (KMC)
60 * Chou "TEFUTEFU" Hirotomi
61 * Kyoto Univ. the faculty of medicine
62 *===============================================================
63 * FreeBSD-2.0.1(98) sio driver.
65 * modified for pc98 Internal i8251 and MICRO CORE MC16550II
66 * T.Koike(hfc01340@niftyserve.or.jp)
67 * implement kernel device configuration
68 * aizu@orient.center.nitech.ac.jp
72 * PC98 localization based on 386BSD(98) com driver. Using its PC98 local
74 * This driver is under debugging,has bugs.
77 * options COM_MULTIPORT #if using MC16550II
78 * device sio0 at nec? port 0x30 tty irq 4 #internal
79 * device sio1 at nec? port 0xd2 tty irq 5 flags 0x101 #mc1
80 * device sio2 at nec? port 0x8d2 tty flags 0x101 #mc2
81 * # ~~~~~iobase ~~multi port flag
82 * # ~ master device is sio1
84 * cd /dev; MAKEDEV ttyd0 ttyd1 ..
86 * 57600bps is too fast for sio0(internal8251)
88 * #set default speed 9600
91 * stty </dev/ttyid$i crtscts 9600
92 * : # ~~~~ default speed(can change after init.)
96 * 5) PC9861K,PIO9032B,B98_01
100 * modified for AIWA B98-01
101 * by T.Hatanou <hatanou@yasuda.comm.waseda.ac.jp> last update: 15 Sep.1995
103 * How to configure...
104 * # options COM_MULTIPORT # support for MICROCORE MC16550II
105 * ... comment-out this line, which will conflict with B98_01.
106 * options "B98_01" # support for AIWA B98-01
107 * device sio1 at nec? port 0x00d1 tty irq ?
108 * device sio2 at nec? port 0x00d5 tty irq ?
109 * ... you can leave these lines `irq ?', irq will be autodetected.
112 * Modified by Y.Takahashi of Kogakuin University.
116 #define COM_IF_INTERNAL 0x00
117 #define COM_IF_PC9861K_1 0x01
118 #define COM_IF_PC9861K_2 0x02
119 #define COM_IF_IND_SS_1 0x03
120 #define COM_IF_IND_SS_2 0x04
121 #define COM_IF_PIO9032B_1 0x05
122 #define COM_IF_PIO9032B_2 0x06
123 #define COM_IF_B98_01_1 0x07
124 #define COM_IF_B98_01_2 0x08
125 #define COM_IF_END1 COM_IF_B98_01_2
126 #define COM_IF_RSA98 0x10 /* same as COM_IF_NS16550 */
127 #define COM_IF_NS16550 0x11
128 #define COM_IF_SECOND_CCU 0x12 /* same as COM_IF_NS16550 */
129 #define COM_IF_MC16550II 0x13
130 #define COM_IF_MCRS98 0x14 /* same as COM_IF_MC16550II */
131 #define COM_IF_RSB3000 0x15
132 #define COM_IF_RSB384 0x16
133 #define COM_IF_MODEM_CARD 0x17 /* same as COM_IF_NS16550 */
134 #define COM_IF_RSA98III 0x18
135 #define COM_IF_ESP98 0x19
136 #define COM_IF_END2 COM_IF_ESP98
139 #include <sys/param.h>
140 #include <sys/systm.h>
141 #include <sys/reboot.h>
142 #include <sys/malloc.h>
144 #include <sys/proc.h>
145 #include <sys/module.h>
146 #include <sys/conf.h>
147 #include <sys/dkstat.h>
148 #include <sys/fcntl.h>
149 #include <sys/interrupt.h>
150 #include <sys/kernel.h>
151 #include <sys/syslog.h>
152 #include <sys/sysctl.h>
154 #include <machine/bus.h>
155 #include <sys/rman.h>
157 #include <sys/devfsext.h>
159 #include <sys/timepps.h>
162 #include <pc98/pc98/pc98.h>
163 #include <pc98/pc98/pc98_machdep.h>
164 #include <i386/isa/ic/i8251.h>
166 #include <isa/isareg.h>
168 #include <isa/isavar.h>
169 #include <machine/lock.h>
171 #include <machine/clock.h>
172 #include <machine/ipl.h>
174 #include <machine/lock.h>
176 #include <machine/resource.h>
178 #include <isa/sioreg.h>
181 #include <i386/isa/ic/esp.h>
183 #include <i386/isa/ic/ns16550.h>
185 #include <i386/isa/ic/rsa.h>
192 #include <sys/module.h>
193 #include <pccard/cardinfo.h>
194 #include <pccard/slot.h>
198 #include <i386/isa/pnp.h>
204 #define disable_intr() 0
205 #define enable_intr() 0
209 #define disable_intr() COM_DISABLE_INTR()
210 #define enable_intr() COM_ENABLE_INTR()
215 #define EXTRA_SIO MAX_PNP_CARDS
221 #define NSIOTOT (NSIO + EXTRA_SIO)
223 #define LOTS_OF_EVENTS 64 /* helps separate urgent events from input */
225 #define CALLOUT_MASK 0x80
226 #define CONTROL_MASK 0x60
227 #define CONTROL_INIT_STATE 0x20
228 #define CONTROL_LOCK_STATE 0x40
229 #define DEV_TO_UNIT(dev) (MINOR_TO_UNIT(minor(dev)))
230 #define MINOR_MAGIC_MASK (CALLOUT_MASK | CONTROL_MASK)
231 #define MINOR_TO_UNIT(mynor) ((mynor) & ~MINOR_MAGIC_MASK)
234 /* checks in flags for multiport and which is multiport "master chip"
237 #define COM_ISMULTIPORT(flags) ((flags) & 0x01)
238 #define COM_MPMASTER(flags) (((flags) >> 8) & 0x0ff)
239 #define COM_NOTAST4(flags) ((flags) & 0x04)
240 #endif /* COM_MULTIPORT */
242 #define COM_CONSOLE(flags) ((flags) & 0x10)
243 #define COM_FORCECONSOLE(flags) ((flags) & 0x20)
244 #define COM_LLCONSOLE(flags) ((flags) & 0x40)
245 #define COM_LOSESOUTINTS(flags) ((flags) & 0x08)
246 #define COM_NOFIFO(flags) ((flags) & 0x02)
247 #define COM_ST16650A(flags) ((flags) & 0x20000)
248 #define COM_C_NOPROBE (0x40000)
249 #define COM_NOPROBE(flags) ((flags) & COM_C_NOPROBE)
250 #define COM_C_IIR_TXRDYBUG (0x80000)
251 #define COM_IIR_TXRDYBUG(flags) ((flags) & COM_C_IIR_TXRDYBUG)
252 #define COM_FIFOSIZE(flags) (((flags) & 0xff000000) >> 24)
255 #define com_emr com_msr /* Extension mode register for RSB-2000/3000 */
257 #define com_scr 7 /* scratch register for 16450-16550 (R/W) */
262 * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher
263 * than the other bits so that they can be tested as a group without masking
266 * The following com and tty flags correspond closely:
267 * CS_BUSY = TS_BUSY (maintained by comstart(), siopoll() and
269 * CS_TTGO = ~TS_TTSTOP (maintained by comparam() and comstart())
270 * CS_CTS_OFLOW = CCTS_OFLOW (maintained by comparam())
271 * CS_RTS_IFLOW = CRTS_IFLOW (maintained by comparam())
272 * TS_FLUSH is not used.
273 * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON.
274 * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state).
276 #define CS_BUSY 0x80 /* output in progress */
277 #define CS_TTGO 0x40 /* output not stopped by XOFF */
278 #define CS_ODEVREADY 0x20 /* external device h/w ready (CTS) */
279 #define CS_CHECKMSR 1 /* check of MSR scheduled */
280 #define CS_CTS_OFLOW 2 /* use CTS output flow control */
281 #define CS_DTR_OFF 0x10 /* DTR held off */
282 #define CS_ODONE 4 /* output completed */
283 #define CS_RTS_IFLOW 8 /* use RTS input flow control */
284 #define CSE_BUSYCHECK 1 /* siobusycheck() scheduled */
286 static char const * const error_desc[] = {
289 #define CE_INTERRUPT_BUF_OVERFLOW 1
290 "interrupt-level buffer overflow",
291 #define CE_TTY_BUF_OVERFLOW 2
292 "tty-level buffer overflow",
296 #define CE_RECORD(com, errnum) (++(com)->delta_error_counts[errnum])
298 /* types. XXX - should be elsewhere */
299 typedef u_int Port_t; /* hardware port */
300 typedef u_char bool_t; /* boolean */
302 /* queue of linear buffers */
304 u_char *l_head; /* next char to process */
305 u_char *l_tail; /* one past the last char to process */
306 struct lbq *l_next; /* next in queue */
307 bool_t l_queued; /* nonzero if queued */
310 /* com device structure */
312 u_int flags; /* Copy isa device flags */
313 u_char state; /* miscellaneous flag bits */
314 bool_t active_out; /* nonzero if the callout device is open */
315 u_char cfcr_image; /* copy of value written to CFCR */
317 bool_t esp; /* is this unit a hayes esp board? */
319 u_char extra_state; /* more flag bits, separate for order trick */
320 u_char fifo_image; /* copy of value written to FIFO */
321 bool_t hasfifo; /* nonzero for 16550 UARTs */
322 bool_t st16650a; /* Is a Startech 16650A or RTS/CTS compat */
323 bool_t loses_outints; /* nonzero if device loses output interrupts */
324 u_char mcr_image; /* copy of value written to MCR */
326 bool_t multiport; /* is this unit part of a multiport device? */
327 #endif /* COM_MULTIPORT */
328 bool_t no_irq; /* nonzero if irq is not attached */
329 bool_t gone; /* hardware disappeared */
330 bool_t poll; /* nonzero if polling is required */
331 bool_t poll_output; /* nonzero if polling for output is required */
332 int unit; /* unit number */
333 int dtr_wait; /* time to hold DTR down on close (* 1/hz) */
335 u_int wopeners; /* # processes waiting for DCD in open() */
338 * The high level of the driver never reads status registers directly
339 * because there would be too many side effects to handle conveniently.
340 * Instead, it reads copies of the registers stored here by the
343 u_char last_modem_status; /* last MSR read by intr handler */
344 u_char prev_modem_status; /* last MSR handled by high level */
346 u_char hotchar; /* ldisc-specific char to be handled ASAP */
347 u_char *ibuf; /* start of input buffer */
348 u_char *ibufend; /* end of input buffer */
349 u_char *ibufold; /* old input buffer, to be freed */
350 u_char *ihighwater; /* threshold in input buffer */
351 u_char *iptr; /* next free spot in input buffer */
352 int ibufsize; /* size of ibuf (not include error bytes) */
353 int ierroff; /* offset of error bytes in ibuf */
355 struct lbq obufq; /* head of queue of output buffers */
356 struct lbq obufs[2]; /* output buffers */
361 Port_t in_modem_port;
362 Port_t intr_ctrl_port;
364 int pc98_prev_modem_status;
365 int pc98_modem_delta;
366 int modem_car_chg_timer;
367 int pc98_prev_siocmd;
368 int pc98_prev_siomod;
372 Port_t data_port; /* i/o ports */
379 Port_t rsabase; /* iobase address of a I/O-DATA RSA board */
381 Port_t modem_ctl_port;
382 Port_t line_status_port;
383 Port_t modem_status_port;
384 Port_t intr_ctl_port; /* Ports of IIR register */
386 struct tty *tp; /* cross reference */
389 struct termios it_in; /* should be in struct tty */
390 struct termios it_out;
393 struct termios lt_in; /* should be in struct tty */
394 struct termios lt_out;
397 bool_t do_dcd_timestamp;
398 struct timeval timestamp;
399 struct timeval dcd_timestamp;
400 struct pps_state pps;
402 u_long bytes_in; /* statistics */
404 u_int delta_error_counts[CE_NTYPES];
405 u_long error_counts[CE_NTYPES];
408 * Data area for output buffers. Someday we should build the output
409 * buffer queue without copying data.
420 void *devfs_token_ttyd;
421 void *devfs_token_ttyl;
422 void *devfs_token_ttyi;
423 void *devfs_token_cuaa;
424 void *devfs_token_cual;
425 void *devfs_token_cuai;
430 static int espattach __P((struct isa_device *isdp, struct com_s *com,
433 static int sioattach __P((device_t dev));
435 static timeout_t siobusycheck;
436 static timeout_t siodtrwakeup;
437 static void comhardclose __P((struct com_s *com));
438 static void sioinput __P((struct com_s *com));
439 static void siointr1 __P((struct com_s *com));
440 static void siointr __P((void *arg));
441 static int commctl __P((struct com_s *com, int bits, int how));
442 static int comparam __P((struct tty *tp, struct termios *t));
443 static swihand_t siopoll;
444 static int sioprobe __P((device_t dev));
445 static void siosettimeout __P((void));
446 static int siosetwater __P((struct com_s *com, speed_t speed));
447 static void comstart __P((struct tty *tp));
448 static timeout_t comwakeup;
449 static void disc_optim __P((struct tty *tp, struct termios *t,
453 static char driver_name[] = "sio";
455 /* table and macro for fast conversion from a unit number to its com struct */
456 static devclass_t sio_devclass;
457 #define com_addr(unit) ((struct com_s *) \
458 devclass_get_softc(sio_devclass, unit))
460 static device_method_t sio_methods[] = {
461 /* Device interface */
462 DEVMETHOD(device_probe, sioprobe),
463 DEVMETHOD(device_attach, sioattach),
468 static driver_t sio_driver = {
472 sizeof(struct com_s),
475 static d_open_t sioopen;
476 static d_close_t sioclose;
477 static d_read_t sioread;
478 static d_write_t siowrite;
479 static d_ioctl_t sioioctl;
480 static d_stop_t siostop;
481 static d_devtotty_t siodevtotty;
483 #define CDEV_MAJOR 28
484 static struct cdevsw sio_cdevsw = {
485 sioopen, sioclose, sioread, siowrite,
486 sioioctl, siostop, noreset, siodevtotty,
487 ttpoll, nommap, NULL, driver_name,
488 NULL, -1, nodump, nopsize,
493 static volatile speed_t comdefaultrate = CONSPEED;
494 static volatile speed_t gdbdefaultrate = CONSPEED;
495 static u_int com_events; /* input chars + weighted output completions */
496 static Port_t siocniobase;
497 static Port_t siogdbiobase;
498 static bool_t sio_registered;
499 static int sio_timeout;
500 static int sio_timeouts_until_log;
501 static struct callout_handle sio_timeout_handle
502 = CALLOUT_HANDLE_INITIALIZER(&sio_timeout_handle);
504 static struct tty *sio_tty[NSIOTOT];
506 static struct tty sio_tty[NSIOTOT];
508 static const int nsio_tty = NSIOTOT;
514 Port_t cmd, sts, ctrl, mod;
518 #define COM_INT_DISABLE {int previpri; previpri=spltty();
519 #define COM_INT_ENABLE splx(previpri);}
520 #define IEN_TxFLAG IEN_Tx
522 #define COM_CARRIER_DETECT_EMULATE 0
523 #define PC98_CHECK_MODEM_INTERVAL (hz/10)
524 #define DCD_OFF_TOLERANCE 2
525 #define DCD_ON_RECOGNITION 2
526 #define IS_8251(if_type) (!(if_type & 0x10))
527 #define COM1_EXT_CLOCK 0x40000
529 static void commint __P((dev_t dev));
530 static void com_tiocm_set __P((struct com_s *com, int msr));
531 static void com_tiocm_bis __P((struct com_s *com, int msr));
532 static void com_tiocm_bic __P((struct com_s *com, int msr));
533 static int com_tiocm_get __P((struct com_s *com));
534 static int com_tiocm_get_delta __P((struct com_s *com));
535 static void pc98_msrint_start __P((dev_t dev));
536 static void com_cflag_and_speed_set __P((struct com_s *com, int cflag, int speed));
537 static int pc98_ttspeedtab __P((struct com_s *com, int speed));
538 static int pc98_get_modem_status __P((struct com_s *com));
539 static timeout_t pc98_check_msr;
540 static void pc98_set_baud_rate __P((struct com_s *com, int count));
541 static void pc98_i8251_reset __P((struct com_s *com, int mode, int command));
542 static void pc98_disable_i8251_interrupt __P((struct com_s *com, int mod));
543 static void pc98_enable_i8251_interrupt __P((struct com_s *com, int mod));
544 static int pc98_check_i8251_interrupt __P((struct com_s *com));
545 static int pc98_i8251_get_cmd __P((struct com_s *com));
546 static int pc98_i8251_get_mod __P((struct com_s *com));
547 static void pc98_i8251_set_cmd __P((struct com_s *com, int x));
548 static void pc98_i8251_or_cmd __P((struct com_s *com, int x));
549 static void pc98_i8251_clear_cmd __P((struct com_s *com, int x));
550 static void pc98_i8251_clear_or_cmd __P((struct com_s *com, int clr, int x));
551 static int pc98_check_if_type __P((device_t dev, struct siodev *iod));
552 static void pc98_check_sysclock __P((void));
553 static int pc98_set_ioport __P((struct com_s *com, int id_flags));
555 #define com_int_Tx_disable(com) \
556 pc98_disable_i8251_interrupt(com,IEN_Tx|IEN_TxEMP)
557 #define com_int_Tx_enable(com) \
558 pc98_enable_i8251_interrupt(com,IEN_TxFLAG)
559 #define com_int_Rx_disable(com) \
560 pc98_disable_i8251_interrupt(com,IEN_Rx)
561 #define com_int_Rx_enable(com) \
562 pc98_enable_i8251_interrupt(com,IEN_Rx)
563 #define com_int_TxRx_disable(com) \
564 pc98_disable_i8251_interrupt(com,IEN_Tx|IEN_TxEMP|IEN_Rx)
565 #define com_int_TxRx_enable(com) \
566 pc98_enable_i8251_interrupt(com,IEN_TxFLAG|IEN_Rx)
567 #define com_send_break_on(com) \
568 pc98_i8251_or_cmd(com,CMD8251_SBRK)
569 #define com_send_break_off(com) \
570 pc98_i8251_clear_cmd(com,CMD8251_SBRK)
572 static struct speedtab pc98speedtab[] = { /* internal RS232C interface */
594 static struct speedtab pc98fast_speedtab[] = {
595 { 9600, 0x80 | COMBRD(9600), },
596 { 19200, 0x80 | COMBRD(19200), },
597 { 38400, 0x80 | COMBRD(38400), },
598 { 57600, 0x80 | COMBRD(57600), },
599 { 115200, 0x80 | COMBRD(115200), },
602 static struct speedtab comspeedtab_pio9032b[] = {
613 static struct speedtab comspeedtab_b98_01[] = {
628 static struct speedtab comspeedtab_mc16550[] = {
644 static struct speedtab comspeedtab_rsb384[] = {
665 static struct speedtab comspeedtab_rsa[] = {
667 { 50, COMBRD_RSA(50) },
668 { 75, COMBRD_RSA(75) },
669 { 110, COMBRD_RSA(110) },
670 { 134, COMBRD_RSA(134) },
671 { 150, COMBRD_RSA(150) },
672 { 200, COMBRD_RSA(200) },
673 { 300, COMBRD_RSA(300) },
674 { 600, COMBRD_RSA(600) },
675 { 1200, COMBRD_RSA(1200) },
676 { 1800, COMBRD_RSA(1800) },
677 { 2400, COMBRD_RSA(2400) },
678 { 4800, COMBRD_RSA(4800) },
679 { 9600, COMBRD_RSA(9600) },
680 { 19200, COMBRD_RSA(19200) },
681 { 38400, COMBRD_RSA(38400) },
682 { 57600, COMBRD_RSA(57600) },
683 { 115200, COMBRD_RSA(115200) },
684 { 230400, COMBRD_RSA(230400) },
685 { 460800, COMBRD_RSA(460800) },
686 { 921600, COMBRD_RSA(921600) },
691 static struct speedtab comspeedtab[] = {
695 { 110, COMBRD(110) },
696 { 134, COMBRD(134) },
697 { 150, COMBRD(150) },
698 { 200, COMBRD(200) },
699 { 300, COMBRD(300) },
700 { 600, COMBRD(600) },
701 { 1200, COMBRD(1200) },
702 { 1800, COMBRD(1800) },
703 { 2400, COMBRD(2400) },
704 { 4800, COMBRD(4800) },
705 { 9600, COMBRD(9600) },
706 { 19200, COMBRD(19200) },
707 { 38400, COMBRD(38400) },
708 { 57600, COMBRD(57600) },
709 { 115200, COMBRD(115200) },
718 struct speedtab *speedtab;
721 /* COM_IF_INTERNAL */
722 { " (internal)", {0x30, 0x32, 0x32, 0x33, 0x35, -1, -1},
723 -1, pc98speedtab, 1 },
724 /* COM_IF_PC9861K_1 */
725 { " (PC9861K)", {0xb1, 0xb3, 0xb3, 0xb0, 0xb0, -1, -1},
727 /* COM_IF_PC9861K_2 */
728 { " (PC9861K)", {0xb9, 0xbb, 0xbb, 0xb2, 0xb2, -1, -1},
730 /* COM_IF_IND_SS_1 */
731 { " (IND-SS)", {0xb1, 0xb3, 0xb3, 0xb0, 0xb0, 0xb3, -1},
732 3, comspeedtab_mc16550, 1 },
733 /* COM_IF_IND_SS_2 */
734 { " (IND-SS)", {0xb9, 0xbb, 0xbb, 0xb2, 0xb2, 0xbb, -1},
735 3, comspeedtab_mc16550, 1 },
736 /* COM_IF_PIO9032B_1 */
737 { " (PIO9032B)", {0xb1, 0xb3, 0xb3, 0xb0, 0xb0, 0xb8, -1},
738 7, comspeedtab_pio9032b, 1 },
739 /* COM_IF_PIO9032B_2 */
740 { " (PIO9032B)", {0xb9, 0xbb, 0xbb, 0xb2, 0xb2, 0xba, -1},
741 7, comspeedtab_pio9032b, 1 },
742 /* COM_IF_B98_01_1 */
743 { " (B98-01)", {0xb1, 0xb3, 0xb3, 0xb0, 0xb0, 0xd1, 0xd3},
744 7, comspeedtab_b98_01, 0 },
745 /* COM_IF_B98_01_2 */
746 { " (B98-01)", {0xb9, 0xbb, 0xbb, 0xb2, 0xb2, 0xd5, 0xd7},
747 7, comspeedtab_b98_01, 0 },
749 #define PC98SIO_data_port(type) (if_8251_type[type].port_table[0])
750 #define PC98SIO_cmd_port(type) (if_8251_type[type].port_table[1])
751 #define PC98SIO_sts_port(type) (if_8251_type[type].port_table[2])
752 #define PC98SIO_in_modem_port(type) (if_8251_type[type].port_table[3])
753 #define PC98SIO_intr_ctrl_port(type) (if_8251_type[type].port_table[4])
754 #define PC98SIO_baud_rate_port(type) (if_8251_type[type].port_table[5])
755 #define PC98SIO_func_port(type) (if_8251_type[type].port_table[6])
763 struct speedtab *speedtab;
764 } if_16550a_type[] = {
766 { " (RSA-98)", -1, -1, 0, IO_COMSIZE, comspeedtab },
768 { "", -1, -1, 0, IO_COMSIZE, comspeedtab },
769 /* COM_IF_SECOND_CCU */
770 { "", -1, -1, 0, IO_COMSIZE, comspeedtab },
771 /* COM_IF_MC16550II */
772 { " (MC16550II)", -1, 0x1000, 8, 1, comspeedtab_mc16550 },
774 { " (MC-RS98)", -1, 0x1000, 8, 1, comspeedtab_mc16550 },
776 { " (RSB-3000)", 0xbf, -1, 1, 1, comspeedtab_rsb384 },
778 { " (RSB-384)", 0xbf, -1, 1, 1, comspeedtab_rsb384 },
779 /* COM_IF_MODEM_CARD */
780 { "", -1, -1, 0, IO_COMSIZE, comspeedtab },
781 /* COM_IF_RSA98III */
782 { " (RSA-98III)", -1, -1, 0, 16, comspeedtab_rsa },
784 { " (ESP98)", -1, -1, 1, 1, comspeedtab_mc16550 },
791 /* XXX configure this properly. */
792 static Port_t likely_com_ports[] = { 0, 0xb0, 0xb1, 0 };
793 static Port_t likely_esp_ports[] = { 0xc0d0, 0 };
795 #define ESP98_CMD1 (ESP_CMD1 * 0x100)
796 #define ESP98_CMD2 (ESP_CMD2 * 0x100)
797 #define ESP98_STATUS1 (ESP_STATUS1 * 0x100)
798 #define ESP98_STATUS2 (ESP_STATUS2 * 0x100)
802 /* XXX configure this properly. */
803 static Port_t likely_com_ports[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, };
804 static Port_t likely_esp_ports[] = { 0x140, 0x180, 0x280, 0 };
810 * handle sysctl read/write requests for console speed
812 * In addition to setting comdefaultrate for I/O through /dev/console,
813 * also set the initial and lock values for the /dev/ttyXX device
814 * if there is one associated with the console. Finally, if the /dev/tty
815 * device has already been open, change the speed on the open running port
820 sysctl_machdep_comdefaultrate SYSCTL_HANDLER_ARGS
827 newspeed = comdefaultrate;
829 error = sysctl_handle_opaque(oidp, &newspeed, sizeof newspeed, req);
830 if (error || !req->newptr)
833 comdefaultrate = newspeed;
835 if (comconsole < 0) /* serial console not selected? */
838 com = com_addr(comconsole);
843 * set the initial and lock rates for /dev/ttydXX and /dev/cuaXX
844 * (note, the lock rates really are boolean -- if non-zero, disallow
847 com->it_in.c_ispeed = com->it_in.c_ospeed =
848 com->lt_in.c_ispeed = com->lt_in.c_ospeed =
849 com->it_out.c_ispeed = com->it_out.c_ospeed =
850 com->lt_out.c_ispeed = com->lt_out.c_ospeed = comdefaultrate;
853 * if we're open, change the running rate too
856 if (tp && (tp->t_state & TS_ISOPEN)) {
857 tp->t_termios.c_ispeed =
858 tp->t_termios.c_ospeed = comdefaultrate;
860 error = comparam(tp, &tp->t_termios);
866 SYSCTL_PROC(_machdep, OID_AUTO, conspeed, CTLTYPE_INT | CTLFLAG_RW,
867 0, 0, sysctl_machdep_comdefaultrate, "I", "");
871 * PC-Card (PCMCIA) specific code.
873 static int sioinit __P((struct pccard_devinfo *));
874 static void siounload __P((struct pccard_devinfo *));
875 static int card_intr __P((struct pccard_devinfo *));
877 PCCARD_MODULE(sio, sioinit, siounload, card_intr, 0, tty_imask);
880 * Initialize the device - called from Slot manager.
883 sioinit(struct pccard_devinfo *devi)
886 /* validate unit number. */
887 if (devi->isahd.id_unit >= (NSIOTOT))
889 /* Make sure it isn't already probed. */
890 if (com_addr(devi->isahd.id_unit))
893 /* It's already probed as serial by Upper */
894 devi->isahd.id_flags |= COM_C_NOPROBE;
897 * Probe the device. If a value is returned, the
898 * device was found at the location.
900 if (sioprobe(&devi->isahd) == 0)
902 if (sioattach(&devi->isahd) == 0)
909 * siounload - unload the driver and clear the table.
911 * This is usually called when the card is ejected, but
912 * can be caused by a modunload of a controller driver.
913 * The idea is to reset the driver's view of the device
914 * and ensure that any driver entry points such as
915 * read and write do not hang.
918 siounload(struct pccard_devinfo *devi)
923 printf("NULL devi in siounload\n");
926 com = com_addr(devi->isahd.id_unit);
928 printf("NULL com in siounload\n");
932 printf("sio%d already unloaded!\n",devi->isahd.id_unit);
935 if (com->tp && (com->tp->t_state & TS_ISOPEN)) {
937 printf("sio%d: unload\n", devi->isahd.id_unit);
943 com_addr(com->unit) = NULL;
944 if (com->ibuf != NULL)
945 free(com->ibuf, M_DEVBUF);
947 printf("sio%d: unload,gone\n", devi->isahd.id_unit);
952 * card_intr - Shared interrupt called from
953 * front end of PC-Card handler.
956 card_intr(struct pccard_devinfo *devi)
961 com = com_addr(devi->isahd.id_unit);
962 if (com && !com->gone)
963 siointr1(com_addr(devi->isahd.id_unit));
967 #endif /* NCARD > 0 */
969 #define SET_FLAG(dev, bit) isa_set_flags(dev, isa_get_flags(dev) | (bit))
970 #define CLR_FLAG(dev, bit) isa_set_flags(dev, isa_get_flags(dev) & ~(bit))
976 static bool_t already_init;
981 intrmask_t irqmap[4];
986 u_int flags = isa_get_flags(dev);
993 Port_t rsabase = NULL;
998 * Turn off MCR_IENABLE for all likely serial ports. An unused
999 * port with its MCR_IENABLE gate open will inhibit interrupts
1000 * from any used port that shares the interrupt vector.
1001 * XXX the gate enable is elsewhere for some multiports.
1006 devclass_get_devices(sio_devclass, &devs, &count);
1008 for (i = 0; i < count; i++) {
1010 tmp = (flags >> 24) & 0xff;
1012 outb((isa_get_port(xdev) & 0xff00) | PC98SIO_cmd_port(tmp & 0x0f), 0xf2);
1014 if (tmp == COM_IF_RSA98III) {
1015 rsabase = isa_get_port(xdev) & 0xfff0;
1016 outb(isa_get_port(xdev) + 8 + (com_mcr << if_16550a_type[tmp & 0x0f].port_shift), 0);
1018 outb(isa_get_port(xdev) + (com_mcr << if_16550a_type[tmp & 0x0f].port_shift), 0);
1021 for (i = 0; i < count; i++) {
1023 outb(isa_get_port(xdev) + com_mcr, 0);
1027 already_init = TRUE;
1030 if (COM_LLCONSOLE(flags)) {
1031 printf("sio%d: reserved for low-level i/o\n",
1032 device_get_unit(dev));
1040 * If the port is i8251 UART (internal, B98_01)
1042 if (pc98_check_if_type(dev, &iod) == -1)
1045 isa_set_irq(dev, iod.irq);
1046 if (IS_8251(iod.if_type)) {
1053 outb(iod.cmd, CMD8251_RESET);
1054 DELAY(1000); /* for a while...*/
1055 outb(iod.cmd, 0xf2); /* MODE (dummy) */
1057 outb(iod.cmd, 0x01); /* CMD (dummy) */
1058 DELAY(1000); /* for a while...*/
1059 if (( inb(iod.sts) & STS8251_TxEMP ) == 0 ) {
1062 if (if_8251_type[iod.if_type & 0x0f].check_irq) {
1064 tmp = ( inb( iod.ctrl ) & ~(IEN_Rx|IEN_TxEMP|IEN_Tx));
1065 outb( iod.ctrl, tmp|IEN_TxEMP );
1067 ret = isa_irq_pending() ? 4 : 0;
1068 outb( iod.ctrl, tmp );
1072 * B98_01 doesn't activate TxEMP interrupt line
1073 * when being reset, so we can't check irq pending.
1077 if (epson_machine_id==0x20) { /* XXX */
1084 * If the device is on a multiport card and has an AST/4
1085 * compatible interrupt control register, initialize this
1086 * register and prepare to leave MCR_IENABLE clear in the mcr.
1087 * Otherwise, prepare to set MCR_IENABLE in the mcr.
1088 * Point idev to the device struct giving the correct id_irq.
1089 * This is the struct for the master device if there is one.
1092 mcr_image = MCR_IENABLE;
1094 if (iod.if_type == COM_IF_RSA98III) {
1096 rsabase = isa_get_port(idev) & 0xfff0;
1097 if (rsabase != isa_get_port(idev))
1099 outb(rsabase + rsa_msr, 0x04);
1100 outb(rsabase + rsa_frr, 0x00);
1101 if ((inb(rsabase + rsa_srr) & 0x36) != 0x36)
1103 outb(rsabase + rsa_ier, 0x00);
1104 outb(rsabase + rsa_frr, 0x00);
1105 outb(rsabase + rsa_tivsr, 0x00);
1106 outb(rsabase + rsa_tcr, 0x00);
1109 #ifdef COM_MULTIPORT
1110 if (COM_ISMULTIPORT(flags)) {
1111 idev = devclass_get_device(sio_devclass, COM_MPMASTER(flags));
1113 printf("sio%d: master device %d not configured\n",
1114 device_get_unit(dev), COM_MPMASTER(flags));
1115 isa_set_irq(dev, 0);
1119 if (!COM_NOTAST4(flags)) {
1120 outb(isa_get_port(idev) + com_scr,
1121 isa_get_irq(idev) >= 0 ? 0x80 : 0);
1126 #endif /* COM_MULTIPORT */
1127 if (isa_get_irq(idev) < 0)
1131 tmp = if_16550a_type[iod.if_type & 0x0f].irr_write;
1134 switch (isa_get_irq(idev)) {
1135 case 3: irqout = 4; break;
1136 case 5: irqout = 5; break;
1137 case 6: irqout = 6; break;
1138 case 12: irqout = 7; break;
1140 printf("sio%d: irq configuration error\n",
1141 device_get_unit(dev));
1144 outb((isa_get_port(dev) & 0x00ff) | tmp, irqout);
1146 port_shift = if_16550a_type[iod.if_type & 0x0f].port_shift;
1148 bzero(failures, sizeof failures);
1149 iobase = isa_get_port(dev);
1151 if (iod.if_type == COM_IF_RSA98III)
1156 * We don't want to get actual interrupts, just masked ones.
1157 * Interrupts from this line should already be masked in the ICU,
1158 * but mask them in the processor as well in case there are some
1159 * (misconfigured) shared interrupts.
1165 * Initialize the speed and the word size and wait long enough to
1166 * drain the maximum of 16 bytes of junk in device output queues.
1167 * The speed is undefined after a master reset and must be set
1168 * before relying on anything related to output. There may be
1169 * junk after a (very fast) soft reboot and (apparently) after
1171 * XXX what about the UART bug avoided by waiting in comparam()?
1172 * We don't want to to wait long enough to drain at 2 bps.
1174 if (iobase == siocniobase)
1175 DELAY((16 + 1) * 1000000 / (comdefaultrate / 10));
1178 tmp = ttspeedtab(SIO_TEST_SPEED,
1179 if_16550a_type[iod.if_type & 0x0f].speedtab);
1180 outb(iobase + (com_cfcr << port_shift), CFCR_DLAB|CFCR_8BITS);
1181 outb(iobase + (com_dlbl << port_shift), tmp & 0xff);
1182 outb(iobase + (com_dlbh << port_shift), (tmp >> 8) & 0xff);
1183 outb(iobase + (com_cfcr << port_shift), CFCR_8BITS);
1185 outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS);
1186 outb(iobase + com_dlbl, COMBRD(SIO_TEST_SPEED) & 0xff);
1187 outb(iobase + com_dlbh, (u_int) COMBRD(SIO_TEST_SPEED) >> 8);
1188 outb(iobase + com_cfcr, CFCR_8BITS);
1190 DELAY((16 + 1) * 1000000 / (SIO_TEST_SPEED / 10));
1194 * Enable the interrupt gate and disable device interupts. This
1195 * should leave the device driving the interrupt line low and
1196 * guarantee an edge trigger if an interrupt can be generated.
1200 outb(iobase + (com_mcr << port_shift), mcr_image);
1201 outb(iobase + (com_ier << port_shift), 0);
1203 outb(iobase + com_mcr, mcr_image);
1204 outb(iobase + com_ier, 0);
1206 DELAY(1000); /* XXX */
1207 irqmap[0] = isa_irq_pending();
1210 * Attempt to set loopback mode so that we can send a null byte
1211 * without annoying any external device.
1215 outb(iobase + (com_mcr << port_shift), mcr_image | MCR_LOOPBACK);
1217 outb(iobase + com_mcr, mcr_image | MCR_LOOPBACK);
1221 * Attempt to generate an output interrupt. On 8250's, setting
1222 * IER_ETXRDY generates an interrupt independent of the current
1223 * setting and independent of whether the THR is empty. On 16450's,
1224 * setting IER_ETXRDY generates an interrupt independent of the
1225 * current setting. On 16550A's, setting IER_ETXRDY only
1226 * generates an interrupt when IER_ETXRDY is not already set.
1229 outb(iobase + (com_ier << port_shift), IER_ETXRDY);
1230 if (iod.if_type == COM_IF_RSA98III)
1231 outb(rsabase + rsa_ier, 0x04);
1233 outb(iobase + com_ier, IER_ETXRDY);
1237 * On some 16x50 incompatibles, setting IER_ETXRDY doesn't generate
1238 * an interrupt. They'd better generate one for actually doing
1239 * output. Loopback may be broken on the same incompatibles but
1240 * it's unlikely to do more than allow the null byte out.
1243 outb(iobase + (com_data << port_shift), 0);
1245 outb(iobase + com_data, 0);
1247 DELAY((1 + 2) * 1000000 / (SIO_TEST_SPEED / 10));
1250 * Turn off loopback mode so that the interrupt gate works again
1251 * (MCR_IENABLE was hidden). This should leave the device driving
1252 * an interrupt line high. It doesn't matter if the interrupt
1253 * line oscillates while we are not looking at it, since interrupts
1258 outb(iobase + (com_mcr << port_shift), mcr_image);
1260 outb(iobase + com_mcr, mcr_image);
1264 * It's a definitly Serial PCMCIA(16550A), but still be required
1265 * for IIR_TXRDY implementation ( Palido 321s, DC-1S... )
1267 if ( COM_NOPROBE(flags) ) {
1268 /* Reading IIR register twice */
1269 for ( fn = 0; fn < 2; fn ++ ) {
1272 failures[6] = inb(iobase + (com_iir << port_shift));
1274 failures[6] = inb(iobase + com_iir);
1277 /* Check IIR_TXRDY clear ? */
1279 isa_set_portsize(dev,
1280 if_16550a_type[iod.if_type & 0x0f].io_size);
1282 isa_set_portsize(dev, IO_COMSIZE);
1285 if ( failures[6] & IIR_TXRDY ) {
1286 /* Nop, Double check with clearing IER */
1288 outb(iobase + (com_ier << port_shift), 0);
1290 (com_iir << port_shift)) & IIR_NOPEND) {
1292 outb(iobase + com_ier, 0);
1293 if ( inb(iobase + com_iir) & IIR_NOPEND ) {
1295 /* Ok. we're familia this gang */
1296 SET_FLAG(dev, COM_C_IIR_TXRDYBUG); /* Set IIR_TXRDYBUG */
1298 /* Unknow, Just omit this chip.. XXX*/
1302 /* OK. this is well-known guys */
1303 CLR_FLAG(dev, COM_C_IIR_TXRDYBUG); /*Clear IIR_TXRDYBUG*/
1306 outb(iobase + (com_cfcr << port_shift), CFCR_8BITS);
1308 outb(iobase + com_cfcr, CFCR_8BITS);
1311 return (iobase == siocniobase ? 0 : result);
1316 * o the CFCR, IER and MCR in UART hold the values written to them
1317 * (the values happen to be all distinct - this is good for
1318 * avoiding false positive tests from bus echoes).
1319 * o an output interrupt is generated and its vector is correct.
1320 * o the interrupt goes away when the IIR in the UART is read.
1324 failures[0] = inb(iobase + (com_cfcr << port_shift)) - CFCR_8BITS;
1325 failures[1] = inb(iobase + (com_ier << port_shift)) - IER_ETXRDY;
1326 failures[2] = inb(iobase + (com_mcr << port_shift)) - mcr_image;
1328 failures[0] = inb(iobase + com_cfcr) - CFCR_8BITS;
1329 failures[1] = inb(iobase + com_ier) - IER_ETXRDY;
1330 failures[2] = inb(iobase + com_mcr) - mcr_image;
1332 DELAY(10000); /* Some internal modems need this time */
1333 irqmap[1] = isa_irq_pending();
1335 failures[4] = (inb(iobase + (com_iir << port_shift)) & IIR_IMASK)
1337 if (iod.if_type == COM_IF_RSA98III)
1338 inb(rsabase + rsa_srr);
1340 failures[4] = (inb(iobase + com_iir) & IIR_IMASK) - IIR_TXRDY;
1342 DELAY(1000); /* XXX */
1343 irqmap[2] = isa_irq_pending();
1345 failures[6] = (inb(iobase + (com_iir << port_shift)) & IIR_IMASK)
1347 if (iod.if_type == COM_IF_RSA98III)
1348 inb(rsabase + rsa_srr);
1350 failures[6] = (inb(iobase + com_iir) & IIR_IMASK) - IIR_NOPEND;
1354 * Turn off all device interrupts and check that they go off properly.
1355 * Leave MCR_IENABLE alone. For ports without a master port, it gates
1356 * the OUT2 output of the UART to
1357 * the ICU input. Closing the gate would give a floating ICU input
1358 * (unless there is another device driving at) and spurious interrupts.
1359 * (On the system that this was first tested on, the input floats high
1360 * and gives a (masked) interrupt as soon as the gate is closed.)
1363 outb(iobase + (com_ier << port_shift), 0);
1364 outb(iobase + (com_cfcr << port_shift), CFCR_8BITS);
1365 failures[7] = inb(iobase + (com_ier << port_shift));
1366 if (iod.if_type == COM_IF_RSA98III)
1367 outb(rsabase + rsa_ier, 0x00);
1369 outb(iobase + com_ier, 0);
1370 outb(iobase + com_cfcr, CFCR_8BITS); /* dummy to avoid bus echo */
1371 failures[7] = inb(iobase + com_ier);
1373 DELAY(1000); /* XXX */
1374 irqmap[3] = isa_irq_pending();
1376 failures[9] = (inb(iobase + (com_iir << port_shift)) & IIR_IMASK)
1378 if (iod.if_type == COM_IF_RSA98III) {
1379 inb(rsabase + rsa_srr);
1380 outb(rsabase + rsa_frr, 0x00);
1383 failures[9] = (inb(iobase + com_iir) & IIR_IMASK) - IIR_NOPEND;
1388 irqs = irqmap[1] & ~irqmap[0];
1389 if (isa_get_irq(idev) >= 0 && ((1 << isa_get_irq(idev)) & irqs) == 0)
1391 "sio%d: configured irq %d not in bitmap of probed irqs %#x\n",
1392 device_get_unit(dev), isa_get_irq(idev), irqs);
1394 printf("sio%d: irq maps: %#x %#x %#x %#x\n",
1395 device_get_unit(dev),
1396 irqmap[0], irqmap[1], irqmap[2], irqmap[3]);
1399 isa_set_portsize(dev, if_16550a_type[iod.if_type & 0x0f].io_size);
1401 isa_set_portsize(dev, IO_COMSIZE);
1404 for (fn = 0; fn < sizeof failures; ++fn)
1407 outb(iobase + (com_mcr << port_shift), 0);
1409 outb(iobase + com_mcr, 0);
1413 printf("sio%d: probe failed test(s):",
1414 device_get_unit(dev));
1415 for (fn = 0; fn < sizeof failures; ++fn)
1422 return (iobase == siocniobase ? 0 : result);
1427 espattach(isdp, com, esp_port)
1428 struct isa_device *isdp;
1436 * Check the ESP-specific I/O port to see if we're an ESP
1437 * card. If not, return failure immediately.
1439 if ((inb(esp_port) & 0xf3) == 0) {
1440 printf(" port 0x%x is not an ESP board?\n", esp_port);
1445 * We've got something that claims to be a Hayes ESP card.
1449 /* Get the dip-switch configuration */
1451 outb(esp_port + ESP98_CMD1, ESP_GETDIPS);
1452 dips = inb(esp_port + ESP98_STATUS1);
1454 outb(esp_port + ESP_CMD1, ESP_GETDIPS);
1455 dips = inb(esp_port + ESP_STATUS1);
1459 * Bits 0,1 of dips say which COM port we are.
1462 if ((com->iobase & 0xff) == likely_com_ports[dips & 0x03])
1464 if (com->iobase == likely_com_ports[dips & 0x03])
1468 printf(" esp_port has com %d\n", dips & 0x03);
1473 * Check for ESP version 2.0 or later: bits 4,5,6 = 010.
1476 outb(esp_port + ESP98_CMD1, ESP_GETTEST);
1477 val = inb(esp_port + ESP98_STATUS1); /* clear reg 1 */
1478 val = inb(esp_port + ESP98_STATUS2);
1480 outb(esp_port + ESP_CMD1, ESP_GETTEST);
1481 val = inb(esp_port + ESP_STATUS1); /* clear reg 1 */
1482 val = inb(esp_port + ESP_STATUS2);
1484 if ((val & 0x70) < 0x20) {
1485 printf("-old (%o)", val & 0x70);
1490 * Check for ability to emulate 16550: bit 7 == 1
1492 if ((dips & 0x80) == 0) {
1498 * Okay, we seem to be a Hayes ESP card. Whee.
1501 com->esp_port = esp_port;
1504 #endif /* COM_ESP */
1518 struct resource *res;
1520 u_int flags = isa_get_flags(dev);
1528 isdp->id_ri_flags |= RI_FAST;
1530 iobase = isa_get_port(dev);
1532 if (((flags >> 24) & 0xff) == COM_IF_RSA98III)
1535 unit = device_get_unit(dev);
1536 com = device_get_softc(dev);
1539 if (((flags >> 24) & 0xff) == COM_IF_RSA98III)
1541 if ((obuf = malloc(obufsize * 2, M_DEVBUF, M_NOWAIT)) == NULL)
1543 bzero(obuf, obufsize * 2);
1547 * sioprobe() has initialized the device registers as follows:
1548 * o cfcr = CFCR_8BITS.
1549 * It is most important that CFCR_DLAB is off, so that the
1550 * data port is not hidden when we enable interrupts.
1552 * Interrupts are only enabled when the line is open.
1553 * o mcr = MCR_IENABLE, or 0 if the port has AST/4 compatible
1554 * interrupt control register or the config specifies no irq.
1555 * Keeping MCR_DTR and MCR_RTS off might stop the external
1556 * device from sending before we are ready.
1558 bzero(com, sizeof *com);
1560 com->obufsize = obufsize;
1562 com->obuf2 = obuf + obufsize;
1565 com->cfcr_image = CFCR_8BITS;
1566 com->dtr_wait = 3 * hz;
1567 com->loses_outints = COM_LOSESOUTINTS(flags) != 0;
1568 com->no_irq = isa_get_irq(dev) < 0;
1569 com->tx_fifo_size = 1;
1570 com->obufs[0].l_head = com->obuf1;
1571 com->obufs[1].l_head = com->obuf2;
1573 com->iobase = iobase;
1575 if (pc98_set_ioport(com, isa_get_flags(dev)) == -1) {
1576 com->pc98_if_type = (isa_get_flags(dev) >> 24) & 0xff;
1577 port_shift = if_16550a_type[com->pc98_if_type & 0x0f].port_shift;
1578 com->data_port = iobase + (com_data << port_shift);
1579 com->int_id_port = iobase + (com_iir << port_shift);
1580 com->modem_ctl_port = iobase + (com_mcr << port_shift);
1581 com->mcr_image = inb(com->modem_ctl_port);
1582 com->line_status_port = iobase + (com_lsr << port_shift);
1583 com->modem_status_port = iobase + (com_msr << port_shift);
1584 com->intr_ctl_port = iobase + (com_ier << port_shift);
1586 #else /* not PC98 */
1587 com->data_port = iobase + com_data;
1588 com->int_id_port = iobase + com_iir;
1589 com->modem_ctl_port = iobase + com_mcr;
1590 com->mcr_image = inb(com->modem_ctl_port);
1591 com->line_status_port = iobase + com_lsr;
1592 com->modem_status_port = iobase + com_msr;
1593 com->intr_ctl_port = iobase + com_ier;
1597 * We don't use all the flags from <sys/ttydefaults.h> since they
1598 * are only relevant for logins. It's important to have echo off
1599 * initially so that the line doesn't start blathering before the
1600 * echo flag can be turned off.
1602 com->it_in.c_iflag = 0;
1603 com->it_in.c_oflag = 0;
1604 com->it_in.c_cflag = TTYDEF_CFLAG;
1605 com->it_in.c_lflag = 0;
1606 if (unit == comconsole) {
1608 if (IS_8251(com->pc98_if_type))
1611 com->it_in.c_iflag = TTYDEF_IFLAG;
1612 com->it_in.c_oflag = TTYDEF_OFLAG;
1613 com->it_in.c_cflag = TTYDEF_CFLAG | CLOCAL;
1614 com->it_in.c_lflag = TTYDEF_LFLAG;
1615 com->lt_out.c_cflag = com->lt_in.c_cflag = CLOCAL;
1616 com->lt_out.c_ispeed = com->lt_out.c_ospeed =
1617 com->lt_in.c_ispeed = com->lt_in.c_ospeed =
1618 com->it_in.c_ispeed = com->it_in.c_ospeed = comdefaultrate;
1620 com->it_in.c_ispeed = com->it_in.c_ospeed = TTYDEF_SPEED;
1621 if (siosetwater(com, com->it_in.c_ispeed) != 0) {
1623 free(com, M_DEVBUF);
1627 termioschars(&com->it_in);
1628 com->it_out = com->it_in;
1630 /* attempt to determine UART type */
1631 printf("sio%d: type", unit);
1635 #ifdef COM_MULTIPORT
1636 if (!COM_ISMULTIPORT(flags) && !COM_IIR_TXRDYBUG(flags))
1638 if (!COM_IIR_TXRDYBUG(flags))
1645 scr = inb(iobase + com_scr);
1646 outb(iobase + com_scr, 0xa5);
1647 scr1 = inb(iobase + com_scr);
1648 outb(iobase + com_scr, 0x5a);
1649 scr2 = inb(iobase + com_scr);
1650 outb(iobase + com_scr, scr);
1651 if (scr1 != 0xa5 || scr2 != 0x5a) {
1653 goto determined_type;
1658 if (IS_8251(com->pc98_if_type)) {
1659 com_int_TxRx_disable( com );
1660 com_cflag_and_speed_set( com, com->it_in.c_cflag, comdefaultrate );
1661 com_tiocm_bic( com, TIOCM_DTR|TIOCM_RTS|TIOCM_LE );
1662 com_send_break_off( com );
1663 printf(" 8251%s", if_8251_type[com->pc98_if_type & 0x0f].name);
1665 outb(iobase + (com_fifo << port_shift), FIFO_ENABLE | FIFO_RX_HIGH);
1667 outb(iobase + com_fifo, FIFO_ENABLE | FIFO_RX_HIGH);
1671 switch (inb(com->int_id_port) & IIR_FIFO_MASK) {
1682 if (COM_NOFIFO(flags)) {
1683 printf(" 16550A fifo disabled");
1685 com->hasfifo = TRUE;
1687 com->tx_fifo_size = 0; /* XXX flag conflicts. */
1690 if (COM_ST16650A(flags)) {
1692 com->tx_fifo_size = 32;
1693 printf(" ST16650A");
1695 com->tx_fifo_size = COM_FIFOSIZE(flags);
1701 if (com->pc98_if_type == COM_IF_RSA98III) {
1702 com->tx_fifo_size = 2048;
1703 com->rsabase = isa_get_port(dev);
1704 outb(com->rsabase + rsa_ier, 0x00);
1705 outb(com->rsabase + rsa_frr, 0x00);
1711 if (com->pc98_if_type == COM_IF_ESP98)
1713 for (espp = likely_esp_ports; *espp != 0; espp++)
1714 if (espattach(dev, com, *espp)) {
1715 com->tx_fifo_size = 1024;
1719 if (!com->st16650a) {
1720 if (!com->tx_fifo_size)
1721 com->tx_fifo_size = 16;
1723 printf(" lookalike with %d bytes FIFO",
1731 if (com->pc98_if_type == COM_IF_RSB3000) {
1732 /* Set RSB-2000/3000 Extended Buffer mode. */
1734 lcr = inb(iobase + (com_cfcr << port_shift));
1735 outb(iobase + (com_cfcr << port_shift), lcr | CFCR_DLAB);
1736 outb(iobase + (com_emr << port_shift), EMR_EXBUFF | EMR_EFMODE);
1737 outb(iobase + (com_cfcr << port_shift), lcr);
1744 * Set 16550 compatibility mode.
1745 * We don't use the ESP_MODE_SCALE bit to increase the
1746 * fifo trigger levels because we can't handle large
1748 * XXX flow control should be set in comparam(), not here.
1751 outb(com->esp_port + ESP98_CMD1, ESP_SETMODE);
1752 outb(com->esp_port + ESP98_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO);
1754 outb(com->esp_port + ESP_CMD1, ESP_SETMODE);
1755 outb(com->esp_port + ESP_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO);
1758 /* Set RTS/CTS flow control. */
1760 outb(com->esp_port + ESP98_CMD1, ESP_SETFLOWTYPE);
1761 outb(com->esp_port + ESP98_CMD2, ESP_FLOW_RTS);
1762 outb(com->esp_port + ESP98_CMD2, ESP_FLOW_CTS);
1764 outb(com->esp_port + ESP_CMD1, ESP_SETFLOWTYPE);
1765 outb(com->esp_port + ESP_CMD2, ESP_FLOW_RTS);
1766 outb(com->esp_port + ESP_CMD2, ESP_FLOW_CTS);
1769 /* Set flow-control levels. */
1771 outb(com->esp_port + ESP98_CMD1, ESP_SETRXFLOW);
1772 outb(com->esp_port + ESP98_CMD2, HIBYTE(768));
1773 outb(com->esp_port + ESP98_CMD2, LOBYTE(768));
1774 outb(com->esp_port + ESP98_CMD2, HIBYTE(512));
1775 outb(com->esp_port + ESP98_CMD2, LOBYTE(512));
1777 outb(com->esp_port + ESP_CMD1, ESP_SETRXFLOW);
1778 outb(com->esp_port + ESP_CMD2, HIBYTE(768));
1779 outb(com->esp_port + ESP_CMD2, LOBYTE(768));
1780 outb(com->esp_port + ESP_CMD2, HIBYTE(512));
1781 outb(com->esp_port + ESP_CMD2, LOBYTE(512));
1785 /* Set UART clock prescaler. */
1786 outb(com->esp_port + ESP98_CMD1, ESP_SETCLOCK);
1787 outb(com->esp_port + ESP98_CMD2, 2); /* 4 times */
1790 #endif /* COM_ESP */
1792 printf("%s", if_16550a_type[com->pc98_if_type & 0x0f].name);
1793 outb(iobase + (com_fifo << port_shift), 0);
1795 outb(iobase + com_fifo, 0);
1799 #ifdef COM_MULTIPORT
1800 if (COM_ISMULTIPORT(flags)) {
1801 com->multiport = TRUE;
1802 printf(" (multiport");
1803 if (unit == COM_MPMASTER(flags))
1807 isa_get_irq(devclass_get_device
1808 (sio_devclass, COM_MPMASTER(flags))) < 0;
1810 #endif /* COM_MULTIPORT */
1814 if (unit == comconsole)
1815 printf(", console");
1816 if ( COM_IIR_TXRDYBUG(flags) )
1817 printf(" with a bogus IIR_TXRDY register");
1820 if (!sio_registered) {
1821 register_swi(SWI_TTY, siopoll);
1822 sio_registered = TRUE;
1825 com->devfs_token_ttyd = devfs_add_devswf(&sio_cdevsw,
1827 UID_ROOT, GID_WHEEL, 0600, "ttyd%r", unit);
1828 com->devfs_token_ttyi = devfs_add_devswf(&sio_cdevsw,
1829 unit | CONTROL_INIT_STATE, DV_CHR,
1830 UID_ROOT, GID_WHEEL, 0600, "ttyid%r", unit);
1831 com->devfs_token_ttyl = devfs_add_devswf(&sio_cdevsw,
1832 unit | CONTROL_LOCK_STATE, DV_CHR,
1833 UID_ROOT, GID_WHEEL, 0600, "ttyld%r", unit);
1834 com->devfs_token_cuaa = devfs_add_devswf(&sio_cdevsw,
1835 unit | CALLOUT_MASK, DV_CHR,
1836 UID_UUCP, GID_DIALER, 0660, "cuaa%r", unit);
1837 com->devfs_token_cuai = devfs_add_devswf(&sio_cdevsw,
1838 unit | CALLOUT_MASK | CONTROL_INIT_STATE, DV_CHR,
1839 UID_UUCP, GID_DIALER, 0660, "cuaia%r", unit);
1840 com->devfs_token_cual = devfs_add_devswf(&sio_cdevsw,
1841 unit | CALLOUT_MASK | CONTROL_LOCK_STATE, DV_CHR,
1842 UID_UUCP, GID_DIALER, 0660, "cuala%r", unit);
1844 com->flags = isa_get_flags(dev); /* Heritate id_flags for later */
1845 com->pps.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR;
1846 pps_init(&com->pps);
1848 res = bus_alloc_resource(dev, SYS_RES_IRQ, &zero, 0ul, ~0ul, 1,
1849 RF_SHAREABLE | RF_ACTIVE);
1850 BUS_SETUP_INTR(device_get_parent(dev), dev, res, siointr, com,
1857 sioopen(dev, flag, mode, p)
1875 unit = MINOR_TO_UNIT(mynor);
1876 if ((u_int) unit >= NSIOTOT || (com = com_addr(unit)) == NULL)
1880 if (mynor & CONTROL_MASK)
1883 tp = com->tp = sio_tty[unit] = ttymalloc(sio_tty[unit]);
1885 tp = com->tp = &sio_tty[unit];
1890 if (!IS_8251(com->pc98_if_type))
1891 port_shift = if_16550a_type[com->pc98_if_type & 0x0f].port_shift;
1894 * We jump to this label after all non-interrupted sleeps to pick
1895 * up any changes of the device state.
1898 while (com->state & CS_DTR_OFF) {
1899 error = tsleep(&com->dtr_wait, TTIPRI | PCATCH, "siodtr", 0);
1900 if (com_addr(unit) == NULL)
1902 if (error != 0 || com->gone)
1905 if (tp->t_state & TS_ISOPEN) {
1907 * The device is open, so everything has been initialized.
1910 if (mynor & CALLOUT_MASK) {
1911 if (!com->active_out) {
1916 if (com->active_out) {
1917 if (flag & O_NONBLOCK) {
1921 error = tsleep(&com->active_out,
1922 TTIPRI | PCATCH, "siobi", 0);
1923 if (com_addr(unit) == NULL)
1925 if (error != 0 || com->gone)
1930 if (tp->t_state & TS_XCLUDE &&
1931 suser(p->p_ucred, &p->p_acflag)) {
1937 * The device isn't open, so there are no conflicts.
1938 * Initialize it. Initialization is done twice in many
1939 * cases: to preempt sleeping callin opens if we are
1940 * callout, and to complete a callin open after DCD rises.
1942 tp->t_oproc = comstart;
1943 tp->t_param = comparam;
1945 tp->t_termios = mynor & CALLOUT_MASK
1946 ? com->it_out : com->it_in;
1948 if (!IS_8251(com->pc98_if_type))
1950 (void)commctl(com, TIOCM_DTR | TIOCM_RTS, DMSET);
1951 com->poll = com->no_irq;
1952 com->poll_output = com->loses_outints;
1954 error = comparam(tp, &tp->t_termios);
1959 if (IS_8251(com->pc98_if_type)) {
1960 com_tiocm_bis(com, TIOCM_DTR|TIOCM_RTS);
1961 pc98_msrint_start(dev);
1965 * XXX we should goto open_top if comparam() slept.
1967 iobase = com->iobase;
1970 * (Re)enable and drain fifos.
1972 * Certain SMC chips cause problems if the fifos
1973 * are enabled while input is ready. Turn off the
1974 * fifo if necessary to clear the input. We test
1975 * the input ready bit after enabling the fifos
1976 * since we've already enabled them in comparam()
1977 * and to handle races between enabling and fresh
1982 outb(iobase + (com_fifo << port_shift),
1983 FIFO_RCV_RST | FIFO_XMT_RST
1985 if (com->pc98_if_type == COM_IF_RSA98III)
1986 outb(com->rsabase + rsa_frr , 0x00);
1988 outb(iobase + com_fifo,
1989 FIFO_RCV_RST | FIFO_XMT_RST
1993 * XXX the delays are for superstitious
1994 * historical reasons. It must be less than
1995 * the character time at the maximum
1996 * supported speed (87 usec at 115200 bps
1997 * 8N1). Otherwise we might loop endlessly
1998 * if data is streaming in. We used to use
1999 * delays of 100. That usually worked
2000 * because DELAY(100) used to usually delay
2001 * for about 85 usec instead of 100.
2005 if (!(inb(com->line_status_port) & LSR_RXRDY))
2007 if (com->pc98_if_type == COM_IF_RSA98III
2008 ? !(inb(com->rsabase + rsa_srr) & 0x08)
2009 : !(inb(com->line_status_port) & LSR_RXRDY))
2013 outb(iobase + (com_fifo << port_shift), 0);
2015 outb(iobase + com_fifo, 0);
2018 (void) inb(com->data_port);
2024 if (IS_8251(com->pc98_if_type)) {
2025 com_tiocm_bis(com, TIOCM_LE);
2026 com->pc98_prev_modem_status = pc98_get_modem_status(com);
2027 com_int_Rx_enable(com);
2030 (void) inb(com->line_status_port);
2031 (void) inb(com->data_port);
2032 com->prev_modem_status = com->last_modem_status
2033 = inb(com->modem_status_port);
2034 if (COM_IIR_TXRDYBUG(com->flags)) {
2035 outb(com->intr_ctl_port, IER_ERXRDY | IER_ERLS
2038 outb(com->intr_ctl_port, IER_ERXRDY | IER_ETXRDY
2039 | IER_ERLS | IER_EMSC);
2042 if (com->pc98_if_type == COM_IF_RSA98III) {
2043 outb(com->rsabase + rsa_ier, 0x1d);
2044 outb(com->intr_ctl_port, IER_ERLS | IER_EMSC);
2052 * Handle initial DCD. Callout devices get a fake initial
2053 * DCD (trapdoor DCD). If we are callout, then any sleeping
2054 * callin opens get woken up and resume sleeping on "siobi"
2055 * instead of "siodcd".
2058 * XXX `mynor & CALLOUT_MASK' should be
2059 * `tp->t_cflag & (SOFT_CARRIER | TRAPDOOR_CARRIER) where
2060 * TRAPDOOR_CARRIER is the default initial state for callout
2061 * devices and SOFT_CARRIER is like CLOCAL except it hides
2065 if ((IS_8251(com->pc98_if_type) &&
2066 (pc98_get_modem_status(com) & TIOCM_CAR)) ||
2067 (!IS_8251(com->pc98_if_type) &&
2068 (com->prev_modem_status & MSR_DCD)) ||
2069 mynor & CALLOUT_MASK)
2071 if (com->prev_modem_status & MSR_DCD || mynor & CALLOUT_MASK)
2073 (*linesw[tp->t_line].l_modem)(tp, 1);
2076 * Wait for DCD if necessary.
2078 if (!(tp->t_state & TS_CARR_ON) && !(mynor & CALLOUT_MASK)
2079 && !(tp->t_cflag & CLOCAL) && !(flag & O_NONBLOCK)) {
2081 error = tsleep(TSA_CARR_ON(tp), TTIPRI | PCATCH, "siodcd", 0);
2082 if (com_addr(unit) == NULL)
2085 if (error != 0 || com->gone)
2089 error = (*linesw[tp->t_line].l_open)(dev, tp);
2090 disc_optim(tp, &tp->t_termios, com);
2091 if (tp->t_state & TS_ISOPEN && mynor & CALLOUT_MASK)
2092 com->active_out = TRUE;
2096 if (!(tp->t_state & TS_ISOPEN) && com->wopeners == 0)
2102 sioclose(dev, flag, mode, p)
2114 if (mynor & CONTROL_MASK)
2116 com = com_addr(MINOR_TO_UNIT(mynor));
2119 (*linesw[tp->t_line].l_close)(tp, flag);
2121 com->modem_checking = 0;
2123 disc_optim(tp, &tp->t_termios, com);
2124 siostop(tp, FREAD | FWRITE);
2130 printf("sio%d: gone\n", com->unit);
2132 if (com->ibuf != NULL)
2133 free(com->ibuf, M_DEVBUF);
2134 bzero(tp,sizeof *tp);
2135 free(com, M_DEVBUF);
2154 iobase = com->iobase;
2157 com->poll_output = FALSE;
2158 com->do_timestamp = FALSE;
2159 com->do_dcd_timestamp = FALSE;
2160 com->pps.ppsparam.mode = 0;
2162 if (IS_8251(com->pc98_if_type))
2163 com_send_break_off(com);
2165 port_shift = if_16550a_type[com->pc98_if_type & 0x0f].port_shift;
2166 outb(iobase + (com_cfcr << port_shift),
2167 com->cfcr_image &= ~CFCR_SBREAK);
2170 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
2175 if (IS_8251(com->pc98_if_type))
2176 com_int_TxRx_disable(com);
2178 outb(iobase + (com_ier << port_shift), 0);
2179 if (com->pc98_if_type == COM_IF_RSA98III) {
2180 outb(com->rsabase + rsa_ier, 0x00);
2183 outb(iobase + com_ier, 0);
2187 if (IS_8251(com->pc98_if_type))
2188 tmp = pc98_get_modem_status(com) & TIOCM_CAR;
2190 tmp = com->prev_modem_status & MSR_DCD;
2192 if (tp->t_cflag & HUPCL
2194 * XXX we will miss any carrier drop between here and the
2195 * next open. Perhaps we should watch DCD even when the
2196 * port is closed; it is not sufficient to check it at
2197 * the next open because it might go up and down while
2198 * we're not watching.
2204 && !(com->prev_modem_status & MSR_DCD)
2206 && !(com->it_in.c_cflag & CLOCAL)
2207 || !(tp->t_state & TS_ISOPEN)) {
2209 if (IS_8251(com->pc98_if_type))
2210 com_tiocm_bic(com, TIOCM_DTR|TIOCM_RTS|TIOCM_LE);
2213 (void)commctl(com, TIOCM_DTR, DMBIC);
2214 if (com->dtr_wait != 0 && !(com->state & CS_DTR_OFF)) {
2215 timeout(siodtrwakeup, com, com->dtr_wait);
2216 com->state |= CS_DTR_OFF;
2221 if (IS_8251(com->pc98_if_type))
2222 com_tiocm_bic(com, TIOCM_LE );
2228 * Disable fifos so that they are off after controlled
2229 * reboots. Some BIOSes fail to detect 16550s when the
2230 * fifos are enabled.
2233 outb(iobase + (com_fifo << port_shift), 0);
2235 outb(iobase + com_fifo, 0);
2238 com->active_out = FALSE;
2239 wakeup(&com->active_out);
2240 wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */
2245 sioread(dev, uio, flag)
2255 if (mynor & CONTROL_MASK)
2257 unit = MINOR_TO_UNIT(mynor);
2258 if (com_addr(unit)->gone)
2260 tp = com_addr(unit)->tp;
2261 return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
2265 siowrite(dev, uio, flag)
2275 if (mynor & CONTROL_MASK)
2278 unit = MINOR_TO_UNIT(mynor);
2279 if (com_addr(unit)->gone)
2281 tp = com_addr(unit)->tp;
2283 * (XXX) We disallow virtual consoles if the physical console is
2284 * a serial port. This is in case there is a display attached that
2285 * is not the console. In that situation we don't need/want the X
2286 * server taking over the console.
2288 if (constty != NULL && unit == comconsole)
2290 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
2300 com = (struct com_s *)chan;
2303 * Clear TS_BUSY if low-level output is complete.
2304 * spl locking is sufficient because siointr1() does not set CS_BUSY.
2305 * If siointr1() clears CS_BUSY after we look at it, then we'll get
2306 * called again. Reading the line status port outside of siointr1()
2307 * is safe because CS_BUSY is clear so there are no output interrupts
2311 if (com->state & CS_BUSY)
2312 com->extra_state &= ~CSE_BUSYCHECK; /* False alarm. */
2314 else if ((IS_8251(com->pc98_if_type) &&
2315 (inb(com->sts_port) & (STS8251_TxRDY | STS8251_TxEMP))
2316 == (STS8251_TxRDY | STS8251_TxEMP)) ||
2317 (inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY))
2318 == (LSR_TSRE | LSR_TXRDY)) {
2320 else if ((inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY))
2321 == (LSR_TSRE | LSR_TXRDY)) {
2323 com->tp->t_state &= ~TS_BUSY;
2325 com->extra_state &= ~CSE_BUSYCHECK;
2327 timeout(siobusycheck, com, hz / 100);
2337 com = (struct com_s *)chan;
2338 com->state &= ~CS_DTR_OFF;
2339 wakeup(&com->dtr_wait);
2357 if (!(tp->t_state & TS_ISOPEN) || !(tp->t_cflag & CREAD)) {
2358 com_events -= (com->iptr - com->ibuf);
2359 com->iptr = com->ibuf;
2362 if (tp->t_state & TS_CAN_BYPASS_L_RINT) {
2364 * Avoid the grotesquely inefficient lineswitch routine
2365 * (ttyinput) in "raw" mode. It usually takes about 450
2366 * instructions (that's without canonical processing or echo!).
2367 * slinput is reasonably fast (usually 40 instructions plus
2372 incc = com->iptr - buf;
2373 if (tp->t_rawq.c_cc + incc > tp->t_ihiwat
2374 && (com->state & CS_RTS_IFLOW
2375 || tp->t_iflag & IXOFF)
2376 && !(tp->t_state & TS_TBLOCK))
2378 com->delta_error_counts[CE_TTY_BUF_OVERFLOW]
2379 += b_to_q((char *)buf, incc, &tp->t_rawq);
2383 tp->t_rawcc += incc;
2385 if (tp->t_state & TS_TTSTOP
2386 && (tp->t_iflag & IXANY
2387 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) {
2388 tp->t_state &= ~TS_TTSTOP;
2389 tp->t_lflag &= ~FLUSHO;
2393 } while (buf < com->iptr);
2397 line_status = buf[com->ierroff];
2400 & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) {
2401 if (line_status & LSR_BI)
2402 recv_data |= TTY_BI;
2403 if (line_status & LSR_FE)
2404 recv_data |= TTY_FE;
2405 if (line_status & LSR_OE)
2406 recv_data |= TTY_OE;
2407 if (line_status & LSR_PE)
2408 recv_data |= TTY_PE;
2410 (*linesw[tp->t_line].l_rint)(recv_data, tp);
2412 } while (buf < com->iptr);
2414 com_events -= (com->iptr - com->ibuf);
2415 com->iptr = com->ibuf;
2418 * There is now room for another low-level buffer full of input,
2419 * so enable RTS if it is now disabled and there is room in the
2420 * high-level buffer.
2423 if (IS_8251(com->pc98_if_type))
2424 tmp = com_tiocm_get(com) & TIOCM_RTS;
2426 tmp = com->mcr_image & MCR_RTS;
2427 if ((com->state & CS_RTS_IFLOW) && !(tmp) &&
2428 !(tp->t_state & TS_TBLOCK))
2429 if (IS_8251(com->pc98_if_type))
2430 com_tiocm_bis(com, TIOCM_RTS);
2432 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2434 if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & MCR_RTS) &&
2435 !(tp->t_state & TS_TBLOCK))
2436 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
2444 #ifndef COM_MULTIPORT
2446 siointr1((struct com_s *) arg);
2448 #else /* COM_MULTIPORT */
2449 bool_t possibly_more_intrs;
2451 u_char rsa_buf_status;
2455 * Loop until there is no activity on any port. This is necessary
2456 * to get an interrupt edge more than to avoid another interrupt.
2457 * If the IRQ signal is just an OR of the IRQ signals from several
2458 * devices, then the edge from one may be lost because another is
2463 possibly_more_intrs = FALSE;
2464 for (unit = 0; unit < NSIOTOT; ++unit) {
2465 com = com_addr(unit);
2468 * would it work here, or be counter-productive?
2473 && IS_8251(com->pc98_if_type)){
2480 && com->pc98_if_type == COM_IF_RSA98III) {
2481 rsa_buf_status = inb(com->rsabase + rsa_srr) & 0xc9;
2482 if ((rsa_buf_status & 0xc8)
2483 || !(rsa_buf_status & 0x01)) {
2486 != (inb(com->rsabase + rsa_srr) & 0xc9))
2487 possibly_more_intrs = TRUE;
2493 && (inb(com->int_id_port) & IIR_IMASK)
2496 possibly_more_intrs = TRUE;
2498 /* XXX COM_UNLOCK(); */
2500 } while (possibly_more_intrs);
2502 #endif /* COM_MULTIPORT */
2510 u_char modem_status;
2515 struct timecounter *tc;
2520 u_char rsa_buf_status = 0;
2521 int rsa_tx_fifo_size=0;
2525 int_ctl = inb(com->intr_ctl_port);
2526 int_ctl_new = int_ctl;
2528 while (!com->gone) {
2531 if (IS_8251(com->pc98_if_type)) {
2532 tmp = inb(com->sts_port);
2535 if (tmp & STS8251_TxRDY) line_status |= LSR_TXRDY;
2536 if (tmp & STS8251_RxRDY) line_status |= LSR_RXRDY;
2537 if (tmp & STS8251_TxEMP) line_status |= LSR_TSRE;
2538 if (tmp & STS8251_PE) line_status |= LSR_PE;
2539 if (tmp & STS8251_OE) line_status |= LSR_OE;
2540 if (tmp & STS8251_FE) line_status |= LSR_FE;
2541 if (tmp & STS8251_BD_SD) line_status |= LSR_BI;
2544 if (com->pps.ppsparam.mode & PPS_CAPTUREBOTH) {
2545 modem_status = inb(com->modem_status_port);
2546 if ((modem_status ^ com->last_modem_status) & MSR_DCD) {
2548 count = tc->tc_get_timecount(tc);
2549 pps_event(&com->pps, tc, count,
2550 (modem_status & MSR_DCD) ?
2551 PPS_CAPTURECLEAR : PPS_CAPTUREASSERT);
2554 line_status = inb(com->line_status_port);
2556 if (com->pc98_if_type == COM_IF_RSA98III)
2557 rsa_buf_status = inb(com->rsabase + rsa_srr);
2560 /* input event? (check first to help avoid overruns) */
2562 while (line_status & LSR_RCV_MASK) {
2564 while ((line_status & LSR_RCV_MASK)
2565 || (com->pc98_if_type == COM_IF_RSA98III
2566 && (rsa_buf_status & 0x08))) {
2568 /* break/unnattached error bits or real input? */
2570 if (IS_8251(com->pc98_if_type)) {
2571 recv_data = inb(com->data_port);
2573 pc98_i8251_or_cmd(com,CMD8251_ER);
2579 if (com->pc98_if_type == COM_IF_RSA98III) {
2580 if (!(rsa_buf_status & 0x08))
2583 recv_data = inb(com->data_port);
2587 if (!(line_status & LSR_RXRDY))
2590 recv_data = inb(com->data_port);
2594 if (line_status & (LSR_BI | LSR_FE | LSR_PE)) {
2596 * Don't store BI if IGNBRK or FE/PE if IGNPAR.
2597 * Otherwise, push the work to a higher level
2598 * (to handle PARMRK) if we're bypassing.
2599 * Otherwise, convert BI/FE and PE+INPCK to 0.
2601 * This makes bypassing work right in the
2602 * usual "raw" case (IGNBRK set, and IGNPAR
2605 * Note: BI together with FE/PE means just BI.
2607 if (line_status & LSR_BI) {
2608 #if defined(DDB) && defined(BREAK_TO_DEBUGGER)
2609 if (com->unit == comconsole) {
2615 || com->tp->t_iflag & IGNBRK)
2619 || com->tp->t_iflag & IGNPAR)
2622 if (com->tp->t_state & TS_CAN_BYPASS_L_RINT
2623 && (line_status & (LSR_BI | LSR_FE)
2624 || com->tp->t_iflag & INPCK))
2628 if (com->hotchar != 0 && recv_data == com->hotchar)
2631 if (ioptr >= com->ibufend)
2632 CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW);
2634 if (com->do_timestamp)
2635 microtime(&com->timestamp);
2638 #if 0 /* for testing input latency vs efficiency */
2639 if (com->iptr - com->ibuf == 8)
2642 ioptr[0] = recv_data;
2643 ioptr[com->ierroff] = line_status;
2644 com->iptr = ++ioptr;
2645 if (ioptr == com->ihighwater
2646 && com->state & CS_RTS_IFLOW)
2648 if (IS_8251(com->pc98_if_type))
2649 com_tiocm_bic(com, TIOCM_RTS);
2652 outb(com->modem_ctl_port,
2653 com->mcr_image &= ~MCR_RTS);
2654 if (line_status & LSR_OE)
2655 CE_RECORD(com, CE_OVERRUN);
2659 * "& 0x7F" is to avoid the gcc-1.40 generating a slow
2660 * jump from the top of the loop to here
2663 if (IS_8251(com->pc98_if_type))
2667 line_status = inb(com->line_status_port) & 0x7F;
2669 if (com->pc98_if_type == COM_IF_RSA98III)
2670 rsa_buf_status = inb(com->rsabase + rsa_srr);
2674 /* modem status change? (always check before doing output) */
2676 if (!IS_8251(com->pc98_if_type)) {
2678 modem_status = inb(com->modem_status_port);
2679 if (modem_status != com->last_modem_status) {
2680 if (com->do_dcd_timestamp
2681 && !(com->last_modem_status & MSR_DCD)
2682 && modem_status & MSR_DCD)
2683 microtime(&com->dcd_timestamp);
2686 * Schedule high level to handle DCD changes. Note
2687 * that we don't use the delta bits anywhere. Some
2688 * UARTs mess them up, and it's easy to remember the
2689 * previous bits and calculate the delta.
2691 com->last_modem_status = modem_status;
2692 if (!(com->state & CS_CHECKMSR)) {
2693 com_events += LOTS_OF_EVENTS;
2694 com->state |= CS_CHECKMSR;
2698 /* handle CTS change immediately for crisp flow ctl */
2699 if (com->state & CS_CTS_OFLOW) {
2700 if (modem_status & MSR_CTS)
2701 com->state |= CS_ODEVREADY;
2703 com->state &= ~CS_ODEVREADY;
2710 /* output queued and everything ready? */
2712 if (line_status & LSR_TXRDY
2713 && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
2715 if (((com->pc98_if_type == COM_IF_RSA98III)
2716 ? (rsa_buf_status & 0x02)
2717 : (line_status & LSR_TXRDY))
2718 && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) {
2720 ioptr = com->obufq.l_head;
2721 if (com->tx_fifo_size > 1) {
2724 ocount = com->obufq.l_tail - ioptr;
2726 if (com->pc98_if_type == COM_IF_RSA98III) {
2727 rsa_buf_status = inb(com->rsabase + rsa_srr);
2728 rsa_tx_fifo_size = 1024;
2729 if (!(rsa_buf_status & 0x01))
2730 rsa_tx_fifo_size = 2048;
2731 if (ocount > rsa_tx_fifo_size)
2732 ocount = rsa_tx_fifo_size;
2735 if (ocount > com->tx_fifo_size)
2736 ocount = com->tx_fifo_size;
2737 com->bytes_out += ocount;
2739 outb(com->data_port, *ioptr++);
2740 while (--ocount != 0);
2742 outb(com->data_port, *ioptr++);
2746 if (IS_8251(com->pc98_if_type))
2747 if (!(pc98_check_i8251_interrupt(com) & IEN_TxFLAG))
2748 com_int_Tx_enable(com);
2750 com->obufq.l_head = ioptr;
2751 if (COM_IIR_TXRDYBUG(com->flags)) {
2752 int_ctl_new = int_ctl | IER_ETXRDY;
2754 if (ioptr >= com->obufq.l_tail) {
2757 qp = com->obufq.l_next;
2758 qp->l_queued = FALSE;
2761 com->obufq.l_head = qp->l_head;
2762 com->obufq.l_tail = qp->l_tail;
2763 com->obufq.l_next = qp;
2765 /* output just completed */
2766 if ( COM_IIR_TXRDYBUG(com->flags) ) {
2767 int_ctl_new = int_ctl & ~IER_ETXRDY;
2769 com->state &= ~CS_BUSY;
2771 if (IS_8251(com->pc98_if_type))
2772 if ( pc98_check_i8251_interrupt(com) & IEN_TxFLAG )
2773 com_int_Tx_disable(com);
2776 if (!(com->state & CS_ODONE)) {
2777 com_events += LOTS_OF_EVENTS;
2778 com->state |= CS_ODONE;
2779 setsofttty(); /* handle at high level ASAP */
2782 if ( COM_IIR_TXRDYBUG(com->flags) && (int_ctl != int_ctl_new)) {
2784 if (com->pc98_if_type == COM_IF_RSA98III) {
2785 int_ctl_new &= ~(IER_ETXRDY | IER_ERXRDY);
2786 outb(com->intr_ctl_port, int_ctl_new);
2787 outb(com->rsabase + rsa_ier, 0x1d);
2790 outb(com->intr_ctl_port, int_ctl_new);
2794 else if (line_status & LSR_TXRDY) {
2795 if (IS_8251(com->pc98_if_type))
2796 if ( pc98_check_i8251_interrupt(com) & IEN_TxFLAG )
2797 com_int_Tx_disable(com);
2799 if (IS_8251(com->pc98_if_type))
2800 if ((tmp = inb(com->sts_port)) & STS8251_RxRDY)
2805 #ifndef COM_MULTIPORT
2807 if (IS_8251(com->pc98_if_type))
2810 if ((inb(com->int_id_port) & IIR_IMASK) == IIR_NOPEND)
2811 #endif /* COM_MULTIPORT */
2817 sioioctl(dev, cmd, data, flag, p)
2830 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2832 struct termios term;
2836 com = com_addr(MINOR_TO_UNIT(mynor));
2839 iobase = com->iobase;
2840 if (mynor & CONTROL_MASK) {
2843 switch (mynor & CONTROL_MASK) {
2844 case CONTROL_INIT_STATE:
2845 ct = mynor & CALLOUT_MASK ? &com->it_out : &com->it_in;
2847 case CONTROL_LOCK_STATE:
2848 ct = mynor & CALLOUT_MASK ? &com->lt_out : &com->lt_in;
2851 return (ENODEV); /* /dev/nodev */
2855 error = suser(p->p_ucred, &p->p_acflag);
2858 *ct = *(struct termios *)data;
2861 *(struct termios *)data = *ct;
2864 *(int *)data = TTYDISC;
2867 bzero(data, sizeof(struct winsize));
2874 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
2875 term = tp->t_termios;
2877 error = ttsetcompat(tp, &cmd, data, &term);
2881 data = (caddr_t)&term;
2883 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
2885 struct termios *dt = (struct termios *)data;
2886 struct termios *lt = mynor & CALLOUT_MASK
2887 ? &com->lt_out : &com->lt_in;
2889 dt->c_iflag = (tp->t_iflag & lt->c_iflag)
2890 | (dt->c_iflag & ~lt->c_iflag);
2891 dt->c_oflag = (tp->t_oflag & lt->c_oflag)
2892 | (dt->c_oflag & ~lt->c_oflag);
2893 dt->c_cflag = (tp->t_cflag & lt->c_cflag)
2894 | (dt->c_cflag & ~lt->c_cflag);
2895 dt->c_lflag = (tp->t_lflag & lt->c_lflag)
2896 | (dt->c_lflag & ~lt->c_lflag);
2897 for (cc = 0; cc < NCCS; ++cc)
2898 if (lt->c_cc[cc] != 0)
2899 dt->c_cc[cc] = tp->t_cc[cc];
2900 if (lt->c_ispeed != 0)
2901 dt->c_ispeed = tp->t_ispeed;
2902 if (lt->c_ospeed != 0)
2903 dt->c_ospeed = tp->t_ospeed;
2905 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
2906 if (error != ENOIOCTL)
2909 error = ttioctl(tp, cmd, data, flag);
2910 disc_optim(tp, &tp->t_termios, com);
2911 if (error != ENOIOCTL) {
2916 if (IS_8251(com->pc98_if_type)) {
2919 com_send_break_on( com );
2922 com_send_break_off( com );
2925 com_tiocm_bis(com, TIOCM_DTR | TIOCM_RTS );
2928 com_tiocm_bic(com, TIOCM_DTR);
2931 * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The
2932 * changes get undone on the next call to comparam().
2935 com_tiocm_set( com, *(int *)data );
2938 com_tiocm_bis( com, *(int *)data );
2941 com_tiocm_bic( com, *(int *)data );
2944 *(int *)data = com_tiocm_get(com);
2947 /* must be root since the wait applies to following logins */
2948 error = suser(p->p_ucred, &p->p_acflag);
2953 com->dtr_wait = *(int *)data * hz / 100;
2956 *(int *)data = com->dtr_wait * 100 / hz;
2959 com->do_timestamp = TRUE;
2960 *(struct timeval *)data = com->timestamp;
2962 case TIOCDCDTIMESTAMP:
2963 com->do_dcd_timestamp = TRUE;
2964 *(struct timeval *)data = com->dcd_timestamp;
2972 port_shift = if_16550a_type[com->pc98_if_type & 0x0f].port_shift;
2977 outb(iobase + (com_cfcr << port_shift),
2978 com->cfcr_image |= CFCR_SBREAK);
2980 outb(iobase + com_cfcr, com->cfcr_image |= CFCR_SBREAK);
2985 outb(iobase + (com_cfcr << port_shift),
2986 com->cfcr_image &= ~CFCR_SBREAK);
2988 outb(iobase + com_cfcr, com->cfcr_image &= ~CFCR_SBREAK);
2992 (void)commctl(com, TIOCM_DTR, DMBIS);
2995 (void)commctl(com, TIOCM_DTR, DMBIC);
2998 * XXX should disallow changing MCR_RTS if CS_RTS_IFLOW is set. The
2999 * changes get undone on the next call to comparam().
3002 (void)commctl(com, *(int *)data, DMSET);
3005 (void)commctl(com, *(int *)data, DMBIS);
3008 (void)commctl(com, *(int *)data, DMBIC);
3011 *(int *)data = commctl(com, 0, DMGET);
3014 /* must be root since the wait applies to following logins */
3015 error = suser(p->p_ucred, &p->p_acflag);
3020 com->dtr_wait = *(int *)data * hz / 100;
3023 *(int *)data = com->dtr_wait * 100 / hz;
3026 com->do_timestamp = TRUE;
3027 *(struct timeval *)data = com->timestamp;
3029 case TIOCDCDTIMESTAMP:
3030 com->do_dcd_timestamp = TRUE;
3031 *(struct timeval *)data = com->dcd_timestamp;
3035 error = pps_ioctl(cmd, data, &com->pps);
3036 if (error == ENODEV)
3052 if (com_events == 0)
3055 for (unit = 0; unit < NSIOTOT; ++unit) {
3060 com = com_addr(unit);
3064 if (tp == NULL || com->gone) {
3066 * Discard any events related to never-opened or
3067 * going-away devices.
3070 incc = com->iptr - com->ibuf;
3071 com->iptr = com->ibuf;
3072 if (com->state & CS_CHECKMSR) {
3073 incc += LOTS_OF_EVENTS;
3074 com->state &= ~CS_CHECKMSR;
3080 if (com->iptr != com->ibuf) {
3085 if (com->state & CS_CHECKMSR) {
3086 u_char delta_modem_status;
3089 if (!IS_8251(com->pc98_if_type)) {
3092 delta_modem_status = com->last_modem_status
3093 ^ com->prev_modem_status;
3094 com->prev_modem_status = com->last_modem_status;
3095 com_events -= LOTS_OF_EVENTS;
3096 com->state &= ~CS_CHECKMSR;
3098 if (delta_modem_status & MSR_DCD)
3099 (*linesw[tp->t_line].l_modem)
3100 (tp, com->prev_modem_status & MSR_DCD);
3105 if (com->state & CS_ODONE) {
3107 com_events -= LOTS_OF_EVENTS;
3108 com->state &= ~CS_ODONE;
3110 if (!(com->state & CS_BUSY)
3111 && !(com->extra_state & CSE_BUSYCHECK)) {
3112 timeout(siobusycheck, com, hz / 100);
3113 com->extra_state |= CSE_BUSYCHECK;
3115 (*linesw[tp->t_line].l_start)(tp);
3117 if (com_events == 0)
3120 if (com_events >= LOTS_OF_EVENTS)
3145 unit = DEV_TO_UNIT(tp->t_dev);
3146 com = com_addr(unit);
3147 iobase = com->iobase;
3148 if (IS_8251(com->pc98_if_type)) {
3149 divisor = pc98_ttspeedtab(com, t->c_ospeed);
3151 port_shift = if_16550a_type[com->pc98_if_type & 0x0f].port_shift;
3153 /* do historical conversions */
3154 if (t->c_ispeed == 0)
3155 t->c_ispeed = t->c_ospeed;
3157 /* check requested parameters */
3158 divisor = ttspeedtab(t->c_ospeed,
3159 if_16550a_type[com->pc98_if_type & 0x0f].speedtab);
3162 /* do historical conversions */
3163 if (t->c_ispeed == 0)
3164 t->c_ispeed = t->c_ospeed;
3166 /* check requested parameters */
3167 divisor = ttspeedtab(t->c_ospeed, comspeedtab);
3169 if (divisor < 0 || divisor > 0 && t->c_ispeed != t->c_ospeed)
3172 /* parameters are OK, convert them to the com struct and the device */
3174 unit = DEV_TO_UNIT(tp->t_dev);
3175 com = com_addr(unit);
3176 iobase = com->iobase;
3180 if (IS_8251(com->pc98_if_type)) {
3182 com_tiocm_bic( com, TIOCM_DTR|TIOCM_RTS|TIOCM_LE );
3184 com_tiocm_bis( com, TIOCM_DTR|TIOCM_RTS|TIOCM_LE );
3188 (void)commctl(com, TIOCM_DTR, DMBIC); /* hang up line */
3190 (void)commctl(com, TIOCM_DTR, DMBIS);
3196 if (!IS_8251(com->pc98_if_type)) {
3198 switch (cflag & CSIZE) {
3212 if (cflag & PARENB) {
3214 if (!(cflag & PARODD))
3220 if (com->hasfifo && divisor != 0) {
3222 * Use a fifo trigger level low enough so that the input
3223 * latency from the fifo is less than about 16 msec and
3224 * the total latency is less than about 30 msec. These
3225 * latencies are reasonable for humans. Serial comms
3226 * protocols shouldn't expect anything better since modem
3227 * latencies are larger.
3229 com->fifo_image = t->c_ospeed <= 4800
3230 ? FIFO_ENABLE : FIFO_ENABLE | FIFO_RX_HIGH;
3233 * The Hayes ESP card needs the fifo DMA mode bit set
3234 * in compatibility mode. If not, it will interrupt
3235 * for each character received.
3238 com->fifo_image |= FIFO_DMA_MODE;
3241 outb(iobase + (com_fifo << port_shift), com->fifo_image);
3243 outb(iobase + com_fifo, com->fifo_image);
3251 * This returns with interrupts disabled so that we can complete
3252 * the speed change atomically. Keeping interrupts disabled is
3253 * especially important while com_data is hidden.
3255 (void) siosetwater(com, t->c_ispeed);
3258 if (IS_8251(com->pc98_if_type))
3259 com_cflag_and_speed_set(com, cflag, t->c_ospeed);
3264 outb(iobase + (com_cfcr << port_shift), cfcr | CFCR_DLAB);
3266 outb(iobase + com_cfcr, cfcr | CFCR_DLAB);
3269 * Only set the divisor registers if they would change,
3270 * since on some 16550 incompatibles (UMC8669F), setting
3271 * them while input is arriving them loses sync until
3272 * data stops arriving.
3274 dlbl = divisor & 0xFF;
3276 if (inb(iobase + (com_dlbl << port_shift)) != dlbl)
3277 outb(iobase + (com_dlbl << port_shift), dlbl);
3278 dlbh = (u_int) divisor >> 8;
3279 if (inb(iobase + (com_dlbh << port_shift)) != dlbh)
3280 outb(iobase + (com_dlbh << port_shift), dlbh);
3282 if (inb(iobase + com_dlbl) != dlbl)
3283 outb(iobase + com_dlbl, dlbl);
3284 dlbh = (u_int) divisor >> 8;
3285 if (inb(iobase + com_dlbh) != dlbh)
3286 outb(iobase + com_dlbh, dlbh);
3293 outb(iobase + (com_cfcr << port_shift), com->cfcr_image = cfcr);
3295 outb(iobase + com_cfcr, com->cfcr_image = cfcr);
3298 if (!(tp->t_state & TS_TTSTOP))
3299 com->state |= CS_TTGO;
3301 if (cflag & CRTS_IFLOW) {
3302 if (com->st16650a) {
3303 outb(iobase + com_cfcr, 0xbf);
3304 outb(iobase + com_fifo, inb(iobase + com_fifo) | 0x40);
3306 com->state |= CS_RTS_IFLOW;
3308 * If CS_RTS_IFLOW just changed from off to on, the change
3309 * needs to be propagated to MCR_RTS. This isn't urgent,
3310 * so do it later by calling comstart() instead of repeating
3311 * a lot of code from comstart() here.
3313 } else if (com->state & CS_RTS_IFLOW) {
3314 com->state &= ~CS_RTS_IFLOW;
3316 * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS
3317 * on here, since comstart() won't do it later.
3320 if (IS_8251(com->pc98_if_type))
3321 com_tiocm_bis(com, TIOCM_RTS);
3324 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
3325 if (com->st16650a) {
3326 outb(iobase + com_cfcr, 0xbf);
3327 outb(iobase + com_fifo, inb(iobase + com_fifo) & ~0x40);
3333 * Set up state to handle output flow control.
3334 * XXX - worth handling MDMBUF (DCD) flow control at the lowest level?
3335 * Now has 10+ msec latency, while CTS flow has 50- usec latency.
3337 com->state |= CS_ODEVREADY;
3338 com->state &= ~CS_CTS_OFLOW;
3340 if (com->pc98_if_type == COM_IF_RSA98III) {
3341 param = inb(com->rsabase + rsa_msr);
3342 outb(com->rsabase + rsa_msr, param & 0x14);
3345 if (cflag & CCTS_OFLOW) {
3346 com->state |= CS_CTS_OFLOW;
3348 if (IS_8251(com->pc98_if_type)) {
3349 if (!(pc98_get_modem_status(com) & TIOCM_CTS))
3350 com->state &= ~CS_ODEVREADY;
3354 if (com->pc98_if_type == COM_IF_RSA98III) {
3355 /* Set automatic flow control mode */
3356 outb(com->rsabase + rsa_msr, param | 0x08);
3359 if (!(com->last_modem_status & MSR_CTS))
3360 com->state &= ~CS_ODEVREADY;
3361 if (com->st16650a) {
3362 outb(iobase + com_cfcr, 0xbf);
3363 outb(iobase + com_fifo, inb(iobase + com_fifo) | 0x80);
3369 if (com->st16650a) {
3370 outb(iobase + com_cfcr, 0xbf);
3371 outb(iobase + com_fifo, inb(iobase + com_fifo) & ~0x80);
3377 outb(iobase + (com_cfcr << port_shift), com->cfcr_image);
3379 outb(iobase + com_cfcr, com->cfcr_image);
3383 /* XXX shouldn't call functions while intrs are disabled. */
3384 disc_optim(tp, t, com);
3386 * Recover from fiddling with CS_TTGO. We used to call siointr1()
3387 * unconditionally, but that defeated the careful discarding of
3388 * stale input in sioopen().
3390 if (com->state >= (CS_BUSY | CS_TTGO))
3396 if (com->ibufold != NULL) {
3397 free(com->ibufold, M_DEVBUF);
3398 com->ibufold = NULL;
3404 siosetwater(com, speed)
3414 * Make the buffer size large enough to handle a softtty interrupt
3415 * latency of about 2 ticks without loss of throughput or data
3416 * (about 3 ticks if input flow control is not used or not honoured,
3417 * but a bit less for CS5-CS7 modes).
3419 cp4ticks = speed / 10 / hz * 4;
3420 for (ibufsize = 128; ibufsize < cp4ticks;)
3423 if (com->pc98_if_type == COM_IF_RSA98III)
3426 if (ibufsize == com->ibufsize) {
3432 * Allocate input buffer. The extra factor of 2 in the size is
3433 * to allow for an error byte for each input byte.
3435 ibuf = malloc(2 * ibufsize, M_DEVBUF, M_NOWAIT);
3441 /* Initialize non-critical variables. */
3442 com->ibufold = com->ibuf;
3443 com->ibufsize = ibufsize;
3446 tp->t_ififosize = 2 * ibufsize;
3447 tp->t_ispeedwat = (speed_t)-1;
3448 tp->t_ospeedwat = (speed_t)-1;
3452 * Read current input buffer, if any. Continue with interrupts
3456 if (com->iptr != com->ibuf)
3460 * Initialize critical variables, including input buffer watermarks.
3461 * The external device is asked to stop sending when the buffer
3462 * exactly reaches high water, or when the high level requests it.
3463 * The high level is notified immediately (rather than at a later
3464 * clock tick) when this watermark is reached.
3465 * The buffer size is chosen so the watermark should almost never
3467 * The low watermark is invisibly 0 since the buffer is always
3468 * emptied all at once.
3470 com->iptr = com->ibuf = ibuf;
3471 com->ibufend = ibuf + ibufsize;
3472 com->ierroff = ibufsize;
3473 com->ihighwater = ibuf + 3 * ibufsize / 4;
3488 unit = DEV_TO_UNIT(tp->t_dev);
3489 com = com_addr(unit);
3492 if (tp->t_state & TS_TTSTOP)
3493 com->state &= ~CS_TTGO;
3495 com->state |= CS_TTGO;
3496 if (tp->t_state & TS_TBLOCK) {
3498 if (IS_8251(com->pc98_if_type))
3499 tmp = com_tiocm_get(com) & TIOCM_RTS;
3501 tmp = com->mcr_image & MCR_RTS;
3502 if (tmp && (com->state & CS_RTS_IFLOW))
3504 if (com->mcr_image & MCR_RTS && com->state & CS_RTS_IFLOW)
3507 if (IS_8251(com->pc98_if_type))
3508 com_tiocm_bic(com, TIOCM_RTS);
3511 outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS);
3514 if (IS_8251(com->pc98_if_type))
3515 tmp = com_tiocm_get(com) & TIOCM_RTS;
3517 tmp = com->mcr_image & MCR_RTS;
3518 if (!(tmp) && com->iptr < com->ihighwater
3519 && com->state & CS_RTS_IFLOW)
3521 if (!(com->mcr_image & MCR_RTS) && com->iptr < com->ihighwater
3522 && com->state & CS_RTS_IFLOW)
3525 if (IS_8251(com->pc98_if_type))
3526 com_tiocm_bis(com, TIOCM_RTS);
3529 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS);
3532 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) {
3535 /* if(IS_8251(com->pc98_if_type))
3536 com_int_Tx_enable(com); */
3541 if (tp->t_outq.c_cc != 0) {
3545 if (!com->obufs[0].l_queued) {
3546 com->obufs[0].l_tail
3547 = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1,
3553 com->obufs[0].l_next = NULL;
3554 com->obufs[0].l_queued = TRUE;
3556 if (com->state & CS_BUSY) {
3557 qp = com->obufq.l_next;
3558 while ((next = qp->l_next) != NULL)
3560 qp->l_next = &com->obufs[0];
3562 com->obufq.l_head = com->obufs[0].l_head;
3563 com->obufq.l_tail = com->obufs[0].l_tail;
3564 com->obufq.l_next = &com->obufs[0];
3565 com->state |= CS_BUSY;
3569 if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) {
3570 com->obufs[1].l_tail
3571 = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2,
3577 com->obufs[1].l_next = NULL;
3578 com->obufs[1].l_queued = TRUE;
3580 if (com->state & CS_BUSY) {
3581 qp = com->obufq.l_next;
3582 while ((next = qp->l_next) != NULL)
3584 qp->l_next = &com->obufs[1];
3586 com->obufq.l_head = com->obufs[1].l_head;
3587 com->obufq.l_tail = com->obufs[1].l_tail;
3588 com->obufq.l_next = &com->obufs[1];
3589 com->state |= CS_BUSY;
3593 tp->t_state |= TS_BUSY;
3596 if (com->state >= (CS_BUSY | CS_TTGO))
3597 siointr1(com); /* fake interrupt to start output */
3600 /* if(IS_8251(com->pc98_if_type))
3601 com_int_Tx_enable(com); */
3618 com = com_addr(DEV_TO_UNIT(tp->t_dev));
3622 if (IS_8251(com->pc98_if_type))
3623 port_shift = if_16550a_type[com->pc98_if_type & 0x0f].port_shift;
3629 /* XXX avoid h/w bug. */
3633 outb(com->iobase + (com_fifo << port_shift),
3634 FIFO_XMT_RST | com->fifo_image);
3635 if (com->pc98_if_type == COM_IF_RSA98III)
3636 for(rsa98_tmp = 0; rsa98_tmp < 2048; rsa98_tmp++)
3637 outb(com->iobase + (com_fifo << port_shift),
3638 FIFO_XMT_RST | com->fifo_image);
3640 outb(com->iobase + com_fifo,
3641 FIFO_XMT_RST | com->fifo_image);
3643 com->obufs[0].l_queued = FALSE;
3644 com->obufs[1].l_queued = FALSE;
3645 if (com->state & CS_ODONE)
3646 com_events -= LOTS_OF_EVENTS;
3647 com->state &= ~(CS_ODONE | CS_BUSY);
3648 com->tp->t_state &= ~TS_BUSY;
3653 /* XXX avoid h/w bug. */
3657 if (com->pc98_if_type == COM_IF_RSA98III) {
3658 for(rsa98_tmp = 0; rsa98_tmp < 2048; rsa98_tmp++)
3659 inb(com->data_port);
3661 outb(com->iobase + (com_fifo << port_shift),
3662 FIFO_RCV_RST | com->fifo_image);
3664 outb(com->iobase + com_fifo,
3665 FIFO_RCV_RST | com->fifo_image);
3667 com_events -= (com->iptr - com->ibuf);
3668 com->iptr = com->ibuf;
3682 if (mynor & CONTROL_MASK)
3684 unit = MINOR_TO_UNIT(mynor);
3685 if ((u_int) unit >= NSIOTOT)
3687 return (&sio_tty[unit]);
3691 commctl(com, bits, how)
3700 bits = TIOCM_LE; /* XXX - always enabled while open */
3701 mcr = com->mcr_image;
3706 msr = com->prev_modem_status;
3714 * XXX - MSR_RI is naturally volatile, and we make MSR_TERI
3715 * more volatile by reading the modem status a lot. Perhaps
3716 * we should latch both bits until the status is read here.
3718 if (msr & (MSR_RI | MSR_TERI))
3723 if (bits & TIOCM_DTR)
3725 if (bits & TIOCM_RTS)
3732 outb(com->modem_ctl_port,
3733 com->mcr_image = mcr | (com->mcr_image & MCR_IENABLE));
3736 outb(com->modem_ctl_port, com->mcr_image |= mcr);
3739 outb(com->modem_ctl_port, com->mcr_image &= ~mcr);
3754 * Set our timeout period to 1 second if no polled devices are open.
3755 * Otherwise set it to max(1/200, 1/hz).
3756 * Enable timeouts iff some device is open.
3758 untimeout(comwakeup, (void *)NULL, sio_timeout_handle);
3761 for (unit = 0; unit < NSIOTOT; ++unit) {
3762 com = com_addr(unit);
3763 if (com != NULL && com->tp != NULL
3764 && com->tp->t_state & TS_ISOPEN && !com->gone) {
3766 if (com->poll || com->poll_output) {
3767 sio_timeout = hz > 200 ? hz / 200 : 1;
3773 sio_timeouts_until_log = hz / sio_timeout;
3774 sio_timeout_handle = timeout(comwakeup, (void *)NULL,
3777 /* Flush error messages, if any. */
3778 sio_timeouts_until_log = 1;
3779 comwakeup((void *)NULL);
3780 untimeout(comwakeup, (void *)NULL, sio_timeout_handle);
3791 sio_timeout_handle = timeout(comwakeup, (void *)NULL, sio_timeout);
3794 * Recover from lost output interrupts.
3795 * Poll any lines that don't use interrupts.
3797 for (unit = 0; unit < NSIOTOT; ++unit) {
3798 com = com_addr(unit);
3799 if (com != NULL && !com->gone
3800 && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) {
3808 * Check for and log errors, but not too often.
3810 if (--sio_timeouts_until_log > 0)
3812 sio_timeouts_until_log = hz / sio_timeout;
3813 for (unit = 0; unit < NSIOTOT; ++unit) {
3816 com = com_addr(unit);
3821 for (errnum = 0; errnum < CE_NTYPES; ++errnum) {
3826 delta = com->delta_error_counts[errnum];
3827 com->delta_error_counts[errnum] = 0;
3831 total = com->error_counts[errnum] += delta;
3832 log(LOG_ERR, "sio%d: %u more %s%s (total %lu)\n",
3833 unit, delta, error_desc[errnum],
3834 delta == 1 ? "" : "s", total);
3840 /* commint is called when modem control line changes */
3844 register struct tty *tp;
3850 unit = MINOR_TO_UNIT(mynor);
3851 com = com_addr(unit);
3854 stat = com_tiocm_get(com);
3855 delta = com_tiocm_get_delta(com);
3857 if (com->state & CS_CTS_OFLOW) {
3858 if (stat & TIOCM_CTS)
3859 com->state |= CS_ODEVREADY;
3861 com->state &= ~CS_ODEVREADY;
3863 if ((delta & TIOCM_CAR) && (mynor & CALLOUT_MASK) == 0) {
3864 if (stat & TIOCM_CAR )
3865 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
3866 else if ((*linesw[tp->t_line].l_modem)(tp, 0) == 0) {
3867 /* negate DTR, RTS */
3868 com_tiocm_bic(com, (tp->t_cflag & HUPCL) ?
3869 TIOCM_DTR|TIOCM_RTS|TIOCM_LE : TIOCM_LE );
3870 /* disable IENABLE */
3871 com_int_TxRx_disable( com );
3878 disc_optim(tp, t, com)
3883 if (!(t->c_iflag & (ICRNL | IGNCR | IMAXBEL | INLCR | ISTRIP | IXON))
3884 && (!(t->c_iflag & BRKINT) || (t->c_iflag & IGNBRK))
3885 && (!(t->c_iflag & PARMRK)
3886 || (t->c_iflag & (IGNPAR | IGNBRK)) == (IGNPAR | IGNBRK))
3887 && !(t->c_lflag & (ECHO | ICANON | IEXTEN | ISIG | PENDIN))
3888 && linesw[tp->t_line].l_rint == ttyinput)
3889 tp->t_state |= TS_CAN_BYPASS_L_RINT;
3891 tp->t_state &= ~TS_CAN_BYPASS_L_RINT;
3892 com->hotchar = linesw[tp->t_line].l_hotchar;
3896 * Following are all routines needed for SIO to act as console
3898 #include <machine/cons.h>
3908 static speed_t siocngetspeed __P((Port_t, struct speedtab *));
3909 static void siocnclose __P((struct siocnstate *sp, Port_t iobase));
3910 static void siocnopen __P((struct siocnstate *sp, Port_t iobase, int speed));
3911 static void siocntxwait __P((Port_t iobase));
3915 * XXX: sciocnget() and sciocnputc() are not declared static, as they are
3916 * referred to from i386/i386/i386-gdbstub.c.
3918 static cn_probe_t siocnprobe;
3919 static cn_init_t siocninit;
3920 static cn_checkc_t siocncheckc;
3921 cn_getc_t siocngetc;
3922 cn_putc_t siocnputc;
3924 CONS_DRIVER(sio, siocnprobe, siocninit, siocngetc, siocncheckc, siocnputc);
3935 * Wait for any pending transmission to finish. Required to avoid
3936 * the UART lockup bug when the speed is changed, and for normal
3940 while ((inb(iobase + com_lsr) & (LSR_TSRE | LSR_TXRDY))
3941 != (LSR_TSRE | LSR_TXRDY) && --timo != 0)
3946 * Read the serial port specified and try to figure out what speed
3947 * it's currently running at. We're assuming the serial port has
3948 * been initialized and is basicly idle. This routine is only intended
3949 * to be run at system startup.
3951 * If the value read from the serial port doesn't make sense, return 0.
3955 siocngetspeed(iobase, table)
3957 struct speedtab *table;
3964 cfcr = inb(iobase + com_cfcr);
3965 outb(iobase + com_cfcr, CFCR_DLAB | cfcr);
3967 dlbl = inb(iobase + com_dlbl);
3968 dlbh = inb(iobase + com_dlbh);
3970 outb(iobase + com_cfcr, cfcr);
3972 code = dlbh << 8 | dlbl;
3974 for ( ; table->sp_speed != -1; table++)
3975 if (table->sp_code == code)
3976 return (table->sp_speed);
3978 return 0; /* didn't match anything sane */
3982 siocnopen(sp, iobase, speed)
3983 struct siocnstate *sp;
3992 * Save all the device control registers except the fifo register
3993 * and set our default ones (cs8 -parenb speed=comdefaultrate).
3994 * We can't save the fifo register since it is read-only.
3996 sp->ier = inb(iobase + com_ier);
3997 outb(iobase + com_ier, 0); /* spltty() doesn't stop siointr() */
3998 siocntxwait(iobase);
3999 sp->cfcr = inb(iobase + com_cfcr);
4000 outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS);
4001 sp->dlbl = inb(iobase + com_dlbl);
4002 sp->dlbh = inb(iobase + com_dlbh);
4004 * Only set the divisor registers if they would change, since on
4005 * some 16550 incompatibles (Startech), setting them clears the
4006 * data input register. This also reduces the effects of the
4009 divisor = ttspeedtab(speed, comspeedtab);
4010 dlbl = divisor & 0xFF;
4011 if (sp->dlbl != dlbl)
4012 outb(iobase + com_dlbl, dlbl);
4013 dlbh = (u_int) divisor >> 8;
4014 if (sp->dlbh != dlbh)
4015 outb(iobase + com_dlbh, dlbh);
4016 outb(iobase + com_cfcr, CFCR_8BITS);
4017 sp->mcr = inb(iobase + com_mcr);
4019 * We don't want interrupts, but must be careful not to "disable"
4020 * them by clearing the MCR_IENABLE bit, since that might cause
4021 * an interrupt by floating the IRQ line.
4023 outb(iobase + com_mcr, (sp->mcr & MCR_IENABLE) | MCR_DTR | MCR_RTS);
4027 siocnclose(sp, iobase)
4028 struct siocnstate *sp;
4032 * Restore the device control registers.
4034 siocntxwait(iobase);
4035 outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS);
4036 if (sp->dlbl != inb(iobase + com_dlbl))
4037 outb(iobase + com_dlbl, sp->dlbl);
4038 if (sp->dlbh != inb(iobase + com_dlbh))
4039 outb(iobase + com_dlbh, sp->dlbh);
4040 outb(iobase + com_cfcr, sp->cfcr);
4042 * XXX damp oscillations of MCR_DTR and MCR_RTS by not restoring them.
4044 outb(iobase + com_mcr, sp->mcr | MCR_DTR | MCR_RTS);
4045 outb(iobase + com_ier, sp->ier);
4058 struct siocnstate sp;
4061 * Find our first enabled console, if any. If it is a high-level
4062 * console device, then initialize it and return successfully.
4063 * If it is a low-level console device, then initialize it and
4064 * return unsuccessfully. It must be initialized in both cases
4065 * for early use by console drivers and debuggers. Initializing
4066 * the hardware is not necessary in all cases, since the i/o
4067 * routines initialize it on the fly, but it is necessary if
4068 * input might arrive while the hardware is switched back to an
4069 * uninitialized state. We can't handle multiple console devices
4070 * yet because our low-level routines don't take a device arg.
4071 * We trust the user to set the console flags properly so that we
4072 * don't need to probe.
4074 cp->cn_pri = CN_DEAD;
4076 for (unit = 0; unit < 16; unit++) { /* XXX need to know how many */
4078 if (resource_int_value("sio", unit, "flags", &flags))
4080 if (COM_CONSOLE(flags)) {
4082 if (resource_int_value("sio", unit, "port", &port))
4086 if (boothowto & RB_SERIAL) {
4087 boot_speed = siocngetspeed(siocniobase,
4090 comdefaultrate = boot_speed;
4094 * Initialize the divisor latch. We can't rely on
4095 * siocnopen() to do this the first time, since it
4096 * avoids writing to the latch if the latch appears
4097 * to have the correct value. Also, if we didn't
4098 * just read the speed from the hardware, then we
4099 * need to set the speed in hardware so that
4100 * switching it later is null.
4102 cfcr = inb(siocniobase + com_cfcr);
4103 outb(siocniobase + com_cfcr, CFCR_DLAB | cfcr);
4104 outb(siocniobase + com_dlbl,
4105 COMBRD(comdefaultrate) & 0xff);
4106 outb(siocniobase + com_dlbh,
4107 (u_int) COMBRD(comdefaultrate) >> 8);
4108 outb(siocniobase + com_cfcr, cfcr);
4110 siocnopen(&sp, siocniobase, comdefaultrate);
4112 if (!COM_LLCONSOLE(flags)) {
4113 cp->cn_dev = makedev(CDEV_MAJOR, unit);
4114 cp->cn_pri = COM_FORCECONSOLE(flags)
4115 || boothowto & RB_SERIAL
4116 ? CN_REMOTE : CN_NORMAL;
4125 struct consdev siocons = {
4126 NULL, NULL, siocngetc, siocncheckc, siocnputc,
4127 NULL, makedev(CDEV_MAJOR, 0), CN_NORMAL,
4130 extern struct consdev *cn_tab;
4133 siocnattach(port, speed)
4139 struct siocnstate sp;
4142 comdefaultrate = speed;
4147 * Initialize the divisor latch. We can't rely on
4148 * siocnopen() to do this the first time, since it
4149 * avoids writing to the latch if the latch appears
4150 * to have the correct value. Also, if we didn't
4151 * just read the speed from the hardware, then we
4152 * need to set the speed in hardware so that
4153 * switching it later is null.
4155 cfcr = inb(siocniobase + com_cfcr);
4156 outb(siocniobase + com_cfcr, CFCR_DLAB | cfcr);
4157 outb(siocniobase + com_dlbl,
4158 COMBRD(comdefaultrate) & 0xff);
4159 outb(siocniobase + com_dlbh,
4160 (u_int) COMBRD(comdefaultrate) >> 8);
4161 outb(siocniobase + com_cfcr, cfcr);
4163 siocnopen(&sp, siocniobase, comdefaultrate);
4171 siogdbattach(port, speed)
4177 struct siocnstate sp;
4179 siogdbiobase = port;
4180 gdbdefaultrate = speed;
4185 * Initialize the divisor latch. We can't rely on
4186 * siocnopen() to do this the first time, since it
4187 * avoids writing to the latch if the latch appears
4188 * to have the correct value. Also, if we didn't
4189 * just read the speed from the hardware, then we
4190 * need to set the speed in hardware so that
4191 * switching it later is null.
4193 cfcr = inb(siogdbiobase + com_cfcr);
4194 outb(siogdbiobase + com_cfcr, CFCR_DLAB | cfcr);
4195 outb(siogdbiobase + com_dlbl,
4196 COMBRD(gdbdefaultrate) & 0xff);
4197 outb(siogdbiobase + com_dlbh,
4198 (u_int) COMBRD(gdbdefaultrate) >> 8);
4199 outb(siogdbiobase + com_cfcr, cfcr);
4201 siocnopen(&sp, siogdbiobase, gdbdefaultrate);
4216 comconsole = DEV_TO_UNIT(cp->cn_dev);
4229 struct siocnstate sp;
4231 iobase = siocniobase;
4233 siocnopen(&sp, iobase, comdefaultrate);
4234 if (inb(iobase + com_lsr) & LSR_RXRDY)
4235 c = inb(iobase + com_data);
4238 siocnclose(&sp, iobase);
4251 struct siocnstate sp;
4253 iobase = siocniobase;
4255 siocnopen(&sp, iobase, comdefaultrate);
4256 while (!(inb(iobase + com_lsr) & LSR_RXRDY))
4258 c = inb(iobase + com_data);
4259 siocnclose(&sp, iobase);
4270 struct siocnstate sp;
4273 siocnopen(&sp, siocniobase, comdefaultrate);
4274 siocntxwait(siocniobase);
4275 outb(siocniobase + com_data, c);
4276 siocnclose(&sp, siocniobase);
4286 struct siocnstate sp;
4288 iobase = siogdbiobase;
4290 siocnopen(&sp, iobase, gdbdefaultrate);
4291 while (!(inb(iobase + com_lsr) & LSR_RXRDY))
4293 c = inb(iobase + com_data);
4294 siocnclose(&sp, iobase);
4304 struct siocnstate sp;
4307 siocnopen(&sp, siogdbiobase, gdbdefaultrate);
4308 siocntxwait(siogdbiobase);
4309 outb(siogdbiobase + com_data, c);
4310 siocnclose(&sp, siogdbiobase);
4316 * support PnP cards if we are using 'em
4321 static pnpid_t siopnp_ids[] = {
4322 { 0x5015f435, "MOT1550"},
4323 { 0x8113b04e, "Supra1381"},
4324 { 0x9012b04e, "Supra1290"},
4325 { 0x7121b04e, "SupraExpress 56i Sp"},
4326 { 0x11007256, "USR0011"},
4327 { 0x30207256, "USR2030"},
4328 { 0x31307256, "USR3031"},
4329 { 0x90307256, "USR3090"},
4330 { 0x0100440e, "Cardinal MVP288IV"},
4334 static char *siopnp_probe(u_long csn, u_long vend_id);
4335 static void siopnp_attach(u_long csn, u_long vend_id, char *name,
4336 struct isa_device *dev);
4337 static u_long nsiopnp = NSIO;
4339 static struct pnp_device siopnp = {
4346 DATA_SET (pnpdevice_set, siopnp);
4349 siopnp_probe(u_long csn, u_long vend_id)
4354 for(id = siopnp_ids; id->vend_id != 0; id++) {
4355 if (vend_id == id->vend_id) {
4363 read_pnp_parms(&d, 0);
4364 if (d.enable == 0 || d.flags & 1) {
4365 printf("CSN %lu is disabled.\n", csn);
4375 siopnp_attach(u_long csn, u_long vend_id, char *name, struct isa_device *dev)
4379 if (dev->id_unit >= NSIOTOT)
4382 if (read_pnp_parms(&d, 0) == 0) {
4383 printf("failed to read pnp parms\n");
4387 write_pnp_parms(&d, 0);
4391 dev->id_iobase = d.port[0];
4392 dev->id_irq = (1 << d.irq[0]);
4393 dev->id_ointr = siointr;
4394 dev->id_ri_flags = RI_FAST;
4397 if (dev->id_driver == NULL) {
4398 dev->id_driver = &siodriver;
4399 dev->id_id = isa_compat_nextid();
4402 if ((dev->id_alive = sioprobe(dev)) != 0)
4405 printf("sio%d: probe failed\n", dev->id_unit);
4409 CDEV_DRIVER_MODULE(sio, isa, sio_driver, sio_devclass,
4410 CDEV_MAJOR, sio_cdevsw, 0, 0);
4414 * pc98 local function
4418 com_tiocm_set(struct com_s *com, int msr)
4422 int mask = CMD8251_TxEN|CMD8251_RxEN|CMD8251_DTR|CMD8251_RTS;
4425 com->pc98_prev_modem_status = ( msr & (TIOCM_LE|TIOCM_DTR|TIOCM_RTS) )
4426 | ( com->pc98_prev_modem_status & ~(TIOCM_LE|TIOCM_DTR|TIOCM_RTS) );
4427 tmp |= (CMD8251_TxEN|CMD8251_RxEN);
4428 if ( msr & TIOCM_DTR ) tmp |= CMD8251_DTR;
4429 if ( msr & TIOCM_RTS ) tmp |= CMD8251_RTS;
4430 pc98_i8251_clear_or_cmd( com, mask, tmp );
4435 com_tiocm_bis(struct com_s *com, int msr)
4441 com->pc98_prev_modem_status |= ( msr & (TIOCM_LE|TIOCM_DTR|TIOCM_RTS) );
4442 tmp |= CMD8251_TxEN|CMD8251_RxEN;
4443 if ( msr & TIOCM_DTR ) tmp |= CMD8251_DTR;
4444 if ( msr & TIOCM_RTS ) tmp |= CMD8251_RTS;
4446 pc98_i8251_or_cmd( com, tmp );
4451 com_tiocm_bic(struct com_s *com, int msr)
4457 com->pc98_prev_modem_status &= ~( msr & (TIOCM_LE|TIOCM_DTR|TIOCM_RTS) );
4458 if ( msr & TIOCM_DTR ) tmp |= CMD8251_DTR;
4459 if ( msr & TIOCM_RTS ) tmp |= CMD8251_RTS;
4461 pc98_i8251_clear_cmd( com, tmp );
4466 com_tiocm_get(struct com_s *com)
4468 return( com->pc98_prev_modem_status );
4472 com_tiocm_get_delta(struct com_s *com)
4476 tmp = com->pc98_modem_delta;
4477 com->pc98_modem_delta = 0;
4481 /* convert to TIOCM_?? ( ioctl.h ) */
4483 pc98_get_modem_status(struct com_s *com)
4488 stat = inb(com->sts_port);
4489 stat2 = inb(com->in_modem_port);
4490 msr = com->pc98_prev_modem_status
4491 & ~(TIOCM_CAR|TIOCM_RI|TIOCM_DSR|TIOCM_CTS);
4492 if ( !(stat2 & CICSCD_CD) ) msr |= TIOCM_CAR;
4493 if ( !(stat2 & CICSCD_CI) ) msr |= TIOCM_RI;
4494 if ( stat & STS8251_DSR ) msr |= TIOCM_DSR;
4495 if ( !(stat2 & CICSCD_CS) ) msr |= TIOCM_CTS;
4496 #if COM_CARRIER_DETECT_EMULATE
4497 if ( msr & (TIOCM_DSR|TIOCM_CTS) ) {
4505 pc98_check_msr(void* chan)
4509 register struct tty *tp;
4517 unit = MINOR_TO_UNIT(mynor);
4518 com = com_addr(unit);
4522 msr = pc98_get_modem_status(com);
4523 /* make change flag */
4524 delta = msr ^ com->pc98_prev_modem_status;
4525 if ( delta & TIOCM_CAR ) {
4526 if ( com->modem_car_chg_timer ) {
4527 if ( -- com->modem_car_chg_timer )
4530 if ((com->modem_car_chg_timer = (msr & TIOCM_CAR) ?
4531 DCD_ON_RECOGNITION : DCD_OFF_TOLERANCE) != 0)
4535 com->modem_car_chg_timer = 0;
4536 delta = ( msr ^ com->pc98_prev_modem_status ) &
4537 (TIOCM_CAR|TIOCM_RI|TIOCM_DSR|TIOCM_CTS);
4538 com->pc98_prev_modem_status = msr;
4539 delta = ( com->pc98_modem_delta |= delta );
4541 if ( com->modem_checking || (tp->t_state & (TS_ISOPEN)) ) {
4545 timeout(pc98_check_msr, (caddr_t)dev,
4546 PC98_CHECK_MODEM_INTERVAL);
4548 com->modem_checking = 0;
4553 pc98_msrint_start(dev_t dev)
4561 unit = MINOR_TO_UNIT(mynor);
4562 com = com_addr(unit);
4563 /* modem control line check routine envoke interval is 1/10 sec */
4564 if ( com->modem_checking == 0 ) {
4565 com->pc98_prev_modem_status = pc98_get_modem_status(com);
4566 com->pc98_modem_delta = 0;
4567 timeout(pc98_check_msr, (caddr_t)dev,
4568 PC98_CHECK_MODEM_INTERVAL);
4569 com->modem_checking = 1;
4575 pc98_disable_i8251_interrupt(struct com_s *com, int mod)
4577 /* disable interrupt */
4580 mod |= ~(IEN_Tx|IEN_TxEMP|IEN_Rx);
4582 tmp = inb( com->intr_ctrl_port ) & ~(IEN_Tx|IEN_TxEMP|IEN_Rx);
4583 outb( com->intr_ctrl_port, (com->intr_enable&=~mod) | tmp );
4588 pc98_enable_i8251_interrupt(struct com_s *com, int mod)
4593 tmp = inb( com->intr_ctrl_port ) & ~(IEN_Tx|IEN_TxEMP|IEN_Rx);
4594 outb( com->intr_ctrl_port, (com->intr_enable|=mod) | tmp );
4599 pc98_check_i8251_interrupt(struct com_s *com)
4601 return ( com->intr_enable & 0x07 );
4605 pc98_i8251_clear_cmd(struct com_s *com, int x)
4610 tmp = com->pc98_prev_siocmd & ~(x);
4611 outb(com->cmd_port, tmp);
4612 com->pc98_prev_siocmd = tmp & ~(CMD8251_ER|CMD8251_RESET|CMD8251_EH);
4617 pc98_i8251_or_cmd(struct com_s *com, int x)
4622 tmp = com->pc98_prev_siocmd | (x);
4623 outb(com->cmd_port, tmp);
4624 com->pc98_prev_siocmd = tmp & ~(CMD8251_ER|CMD8251_RESET|CMD8251_EH);
4629 pc98_i8251_set_cmd(struct com_s *com, int x)
4635 outb(com->cmd_port, tmp);
4636 com->pc98_prev_siocmd = tmp & ~(CMD8251_ER|CMD8251_RESET|CMD8251_EH);
4641 pc98_i8251_clear_or_cmd(struct com_s *com, int clr, int x)
4645 tmp = com->pc98_prev_siocmd & ~(clr);
4647 outb(com->cmd_port, tmp);
4648 com->pc98_prev_siocmd = tmp & ~(CMD8251_ER|CMD8251_RESET|CMD8251_EH);
4653 pc98_i8251_get_cmd(struct com_s *com)
4655 return com->pc98_prev_siocmd;
4659 pc98_i8251_get_mod(struct com_s *com)
4661 return com->pc98_prev_siomod;
4665 pc98_i8251_reset(struct com_s *com, int mode, int command)
4667 outb(com->cmd_port, 0); /* dummy */
4669 outb(com->cmd_port, 0); /* dummy */
4671 outb(com->cmd_port, 0); /* dummy */
4673 outb(com->cmd_port, CMD8251_RESET); /* internal reset */
4675 outb(com->cmd_port, mode ); /* mode register */
4676 com->pc98_prev_siomod = mode;
4678 pc98_i8251_set_cmd( com, (command|CMD8251_ER) );
4682 pc98_check_sysclock(void)
4684 /* get system clock from port */
4685 if ( pc98_machine_type & M_8M ) {
4686 /* 8 MHz system & H98 */
4695 com_cflag_and_speed_set( struct com_s *com, int cflag, int speed)
4700 count = pc98_ttspeedtab( com, speed );
4701 if ( count < 0 ) return;
4703 previnterrupt = pc98_check_i8251_interrupt(com);
4704 pc98_disable_i8251_interrupt( com, IEN_Tx|IEN_TxEMP|IEN_Rx );
4706 switch ( cflag&CSIZE ) {
4708 cfcr = MOD8251_5BITS; break;
4710 cfcr = MOD8251_6BITS; break;
4712 cfcr = MOD8251_7BITS; break;
4714 cfcr = MOD8251_8BITS; break;
4716 if ( cflag&PARENB ) {
4718 cfcr |= MOD8251_PODD;
4720 cfcr |= MOD8251_PEVEN;
4722 cfcr |= MOD8251_PDISAB;
4725 cfcr |= MOD8251_STOP2;
4727 cfcr |= MOD8251_STOP1;
4729 if ( count & 0x10000 )
4730 cfcr |= MOD8251_CLKX1;
4732 cfcr |= MOD8251_CLKX16;
4734 if (epson_machine_id != 0x20) { /* XXX */
4736 while (!((tmp = inb(com->sts_port)) & STS8251_TxEMP))
4739 /* set baud rate from ospeed */
4740 pc98_set_baud_rate( com, count );
4742 if ( cfcr != pc98_i8251_get_mod(com) )
4743 pc98_i8251_reset(com, cfcr, pc98_i8251_get_cmd(com) );
4745 pc98_enable_i8251_interrupt( com, previnterrupt );
4749 pc98_ttspeedtab(struct com_s *com, int speed)
4751 int if_type, effect_sp, count = -1, mod;
4753 if_type = com->pc98_if_type & 0x0f;
4755 switch (com->pc98_if_type) {
4756 case COM_IF_INTERNAL:
4757 if (PC98SIO_baud_rate_port(if_type) != -1) {
4758 count = ttspeedtab(speed, if_8251_type[if_type].speedtab);
4760 count |= COM1_EXT_CLOCK;
4765 /* for *1CLK asynchronous! mode, TEFUTEFU */
4766 mod = (sysclock == 5) ? 2457600 : 1996800;
4767 effect_sp = ttspeedtab( speed, pc98speedtab );
4768 if ( effect_sp < 0 ) /* XXX */
4769 effect_sp = ttspeedtab( (speed - 1), pc98speedtab );
4770 if ( effect_sp <= 0 )
4772 if ( effect_sp == speed )
4774 if ( mod % effect_sp )
4776 count = mod / effect_sp;
4777 if ( count > 65535 )
4779 if ( effect_sp != speed )
4782 case COM_IF_PC9861K_1:
4783 case COM_IF_PC9861K_2:
4786 case COM_IF_IND_SS_1:
4787 case COM_IF_IND_SS_2:
4788 case COM_IF_PIO9032B_1:
4789 case COM_IF_PIO9032B_2:
4790 if ( speed == 0 ) return 0;
4791 count = ttspeedtab( speed, if_8251_type[if_type].speedtab );
4793 case COM_IF_B98_01_1:
4794 case COM_IF_B98_01_2:
4795 if ( speed == 0 ) return 0;
4796 count = ttspeedtab( speed, if_8251_type[if_type].speedtab );
4798 if (count == 0 || count == 1) {
4800 count |= 0x20000; /* x1 mode for 76800 and 153600 */
4810 pc98_set_baud_rate( struct com_s *com, int count )
4814 if_type = com->pc98_if_type & 0x0f;
4815 io = com->iobase & 0xff00;
4817 switch (com->pc98_if_type) {
4818 case COM_IF_INTERNAL:
4819 if (PC98SIO_baud_rate_port(if_type) != -1) {
4820 if (count & COM1_EXT_CLOCK) {
4821 outb((Port_t)PC98SIO_baud_rate_port(if_type), count & 0xff);
4824 outb((Port_t)PC98SIO_baud_rate_port(if_type), 0x09);
4829 printf( "[ Illegal count : %d ]", count );
4831 } else if ( count == 0 )
4840 outb( 0x75, count & 0xff );
4842 outb( 0x75, (count >> 8) & 0xff );
4845 case COM_IF_IND_SS_1:
4846 case COM_IF_IND_SS_2:
4847 outb(io | PC98SIO_intr_ctrl_port(if_type), 0);
4848 outb(io | PC98SIO_baud_rate_port(if_type), 0);
4849 outb(io | PC98SIO_baud_rate_port(if_type), 0xc0);
4850 outb(io | PC98SIO_baud_rate_port(if_type), (count >> 8) | 0x80);
4851 outb(io | PC98SIO_baud_rate_port(if_type), count & 0xff);
4853 case COM_IF_PIO9032B_1:
4854 case COM_IF_PIO9032B_2:
4855 outb(io | PC98SIO_baud_rate_port(if_type), count);
4857 case COM_IF_B98_01_1:
4858 case COM_IF_B98_01_2:
4859 outb(io | PC98SIO_baud_rate_port(if_type), count & 0x0f);
4862 * Some old B98_01 board should be controlled
4863 * in different way, but this hasn't been tested yet.
4865 outb(io | PC98SIO_func_port(if_type),
4866 (count & 0x20000) ? 0xf0 : 0xf2);
4872 pc98_check_if_type(device_t dev, struct siodev *iod)
4874 int irr, io, if_type, tmp;
4875 static short irq_tab[2][8] = {
4876 { 3, 5, 6, 9, 10, 12, 13, -1},
4877 { 3, 10, 12, 13, 5, 6, 9, -1}
4880 iod->if_type = if_type = (isa_get_flags(dev) >> 24) & 0xff;
4881 if ((if_type < 0 || if_type > COM_IF_END1) &&
4882 (if_type < 0x10 || if_type > COM_IF_END2))
4886 io = isa_get_port(dev) & 0xff00;
4888 if (IS_8251(iod->if_type)) {
4889 if (PC98SIO_func_port(if_type) != -1) {
4890 outb(io | PC98SIO_func_port(if_type), 0xf2);
4891 tmp = ttspeedtab(9600, if_8251_type[if_type].speedtab);
4892 if (tmp != -1 && PC98SIO_baud_rate_port(if_type) != -1)
4893 outb(io | PC98SIO_baud_rate_port(if_type), tmp);
4896 iod->cmd = io | PC98SIO_cmd_port(if_type);
4897 iod->sts = io | PC98SIO_sts_port(if_type);
4898 iod->mod = io | PC98SIO_in_modem_port(if_type);
4899 iod->ctrl = io | PC98SIO_intr_ctrl_port(if_type);
4901 if (iod->if_type == COM_IF_INTERNAL) {
4904 /* XXX check new internal port. */
4907 for (tmp = 0; tmp < 100; tmp++) {
4908 if ((inb(0x138) & 1) == 0) {
4909 PC98SIO_baud_rate_port(if_type) = 0x13a;
4910 if_8251_type[if_type].name = " (internal fast)";
4911 if_8251_type[if_type].speedtab = pc98fast_speedtab;
4917 tmp = inb( iod->mod ) & if_8251_type[if_type].irr_mask;
4918 if ((isa_get_port(dev) & 0xff) == IO_COM2)
4919 iod->irq = irq_tab[0][tmp];
4921 iod->irq = irq_tab[1][tmp];
4924 irr = if_16550a_type[if_type].irr_read;
4925 #ifdef COM_MULTIPORT
4926 if (!COM_ISMULTIPORT(dev) || dev->id_unit == COM_MPMASTER(dev))
4929 tmp = inb(io | irr);
4930 if (isa_get_port(dev) & 0x01) /* XXX depend on RSB-384 */
4931 iod->irq = irq_tab[1][tmp >> 3];
4933 iod->irq = irq_tab[0][tmp & 0x07];
4936 if ( iod->irq == -1 ) return -1;
4941 pc98_set_ioport( struct com_s *com, int id_flags )
4945 if_type = (id_flags >> 24) & 0xff;
4946 if (IS_8251(if_type)) {
4947 pc98_check_sysclock();
4948 io = com->iobase & 0xff00;
4949 com->pc98_if_type = if_type;
4951 com->data_port = io | PC98SIO_data_port(if_type);
4952 com->cmd_port = io | PC98SIO_cmd_port(if_type);
4953 com->sts_port = io | PC98SIO_sts_port(if_type);
4954 com->in_modem_port = io | PC98SIO_in_modem_port(if_type);
4955 com->intr_ctrl_port = io | PC98SIO_intr_ctrl_port(if_type);
4961 #endif /* PC98 defined */