zs.c revision 1.3 1 /*
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This software was developed by the Computer Systems Engineering group
6 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7 * contributed to Berkeley.
8 *
9 * All advertising materials mentioning features or use of this software
10 * must display the following acknowledgement:
11 * This product includes software developed by the University of
12 * California, Lawrence Berkeley Laboratory.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. All advertising materials mentioning features or use of this software
23 * must display the following acknowledgement:
24 * This product includes software developed by the University of
25 * California, Berkeley and its contributors.
26 * 4. Neither the name of the University nor the names of its contributors
27 * may be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 * @(#)zs.c 8.1 (Berkeley) 7/19/93
43 *
44 * from: Header: zs.c,v 1.30 93/07/19 23:44:42 torek Exp
45 * from: sparc/dev/zs.c,v 1.3 1993/10/13 02:36:44 deraadt Exp
46 * $Id: zs.c,v 1.3 1994/05/04 05:34:14 gwr Exp $
47 */
48
49 /*
50 * Zilog Z8530 (ZSCC) driver.
51 *
52 * Runs two tty ports (ttya and ttyb) on zs0,
53 * and runs a keyboard and mouse on zs1.
54 *
55 * This driver knows far too much about chip to usage mappings.
56 */
57 #define NZS 2 /* XXX */
58
59 #include <sys/systm.h>
60 #include <sys/param.h>
61 #include <sys/proc.h>
62 #include <sys/device.h>
63 #include <sys/conf.h>
64 #include <sys/file.h>
65 #include <sys/ioctl.h>
66 #include <sys/tty.h>
67 #include <sys/time.h>
68 #include <sys/kernel.h>
69 #include <sys/syslog.h>
70 #include <sys/conf.h>
71
72 #include <machine/autoconf.h>
73 #include <machine/cpu.h>
74 #include <machine/obio.h>
75 #include <machine/mon.h>
76 #include <machine/eeprom.h>
77
78 #include <dev/cons.h>
79
80 #include "kbd.h"
81 #include "zsreg.h"
82 #include "zsvar.h"
83
84 #ifdef KGDB
85 #include <machine/remote-sl.h>
86 #endif
87
88 #define ZSMAJOR 12 /* XXX */
89
90 #define ZS_KBD 2 /* XXX */
91 #define ZS_MOUSE 3 /* XXX */
92
93 /* The Sun3 provides a 4.9152 MHz clock to the ZS chips. */
94 #define PCLK (9600 * 512) /* PCLK pin input clock rate */
95
96 /*
97 * Select software interrupt levels.
98 */
99 #define ZSSOFT_PRI 2 /* XXX - Want TTY_PRI */
100 #define ZSHARD_PRI 6 /* Wired on the CPU board... */
101
102 /*
103 * Software state per found chip. This would be called `zs_softc',
104 * but the previous driver had a rather different zs_softc....
105 */
106 struct zsinfo {
107 struct device zi_dev; /* base device */
108 volatile struct zsdevice *zi_zs;/* chip registers */
109 struct zs_chanstate zi_cs[2]; /* channel A and B software state */
110 };
111
112 struct tty *zs_tty[NZS * 2]; /* XXX should be dynamic */
113
114 /* Definition of the driver for autoconfig. */
115 static int zsmatch(struct device *, struct cfdata *, void *);
116 static void zsattach(struct device *, struct device *, void *);
117 struct cfdriver zscd =
118 { NULL, "zs", zsmatch, zsattach, DV_TTY, sizeof(struct zsinfo) };
119
120 /* Interrupt handlers. */
121 static int zshard(int);
122 static int zssoft(int);
123
124 struct zs_chanstate *zslist;
125
126 /* Routines called from other code. */
127 int zsopen(dev_t, int, int, struct proc *);
128 int zsclose(dev_t, int, int, struct proc *);
129 static void zsiopen(struct tty *);
130 static void zsiclose(struct tty *);
131 static void zsstart(struct tty *);
132 void zsstop(struct tty *, int);
133 static int zsparam(struct tty *, struct termios *);
134
135 /* Routines purely local to this driver. */
136 static int zs_getspeed(volatile struct zschan *);
137 static void zs_reset(volatile struct zschan *, int, int);
138 static void zs_modem(struct zs_chanstate *, int);
139 static void zs_loadchannelregs(volatile struct zschan *, u_char *);
140 static u_char zs_read(volatile struct zschan *, u_char);
141 static u_char zs_write(volatile struct zschan *, u_char, u_char);
142
143 /* Console stuff. */
144 static volatile struct zschan *zs_conschan;
145
146 #ifdef KGDB
147 /* KGDB stuff. Must reboot to change zs_kgdbunit. */
148 extern int kgdb_dev, kgdb_rate;
149 static int zs_kgdb_savedspeed;
150 static void zs_checkkgdb(int, struct zs_chanstate *, struct tty *);
151 #endif
152
153 static volatile struct zsdevice *zsaddr[NZS]; /* XXX, but saves work */
154
155 /*
156 * Console keyboard L1-A processing is done in the hardware interrupt code,
157 * so we need to duplicate some of the console keyboard decode state. (We
158 * must not use the regular state as the hardware code keeps ahead of the
159 * software state: the software state tracks the most recent ring input but
160 * the hardware state tracks the most recent ZSCC input.) See also kbd.h.
161 */
162 static struct conk_state { /* console keyboard state */
163 char conk_id; /* true => ID coming up (console only) */
164 char conk_l1; /* true => L1 pressed (console only) */
165 } zsconk_state;
166
167 int zshardscope;
168 int zsshortcuts; /* number of "shortcut" software interrupts */
169
170 /*
171 * Match slave number to zs unit number, so that misconfiguration will
172 * not set up the keyboard as ttya, etc.
173 */
174 static int
175 zsmatch(struct device *parent, struct cfdata *cf, void *aux)
176 {
177 struct obio_cf_loc *obio_loc;
178 caddr_t zs_addr;
179
180 obio_loc = (struct obio_cf_loc *) CFDATA_LOC(cf);
181 zs_addr = (caddr_t) obio_loc->obio_addr;
182 return !obio_probe_byte(zs_addr);
183 }
184
185 /*
186 * Attach a found zs.
187 *
188 * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR
189 * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE?
190 */
191 static void
192 zsattach(struct device *parent, struct device *dev, void *aux)
193 {
194 struct obio_cf_loc *obio_loc = OBIO_LOC(dev);
195 register int zs = dev->dv_unit, unit;
196 register struct zsinfo *zi;
197 register struct zs_chanstate *cs;
198 register volatile struct zsdevice *addr;
199 register struct tty *tp, *ctp;
200 int softcar;
201 static int didintr;
202 caddr_t obio_addr;
203
204 obio_addr = (caddr_t)obio_loc->obio_addr;
205 obio_print(obio_addr, ZSSOFT_PRI);
206 printf(" hwpri %d\n", ZSHARD_PRI);
207
208 if ((addr = zsaddr[zs]) == NULL) {
209 zsaddr[zs] = addr = (struct zsdevice *)
210 obio_alloc(obio_addr, OBIO_ZS_SIZE, OBIO_WRITE);
211 }
212
213 if (!didintr) {
214 didintr = 1;
215 isr_add(ZSSOFT_PRI, zssoft, 0);
216 isr_add(ZSHARD_PRI, zshard, 0);
217 }
218
219 zi = (struct zsinfo *)dev;
220 zi->zi_zs = addr;
221 unit = zs * 2;
222 cs = zi->zi_cs;
223
224 if(!zs_tty[unit])
225 zs_tty[unit] = ttymalloc();
226 tp = zs_tty[unit];
227 if(!zs_tty[unit+1])
228 zs_tty[unit+1] = ttymalloc();
229
230 if (unit == 0) {
231 softcar = 0;
232 } else
233 softcar = dev->dv_cfdata->cf_flags;
234
235 /* link into interrupt list with order (A,B) (B=A+1) */
236 cs[0].cs_next = &cs[1];
237 cs[1].cs_next = zslist;
238 zslist = cs;
239
240 cs->cs_unit = unit;
241 cs->cs_zc = &addr->zs_chan[CHAN_A];
242 cs->cs_speed = zs_getspeed(cs->cs_zc);
243 #ifdef DEBUG
244 mon_printf("zs%da speed %d ", zs, cs->cs_speed);
245 #endif
246 cs->cs_softcar = softcar & 1;
247 #if 0
248 /* XXX - Drop carrier here? -gwr */
249 zs_modem(cs, cs->cs_softcar ? 1 : 0);
250 #endif
251 cs->cs_ttyp = tp;
252 tp->t_dev = makedev(ZSMAJOR, unit);
253 tp->t_oproc = zsstart;
254 tp->t_param = zsparam;
255 /*tp->t_stop = zsstop;*/
256 if (cs->cs_zc == zs_conschan) {
257 /* This unit is the console. */
258 cs->cs_consio = 1;
259 cs->cs_brkabort = 1;
260 cs->cs_softcar = 1;
261 } else {
262 /* Can not run kgdb on the console? */
263 #ifdef KGDB
264 zs_checkkgdb(unit, cs, tp);
265 #endif
266 }
267 if (unit == ZS_KBD) {
268 /*
269 * Keyboard: tell /dev/kbd driver how to talk to us.
270 */
271 tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
272 tp->t_cflag = CS8;
273 kbd_serial(tp, zsiopen, zsiclose);
274 cs->cs_conk = 1; /* do L1-A processing */
275 }
276 unit++;
277 cs++;
278 tp = zs_tty[unit];
279
280 cs->cs_unit = unit;
281 cs->cs_zc = &addr->zs_chan[CHAN_B];
282 cs->cs_speed = zs_getspeed(cs->cs_zc);
283 #ifdef DEBUG
284 mon_printf("zs%db speed %d\n", zs, cs->cs_speed);
285 #endif
286 cs->cs_softcar = softcar & 2;
287 #if 0
288 /* XXX - Drop carrier here? -gwr */
289 zs_modem(cs, cs->cs_softcar ? 1 : 0);
290 #endif
291 cs->cs_ttyp = tp;
292 tp->t_dev = makedev(ZSMAJOR, unit);
293 tp->t_oproc = zsstart;
294 tp->t_param = zsparam;
295 /*tp->t_stop = zsstop;*/
296 if (cs->cs_zc == zs_conschan) {
297 /* This unit is the console. */
298 cs->cs_consio = 1;
299 cs->cs_brkabort = 1;
300 cs->cs_softcar = 1;
301 } else {
302 /* Can not run kgdb on the console? */
303 #ifdef KGDB
304 zs_checkkgdb(unit, cs, tp);
305 #endif
306 }
307 if (unit == ZS_MOUSE) {
308 /*
309 * Mouse: tell /dev/mouse driver how to talk to us.
310 */
311 tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
312 tp->t_cflag = CS8;
313 ms_serial(tp, zsiopen, zsiclose);
314 }
315 }
316
317 /*
318 * Put a channel in a known state. Interrupts may be left disabled
319 * or enabled, as desired.
320 */
321 static void
322 zs_reset(zc, inten, speed)
323 volatile struct zschan *zc;
324 int inten, speed;
325 {
326 int tconst;
327 static u_char reg[16] = {
328 0,
329 0,
330 0,
331 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
332 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
333 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
334 0,
335 0,
336 0,
337 0,
338 ZSWR10_NRZ,
339 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
340 0,
341 0,
342 ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA,
343 ZSWR15_BREAK_IE | ZSWR15_DCD_IE,
344 };
345
346 reg[9] = inten ? ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR : ZSWR9_NO_VECTOR;
347 tconst = BPS_TO_TCONST(PCLK / 16, speed);
348 reg[12] = tconst;
349 reg[13] = tconst >> 8;
350 zs_loadchannelregs(zc, reg);
351 }
352
353 /*
354 * Console support
355 */
356
357 /*
358 * Used by the kd driver to find out if it can work.
359 */
360 int
361 zscnprobe_kbd()
362 {
363 if (zs1_va == NULL) {
364 mon_printf("zscnprobe_kbd: zs1 not yet mapped\n");
365 return CN_DEAD;
366 }
367 return CN_INTERNAL;
368 }
369
370 /*
371 * This is the console probe routine for ttya and ttyb.
372 */
373 static int
374 zscnprobe(struct consdev *cn, int unit)
375 {
376 int maj, eeCons;
377
378 if (zs0_va == NULL) {
379 mon_printf("zscnprobe: zs0 not yet mapped\n");
380 cn->cn_pri = CN_DEAD;
381 return 0;
382 }
383 zsaddr[0] = (struct zsdevice *)zs0_va;
384 /* XXX - Also try to make sure it exists? */
385
386 /* locate the major number */
387 for (maj = 0; maj < nchrdev; maj++)
388 if (cdevsw[maj].d_open == zsopen)
389 break;
390
391 cn->cn_dev = makedev(maj, unit);
392
393 /* Use EEPROM console setting to decide "remote" console. */
394 if (eeprom_va == NULL) {
395 mon_printf("zscnprobe: eeprom not yet mapped\n");
396 eeCons = -1;
397 } else {
398 eeCons = ((struct eeprom *)eeprom_va)->eeConsole;
399 }
400
401 /* Hack: EE_CONS_TTYA + 1 == EE_CONS_TTYB */
402 if (eeCons == (EE_CONS_TTYA + unit)) {
403 cn->cn_pri = CN_REMOTE;
404 } else {
405 cn->cn_pri = CN_NORMAL;
406 }
407 return (0);
408 }
409
410 /* This is the constab entry for TTYA. */
411 int
412 zscnprobe_a(struct consdev *cn)
413 {
414 return (zscnprobe(cn, 0));
415 }
416
417 /* This is the constab entry for TTYB. */
418 int
419 zscnprobe_b(struct consdev *cn)
420 {
421 return (zscnprobe(cn, 1));
422 }
423
424 /* Attach as console. Also set zs_conschan */
425 int
426 zscninit(struct consdev *cn)
427 {
428 int unit;
429 volatile struct zsdevice *addr;
430
431 unit = minor(cn->cn_dev) & 1;
432 addr = (struct zsdevice *)zs0_va;
433 zs_conschan = ((unit == 0) ?
434 &addr->zs_chan[CHAN_A] :
435 &addr->zs_chan[CHAN_B] );
436
437 mon_printf("console on zs0 (tty%c)\n", unit + 'a');
438 }
439
440
441 /*
442 * Polled console input putchar.
443 */
444 int
445 zscngetc()
446 {
447 register volatile struct zschan *zc = zs_conschan;
448 register int s, c;
449
450 if (zc == NULL)
451 return (0);
452
453 s = splhigh();
454 while ((zc->zc_csr & ZSRR0_RX_READY) == 0)
455 ZS_DELAY();
456 ZS_DELAY();
457 c = zc->zc_data;
458 splx(s);
459 return (c);
460 }
461
462 /*
463 * Polled console output putchar.
464 */
465 int
466 zscnputc(c)
467 int c;
468 {
469 register volatile struct zschan *zc = zs_conschan;
470 register int s;
471
472 if (zc == NULL)
473 return (0);
474
475 if (c == '\n')
476 zscnputc('\r');
477 s = splhigh();
478 while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
479 ZS_DELAY();
480 ZS_DELAY();
481 zc->zc_data = c;
482 ZS_DELAY();
483 splx(s);
484 }
485
486 #ifdef KGDB
487 /*
488 * The kgdb zs port, if any, was altered at boot time (see zs_kgdb_init).
489 * Pick up the current speed and character size and restore the original
490 * speed.
491 */
492 static void
493 zs_checkkgdb(int unit, struct zs_chanstate *cs, struct tty *tp)
494 {
495
496 if (kgdb_dev == makedev(ZSMAJOR, unit)) {
497 tp->t_ispeed = tp->t_ospeed = kgdb_rate;
498 tp->t_cflag = CS8;
499 cs->cs_kgdb = 1;
500 cs->cs_speed = zs_kgdb_savedspeed;
501 (void) zsparam(tp, &tp->t_termios);
502 }
503 }
504 #endif
505
506 /*
507 * Compute the current baud rate given a ZSCC channel.
508 */
509 static int
510 zs_getspeed(zc)
511 register volatile struct zschan *zc;
512 {
513 register int tconst;
514
515 tconst = ZS_READ(zc, 12);
516 tconst |= ZS_READ(zc, 13) << 8;
517 return (TCONST_TO_BPS(PCLK / 16, tconst));
518 }
519
520
521 /*
522 * Do an internal open.
523 */
524 static void
525 zsiopen(struct tty *tp)
526 {
527
528 (void) zsparam(tp, &tp->t_termios);
529 ttsetwater(tp);
530 tp->t_state = TS_ISOPEN | TS_CARR_ON;
531 }
532
533 /*
534 * Do an internal close. Eventually we should shut off the chip when both
535 * ports on it are closed.
536 */
537 static void
538 zsiclose(struct tty *tp)
539 {
540
541 ttylclose(tp, 0); /* ??? */
542 ttyclose(tp); /* ??? */
543 tp->t_state = 0;
544 }
545
546
547 /*
548 * Open a zs serial port. This interface may not be used to open
549 * the keyboard and mouse ports. (XXX)
550 */
551 int
552 zsopen(dev_t dev, int flags, int mode, struct proc *p)
553 {
554 register struct tty *tp;
555 register struct zs_chanstate *cs;
556 struct zsinfo *zi;
557 int unit = minor(dev), zs = unit >> 1, error, s;
558
559 #ifdef DEBUG
560 mon_printf("zs_open\n");
561 #endif
562 if (zs >= zscd.cd_ndevs || (zi = zscd.cd_devs[zs]) == NULL ||
563 unit == ZS_KBD || unit == ZS_MOUSE)
564 return (ENXIO);
565 cs = &zi->zi_cs[unit & 1];
566 #if 0
567 /* The kd driver avoids the need for this hack. */
568 if (cs->cs_consio)
569 return (ENXIO); /* ??? */
570 #endif
571 tp = cs->cs_ttyp;
572 s = spltty();
573 if ((tp->t_state & TS_ISOPEN) == 0) {
574 ttychars(tp);
575 if (tp->t_ispeed == 0) {
576 tp->t_iflag = TTYDEF_IFLAG;
577 tp->t_oflag = TTYDEF_OFLAG;
578 tp->t_cflag = TTYDEF_CFLAG;
579 tp->t_lflag = TTYDEF_LFLAG;
580 tp->t_ispeed = tp->t_ospeed = cs->cs_speed;
581 }
582 (void) zsparam(tp, &tp->t_termios);
583 ttsetwater(tp);
584 } else if (tp->t_state & TS_XCLUDE && p->p_ucred->cr_uid != 0) {
585 splx(s);
586 return (EBUSY);
587 }
588 error = 0;
589 #ifdef DEBUG
590 mon_printf("wait for carrier...\n");
591 #endif
592 for (;;) {
593 /* loop, turning on the device, until carrier present */
594 zs_modem(cs, 1);
595 /* May never get status intr if carrier already on. -gwr */
596 if (cs->cs_zc->zc_csr & ZSRR0_DCD)
597 tp->t_state |= TS_CARR_ON;
598 if (cs->cs_softcar)
599 tp->t_state |= TS_CARR_ON;
600 if (flags & O_NONBLOCK || tp->t_cflag & CLOCAL ||
601 tp->t_state & TS_CARR_ON)
602 break;
603 tp->t_state |= TS_WOPEN;
604 if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
605 ttopen, 0))
606 break;
607 }
608 #ifdef DEBUG
609 mon_printf("...carrier %s\n",
610 (tp->t_state & TS_CARR_ON) ? "on" : "off");
611 #endif
612 splx(s);
613 if (error == 0)
614 error = linesw[tp->t_line].l_open(dev, tp);
615 if (error)
616 zs_modem(cs, 0);
617 return (error);
618 }
619
620 /*
621 * Close a zs serial port.
622 */
623 int
624 zsclose(dev_t dev, int flags, int mode, struct proc *p)
625 {
626 register struct zs_chanstate *cs;
627 register struct tty *tp;
628 struct zsinfo *zi;
629 int unit = minor(dev), s;
630
631 #ifdef DEBUG
632 mon_printf("zs_close\n");
633 #endif
634 zi = zscd.cd_devs[unit >> 1];
635 cs = &zi->zi_cs[unit & 1];
636 tp = cs->cs_ttyp;
637 linesw[tp->t_line].l_close(tp, flags);
638 if (tp->t_cflag & HUPCL || tp->t_state & TS_WOPEN ||
639 (tp->t_state & TS_ISOPEN) == 0) {
640 zs_modem(cs, 0);
641 /* hold low for 1 second */
642 (void) tsleep((caddr_t)cs, TTIPRI, ttclos, hz);
643 }
644 ttyclose(tp);
645 #ifdef KGDB
646 /* Reset the speed if we're doing kgdb on this port */
647 if (cs->cs_kgdb) {
648 tp->t_ispeed = tp->t_ospeed = kgdb_rate;
649 (void) zsparam(tp, &tp->t_termios);
650 }
651 #endif
652 return (0);
653 }
654
655 /*
656 * Read/write zs serial port.
657 */
658 int
659 zsread(dev_t dev, struct uio *uio, int flags)
660 {
661 register struct tty *tp = zs_tty[minor(dev)];
662
663 return (linesw[tp->t_line].l_read(tp, uio, flags));
664 }
665
666 int
667 zswrite(dev_t dev, struct uio *uio, int flags)
668 {
669 register struct tty *tp = zs_tty[minor(dev)];
670
671 return (linesw[tp->t_line].l_write(tp, uio, flags));
672 }
673
674 /*
675 * ZS hardware interrupt. Scan all ZS channels. NB: we know here that
676 * channels are kept in (A,B) pairs.
677 *
678 * Do just a little, then get out; set a software interrupt if more
679 * work is needed.
680 *
681 * We deliberately ignore the vectoring Zilog gives us, and match up
682 * only the number of `reset interrupt under service' operations, not
683 * the order.
684 */
685 /* ARGSUSED */
686 int
687 zshard(int intrarg)
688 {
689 register struct zs_chanstate *a;
690 #define b (a + 1)
691 register volatile struct zschan *zc;
692 register int rr3, intflags = 0, v, i;
693 static int zsrint(struct zs_chanstate *, volatile struct zschan *);
694 static int zsxint(struct zs_chanstate *, volatile struct zschan *);
695 static int zssint(struct zs_chanstate *, volatile struct zschan *);
696
697 for (a = zslist; a != NULL; a = b->cs_next) {
698 rr3 = ZS_READ(a->cs_zc, 3);
699
700 /* XXX - This should loop to empty the on-chip fifo. */
701 if (rr3 & (ZSRR3_IP_A_RX|ZSRR3_IP_A_TX|ZSRR3_IP_A_STAT)) {
702 intflags |= 2;
703 zc = a->cs_zc;
704 i = a->cs_rbput;
705 if (rr3 & ZSRR3_IP_A_RX && (v = zsrint(a, zc)) != 0) {
706 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
707 intflags |= 1;
708 }
709 if (rr3 & ZSRR3_IP_A_TX && (v = zsxint(a, zc)) != 0) {
710 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
711 intflags |= 1;
712 }
713 if (rr3 & ZSRR3_IP_A_STAT && (v = zssint(a, zc)) != 0) {
714 a->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
715 intflags |= 1;
716 }
717 a->cs_rbput = i;
718 }
719
720 /* XXX - This should loop to empty the on-chip fifo. */
721 if (rr3 & (ZSRR3_IP_B_RX|ZSRR3_IP_B_TX|ZSRR3_IP_B_STAT)) {
722 intflags |= 2;
723 zc = b->cs_zc;
724 i = b->cs_rbput;
725 if (rr3 & ZSRR3_IP_B_RX && (v = zsrint(b, zc)) != 0) {
726 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
727 intflags |= 1;
728 }
729 if (rr3 & ZSRR3_IP_B_TX && (v = zsxint(b, zc)) != 0) {
730 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
731 intflags |= 1;
732 }
733 if (rr3 & ZSRR3_IP_B_STAT && (v = zssint(b, zc)) != 0) {
734 b->cs_rbuf[i++ & ZLRB_RING_MASK] = v;
735 intflags |= 1;
736 }
737 b->cs_rbput = i;
738 }
739 }
740 #undef b
741 if (intflags & 1) {
742 isr_soft_request(ZSSOFT_PRI);
743 }
744 return (intflags & 2);
745 }
746
747 static int
748 zsrint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
749 {
750 register int c = zc->zc_data;
751
752 if (cs->cs_conk) {
753 register struct conk_state *conk = &zsconk_state;
754
755 /*
756 * Check here for console abort function, so that we
757 * can abort even when interrupts are locking up the
758 * machine.
759 */
760 if (c == KBD_RESET) {
761 conk->conk_id = 1; /* ignore next byte */
762 conk->conk_l1 = 0;
763 } else if (conk->conk_id)
764 conk->conk_id = 0; /* stop ignoring bytes */
765 else if (c == KBD_L1)
766 conk->conk_l1 = 1; /* L1 went down */
767 else if (c == (KBD_L1|KBD_UP))
768 conk->conk_l1 = 0; /* L1 went up */
769 else if (c == KBD_A && conk->conk_l1) {
770 zsabort();
771 conk->conk_l1 = 0; /* we never see the up */
772 goto clearit; /* eat the A after L1-A */
773 }
774 }
775 #ifdef KGDB
776 if (c == FRAME_START && cs->cs_kgdb &&
777 (cs->cs_ttyp->t_state & TS_ISOPEN) == 0) {
778 zskgdb(cs->cs_unit);
779 goto clearit;
780 }
781 #endif
782 /* compose receive character and status */
783 c <<= 8;
784 c |= ZS_READ(zc, 1);
785
786 /* clear receive error & interrupt condition */
787 zc->zc_csr = ZSWR0_RESET_ERRORS;
788 zc->zc_csr = ZSWR0_CLR_INTR;
789
790 return (ZRING_MAKE(ZRING_RINT, c));
791
792 clearit:
793 zc->zc_csr = ZSWR0_RESET_ERRORS;
794 zc->zc_csr = ZSWR0_CLR_INTR;
795 return (0);
796 }
797
798 static int
799 zsxint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
800 {
801 register int i = cs->cs_tbc;
802
803 if (i == 0) {
804 zc->zc_csr = ZSWR0_RESET_TXINT;
805 zc->zc_csr = ZSWR0_CLR_INTR;
806 return (ZRING_MAKE(ZRING_XINT, 0));
807 }
808 cs->cs_tbc = i - 1;
809 zc->zc_data = *cs->cs_tba++;
810 zc->zc_csr = ZSWR0_CLR_INTR;
811 return (0);
812 }
813
814 static int
815 zssint(register struct zs_chanstate *cs, register volatile struct zschan *zc)
816 {
817 register int rr0;
818
819 rr0 = zc->zc_csr;
820 zc->zc_csr = ZSWR0_RESET_STATUS;
821 zc->zc_csr = ZSWR0_CLR_INTR;
822 /*
823 * The chip's hardware flow control is, as noted in zsreg.h,
824 * busted---if the DCD line goes low the chip shuts off the
825 * receiver (!). If we want hardware CTS flow control but do
826 * not have it, and carrier is now on, turn HFC on; if we have
827 * HFC now but carrier has gone low, turn it off.
828 */
829 if (rr0 & ZSRR0_DCD) {
830 if (cs->cs_ttyp->t_cflag & CCTS_OFLOW &&
831 (cs->cs_creg[3] & ZSWR3_HFC) == 0) {
832 cs->cs_creg[3] |= ZSWR3_HFC;
833 ZS_WRITE(zc, 3, cs->cs_creg[3]);
834 }
835 } else {
836 if (cs->cs_creg[3] & ZSWR3_HFC) {
837 cs->cs_creg[3] &= ~ZSWR3_HFC;
838 ZS_WRITE(zc, 3, cs->cs_creg[3]);
839 }
840 }
841 if ((rr0 & ZSRR0_BREAK) && cs->cs_brkabort) {
842 /* Wait for end of break to avoid PROM abort. */
843 while (zc->zc_csr & ZSRR0_BREAK)
844 ZS_DELAY();
845 zsabort();
846 return (0);
847 }
848 return (ZRING_MAKE(ZRING_SINT, rr0));
849 }
850
851 zsabort()
852 {
853 #ifdef DDB
854 Debugger();
855 #else
856 printf("stopping on keyboard abort\n");
857 sun3_rom_abort();
858 #endif
859 }
860
861 #ifdef KGDB
862 /*
863 * KGDB framing character received: enter kernel debugger. This probably
864 * should time out after a few seconds to avoid hanging on spurious input.
865 */
866 zskgdb(int unit)
867 {
868
869 printf("zs%d%c: kgdb interrupt\n", unit >> 1, (unit & 1) + 'a');
870 kgdb_connect(1);
871 }
872 #endif
873
874 /*
875 * Print out a ring or fifo overrun error message.
876 */
877 static void
878 zsoverrun(int unit, long *ptime, char *what)
879 {
880
881 if (*ptime != time.tv_sec) {
882 *ptime = time.tv_sec;
883 log(LOG_WARNING, "zs%d%c: %s overrun\n", unit >> 1,
884 (unit & 1) + 'a', what);
885 }
886 }
887
888 /*
889 * ZS software interrupt. Scan all channels for deferred interrupts.
890 */
891 int
892 zssoft(int arg)
893 {
894 register struct zs_chanstate *cs;
895 register volatile struct zschan *zc;
896 register struct linesw *line;
897 register struct tty *tp;
898 register int get, n, c, cc, unit, s;
899
900 isr_soft_clear(ZSSOFT_PRI);
901
902 for (cs = zslist; cs != NULL; cs = cs->cs_next) {
903 get = cs->cs_rbget;
904 again:
905 n = cs->cs_rbput; /* atomic */
906 if (get == n) /* nothing more on this line */
907 continue;
908 unit = cs->cs_unit; /* set up to handle interrupts */
909 zc = cs->cs_zc;
910 tp = cs->cs_ttyp;
911 line = &linesw[tp->t_line];
912 /*
913 * Compute the number of interrupts in the receive ring.
914 * If the count is overlarge, we lost some events, and
915 * must advance to the first valid one. It may get
916 * overwritten if more data are arriving, but this is
917 * too expensive to check and gains nothing (we already
918 * lost out; all we can do at this point is trade one
919 * kind of loss for another).
920 */
921 n -= get;
922 if (n > ZLRB_RING_SIZE) {
923 zsoverrun(unit, &cs->cs_rotime, "ring");
924 get += n - ZLRB_RING_SIZE;
925 n = ZLRB_RING_SIZE;
926 }
927 while (--n >= 0) {
928 /* race to keep ahead of incoming interrupts */
929 c = cs->cs_rbuf[get++ & ZLRB_RING_MASK];
930 switch (ZRING_TYPE(c)) {
931
932 case ZRING_RINT:
933 c = ZRING_VALUE(c);
934 if (c & ZSRR1_DO)
935 zsoverrun(unit, &cs->cs_fotime, "fifo");
936 cc = c >> 8;
937 if (c & ZSRR1_FE)
938 cc |= TTY_FE;
939 if (c & ZSRR1_PE)
940 cc |= TTY_PE;
941 /*
942 * this should be done through
943 * bstreams XXX gag choke
944 */
945 if (unit == ZS_KBD)
946 kbd_rint(cc);
947 else if (unit == ZS_MOUSE)
948 ms_rint(cc);
949 else
950 line->l_rint(cc, tp);
951 break;
952
953 case ZRING_XINT:
954 /*
955 * Transmit done: change registers and resume,
956 * or clear BUSY.
957 */
958 if (cs->cs_heldchange) {
959 s = splzs();
960 c = zc->zc_csr;
961 if ((c & ZSRR0_DCD) == 0)
962 cs->cs_preg[3] &= ~ZSWR3_HFC;
963 bcopy((caddr_t)cs->cs_preg,
964 (caddr_t)cs->cs_creg, 16);
965 zs_loadchannelregs(zc, cs->cs_creg);
966 splx(s);
967 cs->cs_heldchange = 0;
968 if (cs->cs_heldtbc &&
969 (tp->t_state & TS_TTSTOP) == 0) {
970 cs->cs_tbc = cs->cs_heldtbc - 1;
971 zc->zc_data = *cs->cs_tba++;
972 goto again;
973 }
974 }
975 tp->t_state &= ~TS_BUSY;
976 if (tp->t_state & TS_FLUSH)
977 tp->t_state &= ~TS_FLUSH;
978 else
979 ndflush(&tp->t_outq,
980 (u_char *)cs->cs_tba - tp->t_outq.c_cf);
981 line->l_start(tp);
982 break;
983
984 case ZRING_SINT:
985 /*
986 * Status line change. HFC bit is run in
987 * hardware interrupt, to avoid locking
988 * at splzs here.
989 */
990 c = ZRING_VALUE(c);
991 if ((c ^ cs->cs_rr0) & ZSRR0_DCD) {
992 cc = (c & ZSRR0_DCD) != 0;
993 if (line->l_modem(tp, cc) == 0)
994 zs_modem(cs, cc);
995 }
996 cs->cs_rr0 = c;
997 break;
998
999 default:
1000 log(LOG_ERR, "zs%d%c: bad ZRING_TYPE (%x)\n",
1001 unit >> 1, (unit & 1) + 'a', c);
1002 break;
1003 }
1004 }
1005 cs->cs_rbget = get;
1006 goto again;
1007 }
1008 return (1);
1009 }
1010
1011 int
1012 zsioctl(dev_t dev, int cmd, caddr_t data, int flag, struct proc *p)
1013 {
1014 int unit = minor(dev);
1015 struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1016 register struct tty *tp = zi->zi_cs[unit & 1].cs_ttyp;
1017 register int error;
1018
1019 error = linesw[tp->t_line].l_ioctl(tp, cmd, data, flag, p);
1020 if (error >= 0)
1021 return (error);
1022 error = ttioctl(tp, cmd, data, flag, p);
1023 if (error >= 0)
1024 return (error);
1025
1026 switch (cmd) {
1027
1028 case TIOCSBRK:
1029 /* FINISH ME ... need implicit TIOCCBRK in zsclose as well */
1030
1031 case TIOCCBRK:
1032
1033 case TIOCSDTR:
1034
1035 case TIOCCDTR:
1036
1037 case TIOCMSET:
1038
1039 case TIOCMBIS:
1040
1041 case TIOCMBIC:
1042
1043 case TIOCMGET:
1044
1045 default:
1046 return (ENOTTY);
1047 }
1048 return (0);
1049 }
1050
1051 /*
1052 * Start or restart transmission.
1053 */
1054 static void
1055 zsstart(register struct tty *tp)
1056 {
1057 register struct zs_chanstate *cs;
1058 register int s, nch;
1059 int unit = minor(tp->t_dev);
1060 struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1061
1062 cs = &zi->zi_cs[unit & 1];
1063 s = spltty();
1064
1065 /*
1066 * If currently active or delaying, no need to do anything.
1067 */
1068 if (tp->t_state & (TS_TIMEOUT | TS_BUSY | TS_TTSTOP))
1069 goto out;
1070
1071 /*
1072 * If there are sleepers, and output has drained below low
1073 * water mark, awaken.
1074 */
1075 if (tp->t_outq.c_cc <= tp->t_lowat) {
1076 if (tp->t_state & TS_ASLEEP) {
1077 tp->t_state &= ~TS_ASLEEP;
1078 wakeup((caddr_t)&tp->t_outq);
1079 }
1080 selwakeup(&tp->t_wsel);
1081 }
1082
1083 nch = ndqb(&tp->t_outq, 0); /* XXX */
1084 if (nch) {
1085 register char *p = tp->t_outq.c_cf;
1086
1087 /* mark busy, enable tx done interrupts, & send first byte */
1088 tp->t_state |= TS_BUSY;
1089 (void) splzs();
1090 cs->cs_preg[1] |= ZSWR1_TIE;
1091 cs->cs_creg[1] |= ZSWR1_TIE;
1092 ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
1093 cs->cs_zc->zc_data = *p;
1094 cs->cs_tba = p + 1;
1095 cs->cs_tbc = nch - 1;
1096 } else {
1097 /*
1098 * Nothing to send, turn off transmit done interrupts.
1099 * This is useful if something is doing polled output.
1100 */
1101 (void) splzs();
1102 cs->cs_preg[1] &= ~ZSWR1_TIE;
1103 cs->cs_creg[1] &= ~ZSWR1_TIE;
1104 ZS_WRITE(cs->cs_zc, 1, cs->cs_creg[1]);
1105 }
1106 out:
1107 splx(s);
1108 }
1109
1110 /*
1111 * Stop output, e.g., for ^S or output flush.
1112 */
1113 void
1114 zsstop(register struct tty *tp, int flag)
1115 {
1116 register struct zs_chanstate *cs;
1117 register int s, unit = minor(tp->t_dev);
1118 struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1119
1120 cs = &zi->zi_cs[unit & 1];
1121 s = splzs();
1122 if (tp->t_state & TS_BUSY) {
1123 /*
1124 * Device is transmitting; must stop it.
1125 */
1126 cs->cs_tbc = 0;
1127 if ((tp->t_state & TS_TTSTOP) == 0)
1128 tp->t_state |= TS_FLUSH;
1129 }
1130 splx(s);
1131 }
1132
1133 /*
1134 * Set ZS tty parameters from termios.
1135 *
1136 * This routine makes use of the fact that only registers
1137 * 1, 3, 4, 5, 9, 10, 11, 12, 13, 14, and 15 are written.
1138 */
1139 static int
1140 zsparam(register struct tty *tp, register struct termios *t)
1141 {
1142 int unit = minor(tp->t_dev);
1143 struct zsinfo *zi = zscd.cd_devs[unit >> 1];
1144 register struct zs_chanstate *cs = &zi->zi_cs[unit & 1];
1145 register int tmp, tmp5, cflag, s;
1146
1147 /*
1148 * Because PCLK is only run at 4.9 MHz, the fastest we
1149 * can go is 51200 baud (this corresponds to TC=1).
1150 * This is somewhat unfortunate as there is no real
1151 * reason we should not be able to handle higher rates.
1152 */
1153 tmp = t->c_ospeed;
1154 if (tmp < 0 || (t->c_ispeed && t->c_ispeed != tmp))
1155 return (EINVAL);
1156 if (tmp == 0) {
1157 /* stty 0 => drop DTR and RTS */
1158 zs_modem(cs, 0);
1159 return (0);
1160 }
1161 tmp = BPS_TO_TCONST(PCLK / 16, tmp);
1162 if (tmp < 2)
1163 return (EINVAL);
1164
1165 cflag = t->c_cflag;
1166 tp->t_ispeed = tp->t_ospeed = TCONST_TO_BPS(PCLK / 16, tmp);
1167 tp->t_cflag = cflag;
1168
1169 /*
1170 * Block interrupts so that state will not
1171 * be altered until we are done setting it up.
1172 */
1173 s = splzs();
1174 cs->cs_preg[12] = tmp;
1175 cs->cs_preg[13] = tmp >> 8;
1176 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE;
1177 switch (cflag & CSIZE) {
1178 case CS5:
1179 tmp = ZSWR3_RX_5;
1180 tmp5 = ZSWR5_TX_5;
1181 break;
1182 case CS6:
1183 tmp = ZSWR3_RX_6;
1184 tmp5 = ZSWR5_TX_6;
1185 break;
1186 case CS7:
1187 tmp = ZSWR3_RX_7;
1188 tmp5 = ZSWR5_TX_7;
1189 break;
1190 case CS8:
1191 default:
1192 tmp = ZSWR3_RX_8;
1193 tmp5 = ZSWR5_TX_8;
1194 break;
1195 }
1196
1197 /*
1198 * Output hardware flow control on the chip is horrendous: if
1199 * carrier detect drops, the receiver is disabled. Hence we
1200 * can only do this when the carrier is on.
1201 */
1202 if (cflag & CCTS_OFLOW && cs->cs_zc->zc_csr & ZSRR0_DCD)
1203 tmp |= ZSWR3_HFC | ZSWR3_RX_ENABLE;
1204 else
1205 tmp |= ZSWR3_RX_ENABLE;
1206 cs->cs_preg[3] = tmp;
1207 cs->cs_preg[5] = tmp5 | ZSWR5_TX_ENABLE | ZSWR5_DTR | ZSWR5_RTS;
1208
1209 tmp = ZSWR4_CLK_X16 | (cflag & CSTOPB ? ZSWR4_TWOSB : ZSWR4_ONESB);
1210 if ((cflag & PARODD) == 0)
1211 tmp |= ZSWR4_EVENP;
1212 if (cflag & PARENB)
1213 tmp |= ZSWR4_PARENB;
1214 cs->cs_preg[4] = tmp;
1215 cs->cs_preg[9] = ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR;
1216 cs->cs_preg[10] = ZSWR10_NRZ;
1217 cs->cs_preg[11] = ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD;
1218 cs->cs_preg[14] = ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA;
1219 cs->cs_preg[15] = ZSWR15_BREAK_IE | ZSWR15_DCD_IE;
1220
1221 /*
1222 * If nothing is being transmitted, set up new current values,
1223 * else mark them as pending.
1224 */
1225 if (cs->cs_heldchange == 0) {
1226 if (cs->cs_ttyp->t_state & TS_BUSY) {
1227 cs->cs_heldtbc = cs->cs_tbc;
1228 cs->cs_tbc = 0;
1229 cs->cs_heldchange = 1;
1230 } else {
1231 bcopy((caddr_t)cs->cs_preg, (caddr_t)cs->cs_creg, 16);
1232 zs_loadchannelregs(cs->cs_zc, cs->cs_creg);
1233 }
1234 }
1235 splx(s);
1236 return (0);
1237 }
1238
1239 /*
1240 * Raise or lower modem control (DTR/RTS) signals. If a character is
1241 * in transmission, the change is deferred.
1242 */
1243 static void
1244 zs_modem(struct zs_chanstate *cs, int onoff)
1245 {
1246 int s, bis, and;
1247
1248 if (onoff) {
1249 bis = ZSWR5_DTR | ZSWR5_RTS;
1250 and = ~0;
1251 } else {
1252 bis = 0;
1253 and = ~(ZSWR5_DTR | ZSWR5_RTS);
1254 }
1255 s = splzs();
1256 cs->cs_preg[5] = (cs->cs_preg[5] | bis) & and;
1257 if (cs->cs_heldchange == 0) {
1258 if (cs->cs_ttyp->t_state & TS_BUSY) {
1259 cs->cs_heldtbc = cs->cs_tbc;
1260 cs->cs_tbc = 0;
1261 cs->cs_heldchange = 1;
1262 } else {
1263 cs->cs_creg[5] = (cs->cs_creg[5] | bis) & and;
1264 ZS_WRITE(cs->cs_zc, 5, cs->cs_creg[5]);
1265 }
1266 }
1267 splx(s);
1268 }
1269
1270 /*
1271 * Write the given register set to the given zs channel in the proper order.
1272 * The channel must not be transmitting at the time. The receiver will
1273 * be disabled for the time it takes to write all the registers.
1274 */
1275 static void
1276 zs_loadchannelregs(volatile struct zschan *zc, u_char *reg)
1277 {
1278 int i;
1279
1280 zc->zc_csr = ZSM_RESET_ERR; /* reset error condition */
1281 i = zc->zc_data; /* drain fifo */
1282 i = zc->zc_data;
1283 i = zc->zc_data;
1284 ZS_WRITE(zc, 4, reg[4]);
1285 ZS_WRITE(zc, 10, reg[10]);
1286 ZS_WRITE(zc, 3, reg[3] & ~ZSWR3_RX_ENABLE);
1287 ZS_WRITE(zc, 5, reg[5] & ~ZSWR5_TX_ENABLE);
1288 ZS_WRITE(zc, 1, reg[1]);
1289 ZS_WRITE(zc, 9, reg[9]);
1290 ZS_WRITE(zc, 11, reg[11]);
1291 ZS_WRITE(zc, 12, reg[12]);
1292 ZS_WRITE(zc, 13, reg[13]);
1293 ZS_WRITE(zc, 14, reg[14]);
1294 ZS_WRITE(zc, 15, reg[15]);
1295 ZS_WRITE(zc, 3, reg[3]);
1296 ZS_WRITE(zc, 5, reg[5]);
1297 }
1298
1299 static u_char
1300 zs_read(zc, reg)
1301 volatile struct zschan *zc;
1302 u_char reg;
1303 {
1304 u_char val;
1305
1306 zc->zc_csr = reg;
1307 ZS_DELAY();
1308 val = zc->zc_csr;
1309 ZS_DELAY();
1310 return val;
1311 }
1312
1313 static u_char
1314 zs_write(zc, reg, val)
1315 volatile struct zschan *zc;
1316 u_char reg, val;
1317 {
1318 zc->zc_csr = reg;
1319 ZS_DELAY();
1320 zc->zc_csr = val;
1321 ZS_DELAY();
1322 return val;
1323 }
1324
1325 #ifdef KGDB
1326 /*
1327 * Get a character from the given kgdb channel. Called at splhigh().
1328 */
1329 static int
1330 zs_kgdb_getc(void *arg)
1331 {
1332 register volatile struct zschan *zc = (volatile struct zschan *)arg;
1333
1334 while ((zc->zc_csr & ZSRR0_RX_READY) == 0)
1335 continue;
1336 return (zc->zc_data);
1337 }
1338
1339 /*
1340 * Put a character to the given kgdb channel. Called at splhigh().
1341 */
1342 static void
1343 zs_kgdb_putc(void *arg, int c)
1344 {
1345 register volatile struct zschan *zc = (volatile struct zschan *)arg;
1346
1347 while ((zc->zc_csr & ZSRR0_TX_READY) == 0)
1348 continue;
1349 zc->zc_data = c;
1350 }
1351
1352 /*
1353 * Set up for kgdb; called at boot time before configuration.
1354 * KGDB interrupts will be enabled later when zs0 is configured.
1355 */
1356 void
1357 zs_kgdb_init()
1358 {
1359 volatile struct zsdevice *addr;
1360 volatile struct zschan *zc;
1361 int unit, zs;
1362
1363 if (major(kgdb_dev) != ZSMAJOR)
1364 return;
1365 unit = minor(kgdb_dev);
1366 /*
1367 * Unit must be 0 or 1 (zs0).
1368 */
1369 if ((unsigned)unit >= ZS_KBD) {
1370 printf("zs_kgdb_init: bad minor dev %d\n", unit);
1371 return;
1372 }
1373 zs = unit >> 1;
1374 unit &= 1;
1375
1376 if ((addr = zs0_va) == NULL)
1377 panic("kbdb_attach: zs0 not yet mapped");
1378
1379 zc = unit == 0 ? &addr->zs_chan[CHAN_A] : &addr->zs_chan[CHAN_B];
1380 zs_kgdb_savedspeed = zs_getspeed(zc);
1381 printf("zs_kgdb_init: attaching zs%d%c at %d baud\n",
1382 zs, unit + 'a', kgdb_rate);
1383 zs_reset(zc, 1, kgdb_rate);
1384 kgdb_attach(zs_kgdb_getc, zs_kgdb_putc, (void *)zc);
1385 }
1386 #endif /* KGDB */
1387