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