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