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