sab.c revision 1.39 1 /* $NetBSD: sab.c,v 1.39 2007/11/19 18:51:43 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.39 2007/11/19 18:51:43 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
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 mutex_spin_enter(&tty_lock);
671 if ((tp->t_state & TS_ISOPEN) == 0) {
672 ttychars(tp);
673 tp->t_iflag = TTYDEF_IFLAG;
674 tp->t_oflag = TTYDEF_OFLAG;
675 tp->t_cflag = TTYDEF_CFLAG;
676 if (sc->sc_openflags & TIOCFLAG_CLOCAL)
677 tp->t_cflag |= CLOCAL;
678 if (sc->sc_openflags & TIOCFLAG_CRTSCTS)
679 tp->t_cflag |= CRTSCTS;
680 if (sc->sc_openflags & TIOCFLAG_MDMBUF)
681 tp->t_cflag |= MDMBUF;
682 tp->t_lflag = TTYDEF_LFLAG;
683 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
684
685 sc->sc_rput = sc->sc_rget = sc->sc_rbuf;
686
687 ttsetwater(tp);
688
689 s1 = splhigh();
690 sabtty_reset(sc);
691 sabtty_param(tp, &tp->t_termios);
692 sc->sc_imr0 = SAB_IMR0_PERR | SAB_IMR0_FERR | SAB_IMR0_PLLA;
693 SAB_WRITE(sc, SAB_IMR0, sc->sc_imr0);
694 sc->sc_imr1 = SAB_IMR1_BRK | SAB_IMR1_ALLS | SAB_IMR1_XDU |
695 SAB_IMR1_TIN | SAB_IMR1_CSC | SAB_IMR1_XMR | SAB_IMR1_XPR;
696 SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
697 SAB_WRITE(sc, SAB_CCR0, SAB_READ(sc, SAB_CCR0) | SAB_CCR0_PU);
698 sabtty_cec_wait(sc);
699 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XRES);
700 sabtty_cec_wait(sc);
701 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
702 sabtty_cec_wait(sc);
703 splx(s1);
704
705 sabtty_flush(sc);
706
707 if ((sc->sc_openflags & TIOCFLAG_SOFTCAR) ||
708 (SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD))
709 tp->t_state |= TS_CARR_ON;
710 else
711 tp->t_state &= ~TS_CARR_ON;
712 }
713
714 if ((flags & O_NONBLOCK) == 0) {
715 while ((tp->t_cflag & CLOCAL) == 0 &&
716 (tp->t_state & TS_CARR_ON) == 0) {
717 int error;
718
719 error = ttysleep(tp, &tp->t_rawq.c_cv, true, 0);
720 if (error != 0) {
721 mutex_spin_exit(&tty_lock);
722 return (error);
723 }
724 }
725 }
726
727 mutex_spin_exit(&tty_lock);
728
729 s = (*tp->t_linesw->l_open)(dev, tp);
730 if (s != 0) {
731 mutex_spin_enter(&tty_lock);
732 if (tp->t_state & TS_ISOPEN) {
733 mutex_spin_exit(&tty_lock);
734 return (s);
735 }
736 if (tp->t_cflag & HUPCL) {
737 sabtty_mdmctrl(sc, 0, DMSET);
738 cv_wait(&lbolt, &tty_lock);
739 }
740
741 if ((sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) == 0) {
742 /* Flush and power down if we're not the console */
743 sabtty_flush(sc);
744 sabtty_reset(sc);
745 }
746 mutex_spin_exit(&tty_lock);
747 }
748 return (s);
749 }
750
751 int
752 sabclose(dev_t dev, int flags, int mode, struct lwp *l)
753 {
754 struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
755 struct sab_softc *bc = sc->sc_parent;
756 struct tty *tp = sc->sc_tty;
757 int s;
758
759 (*tp->t_linesw->l_close)(tp, flags);
760
761 s = spltty();
762
763 if ((tp->t_state & TS_ISOPEN) == 0) {
764 /* Wait for output drain */
765 sc->sc_imr1 &= ~SAB_IMR1_ALLS;
766 SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
767 sc->sc_flags |= SABTTYF_TXDRAIN;
768 (void)tsleep(sc, TTIPRI, ttclos, 5 * hz);
769 sc->sc_imr1 |= SAB_IMR1_ALLS;
770 SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
771 sc->sc_flags &= ~SABTTYF_TXDRAIN;
772
773 if (tp->t_cflag & HUPCL) {
774 sabtty_mdmctrl(sc, 0, DMSET);
775 (void)tsleep(bc, TTIPRI, ttclos, hz);
776 }
777
778 if ((sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) == 0) {
779 /* Flush and power down if we're not the console */
780 sabtty_flush(sc);
781 sabtty_reset(sc);
782 }
783 }
784
785 ttyclose(tp);
786 splx(s);
787
788 return (0);
789 }
790
791 int
792 sabread(dev_t dev, struct uio *uio, int flags)
793 {
794 struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
795 struct tty *tp = sc->sc_tty;
796
797 return ((*tp->t_linesw->l_read)(tp, uio, flags));
798 }
799
800 int
801 sabwrite(dev_t dev, struct uio *uio, int flags)
802 {
803 struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
804 struct tty *tp = sc->sc_tty;
805
806 return ((*tp->t_linesw->l_write)(tp, uio, flags));
807 }
808
809 int
810 sabioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
811 {
812 struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
813 struct tty *tp = sc->sc_tty;
814 int error;
815
816 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flags, l);
817 if (error >= 0)
818 return (error);
819
820 error = ttioctl(tp, cmd, data, flags, l);
821 if (error >= 0)
822 return (error);
823
824 error = 0;
825
826 switch (cmd) {
827 case TIOCSBRK:
828 SAB_WRITE(sc, SAB_DAFO,
829 SAB_READ(sc, SAB_DAFO) | SAB_DAFO_XBRK);
830 break;
831 case TIOCCBRK:
832 SAB_WRITE(sc, SAB_DAFO,
833 SAB_READ(sc, SAB_DAFO) & ~SAB_DAFO_XBRK);
834 break;
835 case TIOCSDTR:
836 sabtty_mdmctrl(sc, TIOCM_DTR, DMBIS);
837 break;
838 case TIOCCDTR:
839 sabtty_mdmctrl(sc, TIOCM_DTR, DMBIC);
840 break;
841 case TIOCMBIS:
842 sabtty_mdmctrl(sc, *((int *)data), DMBIS);
843 break;
844 case TIOCMBIC:
845 sabtty_mdmctrl(sc, *((int *)data), DMBIC);
846 break;
847 case TIOCMGET:
848 *((int *)data) = sabtty_mdmctrl(sc, 0, DMGET);
849 break;
850 case TIOCMSET:
851 sabtty_mdmctrl(sc, *((int *)data), DMSET);
852 break;
853 case TIOCGFLAGS:
854 *((int *)data) = sc->sc_openflags;
855 break;
856 case TIOCSFLAGS:
857 if (kauth_authorize_device_tty(l->l_cred,
858 KAUTH_DEVICE_TTY_PRIVSET, tp))
859 error = EPERM;
860 else
861 sc->sc_openflags = *((int *)data) &
862 (TIOCFLAG_SOFTCAR | TIOCFLAG_CLOCAL |
863 TIOCFLAG_CRTSCTS | TIOCFLAG_MDMBUF);
864 break;
865 default:
866 error = ENOTTY;
867 }
868
869 return (error);
870 }
871
872 struct tty *
873 sabtty(dev_t dev)
874 {
875 struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
876
877 return (sc->sc_tty);
878 }
879
880 void
881 sabstop(struct tty *tp, int flag)
882 {
883 struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(tp->t_dev));
884 int s;
885
886 s = spltty();
887 if (tp->t_state & TS_BUSY) {
888 if ((tp->t_state & TS_TTSTOP) == 0)
889 tp->t_state |= TS_FLUSH;
890 sc->sc_flags |= SABTTYF_STOP;
891 sc->sc_imr1 &= ~SAB_IMR1_ALLS;
892 SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
893 }
894 splx(s);
895 }
896
897 int
898 sabpoll(dev_t dev, int events, struct lwp *l)
899 {
900 struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(dev));
901 struct tty *tp = sc->sc_tty;
902
903 return ((*tp->t_linesw->l_poll)(tp, events, l));
904 }
905
906 int
907 sabtty_mdmctrl(struct sabtty_softc *sc, int bits, int how)
908 {
909 uint8_t r;
910 int s;
911
912 s = spltty();
913 switch (how) {
914 case DMGET:
915 bits = 0;
916 if (SAB_READ(sc, SAB_STAR) & SAB_STAR_CTS)
917 bits |= TIOCM_CTS;
918 if ((SAB_READ(sc, SAB_VSTR) & SAB_VSTR_CD) == 0)
919 bits |= TIOCM_CD;
920
921 r = SAB_READ(sc, SAB_PVR);
922 if ((r & sc->sc_pvr_dtr) == 0)
923 bits |= TIOCM_DTR;
924 if ((r & sc->sc_pvr_dsr) == 0)
925 bits |= TIOCM_DSR;
926
927 r = SAB_READ(sc, SAB_MODE);
928 if ((r & (SAB_MODE_RTS|SAB_MODE_FRTS)) == SAB_MODE_RTS)
929 bits |= TIOCM_RTS;
930 break;
931 case DMSET:
932 r = SAB_READ(sc, SAB_MODE);
933 if (bits & TIOCM_RTS) {
934 r &= ~SAB_MODE_FRTS;
935 r |= SAB_MODE_RTS;
936 } else
937 r |= SAB_MODE_FRTS | SAB_MODE_RTS;
938 SAB_WRITE(sc, SAB_MODE, r);
939
940 r = SAB_READ(sc, SAB_PVR);
941 if (bits & TIOCM_DTR)
942 r &= ~sc->sc_pvr_dtr;
943 else
944 r |= sc->sc_pvr_dtr;
945 SAB_WRITE(sc, SAB_PVR, r);
946 break;
947 case DMBIS:
948 if (bits & TIOCM_RTS) {
949 r = SAB_READ(sc, SAB_MODE);
950 r &= ~SAB_MODE_FRTS;
951 r |= SAB_MODE_RTS;
952 SAB_WRITE(sc, SAB_MODE, r);
953 }
954 if (bits & TIOCM_DTR) {
955 r = SAB_READ(sc, SAB_PVR);
956 r &= ~sc->sc_pvr_dtr;
957 SAB_WRITE(sc, SAB_PVR, r);
958 }
959 break;
960 case DMBIC:
961 if (bits & TIOCM_RTS) {
962 r = SAB_READ(sc, SAB_MODE);
963 r |= SAB_MODE_FRTS | SAB_MODE_RTS;
964 SAB_WRITE(sc, SAB_MODE, r);
965 }
966 if (bits & TIOCM_DTR) {
967 r = SAB_READ(sc, SAB_PVR);
968 r |= sc->sc_pvr_dtr;
969 SAB_WRITE(sc, SAB_PVR, r);
970 }
971 break;
972 }
973 splx(s);
974 return (bits);
975 }
976
977 int
978 sabttyparam(struct sabtty_softc *sc, struct tty *tp, struct termios *t)
979 {
980 int s, ospeed;
981 tcflag_t cflag;
982 uint8_t dafo, r;
983
984 ospeed = sabtty_speed(t->c_ospeed);
985 if (ospeed < 0 || (t->c_ispeed && t->c_ispeed != t->c_ospeed))
986 return (EINVAL);
987
988 s = spltty();
989
990 /* hang up line if ospeed is zero, otherwise raise dtr */
991 sabtty_mdmctrl(sc, TIOCM_DTR,
992 (t->c_ospeed == 0) ? DMBIC : DMBIS);
993
994 dafo = SAB_READ(sc, SAB_DAFO);
995
996 cflag = t->c_cflag;
997
998 if (sc->sc_flags & (SABTTYF_CONS_IN | SABTTYF_CONS_OUT)) {
999 cflag |= CLOCAL;
1000 cflag &= ~HUPCL;
1001 }
1002
1003 if (cflag & CSTOPB)
1004 dafo |= SAB_DAFO_STOP;
1005 else
1006 dafo &= ~SAB_DAFO_STOP;
1007
1008 dafo &= ~SAB_DAFO_CHL_CSIZE;
1009 switch (cflag & CSIZE) {
1010 case CS5:
1011 dafo |= SAB_DAFO_CHL_CS5;
1012 break;
1013 case CS6:
1014 dafo |= SAB_DAFO_CHL_CS6;
1015 break;
1016 case CS7:
1017 dafo |= SAB_DAFO_CHL_CS7;
1018 break;
1019 default:
1020 dafo |= SAB_DAFO_CHL_CS8;
1021 break;
1022 }
1023
1024 dafo &= ~SAB_DAFO_PARMASK;
1025 if (cflag & PARENB) {
1026 if (cflag & PARODD)
1027 dafo |= SAB_DAFO_PAR_ODD;
1028 else
1029 dafo |= SAB_DAFO_PAR_EVEN;
1030 } else
1031 dafo |= SAB_DAFO_PAR_NONE;
1032 SAB_WRITE(sc, SAB_DAFO, dafo);
1033
1034 if (ospeed != 0) {
1035 SAB_WRITE(sc, SAB_BGR, ospeed & 0xff);
1036 r = SAB_READ(sc, SAB_CCR2);
1037 r &= ~(SAB_CCR2_BR9 | SAB_CCR2_BR8);
1038 r |= (ospeed >> 2) & (SAB_CCR2_BR9 | SAB_CCR2_BR8);
1039 SAB_WRITE(sc, SAB_CCR2, r);
1040 }
1041
1042 r = SAB_READ(sc, SAB_MODE);
1043 r |= SAB_MODE_RAC;
1044 if (cflag & CRTSCTS) {
1045 r &= ~(SAB_MODE_RTS | SAB_MODE_FCTS);
1046 r |= SAB_MODE_FRTS;
1047 sc->sc_imr1 &= ~SAB_IMR1_CSC;
1048 } else {
1049 r |= SAB_MODE_RTS | SAB_MODE_FCTS;
1050 r &= ~SAB_MODE_FRTS;
1051 sc->sc_imr1 |= SAB_IMR1_CSC;
1052 }
1053 SAB_WRITE(sc, SAB_MODE, r);
1054 SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
1055
1056 tp->t_cflag = cflag;
1057
1058 splx(s);
1059 return (0);
1060 }
1061
1062 int
1063 sabtty_param(struct tty *tp, struct termios *t)
1064 {
1065 struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(tp->t_dev));
1066
1067 return (sabttyparam(sc, tp, t));
1068 }
1069
1070 void
1071 sabtty_start(struct tty *tp)
1072 {
1073 struct sabtty_softc *sc = device_lookup(&sabtty_cd, SABUNIT(tp->t_dev));
1074 int s;
1075
1076 s = spltty();
1077 if ((tp->t_state & (TS_TTSTOP | TS_TIMEOUT | TS_BUSY)) == 0) {
1078 if (ttypull(tp)) {
1079 sc->sc_txc = ndqb(&tp->t_outq, 0);
1080 sc->sc_txp = tp->t_outq.c_cf;
1081 tp->t_state |= TS_BUSY;
1082 sc->sc_imr1 &= ~(SAB_ISR1_XPR | SAB_ISR1_ALLS);
1083 SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
1084 }
1085 }
1086 splx(s);
1087 }
1088
1089 void
1090 sabtty_cec_wait(struct sabtty_softc *sc)
1091 {
1092 int i = 50000;
1093
1094 for (;;) {
1095 if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_CEC) == 0)
1096 return;
1097 if (--i == 0)
1098 break;
1099 DELAY(1);
1100 }
1101 }
1102
1103 void
1104 sabtty_tec_wait(struct sabtty_softc *sc)
1105 {
1106 int i = 200000;
1107
1108 for (;;) {
1109 if ((SAB_READ(sc, SAB_STAR) & SAB_STAR_TEC) == 0)
1110 return;
1111 if (--i == 0)
1112 break;
1113 DELAY(1);
1114 }
1115 }
1116
1117 void
1118 sabtty_reset(struct sabtty_softc *sc)
1119 {
1120 /* power down */
1121 SAB_WRITE(sc, SAB_CCR0, 0);
1122
1123 /* set basic configuration */
1124 SAB_WRITE(sc, SAB_CCR0,
1125 SAB_CCR0_MCE | SAB_CCR0_SC_NRZ | SAB_CCR0_SM_ASYNC);
1126 SAB_WRITE(sc, SAB_CCR1, SAB_CCR1_ODS | SAB_CCR1_BCR | SAB_CCR1_CM_7);
1127 SAB_WRITE(sc, SAB_CCR2, SAB_CCR2_BDF | SAB_CCR2_SSEL | SAB_CCR2_TOE);
1128 SAB_WRITE(sc, SAB_CCR3, 0);
1129 SAB_WRITE(sc, SAB_CCR4, SAB_CCR4_MCK4 | SAB_CCR4_EBRG | SAB_CCR4_ICD);
1130 SAB_WRITE(sc, SAB_MODE, SAB_MODE_RTS | SAB_MODE_FCTS | SAB_MODE_RAC);
1131 SAB_WRITE(sc, SAB_RFC,
1132 SAB_RFC_DPS | SAB_RFC_RFDF | SAB_RFC_RFTH_32CHAR);
1133
1134 /* clear interrupts */
1135 sc->sc_imr0 = sc->sc_imr1 = 0xff;
1136 SAB_WRITE(sc, SAB_IMR0, sc->sc_imr0);
1137 SAB_WRITE(sc, SAB_IMR1, sc->sc_imr1);
1138 (void)SAB_READ(sc, SAB_ISR0);
1139 (void)SAB_READ(sc, SAB_ISR1);
1140 }
1141
1142 void
1143 sabtty_flush(struct sabtty_softc *sc)
1144 {
1145 /* clear rx fifo */
1146 sabtty_cec_wait(sc);
1147 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
1148
1149 /* clear tx fifo */
1150 sabtty_cec_wait(sc);
1151 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_XRES);
1152 }
1153
1154 int
1155 sabtty_speed(int rate)
1156 {
1157 int i, len, r;
1158
1159 if (rate == 0)
1160 return (0);
1161 len = sizeof(sabtty_baudtable)/sizeof(sabtty_baudtable[0]);
1162 for (i = 0; i < len; i++) {
1163 if (rate == sabtty_baudtable[i].baud) {
1164 r = sabtty_baudtable[i].n |
1165 (sabtty_baudtable[i].m << 6);
1166 return (r);
1167 }
1168 }
1169 return (-1);
1170 }
1171
1172 void
1173 sabtty_cnputc(struct sabtty_softc *sc, int c)
1174 {
1175 sabtty_tec_wait(sc);
1176 SAB_WRITE(sc, SAB_TIC, c);
1177 sabtty_tec_wait(sc);
1178 }
1179
1180 int
1181 sabtty_cngetc(struct sabtty_softc *sc)
1182 {
1183 uint8_t r, len;
1184
1185 again:
1186 do {
1187 r = SAB_READ(sc, SAB_STAR);
1188 } while ((r & SAB_STAR_RFNE) == 0);
1189
1190 /*
1191 * Ok, at least one byte in RFIFO, ask for permission to access RFIFO
1192 * (I hate this chip... hate hate hate).
1193 */
1194 sabtty_cec_wait(sc);
1195 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RFRD);
1196
1197 /* Wait for RFIFO to come ready */
1198 do {
1199 r = SAB_READ(sc, SAB_ISR0);
1200 } while ((r & SAB_ISR0_TCD) == 0);
1201
1202 len = SAB_READ(sc, SAB_RBCL) & (32 - 1);
1203 if (len == 0)
1204 goto again; /* Shouldn't happen... */
1205
1206 r = SAB_READ(sc, SAB_RFIFO);
1207
1208 /*
1209 * Blow away everything left in the FIFO...
1210 */
1211 sabtty_cec_wait(sc);
1212 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RMC);
1213 return (r);
1214 }
1215
1216 void
1217 sabtty_cnpollc(struct sabtty_softc *sc, int on)
1218 {
1219 uint8_t r;
1220
1221 if (on) {
1222 if (sc->sc_polling)
1223 return;
1224 SAB_WRITE(sc, SAB_IPC, SAB_READ(sc, SAB_IPC) | SAB_IPC_VIS);
1225 r = sc->sc_pollrfc = SAB_READ(sc, SAB_RFC);
1226 r &= ~(SAB_RFC_RFDF);
1227 SAB_WRITE(sc, SAB_RFC, r);
1228 sabtty_cec_wait(sc);
1229 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
1230 sc->sc_polling = 1;
1231 } else {
1232 if (!sc->sc_polling)
1233 return;
1234 SAB_WRITE(sc, SAB_IPC, SAB_READ(sc, SAB_IPC) & ~SAB_IPC_VIS);
1235 SAB_WRITE(sc, SAB_RFC, sc->sc_pollrfc);
1236 sabtty_cec_wait(sc);
1237 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
1238 sc->sc_polling = 0;
1239 }
1240 }
1241
1242 void
1243 sab_cnputc(dev_t dev, int c)
1244 {
1245 struct sabtty_softc *sc = sabtty_cons_output;
1246
1247 if (sc == NULL)
1248 return;
1249 sabtty_cnputc(sc, c);
1250 }
1251
1252 void
1253 sab_cnpollc(dev_t dev, int on)
1254 {
1255 struct sabtty_softc *sc = sabtty_cons_input;
1256
1257 sabtty_cnpollc(sc, on);
1258 }
1259
1260 int
1261 sab_cngetc(dev_t dev)
1262 {
1263 struct sabtty_softc *sc = sabtty_cons_input;
1264
1265 if (sc == NULL)
1266 return (-1);
1267 return (sabtty_cngetc(sc));
1268 }
1269
1270 void
1271 sabtty_console_flags(struct sabtty_softc *sc)
1272 {
1273 int node, channel, cookie;
1274 char buf[255];
1275
1276 node = sc->sc_parent->sc_node;
1277 channel = sc->sc_portno;
1278
1279 /* Default to channel 0 if there are no explicit prom args */
1280 cookie = 0;
1281
1282 if (node == prom_instance_to_package(prom_stdin())) {
1283 if (prom_getoption("input-device", buf, sizeof buf) == 0 &&
1284 strcmp("ttyb", buf) == 0)
1285 cookie = 1;
1286
1287 if (channel == cookie)
1288 sc->sc_flags |= SABTTYF_CONS_IN;
1289 }
1290
1291 /* Default to same channel if there are no explicit prom args */
1292
1293 if (node == prom_instance_to_package(prom_stdout())) {
1294 if (prom_getoption("output-device", buf, sizeof buf) == 0 &&
1295 strcmp("ttyb", buf) == 0)
1296 cookie = 1;
1297
1298 if (channel == cookie)
1299 sc->sc_flags |= SABTTYF_CONS_OUT;
1300 }
1301 }
1302
1303 void
1304 sabtty_shutdown(void *vsc)
1305 {
1306 struct sabtty_softc *sc = vsc;
1307
1308 /* Have to put the chip back into single char mode */
1309 sc->sc_flags |= SABTTYF_DONTDDB;
1310 SAB_WRITE(sc, SAB_RFC, SAB_READ(sc, SAB_RFC) & ~SAB_RFC_RFDF);
1311 sabtty_cec_wait(sc);
1312 SAB_WRITE(sc, SAB_CMDR, SAB_CMDR_RRES);
1313 sabtty_cec_wait(sc);
1314 }
1315
1316 #ifdef KGDB
1317 static int
1318 sab_kgdb_getc(void *arg)
1319 {
1320 struct sabtty_softc *sc = arg;
1321
1322 return sabtty_cngetc(sc);
1323 }
1324
1325 static void
1326 sab_kgdb_putc(void *arg, int c)
1327 {
1328 struct sabtty_softc *sc = arg;
1329
1330 return sabtty_cnputc(sc, c);
1331 }
1332
1333 #ifndef KGDB_DEVRATE
1334 #define KGDB_DEVRATE 9600
1335 #endif
1336
1337 int
1338 sab_kgdb_check(struct sabtty_softc *sc)
1339 {
1340 return strcmp(device_xname(&sc->sc_dv), KGDB_DEVNAME) == 0;
1341 }
1342
1343 void
1344 sab_kgdb_init(struct sabtty_softc *sc)
1345 {
1346 int sp;
1347 uint8_t dafo, r;
1348 extern int kgdb_break_at_attach;
1349
1350 kgdb_attach(sab_kgdb_getc, sab_kgdb_putc, sc);
1351 kgdb_dev = 123; /* not used, but checked against NODEV */
1352
1353 /*
1354 * Configure the port to speed/8n1
1355 */
1356 dafo = SAB_READ(sc, SAB_DAFO);
1357 dafo &= ~(SAB_DAFO_STOP|SAB_DAFO_CHL_CSIZE|SAB_DAFO_PARMASK);
1358 dafo |= SAB_DAFO_CHL_CS8|SAB_DAFO_PAR_NONE;
1359 SAB_WRITE(sc, SAB_DAFO, dafo);
1360 sp = sabtty_speed(KGDB_DEVRATE);
1361 SAB_WRITE(sc, SAB_BGR, sp & 0xff);
1362 r = SAB_READ(sc, SAB_CCR2);
1363 r &= ~(SAB_CCR2_BR9 | SAB_CCR2_BR8);
1364 r |= (sp >> 2) & (SAB_CCR2_BR9 | SAB_CCR2_BR8);
1365 SAB_WRITE(sc, SAB_CCR2, r);
1366
1367 /*
1368 * If we are booting with -d, break into kgdb now
1369 */
1370 if (kgdb_break_at_attach)
1371 kgdb_connect(1);
1372 }
1373 #endif
1374