Home | History | Annotate | Line # | Download | only in dev
sab.c revision 1.55
      1 /*	$NetBSD: sab.c,v 1.55 2017/10/31 10:45:19 martin Exp $	*/
      2 /*	$OpenBSD: sab.c,v 1.7 2002/04/08 17:49:42 jason Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 2001 Jason L. Wright (jason (at) thought.net)
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Jason L. Wright
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     24  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     25  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     26  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     27  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     30  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     32  * POSSIBILITY OF SUCH DAMAGE.
     33  *
     34  * Effort sponsored in part by the Defense Advanced Research Projects
     35  * Agency (DARPA) and Air Force Research Laboratory, Air Force
     36  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
     37  *
     38  */
     39 
     40 /*
     41  * SAB82532 Dual UART driver
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: sab.c,v 1.55 2017/10/31 10:45:19 martin Exp $");
     46 
     47 #include "opt_kgdb.h"
     48 #include <sys/types.h>
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/device.h>
     52 #include <sys/conf.h>
     53 #include <sys/file.h>
     54 #include <sys/ioctl.h>
     55 #include <sys/kernel.h>
     56 #include <sys/proc.h>
     57 #include <sys/tty.h>
     58 #include <sys/syslog.h>
     59 #include <sys/kauth.h>
     60 #include <sys/kgdb.h>
     61 #include <sys/intr.h>
     62 
     63 #include <machine/autoconf.h>
     64 #include <machine/openfirm.h>
     65 
     66 #include <dev/cons.h>
     67 
     68 #include <dev/ebus/ebusreg.h>
     69 #include <dev/ebus/ebusvar.h>
     70 #include <sparc64/dev/sab82532reg.h>
     71 
     72 #include "locators.h"
     73 
     74 #define SABUNIT(x)		TTUNIT(x)
     75 #define SABDIALOUT(x)		TTDIALOUT(x)
     76 
     77 #define	SABTTY_RBUF_SIZE	1024	/* must be divisible by 2 */
     78 
     79 struct sab_softc {
     80 	device_t		sc_dev;
     81 	struct intrhand *	sc_ih;
     82 	bus_space_tag_t		sc_bt;
     83 	bus_space_handle_t	sc_bh;
     84 	struct sabtty_softc *	sc_child[SAB_NCHAN];
     85 	u_int			sc_nchild;
     86 	void *			sc_softintr;
     87 	int			sc_node;
     88 };
     89 
     90 struct sabtty_attach_args {
     91 	u_int sbt_portno;
     92 };
     93 
     94 struct sabtty_softc {
     95 	device_t		sc_dev;
     96 	struct sab_softc *	sc_parent;
     97 	bus_space_tag_t		sc_bt;
     98 	bus_space_handle_t	sc_bh;
     99 	struct tty *		sc_tty;
    100 	u_int			sc_portno;
    101 	uint8_t			sc_pvr_dtr, sc_pvr_dsr;
    102 	uint8_t			sc_imr0, sc_imr1;
    103 	int			sc_openflags;
    104 	u_char *		sc_txp;
    105 	int			sc_txc;
    106 	int			sc_flags;
    107 #define SABTTYF_STOP		0x0001
    108 #define	SABTTYF_DONE		0x0002
    109 #define	SABTTYF_RINGOVERFLOW	0x0004
    110 #define	SABTTYF_CDCHG		0x0008
    111 #define	SABTTYF_CONS_IN		0x0010
    112 #define	SABTTYF_CONS_OUT	0x0020
    113 #define	SABTTYF_TXDRAIN		0x0040
    114 #define	SABTTYF_DONTDDB		0x0080
    115 #define SABTTYF_IS_RSC		0x0100
    116 	uint8_t			sc_rbuf[SABTTY_RBUF_SIZE];
    117 	uint8_t			*sc_rend, *sc_rput, *sc_rget;
    118 	uint8_t			sc_polling, sc_pollrfc;
    119 };
    120 
    121 struct sabtty_softc *sabtty_cons_input;
    122 struct sabtty_softc *sabtty_cons_output;
    123 
    124 #define	SAB_READ(sc,r)		\
    125     bus_space_read_1((sc)->sc_bt, (sc)->sc_bh, (r))
    126 #define	SAB_WRITE(sc,r,v)	\
    127     bus_space_write_1((sc)->sc_bt, (sc)->sc_bh, (r), (v))
    128 #define	SAB_WRITE_BLOCK(sc,r,p,c)	\
    129     bus_space_write_region_1((sc)->sc_bt, (sc)->sc_bh, (r), (p), (c))
    130 
    131 int sab_match(device_t, cfdata_t, void *);
    132 void sab_attach(device_t, device_t, void *);
    133 int sab_print(void *, const char *);
    134 int sab_intr(void *);
    135 
    136 void sab_softintr(void *);
    137 void sab_cnputc(dev_t, int);
    138 int sab_cngetc(dev_t);
    139 void sab_cnpollc(dev_t, int);
    140 
    141 int sabtty_match(device_t, cfdata_t, void *);
    142 void sabtty_attach(device_t, device_t, void *);
    143 void sabtty_start(struct tty *);
    144 int sabtty_param(struct tty *, struct termios *);
    145 int sabtty_intr(struct sabtty_softc *, int *);
    146 void sabtty_softintr(struct sabtty_softc *);
    147 int sabtty_mdmctrl(struct sabtty_softc *, int, int);
    148 void sabtty_cec_wait(struct sabtty_softc *);
    149 void sabtty_tec_wait(struct sabtty_softc *);
    150 void sabtty_reset(struct sabtty_softc *);
    151 void sabtty_flush(struct sabtty_softc *);
    152 int sabtty_speed(int);
    153 void sabtty_console_flags(struct sabtty_softc *);
    154 void sabtty_cnpollc(struct sabtty_softc *, int);
    155 bool sabtty_shutdown(device_t, int);
    156 int sabttyparam(struct sabtty_softc *, struct tty *, struct termios *);
    157 
    158 #ifdef KGDB
    159 int sab_kgdb_check(struct sabtty_softc *);
    160 void sab_kgdb_init(struct sabtty_softc *);
    161 #endif
    162 
    163 void sabtty_cnputc(struct sabtty_softc *, int);
    164 int sabtty_cngetc(struct sabtty_softc *);
    165 
    166 CFATTACH_DECL_NEW(sab, sizeof(struct sab_softc),
    167     sab_match, sab_attach, NULL, NULL);
    168 
    169 extern struct cfdriver sab_cd;
    170 
    171 CFATTACH_DECL_NEW(sabtty, sizeof(struct sabtty_softc),
    172     sabtty_match, sabtty_attach, NULL, NULL);
    173 
    174 extern struct cfdriver sabtty_cd;
    175 
    176 dev_type_open(sabopen);
    177 dev_type_close(sabclose);
    178 dev_type_read(sabread);
    179 dev_type_write(sabwrite);
    180 dev_type_ioctl(sabioctl);
    181 dev_type_stop(sabstop);
    182 dev_type_tty(sabtty);
    183 dev_type_poll(sabpoll);
    184 
    185 static struct cnm_state sabtty_cnm_state;
    186 
    187 const struct cdevsw sabtty_cdevsw = {
    188 	.d_open = sabopen,
    189 	.d_close = sabclose,
    190 	.d_read = sabread,
    191 	.d_write = sabwrite,
    192 	.d_ioctl = sabioctl,
    193 	.d_stop = sabstop,
    194 	.d_tty = sabtty,
    195 	.d_poll = sabpoll,
    196 	.d_mmap = nommap,
    197 	.d_kqfilter = ttykqfilter,
    198 	.d_discard = nodiscard,
    199 	.d_flag = D_TTY
    200 };
    201 
    202 struct sabtty_rate {
    203 	int baud;
    204 	int n, m;
    205 };
    206 
    207 struct sabtty_rate sabtty_baudtable[] = {
    208 	{      50,	35,     10 },
    209 	{      75,	47,	9 },
    210 	{     110,	32,	9 },
    211 	{     134,	53,	8 },
    212 	{     150,	47,	8 },
    213 	{     200,	35,	8 },
    214 	{     300,	47,	7 },
    215 	{     600,	47,	6 },
    216 	{    1200,	47,	5 },
    217 	{    1800,	31,	5 },
    218 	{    2400,	47,	4 },
    219 	{    4800,	47,	3 },
    220 	{    9600,	47,	2 },
    221 	{   19200,	47,	1 },
    222 	{   38400,	23,	1 },
    223 	{   57600,	15,	1 },
    224 	{  115200,	 7,	1 },
    225 	{  230400,	 3,	1 },
    226 	{  460800,	 1,	1 },
    227 	{   76800,	11,	1 },
    228 	{  153600,	 5,	1 },
    229 	{  307200,	 3,	1 },
    230 	{  614400,	 3,	0 },
    231 	{  921600,	 0,	1 },
    232 };
    233 
    234 int
    235 sab_match(device_t parent, cfdata_t match, void *aux)
    236 {
    237 	struct ebus_attach_args *ea = aux;
    238 	char *compat;
    239 
    240 	if (strcmp(ea->ea_name, "se") == 0 ||
    241 	    strcmp(ea->ea_name, "FJSV,se") == 0)
    242 		return (1);
    243 
    244 	compat = prom_getpropstring(ea->ea_node, "compatible");
    245 	if (compat != NULL && !strcmp(compat, "sab82532"))
    246 		return (1);
    247 
    248 	return (0);
    249 }
    250 
    251 void
    252 sab_attach(device_t parent, device_t self, void *aux)
    253 {
    254 	struct sab_softc *sc = device_private(self);
    255 	struct ebus_attach_args *ea = aux;
    256 	uint8_t r;
    257 	u_int i;
    258 	int locs[SABCF_NLOCS];
    259 
    260 	sc->sc_dev = self;
    261 	sc->sc_bt = ea->ea_bustag;
    262 	sc->sc_node = ea->ea_node;
    263 
    264 	/* Use prom mapping, if available. */
    265 	if (ea->ea_nvaddr)
    266 		sparc_promaddr_to_handle(sc->sc_bt, ea->ea_vaddr[0],
    267 		    &sc->sc_bh);
    268 	else if (bus_space_map(sc->sc_bt, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
    269 				 ea->ea_reg[0].size, 0, &sc->sc_bh) != 0) {
    270 		printf(": can't map register space\n");
    271 		return;
    272 	}
    273 
    274 	sc->sc_ih = bus_intr_establish(ea->ea_bustag, ea->ea_intr[0],
    275 	    IPL_TTY, sab_intr, sc);
    276 	if (sc->sc_ih == NULL) {
    277 		printf(": can't map interrupt\n");
    278 		return;
    279 	}
    280 
    281 	sc->sc_softintr = softint_establish(SOFTINT_SERIAL, sab_softintr, sc);
    282 	if (sc->sc_softintr == NULL) {
    283 		printf(": can't get soft intr\n");
    284 		return;
    285 	}
    286 
    287 	aprint_normal(": rev ");
    288 	r = SAB_READ(sc, SAB_VSTR) & SAB_VSTR_VMASK;
    289 	switch (r) {
    290 	case SAB_VSTR_V_1:
    291 		aprint_normal("1");
    292 		break;
    293 	case SAB_VSTR_V_2:
    294 		aprint_normal("2");
    295 		break;
    296 	case SAB_VSTR_V_32:
    297 		aprint_normal("3.2");
    298 		break;
    299 	default:
    300 		aprint_normal("unknown(0x%x)", r);
    301 		break;
    302 	}
    303 	aprint_normal("\n");
    304 	aprint_naive(": Serial controller\n");
    305 
    306 	/* Let current output drain */
    307 	DELAY(100000);
    308 
    309 	/* Set all pins, except DTR pins to be inputs */
    310 	SAB_WRITE(sc, SAB_PCR, ~(SAB_PVR_DTR_A | SAB_PVR_DTR_B));
    311 	/* Disable port interrupts */
    312 	SAB_WRITE(sc, SAB_PIM, 0xff);
    313 	SAB_WRITE(sc, SAB_PVR, SAB_PVR_DTR_A | SAB_PVR_DTR_B | SAB_PVR_MAGIC);
    314 	SAB_WRITE(sc, SAB_IPC, SAB_IPC_ICPL);
    315 
    316 	for (i = 0; i < SAB_NCHAN; i++) {
    317 		struct sabtty_attach_args stax;
    318 
    319 		stax.sbt_portno = i;
    320 
    321 		locs[SABCF_CHANNEL] = i;
    322 
    323 		sc->sc_child[i] = device_private(config_found_sm_loc(self,
    324 		     "sab", locs, &stax, sab_print, config_stdsubmatch));
    325 		if (sc->sc_child[i] != NULL)
    326 			sc->sc_nchild++;
    327 	}
    328 }
    329 
    330 int
    331 sab_print(void *args, const char *name)
    332 {
    333 	struct sabtty_attach_args *sa = args;
    334 
    335 	if (name)
    336 		aprint_normal("sabtty at %s", name);
    337 	aprint_normal(" port %u", sa->sbt_portno);
    338 	return (UNCONF);
    339 }
    340 
    341 int
    342 sab_intr(void *vsc)
    343 {
    344 	struct sab_softc *sc = vsc;
    345 	int r = 0, needsoft = 0;
    346 	uint8_t gis;
    347 
    348 	gis = SAB_READ(sc, SAB_GIS);
    349 
    350 	/* channel A */
    351 	if ((gis & (SAB_GIS_ISA1 | SAB_GIS_ISA0)) && sc->sc_child[0] &&
    352 	    sc->sc_child[0]->sc_tty)
    353 		r |= sabtty_intr(sc->sc_child[0], &needsoft);
    354 
    355 	/* channel B */
    356 	if ((gis & (SAB_GIS_ISB1 | SAB_GIS_ISB0)) && sc->sc_child[1] &&
    357 	    sc->sc_child[1]->sc_tty)
    358 		r |= sabtty_intr(sc->sc_child[1], &needsoft);
    359 
    360 	if (needsoft)
    361 		softint_schedule(sc->sc_softintr);
    362 
    363 	return (r);
    364 }
    365 
    366 void
    367 sab_softintr(void *vsc)
    368 {
    369 	struct sab_softc *sc = vsc;
    370 
    371 	if (sc->sc_child[0] && sc->sc_child[0]->sc_tty)
    372 		sabtty_softintr(sc->sc_child[0]);
    373 	if (sc->sc_child[1] && sc->sc_child[1]->sc_tty)
    374 		sabtty_softintr(sc->sc_child[1]);
    375 }
    376 
    377 int
    378 sabtty_match(device_t parent, cfdata_t match, void *aux)
    379 {
    380 
    381 	return (1);
    382 }
    383 
    384 void
    385 sabtty_attach(device_t parent, device_t self, void *aux)
    386 {
    387 	struct sabtty_softc *sc = device_private(self);
    388 	struct sabtty_attach_args *sa = aux;
    389 	int r;
    390 	int maj;
    391 	int is_kgdb = 0;
    392 
    393 	sc->sc_dev = self;
    394 #ifdef KGDB
    395 	is_kgdb = sab_kgdb_check(sc);
    396 #endif
    397 
    398 	if (!is_kgdb) {
    399 		sc->sc_tty = tty_alloc();
    400 		if (sc->sc_tty == NULL) {
    401 			aprint_normal(": failed to allocate tty\n");
    402 			return;
    403 		}
    404 		tty_attach(sc->sc_tty);
    405 		sc->sc_tty->t_oproc = sabtty_start;
    406 		sc->sc_tty->t_param = sabtty_param;
    407 	}
    408 
    409 	sc->sc_parent = device_private(parent);
    410 	sc->sc_bt = sc->sc_parent->sc_bt;
    411 	sc->sc_portno = sa->sbt_portno;
    412 	sc->sc_rend = sc->sc_rbuf + SABTTY_RBUF_SIZE;
    413 
    414 	switch (sa->sbt_portno) {
    415 	case 0:	/* port A */
    416 		sc->sc_pvr_dtr = SAB_PVR_DTR_A;
    417 		sc->sc_pvr_dsr = SAB_PVR_DSR_A;
    418 		r = bus_space_subregion(sc->sc_bt, sc->sc_parent->sc_bh,
    419 		    SAB_CHAN_A, SAB_CHANLEN, &sc->sc_bh);
    420 		break;
    421 	case 1:	/* port B */
    422 		sc->sc_pvr_dtr = SAB_PVR_DTR_B;
    423 		sc->sc_pvr_dsr = SAB_PVR_DSR_B;
    424 		r = bus_space_subregion(sc->sc_bt, sc->sc_parent->sc_bh,
    425 		    SAB_CHAN_B, SAB_CHANLEN, &sc->sc_bh);
    426 		break;
    427 	default:
    428 		aprint_normal(": invalid channel: %u\n", sa->sbt_portno);
    429 		return;
    430 	}
    431 	if (r != 0) {
    432 		aprint_normal(": failed to allocate register subregion\n");
    433 		return;
    434 	}
    435 
    436 	sabtty_console_flags(sc);
    437 
    438 	if (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
    439 		struct termios t;
    440 		const char *acc;
    441 
    442 		/* Let residual prom output drain */
    443 		DELAY(100000);
    444 
    445 		switch (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
    446 		case SABTTYF_CONS_IN:
    447 			acc = "input";
    448 			break;
    449 		case SABTTYF_CONS_OUT:
    450 			acc = "output";
    451 			break;
    452 		case SABTTYF_CONS_IN|SABTTYF_CONS_OUT:
    453 		default:
    454 			acc = "i/o";
    455 			break;
    456 		}
    457 
    458 		t.c_ispeed= 0;
    459 		if (sc->sc_flags & SABTTYF_IS_RSC)
    460 			t.c_ospeed = 115200;
    461 		else
    462 			t.c_ospeed = 9600;
    463 		t.c_cflag = CREAD | CS8 | HUPCL;
    464 		sc->sc_tty->t_ospeed = 0;
    465 		sabttyparam(sc, sc->sc_tty, &t);
    466 
    467 		if (sc->sc_flags & SABTTYF_CONS_IN) {
    468 			sabtty_cons_input = sc;
    469 			cn_tab->cn_pollc = sab_cnpollc;
    470 			cn_tab->cn_getc = sab_cngetc;
    471 			maj = cdevsw_lookup_major(&sabtty_cdevsw);
    472 			cn_tab->cn_dev = makedev(maj, device_unit(self));
    473 			pmf_device_register1(self, NULL, NULL, sabtty_shutdown);
    474 			cn_init_magic(&sabtty_cnm_state);
    475 			cn_set_magic("\047\001"); /* default magic is BREAK */
    476 		}
    477 
    478 		if (sc->sc_flags & SABTTYF_CONS_OUT) {
    479 			sabtty_tec_wait(sc);
    480 			sabtty_cons_output = sc;
    481 			cn_tab->cn_putc = sab_cnputc;
    482 			maj = cdevsw_lookup_major(&sabtty_cdevsw);
    483 			cn_tab->cn_dev = makedev(maj, device_unit(self));
    484 		}
    485 		aprint_normal(": console %s", acc);
    486 	} else {
    487 		/* Not a console... */
    488 		sabtty_reset(sc);
    489 
    490 #ifdef KGDB
    491 		if (is_kgdb) {
    492 			sab_kgdb_init(sc);
    493 			aprint_normal(": kgdb");
    494 		}
    495 #endif
    496 	}
    497 
    498 	aprint_normal("\n");
    499 	aprint_naive(": Serial port\n");
    500 }
    501 
    502 int
    503 sabtty_intr(struct sabtty_softc *sc, int *needsoftp)
    504 {
    505 	uint8_t isr0, isr1;
    506 	int i, len = 0, needsoft = 0, r = 0, clearfifo = 0;
    507 
    508 	isr0 = SAB_READ(sc, SAB_ISR0);
    509 	isr1 = SAB_READ(sc, SAB_ISR1);
    510 
    511 	if (isr0 || isr1)
    512 		r = 1;
    513 
    514 	if (isr0 & SAB_ISR0_RPF) {
    515 		len = 32;
    516 		clearfifo = 1;
    517 	}
    518 	if (isr0 & SAB_ISR0_TCD) {
    519 		len = (32 - 1) & SAB_READ(sc, SAB_RBCL);
    520 		clearfifo = 1;
    521 	}
    522 	if (isr0 & SAB_ISR0_TIME) {
    523 		sabtty_cec_wait(sc);
    524 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RFRD);
    525 	}
    526 	if (isr0 & SAB_ISR0_RFO) {
    527 		sc->sc_flags |= SABTTYF_RINGOVERFLOW;
    528 		clearfifo = 1;
    529 	}
    530 	if (len != 0) {
    531 		uint8_t *ptr, b;
    532 
    533 		ptr = sc->sc_rput;
    534 		for (i = 0; i < len; i++) {
    535 			b = SAB_READ(sc, SAB_RFIFO);
    536 			if (i % 2 == 0) /* skip status byte */
    537 				cn_check_magic(sc->sc_tty->t_dev,
    538 					       b, sabtty_cnm_state);
    539 			*ptr++ = b;
    540 			if (ptr == sc->sc_rend)
    541 				ptr = sc->sc_rbuf;
    542 			if (ptr == sc->sc_rget) {
    543 				if (ptr == sc->sc_rbuf)
    544 					ptr = sc->sc_rend;
    545 				ptr--;
    546 				sc->sc_flags |= SABTTYF_RINGOVERFLOW;
    547 			}
    548 		}
    549 		sc->sc_rput = ptr;
    550 		needsoft = 1;
    551 	}
    552 
    553 	if (clearfifo) {
    554 		sabtty_cec_wait(sc);
    555 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RMC);
    556 	}
    557 
    558 	if (isr0 & SAB_ISR0_CDSC) {
    559 		sc->sc_flags |= SABTTYF_CDCHG;
    560 		needsoft = 1;
    561 	}
    562 
    563 	if (isr1 & SAB_ISR1_BRKT)
    564 		cn_check_magic(sc->sc_tty->t_dev,
    565 			       CNC_BREAK, sabtty_cnm_state);
    566 
    567 	if (isr1 & (SAB_ISR1_XPR | SAB_ISR1_ALLS)) {
    568 		if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_XFW) &&
    569 		    (sc->sc_flags & SABTTYF_STOP) == 0) {
    570 			if (sc->sc_txc < 32)
    571 				len = sc->sc_txc;
    572 			else
    573 				len = 32;
    574 
    575 			if (len > 0) {
    576 				SAB_WRITE_BLOCK(sc, SAB_XFIFO, sc->sc_txp, len);
    577 				sc->sc_txp += len;
    578 				sc->sc_txc -= len;
    579 
    580 				sabtty_cec_wait(sc);
    581 				SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XF);
    582 
    583 				/*
    584 				 * Prevent the false end of xmit from
    585 				 * confusing things below.
    586 				 */
    587 				isr1 &= ~SAB_ISR1_ALLS;
    588 			}
    589 		}
    590 
    591 		if ((sc->sc_txc == 0) || (sc->sc_flags & SABTTYF_STOP)) {
    592 			if ((sc->sc_imr1 & SAB_IMR1_XPR) == 0) {
    593 				sc->sc_imr1 |= SAB_IMR1_XPR;
    594 				sc->sc_imr1 &= ~SAB_IMR1_ALLS;
    595 				SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    596 			}
    597 		}
    598 	}
    599 
    600 	if ((isr1 & SAB_ISR1_ALLS) && ((sc->sc_txc == 0) ||
    601 	    (sc->sc_flags & SABTTYF_STOP))) {
    602 		if (sc->sc_flags & SABTTYF_TXDRAIN)
    603 			wakeup(sc);
    604 		sc->sc_flags &= ~SABTTYF_STOP;
    605 		sc->sc_flags |= SABTTYF_DONE;
    606 		sc->sc_imr1 |= SAB_IMR1_ALLS;
    607 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    608 		needsoft = 1;
    609 	}
    610 
    611 	if (needsoft)
    612 		*needsoftp = needsoft;
    613 	return (r);
    614 }
    615 
    616 void
    617 sabtty_softintr(struct sabtty_softc *sc)
    618 {
    619 	struct tty *tp = sc->sc_tty;
    620 	int s, flags;
    621 	uint8_t r;
    622 
    623 	if (tp == NULL)
    624 		return;
    625 
    626 	if ((tp->t_state & TS_ISOPEN) == 0)
    627 		return;
    628 
    629 	while (sc->sc_rget != sc->sc_rput) {
    630 		int data;
    631 		uint8_t stat;
    632 
    633 		data = sc->sc_rget[0];
    634 		stat = sc->sc_rget[1];
    635 		sc->sc_rget += 2;
    636 		if (stat & SAB_RSTAT_PE)
    637 			data |= TTY_PE;
    638 		if (stat & SAB_RSTAT_FE)
    639 			data |= TTY_FE;
    640 		if (sc->sc_rget == sc->sc_rend)
    641 			sc->sc_rget = sc->sc_rbuf;
    642 
    643 		(*tp->t_linesw->l_rint)(data, tp);
    644 	}
    645 
    646 	s = splhigh();
    647 	flags = sc->sc_flags;
    648 	sc->sc_flags &= ~(SABTTYF_DONE|SABTTYF_CDCHG|SABTTYF_RINGOVERFLOW);
    649 	splx(s);
    650 
    651 	if (flags & SABTTYF_CDCHG) {
    652 		s = spltty();
    653 		r = SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD;
    654 		splx(s);
    655 
    656 		(*tp->t_linesw->l_modem)(tp, r);
    657 	}
    658 
    659 	if (flags & SABTTYF_RINGOVERFLOW)
    660 		log(LOG_WARNING, "%s: ring overflow\n",
    661 		    device_xname(sc->sc_dev));
    662 
    663 	if (flags & SABTTYF_DONE) {
    664 		ndflush(&tp->t_outq, sc->sc_txp - tp->t_outq.c_cf);
    665 		tp->t_state &= ~TS_BUSY;
    666 		(*tp->t_linesw->l_start)(tp);
    667 	}
    668 }
    669 
    670 int
    671 sabopen(dev_t dev, int flags, int mode, struct lwp *l)
    672 {
    673 	struct sabtty_softc *sc;
    674 	struct tty *tp;
    675 	int s, s1;
    676 
    677 	sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    678 	if (sc == NULL)
    679 		return (ENXIO);
    680 
    681 	tp = sc->sc_tty;
    682 	tp->t_dev = dev;
    683 
    684 	/*
    685 	 * If the device is exclusively for kernel use, deny userland
    686 	 * open.
    687 	 */
    688 	if (ISSET(tp->t_state, TS_KERN_ONLY))
    689 		return (EBUSY);
    690 
    691 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    692 		return (EBUSY);
    693 
    694 	mutex_spin_enter(&tty_lock);
    695 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    696 		ttychars(tp);
    697 		tp->t_iflag = TTYDEF_IFLAG;
    698 		tp->t_oflag = TTYDEF_OFLAG;
    699 		tp->t_cflag = TTYDEF_CFLAG;
    700 		if (sc->sc_openflags & TIOCFLAG_CLOCAL)
    701 			tp->t_cflag |= CLOCAL;
    702 		if (sc->sc_openflags & TIOCFLAG_CRTSCTS)
    703 			tp->t_cflag |= CRTSCTS;
    704 		if (sc->sc_openflags & TIOCFLAG_MDMBUF)
    705 			tp->t_cflag |= MDMBUF;
    706 		tp->t_lflag = TTYDEF_LFLAG;
    707 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    708 
    709 		sc->sc_rput = sc->sc_rget = sc->sc_rbuf;
    710 
    711 		ttsetwater(tp);
    712 
    713 		s1 = splhigh();
    714 		sabtty_reset(sc);
    715 		sabtty_param(tp, &tp->t_termios);
    716 		sc->sc_imr0 = SAB_IMR0_PERR | SAB_IMR0_FERR | SAB_IMR0_PLLA;
    717 		SAB_WRITE(sc, SAB_IMR0, sc->sc_imr0);
    718 		sc->sc_imr1 = SAB_IMR1_BRK | SAB_IMR1_ALLS | SAB_IMR1_XDU |
    719 		    SAB_IMR1_TIN | SAB_IMR1_CSC | SAB_IMR1_XMR | SAB_IMR1_XPR;
    720 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    721 		SAB_WRITE(sc, SAB_CCR0, SAB_READ(sc, SAB_CCR0) | SAB_CCR0_PU);
    722 		sabtty_cec_wait(sc);
    723 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XRES);
    724 		sabtty_cec_wait(sc);
    725 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
    726 		sabtty_cec_wait(sc);
    727 		splx(s1);
    728 
    729 		sabtty_flush(sc);
    730 
    731 		if ((sc->sc_openflags & TIOCFLAG_SOFTCAR) ||
    732 		    (SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD))
    733 			tp->t_state |= TS_CARR_ON;
    734 		else
    735 			tp->t_state &= ~TS_CARR_ON;
    736 	}
    737 
    738 	if ((flags & O_NONBLOCK) == 0) {
    739 		while ((tp->t_cflag & CLOCAL) == 0 &&
    740 		    (tp->t_state & TS_CARR_ON) == 0) {
    741 			int error;
    742 
    743 			error = ttysleep(tp, &tp->t_rawcv, true, 0);
    744 			if (error != 0) {
    745 				mutex_spin_exit(&tty_lock);
    746 				return (error);
    747 			}
    748 		}
    749 	}
    750 
    751 	mutex_spin_exit(&tty_lock);
    752 
    753 	s = (*tp->t_linesw->l_open)(dev, tp);
    754 	if (s != 0) {
    755 		mutex_spin_enter(&tty_lock);
    756 		if (tp->t_state & TS_ISOPEN) {
    757 			mutex_spin_exit(&tty_lock);
    758 			return (s);
    759 		}
    760 		if (tp->t_cflag & HUPCL) {
    761 			sabtty_mdmctrl(sc, 0, DMSET);
    762 			cv_wait(&lbolt, &tty_lock);
    763 		}
    764 
    765 		if ((sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) == 0) {
    766 			/* Flush and power down if we're not the console */
    767 			sabtty_flush(sc);
    768 			sabtty_reset(sc);
    769 		}
    770 		mutex_spin_exit(&tty_lock);
    771 	}
    772 	return (s);
    773 }
    774 
    775 int
    776 sabclose(dev_t dev, int flags, int mode, struct lwp *l)
    777 {
    778 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    779 	struct sab_softc *bc = sc->sc_parent;
    780 	struct tty *tp = sc->sc_tty;
    781 	int s;
    782 
    783 	(*tp->t_linesw->l_close)(tp, flags);
    784 
    785 	s = spltty();
    786 
    787 	if ((tp->t_state & TS_ISOPEN) == 0) {
    788 		/* Wait for output drain */
    789 		sc->sc_imr1 &= ~SAB_IMR1_ALLS;
    790 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    791 		sc->sc_flags |= SABTTYF_TXDRAIN;
    792 		(void)tsleep(sc, TTIPRI, ttclos, 5 * hz);
    793 		sc->sc_imr1 |= SAB_IMR1_ALLS;
    794 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    795 		sc->sc_flags &= ~SABTTYF_TXDRAIN;
    796 
    797 		if (tp->t_cflag & HUPCL) {
    798 			sabtty_mdmctrl(sc, 0, DMSET);
    799 			(void)tsleep(bc, TTIPRI, ttclos, hz);
    800 		}
    801 
    802 		if ((sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) == 0) {
    803 			/* Flush and power down if we're not the console */
    804 			sabtty_flush(sc);
    805 			sabtty_reset(sc);
    806 		}
    807 	}
    808 
    809 	ttyclose(tp);
    810 	splx(s);
    811 
    812 	return (0);
    813 }
    814 
    815 int
    816 sabread(dev_t dev, struct uio *uio, int flags)
    817 {
    818 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    819 	struct tty *tp = sc->sc_tty;
    820 
    821 	return ((*tp->t_linesw->l_read)(tp, uio, flags));
    822 }
    823 
    824 int
    825 sabwrite(dev_t dev, struct uio *uio, int flags)
    826 {
    827 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    828 	struct tty *tp = sc->sc_tty;
    829 
    830 	return ((*tp->t_linesw->l_write)(tp, uio, flags));
    831 }
    832 
    833 int
    834 sabioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    835 {
    836 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    837 	struct tty *tp = sc->sc_tty;
    838 	int error;
    839 
    840 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flags, l);
    841 	if (error >= 0)
    842 		return (error);
    843 
    844 	error = ttioctl(tp, cmd, data, flags, l);
    845 	if (error >= 0)
    846 		return (error);
    847 
    848 	error = 0;
    849 
    850 	switch (cmd) {
    851 	case TIOCSBRK:
    852 		SAB_WRITE(sc, SAB_DAFO,
    853 		    SAB_READ(sc, SAB_DAFO) | SAB_DAFO_XBRK);
    854 		break;
    855 	case TIOCCBRK:
    856 		SAB_WRITE(sc, SAB_DAFO,
    857 		    SAB_READ(sc, SAB_DAFO) & ~SAB_DAFO_XBRK);
    858 		break;
    859 	case TIOCSDTR:
    860 		sabtty_mdmctrl(sc, TIOCM_DTR, DMBIS);
    861 		break;
    862 	case TIOCCDTR:
    863 		sabtty_mdmctrl(sc, TIOCM_DTR, DMBIC);
    864 		break;
    865 	case TIOCMBIS:
    866 		sabtty_mdmctrl(sc, *((int *)data), DMBIS);
    867 		break;
    868 	case TIOCMBIC:
    869 		sabtty_mdmctrl(sc, *((int *)data), DMBIC);
    870 		break;
    871 	case TIOCMGET:
    872 		*((int *)data) = sabtty_mdmctrl(sc, 0, DMGET);
    873 		break;
    874 	case TIOCMSET:
    875 		sabtty_mdmctrl(sc, *((int *)data), DMSET);
    876 		break;
    877 	case TIOCGFLAGS:
    878 		*((int *)data) = sc->sc_openflags;
    879 		break;
    880 	case TIOCSFLAGS:
    881 		if (kauth_authorize_device_tty(l->l_cred,
    882 		    KAUTH_DEVICE_TTY_PRIVSET, tp))
    883 			error = EPERM;
    884 		else
    885 			sc->sc_openflags = *((int *)data) &
    886 			    (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
    887 			     TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
    888 		break;
    889 	default:
    890 		error = ENOTTY;
    891 	}
    892 
    893 	return (error);
    894 }
    895 
    896 struct tty *
    897 sabtty(dev_t dev)
    898 {
    899 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    900 
    901 	return (sc->sc_tty);
    902 }
    903 
    904 void
    905 sabstop(struct tty *tp, int flag)
    906 {
    907 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(tp->t_dev));
    908 	int s;
    909 
    910 	s = spltty();
    911 	if (tp->t_state & TS_BUSY) {
    912 		if ((tp->t_state & TS_TTSTOP) == 0)
    913 			tp->t_state |= TS_FLUSH;
    914 		sc->sc_flags |= SABTTYF_STOP;
    915 		sc->sc_imr1 &= ~SAB_IMR1_ALLS;
    916 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    917 	}
    918 	splx(s);
    919 }
    920 
    921 int
    922 sabpoll(dev_t dev, int events, struct lwp *l)
    923 {
    924 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    925 	struct tty *tp = sc->sc_tty;
    926 
    927 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    928 }
    929 
    930 int
    931 sabtty_mdmctrl(struct sabtty_softc *sc, int bits, int how)
    932 {
    933 	uint8_t r;
    934 	int s;
    935 
    936 	s = spltty();
    937 	switch (how) {
    938 	case DMGET:
    939 		bits = 0;
    940 		if (SAB_READ(sc, SAB_STAR) & SAB_STAR_CTS)
    941 			bits |= TIOCM_CTS;
    942 		if ((SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD) == 0)
    943 			bits |= TIOCM_CD;
    944 
    945 		r = SAB_READ(sc, SAB_PVR);
    946 		if ((r & sc->sc_pvr_dtr) == 0)
    947 			bits |= TIOCM_DTR;
    948 		if ((r & sc->sc_pvr_dsr) == 0)
    949 			bits |= TIOCM_DSR;
    950 
    951 		r = SAB_READ(sc, SAB_MODE);
    952 		if ((r & (SAB_MODE_RTS|SAB_MODE_FRTS)) == SAB_MODE_RTS)
    953 			bits |= TIOCM_RTS;
    954 		break;
    955 	case DMSET:
    956 		r = SAB_READ(sc, SAB_MODE);
    957 		if (bits & TIOCM_RTS) {
    958 			r &= ~SAB_MODE_FRTS;
    959 			r |= SAB_MODE_RTS;
    960 		} else
    961 			r |= SAB_MODE_FRTS | SAB_MODE_RTS;
    962 		SAB_WRITE(sc, SAB_MODE, r);
    963 
    964 		r = SAB_READ(sc, SAB_PVR);
    965 		if (bits & TIOCM_DTR)
    966 			r &= ~sc->sc_pvr_dtr;
    967 		else
    968 			r |= sc->sc_pvr_dtr;
    969 		SAB_WRITE(sc, SAB_PVR, r);
    970 		break;
    971 	case DMBIS:
    972 		if (bits & TIOCM_RTS) {
    973 			r = SAB_READ(sc, SAB_MODE);
    974 			r &= ~SAB_MODE_FRTS;
    975 			r |= SAB_MODE_RTS;
    976 			SAB_WRITE(sc, SAB_MODE, r);
    977 		}
    978 		if (bits & TIOCM_DTR) {
    979 			r = SAB_READ(sc, SAB_PVR);
    980 			r &= ~sc->sc_pvr_dtr;
    981 			SAB_WRITE(sc, SAB_PVR, r);
    982 		}
    983 		break;
    984 	case DMBIC:
    985 		if (bits & TIOCM_RTS) {
    986 			r = SAB_READ(sc, SAB_MODE);
    987 			r |= SAB_MODE_FRTS | SAB_MODE_RTS;
    988 			SAB_WRITE(sc, SAB_MODE, r);
    989 		}
    990 		if (bits & TIOCM_DTR) {
    991 			r = SAB_READ(sc, SAB_PVR);
    992 			r |= sc->sc_pvr_dtr;
    993 			SAB_WRITE(sc, SAB_PVR, r);
    994 		}
    995 		break;
    996 	}
    997 	splx(s);
    998 	return (bits);
    999 }
   1000 
   1001 int
   1002 sabttyparam(struct sabtty_softc *sc, struct tty *tp, struct termios *t)
   1003 {
   1004 	int s, ospeed;
   1005 	tcflag_t cflag;
   1006 	uint8_t dafo, r;
   1007 
   1008 	ospeed = sabtty_speed(t->c_ospeed);
   1009 	if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
   1010 		return (EINVAL);
   1011 
   1012 	s = spltty();
   1013 
   1014 	/* hang up line if ospeed is zero, otherwise raise dtr */
   1015 	sabtty_mdmctrl(sc, TIOCM_DTR,
   1016 	    (t->c_ospeed == 0) ? DMBIC : DMBIS);
   1017 
   1018 	dafo = SAB_READ(sc, SAB_DAFO);
   1019 
   1020 	cflag = t->c_cflag;
   1021 
   1022 	if (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
   1023 		cflag |= CLOCAL;
   1024 		cflag &= ~HUPCL;
   1025 	}
   1026 
   1027 	if (cflag & CSTOPB)
   1028 		dafo |= SAB_DAFO_STOP;
   1029 	else
   1030 		dafo &= ~SAB_DAFO_STOP;
   1031 
   1032 	dafo &= ~SAB_DAFO_CHL_CSIZE;
   1033 	switch (cflag & CSIZE) {
   1034 	case CS5:
   1035 		dafo |= SAB_DAFO_CHL_CS5;
   1036 		break;
   1037 	case CS6:
   1038 		dafo |= SAB_DAFO_CHL_CS6;
   1039 		break;
   1040 	case CS7:
   1041 		dafo |= SAB_DAFO_CHL_CS7;
   1042 		break;
   1043 	default:
   1044 		dafo |= SAB_DAFO_CHL_CS8;
   1045 		break;
   1046 	}
   1047 
   1048 	dafo &= ~SAB_DAFO_PARMASK;
   1049 	if (cflag & PARENB) {
   1050 		if (cflag & PARODD)
   1051 			dafo |= SAB_DAFO_PAR_ODD;
   1052 		else
   1053 			dafo |= SAB_DAFO_PAR_EVEN;
   1054 	} else
   1055 		dafo |= SAB_DAFO_PAR_NONE;
   1056 	SAB_WRITE(sc, SAB_DAFO, dafo);
   1057 
   1058 	if (!(sc->sc_flags & SABTTYF_IS_RSC) && ospeed != 0) {
   1059 		SAB_WRITE(sc, SAB_BGR, ospeed & 0xff);
   1060 		r = SAB_READ(sc, SAB_CCR2);
   1061 		r &= ~(SAB_CCR2_BR9 | SAB_CCR2_BR8);
   1062 		r |= (ospeed >> 2) & (SAB_CCR2_BR9 | SAB_CCR2_BR8);
   1063 		SAB_WRITE(sc, SAB_CCR2, r);
   1064 	}
   1065 
   1066 	r = SAB_READ(sc, SAB_MODE);
   1067 	r |= SAB_MODE_RAC;
   1068 	if (cflag & CRTSCTS) {
   1069 		r &= ~(SAB_MODE_RTS | SAB_MODE_FCTS);
   1070 		r |= SAB_MODE_FRTS;
   1071 		sc->sc_imr1 &= ~SAB_IMR1_CSC;
   1072 	} else {
   1073 		r |= SAB_MODE_RTS | SAB_MODE_FCTS;
   1074 		r &= ~SAB_MODE_FRTS;
   1075 		sc->sc_imr1 |= SAB_IMR1_CSC;
   1076 	}
   1077 	SAB_WRITE(sc, SAB_MODE, r);
   1078 	SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
   1079 
   1080 	tp->t_cflag = cflag;
   1081 
   1082 	splx(s);
   1083 	return (0);
   1084 }
   1085 
   1086 int
   1087 sabtty_param(struct tty *tp, struct termios *t)
   1088 {
   1089 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(tp->t_dev));
   1090 
   1091 	return (sabttyparam(sc, tp, t));
   1092 }
   1093 
   1094 void
   1095 sabtty_start(struct tty *tp)
   1096 {
   1097 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(tp->t_dev));
   1098 	int s;
   1099 
   1100 	s = spltty();
   1101 	if ((tp->t_state & (TS_TTSTOP | TS_TIMEOUT | TS_BUSY)) == 0) {
   1102 		if (ttypull(tp)) {
   1103 			sc->sc_txc = ndqb(&tp->t_outq, 0);
   1104 			sc->sc_txp = tp->t_outq.c_cf;
   1105 			tp->t_state |= TS_BUSY;
   1106 			sc->sc_imr1 &= ~(SAB_ISR1_XPR | SAB_ISR1_ALLS);
   1107 			SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
   1108 		}
   1109 	}
   1110 	splx(s);
   1111 }
   1112 
   1113 void
   1114 sabtty_cec_wait(struct sabtty_softc *sc)
   1115 {
   1116 	int i = 50000;
   1117 
   1118 	for (;;) {
   1119 		if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_CEC) == 0)
   1120 			return;
   1121 		if (--i == 0)
   1122 			break;
   1123 		DELAY(1);
   1124 	}
   1125 }
   1126 
   1127 void
   1128 sabtty_tec_wait(struct sabtty_softc *sc)
   1129 {
   1130 	int i = 200000;
   1131 
   1132 	for (;;) {
   1133 		if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_TEC) == 0)
   1134 			return;
   1135 		if (--i == 0)
   1136 			break;
   1137 		DELAY(1);
   1138 	}
   1139 }
   1140 
   1141 void
   1142 sabtty_reset(struct sabtty_softc *sc)
   1143 {
   1144 	/* power down */
   1145 	SAB_WRITE(sc, SAB_CCR0, 0);
   1146 
   1147 	/* set basic configuration */
   1148 	SAB_WRITE(sc, SAB_CCR0,
   1149 	    SAB_CCR0_MCE | SAB_CCR0_SC_NRZ | SAB_CCR0_SM_ASYNC);
   1150 	SAB_WRITE(sc, SAB_CCR1, SAB_CCR1_ODS | SAB_CCR1_BCR | SAB_CCR1_CM_7);
   1151 	SAB_WRITE(sc, SAB_CCR2, SAB_CCR2_BDF | SAB_CCR2_SSEL | SAB_CCR2_TOE);
   1152 	SAB_WRITE(sc, SAB_CCR3, 0);
   1153 	SAB_WRITE(sc, SAB_CCR4, SAB_CCR4_MCK4 | SAB_CCR4_EBRG | SAB_CCR4_ICD);
   1154 	SAB_WRITE(sc, SAB_MODE, SAB_MODE_RTS | SAB_MODE_FCTS | SAB_MODE_RAC);
   1155 	SAB_WRITE(sc, SAB_RFC,
   1156 	    SAB_RFC_DPS | SAB_RFC_RFDF | SAB_RFC_RFTH_32CHAR);
   1157 
   1158 	/* clear interrupts */
   1159 	sc->sc_imr0 = sc->sc_imr1 = 0xff;
   1160 	SAB_WRITE(sc, SAB_IMR0, sc->sc_imr0);
   1161 	SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
   1162 	(void)SAB_READ(sc, SAB_ISR0);
   1163 	(void)SAB_READ(sc, SAB_ISR1);
   1164 }
   1165 
   1166 void
   1167 sabtty_flush(struct sabtty_softc *sc)
   1168 {
   1169 	/* clear rx fifo */
   1170 	sabtty_cec_wait(sc);
   1171 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
   1172 
   1173 	/* clear tx fifo */
   1174 	sabtty_cec_wait(sc);
   1175 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XRES);
   1176 }
   1177 
   1178 int
   1179 sabtty_speed(int rate)
   1180 {
   1181 	int i, len, r;
   1182 
   1183 	if (rate == 0)
   1184 		return (0);
   1185 	len = sizeof(sabtty_baudtable)/sizeof(sabtty_baudtable[0]);
   1186 	for (i = 0; i < len; i++) {
   1187 		if (rate == sabtty_baudtable[i].baud) {
   1188 			r = sabtty_baudtable[i].n |
   1189 			    (sabtty_baudtable[i].m << 6);
   1190 			return (r);
   1191 		}
   1192 	}
   1193 	return (-1);
   1194 }
   1195 
   1196 void
   1197 sabtty_cnputc(struct sabtty_softc *sc, int c)
   1198 {
   1199 	sabtty_tec_wait(sc);
   1200 	SAB_WRITE(sc, SAB_TIC, c);
   1201 	sabtty_tec_wait(sc);
   1202 }
   1203 
   1204 int
   1205 sabtty_cngetc(struct sabtty_softc *sc)
   1206 {
   1207 	uint8_t r, len;
   1208 
   1209 again:
   1210 	do {
   1211 		r = SAB_READ(sc, SAB_STAR);
   1212 	} while ((r & SAB_STAR_RFNE) == 0);
   1213 
   1214 	/*
   1215 	 * Ok, at least one byte in RFIFO, ask for permission to access RFIFO
   1216 	 * (I hate this chip... hate hate hate).
   1217 	 */
   1218 	sabtty_cec_wait(sc);
   1219 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RFRD);
   1220 
   1221 	/* Wait for RFIFO to come ready */
   1222 	do {
   1223 		r = SAB_READ(sc, SAB_ISR0);
   1224 	} while ((r & SAB_ISR0_TCD) == 0);
   1225 
   1226 	len = SAB_READ(sc, SAB_RBCL) & (32 - 1);
   1227 	if (len == 0)
   1228 		goto again;	/* Shouldn't happen... */
   1229 
   1230 	r = SAB_READ(sc, SAB_RFIFO);
   1231 
   1232 	/*
   1233 	 * Blow away everything left in the FIFO...
   1234 	 */
   1235 	sabtty_cec_wait(sc);
   1236 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RMC);
   1237 	return (r);
   1238 }
   1239 
   1240 void
   1241 sabtty_cnpollc(struct sabtty_softc *sc, int on)
   1242 {
   1243 	uint8_t r;
   1244 
   1245 	if (on) {
   1246 		if (sc->sc_polling)
   1247 			return;
   1248 		SAB_WRITE(sc, SAB_IPC, SAB_READ(sc, SAB_IPC) | SAB_IPC_VIS);
   1249 		r = sc->sc_pollrfc = SAB_READ(sc, SAB_RFC);
   1250 		r &= ~(SAB_RFC_RFDF);
   1251 		SAB_WRITE(sc, SAB_RFC, r);
   1252 		sabtty_cec_wait(sc);
   1253 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
   1254 		sc->sc_polling = 1;
   1255 	} else {
   1256 		if (!sc->sc_polling)
   1257 			return;
   1258 		SAB_WRITE(sc, SAB_IPC, SAB_READ(sc, SAB_IPC) & ~SAB_IPC_VIS);
   1259 		SAB_WRITE(sc, SAB_RFC, sc->sc_pollrfc);
   1260 		sabtty_cec_wait(sc);
   1261 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
   1262 		sc->sc_polling = 0;
   1263 	}
   1264 }
   1265 
   1266 void
   1267 sab_cnputc(dev_t dev, int c)
   1268 {
   1269 	struct sabtty_softc *sc = sabtty_cons_output;
   1270 
   1271 	if (sc == NULL)
   1272 		return;
   1273 	sabtty_cnputc(sc, c);
   1274 }
   1275 
   1276 void
   1277 sab_cnpollc(dev_t dev, int on)
   1278 {
   1279 	struct sabtty_softc *sc = sabtty_cons_input;
   1280 
   1281 	sabtty_cnpollc(sc, on);
   1282 }
   1283 
   1284 int
   1285 sab_cngetc(dev_t dev)
   1286 {
   1287 	struct sabtty_softc *sc = sabtty_cons_input;
   1288 
   1289 	if (sc == NULL)
   1290 		return (-1);
   1291 	return (sabtty_cngetc(sc));
   1292 }
   1293 
   1294 void
   1295 sabtty_console_flags(struct sabtty_softc *sc)
   1296 {
   1297 	int node, channel, cookie;
   1298 	char buf[255];
   1299 
   1300 	node = sc->sc_parent->sc_node;
   1301 	channel = sc->sc_portno;
   1302 
   1303 	/* Default to channel 0 if there are no explicit prom args */
   1304 	cookie = 0;
   1305 
   1306 	if (node == prom_instance_to_package(prom_stdin())) {
   1307 		if (prom_getoption("input-device", buf, sizeof buf) == 0 &&
   1308 			strcmp("ttyb", buf) == 0)
   1309 				cookie = 1;
   1310 
   1311 		if (channel == cookie)
   1312 			sc->sc_flags |= SABTTYF_CONS_IN;
   1313 	}
   1314 
   1315 	/* Default to same channel if there are no explicit prom args */
   1316 
   1317 	if (node == prom_instance_to_package(prom_stdout())) {
   1318 		if (prom_getoption("output-device", buf, sizeof buf) == 0 &&
   1319 			strcmp("ttyb", buf) == 0)
   1320 				cookie = 1;
   1321 
   1322 		if (channel == cookie)
   1323 			sc->sc_flags |= SABTTYF_CONS_OUT;
   1324 	}
   1325 	/* Are we connected to an E250 RSC? */
   1326 	if (channel == prom_getpropint(node, "ssp-console", -1) ||
   1327 	    channel == prom_getpropint(node, "ssp-control", -1))
   1328 		sc->sc_flags |= SABTTYF_IS_RSC;
   1329 }
   1330 
   1331 bool
   1332 sabtty_shutdown(device_t dev, int how)
   1333 {
   1334 	struct sabtty_softc *sc = device_private(dev);
   1335 
   1336 	/* Have to put the chip back into single char mode */
   1337 	sc->sc_flags |= SABTTYF_DONTDDB;
   1338 	SAB_WRITE(sc, SAB_RFC, SAB_READ(sc, SAB_RFC) & ~SAB_RFC_RFDF);
   1339 	sabtty_cec_wait(sc);
   1340 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
   1341 	sabtty_cec_wait(sc);
   1342 	return true;
   1343 }
   1344 
   1345 #ifdef KGDB
   1346 static int
   1347 sab_kgdb_getc(void *arg)
   1348 {
   1349 	struct sabtty_softc *sc = arg;
   1350 
   1351 	return sabtty_cngetc(sc);
   1352 }
   1353 
   1354 static void
   1355 sab_kgdb_putc(void *arg, int c)
   1356 {
   1357 	struct sabtty_softc *sc = arg;
   1358 
   1359 	return sabtty_cnputc(sc, c);
   1360 }
   1361 
   1362 #ifndef KGDB_DEVRATE
   1363 #define KGDB_DEVRATE    9600
   1364 #endif
   1365 
   1366 int
   1367 sab_kgdb_check(struct sabtty_softc *sc)
   1368 {
   1369 	return strcmp(device_xname(sc->sc_dev), KGDB_DEVNAME) == 0;
   1370 }
   1371 
   1372 void
   1373 sab_kgdb_init(struct sabtty_softc *sc)
   1374 {
   1375 	int sp;
   1376 	uint8_t dafo, r;
   1377 	extern int kgdb_break_at_attach;
   1378 
   1379 	kgdb_attach(sab_kgdb_getc, sab_kgdb_putc, sc);
   1380 	kgdb_dev = 123;	/* not used, but checked against NODEV */
   1381 
   1382 	/*
   1383 	 * Configure the port to speed/8n1
   1384 	 */
   1385 	dafo = SAB_READ(sc, SAB_DAFO);
   1386 	dafo &= ~(SAB_DAFO_STOP|SAB_DAFO_CHL_CSIZE|SAB_DAFO_PARMASK);
   1387 	dafo |= SAB_DAFO_CHL_CS8|SAB_DAFO_PAR_NONE;
   1388 	SAB_WRITE(sc, SAB_DAFO, dafo);
   1389 	sp = sabtty_speed(KGDB_DEVRATE);
   1390 	SAB_WRITE(sc, SAB_BGR, sp & 0xff);
   1391 	r = SAB_READ(sc, SAB_CCR2);
   1392 	r &= ~(SAB_CCR2_BR9 | SAB_CCR2_BR8);
   1393 	r |= (sp >> 2) & (SAB_CCR2_BR9 | SAB_CCR2_BR8);
   1394 	SAB_WRITE(sc, SAB_CCR2, r);
   1395 
   1396 	/*
   1397 	 * If we are booting with -d, break into kgdb now
   1398 	 */
   1399 	if (kgdb_break_at_attach)
   1400 		kgdb_connect(1);
   1401 }
   1402 #endif
   1403