Home | History | Annotate | Line # | Download | only in dev
sab.c revision 1.55.18.1
      1 /*	$NetBSD: sab.c,v 1.55.18.1 2021/03/22 02:00:59 thorpej 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.18.1 2021/03/22 02:00:59 thorpej 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(
    324 		    config_found(self, &stax, sab_print,
    325 				 CFARG_SUBMATCH, config_stdsubmatch,
    326 				 CFARG_IATTR, "sab",
    327 				 CFARG_LOCATORS, locs,
    328 				 CFARG_EOL));
    329 		if (sc->sc_child[i] != NULL)
    330 			sc->sc_nchild++;
    331 	}
    332 }
    333 
    334 int
    335 sab_print(void *args, const char *name)
    336 {
    337 	struct sabtty_attach_args *sa = args;
    338 
    339 	if (name)
    340 		aprint_normal("sabtty at %s", name);
    341 	aprint_normal(" port %u", sa->sbt_portno);
    342 	return (UNCONF);
    343 }
    344 
    345 int
    346 sab_intr(void *vsc)
    347 {
    348 	struct sab_softc *sc = vsc;
    349 	int r = 0, needsoft = 0;
    350 	uint8_t gis;
    351 
    352 	gis = SAB_READ(sc, SAB_GIS);
    353 
    354 	/* channel A */
    355 	if ((gis & (SAB_GIS_ISA1 | SAB_GIS_ISA0)) && sc->sc_child[0] &&
    356 	    sc->sc_child[0]->sc_tty)
    357 		r |= sabtty_intr(sc->sc_child[0], &needsoft);
    358 
    359 	/* channel B */
    360 	if ((gis & (SAB_GIS_ISB1 | SAB_GIS_ISB0)) && sc->sc_child[1] &&
    361 	    sc->sc_child[1]->sc_tty)
    362 		r |= sabtty_intr(sc->sc_child[1], &needsoft);
    363 
    364 	if (needsoft)
    365 		softint_schedule(sc->sc_softintr);
    366 
    367 	return (r);
    368 }
    369 
    370 void
    371 sab_softintr(void *vsc)
    372 {
    373 	struct sab_softc *sc = vsc;
    374 
    375 	if (sc->sc_child[0] && sc->sc_child[0]->sc_tty)
    376 		sabtty_softintr(sc->sc_child[0]);
    377 	if (sc->sc_child[1] && sc->sc_child[1]->sc_tty)
    378 		sabtty_softintr(sc->sc_child[1]);
    379 }
    380 
    381 int
    382 sabtty_match(device_t parent, cfdata_t match, void *aux)
    383 {
    384 
    385 	return (1);
    386 }
    387 
    388 void
    389 sabtty_attach(device_t parent, device_t self, void *aux)
    390 {
    391 	struct sabtty_softc *sc = device_private(self);
    392 	struct sabtty_attach_args *sa = aux;
    393 	int r;
    394 	int maj;
    395 	int is_kgdb = 0;
    396 
    397 	sc->sc_dev = self;
    398 #ifdef KGDB
    399 	is_kgdb = sab_kgdb_check(sc);
    400 #endif
    401 
    402 	if (!is_kgdb) {
    403 		sc->sc_tty = tty_alloc();
    404 		if (sc->sc_tty == NULL) {
    405 			aprint_normal(": failed to allocate tty\n");
    406 			return;
    407 		}
    408 		tty_attach(sc->sc_tty);
    409 		sc->sc_tty->t_oproc = sabtty_start;
    410 		sc->sc_tty->t_param = sabtty_param;
    411 	}
    412 
    413 	sc->sc_parent = device_private(parent);
    414 	sc->sc_bt = sc->sc_parent->sc_bt;
    415 	sc->sc_portno = sa->sbt_portno;
    416 	sc->sc_rend = sc->sc_rbuf + SABTTY_RBUF_SIZE;
    417 
    418 	switch (sa->sbt_portno) {
    419 	case 0:	/* port A */
    420 		sc->sc_pvr_dtr = SAB_PVR_DTR_A;
    421 		sc->sc_pvr_dsr = SAB_PVR_DSR_A;
    422 		r = bus_space_subregion(sc->sc_bt, sc->sc_parent->sc_bh,
    423 		    SAB_CHAN_A, SAB_CHANLEN, &sc->sc_bh);
    424 		break;
    425 	case 1:	/* port B */
    426 		sc->sc_pvr_dtr = SAB_PVR_DTR_B;
    427 		sc->sc_pvr_dsr = SAB_PVR_DSR_B;
    428 		r = bus_space_subregion(sc->sc_bt, sc->sc_parent->sc_bh,
    429 		    SAB_CHAN_B, SAB_CHANLEN, &sc->sc_bh);
    430 		break;
    431 	default:
    432 		aprint_normal(": invalid channel: %u\n", sa->sbt_portno);
    433 		return;
    434 	}
    435 	if (r != 0) {
    436 		aprint_normal(": failed to allocate register subregion\n");
    437 		return;
    438 	}
    439 
    440 	sabtty_console_flags(sc);
    441 
    442 	if (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
    443 		struct termios t;
    444 		const char *acc;
    445 
    446 		/* Let residual prom output drain */
    447 		DELAY(100000);
    448 
    449 		switch (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
    450 		case SABTTYF_CONS_IN:
    451 			acc = "input";
    452 			break;
    453 		case SABTTYF_CONS_OUT:
    454 			acc = "output";
    455 			break;
    456 		case SABTTYF_CONS_IN|SABTTYF_CONS_OUT:
    457 		default:
    458 			acc = "i/o";
    459 			break;
    460 		}
    461 
    462 		t.c_ispeed= 0;
    463 		if (sc->sc_flags & SABTTYF_IS_RSC)
    464 			t.c_ospeed = 115200;
    465 		else
    466 			t.c_ospeed = 9600;
    467 		t.c_cflag = CREAD | CS8 | HUPCL;
    468 		sc->sc_tty->t_ospeed = 0;
    469 		sabttyparam(sc, sc->sc_tty, &t);
    470 
    471 		if (sc->sc_flags & SABTTYF_CONS_IN) {
    472 			sabtty_cons_input = sc;
    473 			cn_tab->cn_pollc = sab_cnpollc;
    474 			cn_tab->cn_getc = sab_cngetc;
    475 			maj = cdevsw_lookup_major(&sabtty_cdevsw);
    476 			cn_tab->cn_dev = makedev(maj, device_unit(self));
    477 			pmf_device_register1(self, NULL, NULL, sabtty_shutdown);
    478 			cn_init_magic(&sabtty_cnm_state);
    479 			cn_set_magic("\047\001"); /* default magic is BREAK */
    480 		}
    481 
    482 		if (sc->sc_flags & SABTTYF_CONS_OUT) {
    483 			sabtty_tec_wait(sc);
    484 			sabtty_cons_output = sc;
    485 			cn_tab->cn_putc = sab_cnputc;
    486 			maj = cdevsw_lookup_major(&sabtty_cdevsw);
    487 			cn_tab->cn_dev = makedev(maj, device_unit(self));
    488 		}
    489 		aprint_normal(": console %s", acc);
    490 	} else {
    491 		/* Not a console... */
    492 		sabtty_reset(sc);
    493 
    494 #ifdef KGDB
    495 		if (is_kgdb) {
    496 			sab_kgdb_init(sc);
    497 			aprint_normal(": kgdb");
    498 		}
    499 #endif
    500 	}
    501 
    502 	aprint_normal("\n");
    503 	aprint_naive(": Serial port\n");
    504 }
    505 
    506 int
    507 sabtty_intr(struct sabtty_softc *sc, int *needsoftp)
    508 {
    509 	uint8_t isr0, isr1;
    510 	int i, len = 0, needsoft = 0, r = 0, clearfifo = 0;
    511 
    512 	isr0 = SAB_READ(sc, SAB_ISR0);
    513 	isr1 = SAB_READ(sc, SAB_ISR1);
    514 
    515 	if (isr0 || isr1)
    516 		r = 1;
    517 
    518 	if (isr0 & SAB_ISR0_RPF) {
    519 		len = 32;
    520 		clearfifo = 1;
    521 	}
    522 	if (isr0 & SAB_ISR0_TCD) {
    523 		len = (32 - 1) & SAB_READ(sc, SAB_RBCL);
    524 		clearfifo = 1;
    525 	}
    526 	if (isr0 & SAB_ISR0_TIME) {
    527 		sabtty_cec_wait(sc);
    528 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RFRD);
    529 	}
    530 	if (isr0 & SAB_ISR0_RFO) {
    531 		sc->sc_flags |= SABTTYF_RINGOVERFLOW;
    532 		clearfifo = 1;
    533 	}
    534 	if (len != 0) {
    535 		uint8_t *ptr, b;
    536 
    537 		ptr = sc->sc_rput;
    538 		for (i = 0; i < len; i++) {
    539 			b = SAB_READ(sc, SAB_RFIFO);
    540 			if (i % 2 == 0) /* skip status byte */
    541 				cn_check_magic(sc->sc_tty->t_dev,
    542 					       b, sabtty_cnm_state);
    543 			*ptr++ = b;
    544 			if (ptr == sc->sc_rend)
    545 				ptr = sc->sc_rbuf;
    546 			if (ptr == sc->sc_rget) {
    547 				if (ptr == sc->sc_rbuf)
    548 					ptr = sc->sc_rend;
    549 				ptr--;
    550 				sc->sc_flags |= SABTTYF_RINGOVERFLOW;
    551 			}
    552 		}
    553 		sc->sc_rput = ptr;
    554 		needsoft = 1;
    555 	}
    556 
    557 	if (clearfifo) {
    558 		sabtty_cec_wait(sc);
    559 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RMC);
    560 	}
    561 
    562 	if (isr0 & SAB_ISR0_CDSC) {
    563 		sc->sc_flags |= SABTTYF_CDCHG;
    564 		needsoft = 1;
    565 	}
    566 
    567 	if (isr1 & SAB_ISR1_BRKT)
    568 		cn_check_magic(sc->sc_tty->t_dev,
    569 			       CNC_BREAK, sabtty_cnm_state);
    570 
    571 	if (isr1 & (SAB_ISR1_XPR | SAB_ISR1_ALLS)) {
    572 		if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_XFW) &&
    573 		    (sc->sc_flags & SABTTYF_STOP) == 0) {
    574 			if (sc->sc_txc < 32)
    575 				len = sc->sc_txc;
    576 			else
    577 				len = 32;
    578 
    579 			if (len > 0) {
    580 				SAB_WRITE_BLOCK(sc, SAB_XFIFO, sc->sc_txp, len);
    581 				sc->sc_txp += len;
    582 				sc->sc_txc -= len;
    583 
    584 				sabtty_cec_wait(sc);
    585 				SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XF);
    586 
    587 				/*
    588 				 * Prevent the false end of xmit from
    589 				 * confusing things below.
    590 				 */
    591 				isr1 &= ~SAB_ISR1_ALLS;
    592 			}
    593 		}
    594 
    595 		if ((sc->sc_txc == 0) || (sc->sc_flags & SABTTYF_STOP)) {
    596 			if ((sc->sc_imr1 & SAB_IMR1_XPR) == 0) {
    597 				sc->sc_imr1 |= SAB_IMR1_XPR;
    598 				sc->sc_imr1 &= ~SAB_IMR1_ALLS;
    599 				SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    600 			}
    601 		}
    602 	}
    603 
    604 	if ((isr1 & SAB_ISR1_ALLS) && ((sc->sc_txc == 0) ||
    605 	    (sc->sc_flags & SABTTYF_STOP))) {
    606 		if (sc->sc_flags & SABTTYF_TXDRAIN)
    607 			wakeup(sc);
    608 		sc->sc_flags &= ~SABTTYF_STOP;
    609 		sc->sc_flags |= SABTTYF_DONE;
    610 		sc->sc_imr1 |= SAB_IMR1_ALLS;
    611 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    612 		needsoft = 1;
    613 	}
    614 
    615 	if (needsoft)
    616 		*needsoftp = needsoft;
    617 	return (r);
    618 }
    619 
    620 void
    621 sabtty_softintr(struct sabtty_softc *sc)
    622 {
    623 	struct tty *tp = sc->sc_tty;
    624 	int s, flags;
    625 	uint8_t r;
    626 
    627 	if (tp == NULL)
    628 		return;
    629 
    630 	if ((tp->t_state & TS_ISOPEN) == 0)
    631 		return;
    632 
    633 	while (sc->sc_rget != sc->sc_rput) {
    634 		int data;
    635 		uint8_t stat;
    636 
    637 		data = sc->sc_rget[0];
    638 		stat = sc->sc_rget[1];
    639 		sc->sc_rget += 2;
    640 		if (stat & SAB_RSTAT_PE)
    641 			data |= TTY_PE;
    642 		if (stat & SAB_RSTAT_FE)
    643 			data |= TTY_FE;
    644 		if (sc->sc_rget == sc->sc_rend)
    645 			sc->sc_rget = sc->sc_rbuf;
    646 
    647 		(*tp->t_linesw->l_rint)(data, tp);
    648 	}
    649 
    650 	s = splhigh();
    651 	flags = sc->sc_flags;
    652 	sc->sc_flags &= ~(SABTTYF_DONE|SABTTYF_CDCHG|SABTTYF_RINGOVERFLOW);
    653 	splx(s);
    654 
    655 	if (flags & SABTTYF_CDCHG) {
    656 		s = spltty();
    657 		r = SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD;
    658 		splx(s);
    659 
    660 		(*tp->t_linesw->l_modem)(tp, r);
    661 	}
    662 
    663 	if (flags & SABTTYF_RINGOVERFLOW)
    664 		log(LOG_WARNING, "%s: ring overflow\n",
    665 		    device_xname(sc->sc_dev));
    666 
    667 	if (flags & SABTTYF_DONE) {
    668 		ndflush(&tp->t_outq, sc->sc_txp - tp->t_outq.c_cf);
    669 		tp->t_state &= ~TS_BUSY;
    670 		(*tp->t_linesw->l_start)(tp);
    671 	}
    672 }
    673 
    674 int
    675 sabopen(dev_t dev, int flags, int mode, struct lwp *l)
    676 {
    677 	struct sabtty_softc *sc;
    678 	struct tty *tp;
    679 	int s, s1;
    680 
    681 	sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    682 	if (sc == NULL)
    683 		return (ENXIO);
    684 
    685 	tp = sc->sc_tty;
    686 	tp->t_dev = dev;
    687 
    688 	/*
    689 	 * If the device is exclusively for kernel use, deny userland
    690 	 * open.
    691 	 */
    692 	if (ISSET(tp->t_state, TS_KERN_ONLY))
    693 		return (EBUSY);
    694 
    695 	if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
    696 		return (EBUSY);
    697 
    698 	mutex_spin_enter(&tty_lock);
    699 	if (!ISSET(tp->t_state, TS_ISOPEN) && tp->t_wopen == 0) {
    700 		ttychars(tp);
    701 		tp->t_iflag = TTYDEF_IFLAG;
    702 		tp->t_oflag = TTYDEF_OFLAG;
    703 		tp->t_cflag = TTYDEF_CFLAG;
    704 		if (sc->sc_openflags & TIOCFLAG_CLOCAL)
    705 			tp->t_cflag |= CLOCAL;
    706 		if (sc->sc_openflags & TIOCFLAG_CRTSCTS)
    707 			tp->t_cflag |= CRTSCTS;
    708 		if (sc->sc_openflags & TIOCFLAG_MDMBUF)
    709 			tp->t_cflag |= MDMBUF;
    710 		tp->t_lflag = TTYDEF_LFLAG;
    711 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
    712 
    713 		sc->sc_rput = sc->sc_rget = sc->sc_rbuf;
    714 
    715 		ttsetwater(tp);
    716 
    717 		s1 = splhigh();
    718 		sabtty_reset(sc);
    719 		sabtty_param(tp, &tp->t_termios);
    720 		sc->sc_imr0 = SAB_IMR0_PERR | SAB_IMR0_FERR | SAB_IMR0_PLLA;
    721 		SAB_WRITE(sc, SAB_IMR0, sc->sc_imr0);
    722 		sc->sc_imr1 = SAB_IMR1_BRK | SAB_IMR1_ALLS | SAB_IMR1_XDU |
    723 		    SAB_IMR1_TIN | SAB_IMR1_CSC | SAB_IMR1_XMR | SAB_IMR1_XPR;
    724 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    725 		SAB_WRITE(sc, SAB_CCR0, SAB_READ(sc, SAB_CCR0) | SAB_CCR0_PU);
    726 		sabtty_cec_wait(sc);
    727 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XRES);
    728 		sabtty_cec_wait(sc);
    729 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
    730 		sabtty_cec_wait(sc);
    731 		splx(s1);
    732 
    733 		sabtty_flush(sc);
    734 
    735 		if ((sc->sc_openflags & TIOCFLAG_SOFTCAR) ||
    736 		    (SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD))
    737 			tp->t_state |= TS_CARR_ON;
    738 		else
    739 			tp->t_state &= ~TS_CARR_ON;
    740 	}
    741 
    742 	if ((flags & O_NONBLOCK) == 0) {
    743 		while ((tp->t_cflag & CLOCAL) == 0 &&
    744 		    (tp->t_state & TS_CARR_ON) == 0) {
    745 			int error;
    746 
    747 			error = ttysleep(tp, &tp->t_rawcv, true, 0);
    748 			if (error != 0) {
    749 				mutex_spin_exit(&tty_lock);
    750 				return (error);
    751 			}
    752 		}
    753 	}
    754 
    755 	mutex_spin_exit(&tty_lock);
    756 
    757 	s = (*tp->t_linesw->l_open)(dev, tp);
    758 	if (s != 0) {
    759 		mutex_spin_enter(&tty_lock);
    760 		if (tp->t_state & TS_ISOPEN) {
    761 			mutex_spin_exit(&tty_lock);
    762 			return (s);
    763 		}
    764 		if (tp->t_cflag & HUPCL) {
    765 			sabtty_mdmctrl(sc, 0, DMSET);
    766 			cv_wait(&lbolt, &tty_lock);
    767 		}
    768 
    769 		if ((sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) == 0) {
    770 			/* Flush and power down if we're not the console */
    771 			sabtty_flush(sc);
    772 			sabtty_reset(sc);
    773 		}
    774 		mutex_spin_exit(&tty_lock);
    775 	}
    776 	return (s);
    777 }
    778 
    779 int
    780 sabclose(dev_t dev, int flags, int mode, struct lwp *l)
    781 {
    782 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    783 	struct sab_softc *bc = sc->sc_parent;
    784 	struct tty *tp = sc->sc_tty;
    785 	int s;
    786 
    787 	(*tp->t_linesw->l_close)(tp, flags);
    788 
    789 	s = spltty();
    790 
    791 	if ((tp->t_state & TS_ISOPEN) == 0) {
    792 		/* Wait for output drain */
    793 		sc->sc_imr1 &= ~SAB_IMR1_ALLS;
    794 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    795 		sc->sc_flags |= SABTTYF_TXDRAIN;
    796 		(void)tsleep(sc, TTIPRI, ttclos, 5 * hz);
    797 		sc->sc_imr1 |= SAB_IMR1_ALLS;
    798 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    799 		sc->sc_flags &= ~SABTTYF_TXDRAIN;
    800 
    801 		if (tp->t_cflag & HUPCL) {
    802 			sabtty_mdmctrl(sc, 0, DMSET);
    803 			(void)tsleep(bc, TTIPRI, ttclos, hz);
    804 		}
    805 
    806 		if ((sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) == 0) {
    807 			/* Flush and power down if we're not the console */
    808 			sabtty_flush(sc);
    809 			sabtty_reset(sc);
    810 		}
    811 	}
    812 
    813 	ttyclose(tp);
    814 	splx(s);
    815 
    816 	return (0);
    817 }
    818 
    819 int
    820 sabread(dev_t dev, struct uio *uio, int flags)
    821 {
    822 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    823 	struct tty *tp = sc->sc_tty;
    824 
    825 	return ((*tp->t_linesw->l_read)(tp, uio, flags));
    826 }
    827 
    828 int
    829 sabwrite(dev_t dev, struct uio *uio, int flags)
    830 {
    831 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    832 	struct tty *tp = sc->sc_tty;
    833 
    834 	return ((*tp->t_linesw->l_write)(tp, uio, flags));
    835 }
    836 
    837 int
    838 sabioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    839 {
    840 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    841 	struct tty *tp = sc->sc_tty;
    842 	int error;
    843 
    844 	error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flags, l);
    845 	if (error >= 0)
    846 		return (error);
    847 
    848 	error = ttioctl(tp, cmd, data, flags, l);
    849 	if (error >= 0)
    850 		return (error);
    851 
    852 	error = 0;
    853 
    854 	switch (cmd) {
    855 	case TIOCSBRK:
    856 		SAB_WRITE(sc, SAB_DAFO,
    857 		    SAB_READ(sc, SAB_DAFO) | SAB_DAFO_XBRK);
    858 		break;
    859 	case TIOCCBRK:
    860 		SAB_WRITE(sc, SAB_DAFO,
    861 		    SAB_READ(sc, SAB_DAFO) & ~SAB_DAFO_XBRK);
    862 		break;
    863 	case TIOCSDTR:
    864 		sabtty_mdmctrl(sc, TIOCM_DTR, DMBIS);
    865 		break;
    866 	case TIOCCDTR:
    867 		sabtty_mdmctrl(sc, TIOCM_DTR, DMBIC);
    868 		break;
    869 	case TIOCMBIS:
    870 		sabtty_mdmctrl(sc, *((int *)data), DMBIS);
    871 		break;
    872 	case TIOCMBIC:
    873 		sabtty_mdmctrl(sc, *((int *)data), DMBIC);
    874 		break;
    875 	case TIOCMGET:
    876 		*((int *)data) = sabtty_mdmctrl(sc, 0, DMGET);
    877 		break;
    878 	case TIOCMSET:
    879 		sabtty_mdmctrl(sc, *((int *)data), DMSET);
    880 		break;
    881 	case TIOCGFLAGS:
    882 		*((int *)data) = sc->sc_openflags;
    883 		break;
    884 	case TIOCSFLAGS:
    885 		if (kauth_authorize_device_tty(l->l_cred,
    886 		    KAUTH_DEVICE_TTY_PRIVSET, tp))
    887 			error = EPERM;
    888 		else
    889 			sc->sc_openflags = *((int *)data) &
    890 			    (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
    891 			     TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
    892 		break;
    893 	default:
    894 		error = ENOTTY;
    895 	}
    896 
    897 	return (error);
    898 }
    899 
    900 struct tty *
    901 sabtty(dev_t dev)
    902 {
    903 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    904 
    905 	return (sc->sc_tty);
    906 }
    907 
    908 void
    909 sabstop(struct tty *tp, int flag)
    910 {
    911 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(tp->t_dev));
    912 	int s;
    913 
    914 	s = spltty();
    915 	if (tp->t_state & TS_BUSY) {
    916 		if ((tp->t_state & TS_TTSTOP) == 0)
    917 			tp->t_state |= TS_FLUSH;
    918 		sc->sc_flags |= SABTTYF_STOP;
    919 		sc->sc_imr1 &= ~SAB_IMR1_ALLS;
    920 		SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
    921 	}
    922 	splx(s);
    923 }
    924 
    925 int
    926 sabpoll(dev_t dev, int events, struct lwp *l)
    927 {
    928 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(dev));
    929 	struct tty *tp = sc->sc_tty;
    930 
    931 	return ((*tp->t_linesw->l_poll)(tp, events, l));
    932 }
    933 
    934 int
    935 sabtty_mdmctrl(struct sabtty_softc *sc, int bits, int how)
    936 {
    937 	uint8_t r;
    938 	int s;
    939 
    940 	s = spltty();
    941 	switch (how) {
    942 	case DMGET:
    943 		bits = 0;
    944 		if (SAB_READ(sc, SAB_STAR) & SAB_STAR_CTS)
    945 			bits |= TIOCM_CTS;
    946 		if ((SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD) == 0)
    947 			bits |= TIOCM_CD;
    948 
    949 		r = SAB_READ(sc, SAB_PVR);
    950 		if ((r & sc->sc_pvr_dtr) == 0)
    951 			bits |= TIOCM_DTR;
    952 		if ((r & sc->sc_pvr_dsr) == 0)
    953 			bits |= TIOCM_DSR;
    954 
    955 		r = SAB_READ(sc, SAB_MODE);
    956 		if ((r & (SAB_MODE_RTS|SAB_MODE_FRTS)) == SAB_MODE_RTS)
    957 			bits |= TIOCM_RTS;
    958 		break;
    959 	case DMSET:
    960 		r = SAB_READ(sc, SAB_MODE);
    961 		if (bits & TIOCM_RTS) {
    962 			r &= ~SAB_MODE_FRTS;
    963 			r |= SAB_MODE_RTS;
    964 		} else
    965 			r |= SAB_MODE_FRTS | SAB_MODE_RTS;
    966 		SAB_WRITE(sc, SAB_MODE, r);
    967 
    968 		r = SAB_READ(sc, SAB_PVR);
    969 		if (bits & TIOCM_DTR)
    970 			r &= ~sc->sc_pvr_dtr;
    971 		else
    972 			r |= sc->sc_pvr_dtr;
    973 		SAB_WRITE(sc, SAB_PVR, r);
    974 		break;
    975 	case DMBIS:
    976 		if (bits & TIOCM_RTS) {
    977 			r = SAB_READ(sc, SAB_MODE);
    978 			r &= ~SAB_MODE_FRTS;
    979 			r |= SAB_MODE_RTS;
    980 			SAB_WRITE(sc, SAB_MODE, r);
    981 		}
    982 		if (bits & TIOCM_DTR) {
    983 			r = SAB_READ(sc, SAB_PVR);
    984 			r &= ~sc->sc_pvr_dtr;
    985 			SAB_WRITE(sc, SAB_PVR, r);
    986 		}
    987 		break;
    988 	case DMBIC:
    989 		if (bits & TIOCM_RTS) {
    990 			r = SAB_READ(sc, SAB_MODE);
    991 			r |= SAB_MODE_FRTS | SAB_MODE_RTS;
    992 			SAB_WRITE(sc, SAB_MODE, r);
    993 		}
    994 		if (bits & TIOCM_DTR) {
    995 			r = SAB_READ(sc, SAB_PVR);
    996 			r |= sc->sc_pvr_dtr;
    997 			SAB_WRITE(sc, SAB_PVR, r);
    998 		}
    999 		break;
   1000 	}
   1001 	splx(s);
   1002 	return (bits);
   1003 }
   1004 
   1005 int
   1006 sabttyparam(struct sabtty_softc *sc, struct tty *tp, struct termios *t)
   1007 {
   1008 	int s, ospeed;
   1009 	tcflag_t cflag;
   1010 	uint8_t dafo, r;
   1011 
   1012 	ospeed = sabtty_speed(t->c_ospeed);
   1013 	if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
   1014 		return (EINVAL);
   1015 
   1016 	s = spltty();
   1017 
   1018 	/* hang up line if ospeed is zero, otherwise raise dtr */
   1019 	sabtty_mdmctrl(sc, TIOCM_DTR,
   1020 	    (t->c_ospeed == 0) ? DMBIC : DMBIS);
   1021 
   1022 	dafo = SAB_READ(sc, SAB_DAFO);
   1023 
   1024 	cflag = t->c_cflag;
   1025 
   1026 	if (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
   1027 		cflag |= CLOCAL;
   1028 		cflag &= ~HUPCL;
   1029 	}
   1030 
   1031 	if (cflag & CSTOPB)
   1032 		dafo |= SAB_DAFO_STOP;
   1033 	else
   1034 		dafo &= ~SAB_DAFO_STOP;
   1035 
   1036 	dafo &= ~SAB_DAFO_CHL_CSIZE;
   1037 	switch (cflag & CSIZE) {
   1038 	case CS5:
   1039 		dafo |= SAB_DAFO_CHL_CS5;
   1040 		break;
   1041 	case CS6:
   1042 		dafo |= SAB_DAFO_CHL_CS6;
   1043 		break;
   1044 	case CS7:
   1045 		dafo |= SAB_DAFO_CHL_CS7;
   1046 		break;
   1047 	default:
   1048 		dafo |= SAB_DAFO_CHL_CS8;
   1049 		break;
   1050 	}
   1051 
   1052 	dafo &= ~SAB_DAFO_PARMASK;
   1053 	if (cflag & PARENB) {
   1054 		if (cflag & PARODD)
   1055 			dafo |= SAB_DAFO_PAR_ODD;
   1056 		else
   1057 			dafo |= SAB_DAFO_PAR_EVEN;
   1058 	} else
   1059 		dafo |= SAB_DAFO_PAR_NONE;
   1060 	SAB_WRITE(sc, SAB_DAFO, dafo);
   1061 
   1062 	if (!(sc->sc_flags & SABTTYF_IS_RSC) && ospeed != 0) {
   1063 		SAB_WRITE(sc, SAB_BGR, ospeed & 0xff);
   1064 		r = SAB_READ(sc, SAB_CCR2);
   1065 		r &= ~(SAB_CCR2_BR9 | SAB_CCR2_BR8);
   1066 		r |= (ospeed >> 2) & (SAB_CCR2_BR9 | SAB_CCR2_BR8);
   1067 		SAB_WRITE(sc, SAB_CCR2, r);
   1068 	}
   1069 
   1070 	r = SAB_READ(sc, SAB_MODE);
   1071 	r |= SAB_MODE_RAC;
   1072 	if (cflag & CRTSCTS) {
   1073 		r &= ~(SAB_MODE_RTS | SAB_MODE_FCTS);
   1074 		r |= SAB_MODE_FRTS;
   1075 		sc->sc_imr1 &= ~SAB_IMR1_CSC;
   1076 	} else {
   1077 		r |= SAB_MODE_RTS | SAB_MODE_FCTS;
   1078 		r &= ~SAB_MODE_FRTS;
   1079 		sc->sc_imr1 |= SAB_IMR1_CSC;
   1080 	}
   1081 	SAB_WRITE(sc, SAB_MODE, r);
   1082 	SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
   1083 
   1084 	tp->t_cflag = cflag;
   1085 
   1086 	splx(s);
   1087 	return (0);
   1088 }
   1089 
   1090 int
   1091 sabtty_param(struct tty *tp, struct termios *t)
   1092 {
   1093 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(tp->t_dev));
   1094 
   1095 	return (sabttyparam(sc, tp, t));
   1096 }
   1097 
   1098 void
   1099 sabtty_start(struct tty *tp)
   1100 {
   1101 	struct sabtty_softc *sc = device_lookup_private(&sabtty_cd, SABUNIT(tp->t_dev));
   1102 	int s;
   1103 
   1104 	s = spltty();
   1105 	if ((tp->t_state & (TS_TTSTOP | TS_TIMEOUT | TS_BUSY)) == 0) {
   1106 		if (ttypull(tp)) {
   1107 			sc->sc_txc = ndqb(&tp->t_outq, 0);
   1108 			sc->sc_txp = tp->t_outq.c_cf;
   1109 			tp->t_state |= TS_BUSY;
   1110 			sc->sc_imr1 &= ~(SAB_ISR1_XPR | SAB_ISR1_ALLS);
   1111 			SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
   1112 		}
   1113 	}
   1114 	splx(s);
   1115 }
   1116 
   1117 void
   1118 sabtty_cec_wait(struct sabtty_softc *sc)
   1119 {
   1120 	int i = 50000;
   1121 
   1122 	for (;;) {
   1123 		if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_CEC) == 0)
   1124 			return;
   1125 		if (--i == 0)
   1126 			break;
   1127 		DELAY(1);
   1128 	}
   1129 }
   1130 
   1131 void
   1132 sabtty_tec_wait(struct sabtty_softc *sc)
   1133 {
   1134 	int i = 200000;
   1135 
   1136 	for (;;) {
   1137 		if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_TEC) == 0)
   1138 			return;
   1139 		if (--i == 0)
   1140 			break;
   1141 		DELAY(1);
   1142 	}
   1143 }
   1144 
   1145 void
   1146 sabtty_reset(struct sabtty_softc *sc)
   1147 {
   1148 	/* power down */
   1149 	SAB_WRITE(sc, SAB_CCR0, 0);
   1150 
   1151 	/* set basic configuration */
   1152 	SAB_WRITE(sc, SAB_CCR0,
   1153 	    SAB_CCR0_MCE | SAB_CCR0_SC_NRZ | SAB_CCR0_SM_ASYNC);
   1154 	SAB_WRITE(sc, SAB_CCR1, SAB_CCR1_ODS | SAB_CCR1_BCR | SAB_CCR1_CM_7);
   1155 	SAB_WRITE(sc, SAB_CCR2, SAB_CCR2_BDF | SAB_CCR2_SSEL | SAB_CCR2_TOE);
   1156 	SAB_WRITE(sc, SAB_CCR3, 0);
   1157 	SAB_WRITE(sc, SAB_CCR4, SAB_CCR4_MCK4 | SAB_CCR4_EBRG | SAB_CCR4_ICD);
   1158 	SAB_WRITE(sc, SAB_MODE, SAB_MODE_RTS | SAB_MODE_FCTS | SAB_MODE_RAC);
   1159 	SAB_WRITE(sc, SAB_RFC,
   1160 	    SAB_RFC_DPS | SAB_RFC_RFDF | SAB_RFC_RFTH_32CHAR);
   1161 
   1162 	/* clear interrupts */
   1163 	sc->sc_imr0 = sc->sc_imr1 = 0xff;
   1164 	SAB_WRITE(sc, SAB_IMR0, sc->sc_imr0);
   1165 	SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
   1166 	(void)SAB_READ(sc, SAB_ISR0);
   1167 	(void)SAB_READ(sc, SAB_ISR1);
   1168 }
   1169 
   1170 void
   1171 sabtty_flush(struct sabtty_softc *sc)
   1172 {
   1173 	/* clear rx fifo */
   1174 	sabtty_cec_wait(sc);
   1175 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
   1176 
   1177 	/* clear tx fifo */
   1178 	sabtty_cec_wait(sc);
   1179 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XRES);
   1180 }
   1181 
   1182 int
   1183 sabtty_speed(int rate)
   1184 {
   1185 	int i, len, r;
   1186 
   1187 	if (rate == 0)
   1188 		return (0);
   1189 	len = sizeof(sabtty_baudtable)/sizeof(sabtty_baudtable[0]);
   1190 	for (i = 0; i < len; i++) {
   1191 		if (rate == sabtty_baudtable[i].baud) {
   1192 			r = sabtty_baudtable[i].n |
   1193 			    (sabtty_baudtable[i].m << 6);
   1194 			return (r);
   1195 		}
   1196 	}
   1197 	return (-1);
   1198 }
   1199 
   1200 void
   1201 sabtty_cnputc(struct sabtty_softc *sc, int c)
   1202 {
   1203 	sabtty_tec_wait(sc);
   1204 	SAB_WRITE(sc, SAB_TIC, c);
   1205 	sabtty_tec_wait(sc);
   1206 }
   1207 
   1208 int
   1209 sabtty_cngetc(struct sabtty_softc *sc)
   1210 {
   1211 	uint8_t r, len;
   1212 
   1213 again:
   1214 	do {
   1215 		r = SAB_READ(sc, SAB_STAR);
   1216 	} while ((r & SAB_STAR_RFNE) == 0);
   1217 
   1218 	/*
   1219 	 * Ok, at least one byte in RFIFO, ask for permission to access RFIFO
   1220 	 * (I hate this chip... hate hate hate).
   1221 	 */
   1222 	sabtty_cec_wait(sc);
   1223 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RFRD);
   1224 
   1225 	/* Wait for RFIFO to come ready */
   1226 	do {
   1227 		r = SAB_READ(sc, SAB_ISR0);
   1228 	} while ((r & SAB_ISR0_TCD) == 0);
   1229 
   1230 	len = SAB_READ(sc, SAB_RBCL) & (32 - 1);
   1231 	if (len == 0)
   1232 		goto again;	/* Shouldn't happen... */
   1233 
   1234 	r = SAB_READ(sc, SAB_RFIFO);
   1235 
   1236 	/*
   1237 	 * Blow away everything left in the FIFO...
   1238 	 */
   1239 	sabtty_cec_wait(sc);
   1240 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RMC);
   1241 	return (r);
   1242 }
   1243 
   1244 void
   1245 sabtty_cnpollc(struct sabtty_softc *sc, int on)
   1246 {
   1247 	uint8_t r;
   1248 
   1249 	if (on) {
   1250 		if (sc->sc_polling)
   1251 			return;
   1252 		SAB_WRITE(sc, SAB_IPC, SAB_READ(sc, SAB_IPC) | SAB_IPC_VIS);
   1253 		r = sc->sc_pollrfc = SAB_READ(sc, SAB_RFC);
   1254 		r &= ~(SAB_RFC_RFDF);
   1255 		SAB_WRITE(sc, SAB_RFC, r);
   1256 		sabtty_cec_wait(sc);
   1257 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
   1258 		sc->sc_polling = 1;
   1259 	} else {
   1260 		if (!sc->sc_polling)
   1261 			return;
   1262 		SAB_WRITE(sc, SAB_IPC, SAB_READ(sc, SAB_IPC) & ~SAB_IPC_VIS);
   1263 		SAB_WRITE(sc, SAB_RFC, sc->sc_pollrfc);
   1264 		sabtty_cec_wait(sc);
   1265 		SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
   1266 		sc->sc_polling = 0;
   1267 	}
   1268 }
   1269 
   1270 void
   1271 sab_cnputc(dev_t dev, int c)
   1272 {
   1273 	struct sabtty_softc *sc = sabtty_cons_output;
   1274 
   1275 	if (sc == NULL)
   1276 		return;
   1277 	sabtty_cnputc(sc, c);
   1278 }
   1279 
   1280 void
   1281 sab_cnpollc(dev_t dev, int on)
   1282 {
   1283 	struct sabtty_softc *sc = sabtty_cons_input;
   1284 
   1285 	sabtty_cnpollc(sc, on);
   1286 }
   1287 
   1288 int
   1289 sab_cngetc(dev_t dev)
   1290 {
   1291 	struct sabtty_softc *sc = sabtty_cons_input;
   1292 
   1293 	if (sc == NULL)
   1294 		return (-1);
   1295 	return (sabtty_cngetc(sc));
   1296 }
   1297 
   1298 void
   1299 sabtty_console_flags(struct sabtty_softc *sc)
   1300 {
   1301 	int node, channel, cookie;
   1302 	char buf[255];
   1303 
   1304 	node = sc->sc_parent->sc_node;
   1305 	channel = sc->sc_portno;
   1306 
   1307 	/* Default to channel 0 if there are no explicit prom args */
   1308 	cookie = 0;
   1309 
   1310 	if (node == prom_instance_to_package(prom_stdin())) {
   1311 		if (prom_getoption("input-device", buf, sizeof buf) == 0 &&
   1312 			strcmp("ttyb", buf) == 0)
   1313 				cookie = 1;
   1314 
   1315 		if (channel == cookie)
   1316 			sc->sc_flags |= SABTTYF_CONS_IN;
   1317 	}
   1318 
   1319 	/* Default to same channel if there are no explicit prom args */
   1320 
   1321 	if (node == prom_instance_to_package(prom_stdout())) {
   1322 		if (prom_getoption("output-device", buf, sizeof buf) == 0 &&
   1323 			strcmp("ttyb", buf) == 0)
   1324 				cookie = 1;
   1325 
   1326 		if (channel == cookie)
   1327 			sc->sc_flags |= SABTTYF_CONS_OUT;
   1328 	}
   1329 	/* Are we connected to an E250 RSC? */
   1330 	if (channel == prom_getpropint(node, "ssp-console", -1) ||
   1331 	    channel == prom_getpropint(node, "ssp-control", -1))
   1332 		sc->sc_flags |= SABTTYF_IS_RSC;
   1333 }
   1334 
   1335 bool
   1336 sabtty_shutdown(device_t dev, int how)
   1337 {
   1338 	struct sabtty_softc *sc = device_private(dev);
   1339 
   1340 	/* Have to put the chip back into single char mode */
   1341 	sc->sc_flags |= SABTTYF_DONTDDB;
   1342 	SAB_WRITE(sc, SAB_RFC, SAB_READ(sc, SAB_RFC) & ~SAB_RFC_RFDF);
   1343 	sabtty_cec_wait(sc);
   1344 	SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
   1345 	sabtty_cec_wait(sc);
   1346 	return true;
   1347 }
   1348 
   1349 #ifdef KGDB
   1350 static int
   1351 sab_kgdb_getc(void *arg)
   1352 {
   1353 	struct sabtty_softc *sc = arg;
   1354 
   1355 	return sabtty_cngetc(sc);
   1356 }
   1357 
   1358 static void
   1359 sab_kgdb_putc(void *arg, int c)
   1360 {
   1361 	struct sabtty_softc *sc = arg;
   1362 
   1363 	return sabtty_cnputc(sc, c);
   1364 }
   1365 
   1366 #ifndef KGDB_DEVRATE
   1367 #define KGDB_DEVRATE    9600
   1368 #endif
   1369 
   1370 int
   1371 sab_kgdb_check(struct sabtty_softc *sc)
   1372 {
   1373 	return strcmp(device_xname(sc->sc_dev), KGDB_DEVNAME) == 0;
   1374 }
   1375 
   1376 void
   1377 sab_kgdb_init(struct sabtty_softc *sc)
   1378 {
   1379 	int sp;
   1380 	uint8_t dafo, r;
   1381 	extern int kgdb_break_at_attach;
   1382 
   1383 	kgdb_attach(sab_kgdb_getc, sab_kgdb_putc, sc);
   1384 	kgdb_dev = 123;	/* not used, but checked against NODEV */
   1385 
   1386 	/*
   1387 	 * Configure the port to speed/8n1
   1388 	 */
   1389 	dafo = SAB_READ(sc, SAB_DAFO);
   1390 	dafo &= ~(SAB_DAFO_STOP|SAB_DAFO_CHL_CSIZE|SAB_DAFO_PARMASK);
   1391 	dafo |= SAB_DAFO_CHL_CS8|SAB_DAFO_PAR_NONE;
   1392 	SAB_WRITE(sc, SAB_DAFO, dafo);
   1393 	sp = sabtty_speed(KGDB_DEVRATE);
   1394 	SAB_WRITE(sc, SAB_BGR, sp & 0xff);
   1395 	r = SAB_READ(sc, SAB_CCR2);
   1396 	r &= ~(SAB_CCR2_BR9 | SAB_CCR2_BR8);
   1397 	r |= (sp >> 2) & (SAB_CCR2_BR9 | SAB_CCR2_BR8);
   1398 	SAB_WRITE(sc, SAB_CCR2, r);
   1399 
   1400 	/*
   1401 	 * If we are booting with -d, break into kgdb now
   1402 	 */
   1403 	if (kgdb_break_at_attach)
   1404 		kgdb_connect(1);
   1405 }
   1406 #endif
   1407