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