sab.c revision 1.41 1 /* $NetBSD: sab.c,v 1.41 2008/05/25 19:22:21 ad 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.41 2008/05/25 19:22:21 ad Exp $");
46
47 #include "opt_kgdb.h"
48 #include <sys/types.h>
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/device.h>
52 #include <sys/conf.h>
53 #include <sys/file.h>
54 #include <sys/ioctl.h>
55 #include <sys/kernel.h>
56 #include <sys/proc.h>
57 #include <sys/tty.h>
58 #include <sys/syslog.h>
59 #include <sys/kauth.h>
60 #include <sys/kgdb.h>
61 #include <sys/intr.h>
62
63 #include <machine/autoconf.h>
64 #include <machine/openfirm.h>
65
66 #include <dev/cons.h>
67
68 #include <dev/ebus/ebusreg.h>
69 #include <dev/ebus/ebusvar.h>
70 #include <sparc64/dev/sab82532reg.h>
71
72 #include "locators.h"
73
74 #define SABUNIT(x) (minor(x) & 0x7ffff)
75 #define SABDIALOUT(x) (minor(x) & 0x80000)
76
77 #define SABTTY_RBUF_SIZE 1024 /* must be divisible by 2 */
78
79 struct sab_softc {
80 struct device sc_dv;
81 struct intrhand * sc_ih;
82 bus_space_tag_t sc_bt;
83 bus_space_handle_t sc_bh;
84 struct sabtty_softc * sc_child[SAB_NCHAN];
85 u_int sc_nchild;
86 void * sc_softintr;
87 int sc_node;
88 };
89
90 struct sabtty_attach_args {
91 u_int sbt_portno;
92 };
93
94 struct sabtty_softc {
95 struct device sc_dv;
96 struct sab_softc * sc_parent;
97 bus_space_tag_t sc_bt;
98 bus_space_handle_t sc_bh;
99 struct tty * sc_tty;
100 u_int sc_portno;
101 uint8_t sc_pvr_dtr, sc_pvr_dsr;
102 uint8_t sc_imr0, sc_imr1;
103 int sc_openflags;
104 u_char * sc_txp;
105 int sc_txc;
106 int sc_flags;
107 #define SABTTYF_STOP 0x01
108 #define SABTTYF_DONE 0x02
109 #define SABTTYF_RINGOVERFLOW 0x04
110 #define SABTTYF_CDCHG 0x08
111 #define SABTTYF_CONS_IN 0x10
112 #define SABTTYF_CONS_OUT 0x20
113 #define SABTTYF_TXDRAIN 0x40
114 #define SABTTYF_DONTDDB 0x80
115 uint8_t sc_rbuf[SABTTY_RBUF_SIZE];
116 uint8_t *sc_rend, *sc_rput, *sc_rget;
117 uint8_t sc_polling, sc_pollrfc;
118 };
119
120 struct sabtty_softc *sabtty_cons_input;
121 struct sabtty_softc *sabtty_cons_output;
122
123 #define SAB_READ(sc,r) \
124 bus_space_read_1((sc)->sc_bt, (sc)->sc_bh, (r))
125 #define SAB_WRITE(sc,r,v) \
126 bus_space_write_1((sc)->sc_bt, (sc)->sc_bh, (r), (v))
127 #define SAB_WRITE_BLOCK(sc,r,p,c) \
128 bus_space_write_region_1((sc)->sc_bt, (sc)->sc_bh, (r), (p), (c))
129
130 int sab_match(struct device *, struct cfdata *, void *);
131 void sab_attach(struct device *, struct device *, void *);
132 int sab_print(void *, const char *);
133 int sab_intr(void *);
134
135 void sab_softintr(void *);
136 void sab_cnputc(dev_t, int);
137 int sab_cngetc(dev_t);
138 void sab_cnpollc(dev_t, int);
139
140 int sabtty_match(struct device *, struct cfdata *, void *);
141 void sabtty_attach(struct device *, struct device *, void *);
142 void sabtty_start(struct tty *);
143 int sabtty_param(struct tty *, struct termios *);
144 int sabtty_intr(struct sabtty_softc *, int *);
145 void sabtty_softintr(struct sabtty_softc *);
146 int sabtty_mdmctrl(struct sabtty_softc *, int, int);
147 void sabtty_cec_wait(struct sabtty_softc *);
148 void sabtty_tec_wait(struct sabtty_softc *);
149 void sabtty_reset(struct sabtty_softc *);
150 void sabtty_flush(struct sabtty_softc *);
151 int sabtty_speed(int);
152 void sabtty_console_flags(struct sabtty_softc *);
153 void sabtty_cnpollc(struct sabtty_softc *, int);
154 void sabtty_shutdown(void *);
155 int sabttyparam(struct sabtty_softc *, struct tty *, struct termios *);
156
157 #ifdef KGDB
158 int sab_kgdb_check(struct sabtty_softc *);
159 void sab_kgdb_init(struct sabtty_softc *);
160 #endif
161
162 void sabtty_cnputc(struct sabtty_softc *, int);
163 int sabtty_cngetc(struct sabtty_softc *);
164
165 CFATTACH_DECL(sab, sizeof(struct sab_softc),
166 sab_match, sab_attach, NULL, NULL);
167
168 extern struct cfdriver sab_cd;
169
170 CFATTACH_DECL(sabtty, sizeof(struct sabtty_softc),
171 sabtty_match, sabtty_attach, NULL, NULL);
172
173 extern struct cfdriver sabtty_cd;
174
175 dev_type_open(sabopen);
176 dev_type_close(sabclose);
177 dev_type_read(sabread);
178 dev_type_write(sabwrite);
179 dev_type_ioctl(sabioctl);
180 dev_type_stop(sabstop);
181 dev_type_tty(sabtty);
182 dev_type_poll(sabpoll);
183
184 static struct cnm_state sabtty_cnm_state;
185
186 const struct cdevsw sabtty_cdevsw = {
187 sabopen, sabclose, sabread, sabwrite, sabioctl,
188 sabstop, sabtty, sabpoll, nommap, ttykqfilter, D_TTY
189 };
190
191 struct sabtty_rate {
192 int baud;
193 int n, m;
194 };
195
196 struct sabtty_rate sabtty_baudtable[] = {
197 { 50, 35, 10 },
198 { 75, 47, 9 },
199 { 110, 32, 9 },
200 { 134, 53, 8 },
201 { 150, 47, 8 },
202 { 200, 35, 8 },
203 { 300, 47, 7 },
204 { 600, 47, 6 },
205 { 1200, 47, 5 },
206 { 1800, 31, 5 },
207 { 2400, 47, 4 },
208 { 4800, 47, 3 },
209 { 9600, 47, 2 },
210 { 19200, 47, 1 },
211 { 38400, 23, 1 },
212 { 57600, 15, 1 },
213 { 115200, 7, 1 },
214 { 230400, 3, 1 },
215 { 460800, 1, 1 },
216 { 76800, 11, 1 },
217 { 153600, 5, 1 },
218 { 307200, 3, 1 },
219 { 614400, 3, 0 },
220 { 921600, 0, 1 },
221 };
222
223 int
224 sab_match(struct device *parent, struct cfdata *match, void *aux)
225 {
226 struct ebus_attach_args *ea = aux;
227 char *compat;
228
229 if (strcmp(ea->ea_name, "se") == 0)
230 return (1);
231
232 compat = prom_getpropstring(ea->ea_node, "compatible");
233 if (compat != NULL && !strcmp(compat, "sab82532"))
234 return (1);
235
236 return (0);
237 }
238
239 void
240 sab_attach(struct device *parent, struct device *self, void *aux)
241 {
242 struct sab_softc *sc = (struct sab_softc *)self;
243 struct ebus_attach_args *ea = aux;
244 uint8_t r;
245 u_int i;
246 int locs[SABCF_NLOCS];
247
248 sc->sc_bt = ea->ea_bustag;
249 sc->sc_node = ea->ea_node;
250
251 /* Use prom mapping, if available. */
252 if (ea->ea_nvaddr)
253 sparc_promaddr_to_handle(sc->sc_bt, ea->ea_vaddr[0],
254 &sc->sc_bh);
255 else if (bus_space_map(sc->sc_bt, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
256 ea->ea_reg[0].size, 0, &sc->sc_bh) != 0) {
257 printf(": can't map register space\n");
258 return;
259 }
260
261 sc->sc_ih = bus_intr_establish(ea->ea_bustag, ea->ea_intr[0],
262 IPL_TTY, sab_intr, sc);
263 if (sc->sc_ih == NULL) {
264 printf(": can't map interrupt\n");
265 return;
266 }
267
268 sc->sc_softintr = softint_establish(SOFTINT_SERIAL, sab_softintr, sc);
269 if (sc->sc_softintr == NULL) {
270 printf(": can't get soft intr\n");
271 return;
272 }
273
274 aprint_normal(": rev ");
275 r = SAB_READ(sc, SAB_VSTR) & SAB_VSTR_VMASK;
276 switch (r) {
277 case SAB_VSTR_V_1:
278 aprint_normal("1");
279 break;
280 case SAB_VSTR_V_2:
281 aprint_normal("2");
282 break;
283 case SAB_VSTR_V_32:
284 aprint_normal("3.2");
285 break;
286 default:
287 aprint_normal("unknown(0x%x)", r);
288 break;
289 }
290 aprint_normal("\n");
291
292 /* Let current output drain */
293 DELAY(100000);
294
295 /* Set all pins, except DTR pins to be inputs */
296 SAB_WRITE(sc, SAB_PCR, ~(SAB_PVR_DTR_A | SAB_PVR_DTR_B));
297 /* Disable port interrupts */
298 SAB_WRITE(sc, SAB_PIM, 0xff);
299 SAB_WRITE(sc, SAB_PVR, SAB_PVR_DTR_A | SAB_PVR_DTR_B | SAB_PVR_MAGIC);
300 SAB_WRITE(sc, SAB_IPC, SAB_IPC_ICPL);
301
302 for (i = 0; i < SAB_NCHAN; i++) {
303 struct sabtty_attach_args stax;
304
305 stax.sbt_portno = i;
306
307 locs[SABCF_CHANNEL] = i;
308
309 sc->sc_child[i] =
310 (struct sabtty_softc *)config_found_sm_loc(self,
311 "sab", locs, &stax, sab_print, config_stdsubmatch);
312 if (sc->sc_child[i] != NULL)
313 sc->sc_nchild++;
314 }
315 }
316
317 int
318 sab_print(void *args, const char *name)
319 {
320 struct sabtty_attach_args *sa = args;
321
322 if (name)
323 aprint_normal("sabtty at %s", name);
324 aprint_normal(" port %u", sa->sbt_portno);
325 return (UNCONF);
326 }
327
328 int
329 sab_intr(void *vsc)
330 {
331 struct sab_softc *sc = vsc;
332 int r = 0, needsoft = 0;
333 uint8_t gis;
334
335 gis = SAB_READ(sc, SAB_GIS);
336
337 /* channel A */
338 if ((gis & (SAB_GIS_ISA1 | SAB_GIS_ISA0)) && sc->sc_child[0] &&
339 sc->sc_child[0]->sc_tty)
340 r |= sabtty_intr(sc->sc_child[0], &needsoft);
341
342 /* channel B */
343 if ((gis & (SAB_GIS_ISB1 | SAB_GIS_ISB0)) && sc->sc_child[1] &&
344 sc->sc_child[1]->sc_tty)
345 r |= sabtty_intr(sc->sc_child[1], &needsoft);
346
347 if (needsoft)
348 softint_schedule(sc->sc_softintr);
349
350 return (r);
351 }
352
353 void
354 sab_softintr(void *vsc)
355 {
356 struct sab_softc *sc = vsc;
357
358 if (sc->sc_child[0] && sc->sc_child[0]->sc_tty)
359 sabtty_softintr(sc->sc_child[0]);
360 if (sc->sc_child[1] && sc->sc_child[1]->sc_tty)
361 sabtty_softintr(sc->sc_child[1]);
362 }
363
364 int
365 sabtty_match(struct device *parent, struct cfdata *match, void *aux)
366 {
367
368 return (1);
369 }
370
371 void
372 sabtty_attach(struct device *parent, struct device *self, void *aux)
373 {
374 struct sabtty_softc *sc = (struct sabtty_softc *)self;
375 struct sabtty_attach_args *sa = aux;
376 int r;
377 int maj;
378 int is_kgdb = 0;
379
380 #ifdef KGDB
381 is_kgdb = sab_kgdb_check(sc);
382 #endif
383
384 if (!is_kgdb) {
385 sc->sc_tty = ttymalloc();
386 if (sc->sc_tty == NULL) {
387 aprint_normal(": failed to allocate tty\n");
388 return;
389 }
390 tty_attach(sc->sc_tty);
391 sc->sc_tty->t_oproc = sabtty_start;
392 sc->sc_tty->t_param = sabtty_param;
393 }
394
395 sc->sc_parent = (struct sab_softc *)parent;
396 sc->sc_bt = sc->sc_parent->sc_bt;
397 sc->sc_portno = sa->sbt_portno;
398 sc->sc_rend = sc->sc_rbuf + SABTTY_RBUF_SIZE;
399
400 switch (sa->sbt_portno) {
401 case 0: /* port A */
402 sc->sc_pvr_dtr = SAB_PVR_DTR_A;
403 sc->sc_pvr_dsr = SAB_PVR_DSR_A;
404 r = bus_space_subregion(sc->sc_bt, sc->sc_parent->sc_bh,
405 SAB_CHAN_A, SAB_CHANLEN, &sc->sc_bh);
406 break;
407 case 1: /* port B */
408 sc->sc_pvr_dtr = SAB_PVR_DTR_B;
409 sc->sc_pvr_dsr = SAB_PVR_DSR_B;
410 r = bus_space_subregion(sc->sc_bt, sc->sc_parent->sc_bh,
411 SAB_CHAN_B, SAB_CHANLEN, &sc->sc_bh);
412 break;
413 default:
414 aprint_normal(": invalid channel: %u\n", sa->sbt_portno);
415 return;
416 }
417 if (r != 0) {
418 aprint_normal(": failed to allocate register subregion\n");
419 return;
420 }
421
422 sabtty_console_flags(sc);
423
424 if (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
425 struct termios t;
426 const char *acc;
427
428 /* Let residual prom output drain */
429 DELAY(100000);
430
431 switch (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
432 case SABTTYF_CONS_IN:
433 acc = "input";
434 break;
435 case SABTTYF_CONS_OUT:
436 acc = "output";
437 break;
438 case SABTTYF_CONS_IN|SABTTYF_CONS_OUT:
439 default:
440 acc = "i/o";
441 break;
442 }
443
444 t.c_ispeed= 0;
445 t.c_ospeed = 9600;
446 t.c_cflag = CREAD | CS8 | HUPCL;
447 sc->sc_tty->t_ospeed = 0;
448 sabttyparam(sc, sc->sc_tty, &t);
449
450 if (sc->sc_flags & SABTTYF_CONS_IN) {
451 sabtty_cons_input = sc;
452 cn_tab->cn_pollc = sab_cnpollc;
453 cn_tab->cn_getc = sab_cngetc;
454 maj = cdevsw_lookup_major(&sabtty_cdevsw);
455 cn_tab->cn_dev = makedev(maj, device_unit(self));
456 shutdownhook_establish(sabtty_shutdown, sc);
457 cn_init_magic(&sabtty_cnm_state);
458 cn_set_magic("\047\001"); /* default magic is BREAK */
459 }
460
461 if (sc->sc_flags & SABTTYF_CONS_OUT) {
462 sabtty_tec_wait(sc);
463 sabtty_cons_output = sc;
464 cn_tab->cn_putc = sab_cnputc;
465 maj = cdevsw_lookup_major(&sabtty_cdevsw);
466 cn_tab->cn_dev = makedev(maj, device_unit(self));
467 }
468 aprint_normal(": console %s", acc);
469 } else {
470 /* Not a console... */
471 sabtty_reset(sc);
472
473 #ifdef KGDB
474 if (is_kgdb) {
475 sab_kgdb_init(sc);
476 aprint_normal(": kgdb");
477 }
478 #endif
479 }
480
481 aprint_normal("\n");
482 }
483
484 int
485 sabtty_intr(struct sabtty_softc *sc, int *needsoftp)
486 {
487 uint8_t isr0, isr1;
488 int i, len = 0, needsoft = 0, r = 0, clearfifo = 0;
489
490 isr0 = SAB_READ(sc, SAB_ISR0);
491 isr1 = SAB_READ(sc, SAB_ISR1);
492
493 if (isr0 || isr1)
494 r = 1;
495
496 if (isr0 & SAB_ISR0_RPF) {
497 len = 32;
498 clearfifo = 1;
499 }
500 if (isr0 & SAB_ISR0_TCD) {
501 len = (32 - 1) & SAB_READ(sc, SAB_RBCL);
502 clearfifo = 1;
503 }
504 if (isr0 & SAB_ISR0_TIME) {
505 sabtty_cec_wait(sc);
506 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RFRD);
507 }
508 if (isr0 & SAB_ISR0_RFO) {
509 sc->sc_flags |= SABTTYF_RINGOVERFLOW;
510 clearfifo = 1;
511 }
512 if (len != 0) {
513 uint8_t *ptr, b;
514
515 ptr = sc->sc_rput;
516 for (i = 0; i < len; i++) {
517 b = SAB_READ(sc, SAB_RFIFO);
518 if (i % 2 == 0) /* skip status byte */
519 cn_check_magic(sc->sc_tty->t_dev,
520 b, sabtty_cnm_state);
521 *ptr++ = b;
522 if (ptr == sc->sc_rend)
523 ptr = sc->sc_rbuf;
524 if (ptr == sc->sc_rget) {
525 if (ptr == sc->sc_rbuf)
526 ptr = sc->sc_rend;
527 ptr--;
528 sc->sc_flags |= SABTTYF_RINGOVERFLOW;
529 }
530 }
531 sc->sc_rput = ptr;
532 needsoft = 1;
533 }
534
535 if (clearfifo) {
536 sabtty_cec_wait(sc);
537 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RMC);
538 }
539
540 if (isr0 & SAB_ISR0_CDSC) {
541 sc->sc_flags |= SABTTYF_CDCHG;
542 needsoft = 1;
543 }
544
545 if (isr1 & SAB_ISR1_BRKT)
546 cn_check_magic(sc->sc_tty->t_dev,
547 CNC_BREAK, sabtty_cnm_state);
548
549 if (isr1 & (SAB_ISR1_XPR | SAB_ISR1_ALLS)) {
550 if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_XFW) &&
551 (sc->sc_flags & SABTTYF_STOP) == 0) {
552 if (sc->sc_txc < 32)
553 len = sc->sc_txc;
554 else
555 len = 32;
556
557 if (len > 0) {
558 SAB_WRITE_BLOCK(sc, SAB_XFIFO, sc->sc_txp, len);
559 sc->sc_txp += len;
560 sc->sc_txc -= len;
561
562 sabtty_cec_wait(sc);
563 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XF);
564
565 /*
566 * Prevent the false end of xmit from
567 * confusing things below.
568 */
569 isr1 &= ~SAB_ISR1_ALLS;
570 }
571 }
572
573 if ((sc->sc_txc == 0) || (sc->sc_flags & SABTTYF_STOP)) {
574 if ((sc->sc_imr1 & SAB_IMR1_XPR) == 0) {
575 sc->sc_imr1 |= SAB_IMR1_XPR;
576 sc->sc_imr1 &= ~SAB_IMR1_ALLS;
577 SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
578 }
579 }
580 }
581
582 if ((isr1 & SAB_ISR1_ALLS) && ((sc->sc_txc == 0) ||
583 (sc->sc_flags & SABTTYF_STOP))) {
584 if (sc->sc_flags & SABTTYF_TXDRAIN)
585 wakeup(sc);
586 sc->sc_flags &= ~SABTTYF_STOP;
587 sc->sc_flags |= SABTTYF_DONE;
588 sc->sc_imr1 |= SAB_IMR1_ALLS;
589 SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
590 needsoft = 1;
591 }
592
593 if (needsoft)
594 *needsoftp = needsoft;
595 return (r);
596 }
597
598 void
599 sabtty_softintr(struct sabtty_softc *sc)
600 {
601 struct tty *tp = sc->sc_tty;
602 int s, flags;
603 uint8_t r;
604
605 if (tp == NULL)
606 return;
607
608 if ((tp->t_state & TS_ISOPEN) == 0)
609 return;
610
611 while (sc->sc_rget != sc->sc_rput) {
612 int data;
613 uint8_t stat;
614
615 data = sc->sc_rget[0];
616 stat = sc->sc_rget[1];
617 sc->sc_rget += 2;
618 if (stat & SAB_RSTAT_PE)
619 data |= TTY_PE;
620 if (stat & SAB_RSTAT_FE)
621 data |= TTY_FE;
622 if (sc->sc_rget == sc->sc_rend)
623 sc->sc_rget = sc->sc_rbuf;
624
625 (*tp->t_linesw->l_rint)(data, tp);
626 }
627
628 s = splhigh();
629 flags = sc->sc_flags;
630 sc->sc_flags &= ~(SABTTYF_DONE|SABTTYF_CDCHG|SABTTYF_RINGOVERFLOW);
631 splx(s);
632
633 if (flags & SABTTYF_CDCHG) {
634 s = spltty();
635 r = SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD;
636 splx(s);
637
638 (*tp->t_linesw->l_modem)(tp, r);
639 }
640
641 if (flags & SABTTYF_RINGOVERFLOW)
642 log(LOG_WARNING, "%s: ring overflow\n",
643 device_xname(&sc->sc_dv));
644
645 if (flags & SABTTYF_DONE) {
646 ndflush(&tp->t_outq, sc->sc_txp - tp->t_outq.c_cf);
647 tp->t_state &= ~TS_BUSY;
648 (*tp->t_linesw->l_start)(tp);
649 }
650 }
651
652 int
653 sabopen(dev_t dev, int flags, int mode, struct lwp *l)
654 {
655 struct sabtty_softc *sc;
656 struct tty *tp;
657 struct proc *p;
658 int s, s1;
659
660 sc = device_lookup(&sabtty_cd, SABUNIT(dev));
661 if (sc == NULL)
662 return (ENXIO);
663
664 tp = sc->sc_tty;
665 tp->t_dev = dev;
666 p = l->l_proc;
667
668 if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN, tp))
669 return (EBUSY);
670
671 mutex_spin_enter(&tty_lock);
672 if ((tp->t_state & TS_ISOPEN) == 0) {
673 ttychars(tp);
674 tp->t_iflag = TTYDEF_IFLAG;
675 tp->t_oflag = TTYDEF_OFLAG;
676 tp->t_cflag = TTYDEF_CFLAG;
677 if (sc->sc_openflags & TIOCFLAG_CLOCAL)
678 tp->t_cflag |= CLOCAL;
679 if (sc->sc_openflags & TIOCFLAG_CRTSCTS)
680 tp->t_cflag |= CRTSCTS;
681 if (sc->sc_openflags & TIOCFLAG_MDMBUF)
682 tp->t_cflag |= MDMBUF;
683 tp->t_lflag = TTYDEF_LFLAG;
684 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
685
686 sc->sc_rput = sc->sc_rget = sc->sc_rbuf;
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 }
714
715 if ((flags & O_NONBLOCK) == 0) {
716 while ((tp->t_cflag & CLOCAL) == 0 &&
717 (tp->t_state & TS_CARR_ON) == 0) {
718 int error;
719
720 error = ttysleep(tp, &tp->t_rawcv, true, 0);
721 if (error != 0) {
722 mutex_spin_exit(&tty_lock);
723 return (error);
724 }
725 }
726 }
727
728 mutex_spin_exit(&tty_lock);
729
730 s = (*tp->t_linesw->l_open)(dev, tp);
731 if (s != 0) {
732 mutex_spin_enter(&tty_lock);
733 if (tp->t_state & TS_ISOPEN) {
734 mutex_spin_exit(&tty_lock);
735 return (s);
736 }
737 if (tp->t_cflag & HUPCL) {
738 sabtty_mdmctrl(sc, 0, DMSET);
739 cv_wait(&lbolt, &tty_lock);
740 }
741
742 if ((sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) == 0) {
743 /* Flush and power down if we're not the console */
744 sabtty_flush(sc);
745 sabtty_reset(sc);
746 }
747 mutex_spin_exit(&tty_lock);
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 (ttypull(tp)) {
1080 sc->sc_txc = ndqb(&tp->t_outq, 0);
1081 sc->sc_txp = tp->t_outq.c_cf;
1082 tp->t_state |= TS_BUSY;
1083 sc->sc_imr1 &= ~(SAB_ISR1_XPR | SAB_ISR1_ALLS);
1084 SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
1085 }
1086 }
1087 splx(s);
1088 }
1089
1090 void
1091 sabtty_cec_wait(struct sabtty_softc *sc)
1092 {
1093 int i = 50000;
1094
1095 for (;;) {
1096 if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_CEC) == 0)
1097 return;
1098 if (--i == 0)
1099 break;
1100 DELAY(1);
1101 }
1102 }
1103
1104 void
1105 sabtty_tec_wait(struct sabtty_softc *sc)
1106 {
1107 int i = 200000;
1108
1109 for (;;) {
1110 if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_TEC) == 0)
1111 return;
1112 if (--i == 0)
1113 break;
1114 DELAY(1);
1115 }
1116 }
1117
1118 void
1119 sabtty_reset(struct sabtty_softc *sc)
1120 {
1121 /* power down */
1122 SAB_WRITE(sc, SAB_CCR0, 0);
1123
1124 /* set basic configuration */
1125 SAB_WRITE(sc, SAB_CCR0,
1126 SAB_CCR0_MCE | SAB_CCR0_SC_NRZ | SAB_CCR0_SM_ASYNC);
1127 SAB_WRITE(sc, SAB_CCR1, SAB_CCR1_ODS | SAB_CCR1_BCR | SAB_CCR1_CM_7);
1128 SAB_WRITE(sc, SAB_CCR2, SAB_CCR2_BDF | SAB_CCR2_SSEL | SAB_CCR2_TOE);
1129 SAB_WRITE(sc, SAB_CCR3, 0);
1130 SAB_WRITE(sc, SAB_CCR4, SAB_CCR4_MCK4 | SAB_CCR4_EBRG | SAB_CCR4_ICD);
1131 SAB_WRITE(sc, SAB_MODE, SAB_MODE_RTS | SAB_MODE_FCTS | SAB_MODE_RAC);
1132 SAB_WRITE(sc, SAB_RFC,
1133 SAB_RFC_DPS | SAB_RFC_RFDF | SAB_RFC_RFTH_32CHAR);
1134
1135 /* clear interrupts */
1136 sc->sc_imr0 = sc->sc_imr1 = 0xff;
1137 SAB_WRITE(sc, SAB_IMR0, sc->sc_imr0);
1138 SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
1139 (void)SAB_READ(sc, SAB_ISR0);
1140 (void)SAB_READ(sc, SAB_ISR1);
1141 }
1142
1143 void
1144 sabtty_flush(struct sabtty_softc *sc)
1145 {
1146 /* clear rx fifo */
1147 sabtty_cec_wait(sc);
1148 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
1149
1150 /* clear tx fifo */
1151 sabtty_cec_wait(sc);
1152 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XRES);
1153 }
1154
1155 int
1156 sabtty_speed(int rate)
1157 {
1158 int i, len, r;
1159
1160 if (rate == 0)
1161 return (0);
1162 len = sizeof(sabtty_baudtable)/sizeof(sabtty_baudtable[0]);
1163 for (i = 0; i < len; i++) {
1164 if (rate == sabtty_baudtable[i].baud) {
1165 r = sabtty_baudtable[i].n |
1166 (sabtty_baudtable[i].m << 6);
1167 return (r);
1168 }
1169 }
1170 return (-1);
1171 }
1172
1173 void
1174 sabtty_cnputc(struct sabtty_softc *sc, int c)
1175 {
1176 sabtty_tec_wait(sc);
1177 SAB_WRITE(sc, SAB_TIC, c);
1178 sabtty_tec_wait(sc);
1179 }
1180
1181 int
1182 sabtty_cngetc(struct sabtty_softc *sc)
1183 {
1184 uint8_t r, len;
1185
1186 again:
1187 do {
1188 r = SAB_READ(sc, SAB_STAR);
1189 } while ((r & SAB_STAR_RFNE) == 0);
1190
1191 /*
1192 * Ok, at least one byte in RFIFO, ask for permission to access RFIFO
1193 * (I hate this chip... hate hate hate).
1194 */
1195 sabtty_cec_wait(sc);
1196 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RFRD);
1197
1198 /* Wait for RFIFO to come ready */
1199 do {
1200 r = SAB_READ(sc, SAB_ISR0);
1201 } while ((r & SAB_ISR0_TCD) == 0);
1202
1203 len = SAB_READ(sc, SAB_RBCL) & (32 - 1);
1204 if (len == 0)
1205 goto again; /* Shouldn't happen... */
1206
1207 r = SAB_READ(sc, SAB_RFIFO);
1208
1209 /*
1210 * Blow away everything left in the FIFO...
1211 */
1212 sabtty_cec_wait(sc);
1213 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RMC);
1214 return (r);
1215 }
1216
1217 void
1218 sabtty_cnpollc(struct sabtty_softc *sc, int on)
1219 {
1220 uint8_t r;
1221
1222 if (on) {
1223 if (sc->sc_polling)
1224 return;
1225 SAB_WRITE(sc, SAB_IPC, SAB_READ(sc, SAB_IPC) | SAB_IPC_VIS);
1226 r = sc->sc_pollrfc = SAB_READ(sc, SAB_RFC);
1227 r &= ~(SAB_RFC_RFDF);
1228 SAB_WRITE(sc, SAB_RFC, r);
1229 sabtty_cec_wait(sc);
1230 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
1231 sc->sc_polling = 1;
1232 } else {
1233 if (!sc->sc_polling)
1234 return;
1235 SAB_WRITE(sc, SAB_IPC, SAB_READ(sc, SAB_IPC) & ~SAB_IPC_VIS);
1236 SAB_WRITE(sc, SAB_RFC, sc->sc_pollrfc);
1237 sabtty_cec_wait(sc);
1238 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
1239 sc->sc_polling = 0;
1240 }
1241 }
1242
1243 void
1244 sab_cnputc(dev_t dev, int c)
1245 {
1246 struct sabtty_softc *sc = sabtty_cons_output;
1247
1248 if (sc == NULL)
1249 return;
1250 sabtty_cnputc(sc, c);
1251 }
1252
1253 void
1254 sab_cnpollc(dev_t dev, int on)
1255 {
1256 struct sabtty_softc *sc = sabtty_cons_input;
1257
1258 sabtty_cnpollc(sc, on);
1259 }
1260
1261 int
1262 sab_cngetc(dev_t dev)
1263 {
1264 struct sabtty_softc *sc = sabtty_cons_input;
1265
1266 if (sc == NULL)
1267 return (-1);
1268 return (sabtty_cngetc(sc));
1269 }
1270
1271 void
1272 sabtty_console_flags(struct sabtty_softc *sc)
1273 {
1274 int node, channel, cookie;
1275 char buf[255];
1276
1277 node = sc->sc_parent->sc_node;
1278 channel = sc->sc_portno;
1279
1280 /* Default to channel 0 if there are no explicit prom args */
1281 cookie = 0;
1282
1283 if (node == prom_instance_to_package(prom_stdin())) {
1284 if (prom_getoption("input-device", buf, sizeof buf) == 0 &&
1285 strcmp("ttyb", buf) == 0)
1286 cookie = 1;
1287
1288 if (channel == cookie)
1289 sc->sc_flags |= SABTTYF_CONS_IN;
1290 }
1291
1292 /* Default to same channel if there are no explicit prom args */
1293
1294 if (node == prom_instance_to_package(prom_stdout())) {
1295 if (prom_getoption("output-device", buf, sizeof buf) == 0 &&
1296 strcmp("ttyb", buf) == 0)
1297 cookie = 1;
1298
1299 if (channel == cookie)
1300 sc->sc_flags |= SABTTYF_CONS_OUT;
1301 }
1302 }
1303
1304 void
1305 sabtty_shutdown(void *vsc)
1306 {
1307 struct sabtty_softc *sc = vsc;
1308
1309 /* Have to put the chip back into single char mode */
1310 sc->sc_flags |= SABTTYF_DONTDDB;
1311 SAB_WRITE(sc, SAB_RFC, SAB_READ(sc, SAB_RFC) & ~SAB_RFC_RFDF);
1312 sabtty_cec_wait(sc);
1313 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
1314 sabtty_cec_wait(sc);
1315 }
1316
1317 #ifdef KGDB
1318 static int
1319 sab_kgdb_getc(void *arg)
1320 {
1321 struct sabtty_softc *sc = arg;
1322
1323 return sabtty_cngetc(sc);
1324 }
1325
1326 static void
1327 sab_kgdb_putc(void *arg, int c)
1328 {
1329 struct sabtty_softc *sc = arg;
1330
1331 return sabtty_cnputc(sc, c);
1332 }
1333
1334 #ifndef KGDB_DEVRATE
1335 #define KGDB_DEVRATE 9600
1336 #endif
1337
1338 int
1339 sab_kgdb_check(struct sabtty_softc *sc)
1340 {
1341 return strcmp(device_xname(&sc->sc_dv), KGDB_DEVNAME) == 0;
1342 }
1343
1344 void
1345 sab_kgdb_init(struct sabtty_softc *sc)
1346 {
1347 int sp;
1348 uint8_t dafo, r;
1349 extern int kgdb_break_at_attach;
1350
1351 kgdb_attach(sab_kgdb_getc, sab_kgdb_putc, sc);
1352 kgdb_dev = 123; /* not used, but checked against NODEV */
1353
1354 /*
1355 * Configure the port to speed/8n1
1356 */
1357 dafo = SAB_READ(sc, SAB_DAFO);
1358 dafo &= ~(SAB_DAFO_STOP|SAB_DAFO_CHL_CSIZE|SAB_DAFO_PARMASK);
1359 dafo |= SAB_DAFO_CHL_CS8|SAB_DAFO_PAR_NONE;
1360 SAB_WRITE(sc, SAB_DAFO, dafo);
1361 sp = sabtty_speed(KGDB_DEVRATE);
1362 SAB_WRITE(sc, SAB_BGR, sp & 0xff);
1363 r = SAB_READ(sc, SAB_CCR2);
1364 r &= ~(SAB_CCR2_BR9 | SAB_CCR2_BR8);
1365 r |= (sp >> 2) & (SAB_CCR2_BR9 | SAB_CCR2_BR8);
1366 SAB_WRITE(sc, SAB_CCR2, r);
1367
1368 /*
1369 * If we are booting with -d, break into kgdb now
1370 */
1371 if (kgdb_break_at_attach)
1372 kgdb_connect(1);
1373 }
1374 #endif
1375