sio.c revision 155921
1/*- 2 * Copyright (c) 1991 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD: head/sys/pc98/cbus/sio.c 155921 2006-02-22 18:16:26Z jhb $ 30 * from: @(#)com.c 7.5 (Berkeley) 5/16/91 31 * from: i386/isa sio.c,v 1.234 32 */ 33 34#include "opt_comconsole.h" 35#include "opt_compat.h" 36#include "opt_gdb.h" 37#include "opt_kdb.h" 38#include "opt_sio.h" 39 40/* 41 * Serial driver, based on 386BSD-0.1 com driver. 42 * Mostly rewritten to use pseudo-DMA. 43 * Works for National Semiconductor NS8250-NS16550AF UARTs. 44 * COM driver, based on HP dca driver. 45 * 46 * Changes for PC Card integration: 47 * - Added PC Card driver table and handlers 48 */ 49/*=============================================================== 50 * 386BSD(98),FreeBSD-1.1x(98) com driver. 51 * ----- 52 * modified for PC9801 by M.Ishii 53 * Kyoto University Microcomputer Club (KMC) 54 * Chou "TEFUTEFU" Hirotomi 55 * Kyoto Univ. the faculty of medicine 56 *=============================================================== 57 * FreeBSD-2.0.1(98) sio driver. 58 * ----- 59 * modified for pc98 Internal i8251 and MICRO CORE MC16550II 60 * T.Koike(hfc01340@niftyserve.or.jp) 61 * implement kernel device configuration 62 * aizu@orient.center.nitech.ac.jp 63 * 64 * Notes. 65 * ----- 66 * PC98 localization based on 386BSD(98) com driver. Using its PC98 local 67 * functions. 68 * This driver is under debugging,has bugs. 69 */ 70/* 71 * modified for AIWA B98-01 72 * by T.Hatanou <hatanou@yasuda.comm.waseda.ac.jp> last update: 15 Sep.1995 73 */ 74/* 75 * Modified by Y.Takahashi of Kogakuin University. 76 */ 77/* 78 * modified for 8251(FIFO) by Seigo TANIMURA <tanimura@FreeBSD.org> 79 */ 80 81#include <sys/param.h> 82#include <sys/systm.h> 83#include <sys/bus.h> 84#include <sys/conf.h> 85#include <sys/fcntl.h> 86#include <sys/interrupt.h> 87#include <sys/kdb.h> 88#include <sys/kernel.h> 89#include <sys/limits.h> 90#include <sys/lock.h> 91#include <sys/malloc.h> 92#include <sys/module.h> 93#include <sys/mutex.h> 94#include <sys/proc.h> 95#include <sys/reboot.h> 96#include <sys/serial.h> 97#include <sys/sysctl.h> 98#include <sys/syslog.h> 99#include <sys/tty.h> 100#include <machine/bus.h> 101#include <sys/rman.h> 102#include <sys/timepps.h> 103#include <sys/uio.h> 104#include <sys/cons.h> 105 106#include <isa/isavar.h> 107 108#include <machine/resource.h> 109 110#include <dev/sio/sioreg.h> 111#include <dev/sio/siovar.h> 112 113#ifdef PC98 114#include <pc98/cbus/cbus.h> 115#include <pc98/pc98/pc98_machdep.h> 116#endif 117 118#ifdef COM_ESP 119#include <dev/ic/esp.h> 120#endif 121#include <dev/ic/ns16550.h> 122#ifdef PC98 123#include <dev/ic/i8251.h> 124#include <dev/ic/rsa.h> 125#endif 126 127#define LOTS_OF_EVENTS 64 /* helps separate urgent events from input */ 128 129/* 130 * Meaning of flags: 131 * 132 * 0x00000001 shared IRQs 133 * 0x00000002 disable FIFO 134 * 0x00000008 recover sooner from lost output interrupts 135 * 0x00000010 device is potential system console 136 * 0x00000020 device is forced to become system console 137 * 0x00000040 device is reserved for low-level IO 138 * 0x00000080 use this port for remote kernel debugging 139 * 0x0000??00 minor number of master port 140 * 0x00010000 PPS timestamping on CTS instead of DCD 141 * 0x00080000 IIR_TXRDY bug 142 * 0x00400000 If no comconsole found then mark as a comconsole 143 * 0x1?000000 interface type 144 */ 145 146#ifdef COM_MULTIPORT 147/* checks in flags for multiport and which is multiport "master chip" 148 * for a given card 149 */ 150#define COM_ISMULTIPORT(flags) ((flags) & 0x01) 151#define COM_MPMASTER(flags) (((flags) >> 8) & 0x0ff) 152#ifndef PC98 153#define COM_NOTAST4(flags) ((flags) & 0x04) 154#endif 155#else 156#define COM_ISMULTIPORT(flags) (0) 157#endif /* COM_MULTIPORT */ 158 159#define COM_C_IIR_TXRDYBUG 0x80000 160#define COM_CONSOLE(flags) ((flags) & 0x10) 161#define COM_DEBUGGER(flags) ((flags) & 0x80) 162#ifndef PC98 163#define COM_FIFOSIZE(flags) (((flags) & 0xff000000) >> 24) 164#endif 165#define COM_FORCECONSOLE(flags) ((flags) & 0x20) 166#define COM_IIR_TXRDYBUG(flags) ((flags) & COM_C_IIR_TXRDYBUG) 167#define COM_LLCONSOLE(flags) ((flags) & 0x40) 168#define COM_LOSESOUTINTS(flags) ((flags) & 0x08) 169#define COM_NOFIFO(flags) ((flags) & 0x02) 170#ifndef PC98 171#define COM_NOSCR(flags) ((flags) & 0x100000) 172#endif 173#define COM_PPSCTS(flags) ((flags) & 0x10000) 174#ifndef PC98 175#define COM_ST16650A(flags) ((flags) & 0x20000) 176#define COM_TI16754(flags) ((flags) & 0x200000) 177#endif 178 179#define sio_getreg(com, off) \ 180 (bus_space_read_1((com)->bst, (com)->bsh, (off))) 181#define sio_setreg(com, off, value) \ 182 (bus_space_write_1((com)->bst, (com)->bsh, (off), (value))) 183 184/* 185 * com state bits. 186 * (CS_BUSY | CS_TTGO) and (CS_BUSY | CS_TTGO | CS_ODEVREADY) must be higher 187 * than the other bits so that they can be tested as a group without masking 188 * off the low bits. 189 * 190 * The following com and tty flags correspond closely: 191 * CS_BUSY = TS_BUSY (maintained by comstart(), siopoll() and 192 * comstop()) 193 * CS_TTGO = ~TS_TTSTOP (maintained by comparam() and comstart()) 194 * CS_CTS_OFLOW = CCTS_OFLOW (maintained by comparam()) 195 * CS_RTS_IFLOW = CRTS_IFLOW (maintained by comparam()) 196 * TS_FLUSH is not used. 197 * XXX I think TIOCSETA doesn't clear TS_TTSTOP when it clears IXON. 198 * XXX CS_*FLOW should be CF_*FLOW in com->flags (control flags not state). 199 */ 200#define CS_BUSY 0x80 /* output in progress */ 201#define CS_TTGO 0x40 /* output not stopped by XOFF */ 202#define CS_ODEVREADY 0x20 /* external device h/w ready (CTS) */ 203#define CS_CHECKMSR 1 /* check of MSR scheduled */ 204#define CS_CTS_OFLOW 2 /* use CTS output flow control */ 205#define CS_ODONE 4 /* output completed */ 206#define CS_RTS_IFLOW 8 /* use RTS input flow control */ 207#define CSE_BUSYCHECK 1 /* siobusycheck() scheduled */ 208 209static char const * const error_desc[] = { 210#define CE_OVERRUN 0 211 "silo overflow", 212#define CE_INTERRUPT_BUF_OVERFLOW 1 213 "interrupt-level buffer overflow", 214#define CE_TTY_BUF_OVERFLOW 2 215 "tty-level buffer overflow", 216}; 217 218#define CE_NTYPES 3 219#define CE_RECORD(com, errnum) (++(com)->delta_error_counts[errnum]) 220 221/* types. XXX - should be elsewhere */ 222typedef u_int Port_t; /* hardware port */ 223typedef u_char bool_t; /* boolean */ 224 225/* queue of linear buffers */ 226struct lbq { 227 u_char *l_head; /* next char to process */ 228 u_char *l_tail; /* one past the last char to process */ 229 struct lbq *l_next; /* next in queue */ 230 bool_t l_queued; /* nonzero if queued */ 231}; 232 233/* com device structure */ 234struct com_s { 235 u_char state; /* miscellaneous flag bits */ 236 u_char cfcr_image; /* copy of value written to CFCR */ 237#ifdef COM_ESP 238 bool_t esp; /* is this unit a hayes esp board? */ 239#endif 240 u_char extra_state; /* more flag bits, separate for order trick */ 241 u_char fifo_image; /* copy of value written to FIFO */ 242 bool_t hasfifo; /* nonzero for 16550 UARTs */ 243 bool_t loses_outints; /* nonzero if device loses output interrupts */ 244 u_char mcr_image; /* copy of value written to MCR */ 245#ifdef COM_MULTIPORT 246 bool_t multiport; /* is this unit part of a multiport device? */ 247#endif /* COM_MULTIPORT */ 248 bool_t no_irq; /* nonzero if irq is not attached */ 249 bool_t gone; /* hardware disappeared */ 250 bool_t poll; /* nonzero if polling is required */ 251 bool_t poll_output; /* nonzero if polling for output is required */ 252 bool_t st16650a; /* nonzero if Startech 16650A compatible */ 253 int unit; /* unit number */ 254 u_int flags; /* copy of device flags */ 255 u_int tx_fifo_size; 256 257 /* 258 * The high level of the driver never reads status registers directly 259 * because there would be too many side effects to handle conveniently. 260 * Instead, it reads copies of the registers stored here by the 261 * interrupt handler. 262 */ 263 u_char last_modem_status; /* last MSR read by intr handler */ 264 u_char prev_modem_status; /* last MSR handled by high level */ 265 266 u_char *ibuf; /* start of input buffer */ 267 u_char *ibufend; /* end of input buffer */ 268 u_char *ibufold; /* old input buffer, to be freed */ 269 u_char *ihighwater; /* threshold in input buffer */ 270 u_char *iptr; /* next free spot in input buffer */ 271 int ibufsize; /* size of ibuf (not include error bytes) */ 272 int ierroff; /* offset of error bytes in ibuf */ 273 274 struct lbq obufq; /* head of queue of output buffers */ 275 struct lbq obufs[2]; /* output buffers */ 276 277 bus_space_tag_t bst; 278 bus_space_handle_t bsh; 279 280#ifdef PC98 281 Port_t cmd_port; 282 Port_t sts_port; 283 Port_t in_modem_port; 284 Port_t intr_ctrl_port; 285 Port_t rsabase; /* Iobase address of an I/O-DATA RSA board. */ 286 int intr_enable; 287 int pc98_prev_modem_status; 288 int pc98_modem_delta; 289 int modem_car_chg_timer; 290 int pc98_prev_siocmd; 291 int pc98_prev_siomod; 292 int modem_checking; 293 int pc98_if_type; 294 295 bool_t pc98_8251fifo; 296 bool_t pc98_8251fifo_enable; 297#endif /* PC98 */ 298 Port_t data_port; /* i/o ports */ 299#ifdef COM_ESP 300 Port_t esp_port; 301#endif 302 Port_t int_ctl_port; 303 Port_t int_id_port; 304 Port_t modem_ctl_port; 305 Port_t line_status_port; 306 Port_t modem_status_port; 307 308 struct tty *tp; /* cross reference */ 309 310 struct pps_state pps; 311 int pps_bit; 312#ifdef ALT_BREAK_TO_DEBUGGER 313 int alt_brk_state; 314#endif 315 316 u_long bytes_in; /* statistics */ 317 u_long bytes_out; 318 u_int delta_error_counts[CE_NTYPES]; 319 u_long error_counts[CE_NTYPES]; 320 321 u_long rclk; 322 323 struct resource *irqres; 324 struct resource *ioportres; 325 int ioportrid; 326 void *cookie; 327 328 /* 329 * Data area for output buffers. Someday we should build the output 330 * buffer queue without copying data. 331 */ 332#ifdef PC98 333 int obufsize; 334 u_char *obuf1; 335 u_char *obuf2; 336#else 337 u_char obuf1[256]; 338 u_char obuf2[256]; 339#endif 340}; 341 342#ifdef COM_ESP 343static int espattach(struct com_s *com, Port_t esp_port); 344#endif 345 346static void combreak(struct tty *tp, int sig); 347static timeout_t siobusycheck; 348static u_int siodivisor(u_long rclk, speed_t speed); 349static void comclose(struct tty *tp); 350static int comopen(struct tty *tp, struct cdev *dev); 351static void sioinput(struct com_s *com); 352static void siointr1(struct com_s *com); 353static void siointr(void *arg); 354static int commodem(struct tty *tp, int sigon, int sigoff); 355static int comparam(struct tty *tp, struct termios *t); 356static void siopoll(void *); 357static void siosettimeout(void); 358static int siosetwater(struct com_s *com, speed_t speed); 359static void comstart(struct tty *tp); 360static void comstop(struct tty *tp, int rw); 361static timeout_t comwakeup; 362 363char sio_driver_name[] = "sio"; 364static struct mtx sio_lock; 365static int sio_inited; 366 367/* table and macro for fast conversion from a unit number to its com struct */ 368devclass_t sio_devclass; 369#define com_addr(unit) ((struct com_s *) \ 370 devclass_get_softc(sio_devclass, unit)) /* XXX */ 371 372int comconsole = -1; 373static volatile speed_t comdefaultrate = CONSPEED; 374static u_long comdefaultrclk = DEFAULT_RCLK; 375SYSCTL_ULONG(_machdep, OID_AUTO, conrclk, CTLFLAG_RW, &comdefaultrclk, 0, ""); 376static speed_t gdbdefaultrate = GDBSPEED; 377SYSCTL_UINT(_machdep, OID_AUTO, gdbspeed, CTLFLAG_RW, 378 &gdbdefaultrate, GDBSPEED, ""); 379static u_int com_events; /* input chars + weighted output completions */ 380static Port_t siocniobase; 381static int siocnunit = -1; 382static void *sio_slow_ih; 383static void *sio_fast_ih; 384static int sio_timeout; 385static int sio_timeouts_until_log; 386static struct callout_handle sio_timeout_handle 387 = CALLOUT_HANDLE_INITIALIZER(&sio_timeout_handle); 388static int sio_numunits; 389 390#ifdef PC98 391struct siodev { 392 short if_type; 393 short irq; 394 Port_t cmd, sts, ctrl, mod; 395}; 396static int sysclock; 397 398#define COM_INT_DISABLE {int previpri; previpri=spltty(); 399#define COM_INT_ENABLE splx(previpri);} 400#define IEN_TxFLAG IEN_Tx 401 402#define COM_CARRIER_DETECT_EMULATE 0 403#define PC98_CHECK_MODEM_INTERVAL (hz/10) 404#define DCD_OFF_TOLERANCE 2 405#define DCD_ON_RECOGNITION 2 406#define IS_8251(if_type) (!(if_type & 0x10)) 407#define COM1_EXT_CLOCK 0x40000 408 409static void commint(struct cdev *dev); 410static void com_tiocm_bis(struct com_s *com, int msr); 411static void com_tiocm_bic(struct com_s *com, int msr); 412static int com_tiocm_get(struct com_s *com); 413static int com_tiocm_get_delta(struct com_s *com); 414static void pc98_msrint_start(struct cdev *dev); 415static void com_cflag_and_speed_set(struct com_s *com, int cflag, int speed); 416static int pc98_ttspeedtab(struct com_s *com, int speed, u_int *divisor); 417static int pc98_get_modem_status(struct com_s *com); 418static timeout_t pc98_check_msr; 419static void pc98_set_baud_rate(struct com_s *com, u_int count); 420static void pc98_i8251_reset(struct com_s *com, int mode, int command); 421static void pc98_disable_i8251_interrupt(struct com_s *com, int mod); 422static void pc98_enable_i8251_interrupt(struct com_s *com, int mod); 423static int pc98_check_i8251_interrupt(struct com_s *com); 424static int pc98_i8251_get_cmd(struct com_s *com); 425static int pc98_i8251_get_mod(struct com_s *com); 426static void pc98_i8251_set_cmd(struct com_s *com, int x); 427static void pc98_i8251_or_cmd(struct com_s *com, int x); 428static void pc98_i8251_clear_cmd(struct com_s *com, int x); 429static void pc98_i8251_clear_or_cmd(struct com_s *com, int clr, int x); 430static int pc98_check_if_type(device_t dev, struct siodev *iod); 431static int pc98_check_8251vfast(void); 432static int pc98_check_8251fifo(void); 433static void pc98_check_sysclock(void); 434static void pc98_set_ioport(struct com_s *com); 435 436#define com_int_Tx_disable(com) \ 437 pc98_disable_i8251_interrupt(com,IEN_Tx|IEN_TxEMP) 438#define com_int_Tx_enable(com) \ 439 pc98_enable_i8251_interrupt(com,IEN_TxFLAG) 440#define com_int_Rx_disable(com) \ 441 pc98_disable_i8251_interrupt(com,IEN_Rx) 442#define com_int_Rx_enable(com) \ 443 pc98_enable_i8251_interrupt(com,IEN_Rx) 444#define com_int_TxRx_disable(com) \ 445 pc98_disable_i8251_interrupt(com,IEN_Tx|IEN_TxEMP|IEN_Rx) 446#define com_int_TxRx_enable(com) \ 447 pc98_enable_i8251_interrupt(com,IEN_TxFLAG|IEN_Rx) 448#define com_send_break_on(com) \ 449 (IS_8251((com)->pc98_if_type) ? \ 450 pc98_i8251_or_cmd((com), CMD8251_SBRK) : \ 451 sio_setreg((com), com_cfcr, (com)->cfcr_image |= CFCR_SBREAK)) 452#define com_send_break_off(com) \ 453 (IS_8251((com)->pc98_if_type) ? \ 454 pc98_i8251_clear_cmd((com), CMD8251_SBRK) : \ 455 sio_setreg((com), com_cfcr, (com)->cfcr_image &= ~CFCR_SBREAK)) 456 457static struct speedtab pc98speedtab[] = { /* internal RS232C interface */ 458 { 0, 0, }, 459 { 50, 50, }, 460 { 75, 75, }, 461 { 150, 150, }, 462 { 200, 200, }, 463 { 300, 300, }, 464 { 600, 600, }, 465 { 1200, 1200, }, 466 { 2400, 2400, }, 467 { 4800, 4800, }, 468 { 9600, 9600, }, 469 { 19200, 19200, }, 470 { 38400, 38400, }, 471 { 51200, 51200, }, 472 { 76800, 76800, }, 473 { 20800, 20800, }, 474 { 31200, 31200, }, 475 { 41600, 41600, }, 476 { 62400, 62400, }, 477 { -1, -1 } 478}; 479static struct speedtab pc98fast_speedtab[] = { 480 { 9600, 0x80 | (DEFAULT_RCLK / (16 * (9600))), }, 481 { 19200, 0x80 | (DEFAULT_RCLK / (16 * (19200))), }, 482 { 38400, 0x80 | (DEFAULT_RCLK / (16 * (38400))), }, 483 { 57600, 0x80 | (DEFAULT_RCLK / (16 * (57600))), }, 484 { 115200, 0x80 | (DEFAULT_RCLK / (16 * (115200))), }, 485 { -1, -1 } 486}; 487static struct speedtab comspeedtab_pio9032b[] = { 488 { 300, 6, }, 489 { 600, 5, }, 490 { 1200, 4, }, 491 { 2400, 3, }, 492 { 4800, 2, }, 493 { 9600, 1, }, 494 { 19200, 0, }, 495 { 38400, 7, }, 496 { -1, -1 } 497}; 498static struct speedtab comspeedtab_b98_01[] = { 499 { 75, 11, }, 500 { 150, 10, }, 501 { 300, 9, }, 502 { 600, 8, }, 503 { 1200, 7, }, 504 { 2400, 6, }, 505 { 4800, 5, }, 506 { 9600, 4, }, 507 { 19200, 3, }, 508 { 38400, 2, }, 509 { 76800, 1, }, 510 { 153600, 0, }, 511 { -1, -1 } 512}; 513static struct speedtab comspeedtab_ind[] = { 514 { 300, 1536, }, 515 { 600, 768, }, 516 { 1200, 384, }, 517 { 2400, 192, }, 518 { 4800, 96, }, 519 { 9600, 48, }, 520 { 19200, 24, }, 521 { 38400, 12, }, 522 { 57600, 8, }, 523 { 115200, 4, }, 524 { 153600, 3, }, 525 { 230400, 2, }, 526 { 460800, 1, }, 527 { -1, -1 } 528}; 529 530struct { 531 char *name; 532 short port_table[7]; 533 short irr_mask; 534 struct speedtab *speedtab; 535 short check_irq; 536} if_8251_type[] = { 537 /* COM_IF_INTERNAL */ 538 { " (internal)", {0x30, 0x32, 0x32, 0x33, 0x35, -1, -1}, 539 -1, pc98speedtab, 1 }, 540 /* COM_IF_PC9861K_1 */ 541 { " (PC9861K)", {0xb1, 0xb3, 0xb3, 0xb0, 0xb0, -1, -1}, 542 3, NULL, 1 }, 543 /* COM_IF_PC9861K_2 */ 544 { " (PC9861K)", {0xb9, 0xbb, 0xbb, 0xb2, 0xb2, -1, -1}, 545 3, NULL, 1 }, 546 /* COM_IF_IND_SS_1 */ 547 { " (IND-SS)", {0xb1, 0xb3, 0xb3, 0xb0, 0xb0, 0xb3, -1}, 548 3, comspeedtab_ind, 1 }, 549 /* COM_IF_IND_SS_2 */ 550 { " (IND-SS)", {0xb9, 0xbb, 0xbb, 0xb2, 0xb2, 0xbb, -1}, 551 3, comspeedtab_ind, 1 }, 552 /* COM_IF_PIO9032B_1 */ 553 { " (PIO9032B)", {0xb1, 0xb3, 0xb3, 0xb0, 0xb0, 0xb8, -1}, 554 7, comspeedtab_pio9032b, 1 }, 555 /* COM_IF_PIO9032B_2 */ 556 { " (PIO9032B)", {0xb9, 0xbb, 0xbb, 0xb2, 0xb2, 0xba, -1}, 557 7, comspeedtab_pio9032b, 1 }, 558 /* COM_IF_B98_01_1 */ 559 { " (B98-01)", {0xb1, 0xb3, 0xb3, 0xb0, 0xb0, 0xd1, 0xd3}, 560 7, comspeedtab_b98_01, 0 }, 561 /* COM_IF_B98_01_2 */ 562 { " (B98-01)", {0xb9, 0xbb, 0xbb, 0xb2, 0xb2, 0xd5, 0xd7}, 563 7, comspeedtab_b98_01, 0 }, 564}; 565#define PC98SIO_data_port(type) (if_8251_type[type].port_table[0]) 566#define PC98SIO_cmd_port(type) (if_8251_type[type].port_table[1]) 567#define PC98SIO_sts_port(type) (if_8251_type[type].port_table[2]) 568#define PC98SIO_in_modem_port(type) (if_8251_type[type].port_table[3]) 569#define PC98SIO_intr_ctrl_port(type) (if_8251_type[type].port_table[4]) 570#define PC98SIO_baud_rate_port(type) (if_8251_type[type].port_table[5]) 571#define PC98SIO_func_port(type) (if_8251_type[type].port_table[6]) 572 573#define I8251F_data 0x130 574#define I8251F_lsr 0x132 575#define I8251F_msr 0x134 576#define I8251F_iir 0x136 577#define I8251F_fcr 0x138 578#define I8251F_div 0x13a 579 580 581static bus_addr_t port_table_0[] = 582 {0x000, 0x001, 0x002, 0x003, 0x004, 0x005, 0x006, 0x007}; 583static bus_addr_t port_table_1[] = 584 {0x000, 0x002, 0x004, 0x006, 0x008, 0x00a, 0x00c, 0x00e}; 585static bus_addr_t port_table_8[] = 586 {0x000, 0x100, 0x200, 0x300, 0x400, 0x500, 0x600, 0x700}; 587static bus_addr_t port_table_rsa[] = { 588 0x008, 0x009, 0x00a, 0x00b, 0x00c, 0x00d, 0x00e, 0x00f, 589 0x000, 0x001, 0x002, 0x003, 0x004, 0x005, 0x006, 0x007 590}; 591 592struct { 593 char *name; 594 short irr_read; 595 short irr_write; 596 bus_addr_t *iat; 597 bus_size_t iatsz; 598 u_long rclk; 599} if_16550a_type[] = { 600 /* COM_IF_RSA98 */ 601 {" (RSA-98)", -1, -1, port_table_0, IO_COMSIZE, DEFAULT_RCLK}, 602 /* COM_IF_NS16550 */ 603 {"", -1, -1, port_table_0, IO_COMSIZE, DEFAULT_RCLK}, 604 /* COM_IF_SECOND_CCU */ 605 {"", -1, -1, port_table_0, IO_COMSIZE, DEFAULT_RCLK}, 606 /* COM_IF_MC16550II */ 607 {" (MC16550II)", -1, 0x1000, port_table_8, IO_COMSIZE, 608 DEFAULT_RCLK * 4}, 609 /* COM_IF_MCRS98 */ 610 {" (MC-RS98)", -1, 0x1000, port_table_8, IO_COMSIZE, DEFAULT_RCLK * 4}, 611 /* COM_IF_RSB3000 */ 612 {" (RSB-3000)", 0xbf, -1, port_table_1, IO_COMSIZE, DEFAULT_RCLK * 10}, 613 /* COM_IF_RSB384 */ 614 {" (RSB-384)", 0xbf, -1, port_table_1, IO_COMSIZE, DEFAULT_RCLK * 10}, 615 /* COM_IF_MODEM_CARD */ 616 {"", -1, -1, port_table_0, IO_COMSIZE, DEFAULT_RCLK}, 617 /* COM_IF_RSA98III */ 618 {" (RSA-98III)", -1, -1, port_table_rsa, 16, DEFAULT_RCLK * 8}, 619 /* COM_IF_ESP98 */ 620 {" (ESP98)", -1, -1, port_table_1, IO_COMSIZE, DEFAULT_RCLK * 4}, 621}; 622#endif /* PC98 */ 623 624#ifdef GDB 625static Port_t siogdbiobase = 0; 626#endif 627 628#ifdef COM_ESP 629#ifdef PC98 630 631/* XXX configure this properly. */ 632/* XXX quite broken for new-bus. */ 633static Port_t likely_com_ports[] = { 0, 0xb0, 0xb1, 0 }; 634static Port_t likely_esp_ports[] = { 0xc0d0, 0 }; 635 636#define ESP98_CMD1 (ESP_CMD1 * 0x100) 637#define ESP98_CMD2 (ESP_CMD2 * 0x100) 638#define ESP98_STATUS1 (ESP_STATUS1 * 0x100) 639#define ESP98_STATUS2 (ESP_STATUS2 * 0x100) 640 641#else /* PC98 */ 642 643/* XXX configure this properly. */ 644static Port_t likely_com_ports[] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, }; 645static Port_t likely_esp_ports[] = { 0x140, 0x180, 0x280, 0 }; 646 647#endif /* PC98 */ 648#endif 649 650/* 651 * handle sysctl read/write requests for console speed 652 * 653 * In addition to setting comdefaultrate for I/O through /dev/console, 654 * also set the initial and lock values for the /dev/ttyXX device 655 * if there is one associated with the console. Finally, if the /dev/tty 656 * device has already been open, change the speed on the open running port 657 * itself. 658 */ 659 660static int 661sysctl_machdep_comdefaultrate(SYSCTL_HANDLER_ARGS) 662{ 663 int error, s; 664 speed_t newspeed; 665 struct com_s *com; 666 struct tty *tp; 667 668 newspeed = comdefaultrate; 669 670 error = sysctl_handle_opaque(oidp, &newspeed, sizeof newspeed, req); 671 if (error || !req->newptr) 672 return (error); 673 674 comdefaultrate = newspeed; 675 676 if (comconsole < 0) /* serial console not selected? */ 677 return (0); 678 679 com = com_addr(comconsole); 680 if (com == NULL) 681 return (ENXIO); 682 683 tp = com->tp; 684 if (tp == NULL) 685 return (ENXIO); 686 687 /* 688 * set the initial and lock rates for /dev/ttydXX and /dev/cuaXX 689 * (note, the lock rates really are boolean -- if non-zero, disallow 690 * speed changes) 691 */ 692 tp->t_init_in.c_ispeed = tp->t_init_in.c_ospeed = 693 tp->t_lock_in.c_ispeed = tp->t_lock_in.c_ospeed = 694 tp->t_init_out.c_ispeed = tp->t_init_out.c_ospeed = 695 tp->t_lock_out.c_ispeed = tp->t_lock_out.c_ospeed = comdefaultrate; 696 697 if (tp->t_state & TS_ISOPEN) { 698 tp->t_termios.c_ispeed = 699 tp->t_termios.c_ospeed = comdefaultrate; 700 s = spltty(); 701 error = comparam(tp, &tp->t_termios); 702 splx(s); 703 } 704 return error; 705} 706 707SYSCTL_PROC(_machdep, OID_AUTO, conspeed, CTLTYPE_INT | CTLFLAG_RW, 708 0, 0, sysctl_machdep_comdefaultrate, "I", ""); 709TUNABLE_INT("machdep.conspeed", __DEVOLATILE(int *, &comdefaultrate)); 710 711/* 712 * Unload the driver and clear the table. 713 * XXX this is mostly wrong. 714 * XXX TODO: 715 * This is usually called when the card is ejected, but 716 * can be caused by a kldunload of a controller driver. 717 * The idea is to reset the driver's view of the device 718 * and ensure that any driver entry points such as 719 * read and write do not hang. 720 */ 721int 722siodetach(device_t dev) 723{ 724 struct com_s *com; 725 726 com = (struct com_s *) device_get_softc(dev); 727 if (com == NULL) { 728 device_printf(dev, "NULL com in siounload\n"); 729 return (0); 730 } 731 com->gone = TRUE; 732 if (com->tp) 733 ttyfree(com->tp); 734 if (com->irqres) { 735 bus_teardown_intr(dev, com->irqres, com->cookie); 736 bus_release_resource(dev, SYS_RES_IRQ, 0, com->irqres); 737 } 738 if (com->ioportres) 739 bus_release_resource(dev, SYS_RES_IOPORT, com->ioportrid, 740 com->ioportres); 741 if (com->ibuf != NULL) 742 free(com->ibuf, M_DEVBUF); 743#ifdef PC98 744 if (com->obuf1 != NULL) 745 free(com->obuf1, M_DEVBUF); 746#endif 747 748 device_set_softc(dev, NULL); 749 free(com, M_DEVBUF); 750 return (0); 751} 752 753int 754sioprobe(dev, xrid, rclk, noprobe) 755 device_t dev; 756 int xrid; 757 u_long rclk; 758 int noprobe; 759{ 760#if 0 761 static bool_t already_init; 762 device_t xdev; 763#endif 764 struct com_s *com; 765 u_int divisor; 766 bool_t failures[10]; 767 int fn; 768 device_t idev; 769 Port_t iobase; 770 intrmask_t irqmap[4]; 771 intrmask_t irqs; 772 u_char mcr_image; 773 int result; 774 u_long xirq; 775 u_int flags = device_get_flags(dev); 776 int rid; 777 struct resource *port; 778#ifdef PC98 779 int tmp; 780 struct siodev iod; 781#endif 782 783#ifdef PC98 784 iod.if_type = GET_IFTYPE(flags); 785 if ((iod.if_type < 0 || iod.if_type > COM_IF_END1) && 786 (iod.if_type < 0x10 || iod.if_type > COM_IF_END2)) 787 return ENXIO; 788#endif 789 790 rid = xrid; 791#ifdef PC98 792 if (IS_8251(iod.if_type)) { 793 port = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, 794 RF_ACTIVE); 795 } else if (iod.if_type == COM_IF_MODEM_CARD || 796 iod.if_type == COM_IF_RSA98III || 797 isa_get_vendorid(dev)) { 798 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 799 if_16550a_type[iod.if_type & 0x0f].iatsz, RF_ACTIVE); 800 } else { 801 port = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid, 802 if_16550a_type[iod.if_type & 0x0f].iat, 803 if_16550a_type[iod.if_type & 0x0f].iatsz, RF_ACTIVE); 804 } 805#else 806 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 807 0, ~0, IO_COMSIZE, RF_ACTIVE); 808#endif 809 if (!port) 810 return (ENXIO); 811#ifdef PC98 812 if (!IS_8251(iod.if_type)) { 813 if (isa_load_resourcev(port, 814 if_16550a_type[iod.if_type & 0x0f].iat, 815 if_16550a_type[iod.if_type & 0x0f].iatsz) != 0) { 816 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 817 return ENXIO; 818 } 819 } 820#endif 821 822 com = malloc(sizeof(*com), M_DEVBUF, M_NOWAIT | M_ZERO); 823 if (com == NULL) { 824 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 825 return (ENOMEM); 826 } 827 device_set_softc(dev, com); 828 com->bst = rman_get_bustag(port); 829 com->bsh = rman_get_bushandle(port); 830#ifdef PC98 831 if (!IS_8251(iod.if_type) && rclk == 0) 832 rclk = if_16550a_type[iod.if_type & 0x0f].rclk; 833#else 834 if (rclk == 0) 835 rclk = DEFAULT_RCLK; 836#endif 837 com->rclk = rclk; 838 839 while (sio_inited != 2) 840 if (atomic_cmpset_int(&sio_inited, 0, 1)) { 841 mtx_init(&sio_lock, sio_driver_name, NULL, 842 (comconsole != -1) ? 843 MTX_SPIN | MTX_QUIET : MTX_SPIN); 844 atomic_store_rel_int(&sio_inited, 2); 845 } 846 847#if 0 848 /* 849 * XXX this is broken - when we are first called, there are no 850 * previously configured IO ports. We could hard code 851 * 0x3f8, 0x2f8, 0x3e8, 0x2e8 etc but that's probably worse. 852 * This code has been doing nothing since the conversion since 853 * "count" is zero the first time around. 854 */ 855 if (!already_init) { 856 /* 857 * Turn off MCR_IENABLE for all likely serial ports. An unused 858 * port with its MCR_IENABLE gate open will inhibit interrupts 859 * from any used port that shares the interrupt vector. 860 * XXX the gate enable is elsewhere for some multiports. 861 */ 862 device_t *devs; 863 int count, i, xioport; 864#ifdef PC98 865 int xiftype; 866#endif 867 868 devclass_get_devices(sio_devclass, &devs, &count); 869#ifdef PC98 870 for (i = 0; i < count; i++) { 871 xdev = devs[i]; 872 xioport = bus_get_resource_start(xdev, SYS_RES_IOPORT, 0); 873 xiftype = GET_IFTYPE(device_get_flags(xdev)); 874 if (device_is_enabled(xdev) && xioport > 0) { 875 if (IS_8251(xiftype)) 876 outb((xioport & 0xff00) | PC98SIO_cmd_port(xiftype & 0x0f), 0xf2); 877 else 878 outb(xioport + if_16550a_type[xiftype & 0x0f].iat[com_mcr], 0); 879 } 880 } 881#else 882 for (i = 0; i < count; i++) { 883 xdev = devs[i]; 884 if (device_is_enabled(xdev) && 885 bus_get_resource(xdev, SYS_RES_IOPORT, 0, &xioport, 886 NULL) == 0) 887 outb(xioport + com_mcr, 0); 888 } 889#endif 890 free(devs, M_TEMP); 891 already_init = TRUE; 892 } 893#endif 894 895 if (COM_LLCONSOLE(flags)) { 896 printf("sio%d: reserved for low-level i/o\n", 897 device_get_unit(dev)); 898 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 899 device_set_softc(dev, NULL); 900 free(com, M_DEVBUF); 901 return (ENXIO); 902 } 903 904#ifdef PC98 905 DELAY(10); 906 907 /* 908 * If the port is i8251 UART (internal, B98_01) 909 */ 910 if (pc98_check_if_type(dev, &iod) == -1) { 911 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 912 device_set_softc(dev, NULL); 913 free(com, M_DEVBUF); 914 return (ENXIO); 915 } 916 if (iod.irq > 0) 917 bus_set_resource(dev, SYS_RES_IRQ, 0, iod.irq, 1); 918 if (IS_8251(iod.if_type)) { 919 outb(iod.cmd, 0); 920 DELAY(10); 921 outb(iod.cmd, 0); 922 DELAY(10); 923 outb(iod.cmd, 0); 924 DELAY(10); 925 outb(iod.cmd, CMD8251_RESET); 926 DELAY(1000); /* for a while...*/ 927 outb(iod.cmd, 0xf2); /* MODE (dummy) */ 928 DELAY(10); 929 outb(iod.cmd, 0x01); /* CMD (dummy) */ 930 DELAY(1000); /* for a while...*/ 931 if (( inb(iod.sts) & STS8251_TxEMP ) == 0 ) { 932 result = (ENXIO); 933 } 934 if (if_8251_type[iod.if_type & 0x0f].check_irq) { 935 COM_INT_DISABLE 936 tmp = ( inb( iod.ctrl ) & ~(IEN_Rx|IEN_TxEMP|IEN_Tx)); 937 outb( iod.ctrl, tmp|IEN_TxEMP ); 938 DELAY(10); 939 result = isa_irq_pending() ? 0 : ENXIO; 940 outb( iod.ctrl, tmp ); 941 COM_INT_ENABLE 942 } else { 943 /* 944 * B98_01 doesn't activate TxEMP interrupt line 945 * when being reset, so we can't check irq pending. 946 */ 947 result = 0; 948 } 949 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 950 if (result) { 951 device_set_softc(dev, NULL); 952 free(com, M_DEVBUF); 953 } 954 return result; 955 } 956#endif /* PC98 */ 957 /* 958 * If the device is on a multiport card and has an AST/4 959 * compatible interrupt control register, initialize this 960 * register and prepare to leave MCR_IENABLE clear in the mcr. 961 * Otherwise, prepare to set MCR_IENABLE in the mcr. 962 * Point idev to the device struct giving the correct id_irq. 963 * This is the struct for the master device if there is one. 964 */ 965 idev = dev; 966 mcr_image = MCR_IENABLE; 967#ifdef COM_MULTIPORT 968 if (COM_ISMULTIPORT(flags)) { 969#ifndef PC98 970 Port_t xiobase; 971 u_long io; 972#endif 973 974 idev = devclass_get_device(sio_devclass, COM_MPMASTER(flags)); 975 if (idev == NULL) { 976 printf("sio%d: master device %d not configured\n", 977 device_get_unit(dev), COM_MPMASTER(flags)); 978 idev = dev; 979 } 980#ifndef PC98 981 if (!COM_NOTAST4(flags)) { 982 if (bus_get_resource(idev, SYS_RES_IOPORT, 0, &io, 983 NULL) == 0) { 984 xiobase = io; 985 if (bus_get_resource(idev, SYS_RES_IRQ, 0, 986 NULL, NULL) == 0) 987 outb(xiobase + com_scr, 0x80); 988 else 989 outb(xiobase + com_scr, 0); 990 } 991 mcr_image = 0; 992 } 993#endif 994 } 995#endif /* COM_MULTIPORT */ 996 if (bus_get_resource(idev, SYS_RES_IRQ, 0, NULL, NULL) != 0) 997 mcr_image = 0; 998 999 bzero(failures, sizeof failures); 1000 iobase = rman_get_start(port); 1001 1002#ifdef PC98 1003 if (iod.if_type == COM_IF_RSA98III) { 1004 mcr_image = 0; 1005 1006 outb(iobase + rsa_msr, 0x04); 1007 outb(iobase + rsa_frr, 0x00); 1008 if ((inb(iobase + rsa_srr) & 0x36) != 0x36) { 1009 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 1010 device_set_softc(dev, NULL); 1011 free(com, M_DEVBUF); 1012 return (ENXIO); 1013 } 1014 outb(iobase + rsa_ier, 0x00); 1015 outb(iobase + rsa_frr, 0x00); 1016 outb(iobase + rsa_tivsr, 0x00); 1017 outb(iobase + rsa_tcr, 0x00); 1018 } 1019 1020 tmp = if_16550a_type[iod.if_type & 0x0f].irr_write; 1021 if (tmp != -1) { 1022 /* MC16550II */ 1023 int irqout; 1024 switch (isa_get_irq(idev)) { 1025 case 3: irqout = 4; break; 1026 case 5: irqout = 5; break; 1027 case 6: irqout = 6; break; 1028 case 12: irqout = 7; break; 1029 default: 1030 printf("sio%d: irq configuration error\n", 1031 device_get_unit(dev)); 1032 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 1033 device_set_softc(dev, NULL); 1034 free(com, M_DEVBUF); 1035 return (ENXIO); 1036 } 1037 outb((iobase & 0x00ff) | tmp, irqout); 1038 } 1039#endif 1040 1041 /* 1042 * We don't want to get actual interrupts, just masked ones. 1043 * Interrupts from this line should already be masked in the ICU, 1044 * but mask them in the processor as well in case there are some 1045 * (misconfigured) shared interrupts. 1046 */ 1047 mtx_lock_spin(&sio_lock); 1048/* EXTRA DELAY? */ 1049 1050 /* 1051 * Initialize the speed and the word size and wait long enough to 1052 * drain the maximum of 16 bytes of junk in device output queues. 1053 * The speed is undefined after a master reset and must be set 1054 * before relying on anything related to output. There may be 1055 * junk after a (very fast) soft reboot and (apparently) after 1056 * master reset. 1057 * XXX what about the UART bug avoided by waiting in comparam()? 1058 * We don't want to to wait long enough to drain at 2 bps. 1059 */ 1060 if (iobase == siocniobase) 1061 DELAY((16 + 1) * 1000000 / (comdefaultrate / 10)); 1062 else { 1063 sio_setreg(com, com_cfcr, CFCR_DLAB | CFCR_8BITS); 1064 divisor = siodivisor(rclk, SIO_TEST_SPEED); 1065 sio_setreg(com, com_dlbl, divisor & 0xff); 1066 sio_setreg(com, com_dlbh, divisor >> 8); 1067 sio_setreg(com, com_cfcr, CFCR_8BITS); 1068 DELAY((16 + 1) * 1000000 / (SIO_TEST_SPEED / 10)); 1069 } 1070 1071 /* 1072 * Enable the interrupt gate and disable device interupts. This 1073 * should leave the device driving the interrupt line low and 1074 * guarantee an edge trigger if an interrupt can be generated. 1075 */ 1076/* EXTRA DELAY? */ 1077 sio_setreg(com, com_mcr, mcr_image); 1078 sio_setreg(com, com_ier, 0); 1079 DELAY(1000); /* XXX */ 1080 irqmap[0] = isa_irq_pending(); 1081 1082 /* 1083 * Attempt to set loopback mode so that we can send a null byte 1084 * without annoying any external device. 1085 */ 1086/* EXTRA DELAY? */ 1087 sio_setreg(com, com_mcr, mcr_image | MCR_LOOPBACK); 1088 1089 /* 1090 * Attempt to generate an output interrupt. On 8250's, setting 1091 * IER_ETXRDY generates an interrupt independent of the current 1092 * setting and independent of whether the THR is empty. On 16450's, 1093 * setting IER_ETXRDY generates an interrupt independent of the 1094 * current setting. On 16550A's, setting IER_ETXRDY only 1095 * generates an interrupt when IER_ETXRDY is not already set. 1096 */ 1097 sio_setreg(com, com_ier, IER_ETXRDY); 1098#ifdef PC98 1099 if (iod.if_type == COM_IF_RSA98III) 1100 outb(iobase + rsa_ier, 0x04); 1101#endif 1102 1103 /* 1104 * On some 16x50 incompatibles, setting IER_ETXRDY doesn't generate 1105 * an interrupt. They'd better generate one for actually doing 1106 * output. Loopback may be broken on the same incompatibles but 1107 * it's unlikely to do more than allow the null byte out. 1108 */ 1109 sio_setreg(com, com_data, 0); 1110 if (iobase == siocniobase) 1111 DELAY((1 + 2) * 1000000 / (comdefaultrate / 10)); 1112 else 1113 DELAY((1 + 2) * 1000000 / (SIO_TEST_SPEED / 10)); 1114 1115 /* 1116 * Turn off loopback mode so that the interrupt gate works again 1117 * (MCR_IENABLE was hidden). This should leave the device driving 1118 * an interrupt line high. It doesn't matter if the interrupt 1119 * line oscillates while we are not looking at it, since interrupts 1120 * are disabled. 1121 */ 1122/* EXTRA DELAY? */ 1123 sio_setreg(com, com_mcr, mcr_image); 1124 1125 /* 1126 * It seems my Xircom CBEM56G Cardbus modem wants to be reset 1127 * to 8 bits *again*, or else probe test 0 will fail. 1128 * gwk@sgi.com, 4/19/2001 1129 */ 1130 sio_setreg(com, com_cfcr, CFCR_8BITS); 1131 1132 /* 1133 * Some PCMCIA cards (Palido 321s, DC-1S, ...) have the "TXRDY bug", 1134 * so we probe for a buggy IIR_TXRDY implementation even in the 1135 * noprobe case. We don't probe for it in the !noprobe case because 1136 * noprobe is always set for PCMCIA cards and the problem is not 1137 * known to affect any other cards. 1138 */ 1139 if (noprobe) { 1140 /* Read IIR a few times. */ 1141 for (fn = 0; fn < 2; fn ++) { 1142 DELAY(10000); 1143 failures[6] = sio_getreg(com, com_iir); 1144 } 1145 1146 /* IIR_TXRDY should be clear. Is it? */ 1147 result = 0; 1148 if (failures[6] & IIR_TXRDY) { 1149 /* 1150 * No. We seem to have the bug. Does our fix for 1151 * it work? 1152 */ 1153 sio_setreg(com, com_ier, 0); 1154 if (sio_getreg(com, com_iir) & IIR_NOPEND) { 1155 /* Yes. We discovered the TXRDY bug! */ 1156 SET_FLAG(dev, COM_C_IIR_TXRDYBUG); 1157 } else { 1158 /* No. Just fail. XXX */ 1159 result = ENXIO; 1160 sio_setreg(com, com_mcr, 0); 1161 } 1162 } else { 1163 /* Yes. No bug. */ 1164 CLR_FLAG(dev, COM_C_IIR_TXRDYBUG); 1165 } 1166 sio_setreg(com, com_ier, 0); 1167 sio_setreg(com, com_cfcr, CFCR_8BITS); 1168 mtx_unlock_spin(&sio_lock); 1169 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 1170 if (iobase == siocniobase) 1171 result = 0; 1172 if (result != 0) { 1173 device_set_softc(dev, NULL); 1174 free(com, M_DEVBUF); 1175 } 1176 return (result); 1177 } 1178 1179 /* 1180 * Check that 1181 * o the CFCR, IER and MCR in UART hold the values written to them 1182 * (the values happen to be all distinct - this is good for 1183 * avoiding false positive tests from bus echoes). 1184 * o an output interrupt is generated and its vector is correct. 1185 * o the interrupt goes away when the IIR in the UART is read. 1186 */ 1187/* EXTRA DELAY? */ 1188 failures[0] = sio_getreg(com, com_cfcr) - CFCR_8BITS; 1189 failures[1] = sio_getreg(com, com_ier) - IER_ETXRDY; 1190 failures[2] = sio_getreg(com, com_mcr) - mcr_image; 1191 DELAY(10000); /* Some internal modems need this time */ 1192 irqmap[1] = isa_irq_pending(); 1193 failures[4] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_TXRDY; 1194#ifdef PC98 1195 if (iod.if_type == COM_IF_RSA98III) 1196 inb(iobase + rsa_srr); 1197#endif 1198 DELAY(1000); /* XXX */ 1199 irqmap[2] = isa_irq_pending(); 1200 failures[6] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 1201#ifdef PC98 1202 if (iod.if_type == COM_IF_RSA98III) 1203 inb(iobase + rsa_srr); 1204#endif 1205 1206 /* 1207 * Turn off all device interrupts and check that they go off properly. 1208 * Leave MCR_IENABLE alone. For ports without a master port, it gates 1209 * the OUT2 output of the UART to 1210 * the ICU input. Closing the gate would give a floating ICU input 1211 * (unless there is another device driving it) and spurious interrupts. 1212 * (On the system that this was first tested on, the input floats high 1213 * and gives a (masked) interrupt as soon as the gate is closed.) 1214 */ 1215 sio_setreg(com, com_ier, 0); 1216 sio_setreg(com, com_cfcr, CFCR_8BITS); /* dummy to avoid bus echo */ 1217 failures[7] = sio_getreg(com, com_ier); 1218#ifdef PC98 1219 if (iod.if_type == COM_IF_RSA98III) 1220 outb(iobase + rsa_ier, 0x00); 1221#endif 1222 DELAY(1000); /* XXX */ 1223 irqmap[3] = isa_irq_pending(); 1224 failures[9] = (sio_getreg(com, com_iir) & IIR_IMASK) - IIR_NOPEND; 1225#ifdef PC98 1226 if (iod.if_type == COM_IF_RSA98III) { 1227 inb(iobase + rsa_srr); 1228 outb(iobase + rsa_frr, 0x00); 1229 } 1230#endif 1231 1232 mtx_unlock_spin(&sio_lock); 1233 1234 irqs = irqmap[1] & ~irqmap[0]; 1235 if (bus_get_resource(idev, SYS_RES_IRQ, 0, &xirq, NULL) == 0 && 1236 ((1 << xirq) & irqs) == 0) { 1237 printf( 1238 "sio%d: configured irq %ld not in bitmap of probed irqs %#x\n", 1239 device_get_unit(dev), xirq, irqs); 1240 printf( 1241 "sio%d: port may not be enabled\n", 1242 device_get_unit(dev)); 1243 } 1244 if (bootverbose) 1245 printf("sio%d: irq maps: %#x %#x %#x %#x\n", 1246 device_get_unit(dev), 1247 irqmap[0], irqmap[1], irqmap[2], irqmap[3]); 1248 1249 result = 0; 1250 for (fn = 0; fn < sizeof failures; ++fn) 1251 if (failures[fn]) { 1252 sio_setreg(com, com_mcr, 0); 1253 result = ENXIO; 1254 if (bootverbose) { 1255 printf("sio%d: probe failed test(s):", 1256 device_get_unit(dev)); 1257 for (fn = 0; fn < sizeof failures; ++fn) 1258 if (failures[fn]) 1259 printf(" %d", fn); 1260 printf("\n"); 1261 } 1262 break; 1263 } 1264 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 1265 if (iobase == siocniobase) 1266 result = 0; 1267 if (result != 0) { 1268 device_set_softc(dev, NULL); 1269 free(com, M_DEVBUF); 1270 } 1271 return (result); 1272} 1273 1274#ifdef COM_ESP 1275static int 1276espattach(com, esp_port) 1277 struct com_s *com; 1278 Port_t esp_port; 1279{ 1280 u_char dips; 1281 u_char val; 1282 1283 /* 1284 * Check the ESP-specific I/O port to see if we're an ESP 1285 * card. If not, return failure immediately. 1286 */ 1287 if ((inb(esp_port) & 0xf3) == 0) { 1288 printf(" port 0x%x is not an ESP board?\n", esp_port); 1289 return (0); 1290 } 1291 1292 /* 1293 * We've got something that claims to be a Hayes ESP card. 1294 * Let's hope so. 1295 */ 1296 1297 /* Get the dip-switch configuration */ 1298#ifdef PC98 1299 outb(esp_port + ESP98_CMD1, ESP_GETDIPS); 1300 dips = inb(esp_port + ESP98_STATUS1); 1301#else 1302 outb(esp_port + ESP_CMD1, ESP_GETDIPS); 1303 dips = inb(esp_port + ESP_STATUS1); 1304#endif 1305 1306 /* 1307 * Bits 0,1 of dips say which COM port we are. 1308 */ 1309#ifdef PC98 1310 if ((rman_get_start(com->ioportres) & 0xff) == 1311 likely_com_ports[dips & 0x03]) 1312#else 1313 if (rman_get_start(com->ioportres) == likely_com_ports[dips & 0x03]) 1314#endif 1315 printf(" : ESP"); 1316 else { 1317 printf(" esp_port has com %d\n", dips & 0x03); 1318 return (0); 1319 } 1320 1321 /* 1322 * Check for ESP version 2.0 or later: bits 4,5,6 = 010. 1323 */ 1324#ifdef PC98 1325 outb(esp_port + ESP98_CMD1, ESP_GETTEST); 1326 val = inb(esp_port + ESP98_STATUS1); /* clear reg 1 */ 1327 val = inb(esp_port + ESP98_STATUS2); 1328#else 1329 outb(esp_port + ESP_CMD1, ESP_GETTEST); 1330 val = inb(esp_port + ESP_STATUS1); /* clear reg 1 */ 1331 val = inb(esp_port + ESP_STATUS2); 1332#endif 1333 if ((val & 0x70) < 0x20) { 1334 printf("-old (%o)", val & 0x70); 1335 return (0); 1336 } 1337 1338 /* 1339 * Check for ability to emulate 16550: bit 7 == 1 1340 */ 1341 if ((dips & 0x80) == 0) { 1342 printf(" slave"); 1343 return (0); 1344 } 1345 1346 /* 1347 * Okay, we seem to be a Hayes ESP card. Whee. 1348 */ 1349 com->esp = TRUE; 1350 com->esp_port = esp_port; 1351 return (1); 1352} 1353#endif /* COM_ESP */ 1354 1355int 1356sioattach(dev, xrid, rclk) 1357 device_t dev; 1358 int xrid; 1359 u_long rclk; 1360{ 1361 struct com_s *com; 1362#ifdef COM_ESP 1363 Port_t *espp; 1364#endif 1365 Port_t iobase; 1366 int unit; 1367 u_int flags; 1368 int rid; 1369 struct resource *port; 1370 int ret; 1371 int error; 1372 struct tty *tp; 1373#ifdef PC98 1374 u_char *obuf; 1375 u_long obufsize; 1376 int if_type = GET_IFTYPE(device_get_flags(dev)); 1377#endif 1378 1379 rid = xrid; 1380#ifdef PC98 1381 if (IS_8251(if_type)) { 1382 port = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, 1383 RF_ACTIVE); 1384 } else if (if_type == COM_IF_MODEM_CARD || 1385 if_type == COM_IF_RSA98III || 1386 isa_get_vendorid(dev)) { 1387 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 0, ~0, 1388 if_16550a_type[if_type & 0x0f].iatsz, RF_ACTIVE); 1389 } else { 1390 port = isa_alloc_resourcev(dev, SYS_RES_IOPORT, &rid, 1391 if_16550a_type[if_type & 0x0f].iat, 1392 if_16550a_type[if_type & 0x0f].iatsz, RF_ACTIVE); 1393 } 1394#else 1395 port = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid, 1396 0, ~0, IO_COMSIZE, RF_ACTIVE); 1397#endif 1398 if (!port) 1399 return (ENXIO); 1400#ifdef PC98 1401 if (!IS_8251(if_type)) { 1402 if (isa_load_resourcev(port, 1403 if_16550a_type[if_type & 0x0f].iat, 1404 if_16550a_type[if_type & 0x0f].iatsz) != 0) { 1405 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 1406 return ENXIO; 1407 } 1408 } 1409#endif 1410 1411 iobase = rman_get_start(port); 1412 unit = device_get_unit(dev); 1413 com = device_get_softc(dev); 1414 flags = device_get_flags(dev); 1415 1416 if (unit >= sio_numunits) 1417 sio_numunits = unit + 1; 1418 1419#ifdef PC98 1420 obufsize = 256; 1421 if (if_type == COM_IF_RSA98III) 1422 obufsize = 2048; 1423 if ((obuf = malloc(obufsize * 2, M_DEVBUF, M_NOWAIT)) == NULL) { 1424 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 1425 return ENXIO; 1426 } 1427 bzero(obuf, obufsize * 2); 1428#endif 1429 1430 /* 1431 * sioprobe() has initialized the device registers as follows: 1432 * o cfcr = CFCR_8BITS. 1433 * It is most important that CFCR_DLAB is off, so that the 1434 * data port is not hidden when we enable interrupts. 1435 * o ier = 0. 1436 * Interrupts are only enabled when the line is open. 1437 * o mcr = MCR_IENABLE, or 0 if the port has AST/4 compatible 1438 * interrupt control register or the config specifies no irq. 1439 * Keeping MCR_DTR and MCR_RTS off might stop the external 1440 * device from sending before we are ready. 1441 */ 1442 bzero(com, sizeof *com); 1443 com->unit = unit; 1444 com->ioportres = port; 1445 com->ioportrid = rid; 1446 com->bst = rman_get_bustag(port); 1447 com->bsh = rman_get_bushandle(port); 1448 com->cfcr_image = CFCR_8BITS; 1449 com->loses_outints = COM_LOSESOUTINTS(flags) != 0; 1450 com->no_irq = bus_get_resource(dev, SYS_RES_IRQ, 0, NULL, NULL) != 0; 1451 com->tx_fifo_size = 1; 1452#ifdef PC98 1453 com->obufsize = obufsize; 1454 com->obuf1 = obuf; 1455 com->obuf2 = obuf + obufsize; 1456#endif 1457 com->obufs[0].l_head = com->obuf1; 1458 com->obufs[1].l_head = com->obuf2; 1459 1460#ifdef PC98 1461 com->pc98_if_type = if_type; 1462 1463 if (IS_8251(if_type)) { 1464 pc98_set_ioport(com); 1465 1466 if (if_type == COM_IF_INTERNAL && pc98_check_8251fifo()) { 1467 com->pc98_8251fifo = 1; 1468 com->pc98_8251fifo_enable = 0; 1469 } 1470 } else { 1471 bus_addr_t *iat = if_16550a_type[if_type & 0x0f].iat; 1472 1473 com->data_port = iobase + iat[com_data]; 1474 com->int_ctl_port = iobase + iat[com_ier]; 1475 com->int_id_port = iobase + iat[com_iir]; 1476 com->modem_ctl_port = iobase + iat[com_mcr]; 1477 com->mcr_image = inb(com->modem_ctl_port); 1478 com->line_status_port = iobase + iat[com_lsr]; 1479 com->modem_status_port = iobase + iat[com_msr]; 1480 } 1481#else /* not PC98 */ 1482 com->data_port = iobase + com_data; 1483 com->int_ctl_port = iobase + com_ier; 1484 com->int_id_port = iobase + com_iir; 1485 com->modem_ctl_port = iobase + com_mcr; 1486 com->mcr_image = inb(com->modem_ctl_port); 1487 com->line_status_port = iobase + com_lsr; 1488 com->modem_status_port = iobase + com_msr; 1489#endif 1490 1491 tp = com->tp = ttyalloc(); 1492 tp->t_oproc = comstart; 1493 tp->t_param = comparam; 1494 tp->t_stop = comstop; 1495 tp->t_modem = commodem; 1496 tp->t_break = combreak; 1497 tp->t_close = comclose; 1498 tp->t_open = comopen; 1499 tp->t_sc = com; 1500 1501#ifdef PC98 1502 if (!IS_8251(if_type) && rclk == 0) 1503 rclk = if_16550a_type[if_type & 0x0f].rclk; 1504#else 1505 if (rclk == 0) 1506 rclk = DEFAULT_RCLK; 1507#endif 1508 com->rclk = rclk; 1509 1510 if (unit == comconsole) 1511 ttyconsolemode(tp, comdefaultrate); 1512 error = siosetwater(com, tp->t_init_in.c_ispeed); 1513 mtx_unlock_spin(&sio_lock); 1514 if (error) { 1515 /* 1516 * Leave i/o resources allocated if this is a `cn'-level 1517 * console, so that other devices can't snarf them. 1518 */ 1519 if (iobase != siocniobase) 1520 bus_release_resource(dev, SYS_RES_IOPORT, rid, port); 1521 return (ENOMEM); 1522 } 1523 1524 /* attempt to determine UART type */ 1525 printf("sio%d: type", unit); 1526 1527#ifndef PC98 1528 if (!COM_ISMULTIPORT(flags) && 1529 !COM_IIR_TXRDYBUG(flags) && !COM_NOSCR(flags)) { 1530 u_char scr; 1531 u_char scr1; 1532 u_char scr2; 1533 1534 scr = sio_getreg(com, com_scr); 1535 sio_setreg(com, com_scr, 0xa5); 1536 scr1 = sio_getreg(com, com_scr); 1537 sio_setreg(com, com_scr, 0x5a); 1538 scr2 = sio_getreg(com, com_scr); 1539 sio_setreg(com, com_scr, scr); 1540 if (scr1 != 0xa5 || scr2 != 0x5a) { 1541 printf(" 8250 or not responding"); 1542 goto determined_type; 1543 } 1544 } 1545#endif /* !PC98 */ 1546#ifdef PC98 1547 if (IS_8251(com->pc98_if_type)) { 1548 if (com->pc98_8251fifo && !COM_NOFIFO(flags)) 1549 com->tx_fifo_size = 16; 1550 com_int_TxRx_disable( com ); 1551 com_cflag_and_speed_set( com, tp->t_init_in.c_cflag, comdefaultrate ); 1552 com_tiocm_bic( com, TIOCM_DTR|TIOCM_RTS|TIOCM_LE ); 1553 com_send_break_off( com ); 1554 1555 if (com->pc98_if_type == COM_IF_INTERNAL) { 1556 printf(" (internal%s%s)", 1557 com->pc98_8251fifo ? " fifo" : "", 1558 PC98SIO_baud_rate_port(com->pc98_if_type) != -1 ? 1559 " v-fast" : ""); 1560 } else { 1561 printf(" 8251%s", if_8251_type[com->pc98_if_type & 0x0f].name); 1562 } 1563 } else { 1564#endif /* PC98 */ 1565 sio_setreg(com, com_fifo, FIFO_ENABLE | FIFO_RX_HIGH); 1566 DELAY(100); 1567 switch (inb(com->int_id_port) & IIR_FIFO_MASK) { 1568 case FIFO_RX_LOW: 1569 printf(" 16450"); 1570 break; 1571 case FIFO_RX_MEDL: 1572 printf(" 16450?"); 1573 break; 1574 case FIFO_RX_MEDH: 1575 printf(" 16550?"); 1576 break; 1577 case FIFO_RX_HIGH: 1578 if (COM_NOFIFO(flags)) { 1579 printf(" 16550A fifo disabled"); 1580 break; 1581 } 1582 com->hasfifo = TRUE; 1583#ifdef PC98 1584 if (com->pc98_if_type == COM_IF_RSA98III) { 1585 com->tx_fifo_size = 2048; 1586 com->rsabase = iobase; 1587 outb(com->rsabase + rsa_ier, 0x00); 1588 outb(com->rsabase + rsa_frr, 0x00); 1589 } 1590#else 1591 if (COM_ST16650A(flags)) { 1592 printf(" ST16650A"); 1593 com->st16650a = TRUE; 1594 com->tx_fifo_size = 32; 1595 break; 1596 } 1597 if (COM_TI16754(flags)) { 1598 printf(" TI16754"); 1599 com->tx_fifo_size = 64; 1600 break; 1601 } 1602#endif 1603 printf(" 16550A"); 1604#ifdef COM_ESP 1605#ifdef PC98 1606 if (com->pc98_if_type == COM_IF_ESP98) 1607#endif 1608 for (espp = likely_esp_ports; *espp != 0; espp++) 1609 if (espattach(com, *espp)) { 1610 com->tx_fifo_size = 1024; 1611 break; 1612 } 1613 if (com->esp) 1614 break; 1615#endif 1616#ifdef PC98 1617 com->tx_fifo_size = 16; 1618#else 1619 com->tx_fifo_size = COM_FIFOSIZE(flags); 1620 if (com->tx_fifo_size == 0) 1621 com->tx_fifo_size = 16; 1622 else 1623 printf(" lookalike with %u bytes FIFO", 1624 com->tx_fifo_size); 1625#endif 1626 break; 1627 } 1628 1629#ifdef PC98 1630 if (com->pc98_if_type == COM_IF_RSB3000) { 1631 /* Set RSB-2000/3000 Extended Buffer mode. */ 1632 u_char lcr; 1633 lcr = sio_getreg(com, com_cfcr); 1634 sio_setreg(com, com_cfcr, lcr | CFCR_DLAB); 1635 sio_setreg(com, com_emr, EMR_EXBUFF | EMR_EFMODE); 1636 sio_setreg(com, com_cfcr, lcr); 1637 } 1638#endif 1639 1640#ifdef COM_ESP 1641 if (com->esp) { 1642 /* 1643 * Set 16550 compatibility mode. 1644 * We don't use the ESP_MODE_SCALE bit to increase the 1645 * fifo trigger levels because we can't handle large 1646 * bursts of input. 1647 * XXX flow control should be set in comparam(), not here. 1648 */ 1649#ifdef PC98 1650 outb(com->esp_port + ESP98_CMD1, ESP_SETMODE); 1651 outb(com->esp_port + ESP98_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO); 1652#else 1653 outb(com->esp_port + ESP_CMD1, ESP_SETMODE); 1654 outb(com->esp_port + ESP_CMD2, ESP_MODE_RTS | ESP_MODE_FIFO); 1655#endif 1656 1657 /* Set RTS/CTS flow control. */ 1658#ifdef PC98 1659 outb(com->esp_port + ESP98_CMD1, ESP_SETFLOWTYPE); 1660 outb(com->esp_port + ESP98_CMD2, ESP_FLOW_RTS); 1661 outb(com->esp_port + ESP98_CMD2, ESP_FLOW_CTS); 1662#else 1663 outb(com->esp_port + ESP_CMD1, ESP_SETFLOWTYPE); 1664 outb(com->esp_port + ESP_CMD2, ESP_FLOW_RTS); 1665 outb(com->esp_port + ESP_CMD2, ESP_FLOW_CTS); 1666#endif 1667 1668 /* Set flow-control levels. */ 1669#ifdef PC98 1670 outb(com->esp_port + ESP98_CMD1, ESP_SETRXFLOW); 1671 outb(com->esp_port + ESP98_CMD2, HIBYTE(768)); 1672 outb(com->esp_port + ESP98_CMD2, LOBYTE(768)); 1673 outb(com->esp_port + ESP98_CMD2, HIBYTE(512)); 1674 outb(com->esp_port + ESP98_CMD2, LOBYTE(512)); 1675#else 1676 outb(com->esp_port + ESP_CMD1, ESP_SETRXFLOW); 1677 outb(com->esp_port + ESP_CMD2, HIBYTE(768)); 1678 outb(com->esp_port + ESP_CMD2, LOBYTE(768)); 1679 outb(com->esp_port + ESP_CMD2, HIBYTE(512)); 1680 outb(com->esp_port + ESP_CMD2, LOBYTE(512)); 1681#endif 1682 1683#ifdef PC98 1684 /* Set UART clock prescaler. */ 1685 outb(com->esp_port + ESP98_CMD1, ESP_SETCLOCK); 1686 outb(com->esp_port + ESP98_CMD2, 2); /* 4 times */ 1687#endif 1688 } 1689#endif /* COM_ESP */ 1690 sio_setreg(com, com_fifo, 0); 1691#ifdef PC98 1692 printf("%s", if_16550a_type[com->pc98_if_type & 0x0f].name); 1693#else 1694determined_type: ; 1695#endif 1696 1697#ifdef COM_MULTIPORT 1698 if (COM_ISMULTIPORT(flags)) { 1699 device_t masterdev; 1700 1701 com->multiport = TRUE; 1702 printf(" (multiport"); 1703 if (unit == COM_MPMASTER(flags)) 1704 printf(" master"); 1705 printf(")"); 1706 masterdev = devclass_get_device(sio_devclass, 1707 COM_MPMASTER(flags)); 1708 com->no_irq = (masterdev == NULL || bus_get_resource(masterdev, 1709 SYS_RES_IRQ, 0, NULL, NULL) != 0); 1710 } 1711#endif /* COM_MULTIPORT */ 1712#ifdef PC98 1713 } 1714#endif 1715 if (unit == comconsole) 1716 printf(", console"); 1717 if (COM_IIR_TXRDYBUG(flags)) 1718 printf(" with a buggy IIR_TXRDY implementation"); 1719 printf("\n"); 1720 1721 if (sio_fast_ih == NULL) { 1722 swi_add(&tty_intr_event, "sio", siopoll, NULL, SWI_TTY, 0, 1723 &sio_fast_ih); 1724 swi_add(&clk_intr_event, "sio", siopoll, NULL, SWI_CLOCK, 0, 1725 &sio_slow_ih); 1726 } 1727 1728 com->flags = flags; 1729 com->pps.ppscap = PPS_CAPTUREASSERT | PPS_CAPTURECLEAR; 1730 tp->t_pps = &com->pps; 1731 1732 if (COM_PPSCTS(flags)) 1733 com->pps_bit = MSR_CTS; 1734 else 1735 com->pps_bit = MSR_DCD; 1736 pps_init(&com->pps); 1737 1738 rid = 0; 1739 com->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE); 1740 if (com->irqres) { 1741 ret = bus_setup_intr(dev, com->irqres, 1742 INTR_TYPE_TTY | INTR_FAST, 1743 siointr, com, &com->cookie); 1744 if (ret) { 1745 ret = bus_setup_intr(dev, 1746 com->irqres, INTR_TYPE_TTY, 1747 siointr, com, &com->cookie); 1748 if (ret == 0) 1749 device_printf(dev, "unable to activate interrupt in fast mode - using normal mode\n"); 1750 } 1751 if (ret) 1752 device_printf(dev, "could not activate interrupt\n"); 1753#if defined(KDB) && (defined(BREAK_TO_DEBUGGER) || \ 1754 defined(ALT_BREAK_TO_DEBUGGER)) 1755 /* 1756 * Enable interrupts for early break-to-debugger support 1757 * on the console. 1758 */ 1759 if (ret == 0 && unit == comconsole) 1760 outb(siocniobase + com_ier, IER_ERXRDY | IER_ERLS | 1761 IER_EMSC); 1762#endif 1763 } 1764 1765 /* We're ready, open the doors... */ 1766 ttycreate(tp, TS_CALLOUT, "d%r", unit); 1767 1768 return (0); 1769} 1770 1771static int 1772comopen(struct tty *tp, struct cdev *dev) 1773{ 1774 struct com_s *com; 1775 int i; 1776 1777 com = tp->t_sc; 1778 com->poll = com->no_irq; 1779 com->poll_output = com->loses_outints; 1780#ifdef PC98 1781 if (IS_8251(com->pc98_if_type)) { 1782 com_tiocm_bis(com, TIOCM_DTR|TIOCM_RTS); 1783 pc98_msrint_start(dev); 1784 if (com->pc98_8251fifo) { 1785 com->pc98_8251fifo_enable = 1; 1786 outb(I8251F_fcr, CTRL8251F_ENABLE | 1787 CTRL8251F_XMT_RST | CTRL8251F_RCV_RST); 1788 } 1789 } 1790#endif 1791 if (com->hasfifo) { 1792 /* 1793 * (Re)enable and drain fifos. 1794 * 1795 * Certain SMC chips cause problems if the fifos 1796 * are enabled while input is ready. Turn off the 1797 * fifo if necessary to clear the input. We test 1798 * the input ready bit after enabling the fifos 1799 * since we've already enabled them in comparam() 1800 * and to handle races between enabling and fresh 1801 * input. 1802 */ 1803 for (i = 0; i < 500; i++) { 1804 sio_setreg(com, com_fifo, 1805 FIFO_RCV_RST | FIFO_XMT_RST 1806 | com->fifo_image); 1807#ifdef PC98 1808 if (com->pc98_if_type == COM_IF_RSA98III) 1809 outb(com->rsabase + rsa_frr , 0x00); 1810#endif 1811 /* 1812 * XXX the delays are for superstitious 1813 * historical reasons. It must be less than 1814 * the character time at the maximum 1815 * supported speed (87 usec at 115200 bps 1816 * 8N1). Otherwise we might loop endlessly 1817 * if data is streaming in. We used to use 1818 * delays of 100. That usually worked 1819 * because DELAY(100) used to usually delay 1820 * for about 85 usec instead of 100. 1821 */ 1822 DELAY(50); 1823#ifdef PC98 1824 if (com->pc98_if_type == COM_IF_RSA98III ? 1825 !(inb(com->rsabase + rsa_srr) & 0x08) : 1826 !(inb(com->line_status_port) & LSR_RXRDY)) 1827 break; 1828#else 1829 if (!(inb(com->line_status_port) & LSR_RXRDY)) 1830 break; 1831#endif 1832 sio_setreg(com, com_fifo, 0); 1833 DELAY(50); 1834 (void) inb(com->data_port); 1835 } 1836 if (i == 500) 1837 return (EIO); 1838 } 1839 1840 mtx_lock_spin(&sio_lock); 1841#ifdef PC98 1842 if (IS_8251(com->pc98_if_type)) { 1843 com_tiocm_bis(com, TIOCM_LE); 1844 com->pc98_prev_modem_status = pc98_get_modem_status(com); 1845 com_int_Rx_enable(com); 1846 } else { 1847#endif 1848 (void) inb(com->line_status_port); 1849 (void) inb(com->data_port); 1850 com->prev_modem_status = com->last_modem_status 1851 = inb(com->modem_status_port); 1852 outb(com->int_ctl_port, 1853 IER_ERXRDY | IER_ERLS | IER_EMSC 1854 | (COM_IIR_TXRDYBUG(com->flags) ? 0 : IER_ETXRDY)); 1855#ifdef PC98 1856 if (com->pc98_if_type == COM_IF_RSA98III) { 1857 outb(com->rsabase + rsa_ier, 0x1d); 1858 outb(com->int_ctl_port, IER_ERLS | IER_EMSC); 1859 } 1860#endif 1861#ifdef PC98 1862 } 1863#endif 1864 mtx_unlock_spin(&sio_lock); 1865 siosettimeout(); 1866 /* XXX: should be generic ? */ 1867#ifdef PC98 1868 if ((IS_8251(com->pc98_if_type) && 1869 (pc98_get_modem_status(com) & TIOCM_CAR)) || 1870 (!IS_8251(com->pc98_if_type) && 1871 (com->prev_modem_status & MSR_DCD)) || 1872 ISCALLOUT(dev)) 1873 ttyld_modem(tp, 1); 1874#else 1875 if (com->prev_modem_status & MSR_DCD || ISCALLOUT(dev)) 1876 ttyld_modem(tp, 1); 1877#endif 1878 return (0); 1879} 1880 1881static void 1882comclose(tp) 1883 struct tty *tp; 1884{ 1885 int s; 1886 struct com_s *com; 1887 1888 s = spltty(); 1889 com = tp->t_sc; 1890 com->poll = FALSE; 1891 com->poll_output = FALSE; 1892#ifdef PC98 1893 com_send_break_off(com); 1894#else 1895 sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 1896#endif 1897 1898#if defined(KDB) && (defined(BREAK_TO_DEBUGGER) || \ 1899 defined(ALT_BREAK_TO_DEBUGGER)) 1900 /* 1901 * Leave interrupts enabled and don't clear DTR if this is the 1902 * console. This allows us to detect break-to-debugger events 1903 * while the console device is closed. 1904 */ 1905 if (com->unit != comconsole) 1906#endif 1907 { 1908#ifdef PC98 1909 int tmp; 1910 if (IS_8251(com->pc98_if_type)) 1911 com_int_TxRx_disable(com); 1912 else 1913 sio_setreg(com, com_ier, 0); 1914 if (com->pc98_if_type == COM_IF_RSA98III) 1915 outb(com->rsabase + rsa_ier, 0x00); 1916 if (IS_8251(com->pc98_if_type)) 1917 tmp = pc98_get_modem_status(com) & TIOCM_CAR; 1918 else 1919 tmp = com->prev_modem_status & MSR_DCD; 1920#else 1921 sio_setreg(com, com_ier, 0); 1922#endif 1923 if (tp->t_cflag & HUPCL 1924 /* 1925 * XXX we will miss any carrier drop between here and the 1926 * next open. Perhaps we should watch DCD even when the 1927 * port is closed; it is not sufficient to check it at 1928 * the next open because it might go up and down while 1929 * we're not watching. 1930 */ 1931 || (!tp->t_actout 1932#ifdef PC98 1933 && !(tmp) 1934#else 1935 && !(com->prev_modem_status & MSR_DCD) 1936#endif 1937 && !(tp->t_init_in.c_cflag & CLOCAL)) 1938 || !(tp->t_state & TS_ISOPEN)) { 1939#ifdef PC98 1940 if (IS_8251(com->pc98_if_type)) 1941 com_tiocm_bic(com, TIOCM_DTR|TIOCM_RTS|TIOCM_LE); 1942 else 1943#endif 1944 (void)commodem(tp, 0, SER_DTR); 1945 ttydtrwaitstart(tp); 1946 } 1947#ifdef PC98 1948 else { 1949 if (IS_8251(com->pc98_if_type)) 1950 com_tiocm_bic(com, TIOCM_LE); 1951 } 1952#endif 1953 } 1954#ifdef PC98 1955 if (com->pc98_8251fifo) { 1956 if (com->pc98_8251fifo_enable) 1957 outb(I8251F_fcr, CTRL8251F_XMT_RST | CTRL8251F_RCV_RST); 1958 com->pc98_8251fifo_enable = 0; 1959 } 1960#endif 1961 if (com->hasfifo) { 1962 /* 1963 * Disable fifos so that they are off after controlled 1964 * reboots. Some BIOSes fail to detect 16550s when the 1965 * fifos are enabled. 1966 */ 1967 sio_setreg(com, com_fifo, 0); 1968 } 1969 tp->t_actout = FALSE; 1970 wakeup(&tp->t_actout); 1971 wakeup(TSA_CARR_ON(tp)); /* restart any wopeners */ 1972 siosettimeout(); 1973 splx(s); 1974} 1975 1976static void 1977siobusycheck(chan) 1978 void *chan; 1979{ 1980 struct com_s *com; 1981 int s; 1982 1983 com = (struct com_s *)chan; 1984 1985 /* 1986 * Clear TS_BUSY if low-level output is complete. 1987 * spl locking is sufficient because siointr1() does not set CS_BUSY. 1988 * If siointr1() clears CS_BUSY after we look at it, then we'll get 1989 * called again. Reading the line status port outside of siointr1() 1990 * is safe because CS_BUSY is clear so there are no output interrupts 1991 * to lose. 1992 */ 1993 s = spltty(); 1994 if (com->state & CS_BUSY) 1995 com->extra_state &= ~CSE_BUSYCHECK; /* False alarm. */ 1996#ifdef PC98 1997 else if ((IS_8251(com->pc98_if_type) && 1998 ((com->pc98_8251fifo_enable && 1999 (inb(I8251F_lsr) & (STS8251F_TxRDY | STS8251F_TxEMP)) 2000 == (STS8251F_TxRDY | STS8251F_TxEMP)) || 2001 (!com->pc98_8251fifo_enable && 2002 (inb(com->sts_port) & (STS8251_TxRDY | STS8251_TxEMP)) 2003 == (STS8251_TxRDY | STS8251_TxEMP)))) || 2004 ((inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY)) 2005 == (LSR_TSRE | LSR_TXRDY))) { 2006#else 2007 else if ((inb(com->line_status_port) & (LSR_TSRE | LSR_TXRDY)) 2008 == (LSR_TSRE | LSR_TXRDY)) { 2009#endif 2010 com->tp->t_state &= ~TS_BUSY; 2011 ttwwakeup(com->tp); 2012 com->extra_state &= ~CSE_BUSYCHECK; 2013 } else 2014 timeout(siobusycheck, com, hz / 100); 2015 splx(s); 2016} 2017 2018static u_int 2019siodivisor(rclk, speed) 2020 u_long rclk; 2021 speed_t speed; 2022{ 2023 long actual_speed; 2024 u_int divisor; 2025 int error; 2026 2027 if (speed == 0) 2028 return (0); 2029#if UINT_MAX > (ULONG_MAX - 1) / 8 2030 if (speed > (ULONG_MAX - 1) / 8) 2031 return (0); 2032#endif 2033 divisor = (rclk / (8UL * speed) + 1) / 2; 2034 if (divisor == 0 || divisor >= 65536) 2035 return (0); 2036 actual_speed = rclk / (16UL * divisor); 2037 2038 /* 10 times error in percent: */ 2039 error = ((actual_speed - (long)speed) * 2000 / (long)speed + 1) / 2; 2040 2041 /* 3.0% maximum error tolerance: */ 2042 if (error < -30 || error > 30) 2043 return (0); 2044 2045 return (divisor); 2046} 2047 2048/* 2049 * Call this function with the sio_lock mutex held. It will return with the 2050 * lock still held. 2051 */ 2052static void 2053sioinput(com) 2054 struct com_s *com; 2055{ 2056 u_char *buf; 2057 int incc; 2058 u_char line_status; 2059 int recv_data; 2060 struct tty *tp; 2061 2062 buf = com->ibuf; 2063 tp = com->tp; 2064 if (!(tp->t_state & TS_ISOPEN) || !(tp->t_cflag & CREAD)) { 2065 com_events -= (com->iptr - com->ibuf); 2066 com->iptr = com->ibuf; 2067 return; 2068 } 2069 if (tp->t_state & TS_CAN_BYPASS_L_RINT) { 2070 /* 2071 * Avoid the grotesquely inefficient lineswitch routine 2072 * (ttyinput) in "raw" mode. It usually takes about 450 2073 * instructions (that's without canonical processing or echo!). 2074 * slinput is reasonably fast (usually 40 instructions plus 2075 * call overhead). 2076 */ 2077 do { 2078 /* 2079 * This may look odd, but it is using save-and-enable 2080 * semantics instead of the save-and-disable semantics 2081 * that are used everywhere else. 2082 */ 2083 mtx_unlock_spin(&sio_lock); 2084 incc = com->iptr - buf; 2085 if (tp->t_rawq.c_cc + incc > tp->t_ihiwat 2086 && (com->state & CS_RTS_IFLOW 2087 || tp->t_iflag & IXOFF) 2088 && !(tp->t_state & TS_TBLOCK)) 2089 ttyblock(tp); 2090 com->delta_error_counts[CE_TTY_BUF_OVERFLOW] 2091 += b_to_q((char *)buf, incc, &tp->t_rawq); 2092 buf += incc; 2093 tk_nin += incc; 2094 tk_rawcc += incc; 2095 tp->t_rawcc += incc; 2096 ttwakeup(tp); 2097 if (tp->t_state & TS_TTSTOP 2098 && (tp->t_iflag & IXANY 2099 || tp->t_cc[VSTART] == tp->t_cc[VSTOP])) { 2100 tp->t_state &= ~TS_TTSTOP; 2101 tp->t_lflag &= ~FLUSHO; 2102 comstart(tp); 2103 } 2104 mtx_lock_spin(&sio_lock); 2105 } while (buf < com->iptr); 2106 } else { 2107 do { 2108 /* 2109 * This may look odd, but it is using save-and-enable 2110 * semantics instead of the save-and-disable semantics 2111 * that are used everywhere else. 2112 */ 2113 mtx_unlock_spin(&sio_lock); 2114 line_status = buf[com->ierroff]; 2115 recv_data = *buf++; 2116 if (line_status 2117 & (LSR_BI | LSR_FE | LSR_OE | LSR_PE)) { 2118 if (line_status & LSR_BI) 2119 recv_data |= TTY_BI; 2120 if (line_status & LSR_FE) 2121 recv_data |= TTY_FE; 2122 if (line_status & LSR_OE) 2123 recv_data |= TTY_OE; 2124 if (line_status & LSR_PE) 2125 recv_data |= TTY_PE; 2126 } 2127 ttyld_rint(tp, recv_data); 2128 mtx_lock_spin(&sio_lock); 2129 } while (buf < com->iptr); 2130 } 2131 com_events -= (com->iptr - com->ibuf); 2132 com->iptr = com->ibuf; 2133 2134 /* 2135 * There is now room for another low-level buffer full of input, 2136 * so enable RTS if it is now disabled and there is room in the 2137 * high-level buffer. 2138 */ 2139#ifdef PC98 2140 if (IS_8251(com->pc98_if_type)) { 2141 if ((com->state & CS_RTS_IFLOW) && 2142 !(com_tiocm_get(com) & TIOCM_RTS) && 2143 !(tp->t_state & TS_TBLOCK)) 2144 com_tiocm_bis(com, TIOCM_RTS); 2145 } else { 2146 if ((com->state & CS_RTS_IFLOW) && 2147 !(com->mcr_image & MCR_RTS) && 2148 !(tp->t_state & TS_TBLOCK)) 2149 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 2150 } 2151#else 2152 if ((com->state & CS_RTS_IFLOW) && !(com->mcr_image & MCR_RTS) && 2153 !(tp->t_state & TS_TBLOCK)) 2154 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 2155#endif 2156} 2157 2158static void 2159siointr(arg) 2160 void *arg; 2161{ 2162 struct com_s *com; 2163#if defined(PC98) && defined(COM_MULTIPORT) 2164 u_char rsa_buf_status; 2165#endif 2166 2167#ifndef COM_MULTIPORT 2168 com = (struct com_s *)arg; 2169 2170 mtx_lock_spin(&sio_lock); 2171 siointr1(com); 2172 mtx_unlock_spin(&sio_lock); 2173#else /* COM_MULTIPORT */ 2174 bool_t possibly_more_intrs; 2175 int unit; 2176 2177 /* 2178 * Loop until there is no activity on any port. This is necessary 2179 * to get an interrupt edge more than to avoid another interrupt. 2180 * If the IRQ signal is just an OR of the IRQ signals from several 2181 * devices, then the edge from one may be lost because another is 2182 * on. 2183 */ 2184 mtx_lock_spin(&sio_lock); 2185 do { 2186 possibly_more_intrs = FALSE; 2187 for (unit = 0; unit < sio_numunits; ++unit) { 2188 com = com_addr(unit); 2189 /* 2190 * XXX COM_LOCK(); 2191 * would it work here, or be counter-productive? 2192 */ 2193#ifdef PC98 2194 if (com != NULL 2195 && !com->gone 2196 && IS_8251(com->pc98_if_type)) { 2197 siointr1(com); 2198 } else if (com != NULL 2199 && !com->gone 2200 && com->pc98_if_type == COM_IF_RSA98III) { 2201 rsa_buf_status = 2202 inb(com->rsabase + rsa_srr) & 0xc9; 2203 if ((rsa_buf_status & 0xc8) 2204 || !(rsa_buf_status & 0x01)) { 2205 siointr1(com); 2206 if (rsa_buf_status != 2207 (inb(com->rsabase + rsa_srr) & 0xc9)) 2208 possibly_more_intrs = TRUE; 2209 } 2210 } else 2211#endif 2212 if (com != NULL 2213 && !com->gone 2214 && (inb(com->int_id_port) & IIR_IMASK) 2215 != IIR_NOPEND) { 2216 siointr1(com); 2217 possibly_more_intrs = TRUE; 2218 } 2219 /* XXX COM_UNLOCK(); */ 2220 } 2221 } while (possibly_more_intrs); 2222 mtx_unlock_spin(&sio_lock); 2223#endif /* COM_MULTIPORT */ 2224} 2225 2226static struct timespec siots[8]; 2227static int siotso; 2228static int volatile siotsunit = -1; 2229 2230static int 2231sysctl_siots(SYSCTL_HANDLER_ARGS) 2232{ 2233 char buf[128]; 2234 long long delta; 2235 size_t len; 2236 int error, i, tso; 2237 2238 for (i = 1, tso = siotso; i < tso; i++) { 2239 delta = (long long)(siots[i].tv_sec - siots[i - 1].tv_sec) * 2240 1000000000 + 2241 (siots[i].tv_nsec - siots[i - 1].tv_nsec); 2242 len = sprintf(buf, "%lld\n", delta); 2243 if (delta >= 110000) 2244 len += sprintf(buf + len - 1, ": *** %ld.%09ld\n", 2245 (long)siots[i].tv_sec, siots[i].tv_nsec) - 1; 2246 if (i == tso - 1) 2247 buf[len - 1] = '\0'; 2248 error = SYSCTL_OUT(req, buf, len); 2249 if (error != 0) 2250 return (error); 2251 uio_yield(); 2252 } 2253 return (0); 2254} 2255 2256SYSCTL_PROC(_machdep, OID_AUTO, siots, CTLTYPE_STRING | CTLFLAG_RD, 2257 0, 0, sysctl_siots, "A", "sio timestamps"); 2258 2259static void 2260siointr1(com) 2261 struct com_s *com; 2262{ 2263 u_char int_ctl; 2264 u_char int_ctl_new; 2265 u_char line_status; 2266 u_char modem_status; 2267 u_char *ioptr; 2268 u_char recv_data; 2269 2270#ifdef PC98 2271 u_char tmp = 0; 2272 u_char rsa_buf_status = 0; 2273 int rsa_tx_fifo_size = 0; 2274#endif /* PC98 */ 2275 2276 if (COM_IIR_TXRDYBUG(com->flags)) { 2277 int_ctl = inb(com->int_ctl_port); 2278 int_ctl_new = int_ctl; 2279 } else { 2280 int_ctl = 0; 2281 int_ctl_new = 0; 2282 } 2283 2284 while (!com->gone) { 2285#ifdef PC98 2286status_read:; 2287 if (IS_8251(com->pc98_if_type)) { 2288 if (com->pc98_8251fifo_enable) 2289 tmp = inb(I8251F_lsr); 2290 else 2291 tmp = inb(com->sts_port); 2292more_intr: 2293 line_status = 0; 2294 if (com->pc98_8251fifo_enable) { 2295 if (tmp & STS8251F_TxRDY) line_status |= LSR_TXRDY; 2296 if (tmp & STS8251F_RxRDY) line_status |= LSR_RXRDY; 2297 if (tmp & STS8251F_TxEMP) line_status |= LSR_TSRE; 2298 if (tmp & STS8251F_PE) line_status |= LSR_PE; 2299 if (tmp & STS8251F_OE) line_status |= LSR_OE; 2300 if (tmp & STS8251F_BD_SD) line_status |= LSR_BI; 2301 } else { 2302 if (tmp & STS8251_TxRDY) line_status |= LSR_TXRDY; 2303 if (tmp & STS8251_RxRDY) line_status |= LSR_RXRDY; 2304 if (tmp & STS8251_TxEMP) line_status |= LSR_TSRE; 2305 if (tmp & STS8251_PE) line_status |= LSR_PE; 2306 if (tmp & STS8251_OE) line_status |= LSR_OE; 2307 if (tmp & STS8251_FE) line_status |= LSR_FE; 2308 if (tmp & STS8251_BD_SD) line_status |= LSR_BI; 2309 } 2310 } else { 2311#endif /* PC98 */ 2312 if (com->pps.ppsparam.mode & PPS_CAPTUREBOTH) { 2313 modem_status = inb(com->modem_status_port); 2314 if ((modem_status ^ com->last_modem_status) & 2315 com->pps_bit) { 2316 pps_capture(&com->pps); 2317 pps_event(&com->pps, 2318 (modem_status & com->pps_bit) ? 2319 PPS_CAPTUREASSERT : PPS_CAPTURECLEAR); 2320 } 2321 } 2322 line_status = inb(com->line_status_port); 2323#ifdef PC98 2324 } 2325 if (com->pc98_if_type == COM_IF_RSA98III) 2326 rsa_buf_status = inb(com->rsabase + rsa_srr); 2327#endif /* PC98 */ 2328 2329 /* input event? (check first to help avoid overruns) */ 2330#ifndef PC98 2331 while (line_status & LSR_RCV_MASK) { 2332#else 2333 while ((line_status & LSR_RCV_MASK) 2334 || (com->pc98_if_type == COM_IF_RSA98III 2335 && (rsa_buf_status & 0x08))) { 2336#endif /* PC98 */ 2337 /* break/unnattached error bits or real input? */ 2338#ifdef PC98 2339 if (IS_8251(com->pc98_if_type)) { 2340 if (com->pc98_8251fifo_enable) { 2341 recv_data = inb(I8251F_data); 2342 if (tmp & (STS8251F_PE | STS8251F_OE | 2343 STS8251F_BD_SD)) { 2344 pc98_i8251_or_cmd(com, CMD8251_ER); 2345 recv_data = 0; 2346 } 2347 } else { 2348 recv_data = inb(com->data_port); 2349 if (tmp & (STS8251_PE | STS8251_OE | 2350 STS8251_FE | STS8251_BD_SD)) { 2351 pc98_i8251_or_cmd(com, CMD8251_ER); 2352 recv_data = 0; 2353 } 2354 } 2355 } else if (com->pc98_if_type == COM_IF_RSA98III) { 2356 if (!(rsa_buf_status & 0x08)) 2357 recv_data = 0; 2358 else 2359 recv_data = inb(com->data_port); 2360 } else 2361#endif 2362 if (!(line_status & LSR_RXRDY)) 2363 recv_data = 0; 2364 else 2365 recv_data = inb(com->data_port); 2366#ifdef KDB 2367#ifdef ALT_BREAK_TO_DEBUGGER 2368 if (com->unit == comconsole && 2369 kdb_alt_break(recv_data, &com->alt_brk_state) != 0) 2370 kdb_enter("Break sequence on console"); 2371#endif /* ALT_BREAK_TO_DEBUGGER */ 2372#endif /* KDB */ 2373 if (line_status & (LSR_BI | LSR_FE | LSR_PE)) { 2374 /* 2375 * Don't store BI if IGNBRK or FE/PE if IGNPAR. 2376 * Otherwise, push the work to a higher level 2377 * (to handle PARMRK) if we're bypassing. 2378 * Otherwise, convert BI/FE and PE+INPCK to 0. 2379 * 2380 * This makes bypassing work right in the 2381 * usual "raw" case (IGNBRK set, and IGNPAR 2382 * and INPCK clear). 2383 * 2384 * Note: BI together with FE/PE means just BI. 2385 */ 2386 if (line_status & LSR_BI) { 2387#if defined(KDB) && defined(BREAK_TO_DEBUGGER) 2388 if (com->unit == comconsole) { 2389 kdb_enter("Line break on console"); 2390 goto cont; 2391 } 2392#endif 2393 if (com->tp == NULL 2394 || com->tp->t_iflag & IGNBRK) 2395 goto cont; 2396 } else { 2397 if (com->tp == NULL 2398 || com->tp->t_iflag & IGNPAR) 2399 goto cont; 2400 } 2401 if (com->tp->t_state & TS_CAN_BYPASS_L_RINT 2402 && (line_status & (LSR_BI | LSR_FE) 2403 || com->tp->t_iflag & INPCK)) 2404 recv_data = 0; 2405 } 2406 ++com->bytes_in; 2407 if (com->tp != NULL && 2408 com->tp->t_hotchar != 0 && recv_data == com->tp->t_hotchar) 2409 swi_sched(sio_fast_ih, 0); 2410 ioptr = com->iptr; 2411 if (ioptr >= com->ibufend) 2412 CE_RECORD(com, CE_INTERRUPT_BUF_OVERFLOW); 2413 else { 2414 if (com->tp != NULL && com->tp->t_do_timestamp) 2415 microtime(&com->tp->t_timestamp); 2416 ++com_events; 2417 swi_sched(sio_slow_ih, SWI_DELAY); 2418#if 0 /* for testing input latency vs efficiency */ 2419if (com->iptr - com->ibuf == 8) 2420 swi_sched(sio_fast_ih, 0); 2421#endif 2422 ioptr[0] = recv_data; 2423 ioptr[com->ierroff] = line_status; 2424 com->iptr = ++ioptr; 2425 if (ioptr == com->ihighwater 2426 && com->state & CS_RTS_IFLOW) 2427#ifdef PC98 2428 IS_8251(com->pc98_if_type) ? 2429 com_tiocm_bic(com, TIOCM_RTS) : 2430#endif 2431 outb(com->modem_ctl_port, 2432 com->mcr_image &= ~MCR_RTS); 2433 if (line_status & LSR_OE) 2434 CE_RECORD(com, CE_OVERRUN); 2435 } 2436cont: 2437 if (line_status & LSR_TXRDY 2438 && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) 2439 goto txrdy; 2440 2441 /* 2442 * "& 0x7F" is to avoid the gcc-1.40 generating a slow 2443 * jump from the top of the loop to here 2444 */ 2445#ifdef PC98 2446 if (IS_8251(com->pc98_if_type)) 2447 goto status_read; 2448 else 2449#endif 2450 line_status = inb(com->line_status_port) & 0x7F; 2451#ifdef PC98 2452 if (com->pc98_if_type == COM_IF_RSA98III) 2453 rsa_buf_status = inb(com->rsabase + rsa_srr); 2454#endif /* PC98 */ 2455 } 2456 2457 /* modem status change? (always check before doing output) */ 2458#ifdef PC98 2459 if (!IS_8251(com->pc98_if_type)) { 2460#endif 2461 modem_status = inb(com->modem_status_port); 2462 if (modem_status != com->last_modem_status) { 2463 /* 2464 * Schedule high level to handle DCD changes. Note 2465 * that we don't use the delta bits anywhere. Some 2466 * UARTs mess them up, and it's easy to remember the 2467 * previous bits and calculate the delta. 2468 */ 2469 com->last_modem_status = modem_status; 2470 if (!(com->state & CS_CHECKMSR)) { 2471 com_events += LOTS_OF_EVENTS; 2472 com->state |= CS_CHECKMSR; 2473 swi_sched(sio_fast_ih, 0); 2474 } 2475 2476 /* handle CTS change immediately for crisp flow ctl */ 2477 if (com->state & CS_CTS_OFLOW) { 2478 if (modem_status & MSR_CTS) 2479 com->state |= CS_ODEVREADY; 2480 else 2481 com->state &= ~CS_ODEVREADY; 2482 } 2483 } 2484#ifdef PC98 2485 } 2486#endif 2487 2488txrdy: 2489 /* output queued and everything ready? */ 2490#ifndef PC98 2491 if (line_status & LSR_TXRDY 2492 && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) { 2493#else 2494 if (((com->pc98_if_type == COM_IF_RSA98III) 2495 ? (rsa_buf_status & 0x02) 2496 : (line_status & LSR_TXRDY)) 2497 && com->state >= (CS_BUSY | CS_TTGO | CS_ODEVREADY)) { 2498#endif 2499#ifdef PC98 2500 Port_t tmp_data_port; 2501 2502 if (IS_8251(com->pc98_if_type) && 2503 com->pc98_8251fifo_enable) 2504 tmp_data_port = I8251F_data; 2505 else 2506 tmp_data_port = com->data_port; 2507#endif 2508 2509 ioptr = com->obufq.l_head; 2510 if (com->tx_fifo_size > 1 && com->unit != siotsunit) { 2511 u_int ocount; 2512 2513 ocount = com->obufq.l_tail - ioptr; 2514#ifdef PC98 2515 if (com->pc98_if_type == COM_IF_RSA98III) { 2516 rsa_buf_status = inb(com->rsabase + rsa_srr); 2517 rsa_tx_fifo_size = 1024; 2518 if (!(rsa_buf_status & 0x01)) 2519 rsa_tx_fifo_size = 2048; 2520 if (ocount > rsa_tx_fifo_size) 2521 ocount = rsa_tx_fifo_size; 2522 } else 2523#endif 2524 if (ocount > com->tx_fifo_size) 2525 ocount = com->tx_fifo_size; 2526 com->bytes_out += ocount; 2527 do 2528#ifdef PC98 2529 outb(tmp_data_port, *ioptr++); 2530#else 2531 outb(com->data_port, *ioptr++); 2532#endif 2533 while (--ocount != 0); 2534 } else { 2535#ifdef PC98 2536 outb(tmp_data_port, *ioptr++); 2537#else 2538 outb(com->data_port, *ioptr++); 2539#endif 2540 ++com->bytes_out; 2541 if (com->unit == siotsunit 2542 && siotso < sizeof siots / sizeof siots[0]) 2543 nanouptime(&siots[siotso++]); 2544 } 2545#ifdef PC98 2546 if (IS_8251(com->pc98_if_type)) 2547 if (!(pc98_check_i8251_interrupt(com) & IEN_TxFLAG)) 2548 com_int_Tx_enable(com); 2549#endif 2550 com->obufq.l_head = ioptr; 2551 if (COM_IIR_TXRDYBUG(com->flags)) 2552 int_ctl_new = int_ctl | IER_ETXRDY; 2553 if (ioptr >= com->obufq.l_tail) { 2554 struct lbq *qp; 2555 2556 qp = com->obufq.l_next; 2557 qp->l_queued = FALSE; 2558 qp = qp->l_next; 2559 if (qp != NULL) { 2560 com->obufq.l_head = qp->l_head; 2561 com->obufq.l_tail = qp->l_tail; 2562 com->obufq.l_next = qp; 2563 } else { 2564 /* output just completed */ 2565 if (COM_IIR_TXRDYBUG(com->flags)) 2566 int_ctl_new = int_ctl 2567 & ~IER_ETXRDY; 2568 com->state &= ~CS_BUSY; 2569#if defined(PC98) 2570 if (IS_8251(com->pc98_if_type) && 2571 pc98_check_i8251_interrupt(com) & IEN_TxFLAG) 2572 com_int_Tx_disable(com); 2573#endif 2574 } 2575 if (!(com->state & CS_ODONE)) { 2576 com_events += LOTS_OF_EVENTS; 2577 com->state |= CS_ODONE; 2578 /* handle at high level ASAP */ 2579 swi_sched(sio_fast_ih, 0); 2580 } 2581 } 2582#ifdef PC98 2583 if (COM_IIR_TXRDYBUG(com->flags) 2584 && int_ctl != int_ctl_new) { 2585 if (com->pc98_if_type == COM_IF_RSA98III) { 2586 int_ctl_new &= ~(IER_ETXRDY | IER_ERXRDY); 2587 outb(com->int_ctl_port, int_ctl_new); 2588 outb(com->rsabase + rsa_ier, 0x1d); 2589 } else 2590 outb(com->int_ctl_port, int_ctl_new); 2591 } 2592#else 2593 if (COM_IIR_TXRDYBUG(com->flags) 2594 && int_ctl != int_ctl_new) 2595 outb(com->int_ctl_port, int_ctl_new); 2596#endif 2597 } 2598#ifdef PC98 2599 else if (line_status & LSR_TXRDY) { 2600 if (IS_8251(com->pc98_if_type)) 2601 if (pc98_check_i8251_interrupt(com) & IEN_TxFLAG) 2602 com_int_Tx_disable(com); 2603 } 2604 if (IS_8251(com->pc98_if_type)) { 2605 if (com->pc98_8251fifo_enable) { 2606 if ((tmp = inb(I8251F_lsr)) & STS8251F_RxRDY) 2607 goto more_intr; 2608 } else { 2609 if ((tmp = inb(com->sts_port)) & STS8251_RxRDY) 2610 goto more_intr; 2611 } 2612 } 2613#endif 2614 2615 /* finished? */ 2616#ifndef COM_MULTIPORT 2617#ifdef PC98 2618 if (IS_8251(com->pc98_if_type)) 2619 return; 2620#endif 2621 if ((inb(com->int_id_port) & IIR_IMASK) == IIR_NOPEND) 2622#endif /* COM_MULTIPORT */ 2623 return; 2624 } 2625} 2626 2627/* software interrupt handler for SWI_TTY */ 2628static void 2629siopoll(void *dummy) 2630{ 2631 int unit; 2632 2633 if (com_events == 0) 2634 return; 2635repeat: 2636 for (unit = 0; unit < sio_numunits; ++unit) { 2637 struct com_s *com; 2638 int incc; 2639 struct tty *tp; 2640 2641 com = com_addr(unit); 2642 if (com == NULL) 2643 continue; 2644 tp = com->tp; 2645 if (tp == NULL || com->gone) { 2646 /* 2647 * Discard any events related to never-opened or 2648 * going-away devices. 2649 */ 2650 mtx_lock_spin(&sio_lock); 2651 incc = com->iptr - com->ibuf; 2652 com->iptr = com->ibuf; 2653 if (com->state & CS_CHECKMSR) { 2654 incc += LOTS_OF_EVENTS; 2655 com->state &= ~CS_CHECKMSR; 2656 } 2657 com_events -= incc; 2658 mtx_unlock_spin(&sio_lock); 2659 continue; 2660 } 2661 if (com->iptr != com->ibuf) { 2662 mtx_lock_spin(&sio_lock); 2663 sioinput(com); 2664 mtx_unlock_spin(&sio_lock); 2665 } 2666 if (com->state & CS_CHECKMSR) { 2667 u_char delta_modem_status; 2668 2669#ifdef PC98 2670 if (!IS_8251(com->pc98_if_type)) { 2671#endif 2672 mtx_lock_spin(&sio_lock); 2673 delta_modem_status = com->last_modem_status 2674 ^ com->prev_modem_status; 2675 com->prev_modem_status = com->last_modem_status; 2676 com_events -= LOTS_OF_EVENTS; 2677 com->state &= ~CS_CHECKMSR; 2678 mtx_unlock_spin(&sio_lock); 2679 if (delta_modem_status & MSR_DCD) 2680 ttyld_modem(tp, 2681 com->prev_modem_status & MSR_DCD); 2682#ifdef PC98 2683 } 2684#endif 2685 } 2686 if (com->state & CS_ODONE) { 2687 mtx_lock_spin(&sio_lock); 2688 com_events -= LOTS_OF_EVENTS; 2689 com->state &= ~CS_ODONE; 2690 mtx_unlock_spin(&sio_lock); 2691 if (!(com->state & CS_BUSY) 2692 && !(com->extra_state & CSE_BUSYCHECK)) { 2693 timeout(siobusycheck, com, hz / 100); 2694 com->extra_state |= CSE_BUSYCHECK; 2695 } 2696 ttyld_start(tp); 2697 } 2698 if (com_events == 0) 2699 break; 2700 } 2701 if (com_events >= LOTS_OF_EVENTS) 2702 goto repeat; 2703} 2704 2705static void 2706combreak(tp, sig) 2707 struct tty *tp; 2708 int sig; 2709{ 2710 struct com_s *com; 2711 2712 com = tp->t_sc; 2713 2714#ifdef PC98 2715 if (sig) 2716 com_send_break_on(com); 2717 else 2718 com_send_break_off(com); 2719#else 2720 if (sig) 2721 sio_setreg(com, com_cfcr, com->cfcr_image |= CFCR_SBREAK); 2722 else 2723 sio_setreg(com, com_cfcr, com->cfcr_image &= ~CFCR_SBREAK); 2724#endif 2725} 2726 2727static int 2728comparam(tp, t) 2729 struct tty *tp; 2730 struct termios *t; 2731{ 2732 u_int cfcr; 2733 int cflag; 2734 struct com_s *com; 2735 u_int divisor; 2736 u_char dlbh; 2737 u_char dlbl; 2738 u_char efr_flowbits; 2739 int s; 2740#ifdef PC98 2741 u_char param = 0; 2742#endif 2743 2744 com = tp->t_sc; 2745 if (com == NULL) 2746 return (ENODEV); 2747 2748#ifdef PC98 2749 cfcr = 0; 2750 2751 if (IS_8251(com->pc98_if_type)) { 2752 if (pc98_ttspeedtab(com, t->c_ospeed, &divisor) != 0) 2753 return (EINVAL); 2754 } else { 2755#endif 2756 /* check requested parameters */ 2757 if (t->c_ispeed != (t->c_ospeed != 0 ? t->c_ospeed : tp->t_ospeed)) 2758 return (EINVAL); 2759 divisor = siodivisor(com->rclk, t->c_ispeed); 2760 if (divisor == 0) 2761 return (EINVAL); 2762#ifdef PC98 2763 } 2764#endif 2765 2766 /* parameters are OK, convert them to the com struct and the device */ 2767 s = spltty(); 2768#ifdef PC98 2769 if (IS_8251(com->pc98_if_type)) { 2770 if (t->c_ospeed == 0) 2771 com_tiocm_bic(com, TIOCM_DTR|TIOCM_RTS|TIOCM_LE); 2772 else 2773 com_tiocm_bis(com, TIOCM_DTR|TIOCM_RTS|TIOCM_LE); 2774 } else 2775#endif 2776 if (t->c_ospeed == 0) 2777 (void)commodem(tp, 0, SER_DTR); /* hang up line */ 2778 else 2779 (void)commodem(tp, SER_DTR, 0); 2780 cflag = t->c_cflag; 2781#ifdef PC98 2782 if (!IS_8251(com->pc98_if_type)) { 2783#endif 2784 switch (cflag & CSIZE) { 2785 case CS5: 2786 cfcr = CFCR_5BITS; 2787 break; 2788 case CS6: 2789 cfcr = CFCR_6BITS; 2790 break; 2791 case CS7: 2792 cfcr = CFCR_7BITS; 2793 break; 2794 default: 2795 cfcr = CFCR_8BITS; 2796 break; 2797 } 2798 if (cflag & PARENB) { 2799 cfcr |= CFCR_PENAB; 2800 if (!(cflag & PARODD)) 2801 cfcr |= CFCR_PEVEN; 2802 } 2803 if (cflag & CSTOPB) 2804 cfcr |= CFCR_STOPB; 2805 2806 if (com->hasfifo) { 2807 /* 2808 * Use a fifo trigger level low enough so that the input 2809 * latency from the fifo is less than about 16 msec and 2810 * the total latency is less than about 30 msec. These 2811 * latencies are reasonable for humans. Serial comms 2812 * protocols shouldn't expect anything better since modem 2813 * latencies are larger. 2814 * 2815 * The fifo trigger level cannot be set at RX_HIGH for high 2816 * speed connections without further work on reducing 2817 * interrupt disablement times in other parts of the system, 2818 * without producing silo overflow errors. 2819 */ 2820 com->fifo_image = com->unit == siotsunit ? 0 2821 : t->c_ispeed <= 4800 2822 ? FIFO_ENABLE : FIFO_ENABLE | FIFO_RX_MEDH; 2823#ifdef COM_ESP 2824 /* 2825 * The Hayes ESP card needs the fifo DMA mode bit set 2826 * in compatibility mode. If not, it will interrupt 2827 * for each character received. 2828 */ 2829 if (com->esp) 2830 com->fifo_image |= FIFO_DMA_MODE; 2831#endif 2832 sio_setreg(com, com_fifo, com->fifo_image); 2833 } 2834#ifdef PC98 2835 } 2836#endif 2837 2838 /* 2839 * This returns with interrupts disabled so that we can complete 2840 * the speed change atomically. Keeping interrupts disabled is 2841 * especially important while com_data is hidden. 2842 */ 2843 (void) siosetwater(com, t->c_ispeed); 2844 2845#ifdef PC98 2846 if (IS_8251(com->pc98_if_type)) 2847 com_cflag_and_speed_set(com, cflag, t->c_ospeed); 2848 else { 2849#endif 2850 sio_setreg(com, com_cfcr, cfcr | CFCR_DLAB); 2851 /* 2852 * Only set the divisor registers if they would change, since on 2853 * some 16550 incompatibles (UMC8669F), setting them while input 2854 * is arriving loses sync until data stops arriving. 2855 */ 2856 dlbl = divisor & 0xFF; 2857 if (sio_getreg(com, com_dlbl) != dlbl) 2858 sio_setreg(com, com_dlbl, dlbl); 2859 dlbh = divisor >> 8; 2860 if (sio_getreg(com, com_dlbh) != dlbh) 2861 sio_setreg(com, com_dlbh, dlbh); 2862#ifdef PC98 2863 } 2864#endif 2865 2866 efr_flowbits = 0; 2867 2868 if (cflag & CRTS_IFLOW) { 2869 com->state |= CS_RTS_IFLOW; 2870 efr_flowbits |= EFR_AUTORTS; 2871 /* 2872 * If CS_RTS_IFLOW just changed from off to on, the change 2873 * needs to be propagated to MCR_RTS. This isn't urgent, 2874 * so do it later by calling comstart() instead of repeating 2875 * a lot of code from comstart() here. 2876 */ 2877 } else if (com->state & CS_RTS_IFLOW) { 2878 com->state &= ~CS_RTS_IFLOW; 2879 /* 2880 * CS_RTS_IFLOW just changed from on to off. Force MCR_RTS 2881 * on here, since comstart() won't do it later. 2882 */ 2883#ifdef PC98 2884 if (IS_8251(com->pc98_if_type)) 2885 com_tiocm_bis(com, TIOCM_RTS); 2886 else 2887 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 2888#else 2889 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 2890#endif 2891 } 2892 2893 /* 2894 * Set up state to handle output flow control. 2895 * XXX - worth handling MDMBUF (DCD) flow control at the lowest level? 2896 * Now has 10+ msec latency, while CTS flow has 50- usec latency. 2897 */ 2898 com->state |= CS_ODEVREADY; 2899 com->state &= ~CS_CTS_OFLOW; 2900#ifdef PC98 2901 if (com->pc98_if_type == COM_IF_RSA98III) { 2902 param = inb(com->rsabase + rsa_msr); 2903 outb(com->rsabase + rsa_msr, param & 0x14); 2904 } 2905#endif 2906 if (cflag & CCTS_OFLOW) { 2907 com->state |= CS_CTS_OFLOW; 2908 efr_flowbits |= EFR_AUTOCTS; 2909#ifdef PC98 2910 if (IS_8251(com->pc98_if_type)) { 2911 if (!(pc98_get_modem_status(com) & TIOCM_CTS)) 2912 com->state &= ~CS_ODEVREADY; 2913 } else if (com->pc98_if_type == COM_IF_RSA98III) { 2914 /* Set automatic flow control mode */ 2915 outb(com->rsabase + rsa_msr, param | 0x08); 2916 } else 2917#endif 2918 if (!(com->last_modem_status & MSR_CTS)) 2919 com->state &= ~CS_ODEVREADY; 2920 } 2921 2922#ifdef PC98 2923 if (!IS_8251(com->pc98_if_type)) 2924 sio_setreg(com, com_cfcr, com->cfcr_image = cfcr); 2925#else 2926 if (com->st16650a) { 2927 sio_setreg(com, com_lcr, LCR_EFR_ENABLE); 2928 sio_setreg(com, com_efr, 2929 (sio_getreg(com, com_efr) 2930 & ~(EFR_AUTOCTS | EFR_AUTORTS)) | efr_flowbits); 2931 } 2932 sio_setreg(com, com_cfcr, com->cfcr_image = cfcr); 2933#endif 2934 2935 /* XXX shouldn't call functions while intrs are disabled. */ 2936 ttyldoptim(tp); 2937 2938 mtx_unlock_spin(&sio_lock); 2939 splx(s); 2940 comstart(tp); 2941 if (com->ibufold != NULL) { 2942 free(com->ibufold, M_DEVBUF); 2943 com->ibufold = NULL; 2944 } 2945 return (0); 2946} 2947 2948/* 2949 * This function must be called with the sio_lock mutex released and will 2950 * return with it obtained. 2951 */ 2952static int 2953siosetwater(com, speed) 2954 struct com_s *com; 2955 speed_t speed; 2956{ 2957 int cp4ticks; 2958 u_char *ibuf; 2959 int ibufsize; 2960 struct tty *tp; 2961 2962 /* 2963 * Make the buffer size large enough to handle a softtty interrupt 2964 * latency of about 2 ticks without loss of throughput or data 2965 * (about 3 ticks if input flow control is not used or not honoured, 2966 * but a bit less for CS5-CS7 modes). 2967 */ 2968 cp4ticks = speed / 10 / hz * 4; 2969 for (ibufsize = 128; ibufsize < cp4ticks;) 2970 ibufsize <<= 1; 2971#ifdef PC98 2972 if (com->pc98_if_type == COM_IF_RSA98III) 2973 ibufsize = 2048; 2974#endif 2975 if (ibufsize == com->ibufsize) { 2976 mtx_lock_spin(&sio_lock); 2977 return (0); 2978 } 2979 2980 /* 2981 * Allocate input buffer. The extra factor of 2 in the size is 2982 * to allow for an error byte for each input byte. 2983 */ 2984 ibuf = malloc(2 * ibufsize, M_DEVBUF, M_NOWAIT); 2985 if (ibuf == NULL) { 2986 mtx_lock_spin(&sio_lock); 2987 return (ENOMEM); 2988 } 2989 2990 /* Initialize non-critical variables. */ 2991 com->ibufold = com->ibuf; 2992 com->ibufsize = ibufsize; 2993 tp = com->tp; 2994 if (tp != NULL) { 2995 tp->t_ififosize = 2 * ibufsize; 2996 tp->t_ispeedwat = (speed_t)-1; 2997 tp->t_ospeedwat = (speed_t)-1; 2998 } 2999 3000 /* 3001 * Read current input buffer, if any. Continue with interrupts 3002 * disabled. 3003 */ 3004 mtx_lock_spin(&sio_lock); 3005 if (com->iptr != com->ibuf) 3006 sioinput(com); 3007 3008 /*- 3009 * Initialize critical variables, including input buffer watermarks. 3010 * The external device is asked to stop sending when the buffer 3011 * exactly reaches high water, or when the high level requests it. 3012 * The high level is notified immediately (rather than at a later 3013 * clock tick) when this watermark is reached. 3014 * The buffer size is chosen so the watermark should almost never 3015 * be reached. 3016 * The low watermark is invisibly 0 since the buffer is always 3017 * emptied all at once. 3018 */ 3019 com->iptr = com->ibuf = ibuf; 3020 com->ibufend = ibuf + ibufsize; 3021 com->ierroff = ibufsize; 3022 com->ihighwater = ibuf + 3 * ibufsize / 4; 3023 return (0); 3024} 3025 3026static void 3027comstart(tp) 3028 struct tty *tp; 3029{ 3030 struct com_s *com; 3031 int s; 3032 3033 com = tp->t_sc; 3034 if (com == NULL) 3035 return; 3036 s = spltty(); 3037 mtx_lock_spin(&sio_lock); 3038 if (tp->t_state & TS_TTSTOP) 3039 com->state &= ~CS_TTGO; 3040 else 3041 com->state |= CS_TTGO; 3042 if (tp->t_state & TS_TBLOCK) { 3043#ifdef PC98 3044 if (IS_8251(com->pc98_if_type)) { 3045 if ((com_tiocm_get(com) & TIOCM_RTS) && 3046 (com->state & CS_RTS_IFLOW)) 3047 com_tiocm_bic(com, TIOCM_RTS); 3048 } else { 3049 if ((com->mcr_image & MCR_RTS) && 3050 (com->state & CS_RTS_IFLOW)) 3051 outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS); 3052 } 3053#else 3054 if (com->mcr_image & MCR_RTS && com->state & CS_RTS_IFLOW) 3055 outb(com->modem_ctl_port, com->mcr_image &= ~MCR_RTS); 3056#endif 3057 } else { 3058#ifdef PC98 3059 if (IS_8251(com->pc98_if_type)) { 3060 if (!(com_tiocm_get(com) & TIOCM_RTS) && 3061 com->iptr < com->ihighwater && 3062 com->state & CS_RTS_IFLOW) 3063 com_tiocm_bis(com, TIOCM_RTS); 3064 } else { 3065 if (!(com->mcr_image & MCR_RTS) && 3066 com->iptr < com->ihighwater && 3067 com->state & CS_RTS_IFLOW) 3068 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 3069 } 3070#else 3071 if (!(com->mcr_image & MCR_RTS) && com->iptr < com->ihighwater 3072 && com->state & CS_RTS_IFLOW) 3073 outb(com->modem_ctl_port, com->mcr_image |= MCR_RTS); 3074#endif 3075 } 3076 mtx_unlock_spin(&sio_lock); 3077 if (tp->t_state & (TS_TIMEOUT | TS_TTSTOP)) { 3078 ttwwakeup(tp); 3079 splx(s); 3080 return; 3081 } 3082 if (tp->t_outq.c_cc != 0) { 3083 struct lbq *qp; 3084 struct lbq *next; 3085 3086 if (!com->obufs[0].l_queued) { 3087 com->obufs[0].l_tail 3088 = com->obuf1 + q_to_b(&tp->t_outq, com->obuf1, 3089#ifdef PC98 3090 com->obufsize); 3091#else 3092 sizeof com->obuf1); 3093#endif 3094 com->obufs[0].l_next = NULL; 3095 com->obufs[0].l_queued = TRUE; 3096 mtx_lock_spin(&sio_lock); 3097 if (com->state & CS_BUSY) { 3098 qp = com->obufq.l_next; 3099 while ((next = qp->l_next) != NULL) 3100 qp = next; 3101 qp->l_next = &com->obufs[0]; 3102 } else { 3103 com->obufq.l_head = com->obufs[0].l_head; 3104 com->obufq.l_tail = com->obufs[0].l_tail; 3105 com->obufq.l_next = &com->obufs[0]; 3106 com->state |= CS_BUSY; 3107 } 3108 mtx_unlock_spin(&sio_lock); 3109 } 3110 if (tp->t_outq.c_cc != 0 && !com->obufs[1].l_queued) { 3111 com->obufs[1].l_tail 3112 = com->obuf2 + q_to_b(&tp->t_outq, com->obuf2, 3113#ifdef PC98 3114 com->obufsize); 3115#else 3116 sizeof com->obuf2); 3117#endif 3118 com->obufs[1].l_next = NULL; 3119 com->obufs[1].l_queued = TRUE; 3120 mtx_lock_spin(&sio_lock); 3121 if (com->state & CS_BUSY) { 3122 qp = com->obufq.l_next; 3123 while ((next = qp->l_next) != NULL) 3124 qp = next; 3125 qp->l_next = &com->obufs[1]; 3126 } else { 3127 com->obufq.l_head = com->obufs[1].l_head; 3128 com->obufq.l_tail = com->obufs[1].l_tail; 3129 com->obufq.l_next = &com->obufs[1]; 3130 com->state |= CS_BUSY; 3131 } 3132 mtx_unlock_spin(&sio_lock); 3133 } 3134 tp->t_state |= TS_BUSY; 3135 } 3136 mtx_lock_spin(&sio_lock); 3137 if (com->state >= (CS_BUSY | CS_TTGO)) 3138 siointr1(com); /* fake interrupt to start output */ 3139 mtx_unlock_spin(&sio_lock); 3140 ttwwakeup(tp); 3141 splx(s); 3142} 3143 3144static void 3145comstop(tp, rw) 3146 struct tty *tp; 3147 int rw; 3148{ 3149 struct com_s *com; 3150#ifdef PC98 3151 int rsa98_tmp = 0; 3152#endif 3153 3154 com = tp->t_sc; 3155 if (com == NULL || com->gone) 3156 return; 3157 mtx_lock_spin(&sio_lock); 3158 if (rw & FWRITE) { 3159#ifdef PC98 3160 if (!IS_8251(com->pc98_if_type)) { 3161#endif 3162 if (com->hasfifo) 3163#ifdef COM_ESP 3164 /* XXX avoid h/w bug. */ 3165 if (!com->esp) 3166#endif 3167 sio_setreg(com, com_fifo, 3168 FIFO_XMT_RST | com->fifo_image); 3169#ifdef PC98 3170 if (com->pc98_if_type == COM_IF_RSA98III) 3171 for (rsa98_tmp = 0; rsa98_tmp < 2048; rsa98_tmp++) 3172 sio_setreg(com, com_fifo, 3173 FIFO_XMT_RST | com->fifo_image); 3174 } 3175#endif 3176 com->obufs[0].l_queued = FALSE; 3177 com->obufs[1].l_queued = FALSE; 3178 if (com->state & CS_ODONE) 3179 com_events -= LOTS_OF_EVENTS; 3180 com->state &= ~(CS_ODONE | CS_BUSY); 3181 com->tp->t_state &= ~TS_BUSY; 3182 } 3183 if (rw & FREAD) { 3184#ifdef PC98 3185 if (!IS_8251(com->pc98_if_type)) { 3186 if (com->pc98_if_type == COM_IF_RSA98III) 3187 for (rsa98_tmp = 0; rsa98_tmp < 2048; rsa98_tmp++) 3188 sio_getreg(com, com_data); 3189#endif 3190 if (com->hasfifo) 3191#ifdef COM_ESP 3192 /* XXX avoid h/w bug. */ 3193 if (!com->esp) 3194#endif 3195 sio_setreg(com, com_fifo, 3196 FIFO_RCV_RST | com->fifo_image); 3197#ifdef PC98 3198 } 3199#endif 3200 com_events -= (com->iptr - com->ibuf); 3201 com->iptr = com->ibuf; 3202 } 3203 mtx_unlock_spin(&sio_lock); 3204 comstart(tp); 3205} 3206 3207static int 3208commodem(struct tty *tp, int sigon, int sigoff) 3209{ 3210 struct com_s *com; 3211 int bitand, bitor, msr; 3212#ifdef PC98 3213 int clr, set; 3214#endif 3215 3216 com = tp->t_sc; 3217 if (com->gone) 3218 return(0); 3219 if (sigon != 0 || sigoff != 0) { 3220#ifdef PC98 3221 if (IS_8251(com->pc98_if_type)) { 3222 bitand = bitor = 0; 3223 clr = set = 0; 3224 if (sigoff & SER_DTR) { 3225 bitand |= TIOCM_DTR; 3226 clr |= CMD8251_DTR; 3227 } 3228 if (sigoff & SER_RTS) { 3229 bitand |= TIOCM_RTS; 3230 clr |= CMD8251_RxEN | CMD8251_RTS; 3231 } 3232 if (sigon & SER_DTR) { 3233 bitor |= TIOCM_DTR; 3234 set |= CMD8251_TxEN | CMD8251_RxEN | 3235 CMD8251_DTR; 3236 } 3237 if (sigon & SER_RTS) { 3238 bitor |= TIOCM_RTS; 3239 set |= CMD8251_TxEN | CMD8251_RxEN | 3240 CMD8251_RTS; 3241 } 3242 bitand = ~bitand; 3243 mtx_lock_spin(&sio_lock); 3244 com->pc98_prev_modem_status &= bitand; 3245 com->pc98_prev_modem_status |= bitor; 3246 pc98_i8251_clear_or_cmd(com, clr, set); 3247 mtx_unlock_spin(&sio_lock); 3248 return (0); 3249 } else { 3250#endif 3251 bitand = bitor = 0; 3252 if (sigoff & SER_DTR) 3253 bitand |= MCR_DTR; 3254 if (sigoff & SER_RTS) 3255 bitand |= MCR_RTS; 3256 if (sigon & SER_DTR) 3257 bitor |= MCR_DTR; 3258 if (sigon & SER_RTS) 3259 bitor |= MCR_RTS; 3260 bitand = ~bitand; 3261 mtx_lock_spin(&sio_lock); 3262 com->mcr_image &= bitand; 3263 com->mcr_image |= bitor; 3264 outb(com->modem_ctl_port, com->mcr_image); 3265 mtx_unlock_spin(&sio_lock); 3266 return (0); 3267#ifdef PC98 3268 } 3269#endif 3270 } else { 3271#ifdef PC98 3272 if (IS_8251(com->pc98_if_type)) 3273 return (com_tiocm_get(com)); 3274 else { 3275#endif 3276 bitor = 0; 3277 if (com->mcr_image & MCR_DTR) 3278 bitor |= SER_DTR; 3279 if (com->mcr_image & MCR_RTS) 3280 bitor |= SER_RTS; 3281 msr = com->prev_modem_status; 3282 if (msr & MSR_CTS) 3283 bitor |= SER_CTS; 3284 if (msr & MSR_DCD) 3285 bitor |= SER_DCD; 3286 if (msr & MSR_DSR) 3287 bitor |= SER_DSR; 3288 if (msr & MSR_DSR) 3289 bitor |= SER_DSR; 3290 if (msr & (MSR_RI | MSR_TERI)) 3291 bitor |= SER_RI; 3292 return (bitor); 3293#ifdef PC98 3294 } 3295#endif 3296 } 3297} 3298 3299static void 3300siosettimeout() 3301{ 3302 struct com_s *com; 3303 bool_t someopen; 3304 int unit; 3305 3306 /* 3307 * Set our timeout period to 1 second if no polled devices are open. 3308 * Otherwise set it to max(1/200, 1/hz). 3309 * Enable timeouts iff some device is open. 3310 */ 3311 untimeout(comwakeup, (void *)NULL, sio_timeout_handle); 3312 sio_timeout = hz; 3313 someopen = FALSE; 3314 for (unit = 0; unit < sio_numunits; ++unit) { 3315 com = com_addr(unit); 3316 if (com != NULL && com->tp != NULL 3317 && com->tp->t_state & TS_ISOPEN && !com->gone) { 3318 someopen = TRUE; 3319 if (com->poll || com->poll_output) { 3320 sio_timeout = hz > 200 ? hz / 200 : 1; 3321 break; 3322 } 3323 } 3324 } 3325 if (someopen) { 3326 sio_timeouts_until_log = hz / sio_timeout; 3327 sio_timeout_handle = timeout(comwakeup, (void *)NULL, 3328 sio_timeout); 3329 } else { 3330 /* Flush error messages, if any. */ 3331 sio_timeouts_until_log = 1; 3332 comwakeup((void *)NULL); 3333 untimeout(comwakeup, (void *)NULL, sio_timeout_handle); 3334 } 3335} 3336 3337static void 3338comwakeup(chan) 3339 void *chan; 3340{ 3341 struct com_s *com; 3342 int unit; 3343 3344 sio_timeout_handle = timeout(comwakeup, (void *)NULL, sio_timeout); 3345 3346 /* 3347 * Recover from lost output interrupts. 3348 * Poll any lines that don't use interrupts. 3349 */ 3350 for (unit = 0; unit < sio_numunits; ++unit) { 3351 com = com_addr(unit); 3352 if (com != NULL && !com->gone 3353 && (com->state >= (CS_BUSY | CS_TTGO) || com->poll)) { 3354 mtx_lock_spin(&sio_lock); 3355 siointr1(com); 3356 mtx_unlock_spin(&sio_lock); 3357 } 3358 } 3359 3360 /* 3361 * Check for and log errors, but not too often. 3362 */ 3363 if (--sio_timeouts_until_log > 0) 3364 return; 3365 sio_timeouts_until_log = hz / sio_timeout; 3366 for (unit = 0; unit < sio_numunits; ++unit) { 3367 int errnum; 3368 3369 com = com_addr(unit); 3370 if (com == NULL) 3371 continue; 3372 if (com->gone) 3373 continue; 3374 for (errnum = 0; errnum < CE_NTYPES; ++errnum) { 3375 u_int delta; 3376 u_long total; 3377 3378 mtx_lock_spin(&sio_lock); 3379 delta = com->delta_error_counts[errnum]; 3380 com->delta_error_counts[errnum] = 0; 3381 mtx_unlock_spin(&sio_lock); 3382 if (delta == 0) 3383 continue; 3384 total = com->error_counts[errnum] += delta; 3385 log(LOG_ERR, "sio%d: %u more %s%s (total %lu)\n", 3386 unit, delta, error_desc[errnum], 3387 delta == 1 ? "" : "s", total); 3388 } 3389 } 3390} 3391 3392#ifdef PC98 3393/* commint is called when modem control line changes */ 3394static void 3395commint(struct cdev *dev) 3396{ 3397 register struct tty *tp; 3398 int stat,delta; 3399 struct com_s *com; 3400 3401 com = dev->si_drv1; 3402 tp = com->tp; 3403 3404 stat = com_tiocm_get(com); 3405 delta = com_tiocm_get_delta(com); 3406 3407 if (com->state & CS_CTS_OFLOW) { 3408 if (stat & TIOCM_CTS) 3409 com->state |= CS_ODEVREADY; 3410 else 3411 com->state &= ~CS_ODEVREADY; 3412 } 3413 if ((delta & TIOCM_CAR) && (ISCALLOUT(dev)) == 0) { 3414 if (stat & TIOCM_CAR ) 3415 (void)ttyld_modem(tp, 1); 3416 else if (ttyld_modem(tp, 0) == 0) { 3417 /* negate DTR, RTS */ 3418 com_tiocm_bic(com, (tp->t_cflag & HUPCL) ? 3419 TIOCM_DTR|TIOCM_RTS|TIOCM_LE : TIOCM_LE ); 3420 /* disable IENABLE */ 3421 com_int_TxRx_disable( com ); 3422 } 3423 } 3424} 3425#endif 3426 3427/* 3428 * Following are all routines needed for SIO to act as console 3429 */ 3430struct siocnstate { 3431 u_char dlbl; 3432 u_char dlbh; 3433 u_char ier; 3434 u_char cfcr; 3435 u_char mcr; 3436}; 3437 3438/* 3439 * This is a function in order to not replicate "ttyd%d" more 3440 * places than absolutely necessary. 3441 */ 3442static void 3443siocnset(struct consdev *cd, int unit) 3444{ 3445 3446 cd->cn_unit = unit; 3447 sprintf(cd->cn_name, "ttyd%d", unit); 3448} 3449 3450static speed_t siocngetspeed(Port_t, u_long rclk); 3451static void siocnclose(struct siocnstate *sp, Port_t iobase); 3452static void siocnopen(struct siocnstate *sp, Port_t iobase, int speed); 3453static void siocntxwait(Port_t iobase); 3454 3455static cn_probe_t siocnprobe; 3456static cn_init_t siocninit; 3457static cn_term_t siocnterm; 3458static cn_checkc_t siocncheckc; 3459static cn_getc_t siocngetc; 3460static cn_putc_t siocnputc; 3461 3462CONS_DRIVER(sio, siocnprobe, siocninit, siocnterm, siocngetc, siocncheckc, 3463 siocnputc, NULL); 3464 3465static void 3466siocntxwait(iobase) 3467 Port_t iobase; 3468{ 3469 int timo; 3470 3471 /* 3472 * Wait for any pending transmission to finish. Required to avoid 3473 * the UART lockup bug when the speed is changed, and for normal 3474 * transmits. 3475 */ 3476 timo = 100000; 3477 while ((inb(iobase + com_lsr) & (LSR_TSRE | LSR_TXRDY)) 3478 != (LSR_TSRE | LSR_TXRDY) && --timo != 0) 3479 ; 3480} 3481 3482/* 3483 * Read the serial port specified and try to figure out what speed 3484 * it's currently running at. We're assuming the serial port has 3485 * been initialized and is basicly idle. This routine is only intended 3486 * to be run at system startup. 3487 * 3488 * If the value read from the serial port doesn't make sense, return 0. 3489 */ 3490 3491static speed_t 3492siocngetspeed(iobase, rclk) 3493 Port_t iobase; 3494 u_long rclk; 3495{ 3496 u_int divisor; 3497 u_char dlbh; 3498 u_char dlbl; 3499 u_char cfcr; 3500 3501 cfcr = inb(iobase + com_cfcr); 3502 outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 3503 3504 dlbl = inb(iobase + com_dlbl); 3505 dlbh = inb(iobase + com_dlbh); 3506 3507 outb(iobase + com_cfcr, cfcr); 3508 3509 divisor = dlbh << 8 | dlbl; 3510 3511 /* XXX there should be more sanity checking. */ 3512 if (divisor == 0) 3513 return (CONSPEED); 3514 return (rclk / (16UL * divisor)); 3515} 3516 3517static void 3518siocnopen(sp, iobase, speed) 3519 struct siocnstate *sp; 3520 Port_t iobase; 3521 int speed; 3522{ 3523 u_int divisor; 3524 u_char dlbh; 3525 u_char dlbl; 3526 3527 /* 3528 * Save all the device control registers except the fifo register 3529 * and set our default ones (cs8 -parenb speed=comdefaultrate). 3530 * We can't save the fifo register since it is read-only. 3531 */ 3532 sp->ier = inb(iobase + com_ier); 3533 outb(iobase + com_ier, 0); /* spltty() doesn't stop siointr() */ 3534 siocntxwait(iobase); 3535 sp->cfcr = inb(iobase + com_cfcr); 3536 outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 3537 sp->dlbl = inb(iobase + com_dlbl); 3538 sp->dlbh = inb(iobase + com_dlbh); 3539 /* 3540 * Only set the divisor registers if they would change, since on 3541 * some 16550 incompatibles (Startech), setting them clears the 3542 * data input register. This also reduces the effects of the 3543 * UMC8669F bug. 3544 */ 3545 divisor = siodivisor(comdefaultrclk, speed); 3546 dlbl = divisor & 0xFF; 3547 if (sp->dlbl != dlbl) 3548 outb(iobase + com_dlbl, dlbl); 3549 dlbh = divisor >> 8; 3550 if (sp->dlbh != dlbh) 3551 outb(iobase + com_dlbh, dlbh); 3552 outb(iobase + com_cfcr, CFCR_8BITS); 3553 sp->mcr = inb(iobase + com_mcr); 3554 /* 3555 * We don't want interrupts, but must be careful not to "disable" 3556 * them by clearing the MCR_IENABLE bit, since that might cause 3557 * an interrupt by floating the IRQ line. 3558 */ 3559 outb(iobase + com_mcr, (sp->mcr & MCR_IENABLE) | MCR_DTR | MCR_RTS); 3560} 3561 3562static void 3563siocnclose(sp, iobase) 3564 struct siocnstate *sp; 3565 Port_t iobase; 3566{ 3567 /* 3568 * Restore the device control registers. 3569 */ 3570 siocntxwait(iobase); 3571 outb(iobase + com_cfcr, CFCR_DLAB | CFCR_8BITS); 3572 if (sp->dlbl != inb(iobase + com_dlbl)) 3573 outb(iobase + com_dlbl, sp->dlbl); 3574 if (sp->dlbh != inb(iobase + com_dlbh)) 3575 outb(iobase + com_dlbh, sp->dlbh); 3576 outb(iobase + com_cfcr, sp->cfcr); 3577 /* 3578 * XXX damp oscillations of MCR_DTR and MCR_RTS by not restoring them. 3579 */ 3580 outb(iobase + com_mcr, sp->mcr | MCR_DTR | MCR_RTS); 3581 outb(iobase + com_ier, sp->ier); 3582} 3583 3584static void 3585siocnprobe(cp) 3586 struct consdev *cp; 3587{ 3588 speed_t boot_speed; 3589 u_char cfcr; 3590 u_int divisor; 3591 int s, unit; 3592 struct siocnstate sp; 3593 3594 /* 3595 * Find our first enabled console, if any. If it is a high-level 3596 * console device, then initialize it and return successfully. 3597 * If it is a low-level console device, then initialize it and 3598 * return unsuccessfully. It must be initialized in both cases 3599 * for early use by console drivers and debuggers. Initializing 3600 * the hardware is not necessary in all cases, since the i/o 3601 * routines initialize it on the fly, but it is necessary if 3602 * input might arrive while the hardware is switched back to an 3603 * uninitialized state. We can't handle multiple console devices 3604 * yet because our low-level routines don't take a device arg. 3605 * We trust the user to set the console flags properly so that we 3606 * don't need to probe. 3607 */ 3608 cp->cn_pri = CN_DEAD; 3609 3610 for (unit = 0; unit < 16; unit++) { /* XXX need to know how many */ 3611 int flags; 3612 3613 if (resource_disabled("sio", unit)) 3614 continue; 3615 if (resource_int_value("sio", unit, "flags", &flags)) 3616 continue; 3617 if (COM_CONSOLE(flags) || COM_DEBUGGER(flags)) { 3618 int port; 3619 Port_t iobase; 3620 3621 if (resource_int_value("sio", unit, "port", &port)) 3622 continue; 3623 iobase = port; 3624 s = spltty(); 3625 if (boothowto & RB_SERIAL) { 3626 boot_speed = 3627 siocngetspeed(iobase, comdefaultrclk); 3628 if (boot_speed) 3629 comdefaultrate = boot_speed; 3630 } 3631 3632 /* 3633 * Initialize the divisor latch. We can't rely on 3634 * siocnopen() to do this the first time, since it 3635 * avoids writing to the latch if the latch appears 3636 * to have the correct value. Also, if we didn't 3637 * just read the speed from the hardware, then we 3638 * need to set the speed in hardware so that 3639 * switching it later is null. 3640 */ 3641 cfcr = inb(iobase + com_cfcr); 3642 outb(iobase + com_cfcr, CFCR_DLAB | cfcr); 3643 divisor = siodivisor(comdefaultrclk, comdefaultrate); 3644 outb(iobase + com_dlbl, divisor & 0xff); 3645 outb(iobase + com_dlbh, divisor >> 8); 3646 outb(iobase + com_cfcr, cfcr); 3647 3648 siocnopen(&sp, iobase, comdefaultrate); 3649 3650 splx(s); 3651 if (COM_CONSOLE(flags) && !COM_LLCONSOLE(flags)) { 3652 siocnset(cp, unit); 3653 cp->cn_pri = COM_FORCECONSOLE(flags) 3654 || boothowto & RB_SERIAL 3655 ? CN_REMOTE : CN_NORMAL; 3656 siocniobase = iobase; 3657 siocnunit = unit; 3658 } 3659#ifdef GDB 3660 if (COM_DEBUGGER(flags)) 3661 siogdbiobase = iobase; 3662#endif 3663 } 3664 } 3665} 3666 3667static void 3668siocninit(cp) 3669 struct consdev *cp; 3670{ 3671 comconsole = cp->cn_unit; 3672} 3673 3674static void 3675siocnterm(cp) 3676 struct consdev *cp; 3677{ 3678 comconsole = -1; 3679} 3680 3681static int 3682siocncheckc(struct consdev *cd) 3683{ 3684 int c; 3685 Port_t iobase; 3686 int s; 3687 struct siocnstate sp; 3688 speed_t speed; 3689 3690 if (cd != NULL && cd->cn_unit == siocnunit) { 3691 iobase = siocniobase; 3692 speed = comdefaultrate; 3693 } else { 3694#ifdef GDB 3695 iobase = siogdbiobase; 3696 speed = gdbdefaultrate; 3697#else 3698 return (-1); 3699#endif 3700 } 3701 s = spltty(); 3702 siocnopen(&sp, iobase, speed); 3703 if (inb(iobase + com_lsr) & LSR_RXRDY) 3704 c = inb(iobase + com_data); 3705 else 3706 c = -1; 3707 siocnclose(&sp, iobase); 3708 splx(s); 3709 return (c); 3710} 3711 3712static int 3713siocngetc(struct consdev *cd) 3714{ 3715 int c; 3716 Port_t iobase; 3717 int s; 3718 struct siocnstate sp; 3719 speed_t speed; 3720 3721 if (cd != NULL && cd->cn_unit == siocnunit) { 3722 iobase = siocniobase; 3723 speed = comdefaultrate; 3724 } else { 3725#ifdef GDB 3726 iobase = siogdbiobase; 3727 speed = gdbdefaultrate; 3728#else 3729 return (-1); 3730#endif 3731 } 3732 s = spltty(); 3733 siocnopen(&sp, iobase, speed); 3734 while (!(inb(iobase + com_lsr) & LSR_RXRDY)) 3735 ; 3736 c = inb(iobase + com_data); 3737 siocnclose(&sp, iobase); 3738 splx(s); 3739 return (c); 3740} 3741 3742static void 3743siocnputc(struct consdev *cd, int c) 3744{ 3745 int need_unlock; 3746 int s; 3747 struct siocnstate sp; 3748 Port_t iobase; 3749 speed_t speed; 3750 3751 if (cd != NULL && cd->cn_unit == siocnunit) { 3752 iobase = siocniobase; 3753 speed = comdefaultrate; 3754 } else { 3755#ifdef GDB 3756 iobase = siogdbiobase; 3757 speed = gdbdefaultrate; 3758#else 3759 return; 3760#endif 3761 } 3762 s = spltty(); 3763 need_unlock = 0; 3764 if (!kdb_active && sio_inited == 2 && !mtx_owned(&sio_lock)) { 3765 mtx_lock_spin(&sio_lock); 3766 need_unlock = 1; 3767 } 3768 siocnopen(&sp, iobase, speed); 3769 siocntxwait(iobase); 3770 outb(iobase + com_data, c); 3771 siocnclose(&sp, iobase); 3772 if (need_unlock) 3773 mtx_unlock_spin(&sio_lock); 3774 splx(s); 3775} 3776 3777/* 3778 * Remote gdb(1) support. 3779 */ 3780 3781#if defined(GDB) 3782 3783#include <gdb/gdb.h> 3784 3785static gdb_probe_f siogdbprobe; 3786static gdb_init_f siogdbinit; 3787static gdb_term_f siogdbterm; 3788static gdb_getc_f siogdbgetc; 3789static gdb_checkc_f siogdbcheckc; 3790static gdb_putc_f siogdbputc; 3791 3792GDB_DBGPORT(sio, siogdbprobe, siogdbinit, siogdbterm, siogdbcheckc, 3793 siogdbgetc, siogdbputc); 3794 3795static int 3796siogdbprobe(void) 3797{ 3798 return ((siogdbiobase != 0) ? 0 : -1); 3799} 3800 3801static void 3802siogdbinit(void) 3803{ 3804} 3805 3806static void 3807siogdbterm(void) 3808{ 3809} 3810 3811static void 3812siogdbputc(int c) 3813{ 3814 siocnputc(NULL, c); 3815} 3816 3817static int 3818siogdbcheckc(void) 3819{ 3820 return (siocncheckc(NULL)); 3821} 3822 3823static int 3824siogdbgetc(void) 3825{ 3826 return (siocngetc(NULL)); 3827} 3828 3829#endif 3830 3831#ifdef PC98 3832/* 3833 * pc98 local function 3834 */ 3835static void 3836com_tiocm_bis(struct com_s *com, int msr) 3837{ 3838 int s; 3839 int tmp = 0; 3840 3841 s=spltty(); 3842 com->pc98_prev_modem_status |= ( msr & (TIOCM_LE|TIOCM_DTR|TIOCM_RTS) ); 3843 tmp |= CMD8251_TxEN|CMD8251_RxEN; 3844 if ( msr & TIOCM_DTR ) tmp |= CMD8251_DTR; 3845 if ( msr & TIOCM_RTS ) tmp |= CMD8251_RTS; 3846 3847 pc98_i8251_or_cmd( com, tmp ); 3848 splx(s); 3849} 3850 3851static void 3852com_tiocm_bic(struct com_s *com, int msr) 3853{ 3854 int s; 3855 int tmp = msr; 3856 3857 s=spltty(); 3858 com->pc98_prev_modem_status &= ~( msr & (TIOCM_LE|TIOCM_DTR|TIOCM_RTS) ); 3859 if ( msr & TIOCM_DTR ) tmp |= CMD8251_DTR; 3860 if ( msr & TIOCM_RTS ) tmp |= CMD8251_RTS; 3861 3862 pc98_i8251_clear_cmd( com, tmp ); 3863 splx(s); 3864} 3865 3866static int 3867com_tiocm_get(struct com_s *com) 3868{ 3869 return( com->pc98_prev_modem_status ); 3870} 3871 3872static int 3873com_tiocm_get_delta(struct com_s *com) 3874{ 3875 int tmp; 3876 3877 tmp = com->pc98_modem_delta; 3878 com->pc98_modem_delta = 0; 3879 return( tmp ); 3880} 3881 3882/* convert to TIOCM_?? ( ioctl.h ) */ 3883static int 3884pc98_get_modem_status(struct com_s *com) 3885{ 3886 register int msr; 3887 3888 msr = com->pc98_prev_modem_status 3889 & ~(TIOCM_CAR|TIOCM_RI|TIOCM_DSR|TIOCM_CTS); 3890 if (com->pc98_8251fifo_enable) { 3891 int stat2; 3892 3893 stat2 = inb(I8251F_msr); 3894 if ( stat2 & CICSCDF_CD ) msr |= TIOCM_CAR; 3895 if ( stat2 & CICSCDF_CI ) msr |= TIOCM_RI; 3896 if ( stat2 & CICSCDF_DR ) msr |= TIOCM_DSR; 3897 if ( stat2 & CICSCDF_CS ) msr |= TIOCM_CTS; 3898#if COM_CARRIER_DETECT_EMULATE 3899 if ( msr & (TIOCM_DSR|TIOCM_CTS) ) { 3900 msr |= TIOCM_CAR; 3901 } 3902#endif 3903 } else { 3904 int stat, stat2; 3905 3906 stat = inb(com->sts_port); 3907 stat2 = inb(com->in_modem_port); 3908 if ( !(stat2 & CICSCD_CD) ) msr |= TIOCM_CAR; 3909 if ( !(stat2 & CICSCD_CI) ) msr |= TIOCM_RI; 3910 if ( stat & STS8251_DSR ) msr |= TIOCM_DSR; 3911 if ( !(stat2 & CICSCD_CS) ) msr |= TIOCM_CTS; 3912#if COM_CARRIER_DETECT_EMULATE 3913 if ( msr & (TIOCM_DSR|TIOCM_CTS) ) { 3914 msr |= TIOCM_CAR; 3915 } 3916#endif 3917 } 3918 return(msr); 3919} 3920 3921static void 3922pc98_check_msr(void* chan) 3923{ 3924 int msr, delta; 3925 int s; 3926 register struct tty *tp; 3927 struct com_s *com; 3928 struct cdev *dev; 3929 3930 dev=(struct cdev *)chan; 3931 com = dev->si_drv1; 3932 tp = dev->si_tty; 3933 3934 s = spltty(); 3935 msr = pc98_get_modem_status(com); 3936 /* make change flag */ 3937 delta = msr ^ com->pc98_prev_modem_status; 3938 if ( delta & TIOCM_CAR ) { 3939 if ( com->modem_car_chg_timer ) { 3940 if ( -- com->modem_car_chg_timer ) 3941 msr ^= TIOCM_CAR; 3942 } else { 3943 if ((com->modem_car_chg_timer = (msr & TIOCM_CAR) ? 3944 DCD_ON_RECOGNITION : DCD_OFF_TOLERANCE) != 0) 3945 msr ^= TIOCM_CAR; 3946 } 3947 } else 3948 com->modem_car_chg_timer = 0; 3949 delta = ( msr ^ com->pc98_prev_modem_status ) & 3950 (TIOCM_CAR|TIOCM_RI|TIOCM_DSR|TIOCM_CTS); 3951 com->pc98_prev_modem_status = msr; 3952 delta = ( com->pc98_modem_delta |= delta ); 3953 splx(s); 3954 if ( com->modem_checking || (tp->t_state & (TS_ISOPEN)) ) { 3955 if ( delta ) { 3956 commint(dev); 3957 } 3958 timeout(pc98_check_msr, (caddr_t)dev, 3959 PC98_CHECK_MODEM_INTERVAL); 3960 } else { 3961 com->modem_checking = 0; 3962 } 3963} 3964 3965static void 3966pc98_msrint_start(struct cdev *dev) 3967{ 3968 struct com_s *com; 3969 int s = spltty(); 3970 3971 com = dev->si_drv1; 3972 /* modem control line check routine envoke interval is 1/10 sec */ 3973 if ( com->modem_checking == 0 ) { 3974 com->pc98_prev_modem_status = pc98_get_modem_status(com); 3975 com->pc98_modem_delta = 0; 3976 timeout(pc98_check_msr, (caddr_t)dev, 3977 PC98_CHECK_MODEM_INTERVAL); 3978 com->modem_checking = 1; 3979 } 3980 splx(s); 3981} 3982 3983static void 3984pc98_disable_i8251_interrupt(struct com_s *com, int mod) 3985{ 3986 /* disable interrupt */ 3987 register int tmp; 3988 3989 mod |= ~(IEN_Tx|IEN_TxEMP|IEN_Rx); 3990 COM_INT_DISABLE 3991 tmp = inb( com->intr_ctrl_port ) & ~(IEN_Tx|IEN_TxEMP|IEN_Rx); 3992 outb( com->intr_ctrl_port, (com->intr_enable&=~mod) | tmp ); 3993 COM_INT_ENABLE 3994} 3995 3996static void 3997pc98_enable_i8251_interrupt(struct com_s *com, int mod) 3998{ 3999 register int tmp; 4000 4001 COM_INT_DISABLE 4002 tmp = inb( com->intr_ctrl_port ) & ~(IEN_Tx|IEN_TxEMP|IEN_Rx); 4003 outb( com->intr_ctrl_port, (com->intr_enable|=mod) | tmp ); 4004 COM_INT_ENABLE 4005} 4006 4007static int 4008pc98_check_i8251_interrupt(struct com_s *com) 4009{ 4010 return ( com->intr_enable & 0x07 ); 4011} 4012 4013static void 4014pc98_i8251_clear_cmd(struct com_s *com, int x) 4015{ 4016 int tmp; 4017 4018 COM_INT_DISABLE 4019 tmp = com->pc98_prev_siocmd & ~(x); 4020 if (com->pc98_8251fifo_enable) 4021 outb(I8251F_fcr, 0); 4022 outb(com->cmd_port, tmp); 4023 com->pc98_prev_siocmd = tmp & ~(CMD8251_ER|CMD8251_RESET|CMD8251_EH); 4024 if (com->pc98_8251fifo_enable) 4025 outb(I8251F_fcr, CTRL8251F_ENABLE); 4026 COM_INT_ENABLE 4027} 4028 4029static void 4030pc98_i8251_or_cmd(struct com_s *com, int x) 4031{ 4032 int tmp; 4033 4034 COM_INT_DISABLE 4035 if (com->pc98_8251fifo_enable) 4036 outb(I8251F_fcr, 0); 4037 tmp = com->pc98_prev_siocmd | (x); 4038 outb(com->cmd_port, tmp); 4039 com->pc98_prev_siocmd = tmp & ~(CMD8251_ER|CMD8251_RESET|CMD8251_EH); 4040 if (com->pc98_8251fifo_enable) 4041 outb(I8251F_fcr, CTRL8251F_ENABLE); 4042 COM_INT_ENABLE 4043} 4044 4045static void 4046pc98_i8251_set_cmd(struct com_s *com, int x) 4047{ 4048 int tmp; 4049 4050 COM_INT_DISABLE 4051 if (com->pc98_8251fifo_enable) 4052 outb(I8251F_fcr, 0); 4053 tmp = (x); 4054 outb(com->cmd_port, tmp); 4055 com->pc98_prev_siocmd = tmp & ~(CMD8251_ER|CMD8251_RESET|CMD8251_EH); 4056 if (com->pc98_8251fifo_enable) 4057 outb(I8251F_fcr, CTRL8251F_ENABLE); 4058 COM_INT_ENABLE 4059} 4060 4061static void 4062pc98_i8251_clear_or_cmd(struct com_s *com, int clr, int x) 4063{ 4064 int tmp; 4065 COM_INT_DISABLE 4066 if (com->pc98_8251fifo_enable) 4067 outb(I8251F_fcr, 0); 4068 tmp = com->pc98_prev_siocmd & ~(clr); 4069 tmp |= (x); 4070 outb(com->cmd_port, tmp); 4071 com->pc98_prev_siocmd = tmp & ~(CMD8251_ER|CMD8251_RESET|CMD8251_EH); 4072 if (com->pc98_8251fifo_enable) 4073 outb(I8251F_fcr, CTRL8251F_ENABLE); 4074 COM_INT_ENABLE 4075} 4076 4077static int 4078pc98_i8251_get_cmd(struct com_s *com) 4079{ 4080 return com->pc98_prev_siocmd; 4081} 4082 4083static int 4084pc98_i8251_get_mod(struct com_s *com) 4085{ 4086 return com->pc98_prev_siomod; 4087} 4088 4089static void 4090pc98_i8251_reset(struct com_s *com, int mode, int command) 4091{ 4092 if (com->pc98_8251fifo_enable) 4093 outb(I8251F_fcr, 0); 4094 outb(com->cmd_port, 0); /* dummy */ 4095 DELAY(2); 4096 outb(com->cmd_port, 0); /* dummy */ 4097 DELAY(2); 4098 outb(com->cmd_port, 0); /* dummy */ 4099 DELAY(2); 4100 outb(com->cmd_port, CMD8251_RESET); /* internal reset */ 4101 DELAY(2); 4102 outb(com->cmd_port, mode ); /* mode register */ 4103 com->pc98_prev_siomod = mode; 4104 DELAY(2); 4105 pc98_i8251_set_cmd( com, (command|CMD8251_ER) ); 4106 DELAY(10); 4107 if (com->pc98_8251fifo_enable) 4108 outb(I8251F_fcr, CTRL8251F_ENABLE | 4109 CTRL8251F_XMT_RST | CTRL8251F_RCV_RST); 4110} 4111 4112static void 4113pc98_check_sysclock(void) 4114{ 4115 /* get system clock from port */ 4116 if ( pc98_machine_type & M_8M ) { 4117 /* 8 MHz system & H98 */ 4118 sysclock = 8; 4119 } else { 4120 /* 5 MHz system */ 4121 sysclock = 5; 4122 } 4123} 4124 4125static void 4126com_cflag_and_speed_set( struct com_s *com, int cflag, int speed) 4127{ 4128 int cfcr=0; 4129 int previnterrupt; 4130 int tmp; 4131 u_int count; 4132 4133 if (pc98_ttspeedtab(com, speed, &count) != 0) 4134 return; 4135 4136 previnterrupt = pc98_check_i8251_interrupt(com); 4137 pc98_disable_i8251_interrupt( com, IEN_Tx|IEN_TxEMP|IEN_Rx ); 4138 4139 switch ( cflag&CSIZE ) { 4140 case CS5: 4141 cfcr = MOD8251_5BITS; break; 4142 case CS6: 4143 cfcr = MOD8251_6BITS; break; 4144 case CS7: 4145 cfcr = MOD8251_7BITS; break; 4146 case CS8: 4147 cfcr = MOD8251_8BITS; break; 4148 } 4149 if ( cflag&PARENB ) { 4150 if ( cflag&PARODD ) 4151 cfcr |= MOD8251_PODD; 4152 else 4153 cfcr |= MOD8251_PEVEN; 4154 } else 4155 cfcr |= MOD8251_PDISAB; 4156 4157 if ( cflag&CSTOPB ) 4158 cfcr |= MOD8251_STOP2; 4159 else 4160 cfcr |= MOD8251_STOP1; 4161 4162 if ( count & 0x10000 ) 4163 cfcr |= MOD8251_CLKX1; 4164 else 4165 cfcr |= MOD8251_CLKX16; 4166 4167 while (!((tmp = inb(com->sts_port)) & STS8251_TxEMP)) 4168 ; 4169 4170 /* set baud rate from ospeed */ 4171 pc98_set_baud_rate( com, count ); 4172 4173 if ( cfcr != pc98_i8251_get_mod(com) ) 4174 pc98_i8251_reset(com, cfcr, pc98_i8251_get_cmd(com) ); 4175 4176 pc98_enable_i8251_interrupt( com, previnterrupt ); 4177} 4178 4179static int 4180pc98_ttspeedtab(struct com_s *com, int speed, u_int *divisor) 4181{ 4182 int if_type, effect_sp, count = -1, mod; 4183 4184 if_type = com->pc98_if_type & 0x0f; 4185 4186 switch (com->pc98_if_type) { 4187 case COM_IF_INTERNAL: 4188 if (PC98SIO_baud_rate_port(if_type) != -1) { 4189 count = ttspeedtab(speed, if_8251_type[if_type].speedtab); 4190 if (count > 0) { 4191 count |= COM1_EXT_CLOCK; 4192 break; 4193 } 4194 } 4195 4196 /* for *1CLK asynchronous! mode, TEFUTEFU */ 4197 mod = (sysclock == 5) ? 2457600 : 1996800; 4198 effect_sp = ttspeedtab( speed, pc98speedtab ); 4199 if ( effect_sp < 0 ) /* XXX */ 4200 effect_sp = ttspeedtab( (speed - 1), pc98speedtab ); 4201 if ( effect_sp <= 0 ) 4202 return effect_sp; 4203 if ( effect_sp == speed ) 4204 mod /= 16; 4205 if ( mod % effect_sp ) 4206 return(-1); 4207 count = mod / effect_sp; 4208 if ( count > 65535 ) 4209 return(-1); 4210 if ( effect_sp != speed ) 4211 count |= 0x10000; 4212 break; 4213 case COM_IF_PC9861K_1: 4214 case COM_IF_PC9861K_2: 4215 count = 1; 4216 break; 4217 case COM_IF_IND_SS_1: 4218 case COM_IF_IND_SS_2: 4219 case COM_IF_PIO9032B_1: 4220 case COM_IF_PIO9032B_2: 4221 count = ttspeedtab( speed, if_8251_type[if_type].speedtab ); 4222 break; 4223 case COM_IF_B98_01_1: 4224 case COM_IF_B98_01_2: 4225 count = ttspeedtab( speed, if_8251_type[if_type].speedtab ); 4226#ifdef B98_01_OLD 4227 if (count == 0 || count == 1) { 4228 count += 4; 4229 count |= 0x20000; /* x1 mode for 76800 and 153600 */ 4230 } 4231#endif 4232 break; 4233 } 4234 4235 if (count < 0) 4236 return count; 4237 4238 *divisor = (u_int) count; 4239 return 0; 4240} 4241 4242static void 4243pc98_set_baud_rate( struct com_s *com, u_int count ) 4244{ 4245 int if_type, io, s; 4246 4247 if_type = com->pc98_if_type & 0x0f; 4248 io = rman_get_start(com->ioportres) & 0xff00; 4249 4250 switch (com->pc98_if_type) { 4251 case COM_IF_INTERNAL: 4252 if (PC98SIO_baud_rate_port(if_type) != -1) { 4253 if (count & COM1_EXT_CLOCK) { 4254 outb((Port_t)PC98SIO_baud_rate_port(if_type), count & 0xff); 4255 break; 4256 } else { 4257 outb((Port_t)PC98SIO_baud_rate_port(if_type), 0x09); 4258 } 4259 } 4260 4261 if (count == 0) 4262 return; 4263 4264 /* set i8253 */ 4265 s = splclock(); 4266 if (count != 3) 4267 outb( 0x77, 0xb6 ); 4268 else 4269 outb( 0x77, 0xb4 ); 4270 outb( 0x5f, 0); 4271 outb( 0x75, count & 0xff ); 4272 outb( 0x5f, 0); 4273 outb( 0x75, (count >> 8) & 0xff ); 4274 splx(s); 4275 break; 4276 case COM_IF_IND_SS_1: 4277 case COM_IF_IND_SS_2: 4278 outb(io | PC98SIO_intr_ctrl_port(if_type), 0); 4279 outb(io | PC98SIO_baud_rate_port(if_type), 0); 4280 outb(io | PC98SIO_baud_rate_port(if_type), 0xc0); 4281 outb(io | PC98SIO_baud_rate_port(if_type), (count >> 8) | 0x80); 4282 outb(io | PC98SIO_baud_rate_port(if_type), count & 0xff); 4283 break; 4284 case COM_IF_PIO9032B_1: 4285 case COM_IF_PIO9032B_2: 4286 outb(io | PC98SIO_baud_rate_port(if_type), count); 4287 break; 4288 case COM_IF_B98_01_1: 4289 case COM_IF_B98_01_2: 4290 outb(io | PC98SIO_baud_rate_port(if_type), count & 0x0f); 4291#ifdef B98_01_OLD 4292 /* 4293 * Some old B98_01 board should be controlled 4294 * in different way, but this hasn't been tested yet. 4295 */ 4296 outb(io | PC98SIO_func_port(if_type), 4297 (count & 0x20000) ? 0xf0 : 0xf2); 4298#endif 4299 break; 4300 } 4301} 4302static int 4303pc98_check_if_type(device_t dev, struct siodev *iod) 4304{ 4305 int irr, io, if_type, tmp; 4306 static short irq_tab[2][8] = { 4307 { 3, 5, 6, 9, 10, 12, 13, -1}, 4308 { 3, 10, 12, 13, 5, 6, 9, -1} 4309 }; 4310 4311 if_type = iod->if_type & 0x0f; 4312 iod->irq = 0; 4313 io = isa_get_port(dev) & 0xff00; 4314 4315 if (IS_8251(iod->if_type)) { 4316 if (PC98SIO_func_port(if_type) != -1) { 4317 outb(io | PC98SIO_func_port(if_type), 0xf2); 4318 tmp = ttspeedtab(9600, if_8251_type[if_type].speedtab); 4319 if (tmp != -1 && PC98SIO_baud_rate_port(if_type) != -1) 4320 outb(io | PC98SIO_baud_rate_port(if_type), tmp); 4321 } 4322 4323 iod->cmd = io | PC98SIO_cmd_port(if_type); 4324 iod->sts = io | PC98SIO_sts_port(if_type); 4325 iod->mod = io | PC98SIO_in_modem_port(if_type); 4326 iod->ctrl = io | PC98SIO_intr_ctrl_port(if_type); 4327 4328 if (iod->if_type == COM_IF_INTERNAL) { 4329 iod->irq = 4; 4330 4331 if (pc98_check_8251vfast()) { 4332 PC98SIO_baud_rate_port(if_type) = I8251F_div; 4333 if_8251_type[if_type].speedtab = pc98fast_speedtab; 4334 } 4335 } else { 4336 tmp = inb( iod->mod ) & if_8251_type[if_type].irr_mask; 4337 if ((isa_get_port(dev) & 0xff) == IO_COM2) 4338 iod->irq = irq_tab[0][tmp]; 4339 else 4340 iod->irq = irq_tab[1][tmp]; 4341 } 4342 } else { 4343 irr = if_16550a_type[if_type].irr_read; 4344#ifdef COM_MULTIPORT 4345 if (!COM_ISMULTIPORT(device_get_flags(dev)) || 4346 device_get_unit(dev) == COM_MPMASTER(device_get_flags(dev))) 4347#endif 4348 if (irr != -1) { 4349 tmp = inb(io | irr); 4350 if (isa_get_port(dev) & 0x01) /* XXX depend on RSB-384 */ 4351 iod->irq = irq_tab[1][tmp >> 3]; 4352 else 4353 iod->irq = irq_tab[0][tmp & 0x07]; 4354 } 4355 } 4356 if ( iod->irq == -1 ) return -1; 4357 4358 return 0; 4359} 4360static void 4361pc98_set_ioport(struct com_s *com) 4362{ 4363 int if_type = com->pc98_if_type & 0x0f; 4364 Port_t io = rman_get_start(com->ioportres) & 0xff00; 4365 4366 pc98_check_sysclock(); 4367 com->data_port = io | PC98SIO_data_port(if_type); 4368 com->cmd_port = io | PC98SIO_cmd_port(if_type); 4369 com->sts_port = io | PC98SIO_sts_port(if_type); 4370 com->in_modem_port = io | PC98SIO_in_modem_port(if_type); 4371 com->intr_ctrl_port = io | PC98SIO_intr_ctrl_port(if_type); 4372} 4373static int 4374pc98_check_8251vfast(void) 4375{ 4376 int i; 4377 4378 outb(I8251F_div, 0x8c); 4379 DELAY(10); 4380 for (i = 0; i < 100; i++) { 4381 if ((inb(I8251F_div) & 0x80) != 0) { 4382 i = 0; 4383 break; 4384 } 4385 DELAY(1); 4386 } 4387 outb(I8251F_div, 0); 4388 DELAY(10); 4389 for (; i < 100; i++) { 4390 if ((inb(I8251F_div) & 0x80) == 0) 4391 return 1; 4392 DELAY(1); 4393 } 4394 4395 return 0; 4396} 4397static int 4398pc98_check_8251fifo(void) 4399{ 4400 u_char tmp1, tmp2; 4401 4402 tmp1 = inb(I8251F_iir); 4403 DELAY(10); 4404 tmp2 = inb(I8251F_iir); 4405 if (((tmp1 ^ tmp2) & 0x40) != 0 && ((tmp1 | tmp2) & 0x20) == 0) 4406 return 1; 4407 4408 return 0; 4409} 4410#endif /* PC98 defined */ 4411