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